buzzer processing code
This commit is contained in:
48
buzzer.c
Normal file
48
buzzer.c
Normal file
@@ -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;
|
||||
}
|
||||
};
|
||||
43
buzzer.h
Normal file
43
buzzer.h
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user