Files
wordclock/code/main/main.cpp
T
Matthias Mitscherlich 027dd38eb9 Added led matrix
2023-01-17 16:30:14 +01:00

189 lines
5.1 KiB
C++

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_sntp.h"
#include "esp_wifi.h"
#include "time.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "driver/rmt_tx.h"
#include "driver/uart_select.h"
#include "inc/gpio.h"
#include "inc/led_strip_encoder.h"
#include "inc/ledmatrix.h"
#include "inc/logger.h"
#include "inc/wifi.h"
static const uart_port_t uartPort = UART_NUM_0;
static TaskHandle_t devTaskHandle = NULL;
static void devTask(void* parameters);
static GPIO gpio0(4, GPIO_DIRECTION_OUTPUT);
static GPIO gpio1(18, GPIO_DIRECTION_OUTPUT);
static time_t currentTime;
static rmt_channel_handle_t led_chan = NULL;
static rmt_transmit_config_t tx_config;
static rmt_encoder_handle_t led_encoder = NULL;
#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
#define RMT_LED_STRIP_GPIO_NUM 8
extern "C" void app_main(void)
{
esp_log_level_set("*", ESP_LOG_WARN);
esp_err_t ret = nvs_flash_init();
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
//--------------------------------------------
// UART
//
const uart_config_t uartConfig =
{
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 0,
.source_clk = UART_SCLK_DEFAULT
};
ESP_ERROR_CHECK(uart_param_config(uartPort, &uartConfig));
ESP_ERROR_CHECK(uart_set_pin(uartPort, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(uartPort, 1024, 1024, 0, NULL, 0));
//--------------------------------------------
// LOGGER
//
Logger logger(10, uartPort);
LOGGER_DEBUG("Let's start the WORDCLOCK");
//--------------------------------------------
// RMT Channel
//
LOGGER_INFO("Create RMT TX channel");
rmt_tx_channel_config_t tx_chan_config;
memset(&tx_chan_config, 0, sizeof(tx_chan_config));
tx_chan_config.clk_src = RMT_CLK_SRC_DEFAULT; // select source clock
tx_chan_config.gpio_num = RMT_LED_STRIP_GPIO_NUM;
tx_chan_config.mem_block_symbols = 64; // increase the block size can make the LED less flickering
tx_chan_config.resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ;
tx_chan_config.trans_queue_depth = 4;
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan));
LOGGER_INFO("Install led strip encoder");
led_strip_encoder_config_t encoder_config;
memset(&encoder_config, 0, sizeof(encoder_config));
encoder_config.resolution = RMT_LED_STRIP_RESOLUTION_HZ;
ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder));
LOGGER_INFO("Enable RMT TX channel");
ESP_ERROR_CHECK(rmt_enable(led_chan));
memset(&tx_config, 0, sizeof(tx_config));
tx_config.loop_count = 0;
//--------------------------------------------
// LED Matrix
//
LEDMatrix_Parameters_t ledmatrix_parameters;
ledmatrix_parameters.columOrientation = LEDMATRIX_ORIENTATION_COLUM_DOWN_UP;
ledmatrix_parameters.height = 10;
ledmatrix_parameters.matrixOrientation = LEDMATRIX_ORIENTATION_COLUM;
ledmatrix_parameters.rmtChannel = &led_chan;
ledmatrix_parameters.rmtConfig = &tx_config;
ledmatrix_parameters.rmtEncoder = &led_encoder;
ledmatrix_parameters.rowOrientation = LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT;
ledmatrix_parameters.width = 11;
LEDMatrix LEDMatrix(&ledmatrix_parameters);
// Create the development task
if(xTaskCreate(devTask, "DevTask", 2048, NULL, 3, &devTaskHandle) != pdPASS)
{
LOGGER_ERROR("Task not created");
}
Wifi wifi;
wifi.start_client();
// Start NTP
setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 1);
tzset();
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
sntp_init();
unsigned int row = 0;
unsigned int colum = 0;
while (true)
{
// struct tm tm;
// time(&currentTime);
// 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);
//
// vTaskDelay(300);
LOGGER_DEBUG("Enable pixel (%i:%i)", row, colum);
LEDMatrix.setPixelValue(row, colum, true);
vTaskDelay(100);
LEDMatrix.setPixelValue(row, colum, false);
if (row<10)
{
row++;
}
else
{
row = 0;
if(colum < 2)
{
colum++;
}
else
{
colum = 0;
}
}
}
}
static void devTask(void* parameters)
{
uint32_t counter = 0;
printf("DevTask created");
while (true)
{
(void)gpio0.SetOutput((GPIO_Value_t)(counter % 2));
(void)gpio1.SetOutput((GPIO_Value_t)(counter % 7));
counter++;
vTaskDelay(100);
}
}