diff --git a/code/main/CMakeLists.txt b/code/main/CMakeLists.txt index 38431da..8cc51ab 100644 --- a/code/main/CMakeLists.txt +++ b/code/main/CMakeLists.txt @@ -9,8 +9,9 @@ idf_component_register( "hal/src/gpio.cpp" "hal/src/uart.cpp" + "platform/src/isl29125.cpp" "platform/src/logger.cpp" -# "old/src/wifi.cpp" + "platform/src/wifi.cpp" # "old/src/led_strip_encoder.c" # "old/src/ledmatrix.cpp" # "old/src/clock.cpp" diff --git a/code/main/main.cpp b/code/main/main.cpp index 3c15e08..c67096f 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -33,7 +33,9 @@ #include "uart.h" // Platform includes +#include "isl29125.h" #include "logger.h" +#include "Wifi.h" // -------------------------------------------------------------------------------------------------------------------- // Constant and macro definitions // -------------------------------------------------------------------------------------------------------------------- @@ -48,6 +50,7 @@ // File-scope variables // -------------------------------------------------------------------------------------------------------------------- + static TaskHandle_t loggerTaskHandle; // -------------------------------------------------------------------------------------------------------------------- // Function declarations @@ -84,13 +87,14 @@ extern "C" void app_main(void) .rx_flow_ctrl_thresh = 0, .source_clk = UART_SCLK_DEFAULT }; - static uart_port_t debugUart = UART_NUM_0; + uart_port_t debugUart = UART_NUM_0; ESP_ERROR_CHECK(uart_param_config(debugUart, &uartConfig)); ESP_ERROR_CHECK(uart_set_pin(debugUart, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); ESP_ERROR_CHECK(uart_driver_install(debugUart, 1024, 1024, 0, NULL, 0)); uart uartDebug = uart(&debugUart); uartDebug.open(); + uartDebug.write(255, 255, (uint8_t*)"START", 5); // ----------------------------------------------------------------------------------------------------------------- // System-wide Debug Logger @@ -114,8 +118,8 @@ extern "C" void app_main(void) i2c_config_t i2cConfig = { .mode = I2C_MODE_MASTER, - .sda_io_num = (int)4, - .scl_io_num = (int)5, + .sda_io_num = (int)2, + .scl_io_num = (int)3, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE, .master = 400000, @@ -127,19 +131,32 @@ extern "C" void app_main(void) i2c i2cSensor = i2c(&i2c_master_port); i2cSensor.open(); - uint8_t i2cData[2] = {0x12, 0x34}; - i2cSensor.write(0x40, 0x00, i2cData, 2); + // ----------------------------------------------------------------------------------------------------------------- + // I2C RGB Sensor on I2C MasterBus for sensors + // + isl29125 rgbSensor = isl29125(0x44, i2cSensor); + rgbSensor.initialize(); + vTaskDelay(100); + rgbSensor.setMode(isl29125::RGB); + rgbSensor.setRange(isl29125::HIGH); + rgbSensor.setResolution(isl29125::RES_16BIT); + vTaskDelay(100); + struct isl29125::rgb_t rgbValue; + rgbSensor.getRGB(&rgbValue); + gpio debugIO = gpio(19, gpio::Direction_t::GPIO_DIRECTION_OUTPUT, gpio::Value_t::GPIO_VALUE_LOW); - gpio debugIO = gpio(2, gpio::Direction_t::GPIO_DIRECTION_OUTPUT, gpio::Value_t::GPIO_VALUE_LOW); - - + // ----------------------------------------------------------------------------------------------------------------- + // Wifi create and connect + // + Wifi wifi; + wifi.start_client(); while(1) { - vTaskDelay(100); + vTaskDelay(1000); } } diff --git a/code/main/platform/inc/isl29125.h b/code/main/platform/inc/isl29125.h new file mode 100644 index 0000000..8611eca --- /dev/null +++ b/code/main/platform/inc/isl29125.h @@ -0,0 +1,216 @@ +// -------------------------------------------------------------------------------------------------------------------- +/// \file isl29125.h +/// \brief File description +// -------------------------------------------------------------------------------------------------------------------- +// +// vbchaos software design +// +// -------------------------------------------------------------------------------------------------------------------- +/// $Revision: $ +/// $Author: $ +/// $Date: $ +// (c) 2023 vbchaos +// -------------------------------------------------------------------------------------------------------------------- + + +#ifndef MAIN_PLATFORM_INC_ISL29125_H_ +#define MAIN_PLATFORM_INC_ISL29125_H_ + +/** + * isl29125 implementation + * \defgroup isl29125 + * \brief {group_description} + * \addtogroup {Layer} + * + * Detailed description + * @{ + */ + + + +// -------------------------------------------------------------------------------------------------------------------- +// Include files +// -------------------------------------------------------------------------------------------------------------------- + +// CompilerIncludes +// All include files that are provided by the compiler directly +#include + + +// ProjectIncludes +// All include files that are provided by the project +#include "ISerialBus.h" + +// -------------------------------------------------------------------------------------------------------------------- +// Constant and macro definitions +// -------------------------------------------------------------------------------------------------------------------- + + +// -------------------------------------------------------------------------------------------------------------------- +// Type definitions. +// -------------------------------------------------------------------------------------------------------------------- + + + +// -------------------------------------------------------------------------------------------------------------------- +// Function declarations +// -------------------------------------------------------------------------------------------------------------------- + +class isl29125 +{ + // ----------------------------------------------------------------------------------------------------------------- + // Public Section + // ----------------------------------------------------------------------------------------------------------------- + public: + + struct rgb_t + { + uint16_t red; + uint16_t green; + uint16_t blue; + } RGB_t; + + typedef enum + { + POWER_DOWN = 0, + GREEN = 1, + RED = 2, + BLUE = 3, + STANDBY = 4, + RGB = 5, + GREEN_RED = 6, + GREEN_BLUE = 7 + } Mode_t; + + typedef enum + { + LOW = 0, + HIGH = 1 + } Range_t; + + typedef enum + { + RES_12BIT = 0, + RES_16BIT = 1 + } Resolution_t; + + const uint8_t deviceID = 0x7D; + + // Class Constructor + isl29125(uint8_t slaveAddress, ISerialBus& serialPort); + FunctionStatus initialize(void); + + FunctionStatus setMode(Mode_t mode); + FunctionStatus setRange(Range_t range); + FunctionStatus setResolution(Resolution_t resolutionß); + + FunctionStatus getRGB(struct rgb_t* rgb); + + // ----------------------------------------------------------------------------------------------------------------- + // Protected Section + // ----------------------------------------------------------------------------------------------------------------- + protected: + + + // ----------------------------------------------------------------------------------------------------------------- + // Private Section + // ----------------------------------------------------------------------------------------------------------------- + private: + + struct __attribute__ ((packed)) memorymap + { + uint8_t device_id; + struct + { + uint8_t mode :3; + uint8_t range :1; + uint8_t bits :1; + uint8_t sync :1; + uint8_t RESERVERED :2; + } configuration1; + struct + { + uint8_t alscc :6; + uint8_t RESERVED :1; + uint8_t ircom :1; + } configuration2; + struct + { + uint8_t intsel :2; + uint8_t prst :2; + uint8_t conven :1; + uint8_t RESERVED :3; + } configuration3; + struct + { + uint8_t lowByte; + uint8_t highByte; + } lowThreshold; + struct + { + uint8_t lowByte; + uint8_t highByte; + } highThreshold; + struct + { + uint8_t RESERVED1 :2; + uint8_t grbcf :2; + uint8_t RESERVED2 :1; + uint8_t boutf :1; + uint8_t convenf :1; + uint8_t rgbthf :1; + } statusFlags; + union + { + uint16_t word; + struct + { + uint8_t lowByte; + uint8_t highByte; + }; + } greenData; + union + { + uint16_t word; + struct + { + uint8_t lowByte; + uint8_t highByte; + }; + } redData; + union + { + uint16_t word; + struct + { + uint8_t lowByte; + uint8_t highByte; + }; + } blueData; + }; + + struct memorymap memorymap; + + uint8_t slaveAddress; + ISerialBus& bus; + bool initialized; + + // Reads the device ID directly into the memory map + FunctionStatus getDeviceID(void); + // Reads all configuration registers into the memory map + FunctionStatus getConfiguration(void); + // Reads all Threshold registers into the memory map + FunctionStatus getTheshold(void); + // Reads the status register into the memory map + FunctionStatus getStatusFlags(void); + // Reads the RGB data registers into the memory map + FunctionStatus getRGBRegisters(void); + // Read the full memory map from device into the local memory map, which creates a perfect memory copy + FunctionStatus getCompleteRegisterMap(void); + +}; + +/** @} */ + + +#endif /* MAIN_PLATFORM_INC_ISL29125_H_ */ diff --git a/code/main/platform/inc/logger.h b/code/main/platform/inc/logger.h index 5616374..c8daf80 100644 --- a/code/main/platform/inc/logger.h +++ b/code/main/platform/inc/logger.h @@ -53,7 +53,7 @@ #if defined(ENABLE_SERIAL_LOGGING) #define LOGGER_LOG(severity,...) \ - debugLogger.log(__FILE__, __func__, __LINE__, severity, ##__VA_ARGS__) + logger::log(__FILE__, __func__, __LINE__, severity, ##__VA_ARGS__) #else #define LOGGER_LOG(severity, message) #endif @@ -98,9 +98,8 @@ // Type definitions. // -------------------------------------------------------------------------------------------------------------------- - -class logger; -extern logger debugLogger; +//class logger; +//extern logger debugLogger; // -------------------------------------------------------------------------------------------------------------------- // Function declarations @@ -126,7 +125,7 @@ class logger // Class Constructor logger(uint32_t queuesize, ISerialBus& serialPort); - FunctionStatus log(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* format, ...); + static FunctionStatus log(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* format, ...); // The Logger task - should be called by the system scheduler regularly void task(); @@ -160,18 +159,20 @@ class logger void composeTypeParameterList(void); - void composeLogQueueItem(struct LogQueueItem* logQueueItem, std::string fileName, std::string functionName, - int lineNumber, LogType logType, std::string context); + static void composeLogQueueItem(struct LogQueueItem* logQueueItem, const std::string& fileName, const std::string& functionName, + int lineNumber, LogType logType, const std::string& context); std::list typeParameterList; - std::list queue; + static std::list queue; ISerialBus& port; - uint32_t queuesize; - uint32_t numberOfLostMessages; + static uint32_t queuesize; + static uint32_t numberOfLostMessages; + static bool overflowRecovery; }; + /** @} */ diff --git a/code/main/old/inc/wifi.h b/code/main/platform/inc/wifi.h similarity index 96% rename from code/main/old/inc/wifi.h rename to code/main/platform/inc/wifi.h index 5397694..2185aa5 100644 --- a/code/main/old/inc/wifi.h +++ b/code/main/platform/inc/wifi.h @@ -34,12 +34,12 @@ // CompilerIncludes // All include files that are provided by the compiler directly -#include "esp_system.h" +//#include "esp_system.h" #include "esp_event.h" -#include "esp_log.h" - +//#include "esp_log.h" +// #include "freertos/FreeRTOS.h" -#include "freertos/task.h" +//#include "freertos/task.h" #include "freertos/event_groups.h" diff --git a/code/main/platform/src/isl29125.cpp b/code/main/platform/src/isl29125.cpp new file mode 100644 index 0000000..f0b8880 --- /dev/null +++ b/code/main/platform/src/isl29125.cpp @@ -0,0 +1,195 @@ +// -------------------------------------------------------------------------------------------------------------------- +/// \file isl29125.cpp +/// \brief Description +// -------------------------------------------------------------------------------------------------------------------- +// +// vbchaos software design +// +// -------------------------------------------------------------------------------------------------------------------- +/// $Revision: $ +/// $Author: $ +/// $Date: $ +// (c) 2023 vbchaos +// -------------------------------------------------------------------------------------------------------------------- + + +// -------------------------------------------------------------------------------------------------------------------- +// Include files +// -------------------------------------------------------------------------------------------------------------------- + +#include + + +// -------------------------------------------------------------------------------------------------------------------- +// Constant and macro definitions +// -------------------------------------------------------------------------------------------------------------------- + + +// -------------------------------------------------------------------------------------------------------------------- +// Type definitions +// -------------------------------------------------------------------------------------------------------------------- + + + +// -------------------------------------------------------------------------------------------------------------------- +// File-scope variables +// -------------------------------------------------------------------------------------------------------------------- + + + +// -------------------------------------------------------------------------------------------------------------------- +// Function declarations +// -------------------------------------------------------------------------------------------------------------------- + + + +// -------------------------------------------------------------------------------------------------------------------- +// Function definitions +// -------------------------------------------------------------------------------------------------------------------- + + +isl29125::isl29125(uint8_t slaveAddress, ISerialBus& serialPort) : slaveAddress {slaveAddress}, bus {serialPort} +{ + initialized = false; +} + + +FunctionStatus isl29125::initialize(void) +{ + FunctionStatus returnValue = FUNCTION_STATUS_OK; + + // Get the Device ID + returnValue = getDeviceID(); + + if (returnValue == FUNCTION_STATUS_OK) + { + // Verify the Device ID against the default known ID + if (memorymap.device_id != deviceID) + { + returnValue = FUNCTION_STATUS_ERROR; + } + } + + if (returnValue == FUNCTION_STATUS_OK) + { + // Fetch the complete register map for an initial copy + returnValue = getCompleteRegisterMap(); + } + + if (returnValue == FUNCTION_STATUS_OK) + { + initialized = true; + } + + return returnValue; +} + + +FunctionStatus isl29125::setMode(Mode_t mode) +{ + FunctionStatus returnValue = FUNCTION_STATUS_OK; + if (!initialized) + { + returnValue = FUNCTION_STATUS_NOT_INITIALIZED; + } + else + { + memorymap.configuration1.mode = mode; + returnValue = bus.write(slaveAddress, 0x01, (uint8_t*)&memorymap.configuration1, 1); + } + return returnValue; +} + + +FunctionStatus isl29125::setRange(Range_t range) +{ + FunctionStatus returnValue = FUNCTION_STATUS_OK; + if (!initialized) + { + returnValue = FUNCTION_STATUS_NOT_INITIALIZED; + } + else + { + memorymap.configuration1.range = range; + returnValue = bus.write(slaveAddress, 0x01, (uint8_t*)&memorymap.configuration1, 1); + } + return returnValue; +} + + +FunctionStatus isl29125::setResolution(Resolution_t resolution) +{ + FunctionStatus returnValue = FUNCTION_STATUS_OK; + if (!initialized) + { + returnValue = FUNCTION_STATUS_NOT_INITIALIZED; + } + else + { + memorymap.configuration1.bits = resolution; + returnValue = bus.write(slaveAddress, 0x01, (uint8_t*)&memorymap.configuration1, 1); + } + return returnValue; +} + +FunctionStatus isl29125::getRGB(struct rgb_t* rgb) +{ + FunctionStatus returnValue = FUNCTION_STATUS_OK; + if (!initialized) + { + returnValue = FUNCTION_STATUS_NOT_INITIALIZED; + } + else + { + returnValue = getRGBRegisters(); + } + if (returnValue == FUNCTION_STATUS_OK) + { + rgb->blue = memorymap.blueData.word; + rgb->green = memorymap.greenData.word; + rgb->red = memorymap.redData.word; + } + return returnValue; +} + + +FunctionStatus isl29125::getDeviceID(void) +{ + uint32_t actualLength; + return bus.read(slaveAddress, 0x00, (uint8_t*)&memorymap, 1, &actualLength); +} + + +FunctionStatus isl29125::getConfiguration(void) +{ + uint32_t actualLength; + return bus.read(slaveAddress, 0x01, (uint8_t*)&memorymap, 3, &actualLength); +} + + +FunctionStatus isl29125::getTheshold(void) +{ + uint32_t actualLength; + return bus.read(slaveAddress, 0x04, (uint8_t*)&memorymap, 4, &actualLength); +} + + +FunctionStatus isl29125::getStatusFlags(void) +{ + uint32_t actualLength; + return bus.read(slaveAddress, 0x08, (uint8_t*)&memorymap, 1, &actualLength); +} + + +FunctionStatus isl29125::getRGBRegisters(void) +{ + uint32_t actualLength; + return bus.read(slaveAddress, 0x09, (uint8_t*)&memorymap, 6, &actualLength); +} + + +FunctionStatus isl29125::getCompleteRegisterMap(void) +{ + uint32_t actualLength; + return bus.read(slaveAddress, 0x00, (uint8_t*)&memorymap, 1, &actualLength); +} diff --git a/code/main/platform/src/logger.cpp b/code/main/platform/src/logger.cpp index c9f7e13..a6cb74a 100644 --- a/code/main/platform/src/logger.cpp +++ b/code/main/platform/src/logger.cpp @@ -49,8 +49,12 @@ // Function definitions // -------------------------------------------------------------------------------------------------------------------- +std::list logger::queue; +uint32_t logger::queuesize = 16; +uint32_t logger::numberOfLostMessages = 0; +bool logger::overflowRecovery = false; -logger::logger(uint32_t queuesize, ISerialBus& serialPort) : port {serialPort}, queuesize {queuesize} +logger::logger(uint32_t queuesize, ISerialBus& serialPort) : port {serialPort} { numberOfLostMessages = 0; // Compose the debug type level list @@ -63,14 +67,13 @@ FunctionStatus logger::log(const char* fileName, const char* functionName, int l FunctionStatus returnValue = FUNCTION_STATUS_OK; #if defined(ENABLE_SERIAL_LOGGING) - bool overflowRecovery = false; int nrOfMessages; struct LogQueueItem logQueueItem; va_list ap; nrOfMessages = queue.size(); - if((nrOfMessages == queuesize - 1) && !overflowRecovery) + if((nrOfMessages >= queuesize - 1) && !overflowRecovery) { // Queue almost full, only one entry left. Log a warning instead composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, "Log queue overflow"); @@ -82,8 +85,7 @@ FunctionStatus logger::log(const char* fileName, const char* functionName, int l else if((nrOfMessages == 0) && overflowRecovery) { // Queue empty again after an overflow - char str[128]; - snprintf(str, sizeof(str) / sizeof(str[0]), "%d messages lost", (unsigned int)numberOfLostMessages); + std::string str = std::to_string(numberOfLostMessages) + " messages were lost in the logger due to QUEUE overflow"; composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, str); queue.push_back(logQueueItem); @@ -130,46 +132,16 @@ void logger::composeTypeParameterList(void) #if defined(ENABLE_SERIAL_LOGGING) -void logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, std::string fileName, std::string functionName, - int lineNumber, LogType logType, std::string context) +void logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, const std::string& fileName, const std::string& functionName, + int lineNumber, LogType logType, const std::string& context) { logQueueItem->logType = logType; logQueueItem->context = context; -// strncpy((char*)&(logQueueItem->context[0]), context, contextSize); -// logQueueItem->context[contextSize - 1] = '\0'; - if(logType != LOGTYPE_PRINT) - { - int fileNameIndex = 0; - - // If filename starts with "src/", strip this part - if((fileName[0] == 's') && - (fileName[1] == 'r') && - (fileName[2] == 'c') && - (fileName[3] == '/')) - { - fileNameIndex = 4; - } - -// It is known that the strncpy use can potentially truncate the source string, meaning -// that the target string size is smaller then the original string -// This is not a problem in this particular case, so the compiler warning is disabled -// for this situation -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstringop-truncation" - // All logtypes except LOGTYPE_PRINT need filename, functionname and linenumber. - logQueueItem->fileName = fileName; - logQueueItem->functionName = functionName; -// strncpy((char*)&(logQueueItem->fileName[0]), &fileName[fileNameIndex], fileNameSize); -// strncpy((char*)&(logQueueItem->functionName[0]), functionName, functionNameSize); -#pragma GCC diagnostic pop - logQueueItem->lineNumber = lineNumber; - - // Fix terminating null byte in strncpy in case string to be copied is too long -// logQueueItem->fileName[fileNameSize - 1] = '\0'; -// logQueueItem->functionName[functionNameSize - 1] = '\0'; - } + logQueueItem->fileName = fileName; + logQueueItem->functionName = functionName; + logQueueItem->lineNumber = lineNumber; } #endif @@ -186,7 +158,7 @@ void logger::task() // Get the first log item from queue logQueueItem = queue.front(); // Remove the item from the queue - this->queue.pop_front(); + queue.pop_front(); if(logQueueItem.logType == LOGTYPE_PRINT) @@ -194,7 +166,7 @@ void logger::task() // Raw print #if defined(ENABLE_SERIAL_LOGGING) - port.write(NO_DEVICE_ADDRESS, NO_REGISTER_ADDRESS, (uint8_t*)logQueueItem.context.c_str(), logQueueItem.context.length() + 1); + port.write(NO_DEVICE_ADDRESS, NO_REGISTER_ADDRESS, (uint8_t*)logQueueItem.context.c_str(), logQueueItem.context.length()); #endif } @@ -208,7 +180,8 @@ void logger::task() // Find the correct Log level type auto it = std::find_if(typeParameterList.begin(), typeParameterList.end(), [&logQueueItem](const struct typeParameters& obj) {return obj.logType == logQueueItem.logType;}); - std::string outputString = it->vt100Prefix + " " + std::string outputString = "\n\r" + + it->vt100Prefix + " " + it->vt100Prefix + " " + std::to_string(seconds) + " " + logQueueItem.fileName + " " diff --git a/code/main/old/src/wifi.cpp b/code/main/platform/src/wifi.cpp similarity index 99% rename from code/main/old/src/wifi.cpp rename to code/main/platform/src/wifi.cpp index 9436f63..2c1d9e0 100644 --- a/code/main/old/src/wifi.cpp +++ b/code/main/platform/src/wifi.cpp @@ -63,15 +63,13 @@ static const char* pass = "madagascar"; // Function definitions // -------------------------------------------------------------------------------------------------------------------- - -int Wifi::s_retry_num = 0; -const char* Wifi::TAG = "wifi station"; EventGroupHandle_t Wifi::s_wifi_event_group = xEventGroupCreate(); +int Wifi::s_retry_num = 0; +const char* Wifi::TAG; Wifi::Wifi() { - - + TAG = "wifi station"; } void Wifi::event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) diff --git a/doc/isl29125.pdf b/doc/isl29125.pdf new file mode 100644 index 0000000..0df7e8c Binary files /dev/null and b/doc/isl29125.pdf differ