From 68842bbe0f32b6240cf78656b79e28f98273a704 Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 7 Dec 2025 16:05:52 -0600 Subject: [PATCH 1/6] update readme --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 24b3a11..ab00571 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ Scripts are in scripts directory, named appropriately. ## Development plan: - [x] Create repository. -- [ ] Build setup, get working hello-world program. -- [ ] Develop 7-segment display driver for the soburg-v2 board, which uses 32-bit shift registers. +- [x] Build setup, get working hello-world program. +- [x] Develop 7-segment display driver for the soburg-v2 board, which uses 32-bit shift registers. Ensures functional hardware interaction and interface design. - [ ] Develop WS2812b driver, would be nice to have some kind of testing for this. - [ ] Develop WS2812b interface. I like the FastLed library for Arduino so might have a lot of similar functionality. @@ -31,7 +31,7 @@ Scripts are in scripts directory, named appropriately. This is basic but hopefully can be good platform for future WS2812b projects. ## Scripts: -After cloning: $ ./scripts/repo-setup # installs esp-idf, sets up target configurations -To build: $ ./scripts/build.sh -To flash: $ ./scripts/flash.sh # note: flash.sh automatically builds -To monitor: $ ./scripts/monitor.sh +After cloning: $ ./scripts/repo-setup # installs esp-idf, sets up target configurations\ +To build: $ ./scripts/build.sh\ +To flash: $ ./scripts/flash.sh # note: flash.sh automatically builds\ +To monitor: $ ./scripts/monitor.sh\ From 8e9d459345ea2b33041b8370defadc8385288b89 Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 7 Dec 2025 18:20:08 -0600 Subject: [PATCH 2/6] add driver --- src/SsdInterface.cpp | 0 src/SsdInterface.hpp | 21 +++++++++++++++++++++ src/drivers/ssd.c | 34 ++++++++++++++++++++++++++++++++++ src/drivers/ssd.h | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 src/SsdInterface.cpp create mode 100644 src/SsdInterface.hpp create mode 100644 src/drivers/ssd.c create mode 100644 src/drivers/ssd.h diff --git a/src/SsdInterface.cpp b/src/SsdInterface.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/SsdInterface.hpp b/src/SsdInterface.hpp new file mode 100644 index 0000000..f7e690f --- /dev/null +++ b/src/SsdInterface.hpp @@ -0,0 +1,21 @@ + +#pragma once + +#include "drivers/ssd.h" +#include "stdint.h" + +class SsdInterface { + +public: + + SsdInterface(); + ~SsdInterface() = default; + + uint32_t write(int32_t value, bool hex); // hex=false => decimal + uint32_t write(int32_t value, uint32_t decimal); + +private: + + + +}; diff --git a/src/drivers/ssd.c b/src/drivers/ssd.c new file mode 100644 index 0000000..079126a --- /dev/null +++ b/src/drivers/ssd.c @@ -0,0 +1,34 @@ + +#include "ssd.h" + +void shiftInit(const ssd_595_t* device) { + gpio_config_t ioConfig = { + .mode = GPIO_MODE_OUTPUT, + .pin_bit_mask = (1ULL << device->dataPin) | + (1ULL << device->clockPin) | + (1ULL << device->latchPin) + }; + gpio_config(&ioConfig); + + gpio_set_level(device->dataPin, 0); + gpio_set_level(device->clockPin, 0); + gpio_set_level(device->latchPin, 0); +} + +void addDecimal(uint8_t* data) { + data = (*data | 0x01); +} + +void shiftByte(const ssd_595_t* device, uint8_t byte) { + for(int i = 0; i < __CHAR_BIT__; i++) { + gpio_set_level(device->dataPin, (byte >> i) & 0x1); + pulse(device->clockPin); + } + pulse(device->latchPin); +} + +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 diff --git a/src/drivers/ssd.h b/src/drivers/ssd.h new file mode 100644 index 0000000..f553084 --- /dev/null +++ b/src/drivers/ssd.h @@ -0,0 +1,37 @@ + +#include +#include "driver/gpio.h" + +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 + 0xFC, // 0 + 0x60, // 1 + 0xDA, // 2 + 0xF2, // 3 + 0x66, // 4 + 0xB6, // 5 + 0xBE, // 6 + 0xE0, // 7 + 0xFE, // 8 + 0xF6, // 9 + 0xEE, // A + 0x3E, // B + 0x9C, // C + 0x7A, // D + 0x9E, // E + 0x8E, // F + 0x02, // - +}; + +void shiftInit(const ssd_595_t* device); + +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 + From 54c9672ba07d27bd93ee723853d4c0eaf9581d8c Mon Sep 17 00:00:00 2001 From: Blitblank Date: Thu, 11 Dec 2025 20:10:35 -0600 Subject: [PATCH 3/6] checkpoint --- src/drivers/ssd.c | 8 ++++++++ src/drivers/ssd.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/drivers/ssd.c b/src/drivers/ssd.c index 079126a..7b6b923 100644 --- a/src/drivers/ssd.c +++ b/src/drivers/ssd.c @@ -1,6 +1,14 @@ #include "ssd.h" +inline void pulse(gpio_num_t pin) { + gpio_set_level(pin, 1); + esp_rom_delay_us(1); + gpio_set_level(pin, 0); + esp_rom_delay_us(1); +} + + void shiftInit(const ssd_595_t* device) { gpio_config_t ioConfig = { .mode = GPIO_MODE_OUTPUT, diff --git a/src/drivers/ssd.h b/src/drivers/ssd.h index f553084..1d8debc 100644 --- a/src/drivers/ssd.h +++ b/src/drivers/ssd.h @@ -29,6 +29,7 @@ uint8_t map[17] = { // encoding of digits on the seven segment display }; void shiftInit(const ssd_595_t* device); +void pulse(gpio_num_t pin); void addDecimal(uint8_t* data); // adds a decimal to a single digit From c4644105a7cb755682c1605677e7b430f7df9bbf Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 14 Dec 2025 00:23:15 -0600 Subject: [PATCH 4/6] 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 From a80e918d63c73a6112dd5bbd11ecb2cbe6b0012e Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 14 Dec 2025 00:23:30 -0600 Subject: [PATCH 5/6] task base --- src/TaskBase.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/TaskBase.cpp diff --git a/src/TaskBase.cpp b/src/TaskBase.cpp new file mode 100644 index 0000000..ca6ff53 --- /dev/null +++ b/src/TaskBase.cpp @@ -0,0 +1,15 @@ + +#include "TaskBase.hpp" + +void TaskBase::start(const char* name, uint32_t stackSize, UBaseType_t priority, BaseType_t core) { + xTaskCreatePinnedToCore(&TaskBase::taskEntryPoint, name, stackSize, this, priority, &handle, core); + return; +} + +void TaskBase::taskEntryPoint(void* param) { + auto* task = static_cast(param); + task->run(); + vTaskDelete(nullptr); + + return; +} From 5cde1bdfceea594f45b1709383ae252141ea540d Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 14 Dec 2025 18:51:52 -0600 Subject: [PATCH 6/6] make ssd driver work --- src/App.cpp | 7 +++++-- src/DemoTask.cpp | 26 +++++++------------------- src/SsdInterface.cpp | 6 ++---- src/SsdInterface.hpp | 4 ++-- src/TaskBase.cpp | 2 ++ src/drivers/ssd.c | 17 ++++++++++++----- src/drivers/ssd.h | 1 + 7 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index 0320ac9..e4dcc62 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -17,9 +17,12 @@ App::App() { uint32_t App::main() { - DemoTask demoTask{}; + ESP_LOGI(__FILE__, "Running App::main()"); - demoTask.start("BlinkTask", 2048, 5, 1); + static DemoTask demoTask{}; + + ESP_LOGI(__FILE__, "Starting DemoTask"); + demoTask.start("DemoTask", 4096, 5, 1); return 1; } diff --git a/src/DemoTask.cpp b/src/DemoTask.cpp index 13a1d56..a5bad7b 100644 --- a/src/DemoTask.cpp +++ b/src/DemoTask.cpp @@ -15,30 +15,18 @@ 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); + ESP_LOGI(__FILE__, "Demo Task: run()"); - 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); - } - */ + ssd_595_t dev = { gpio_ssd_data, gpio_ssd_clk, gpio_ssd_latch, true }; + SsdInterface ssd(&dev, ssdDigits); - 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; + uint8_t digit = 0; while(1) { - ESP_LOGI(__FILE__, "Shifting out byte %02hhx", byte); - - ssd.writeRaw(&byte, 1); - byte++; + ssd.writeRaw(&digitMap[digit], 1); + digit++; + if(digit >= 16) digit = 0; vTaskDelay(delay / portTICK_PERIOD_MS); } diff --git a/src/SsdInterface.cpp b/src/SsdInterface.cpp index 5fdd61d..a0522dc 100644 --- a/src/SsdInterface.cpp +++ b/src/SsdInterface.cpp @@ -1,11 +1,9 @@ #include "SsdInterface.hpp" -SsdInterface::SsdInterface(const uint8_t dataPin, const uint8_t clockPin, const uint8_t latchPin, size_t numDigits) : numDigits_(numDigits) { +SsdInterface::SsdInterface(const ssd_595_t* device, size_t numDigits) : device_(device), numDigits_(numDigits) { - // create device - static ssd_595_t dev = { (gpio_num_t)dataPin, (gpio_num_t)clockPin, (gpio_num_t)latchPin }; - device_ = &dev; + shiftInit(device_); } diff --git a/src/SsdInterface.hpp b/src/SsdInterface.hpp index 8521a90..9387483 100644 --- a/src/SsdInterface.hpp +++ b/src/SsdInterface.hpp @@ -10,7 +10,7 @@ class SsdInterface { public: - SsdInterface(const uint8_t dataPin, const uint8_t clockPin, const uint8_t latchPinPin, size_t numDigits); + SsdInterface(const ssd_595_t* device, size_t numDigits); ~SsdInterface() = default; // Outputs the data straight to hardware, mostly for testing purposes @@ -35,7 +35,7 @@ public: private: - ssd_595_t* device_; + const ssd_595_t* device_; size_t numDigits_; // number of chained digits uint8_t* data_; // pointer to the data written diff --git a/src/TaskBase.cpp b/src/TaskBase.cpp index ca6ff53..e1cedca 100644 --- a/src/TaskBase.cpp +++ b/src/TaskBase.cpp @@ -1,6 +1,8 @@ #include "TaskBase.hpp" +#include "esp_log.h" + void TaskBase::start(const char* name, uint32_t stackSize, UBaseType_t priority, BaseType_t core) { xTaskCreatePinnedToCore(&TaskBase::taskEntryPoint, name, stackSize, this, priority, &handle, core); return; diff --git a/src/drivers/ssd.c b/src/drivers/ssd.c index 26aa716..525f2fb 100644 --- a/src/drivers/ssd.c +++ b/src/drivers/ssd.c @@ -2,6 +2,7 @@ #include "ssd.h" #include "esp_rom_sys.h" +#include "esp_log.h" #ifdef __cplusplus extern "C" { @@ -9,23 +10,28 @@ extern "C" { inline void pulse(gpio_num_t pin) { gpio_set_level(pin, 1); - esp_rom_delay_us(1); gpio_set_level(pin, 0); - esp_rom_delay_us(1); } void shiftInit(const ssd_595_t* device) { + + gpio_reset_pin(device->dataPin); + gpio_reset_pin(device->clockPin); + gpio_reset_pin(device->latchPin); + gpio_config_t ioConfig = { .mode = GPIO_MODE_OUTPUT, .pin_bit_mask = (1ULL << device->dataPin) | (1ULL << device->clockPin) | - (1ULL << device->latchPin) + (1ULL << device->latchPin), }; gpio_config(&ioConfig); gpio_set_level(device->dataPin, 0); gpio_set_level(device->clockPin, 0); gpio_set_level(device->latchPin, 0); + + ESP_LOGI(__FILE__, "ssd_595 initialized"); } void addDecimal(uint8_t* data) { @@ -34,8 +40,9 @@ void addDecimal(uint8_t* data) { } void shiftByte(const ssd_595_t* device, uint8_t byte) { - for(int i = 0; i < __CHAR_BIT__; i++) { - gpio_set_level(device->dataPin, (byte >> i) & 0x1); + for(int i = 0; i < __CHAR_BIT__ ; i++) { + uint32_t level = ((byte >> i) & 0x1) ^ device->commonCathode; + gpio_set_level(device->dataPin, level); pulse(device->clockPin); } pulse(device->latchPin); diff --git a/src/drivers/ssd.h b/src/drivers/ssd.h index da79d27..17cdebd 100644 --- a/src/drivers/ssd.h +++ b/src/drivers/ssd.h @@ -14,6 +14,7 @@ typedef struct { gpio_num_t dataPin; gpio_num_t clockPin; gpio_num_t latchPin; + bool commonCathode; // false = common anode } ssd_595_t; // encoding of digits on the seven segment display