Skip to content

Commit

Permalink
Add thread safety to Log class
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizoku-oh committed Aug 20, 2023
1 parent 243ec1d commit 1fd19bf
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 63 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# resto-device
Embedded project for resto card reader

### Final application vision
``` C
// Clean code

#include <Arduino.h>

#include "Log.h"
#include "Led.h"
#include "Buzzer.h"
#include "CardReader.h"

#define MFRC522_RST_PIN (5)
#define MFRC522_SPI_SS_PIN (10)
#define MFRC522_IRQ_PIN (1)
#define BUZZER_PIN (0)
#define BUZZER_BEEP_DURATION_MS (1000)
#define GREEN_LED_ON_DURATION_MS (1000)

static void onCardDetected(Card card);

static const char *TAG = "MAIN";

static CardReader cardReader(MFRC522_SPI_SS_PIN, MFRC522_RST_PIN, MFRC522_IRQ_PIN);
static Buzzer buzzer(BUZZER_PIN);
static Led greenLED(LED_GREEN);
static Log logger(&Serial);

void setup() {
logger.i(TAG, "App started...");

cardReader.registerCallback(onCardDetected);
cardReader.run();
}

void loop() {}

static void onCardDetected(Card card) {
logger.i(TAG, "UID: " + card.getUID());
greenLED.asyncOn(GREEN_LED_ON_DURATION_MS);
buzzer.asyncOn(BUZZER_BEEP_DURATION_MS);
}
```
4 changes: 4 additions & 0 deletions include/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <Arduino.h>
#include <stdarg.h>
#include "FreeRTOS.h"
#include "semphr.h"

typedef struct {
const char *level;
Expand All @@ -12,6 +14,7 @@ typedef struct {
class Log {
public:
Log(HardwareSerial *serial);
void setup(unsigned long baudrate);
void test(const char *tag, const char *format, ...);
void d(const char *tag, const char *format, ...);
void i(const char *tag, const char *format, ...);
Expand All @@ -20,6 +23,7 @@ class Log {

private:
HardwareSerial *_serial;
SemaphoreHandle_t _mutex;
void printLog(const char *level, const char *tag, const char *format, va_list args);
const char *getColor(const char *level);
};
Expand Down
33 changes: 24 additions & 9 deletions src/Log.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#include <Arduino.h>
#include "Log.h"

config_t config[] =
{
{"TEST", "\033[1;35m"},
{"DEBUG", "\033[1;36m"},
{"INFO", "\033[1;32m"},
{"WARNING", "\033[1;33m"},
{"ERROR", "\033[1;31m"},
#include "FreeRTOS.h"

#define COLOR_MAGENTA "\033[1;35m"
#define COLOR_CYAN "\033[1;36m"
#define COLOR_GREEN "\033[1;32m"
#define COLOR_YELLOW "\033[1;33m"
#define COLOR_RED "\033[1;31m"

const config_t config[] = {
{"TEST", COLOR_MAGENTA},
{"DEBUG", COLOR_CYAN },
{"INFO", COLOR_GREEN },
{"WARNING", COLOR_YELLOW },
{"ERROR", COLOR_RED },
};

Log::Log(HardwareSerial *serial) {
_serial = serial;
_serial->begin(115200);
}

void Log::setup(unsigned long baudrate) {
_serial->begin(baudrate);
while (!(_serial->availableForWrite()));
_mutex = xSemaphoreCreateMutex();
}

void Log::test(const char *tag, const char *format, ...) {
Expand Down Expand Up @@ -56,6 +67,8 @@ void Log::e(const char *tag, const char *format, ...) {
}

void Log::printLog(const char *level, const char *tag, const char *format, va_list args) {
xSemaphoreTake(_mutex, portMAX_DELAY);

// Get the color associated with the level
const char *color = getColor(level);

Expand All @@ -66,6 +79,8 @@ void Log::printLog(const char *level, const char *tag, const char *format, va_li

// Reset color
_serial->printf("\033[0m");

xSemaphoreGive(_mutex);
}

const char* Log::getColor(const char *level) {
Expand Down
100 changes: 46 additions & 54 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define BUZZER_BEEP_DURATION_MS (1000)
#define BUZZER_TONE_FREQUENCY_HZ (1000)

static void vTask1(void *pvParameters);
static void cardReaderTask(void *pvParameters);
static void vTask2(void *pvParameters);
static void vTask3(void *pvParameters);

Expand All @@ -38,17 +38,33 @@ static volatile uint32_t detectionCounter = 0;
const char *TAG = "MAIN";

void setup() {
Serial.begin(SERIAL_BAUDRATE);
logger.setup(SERIAL_BAUDRATE);

xTaskCreate(vTask1, "Task 1", 1024, NULL, 1, NULL);
xTaskCreate(cardReaderTask, "Card Reader Task", 1024, NULL, 1, NULL);
xTaskCreate(vTask2, "Task 2", 1024, NULL, 1, NULL);
xTaskCreate(vTask3, "Task 3", 1024, NULL, 1, NULL);

vTaskStartScheduler();
}

void loop() {
delay(100);
}

void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName) {
while (true);
}

static void cardReaderTask(void *pvParameters) {
const TickType_t xDelay = pdMS_TO_TICKS(100);
Led redLED(LED_RED);
byte byteIndex;

SPI.begin();
mfrc522.PCD_Init();

// Read and printout the MFRC522 version (valid values are 0x91 and 0x92)
logger.i(TAG, "Version: 0x%X", mfrc522.PCD_ReadRegister(mfrc522.VersionReg));
logger.i(TAG, "MFRC522 version: 0x%X", mfrc522.PCD_ReadRegister(mfrc522.VersionReg));

pinMode(MFRC522_IRQ_PIN, INPUT_PULLUP);

Expand All @@ -58,59 +74,35 @@ void setup() {

attachInterrupt(digitalPinToInterrupt(MFRC522_IRQ_PIN), onCardDetected, FALLING);

vTaskStartScheduler();
}

void loop() {
byte byteIndex;

if (cardDetected) {
cardDetected = false;

// Once card is detected read its UID
logger.i(TAG, "Card detected");
logger.d(TAG, "detectionCounter: %lu\r\n", detectionCounter);
mfrc522.PICC_ReadCardSerial();
Serial.print(F("Card UID: "));
for (byteIndex = 0; byteIndex < mfrc522.uid.size; byteIndex++) {
Serial.print(mfrc522.uid.uidByte[byteIndex] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[byteIndex], HEX);
logger.i(TAG, "Started Card Reader Task");
for(;;) {
if (cardDetected) {
cardDetected = false;

// Once card is detected read its UID
logger.i(TAG, "Card detected");
logger.d(TAG, "detectionCounter: %lu\r\n", detectionCounter);
mfrc522.PICC_ReadCardSerial();
Serial.print(F("Card UID: "));
for (byteIndex = 0; byteIndex < mfrc522.uid.size; byteIndex++) {
Serial.print(mfrc522.uid.uidByte[byteIndex] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[byteIndex], HEX);
}
Serial.println();

// Clear pending interrupt
mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
mfrc522.PICC_HaltA();

// Make a sound
buzzer.beep(BUZZER_BEEP_DURATION_MS);
}
Serial.println();

// Clear pending interrupt
mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
mfrc522.PICC_HaltA();

// Make a sound
buzzer.beep(BUZZER_BEEP_DURATION_MS);
}

// Activate reception
mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);

delay(100);
}

void vApplicationIdleHook(void) {
loop();
}

void vApplicationTickHook(void) {
}

void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName) {
for(;;);
}
// Activate reception
mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);

static void vTask1(void *pvParameters) {
const TickType_t xDelay = pdMS_TO_TICKS(500);
Led redLED(LED_RED);

logger.i(TAG, "Started Task 1");
for(;;) {
redLED.toggle();
vTaskDelay(xDelay);
}
Expand Down

0 comments on commit 1fd19bf

Please sign in to comment.