Started reorganizing the code. Put everything existing to OLD and began with a new main.cppy

This commit is contained in:
Matthias Mitscherlich
2024-02-29 16:53:16 +01:00
parent 29e2e14850
commit 4b31b6c618
29 changed files with 446 additions and 359 deletions
+191
View File
@@ -0,0 +1,191 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file bme280.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include <bmp280.h>
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// List of registers
#define ADDRESS_COMP_PARAMETERS ((uint32_t)0x88)
#define ADDRESS_REG_ID ((uint32_t)0xD0)
#define ADDRESS_REG_RESET ((uint32_t)0xE0)
#define ADDRESS_REG_STATUS ((uint32_t)0xF3)
#define ADDRESS_REG_CTRL_MEAS ((uint32_t)0xF4)
#define ADDRESS_REG_CONFIG ((uint32_t)0xF5)
#define ADDRESS_REG_PRESSURE_MSB ((uint32_t)0xF7)
#define ADDRESS_REG_PRESSURE_LSB ((uint32_t)0xF8)
#define ADDRESS_REG_PRESSURE_XLSB ((uint32_t)0xF9)
#define ADDRESS_REG_TEMPERATURE_MSB ((uint32_t)0xFA)
#define ADDRESS_REG_TEMPERATURE_LSB ((uint32_t)0xFB)
#define ADDRESS_REG_TEMPERATURE_XLSB ((uint32_t)0xFC)
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
BMP280::BMP280(I2C* bus, uint8_t slaveAddress)
{
// Take over the bus
BMP280::bus = bus;
// Take over the device slave address
BMP280::slaveAddress = slaveAddress;
// Reset the driver itself
resetDriver();
// Reset the device
}
void BMP280::resetSensor(void)
{
resetDevice();
}
bool BMP280::initialize(void)
{
bool returnValue = true;
// First read the device ID
getDeviceID();
// Device ID must match, otherwise there is a component error
if (memorymap.id != BMP280_DEVICE_ID)
{
returnValue = false;
}
if (returnValue)
{
// Get the compensation values from the device
getCompensationValues();
}
return returnValue;
}
bool BMP280::setSensorMode(BMP280_Mode_t mode)
{
bool returnValue = true;
memorymap.ctrl_meas.mode = mode;
setSensorControlMeasurement();
return returnValue;
}
bool BMP280::setSensorTemperatureOversampling(BMP280_Oversampling_t oversampling)
{
bool returnValue = true;
memorymap.ctrl_meas.oversampling_temp = oversampling;
setSensorControlMeasurement();
return returnValue;
}
int BMP280::getTemperature(void)
{
// Get latest raw values from device
BMP280::getTemperatureValues();
// Calculate temperature
BMP280::compensateTemperature();
// return the value
return temperature;
}
void BMP280::resetDriver(void)
{
// Reset the parameters
BMP280::compensationParameters = {};
// Reset the device memory map
BMP280::memorymap = {};
// Reset the mode
BMP280::mode = STANDBY;
// Reset calculation values
t_fine = 0;
temperature = 0;
}
void BMP280::getDeviceID(void)
{
bus->read_register(slaveAddress, ADDRESS_REG_ID, &memorymap.id, 1);
}
void BMP280::resetDevice(void)
{
uint8_t resetValue = BMP280_RESET_VALUE;
bus->write_register(slaveAddress, ADDRESS_REG_RESET, &resetValue, 1);
}
void BMP280::setSensorControlMeasurement(void)
{
bus->write_register(slaveAddress, ADDRESS_REG_CTRL_MEAS, (uint8_t*)&memorymap.ctrl_meas, 1);
}
void BMP280::setSensorConfiguration(void)
{
bus->write_register(slaveAddress, ADDRESS_REG_CONFIG, (uint8_t*)&memorymap.config, 1);
}
void BMP280::getCompensationValues(void)
{
bus->read_register(slaveAddress, ADDRESS_COMP_PARAMETERS, (uint8_t*)&compensationParameters, sizeof(compensationParameters));
LOGGER_DEBUG("Got compensation values: %04X %04X %04X", compensationParameters.dig_T1, compensationParameters.dig_T2, compensationParameters.dig_T3);
}
void BMP280::getPreasureValues(void)
{
bus->read_register(slaveAddress, ADDRESS_REG_PRESSURE_LSB, (uint8_t*)&memorymap.pressure_raw, sizeof(memorymap.pressure_raw));
}
void BMP280::getTemperatureValues(void)
{
bus->read_register(slaveAddress, ADDRESS_REG_TEMPERATURE_MSB, (uint8_t*)&memorymap.temperature_raw, sizeof(memorymap.temperature_raw));
}
void BMP280::compensateTemperature(void)
{
int adc_T = 0;
// Create a single temperature value from the individual memory entries
adc_T |= memorymap.temperature_raw.msb << 12;
adc_T |= memorymap.temperature_raw.lsb << 4;
// XLSB is only a nibble
adc_T |= memorymap.temperature_raw.xlsb >> 4;
int var1 = ((((adc_T >> 3) - ((int)compensationParameters.dig_T1<<1))) * ((int)compensationParameters.dig_T2)) >> 11;
int var2 = (((((adc_T>>4) - ((int)compensationParameters.dig_T1)) * ((adc_T>>4) - ((int)compensationParameters.dig_T1))) >> 12) * ((int)compensationParameters.dig_T3)) >> 14;
t_fine = var1 + var2;
temperature = (t_fine * 5 + 128) >> 8;
}
+231
View File
@@ -0,0 +1,231 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file clock.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "clock.h"
#include "esp_sntp.h"
#include "esp_wifi.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
Clock::Clock(Clock::mode mode)
{
Clock::currentTime = 40000;
Clock::clockmode = mode;
// Start NTP
setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 1);
tzset();
esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
esp_sntp_setservername(0, "pool.ntp.org");
esp_sntp_init();
}
void Clock::generateWordlist(list<string>* wordlist)
{
struct tm tm;
time(&currentTime);
// currentTime += 100;
localtime_r(&currentTime, &tm);
// LOGGER_INFO("%lld\n\r", currentTime);
// LOGGER_INFO("%02i:%02i:%02i\n\r", tm.tm_hour, tm.tm_min, tm.tm_sec);
wordlist->clear();
wordlist->push_back("it");
wordlist->push_back("is");
wordlist->push_back(toNumbers[calculateHours(tm)]);
if (tm.tm_min < 4)
{
wordlist->push_back("hours");
}
else if (tm.tm_min < 9)
{
wordlist->push_back("ind_five");
wordlist->push_back("after");
}
else if (tm.tm_min < 14)
{
wordlist->push_back("ind_ten");
wordlist->push_back("after");
}
else if (tm.tm_min < 19)
{
wordlist->push_back("ind_quart");
wordlist->push_back("after");
}
else if (tm.tm_min < 24)
{
wordlist->push_back("ind_ten");
wordlist->push_back("before");
wordlist->push_back("half");
}
else if (tm.tm_min < 28)
{
wordlist->push_back("ind_five");
wordlist->push_back("before");
wordlist->push_back("half");
}
else if (tm.tm_min < 30)
{
wordlist->push_back("almost");
wordlist->push_back("half");
}
else if (tm.tm_min < 34)
{
wordlist->push_back("half");
}
else if (tm.tm_min < 39)
{
wordlist->push_back("ind_five");
wordlist->push_back("after");
wordlist->push_back("half");
}
else if (tm.tm_min < 44)
{
wordlist->push_back("ind_ten");
wordlist->push_back("after");
wordlist->push_back("half");
}
else if (tm.tm_min < 49)
{
wordlist->push_back("ind_quart");
wordlist->push_back("before");
}
else if (tm.tm_min < 54)
{
wordlist->push_back("ind_ten");
wordlist->push_back("before");
}
else if (tm.tm_min < 58)
{
wordlist->push_back("ind_five");
wordlist->push_back("before");
}
else
{
wordlist->push_back("almost");
wordlist->push_back("hours");
}
// Attach the day as a word, too
switch (tm.tm_wday)
{
case 0:
wordlist->push_back("sunday");
break;
case 1:
wordlist->push_back("monday");
break;
case 2:
wordlist->push_back("tuesday");
break;
case 3:
wordlist->push_back("wednesday");
break;
case 4:
wordlist->push_back("thursday");
break;
case 5:
wordlist->push_back("friday");
break;
case 6:
wordlist->push_back("saturday");
break;
default:
wordlist->push_back("sunday");
}
}
time_t Clock::getTime(void)
{
// time(&currentTime);
return currentTime;
}
int Clock::calculateHours(struct tm time)
{
int hours = time.tm_hour;
// Add one hour in case the clock is heading towards half
if (time.tm_min > 19)
{
hours = hours + 1;
}
// Calculate hours to 12hour system
if (hours > 12)
{
hours = hours - 12;
}
// Start at 1, not 0
if (hours == 0)
{
hours++;
}
return hours;
}
//void Clock::toString(TimeStructure* timestructure)
//{
// LOGGER_SUCCESS("%s%s%s%s%d%s", timestructure->prefix ? "Het is " : "",
// timestructure->fifths == Five ? "Vijf " :
// timestructure->fifths == Ten ? "Tien " :
// timestructure->fifths == Quarter ? "kwart ": "",
// timestructure->beforeAfter == Before ? "voor " :
// timestructure->beforeAfter == After ? "over " : "",
// timestructure->half ? "half " : "",
// timestructure->hours,
// timestructure->hourPostfix ? " uur" : ""
// );
//}
+95
View File
@@ -0,0 +1,95 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file clockwordmap.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "clockwordmap.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
ClockWordmap::ClockWordmap(LEDMatrix* matrix) : Wordmap(matrix)
{
createList_NL();
createList_EN();
}
void ClockWordmap::createList_NL(void)
{
// First, clear the list
wordlist[NL].clear();
// Now lets add all relevant words
wordlist[NL].push_back((struct word){"it", {{0,0},{1,0},{2,0}}});
wordlist[NL].push_back((struct word){"is", {{4,0},{5,0}}});
wordlist[NL].push_back((struct word){"ind_five", {{7,0},{8,0},{9,0},{10,0}}});
wordlist[NL].push_back((struct word){"ind_ten", {{1,1},{2,1},{3,1},{4,1}}});
wordlist[NL].push_back((struct word){"ind_quart", {{6,1},{7,1},{8,1},{9,1},{10,1}}});
wordlist[NL].push_back((struct word){"ind_twenty", {{0,2},{1,2},{2,2},{3,2},{4,2},{5,2},{6,2}}});
wordlist[NL].push_back((struct word){"before", {{6,3},{7,3},{8,3},{9,3}}});
wordlist[NL].push_back((struct word){"after", {{1,3},{2,3},{3,3},{4,3}}});
wordlist[NL].push_back((struct word){"almost", {{0,4},{1,4},{2,4},{3,4},{4,4}}});
wordlist[NL].push_back((struct word){"half", {{7,4},{8,4},{9,4},{10,4}}});
wordlist[NL].push_back((struct word){"one", {{3,5},{4,5},{5,5}}});
wordlist[NL].push_back((struct word){"two", {{1,5},{2,5},{3,5},{4,5}}});
wordlist[NL].push_back((struct word){"three", {{6,5},{7,5},{8,5},{9,5}}});
wordlist[NL].push_back((struct word){"four", {{7,6},{8,6},{9,6},{10,6}}});
wordlist[NL].push_back((struct word){"five", {{0,6},{1,6},{2,6},{3,6}}});
wordlist[NL].push_back((struct word){"six", {{4,6},{5,6},{6,6}}});
wordlist[NL].push_back((struct word){"seven", {{0,7},{1,7},{2,7},{3,7},{4,7}}});
wordlist[NL].push_back((struct word){"eight", {{0,8},{1,8},{2,8},{3,8}}});
wordlist[NL].push_back((struct word){"nine", {{6,7},{7,7},{8,7},{9,7},{10,7}}});
wordlist[NL].push_back((struct word){"ten", {{4,8},{5,8},{6,8},{7,8}}});
wordlist[NL].push_back((struct word){"eleven", {{8,8},{9,8},{10,8}}});
wordlist[NL].push_back((struct word){"twelve", {{0,9},{1,9},{2,9},{3,9},{4,9},{5,9}}});
wordlist[NL].push_back((struct word){"hours", {{8,9},{9,9},{10,9}}});
}
+76
View File
@@ -0,0 +1,76 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file daywordmap.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "daywordmap.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
DayWordmap::DayWordmap(LEDMatrix* matrix) : Wordmap(matrix)
{
createList_NL();
createList_EN();
}
void DayWordmap::createList_NL(void)
{
// First, clear the list
wordlist[NL].clear();
// Now lets add all relevant words
wordlist[NL].push_back((struct word){"monday", {{0,12},{1,12},{2,12},{3,12},{4,12},{5,12},{6,12}}});
wordlist[NL].push_back((struct word){"tuesday", {{13,12},{14,12},{15,12},{16,12},{17,12},{18,12},{19,12}}});
wordlist[NL].push_back((struct word){"wednesday", {{11,10},{12,10},{13,10},{14,10},{15,10},{16,10},{17,10},{18,10}}});
wordlist[NL].push_back((struct word){"thursday", {{2,11},{3,11},{4,11},{5,11},{6,11},{7,11},{8,11},{9,11},{10,11}}});
wordlist[NL].push_back((struct word){"friday", {{12,11},{13,11},{14,11},{15,11},{16,11},{17,11},{18,11}}});
wordlist[NL].push_back((struct word){"saturday", {{0,10},{1,10},{2,10},{3,10},{4,10},{5,10},{6,10},{7,10}}});
wordlist[NL].push_back((struct word){"sunday", {{7,12},{8,12},{9,12},{10,12},{11,12},{12,12}}});
}
+106
View File
@@ -0,0 +1,106 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file gpio.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include <gpio.h>
#include "driver/gpio.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
GPIO::GPIO(int number, GPIO_Direction_t direction)
{
this->number = number;
this->direction = direction;
//zero-initialize the config structure.
gpio_config_t io_conf = {};
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
io_conf.mode = (this->direction == GPIO_DIRECTION_OUTPUT) ? GPIO_MODE_OUTPUT : GPIO_MODE_INPUT;
//bit mask of the pins that you want to set
io_conf.pin_bit_mask = (1ULL << this->number);
//disable pull-down mode
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
//disable pull-up mode
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
//configure GPIO with the given settings
ESP_ERROR_CHECK(gpio_config(&io_conf));
}
bool GPIO::SetOutput(GPIO_Value_t value)
{
bool returnValue = false;
if (this->direction == GPIO_DIRECTION_OUTPUT)
{
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t)this->number, (uint32_t)value));
this->value = value;
returnValue = true;
}
else
{
returnValue = false;
}
return returnValue;
}
GPIO_Value_t GPIO::GetInput(void)
{
if (gpio_get_level((gpio_num_t)this->number))
{
this->value = GPIO_VALUE_HIGH;
}
else
{
this->value = GPIO_VALUE_LOW;
}
return this->value;
}
+122
View File
@@ -0,0 +1,122 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file i2c.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "i2c.h"
#include "driver/i2c.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_TIMEOUT_MS 1000
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
// Reset the class variable
unsigned int I2C::number = 0;
I2C::I2C(unsigned int SCL, unsigned int SDA)
{
I2C::thisNumber = number++;
I2C::SCL = SCL;
I2C::SDA = SDA;
I2C::frequency = I2C_MASTER_FREQ_HZ;
I2C::timeout_ms = I2C_MASTER_TIMEOUT_MS;
//
i2c_port_t i2c_master_port = (i2c_port_t)I2C::thisNumber;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = (int)I2C::SDA,
.scl_io_num = (int)I2C::SCL,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {I2C::frequency},
.clk_flags = 0
};
ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
}
bool I2C::write_register(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* const data, uint8_t length)
{
bool returnValue = true;
write(slaveAddress, registerAddress, data, length);
return returnValue;
}
bool I2C::read_register(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length)
{
bool returnValue = true;
read(slaveAddress, registerAddress, data, length);
return returnValue;
}
void I2C::write(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* const data, uint8_t length)
{
uint8_t buffer[length + 1];
buffer[0] = registerAddress;
for (int i = 0; i < length; i++)
{
buffer[i+1] = data[i];
}
ESP_ERROR_CHECK(i2c_master_write_to_device((i2c_port_t)thisNumber, slaveAddress, buffer, sizeof(buffer), timeout_ms / portTICK_PERIOD_MS));
}
void I2C::read(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length)
{
ESP_ERROR_CHECK(i2c_master_write_read_device((i2c_port_t)thisNumber, slaveAddress, &registerAddress, 1, data, length, timeout_ms / portTICK_PERIOD_MS));
}
+124
View File
@@ -0,0 +1,124 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_check.h"
#include "led_strip_encoder.h"
static const char *TAG = "led_encoder";
typedef struct {
rmt_encoder_t base;
rmt_encoder_t *bytes_encoder;
rmt_encoder_t *copy_encoder;
int state;
rmt_symbol_word_t reset_code;
} rmt_led_strip_encoder_t;
static size_t rmt_encode_led_strip(rmt_encoder_t *encoder, rmt_channel_handle_t channel, const void *primary_data, size_t data_size, rmt_encode_state_t *ret_state)
{
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
rmt_encoder_handle_t bytes_encoder = led_encoder->bytes_encoder;
rmt_encoder_handle_t copy_encoder = led_encoder->copy_encoder;
rmt_encode_state_t session_state = 0;
rmt_encode_state_t state = 0;
size_t encoded_symbols = 0;
switch (led_encoder->state) {
case 0: // send RGB data
encoded_symbols += bytes_encoder->encode(bytes_encoder, channel, primary_data, data_size, &session_state);
if (session_state & RMT_ENCODING_COMPLETE) {
led_encoder->state = 1; // switch to next state when current encoding session finished
}
if (session_state & RMT_ENCODING_MEM_FULL) {
state |= RMT_ENCODING_MEM_FULL;
goto out; // yield if there's no free space for encoding artifacts
}
// fall-through
case 1: // send reset code
encoded_symbols += copy_encoder->encode(copy_encoder, channel, &led_encoder->reset_code,
sizeof(led_encoder->reset_code), &session_state);
if (session_state & RMT_ENCODING_COMPLETE) {
led_encoder->state = 0; // back to the initial encoding session
state |= RMT_ENCODING_COMPLETE;
}
if (session_state & RMT_ENCODING_MEM_FULL) {
state |= RMT_ENCODING_MEM_FULL;
goto out; // yield if there's no free space for encoding artifacts
}
}
out:
*ret_state = state;
return encoded_symbols;
}
static esp_err_t rmt_del_led_strip_encoder(rmt_encoder_t *encoder)
{
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
rmt_del_encoder(led_encoder->bytes_encoder);
rmt_del_encoder(led_encoder->copy_encoder);
free(led_encoder);
return ESP_OK;
}
static esp_err_t rmt_led_strip_encoder_reset(rmt_encoder_t *encoder)
{
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
rmt_encoder_reset(led_encoder->bytes_encoder);
rmt_encoder_reset(led_encoder->copy_encoder);
led_encoder->state = 0;
return ESP_OK;
}
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder)
{
esp_err_t ret = ESP_OK;
rmt_led_strip_encoder_t *led_encoder = NULL;
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
led_encoder = calloc(1, sizeof(rmt_led_strip_encoder_t));
ESP_GOTO_ON_FALSE(led_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for led strip encoder");
led_encoder->base.encode = rmt_encode_led_strip;
led_encoder->base.del = rmt_del_led_strip_encoder;
led_encoder->base.reset = rmt_led_strip_encoder_reset;
// different led strip might have its own timing requirements, following parameter is for WS2812
rmt_bytes_encoder_config_t bytes_encoder_config = {
.bit0 = {
.level0 = 1,
.duration0 = 0.3 * config->resolution / 1000000, // T0H=0.3us
.level1 = 0,
.duration1 = 0.9 * config->resolution / 1000000, // T0L=0.9us
},
.bit1 = {
.level0 = 1,
.duration0 = 0.9 * config->resolution / 1000000, // T1H=0.9us
.level1 = 0,
.duration1 = 0.3 * config->resolution / 1000000, // T1L=0.3us
},
.flags.msb_first = 1 // WS2812 transfer bit order: G7...G0R7...R0B7...B0
};
ESP_GOTO_ON_ERROR(rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder), err, TAG, "create bytes encoder failed");
rmt_copy_encoder_config_t copy_encoder_config = {};
ESP_GOTO_ON_ERROR(rmt_new_copy_encoder(&copy_encoder_config, &led_encoder->copy_encoder), err, TAG, "create copy encoder failed");
uint32_t reset_ticks = config->resolution / 1000000 * 50 / 2; // reset code duration defaults to 50us
led_encoder->reset_code = (rmt_symbol_word_t) {
.level0 = 0,
.duration0 = reset_ticks,
.level1 = 0,
.duration1 = reset_ticks,
};
*ret_encoder = &led_encoder->base;
return ESP_OK;
err:
if (led_encoder) {
if (led_encoder->bytes_encoder) {
rmt_del_encoder(led_encoder->bytes_encoder);
}
if (led_encoder->copy_encoder) {
rmt_del_encoder(led_encoder->copy_encoder);
}
free(led_encoder);
}
return ret;
}
+259
View File
@@ -0,0 +1,259 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file ledmatrix.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "ledmatrix.h"
#include "logger.h"
#include "string.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
SemaphoreHandle_t LEDMatrix::taskSemaphore = 0;
TaskHandle_t LEDMatrix::matrixTaskHandle = NULL;
bool LEDMatrix::initialized = false;
LEDMatrix::LEDMatrix(LEDMatrix_Parameters_t* parameters)
{
// Take the parameters
LEDMatrix::parameters = *parameters;
// Calculate the total number of pixels
LEDMatrix::numberOfPixels = parameters->width * parameters->height;
// Create the matrix storage
matrix = new LEDMatrix_Pixel_t[parameters->width * parameters->height];
// Create the second matrix, that is actually transmitted to the periphery
// Create the task Semaphore
vSemaphoreCreateBinary(LEDMatrix::taskSemaphore);
// Take the semaphore immediately so the task cannot run
xSemaphoreTake(LEDMatrix::taskSemaphore, 0);
// Create the matrix task
xTaskCreate(matrixTask, (const char*)"matrixTask", 4096, this, 3, &LEDMatrix::matrixTaskHandle);
// Clear the matrix initially
LEDMatrix::clear();
initialized = true;
}
bool LEDMatrix::setPixelValue(unsigned int colum, unsigned int row, bool value)
{
bool returnValue = true;
// unsigned int rowC = 0;
// unsigned int colC = 0;
unsigned int pixelAddress = 0;
if ((row < parameters.height) && (colum < parameters.width))
{
returnValue = true;
}
else
{
returnValue = false;
}
if (returnValue)
{
// // Determine the actual row coordinate based on the matrix orientation
// if (parameters.columOrientation == LEDMATRIX_ORIENTATION_COLUM_UP_DOWN)
// {
// rowC = row;
// }
// else if (parameters.columOrientation == LEDMATRIX_ORIENTATION_COLUM_DOWN_UP)
// {
// rowC = (parameters.height - 1) - row;
// }
//
// // Determine the actual row coordinate based on the matrix orientation
// if (parameters.rowOrientation == LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT)
// {
// colC = colum;
// }
// else if (parameters.rowOrientation == LEDMATRIX_ORIENTATION_ROW_RIGHT_LEFT)
// {
// colC = (parameters.width - 1) - colum;
// }
//
// // Calculate the pixel address in the pixel array based on the previous information
// if (parameters.matrixOrientation == LEDMATRIX_ORIENTATION_COLUM)
// {
// pixelAddress = rowC + (colC * parameters.height);
// }
// else if (parameters.matrixOrientation == LEDMATRIX_ORIENTATION_ROW)
// {
// pixelAddress = (rowC * parameters.width) + colC;
// }
pixelAddress = LEDMatrix::findPixelIndexFromCoordinates(colum, row);
// Update the pixel value
matrix[pixelAddress].on = value;
}
return returnValue;
}
void LEDMatrix::setGlobalColour(uint8_t red, uint8_t green, uint8_t blue)
{
for (int i = 0; i < numberOfPixels; i++)
{
matrix[i].red = red;
matrix[i].green = green;
matrix[i].blue = blue;
}
}
void LEDMatrix::setPixelColour(unsigned int colum, unsigned int row, uint8_t red, uint8_t green, uint8_t blue)
{
unsigned int index;
index = LEDMatrix::findPixelIndexFromCoordinates(colum, row);
matrix[index].red = red;
matrix[index].green = green;
matrix[index].blue = blue;
}
void LEDMatrix::clear(void)
{
for (int i = 0; i < numberOfPixels; i++)
{
matrix[i].on = false;
}
}
unsigned int LEDMatrix::findPixelIndexFromCoordinates(unsigned int colum, unsigned int row)
{
bool returnValue = true;
unsigned int index = 0;
unsigned int rowC = 0;
unsigned int colC = 0;
if ((row < parameters.height) && (colum < parameters.width))
{
returnValue = true;
}
else
{
returnValue = false;
}
if (returnValue)
{
// Determine the actual row coordinate based on the matrix orientation
if (parameters.columOrientation == LEDMATRIX_ORIENTATION_COLUM_UP_DOWN)
{
rowC = row;
}
else if (parameters.columOrientation == LEDMATRIX_ORIENTATION_COLUM_DOWN_UP)
{
rowC = (parameters.height - 1) - row;
}
// Determine the actual row coordinate based on the matrix orientation
if (parameters.rowOrientation == LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT)
{
colC = colum;
}
else if (parameters.rowOrientation == LEDMATRIX_ORIENTATION_ROW_RIGHT_LEFT)
{
colC = (parameters.width - 1) - colum;
}
// Calculate the pixel address in the pixel array based on the previous information
if (parameters.matrixOrientation == LEDMATRIX_ORIENTATION_COLUM)
{
index = rowC + (colC * parameters.height);
}
else if (parameters.matrixOrientation == LEDMATRIX_ORIENTATION_ROW)
{
index = (rowC * parameters.width) + colC;
}
}
return index;
}
void LEDMatrix::matrixTask(void* parameters)
{
LEDMatrix* ledmatrix = (LEDMatrix*)parameters;
uint8_t matrix[ledmatrix->numberOfPixels * 3];
memset(&matrix, 0, sizeof(matrix));
while(1)
{
xSemaphoreTake(ledmatrix->taskSemaphore, portMAX_DELAY);
for (int i = 0; i < ledmatrix->numberOfPixels; i++)
{
if (ledmatrix->matrix[i].on)
{
matrix[i * 3 + LEDMATRIX_RED_INDEX] = ledmatrix->matrix[i].red;
matrix[i * 3 + LEDMATRIX_GREEN_INDEX] = ledmatrix->matrix[i].green;
matrix[i * 3 + LEDMATRIX_BLUE_INDEX] = ledmatrix->matrix[i].blue;
}
else
{
matrix[i * 3 + 0] = 0;
matrix[i * 3 + 1] = 0;
matrix[i * 3 + 2] = 0;
}
}
rmt_transmit(*ledmatrix->parameters.rmtChannel, *ledmatrix->parameters.rmtEncoder, matrix, sizeof(matrix), ledmatrix->parameters.rmtConfig);
}
}
+254
View File
@@ -0,0 +1,254 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file logger.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include <string.h>
#include <stdarg.h>
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
#define ENABLE_SERIAL_LOGGING
//#undef ENABLE_SERIAL_LOGGING
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
TaskHandle_t Logger::logTaskHandle = NULL;
uart_port_t Logger::uartPort = 0;
QueueHandle_t Logger::logQueue = NULL;
int Logger::queuesize = 0;
Logger::Logger(int queuesize, uart_port_t uartPort)
{
Logger::uartPort = uartPort;
Logger::queuesize = queuesize;
logQueue = xQueueCreate(queuesize, sizeof(struct LogQueueItem));
xTaskCreate(loggerTask, (const char*)"loggerTask", 3072, NULL, 3, &logTaskHandle);
}
void Logger::Logger_log(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* format, ...)
{
#if defined(ENABLE_SERIAL_LOGGING)
static int nrLostMessages = 0;
static bool overflowRecovery = false;
int nrOfMessages;
struct LogQueueItem logQueueItem;
va_list ap;
nrOfMessages = uxQueueMessagesWaiting(Logger::logQueue);
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");
(void)xQueueSend(logQueue, &logQueueItem, 0);
overflowRecovery = true;
nrLostMessages = 1;
}
else if((nrOfMessages == 0) && overflowRecovery)
{
// Queue empty again after an overflow
char str[128];
snprintf(str, sizeof(str) / sizeof(str[0]), "%d messages lost", nrLostMessages);
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, str);
(void)xQueueSend(logQueue, &logQueueItem, 0);
overflowRecovery = false;
}
else if(!overflowRecovery)
{
// Normal behaviour, queue not full
char str[128];
va_start(ap, format);
vsnprintf(str, sizeof(str) / sizeof(str[0]), format, ap);
va_end(ap);
composeLogQueueItem(&logQueueItem, fileName, functionName, lineNumber, logType, str);
(void)xQueueSend(logQueue, &logQueueItem, 0);
}
else
{
// Count number of lost messages
++nrLostMessages;
}
#endif
}
void Logger_logISR(struct Logger* self, const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* context)
{
#if defined(ENABLE_LOGGING)
if (self->initialized)
{
struct LogQueueItem logQueueItem;
portBASE_TYPE higherPriorityTaskWoken = pdFALSE;
composeLogQueueItem(&logQueueItem, fileName, functionName, lineNumber, logType, context);
if(xQueueSendFromISR(self->logQueue, &logQueueItem, &higherPriorityTaskWoken) != pdTRUE)
{
// Queue failed
}
portEND_SWITCHING_ISR(higherPriorityTaskWoken);
}
#endif
}
#if defined(ENABLE_SERIAL_LOGGING)
void Logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, const char* fileName, const char* functionName,
int lineNumber, LogType logType, const char* context)
{
const size_t fileNameSize = sizeof(logQueueItem->fileName) / sizeof(logQueueItem->fileName[0]);
const size_t functionNameSize = sizeof(logQueueItem->functionName) / sizeof(logQueueItem->functionName[0]);
const size_t contextSize = sizeof(logQueueItem->context) / sizeof(logQueueItem->context[0]);
logQueueItem->logType = logType;
strncpy(&(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.
strncpy(&(logQueueItem->fileName[0]), &fileName[fileNameIndex], fileNameSize);
strncpy(&(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';
}
}
#endif
void Logger::loggerTask(void* parameters)
{
while(true)
{
struct LogQueueItem logQueueItem;
xQueueReceive(logQueue, &logQueueItem, portMAX_DELAY);
if(logQueueItem.logType == LOGTYPE_PRINT)
{
// Raw print
#if defined(ENABLE_SERIAL_LOGGING)
uart_write_bytes(uartPort, logQueueItem.context, strlen(logQueueItem.context));
#endif
}
else
{
#if defined(ENABLE_SERIAL_LOGGING)
char str[256];
char* vt100Prefix = "";
const char* vt100Postfix = "\033[0m";
#endif
#if defined(ENABLE_SERIAL_LOGGING)
if(logQueueItem.logType == LOGTYPE_INFO)
{
vt100Prefix = "\033[33m";
}
else if(logQueueItem.logType == LOGTYPE_WARNING)
{
vt100Prefix = "\033[35m";
}
else if(logQueueItem.logType == LOGTYPE_ERROR)
{
vt100Prefix = "\033[31m";
}
else if(logQueueItem.logType == LOGTYPE_SUCCESS)
{
vt100Prefix = "\033[32m";
}
#endif
unsigned int seconds = 0;
#if defined(ENABLE_SERIAL_LOGGING)
// Formatted print
snprintf(str, sizeof(str) / sizeof(str[0]), "%s[%s] %09d %s, %s, %d: %s%s\n\r",
vt100Prefix,
(logQueueItem.logType == LOGTYPE_DEBUG) ? "DBG" :
(logQueueItem.logType == LOGTYPE_INFO) ? "INF" :
(logQueueItem.logType == LOGTYPE_SUCCESS) ? "SCS" :
(logQueueItem.logType == LOGTYPE_WARNING) ? "WRN" : "ERR",
seconds,
logQueueItem.fileName,
logQueueItem.functionName,
logQueueItem.lineNumber,
logQueueItem.context,
vt100Postfix);
#endif
#if defined(ENABLE_SERIAL_LOGGING)
uart_write_bytes(uartPort, str, strlen(str));
#endif
}
}
vTaskDelete(NULL);
}
+123
View File
@@ -0,0 +1,123 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file temperature.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "temperature.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
Temperature::Temperature()
{
Temperature::minTemperature = 14;
Temperature::maxTemperature = 29;
};
void Temperature::generateWordlist(int temperature, list<string>* wordlist)
{
// Clear the list
wordlist->clear();
// Add fixed preamble
wordlist->push_back("it");
wordlist->push_back("is");
// Temperature value to string
if (temperature < minTemperature)
{
wordlist->push_back("below");
wordlist->push_back(to_string(minTemperature));
}
else if (temperature > maxTemperature)
{
wordlist->push_back("above");
wordlist->push_back(to_string(maxTemperature));
}
else
{
wordlist->push_back(to_string(temperature));
}
// Add fixed postamble
wordlist->push_back("degrees");
}
void Temperature::calculateRGB(int temperature, uint8_t* red, uint8_t* green, uint8_t* blue)
{
int calcBlue = 0;
int calcRed = 0;
int factor = 100 / (maxTemperature - minTemperature);
LOGGER_INFO("Incoming Temperature is: %i (min: %i, max: %i", temperature, minTemperature, maxTemperature);
if (temperature < minTemperature)
{
calcBlue = 0xFF;
calcRed = 0x00;
}
else if (temperature > maxTemperature)
{
calcBlue = 0x00;
calcRed = 0xFF;
}
else
{
calcBlue = (((maxTemperature - temperature) * factor) * 0xFF) / 100;
calcRed = (((temperature - minTemperature) * factor) * 0xFF) / 100;
}
LOGGER_PRINT("\n\rRed %i %x (%i)", calcRed, calcRed, (temperature - minTemperature) * factor);
LOGGER_PRINT("\n\rGreen %i %x", 0, 0);
LOGGER_PRINT("\n\rBlue %i %x (%i)", calcBlue, calcBlue, (maxTemperature - temperature) * factor);
*red = calcRed & 0xFF;
*green = 0x00;
*blue = calcBlue & 0xFF;
}
+96
View File
@@ -0,0 +1,96 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file temperaturewordmap.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "temperaturewordmap.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
TemperatureWordmap::TemperatureWordmap(LEDMatrix* matrix) : Wordmap(matrix)
{
createList_NL();
createList_EN();
}
void TemperatureWordmap::createList_NL(void)
{
// First, clear the list
wordlist[NL].clear();
// Now lets add all relevant words
wordlist[NL].push_back((struct word){"it", {{14,0},{15,0},{16,0}}});
wordlist[NL].push_back((struct word){"is", {{18,0},{19,0}}});
wordlist[NL].push_back((struct word){"above", {{11,1},{12,1},{13,1},{14,1},{15,1},{18,2},{19,2}}});
wordlist[NL].push_back((struct word){"below", {{12,2},{13,2},{14,2},{15,2},{16,2},{18,2},{19,2}}});
// Tenth
wordlist[NL].push_back((struct word){"14", {{11,4},{12,4},{13,4},{14,4},{16,7},{17,7},{18,7},{19,7}}});
wordlist[NL].push_back((struct word){"15", {{15,4},{16,4},{17,4},{18,4},{16,7},{17,7},{18,7},{19,7}}});
wordlist[NL].push_back((struct word){"16", {{11,5},{12,5},{13,5},{16,7},{17,7},{18,7},{19,7}}});
wordlist[NL].push_back((struct word){"17", {{14,5},{15,5},{16,5},{17,5},{18,5},{16,7},{17,7},{18,7},{19,7}}});
wordlist[NL].push_back((struct word){"18", {{11,6},{12,6},{13,6},{14,6},{16,7},{17,7},{18,7},{19,7}}});
wordlist[NL].push_back((struct word){"19", {{15,6},{16,6},{17,6},{18,6},{19,6},{16,7},{17,7},{18,7},{19,7}}});
// Twentieth
wordlist[NL].push_back((struct word){"20", {{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"21", {{13,3},{14,3},{15,3},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"22", {{11,3},{12,3},{13,3},{14,3},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"23", {{16,3},{17,3},{18,3},{19,3},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"24", {{11,4},{12,4},{13,4},{14,4},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"25", {{15,4},{16,4},{17,4},{18,4},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"26", {{11,5},{12,5},{13,5},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"27", {{14,5},{15,5},{16,5},{17,5},{18,5},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"28", {{11,6},{12,6},{13,6},{14,6},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"29", {{15,6},{16,6},{17,6},{18,6},{19,6},{12,7},{13,7},{11,8},{12,8},{13,8},{14,8},{15,8},{16,8},{17,8}}});
wordlist[NL].push_back((struct word){"degrees", {{14,9},{15,9},{16,9},{17,9},{18,9},{19,9}}});
}
+162
View File
@@ -0,0 +1,162 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file wifi.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "string.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "wifi.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
static const char* ssid = "Kowalski";
static const char* pass = "madagascar";
//static const char* ssid = "vbchaos";
//static const char* pass = "mijninternet";
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
int Wifi::s_retry_num = 0;
const char* Wifi::TAG = "wifi station";
EventGroupHandle_t Wifi::s_wifi_event_group = xEventGroupCreate();
Wifi::Wifi()
{
}
void Wifi::event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (Wifi::s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
{
esp_wifi_connect();
s_retry_num++;
LOGGER_INFO("%s - retry to connect to the AP", TAG);
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
LOGGER_ERROR("%S - connect to the AP fail", TAG);
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
LOGGER_SUCCESS("%s - got ip: %d:%d:%d:%d", TAG, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void Wifi::start_client(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
strncpy((char*)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
strncpy((char*)wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
wifi_config.sta.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD;
wifi_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
LOGGER_INFO("%s - wifi_init_sta finished", TAG);
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT)
{
LOGGER_SUCCESS("%s - connected to ap SSID:%s password:%s", TAG, ssid, pass);
}
else if (bits & WIFI_FAIL_BIT)
{
LOGGER_ERROR("%s - Failed to connect to SSID:%s, password:%s", TAG, ssid, pass);
}
else
{
LOGGER_ERROR("%s - UNEXPECTED EVENT", TAG);
}
}
+131
View File
@@ -0,0 +1,131 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file wordmap.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include "wordmap.h"
#include "logger.h"
#include <algorithm>
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
Wordmap::Wordmap(LEDMatrix* matrix)
{
Wordmap::matrix = matrix;
Wordmap::language = NL;
Wordmap::red = 0xFF;
Wordmap::green = 0xFF;
Wordmap::blue = 0xFF;
}
bool Wordmap::setWord(Language_t lang, string identifier, bool value)
{
bool returnValue;
auto _compare = [&](struct word currentword)
{
if (identifier.compare(currentword.identifier) == 0)
{
return true;
}
else
{
return false;
}
};
// Create a list Iterator
std::list<struct word>::iterator it;
// Fetch the iterator of element with value 'the'
it = find_if(wordlist[lang].begin(), wordlist[lang].end(), _compare);
// Check if iterator points to end or not
if(it != wordlist[lang].end())
{
returnValue = true;
}
else
{
returnValue = false;
}
if (returnValue)
{
std::list<LEDMatrix::coordinate>::iterator pixel;
for (pixel = it->pixels.begin(); pixel != it->pixels.end(); pixel++)
{
matrix->setPixelValue(pixel->x, pixel->y, value);
matrix->setPixelColour(pixel->x, pixel->y, Wordmap::red, Wordmap::green, Wordmap::blue);
}
}
return returnValue;
}
bool Wordmap::setColour(uint8_t red, uint8_t green, uint8_t blue)
{
bool returnValue = true;
Wordmap::red = red;
Wordmap::green = green;
Wordmap::blue = blue;
return returnValue;
}
void Wordmap::createList_NL(void)
{
// First, clear the list
wordlist[NL].clear();
}
void Wordmap::createList_EN(void)
{
// First, clear the list
wordlist[EN].clear();
}