add drivers and interface and task classes

This commit is contained in:
2025-12-14 00:23:15 -06:00
parent 54c9672ba0
commit c4644105a7
11 changed files with 202 additions and 21 deletions

View File

@@ -9,23 +9,17 @@
#include "pins.hpp" #include "pins.hpp"
#include "DemoTask.hpp"
App::App() { App::App() {
ESP_LOGI(TAG, "App constructor"); ESP_LOGI(TAG, "App constructor");
} }
uint32_t App::main() { uint32_t App::main() {
ESP_LOGI(TAG, "Example configured to blink GPIO LED!"); DemoTask demoTask{};
gpio_reset_pin(gpio_onboardLed);
gpio_set_direction(gpio_onboardLed, GPIO_MODE_OUTPUT);
while (1) { demoTask.start("BlinkTask", 2048, 5, 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);
}
return 1; // unreachable return 1;
} }

View File

@@ -16,6 +16,6 @@ private:
uint32_t ledState = 0; uint32_t ledState = 0;
uint32_t blinkTime = 250; uint32_t blinkTime = 250;
const char *TAG = "app"; const char *TAG = "app"; // TODO: instead of this for logging you can use __FILE__ or __func__
}; };

View File

@@ -1,5 +1,9 @@
file(GLOB SRC_FILES "*.c" "*.cpp") file(GLOB SRC_FILES
"*.c"
"*.cpp"
"drivers/*.c"
)
idf_component_register( idf_component_register(
SRCS ${SRC_FILES} SRCS ${SRC_FILES}

46
src/DemoTask.cpp Normal file
View File

@@ -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);
}
}

16
src/DemoTask.hpp Normal file
View File

@@ -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;
};

View File

@@ -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;
}

View File

@@ -1,21 +1,43 @@
#pragma once #pragma once
#include "drivers/ssd.h"
#include "stdint.h" #include "stdint.h"
#include "drivers/ssd.h"
#include "common.h"
class SsdInterface { class SsdInterface {
public: public:
SsdInterface(); SsdInterface(const uint8_t dataPin, const uint8_t clockPin, const uint8_t latchPinPin, size_t numDigits);
~SsdInterface() = default; ~SsdInterface() = default;
uint32_t write(int32_t value, bool hex); // hex=false => decimal // Outputs the data straight to hardware, mostly for testing purposes
uint32_t write(int32_t value, uint32_t decimal); // 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: private:
ssd_595_t* device_;
size_t numDigits_; // number of chained digits
uint8_t* data_; // pointer to the data written
}; };

20
src/TaskBase.hpp Normal file
View File

@@ -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);
};

8
src/common.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
enum STATUS {
OKAY = 0,
ERROR = -1,
NOT_IMPLEMENTED = -2,
};

View File

@@ -1,6 +1,12 @@
#include "ssd.h" #include "ssd.h"
#include "esp_rom_sys.h"
#ifdef __cplusplus
extern "C" {
#endif
inline void pulse(gpio_num_t pin) { inline void pulse(gpio_num_t pin) {
gpio_set_level(pin, 1); gpio_set_level(pin, 1);
esp_rom_delay_us(1); esp_rom_delay_us(1);
@@ -8,7 +14,6 @@ inline void pulse(gpio_num_t pin) {
esp_rom_delay_us(1); esp_rom_delay_us(1);
} }
void shiftInit(const ssd_595_t* device) { void shiftInit(const ssd_595_t* device) {
gpio_config_t ioConfig = { gpio_config_t ioConfig = {
.mode = GPIO_MODE_OUTPUT, .mode = GPIO_MODE_OUTPUT,
@@ -24,7 +29,8 @@ void shiftInit(const ssd_595_t* device) {
} }
void addDecimal(uint8_t* data) { void addDecimal(uint8_t* data) {
data = (*data | 0x01); // TODO: fix
// data = (*data | 0x01);
} }
void shiftByte(const ssd_595_t* device, uint8_t byte) { 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++) { for(size_t i = 0; i < numBytes; i++) {
shiftByte(device, bytes[i]); shiftByte(device, bytes[i]);
} }
} }
#ifdef __cplusplus
}
#endif

View File

@@ -1,14 +1,25 @@
#pragma once
#include <stdint.h> #include <stdint.h>
#include "driver/gpio.h" #include "driver/gpio.h"
#define SSD_DIGIT_MAP_LENGTH 32
#ifdef __cplusplus
extern "C" {
#endif
typedef struct { typedef struct {
gpio_num_t dataPin; gpio_num_t dataPin;
gpio_num_t clockPin; gpio_num_t clockPin;
gpio_num_t latchPin; gpio_num_t latchPin;
} ssd_595_t; } 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 0xFC, // 0
0x60, // 1 0x60, // 1
0xDA, // 2 0xDA, // 2
@@ -26,8 +37,25 @@ uint8_t map[17] = { // encoding of digits on the seven segment display
0x9E, // E 0x9E, // E
0x8E, // F 0x8E, // F
0x02, // - 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 shiftInit(const ssd_595_t* device);
void pulse(gpio_num_t pin); 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 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 void shiftBytes(const ssd_595_t* device, uint8_t* bytes, size_t numBytes); // outputs multiple bytes
#ifdef __cplusplus
}
#endif