connect via MQTTS
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
291
firmware/main/src/app_task.cpp
Normal file
291
firmware/main/src/app_task.cpp
Normal file
@@ -0,0 +1,291 @@
|
||||
#include "app_task.h"
|
||||
|
||||
#include "analytics.h"
|
||||
#include "button_handling.h"
|
||||
#include "common.h"
|
||||
#include "common/InactivityTracker.h"
|
||||
#include "hal/u8g2_esp32_hal.h"
|
||||
#include "i2c_checker.h"
|
||||
#include "led_status.h"
|
||||
#include "message_manager.h"
|
||||
#include "my_mqtt_client.h"
|
||||
#include "persistence_manager.h"
|
||||
#include "simulator.h"
|
||||
#include "ui/ClockScreenSaver.h"
|
||||
#include "ui/ScreenSaver.h"
|
||||
#include "ui/SplashScreen.h"
|
||||
#include "wifi_manager.h"
|
||||
#include <cstring>
|
||||
#include <driver/i2c.h>
|
||||
#include <esp_diagnostics.h>
|
||||
#include <esp_insights.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_task_wdt.h>
|
||||
#include <esp_timer.h>
|
||||
#include <sdkconfig.h>
|
||||
#include <u8g2.h>
|
||||
|
||||
#define PIN_RST GPIO_NUM_NC
|
||||
|
||||
static const char *TAG = "app_task";
|
||||
|
||||
u8g2_t u8g2;
|
||||
uint8_t last_value = 0;
|
||||
menu_options_t options;
|
||||
uint8_t received_signal;
|
||||
|
||||
std::shared_ptr<Widget> m_widget;
|
||||
std::vector<std::shared_ptr<Widget>> m_history;
|
||||
std::unique_ptr<InactivityTracker> m_inactivityTracker;
|
||||
// Persistence Manager for C-API
|
||||
persistence_manager_t g_persistence_manager;
|
||||
|
||||
extern QueueHandle_t buttonQueue;
|
||||
|
||||
static void setup_screen(void)
|
||||
{
|
||||
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
|
||||
u8g2_esp32_hal.bus.i2c.sda = I2C_MASTER_SDA_PIN;
|
||||
u8g2_esp32_hal.bus.i2c.scl = I2C_MASTER_SCL_PIN;
|
||||
u8g2_esp32_hal.reset = PIN_RST;
|
||||
u8g2_esp32_hal_init(u8g2_esp32_hal);
|
||||
|
||||
u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
|
||||
u8x8_SetI2CAddress(&u8g2.u8x8, DISPLAY_I2C_ADDRESS << 1);
|
||||
|
||||
ESP_DIAG_EVENT(TAG, "u8g2_InitDisplay");
|
||||
u8g2_InitDisplay(&u8g2);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
ESP_DIAG_EVENT(TAG, "u8g2_SetPowerSave");
|
||||
u8g2_SetPowerSave(&u8g2, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
u8g2_ClearDisplay(&u8g2);
|
||||
}
|
||||
|
||||
void setScreen(const std::shared_ptr<Widget> &screen)
|
||||
{
|
||||
if (screen != nullptr)
|
||||
{
|
||||
ESP_DIAG_EVENT(TAG, "Screen set: %s", screen->getName());
|
||||
m_widget = screen;
|
||||
m_history.clear();
|
||||
m_history.emplace_back(m_widget);
|
||||
m_widget->onEnter();
|
||||
}
|
||||
}
|
||||
|
||||
void pushScreen(const std::shared_ptr<Widget> &screen)
|
||||
{
|
||||
if (screen != nullptr)
|
||||
{
|
||||
if (m_widget)
|
||||
{
|
||||
m_widget->onPause();
|
||||
}
|
||||
ESP_DIAG_EVENT(TAG, "Screen pushed: %s", screen->getName());
|
||||
m_widget = screen;
|
||||
m_widget->onEnter();
|
||||
m_history.emplace_back(m_widget);
|
||||
}
|
||||
}
|
||||
|
||||
void popScreen()
|
||||
{
|
||||
if (m_history.size() >= 2)
|
||||
{
|
||||
m_history.pop_back();
|
||||
if (m_widget)
|
||||
{
|
||||
persistence_manager_save(&g_persistence_manager);
|
||||
m_widget->onExit();
|
||||
}
|
||||
m_widget = m_history.back();
|
||||
ESP_DIAG_EVENT(TAG, "Screen popped, now: %s", m_widget->getName());
|
||||
m_widget->onResume();
|
||||
}
|
||||
}
|
||||
|
||||
static void init_ui(void)
|
||||
{
|
||||
persistence_manager_init(&g_persistence_manager, "config");
|
||||
options = {
|
||||
.u8g2 = &u8g2,
|
||||
.setScreen = [](const std::shared_ptr<Widget> &screen) { setScreen(screen); },
|
||||
.pushScreen = [](const std::shared_ptr<Widget> &screen) { pushScreen(screen); },
|
||||
.popScreen = []() { popScreen(); },
|
||||
.onButtonClicked = nullptr,
|
||||
.persistenceManager = &g_persistence_manager,
|
||||
};
|
||||
m_widget = std::make_shared<SplashScreen>(&options);
|
||||
m_inactivityTracker = std::make_unique<InactivityTracker>(60000, []() {
|
||||
auto screensaver = std::make_shared<ClockScreenSaver>(&options);
|
||||
options.pushScreen(screensaver);
|
||||
});
|
||||
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
m_widget->Render();
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
}
|
||||
|
||||
static void on_message_received(const message_t *msg)
|
||||
{
|
||||
if (msg && msg->type == MESSAGE_TYPE_SETTINGS &&
|
||||
(std::strcmp(msg->data.settings.key, "light_active") == 0 ||
|
||||
std::strcmp(msg->data.settings.key, "light_variant") == 0 ||
|
||||
std::strcmp(msg->data.settings.key, "light_mode") == 0))
|
||||
{
|
||||
start_simulation();
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_button(uint8_t button)
|
||||
{
|
||||
m_inactivityTracker->reset();
|
||||
|
||||
if (m_widget)
|
||||
{
|
||||
switch (button)
|
||||
{
|
||||
case 1:
|
||||
m_widget->OnButtonClicked(ButtonType::UP);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
m_widget->OnButtonClicked(ButtonType::LEFT);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
m_widget->OnButtonClicked(ButtonType::RIGHT);
|
||||
break;
|
||||
|
||||
case 6:
|
||||
m_widget->OnButtonClicked(ButtonType::DOWN);
|
||||
break;
|
||||
|
||||
case 16:
|
||||
m_widget->OnButtonClicked(ButtonType::BACK);
|
||||
break;
|
||||
|
||||
case 18:
|
||||
m_widget->OnButtonClicked(ButtonType::SELECT);
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unhandled button: %u", button);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_task(void *args)
|
||||
{
|
||||
if (i2c_bus_scan_and_check() != ESP_OK)
|
||||
{
|
||||
led_behavior_t led_behavior = {
|
||||
.on_time_ms = 1000,
|
||||
.off_time_ms = 500,
|
||||
.color = {.red = 50, .green = 0, .blue = 0},
|
||||
.index = 0,
|
||||
.mode = LED_MODE_BLINK,
|
||||
};
|
||||
led_status_set_behavior(led_behavior);
|
||||
|
||||
ESP_LOGE(TAG, "Display not found on I2C bus");
|
||||
vTaskDelete(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize display so that info can be shown
|
||||
setup_screen();
|
||||
|
||||
// Check BACK button and delete settings if necessary (with countdown)
|
||||
gpio_config_t io_conf = {};
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << BUTTON_BACK);
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
if (gpio_get_level(BUTTON_BACK) == 0)
|
||||
{
|
||||
u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
|
||||
for (int i = 5; i > 0; --i)
|
||||
{
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
u8g2_DrawStr(&u8g2, 5, 20, "BACK gedrueckt!");
|
||||
u8g2_DrawStr(&u8g2, 5, 35, "Halte fuer Reset...");
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "Loesche in %d s", i);
|
||||
u8g2_DrawStr(&u8g2, 5, 55, buf);
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
if (gpio_get_level(BUTTON_BACK) != 0)
|
||||
{
|
||||
// Button released, abort
|
||||
break;
|
||||
}
|
||||
if (i == 1)
|
||||
{
|
||||
// After 5 seconds still pressed: perform factory reset
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
u8g2_DrawStr(&u8g2, 5, 30, "Alle Einstellungen ");
|
||||
u8g2_DrawStr(&u8g2, 5, 45, "werden geloescht...");
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
persistence_manager_factory_reset();
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
u8g2_DrawStr(&u8g2, 5, 35, "Fertig. Neustart...");
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
esp_restart();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
message_manager_init();
|
||||
|
||||
setup_buttons();
|
||||
init_ui();
|
||||
|
||||
wifi_manager_init();
|
||||
|
||||
mqtt_client_start();
|
||||
|
||||
message_manager_register_listener(on_message_received);
|
||||
|
||||
start_simulation();
|
||||
|
||||
auto oldTime = esp_timer_get_time();
|
||||
|
||||
while (true)
|
||||
{
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
|
||||
if (m_widget != nullptr)
|
||||
{
|
||||
auto currentTime = esp_timer_get_time();
|
||||
auto delta = currentTime - oldTime;
|
||||
oldTime = currentTime;
|
||||
|
||||
uint64_t deltaMs = delta / 1000;
|
||||
|
||||
m_widget->Update(deltaMs);
|
||||
m_widget->Render();
|
||||
|
||||
m_inactivityTracker->update(deltaMs);
|
||||
}
|
||||
|
||||
u8g2_SendBuffer(&u8g2);
|
||||
|
||||
if (xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE)
|
||||
{
|
||||
handle_button(received_signal);
|
||||
}
|
||||
}
|
||||
|
||||
cleanup_buttons();
|
||||
}
|
||||
93
firmware/main/src/button_handling.c
Normal file
93
firmware/main/src/button_handling.c
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "button_handling.h"
|
||||
|
||||
#include "button_gpio.h"
|
||||
#include "common.h"
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_insights.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_mac.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <iot_button.h>
|
||||
#include <sdkconfig.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "button_handling";
|
||||
|
||||
const uint8_t gpios[] = {BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK};
|
||||
static const char *button_names[] = {"DOWN", "UP", "LEFT", "RIGHT", "SELECT", "BACK"};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t gpio;
|
||||
uint8_t index;
|
||||
} button_user_data_t;
|
||||
|
||||
static button_user_data_t button_data[6];
|
||||
|
||||
QueueHandle_t buttonQueue = NULL;
|
||||
|
||||
static void button_event_cb(void *arg, void *usr_data)
|
||||
{
|
||||
if (buttonQueue == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Button queue not initialized!");
|
||||
return;
|
||||
}
|
||||
button_user_data_t *data = (button_user_data_t *)usr_data;
|
||||
uint8_t gpio_num = data->gpio;
|
||||
const char *button_name = button_names[data->index];
|
||||
|
||||
ESP_DIAG_EVENT(TAG, "Button %s pressed (GPIO %d)", button_name, gpio_num);
|
||||
|
||||
if (xQueueSend(buttonQueue, &gpio_num, 0) != pdTRUE)
|
||||
{
|
||||
ESP_LOGW(TAG, "Failed to send button press to queue");
|
||||
}
|
||||
}
|
||||
|
||||
static void init_button(uint8_t gpio, int index)
|
||||
{
|
||||
const button_config_t btn_cfg = {0};
|
||||
const button_gpio_config_t btn_gpio_cfg = {
|
||||
.gpio_num = gpio,
|
||||
.active_level = 0,
|
||||
.enable_power_save = true,
|
||||
};
|
||||
button_handle_t gpio_btn = NULL;
|
||||
const esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &gpio_btn);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Button create failed");
|
||||
}
|
||||
|
||||
button_data[index].gpio = gpio;
|
||||
button_data[index].index = index;
|
||||
iot_button_register_cb(gpio_btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, &button_data[index]);
|
||||
}
|
||||
|
||||
void setup_buttons(void)
|
||||
{
|
||||
buttonQueue = xQueueCreate(10, sizeof(uint8_t));
|
||||
if (buttonQueue == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create button queue");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_DIAG_EVENT(TAG, "Button queue created successfully");
|
||||
for (int i = 0; i < sizeof(gpios) / sizeof(gpios[0]); i++)
|
||||
{
|
||||
init_button(gpios[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup function (optional)
|
||||
void cleanup_buttons(void)
|
||||
{
|
||||
if (buttonQueue != NULL)
|
||||
{
|
||||
vQueueDelete(buttonQueue);
|
||||
}
|
||||
}
|
||||
292
firmware/main/src/hal/u8g2_esp32_hal.c
Normal file
292
firmware/main/src/hal/u8g2_esp32_hal.c
Normal file
@@ -0,0 +1,292 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "hal/u8g2_esp32_hal.h"
|
||||
|
||||
static const char *TAG = "u8g2_hal";
|
||||
static const unsigned int I2C_TIMEOUT_MS = 1000;
|
||||
|
||||
static spi_device_handle_t handle_spi; // SPI handle.
|
||||
static i2c_cmd_handle_t handle_i2c; // I2C handle.
|
||||
static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
|
||||
static bool i2c_transfer_failed = false; // Flag to track I2C transfer errors
|
||||
|
||||
#define HOST SPI2_HOST
|
||||
|
||||
#undef ESP_ERROR_CHECK
|
||||
#define ESP_ERROR_CHECK(x) \
|
||||
do \
|
||||
{ \
|
||||
esp_err_t rc = (x); \
|
||||
if (rc != ESP_OK) \
|
||||
{ \
|
||||
ESP_LOGE("err", "esp_err_t = %d", rc); \
|
||||
assert(0 && #x); \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
// Softer error handling for I2C operations that may fail temporarily
|
||||
#define I2C_ERROR_CHECK(x) \
|
||||
do \
|
||||
{ \
|
||||
esp_err_t rc = (x); \
|
||||
if (rc != ESP_OK) \
|
||||
{ \
|
||||
ESP_LOGW(TAG, "I2C error: %s = %d", #x, rc); \
|
||||
i2c_transfer_failed = true; \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
/*
|
||||
* Initialze the ESP32 HAL.
|
||||
*/
|
||||
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param)
|
||||
{
|
||||
u8g2_esp32_hal = u8g2_esp32_hal_param;
|
||||
} // u8g2_esp32_hal_init
|
||||
|
||||
/*
|
||||
* HAL callback function as prescribed by the U8G2 library. This callback is
|
||||
* invoked to handle SPI communications.
|
||||
*/
|
||||
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
ESP_LOGD(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
|
||||
switch (msg)
|
||||
{
|
||||
case U8X8_MSG_BYTE_SET_DC:
|
||||
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
|
||||
}
|
||||
break;
|
||||
|
||||
case U8X8_MSG_BYTE_INIT: {
|
||||
if (u8g2_esp32_hal.bus.spi.clk == U8G2_ESP32_HAL_UNDEFINED ||
|
||||
u8g2_esp32_hal.bus.spi.mosi == U8G2_ESP32_HAL_UNDEFINED ||
|
||||
u8g2_esp32_hal.bus.spi.cs == U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
spi_bus_config_t bus_config;
|
||||
memset(&bus_config, 0, sizeof(spi_bus_config_t));
|
||||
bus_config.sclk_io_num = u8g2_esp32_hal.bus.spi.clk; // CLK
|
||||
bus_config.mosi_io_num = u8g2_esp32_hal.bus.spi.mosi; // MOSI
|
||||
bus_config.miso_io_num = GPIO_NUM_NC; // MISO
|
||||
bus_config.quadwp_io_num = GPIO_NUM_NC; // Not used
|
||||
bus_config.quadhd_io_num = GPIO_NUM_NC; // Not used
|
||||
// ESP_LOGI(TAG, "... Initializing bus.");
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(HOST, &bus_config, 1));
|
||||
|
||||
spi_device_interface_config_t dev_config;
|
||||
dev_config.address_bits = 0;
|
||||
dev_config.command_bits = 0;
|
||||
dev_config.dummy_bits = 0;
|
||||
dev_config.mode = 0;
|
||||
dev_config.duty_cycle_pos = 0;
|
||||
dev_config.cs_ena_posttrans = 0;
|
||||
dev_config.cs_ena_pretrans = 0;
|
||||
dev_config.clock_speed_hz = 10000;
|
||||
dev_config.spics_io_num = u8g2_esp32_hal.bus.spi.cs;
|
||||
dev_config.flags = 0;
|
||||
dev_config.queue_size = 200;
|
||||
dev_config.pre_cb = NULL;
|
||||
dev_config.post_cb = NULL;
|
||||
// ESP_LOGI(TAG, "... Adding device bus.");
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(HOST, &dev_config, &handle_spi));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_SEND: {
|
||||
spi_transaction_t trans_desc;
|
||||
trans_desc.addr = 0;
|
||||
trans_desc.cmd = 0;
|
||||
trans_desc.flags = 0;
|
||||
trans_desc.length = 8 * arg_int; // Number of bits NOT number of bytes.
|
||||
trans_desc.rxlength = 0;
|
||||
trans_desc.tx_buffer = arg_ptr;
|
||||
trans_desc.rx_buffer = NULL;
|
||||
|
||||
// ESP_LOGI(TAG, "... Transmitting %d bytes.", arg_int);
|
||||
ESP_ERROR_CHECK(spi_device_transmit(handle_spi, &trans_desc));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} // u8g2_esp32_spi_byte_cb
|
||||
|
||||
/*
|
||||
* HAL callback function as prescribed by the U8G2 library. This callback is
|
||||
* invoked to handle I2C communications.
|
||||
*/
|
||||
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
ESP_LOGD(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
|
||||
|
||||
switch (msg)
|
||||
{
|
||||
case U8X8_MSG_BYTE_SET_DC: {
|
||||
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_INIT: {
|
||||
if (u8g2_esp32_hal.bus.i2c.sda == U8G2_ESP32_HAL_UNDEFINED ||
|
||||
u8g2_esp32_hal.bus.i2c.scl == U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
i2c_config_t conf = {0};
|
||||
conf.mode = I2C_MODE_MASTER;
|
||||
ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.bus.i2c.sda);
|
||||
conf.sda_io_num = u8g2_esp32_hal.bus.i2c.sda;
|
||||
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.bus.i2c.scl);
|
||||
conf.scl_io_num = u8g2_esp32_hal.bus.i2c.scl;
|
||||
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ);
|
||||
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
|
||||
ESP_LOGI(TAG, "i2c_param_config %d", conf.mode);
|
||||
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
|
||||
ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
|
||||
ESP_ERROR_CHECK(
|
||||
i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_SEND: {
|
||||
if (i2c_transfer_failed)
|
||||
{
|
||||
break; // Skip sending if transfer already failed
|
||||
}
|
||||
uint8_t *data_ptr = (uint8_t *)arg_ptr;
|
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE);
|
||||
|
||||
while (arg_int > 0)
|
||||
{
|
||||
I2C_ERROR_CHECK(i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN));
|
||||
if (i2c_transfer_failed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
data_ptr++;
|
||||
arg_int--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_START_TRANSFER: {
|
||||
uint8_t i2c_address = u8x8_GetI2CAddress(u8x8);
|
||||
handle_i2c = i2c_cmd_link_create();
|
||||
i2c_transfer_failed = false; // Reset error flag at start of transfer
|
||||
ESP_LOGD(TAG, "Start I2C transfer to %02X.", i2c_address >> 1);
|
||||
I2C_ERROR_CHECK(i2c_master_start(handle_i2c));
|
||||
I2C_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN));
|
||||
break;
|
||||
}
|
||||
|
||||
case U8X8_MSG_BYTE_END_TRANSFER: {
|
||||
ESP_LOGD(TAG, "End I2C transfer.");
|
||||
if (!i2c_transfer_failed)
|
||||
{
|
||||
I2C_ERROR_CHECK(i2c_master_stop(handle_i2c));
|
||||
I2C_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, pdMS_TO_TICKS(I2C_TIMEOUT_MS)));
|
||||
}
|
||||
i2c_cmd_link_delete(handle_i2c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} // u8g2_esp32_i2c_byte_cb
|
||||
|
||||
/*
|
||||
* HAL callback function as prescribed by the U8G2 library. This callback is
|
||||
* invoked to handle callbacks for GPIO and delay functions.
|
||||
*/
|
||||
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
|
||||
{
|
||||
ESP_LOGD(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
|
||||
|
||||
switch (msg)
|
||||
{
|
||||
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and
|
||||
// RESET have been specified then we define those pins as GPIO outputs.
|
||||
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
|
||||
uint64_t bitmask = 0;
|
||||
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
bitmask = bitmask | (1ull << u8g2_esp32_hal.dc);
|
||||
}
|
||||
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
bitmask = bitmask | (1ull << u8g2_esp32_hal.reset);
|
||||
}
|
||||
if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
bitmask = bitmask | (1ull << u8g2_esp32_hal.bus.spi.cs);
|
||||
}
|
||||
|
||||
if (bitmask == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
gpio_config_t gpioConfig;
|
||||
gpioConfig.pin_bit_mask = bitmask;
|
||||
gpioConfig.mode = GPIO_MODE_OUTPUT;
|
||||
gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
gpioConfig.pull_down_en = GPIO_PULLDOWN_ENABLE;
|
||||
gpioConfig.intr_type = GPIO_INTR_DISABLE;
|
||||
gpio_config(&gpioConfig);
|
||||
break;
|
||||
}
|
||||
|
||||
// Set the GPIO reset pin to the value passed in through arg_int.
|
||||
case U8X8_MSG_GPIO_RESET:
|
||||
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
gpio_set_level(u8g2_esp32_hal.reset, arg_int);
|
||||
}
|
||||
break;
|
||||
// Set the GPIO client select pin to the value passed in through arg_int.
|
||||
case U8X8_MSG_GPIO_CS:
|
||||
if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
gpio_set_level(u8g2_esp32_hal.bus.spi.cs, arg_int);
|
||||
}
|
||||
break;
|
||||
// Set the Software I²C pin to the value passed in through arg_int.
|
||||
case U8X8_MSG_GPIO_I2C_CLOCK:
|
||||
if (u8g2_esp32_hal.bus.i2c.scl != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
gpio_set_level(u8g2_esp32_hal.bus.i2c.scl, arg_int);
|
||||
// printf("%c",(arg_int==1?'C':'c'));
|
||||
}
|
||||
break;
|
||||
// Set the Software I²C pin to the value passed in through arg_int.
|
||||
case U8X8_MSG_GPIO_I2C_DATA:
|
||||
if (u8g2_esp32_hal.bus.i2c.sda != U8G2_ESP32_HAL_UNDEFINED)
|
||||
{
|
||||
gpio_set_level(u8g2_esp32_hal.bus.i2c.sda, arg_int);
|
||||
// printf("%c",(arg_int==1?'D':'d'));
|
||||
}
|
||||
break;
|
||||
|
||||
// Delay for the number of milliseconds passed in through arg_int.
|
||||
case U8X8_MSG_DELAY_MILLI:
|
||||
vTaskDelay(arg_int / portTICK_PERIOD_MS);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
} // u8g2_esp32_gpio_and_delay_cb
|
||||
74
firmware/main/src/i2c_checker.c
Normal file
74
firmware/main/src/i2c_checker.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "i2c_checker.h"
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_insights.h"
|
||||
#include "esp_log.h"
|
||||
#include "hal/u8g2_esp32_hal.h"
|
||||
|
||||
static const char *TAG = "i2c_checker";
|
||||
|
||||
esp_err_t i2c_device_check(i2c_port_t i2c_port, uint8_t device_address)
|
||||
{
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
// Send the device address with the write bit (LSB = 0)
|
||||
i2c_master_write_byte(cmd, (device_address << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_stop(cmd);
|
||||
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(100));
|
||||
|
||||
i2c_cmd_link_delete(cmd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t i2c_bus_scan_and_check(void)
|
||||
{
|
||||
// 1. Configure and install I2C bus
|
||||
i2c_config_t conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = I2C_MASTER_SDA_PIN,
|
||||
.scl_io_num = I2C_MASTER_SCL_PIN,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = I2C_MASTER_FREQ_HZ,
|
||||
};
|
||||
esp_err_t err = i2c_param_config(I2C_MASTER_NUM, &conf);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "I2C parameter configuration failed: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
err = i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "I2C driver installation failed: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "I2C driver initialized. Searching for device...");
|
||||
|
||||
// 2. Check if the device is present
|
||||
err = i2c_device_check(I2C_MASTER_NUM, DISPLAY_I2C_ADDRESS);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "Device found at address 0x%02X!", DISPLAY_I2C_ADDRESS);
|
||||
// Here you could now call e.g. setup_screen()
|
||||
}
|
||||
else if (err == ESP_ERR_TIMEOUT)
|
||||
{
|
||||
ESP_LOGE(TAG, "Timeout! Device at address 0x%02X is not responding.", DISPLAY_I2C_ADDRESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Error communicating with address 0x%02X: %s", DISPLAY_I2C_ADDRESS, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// 3. Uninstall I2C driver if it is no longer needed
|
||||
i2c_driver_delete(I2C_MASTER_NUM);
|
||||
ESP_DIAG_EVENT(TAG, "I2C driver uninstalled.");
|
||||
|
||||
return err;
|
||||
}
|
||||
40
firmware/main/src/main.cpp
Normal file
40
firmware/main/src/main.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "app_task.h"
|
||||
#include "color.h"
|
||||
#include "led_status.h"
|
||||
#include "led_strip_ws2812.h"
|
||||
#include "persistence_manager.h"
|
||||
#include "wifi_manager.h"
|
||||
#include <ble_manager.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_insights.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <sdkconfig.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
void app_main(void)
|
||||
{
|
||||
// Initialize NVS
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
}
|
||||
|
||||
persistence_manager_t persistence;
|
||||
persistence_manager_init(&persistence, "config");
|
||||
persistence_manager_load(&persistence);
|
||||
|
||||
led_status_init(CONFIG_STATUS_WLED_PIN);
|
||||
|
||||
led_strip_init();
|
||||
|
||||
xTaskCreatePinnedToCore(app_task, "app_task", 8192, NULL, tskIDLE_PRIORITY + 5, NULL,
|
||||
CONFIG_FREERTOS_NUMBER_OF_CORES - 1);
|
||||
// xTaskCreatePinnedToCore(ble_manager_task, "ble_manager", 4096, NULL, tskIDLE_PRIORITY + 1, NULL,
|
||||
// CONFIG_FREERTOS_NUMBER_OF_CORES - 1);
|
||||
}
|
||||
__END_DECLS
|
||||
Reference in New Issue
Block a user