Skip to content

Commit

Permalink
Added buzzer class
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizoku-oh committed Aug 20, 2023
1 parent 889dea4 commit 35e0a28
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
19 changes: 19 additions & 0 deletions include/Buzzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef Buzzer_h
#define Buzzer_h

#include "Arduino.h"

class Buzzer {

public:
Buzzer(int pin);
void beep();
void beep(int duration);
void stop();

private:
int _pin;

};

#endif // Buzzer_h
1 change: 0 additions & 1 deletion include/Led.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Led {

public:
Led(int pin);
void blink(int blinkRate);
void on();
void off();
void toggle();
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ framework = arduino
monitor_speed = 115200
monitor_filters = direct

lib_deps =
lib_deps =
stm32duino/STM32duino FreeRTOS@^10.3.2
miguelbalboa/MFRC522@^1.4.10
19 changes: 19 additions & 0 deletions src/Buzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "Arduino.h"
#include "Buzzer.h"

Buzzer::Buzzer(int pin) {
pinMode(pin, OUTPUT);
_pin = pin;
}

void Buzzer::beep() {
tone(_pin, 1000, 0xFFFFFFFF);
}

void Buzzer::beep(int duration) {
tone(_pin, 1000, duration);
}

void Buzzer::stop() {
noTone(_pin);
}
7 changes: 0 additions & 7 deletions src/Led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ Led::Led(int pin) {
_pin = pin;
}

void Led::blink(int blinkRate) {
digitalWrite(_pin, HIGH);
delay(blinkRate);
digitalWrite(_pin, LOW);
delay(blinkRate);
}

void Led::on() {
digitalWrite(_pin, HIGH);
}
Expand Down
14 changes: 8 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <Arduino.h>
#include <SPI.h>

#include "MFRC522.h"
#include "Led.h"
#include "Buzzer.h"
#include "MFRC522.h"

#include "FreeRTOS.h"
#include "task.h"
Expand All @@ -17,7 +18,7 @@
#define MFRC522_IRQ_PIN (1)

#define BUZZER_PIN (0)
#define BUZZER_TONE_DURATION_MS (1000)
#define BUZZER_BEEP_DURATION_MS (1000)
#define BUZZER_TONE_FREQUENCY_HZ (1000)

static void vTask1(void *pvParameters);
Expand All @@ -27,6 +28,7 @@ static void vTask3(void *pvParameters);
static void onCardDetected(void);

static MFRC522 mfrc522(MFRC522_SPI_SS_PIN, MFRC522_RST_PIN);
static Buzzer buzzer(BUZZER_PIN);
static byte registerValue = 0x7F;
static volatile bool cardDetected = false;

Expand Down Expand Up @@ -75,7 +77,7 @@ void loop() {
mfrc522.PICC_HaltA();
cardDetected = false;

tone(BUZZER_PIN, BUZZER_TONE_FREQUENCY_HZ, BUZZER_TONE_DURATION_MS);
buzzer.beep(BUZZER_BEEP_DURATION_MS);
}

// Activate reception
Expand All @@ -102,7 +104,7 @@ static void vTask1(void *pvParameters) {

Serial.println("Started Task 1");
for(;;) {
redLED.blink(100);
redLED.toggle();
vTaskDelay(xDelay);
}
}
Expand All @@ -113,7 +115,7 @@ static void vTask2(void *pvParameters) {

Serial.println("Started Task 2");
for(;;) {
greenLED.blink(100);
greenLED.toggle();
vTaskDelay(xDelay);
}
}
Expand All @@ -124,7 +126,7 @@ static void vTask3(void *pvParameters) {

Serial.println("Started Task 3");
for(;;) {
blueLED.blink(100);
blueLED.toggle();
vTaskDelay(xDelay);
}
}
Expand Down

0 comments on commit 35e0a28

Please sign in to comment.