diff --git a/bme280.ino b/bme280.ino index 42f64cf..a04becf 100644 --- a/bme280.ino +++ b/bme280.ino @@ -2,7 +2,7 @@ BME280 sensor; -void initTemperature() { +void initBME280() { sensor.init(); } diff --git a/buzzer.ino b/buzzer.ino new file mode 100644 index 0000000..7edbc85 --- /dev/null +++ b/buzzer.ino @@ -0,0 +1,46 @@ +#include + +SAMDTimer BuzzerTimer(TIMER_TC5); +bool attached = false; + +void initBuzzer() { + pinMode(BUZZER_PIN, OUTPUT); +} + +volatile bool buzzing = false; +void buzzerISR() { + digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); + buzzing = !buzzing; + //digitalToggle(BUZZER_PIN); +} + +void buzz() { + if (attached) + BuzzerTimer.enableTimer(); + else + BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzzerISR); +} + +void noBuzz() { + BuzzerTimer.disableTimer(); +} + +/* +// https://forum.arduino.cc/t/adding-a-toggle-function-to-wiring-digital-c/975608 +void digitalToggle(uint8_t pin) { + uint8_t timer = digitalPinToTimer(pin); + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + volatile uint8_t *out; + + if (port == NOT_A_PIN) return; + if (timer != NOT_ON_TIMER) turnOffPWM(timer); + + out = portOutputRegister(port); + + uint8_t oldSREG = SREG; + cli(); + *out ^= bit; + SREG = oldSREG; +} +*/ diff --git a/metronom.ino b/metronom.ino index fb853f9..e2dc9ac 100644 --- a/metronom.ino +++ b/metronom.ino @@ -2,7 +2,6 @@ #include "SAMD_ISR_Timer.h" SAMDTimer TaskTimer(TIMER_TC3); -SAMDTimer BuzzerTimer(TIMER_TC5); SAMD_ISR_Timer TasksISRs; void TasksHandler(void) { TasksISRs.run(); } @@ -26,40 +25,25 @@ volatile float temperature = 0.0; void setup() { initButton(); - initTemperature(); + initBME280(); + initBuzzer(); initTube(); - pinMode(BUZZER_PIN, OUTPUT); - BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzz); - BuzzerTimer.disableTimer(); - - TaskTimer.attachInterruptInterval_MS(25, TasksHandler); + TaskTimer.attachInterruptInterval_MS(20, TasksHandler); TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, readTemperature); TasksISRs.setInterval(PERIOD_MS, updateTube); } -volatile bool buzzing = false; -void buzz() { - digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); - buzzing = !buzzing; -} - -void stopBuzzer() { - BuzzerTimer.disableTimer(); - digitalWrite(BUZZER_PIN, LOW); -} - void runMetronome() { if (countdown > 0) { countdown -= 1; digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } else if (countdown == 0) { - TasksISRs.setTimeout(100, stopBuzzer); - BuzzerTimer.enableTimer(); + TasksISRs.setTimeout(100, noBuzz); + buzz(); } } -void loop() { -} +void loop() {} diff --git a/tube.ino b/tube.ino index 2050ce3..0eaa44f 100644 --- a/tube.ino +++ b/tube.ino @@ -3,7 +3,7 @@ Seeed_Digital_Tube tube; void initTube() { - // Wire initialized by temperature sensor + // Wire initialized by BME280 sensor tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR); tube.setBrightness(15); tube.setBlinkRate(BLINK_OFF);