From c4644105a7cb755682c1605677e7b430f7df9bbf Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 14 Dec 2025 00:23:15 -0600 Subject: [PATCH] add drivers and interface and task classes --- src/App.cpp | 16 +++++---------- src/App.hpp | 2 +- src/CMakeLists.txt | 6 +++++- src/DemoTask.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/DemoTask.hpp | 16 +++++++++++++++ src/SsdInterface.cpp | 30 +++++++++++++++++++++++++++++ src/SsdInterface.hpp | 30 +++++++++++++++++++++++++---- src/TaskBase.hpp | 20 +++++++++++++++++++ src/common.h | 8 ++++++++ src/drivers/ssd.c | 16 ++++++++++++--- src/drivers/ssd.h | 33 ++++++++++++++++++++++++++++++- 11 files changed, 202 insertions(+), 21 deletions(-) create mode 100644 src/DemoTask.cpp create mode 100644 src/DemoTask.hpp create mode 100644 src/TaskBase.hpp create mode 100644 src/common.h diff --git a/src/App.cpp b/src/App.cpp index b8ebe65..0320ac9 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -9,23 +9,17 @@ #include "pins.hpp" +#include "DemoTask.hpp" + App::App() { ESP_LOGI(TAG, "App constructor"); } uint32_t App::main() { - ESP_LOGI(TAG, "Example configured to blink GPIO LED!"); - gpio_reset_pin(gpio_onboardLed); - gpio_set_direction(gpio_onboardLed, GPIO_MODE_OUTPUT); + DemoTask demoTask{}; - while (1) { - ESP_LOGI(TAG, "Turning the LED %s!", ledState == true ? "ON" : "OFF"); - gpio_set_level(gpio_onboardLed, ledState); - /* Toggle the LED state */ - ledState = !ledState; - vTaskDelay(blinkTime / portTICK_PERIOD_MS); - } + demoTask.start("BlinkTask", 2048, 5, 1); - return 1; // unreachable + return 1; } diff --git a/src/App.hpp b/src/App.hpp index 0e42fee..cd1acd3 100644 --- a/src/App.hpp +++ b/src/App.hpp @@ -16,6 +16,6 @@ private: uint32_t ledState = 0; uint32_t blinkTime = 250; - const char *TAG = "app"; + const char *TAG = "app"; // TODO: instead of this for logging you can use __FILE__ or __func__ }; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 14d5180..ff8896e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,9 @@ -file(GLOB SRC_FILES "*.c" "*.cpp") +file(GLOB SRC_FILES + "*.c" + "*.cpp" + "drivers/*.c" +) idf_component_register( SRCS ${SRC_FILES} diff --git a/src/DemoTask.cpp b/src/DemoTask.cpp new file mode 100644 index 0000000..13a1d56 --- /dev/null +++ b/src/DemoTask.cpp @@ -0,0 +1,46 @@ + +#include "DemoTask.hpp" + +#include "driver/gpio.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" + +#include "SsdInterface.hpp" +#include "pins.hpp" + +DemoTask::DemoTask() { + +} + +void DemoTask::run() { + + /* + ESP_LOGI(TAG, "Example configured to blink GPIO LED!"); + gpio_reset_pin(gpio_onboardLed); + gpio_set_direction(gpio_onboardLed, GPIO_MODE_OUTPUT); + + while (1) { + ESP_LOGI(TAG, "Turning the LED %s!", ledState == true ? "ON" : "OFF"); + gpio_set_level(gpio_onboardLed, ledState); + ledState = !ledState; + vTaskDelay(blinkTime / portTICK_PERIOD_MS); + } + */ + + ESP_LOGI(__FILE__, "Demo Task"); + + SsdInterface ssd(gpio_ssd_data, gpio_ssd_clk, gpio_ssd_latch, ssdDigits); + uint32_t delay = 500; // ms + uint8_t byte = 0x00; + + while(1) { + ESP_LOGI(__FILE__, "Shifting out byte %02hhx", byte); + + ssd.writeRaw(&byte, 1); + byte++; + + vTaskDelay(delay / portTICK_PERIOD_MS); + } + +} \ No newline at end of file diff --git a/src/DemoTask.hpp b/src/DemoTask.hpp new file mode 100644 index 0000000..afb200e --- /dev/null +++ b/src/DemoTask.hpp @@ -0,0 +1,16 @@ + +#pragma once +#include "TaskBase.hpp" + +class DemoTask : public TaskBase { +public: + DemoTask(); + +protected: + void run() override; + +private: + + const size_t ssdDigits = 4; + +}; diff --git a/src/SsdInterface.cpp b/src/SsdInterface.cpp index e69de29..5fdd61d 100644 --- a/src/SsdInterface.cpp +++ b/src/SsdInterface.cpp @@ -0,0 +1,30 @@ + +#include "SsdInterface.hpp" + +SsdInterface::SsdInterface(const uint8_t dataPin, const uint8_t clockPin, const uint8_t latchPin, size_t numDigits) : numDigits_(numDigits) { + + // create device + static ssd_595_t dev = { (gpio_num_t)dataPin, (gpio_num_t)clockPin, (gpio_num_t)latchPin }; + device_ = &dev; + +} + +STATUS SsdInterface::writeRaw(uint8_t* bytes, size_t numBytes) { + shiftBytes(device_, bytes, numBytes); + return OKAY; +} + +STATUS SsdInterface::write10(int32_t value) { + // TODO: implement + return NOT_IMPLEMENTED; +} + +STATUS SsdInterface::write16(int32_t value) { + // TODO: implement + return NOT_IMPLEMENTED; +} + +STATUS SsdInterface::get(uint8_t* bytes) { + // TODO: implement + return NOT_IMPLEMENTED; +} \ No newline at end of file diff --git a/src/SsdInterface.hpp b/src/SsdInterface.hpp index f7e690f..8521a90 100644 --- a/src/SsdInterface.hpp +++ b/src/SsdInterface.hpp @@ -1,21 +1,43 @@ #pragma once -#include "drivers/ssd.h" #include "stdint.h" +#include "drivers/ssd.h" +#include "common.h" + class SsdInterface { public: - SsdInterface(); + SsdInterface(const uint8_t dataPin, const uint8_t clockPin, const uint8_t latchPinPin, size_t numDigits); ~SsdInterface() = default; - uint32_t write(int32_t value, bool hex); // hex=false => decimal - uint32_t write(int32_t value, uint32_t decimal); + // Outputs the data straight to hardware, mostly for testing purposes + // bytes: the data to write, with bits targetting an led on the ssd + // Returns: execution status + STATUS writeRaw(uint8_t* bytes, size_t numBytes); + + // Displays a decimal integer on the ssd + // value: the integer to display + // Returns: execution status + STATUS write10(int32_t value); + + // Displays a hexadecimal integer on the ssd + // value: the integer to display + // Returns: execution status + STATUS write16(int32_t value); + + // Copies the data currently displayed on the ssd to bytes + // bytes: place to write to + // Returns: execution status + STATUS get(uint8_t* bytes); private: + ssd_595_t* device_; + size_t numDigits_; // number of chained digits + uint8_t* data_; // pointer to the data written }; diff --git a/src/TaskBase.hpp b/src/TaskBase.hpp new file mode 100644 index 0000000..ef9a7a9 --- /dev/null +++ b/src/TaskBase.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +class TaskBase { +public: + + virtual ~TaskBase() = default; + + void start(const char* name, uint32_t stackSize, UBaseType_t priority, BaseType_t core = tskNO_AFFINITY); + +protected: + virtual void run() = 0; + +private: + TaskHandle_t handle = nullptr; + + static void taskEntryPoint(void* param); +}; diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..6559f1d --- /dev/null +++ b/src/common.h @@ -0,0 +1,8 @@ + +#pragma once + +enum STATUS { + OKAY = 0, + ERROR = -1, + NOT_IMPLEMENTED = -2, +}; \ No newline at end of file diff --git a/src/drivers/ssd.c b/src/drivers/ssd.c index 7b6b923..26aa716 100644 --- a/src/drivers/ssd.c +++ b/src/drivers/ssd.c @@ -1,6 +1,12 @@ #include "ssd.h" +#include "esp_rom_sys.h" + +#ifdef __cplusplus +extern "C" { +#endif + inline void pulse(gpio_num_t pin) { gpio_set_level(pin, 1); esp_rom_delay_us(1); @@ -8,7 +14,6 @@ inline void pulse(gpio_num_t pin) { esp_rom_delay_us(1); } - void shiftInit(const ssd_595_t* device) { gpio_config_t ioConfig = { .mode = GPIO_MODE_OUTPUT, @@ -24,7 +29,8 @@ void shiftInit(const ssd_595_t* device) { } void addDecimal(uint8_t* data) { - data = (*data | 0x01); + // TODO: fix + // data = (*data | 0x01); } void shiftByte(const ssd_595_t* device, uint8_t byte) { @@ -39,4 +45,8 @@ void shiftBytes(const ssd_595_t* device, uint8_t* bytes, size_t numBytes) { for(size_t i = 0; i < numBytes; i++) { shiftByte(device, bytes[i]); } -} \ No newline at end of file +} + +#ifdef __cplusplus +} +#endif diff --git a/src/drivers/ssd.h b/src/drivers/ssd.h index 1d8debc..da79d27 100644 --- a/src/drivers/ssd.h +++ b/src/drivers/ssd.h @@ -1,14 +1,25 @@ +#pragma once + #include #include "driver/gpio.h" +#define SSD_DIGIT_MAP_LENGTH 32 + +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { gpio_num_t dataPin; gpio_num_t clockPin; gpio_num_t latchPin; } ssd_595_t; -uint8_t map[17] = { // encoding of digits on the seven segment display +// encoding of digits on the seven segment display +// 0bxxxxxxxx +// ABCDEFG. +static uint8_t digitMap[SSD_DIGIT_MAP_LENGTH] = { 0xFC, // 0 0x60, // 1 0xDA, // 2 @@ -26,8 +37,25 @@ uint8_t map[17] = { // encoding of digits on the seven segment display 0x9E, // E 0x8E, // F 0x02, // - + 0x01, // . + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) + 0x92, // Error code (not implemented) }; +// TODO: have these return error codes likewise + void shiftInit(const ssd_595_t* device); void pulse(gpio_num_t pin); @@ -36,3 +64,6 @@ void addDecimal(uint8_t* data); // adds a decimal to a single digit void shiftByte(const ssd_595_t* device, uint8_t byte); // outputs a serial byte, big-endian void shiftBytes(const ssd_595_t* device, uint8_t* bytes, size_t numBytes); // outputs multiple bytes +#ifdef __cplusplus +} +#endif