From d29f76fa7ccc6f907edc674017ce9734f21bc537 Mon Sep 17 00:00:00 2001 From: "Pawel Spychalski (DzikuVx)" Date: Sat, 28 Oct 2017 16:55:48 +0200 Subject: [PATCH] buzzer processing code --- buzzer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ buzzer.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 buzzer.c create mode 100644 buzzer.h diff --git a/buzzer.c b/buzzer.c new file mode 100644 index 0000000..59c28b9 --- /dev/null +++ b/buzzer.c @@ -0,0 +1,48 @@ +#include "Arduino.h" +#include "buzzer.h" + +void buzzerProcess(uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer) +{ + + if (!buzzer->enabled) + { + digitalWrite(pin, LOW); + return; + } + + if (timestamp > buzzer->updateTime) + { + + int8_t currentPattern = buzzer->pattern[buzzer->mode][buzzer->element]; + + if (currentPattern == PATTERN_CYCLE_OFF) + { + digitalWrite(pin, LOW); + } + else if (currentPattern == PATTERN_CYCLE_ON) + { + digitalWrite(pin, HIGH); + } + else if (currentPattern == PATTERN_CYCLE_IGNORE || currentPattern == buzzer->tick) + { + + if (currentPattern != PATTERN_CYCLE_IGNORE) + { + digitalWrite(pin, !digitalRead(pin)); + } + + buzzer->element++; + if (buzzer->element == PATTERN_ELEMENT_NUMBER) + { + buzzer->element = 0; + } + } + + buzzer->tick++; + if (buzzer->tick >= buzzer->patternMaxTick) + { + buzzer->tick = 0; + } + buzzer->updateTime = timestamp + buzzer->patternTickPerdiod; + } +}; \ No newline at end of file diff --git a/buzzer.h b/buzzer.h new file mode 100644 index 0000000..ec09b37 --- /dev/null +++ b/buzzer.h @@ -0,0 +1,43 @@ +#pragma once + +#include "Arduino.h" + +#define PATTERN_CYCLE_OFF 127 +#define PATTERN_CYCLE_ON -1 +#define PATTERN_CYCLE_IGNORE -2 + +#define PATTERN_MODES_NUMBER 6 +#define PATTERN_ELEMENT_NUMBER 4 + +enum { + BUZZER_MODE_OFF = 0, + BUZZER_MODE_CONTINUOUS = 1, + BUZZER_MODE_SLOW_BEEP = 2, + BUZZER_MODE_FAST_BEEP = 3, + BUZZER_MODE_CHIRP = 4, + BUZZER_MODE_DOUBLE_CHIRP = 5 +}; + +struct BuzzerState_t { + bool enabled = true; + uint8_t mode = BUZZER_MODE_DOUBLE_CHIRP; + + uint32_t updateTime = 0; + + uint8_t tick = 0; + uint8_t element = 0; + const uint8_t patternMaxTick = 20; + const uint8_t patternTickPerdiod = 100; + + const int8_t pattern[PATTERN_MODES_NUMBER][PATTERN_ELEMENT_NUMBER] = { + {PATTERN_CYCLE_OFF}, + {PATTERN_CYCLE_ON}, + {0, 7, 10, 17}, + {0, 4, 10, 14}, + {0, 1, 10, 11}, + {0, 1, 2, 3} + }; + +}; + +void buzzerProcess(uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer); \ No newline at end of file