Added Buzzer and RGB led drivers
This commit is contained in:
70
lib/GPIO/BUZZER_Driver.cpp
Normal file
70
lib/GPIO/BUZZER_Driver.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <BUZZER_Driver.h>
|
||||
|
||||
#define TASK_PRIORITY 20
|
||||
#define TASK_STACK 2048
|
||||
#define OCTAVE 6
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
Buzzer::Buzzer()
|
||||
{
|
||||
LOG_INFO("Initializing Beeper");
|
||||
pinMode(buzzerPin, OUTPUT);
|
||||
ledcAttach(buzzerPin, 1000, 8);
|
||||
m_bp.pin = buzzerPin;
|
||||
m_bp.beeperTask = NULL;
|
||||
beep(50, NOTE_G);
|
||||
}
|
||||
|
||||
Buzzer::~Buzzer()
|
||||
{
|
||||
beepStop();
|
||||
ledcDetach(buzzerPin);
|
||||
pinMode(buzzerPin, INPUT);
|
||||
}
|
||||
|
||||
void Buzzer::beep(const uint16_t tBeep, const note_t note)
|
||||
{
|
||||
beepStop();
|
||||
m_bp.tOn = tBeep;
|
||||
m_bp.tOff = 0;
|
||||
m_bp.note = note;
|
||||
xTaskCreate(beepTask, "beeper", TASK_STACK, static_cast<void *>(&m_bp), TASK_PRIORITY, &m_bp.beeperTask);
|
||||
}
|
||||
|
||||
void Buzzer::beepRepeat(const uint16_t tOn, const uint16_t tOff, const note_t note)
|
||||
{
|
||||
beepStop();
|
||||
m_bp.tOn = tOn;
|
||||
m_bp.tOff = tOff;
|
||||
m_bp.note = note;
|
||||
xTaskCreate(beepTask, "beeper", TASK_STACK, static_cast<void *>(&m_bp), TASK_PRIORITY, &m_bp.beeperTask);
|
||||
}
|
||||
|
||||
void Buzzer::beepStop()
|
||||
{
|
||||
if (m_bp.beeperTask != NULL)
|
||||
vTaskDelete(m_bp.beeperTask);
|
||||
ledcWriteTone(m_bp.pin, 0); // off
|
||||
m_bp.beeperTask = NULL;
|
||||
}
|
||||
|
||||
void Buzzer::beepTask(void *params)
|
||||
{
|
||||
LOG_DEBUG("Beeper Task Created");
|
||||
beep_params_t *bPar = static_cast<beep_params_t *>(params);
|
||||
while (true)
|
||||
{
|
||||
ledcWriteNote(bPar->pin, bPar->note, OCTAVE); // on with selected note
|
||||
vTaskDelay(pdMS_TO_TICKS(bPar->tOn));
|
||||
ledcWriteTone(bPar->pin, 0); // off
|
||||
if (bPar->tOff == 0)
|
||||
break;
|
||||
vTaskDelay(pdMS_TO_TICKS(bPar->tOff));
|
||||
}
|
||||
LOG_DEBUG("Beeper Task Ended");
|
||||
bPar->beeperTask = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
}
|
||||
38
lib/GPIO/BUZZER_Driver.h
Normal file
38
lib/GPIO/BUZZER_Driver.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
#include <DebugLog.h>
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
class Buzzer
|
||||
{
|
||||
const uint8_t buzzerPin = 46; // hardware assigned
|
||||
typedef struct
|
||||
{
|
||||
note_t note;
|
||||
uint8_t pin;
|
||||
uint16_t tOn;
|
||||
uint16_t tOff;
|
||||
TaskHandle_t beeperTask;
|
||||
} beep_params_t;
|
||||
|
||||
public:
|
||||
Buzzer();
|
||||
~Buzzer();
|
||||
|
||||
void beep(const uint16_t tBeep, const note_t note);
|
||||
void beepRepeat(const uint16_t tOn, const uint16_t tOff, const note_t note);
|
||||
void beepStop();
|
||||
|
||||
private:
|
||||
static void beepTask(void *params);
|
||||
|
||||
private:
|
||||
beep_params_t m_bp;
|
||||
};
|
||||
|
||||
}
|
||||
75
lib/GPIO/LED_Driver.cpp
Normal file
75
lib/GPIO/LED_Driver.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <LED_Driver.h>
|
||||
|
||||
#define TASK_PRIORITY 20
|
||||
#define TASK_STACK 2048
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
Led::Led()
|
||||
{
|
||||
LOG_INFO("Inizializing RGB Led");
|
||||
pinMode(ledPin, OUTPUT);
|
||||
m_lp.pin = ledPin;
|
||||
m_lp.blinkTask = NULL;
|
||||
}
|
||||
|
||||
Led::~Led()
|
||||
{
|
||||
setColor({0, 0, 0});
|
||||
pinMode(ledPin, INPUT);
|
||||
}
|
||||
|
||||
void Led::setColor(const color_t color)
|
||||
{
|
||||
blinkStop();
|
||||
rgbLedWrite(ledPin, color.r, color.g, color.b);
|
||||
}
|
||||
|
||||
void Led::blinkColor(const uint16_t tOn, const uint16_t tOff, const color_t color)
|
||||
{
|
||||
blinkStop();
|
||||
m_lp.color1 = color;
|
||||
m_lp.color2 = {0, 0, 0};
|
||||
m_lp.tOn = tOn;
|
||||
m_lp.tOff = tOff;
|
||||
xTaskCreate(blinkTask, "blinker", TASK_STACK, static_cast<void *>(&m_lp), TASK_PRIORITY, &m_lp.blinkTask);
|
||||
}
|
||||
|
||||
void Led::blinkAlternate(const uint16_t tOn, const uint16_t tOff, const color_t color1, const color_t color2)
|
||||
{
|
||||
{
|
||||
blinkStop();
|
||||
m_lp.color1 = color1;
|
||||
m_lp.color2 = color2;
|
||||
m_lp.tOn = tOn;
|
||||
m_lp.tOff = tOff;
|
||||
xTaskCreate(blinkTask, "blinker", TASK_STACK, static_cast<void *>(&m_lp), TASK_PRIORITY, &m_lp.blinkTask);
|
||||
}
|
||||
}
|
||||
|
||||
void Led::blinkStop()
|
||||
{
|
||||
if (m_lp.blinkTask != NULL)
|
||||
vTaskDelete(m_lp.blinkTask);
|
||||
m_lp.blinkTask = NULL;
|
||||
}
|
||||
|
||||
void Led::blinkTask(void *params)
|
||||
{
|
||||
LOG_DEBUG("Blinker Task Created");
|
||||
led_params_t *lPar = static_cast<led_params_t *>(params);
|
||||
while (true)
|
||||
{
|
||||
rgbLedWrite(lPar->pin, lPar->color1.g, lPar->color1.r, lPar->color1.b);
|
||||
vTaskDelay(pdMS_TO_TICKS(lPar->tOn));
|
||||
rgbLedWrite(lPar->pin, lPar->color2.g, lPar->color2.r, lPar->color2.b); // off
|
||||
if (lPar->tOff == 0)
|
||||
break;
|
||||
vTaskDelay(pdMS_TO_TICKS(lPar->tOff));
|
||||
}
|
||||
LOG_DEBUG("Blinker Task Ended");
|
||||
lPar->blinkTask = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
}
|
||||
50
lib/GPIO/LED_Driver.h
Normal file
50
lib/GPIO/LED_Driver.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
#include <DebugLog.h>
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
class Led
|
||||
{
|
||||
const uint8_t ledPin = 38;
|
||||
|
||||
public:
|
||||
typedef struct
|
||||
{
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
} color_t;
|
||||
|
||||
private:
|
||||
typedef struct
|
||||
{
|
||||
color_t color1;
|
||||
color_t color2;
|
||||
uint8_t pin;
|
||||
uint16_t tOn;
|
||||
uint16_t tOff;
|
||||
TaskHandle_t blinkTask;
|
||||
} led_params_t;
|
||||
|
||||
public:
|
||||
Led();
|
||||
~Led();
|
||||
|
||||
void setColor(const color_t color);
|
||||
void blinkColor(const uint16_t tOn, const uint16_t tOff, const color_t color);
|
||||
void blinkAlternate(const uint16_t tOn, const uint16_t tOff, const color_t color1, const color_t color2);
|
||||
void blinkStop();
|
||||
|
||||
private:
|
||||
static void blinkTask(void *params);
|
||||
|
||||
private:
|
||||
led_params_t m_lp;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
robtillaart/CRC@^1.0.3
|
||||
hideakitai/DebugLog@^0.8.4
|
||||
build_type = release
|
||||
|
||||
[env:esp32-s3-waveshare8-debug]
|
||||
platform = ${env:esp32-s3-waveshare8.platform}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <remoteIO.h>
|
||||
#include <TCA9554PWR_Driver.h>
|
||||
|
||||
|
||||
96
src/main.cpp
96
src/main.cpp
@@ -8,6 +8,8 @@
|
||||
#include <PCF85063_Driver.h>
|
||||
#include <R4DCB08_Driver.h>
|
||||
#include <S50140_Driver.h>
|
||||
#include <BUZZER_Driver.h>
|
||||
#include <LED_Driver.h>
|
||||
#include <ETH_Driver.h>
|
||||
|
||||
#include <digitalIO.h>
|
||||
@@ -50,6 +52,7 @@ void loop()
|
||||
uint16_t k(0);
|
||||
uint8_t ethRetries(0);
|
||||
uint8_t sensors(0);
|
||||
bool buzzing(false);
|
||||
|
||||
//////////////// DEVICES ////////////////
|
||||
// Declared here to keep devices local to the main loop otherwise the kernel crashes //
|
||||
@@ -62,33 +65,56 @@ void loop()
|
||||
auto io = digitalIO(i2c, bus, {relayBoardAddr});
|
||||
delay(100);
|
||||
auto seneca = drivers::S50140(bus, senecaMeterAddr);
|
||||
|
||||
Network.onEvent([ð](arduino_event_id_t event, arduino_event_info_t info)
|
||||
{ eth.onEvent(event, info); });
|
||||
|
||||
while (!eth.isConnected() && ethRetries++ < 5)
|
||||
{
|
||||
LOG_WARN("Waiting for Ethernet retry", ethRetries);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
auto buzzer = drivers::Buzzer();
|
||||
auto led = drivers::Led();
|
||||
//////////////// DEVICES ////////////////
|
||||
// Initialize temperature sensors
|
||||
sensors = tmp.getNum();
|
||||
LOG_INFO("Temperature sensors connected ->", sensors);
|
||||
|
||||
// Get RTC time at startup
|
||||
time_t ntpTime;
|
||||
drivers::PCF85063::datetime_t dt;
|
||||
|
||||
// MQTT Test
|
||||
//////////////// NETWORK ////////////////
|
||||
// MQTT Test //
|
||||
NetworkClient tcp;
|
||||
PubSubClient mqtt(tcp);
|
||||
|
||||
mqtt.setServer("10.0.2.249", 1883);
|
||||
mqtt.setCallback(callback);
|
||||
mqtt.connect("esp32-client");
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
//////////////// NETWORK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
Network.onEvent(
|
||||
[ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
|
||||
{
|
||||
eth.onEvent(event, info); // Arduino Ethernet event handler
|
||||
if (!eth.isConnected())
|
||||
return;
|
||||
// Get RTC time at ethernet connection
|
||||
time_t ntpTime;
|
||||
uint8_t timeRetries(0);
|
||||
uint8_t mqttRetries(0);
|
||||
while (timeRetries++ < 5)
|
||||
{
|
||||
if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime)))
|
||||
{
|
||||
buzzer.beep(250, NOTE_F);
|
||||
led.setColor({255, 255, 0});
|
||||
const drivers::PCF85063::datetime_t dt(drivers::PCF85063::fromEpoch(ntpTime));
|
||||
LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str());
|
||||
delay(100);
|
||||
}
|
||||
break;
|
||||
}
|
||||
while (mqttRetries++ < 5)
|
||||
{
|
||||
if (!mqtt.connected() && mqtt.connect("esp32-client"))
|
||||
{
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
break;
|
||||
}
|
||||
delay(100);
|
||||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
///////// MAIN LOOP INSIDE LOOP ////////
|
||||
@@ -98,11 +124,9 @@ void loop()
|
||||
{
|
||||
LOG_INFO("[", k++, "] Loop");
|
||||
|
||||
eth.getNtpTime(ntpTime);
|
||||
dt = drivers::PCF85063::fromEpoch(ntpTime);
|
||||
LOG_INFO("Network Datetime", rtc.datetime2str(dt).c_str());
|
||||
LOG_INFO("Current Datetime", rtc.getTimeStr().c_str());
|
||||
mqtt.publish("test/esp32-out", ("[" + std::to_string(k) + "] -> " + rtc.getTimeStr()).c_str());
|
||||
const std::string timeStr(rtc.getTimeStr());
|
||||
LOG_INFO("Current Datetime", timeStr.c_str());
|
||||
mqtt.publish("test/esp32-out", ("[" + std::to_string(k) + "] -> " + timeStr).c_str());
|
||||
|
||||
uint8_t i(0);
|
||||
for (auto v : tmp.getTempAll())
|
||||
@@ -116,12 +140,7 @@ void loop()
|
||||
LOG_INFO("Temperature correction channel", i++, "tc", v);
|
||||
}
|
||||
|
||||
for (auto j(0); j < io.getOutNum(); j++)
|
||||
{
|
||||
LOG_INFO("Input", j, io.digitalIORead(j) ? "True" : "False");
|
||||
delay(50);
|
||||
}
|
||||
|
||||
delay(100);
|
||||
drivers::S50140::powerinfo_t pinfo = seneca.getAll();
|
||||
LOG_INFO("Power Info ==> V:", pinfo.v, "- A:", pinfo.a, "- W:", pinfo.pAct, "- F:", pinfo.f, "- Wh_t:", pinfo.whTot, "- Wh_p:", pinfo.whPar);
|
||||
|
||||
@@ -133,6 +152,23 @@ void loop()
|
||||
LOG_INFO("Counter Status: ", countStat);
|
||||
seneca.resetPartialCounters();
|
||||
}
|
||||
delay(100);
|
||||
if (io.digitalIORead(8))
|
||||
{
|
||||
if (!buzzing)
|
||||
{
|
||||
buzzing = true;
|
||||
buzzer.beepRepeat(100, 1000, NOTE_C);
|
||||
led.blinkColor(100, 500, {255, 0, 255});
|
||||
}
|
||||
else
|
||||
{
|
||||
buzzer.beepStop();
|
||||
led.blinkAlternate(500, 500, {255, 255, 0}, {0, 255, 255});
|
||||
buzzing = false;
|
||||
}
|
||||
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <RS485_Driver.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user