Extract buzzer to separate file
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
BME280 sensor;
|
||||
|
||||
void initTemperature() {
|
||||
void initBME280() {
|
||||
sensor.init();
|
||||
}
|
||||
|
||||
|
||||
46
buzzer.ino
Normal file
46
buzzer.ino
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
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;
|
||||
}
|
||||
*/
|
||||
28
metronom.ino
28
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() {}
|
||||
|
||||
Reference in New Issue
Block a user