Initial commit: Base Project for Kode Dot (ESP32-S3)
This commit is contained in:
+188
@@ -0,0 +1,188 @@
|
||||
#include <Arduino.h>
|
||||
#include <lvgl.h>
|
||||
#include <kodedot/display_manager.h>
|
||||
#include <TCA9555.h>
|
||||
#include <kodedot/pin_config.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
// Display manager instance
|
||||
DisplayManager display;
|
||||
|
||||
// UI labels
|
||||
static lv_obj_t *touch_label;
|
||||
static lv_obj_t *button_label;
|
||||
|
||||
// IO Expander
|
||||
static TCA9555 ioexp(IOEXP_I2C_ADDR);
|
||||
|
||||
// NeoPixel
|
||||
static Adafruit_NeoPixel pixel(NEO_PIXEL_COUNT, NEO_PIXEL_PIN, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
// Forward declarations
|
||||
void createUserInterface();
|
||||
void createFontExamples(lv_obj_t *parent);
|
||||
void updateTouchDisplay();
|
||||
void updateButtonDisplay();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting Base Project with LVGL...");
|
||||
|
||||
// Initialize display subsystem
|
||||
if (!display.init()) {
|
||||
Serial.println("Error: Failed to initialize display");
|
||||
while(1) {
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
// Create the UI
|
||||
createUserInterface();
|
||||
|
||||
// Initialize IO Expander (inputs)
|
||||
if (!ioexp.begin(INPUT)) {
|
||||
Serial.println("Warning: IO Expander not connected");
|
||||
}
|
||||
|
||||
// Configure TOP button (GPIO with external pull-up)
|
||||
pinMode(BUTTON_TOP, INPUT);
|
||||
|
||||
// Initialize NeoPixel
|
||||
pixel.begin();
|
||||
pixel.setBrightness(64);
|
||||
pixel.clear();
|
||||
pixel.show();
|
||||
|
||||
Serial.println("System ready!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Pump display subsystem
|
||||
display.update();
|
||||
|
||||
// Update touch coordinates on the UI
|
||||
updateTouchDisplay();
|
||||
updateButtonDisplay();
|
||||
|
||||
delay(5);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create the main user interface
|
||||
*/
|
||||
void createUserInterface() {
|
||||
lv_obj_t * scr = lv_scr_act();
|
||||
lv_obj_set_style_bg_color(scr, lv_color_hex(0x111111), 0);
|
||||
|
||||
// Title
|
||||
lv_obj_t * title = lv_label_create(scr);
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_30, 0);
|
||||
lv_label_set_text(title, "Base Project");
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 30);
|
||||
|
||||
// Label for touch coordinates
|
||||
touch_label = lv_label_create(scr);
|
||||
lv_obj_set_style_text_font(touch_label, &lv_font_montserrat_22, 0);
|
||||
lv_label_set_text(touch_label, "Touch: (-, -)");
|
||||
lv_obj_set_style_text_color(touch_label, lv_color_hex(0x00FF00), 0);
|
||||
lv_obj_align(touch_label, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
// Label for buttons state (expander)
|
||||
button_label = lv_label_create(scr);
|
||||
lv_obj_set_style_text_font(button_label, &lv_font_montserrat_18, 0);
|
||||
lv_label_set_text(button_label, "Button: none");
|
||||
lv_obj_set_style_text_color(button_label, lv_color_hex(0x66CCFF), 0);
|
||||
lv_obj_align(button_label, LV_ALIGN_BOTTOM_MID, 0, -10);
|
||||
|
||||
// Ejemplos de diferentes fuentes
|
||||
createFontExamples(scr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create sample labels using different font sizes
|
||||
*/
|
||||
void createFontExamples(lv_obj_t *parent) {
|
||||
// Small font
|
||||
lv_obj_t * font_small = lv_label_create(parent);
|
||||
lv_obj_set_style_text_font(font_small, &lv_font_montserrat_14, 0);
|
||||
lv_label_set_text(font_small, "Montserrat 14px");
|
||||
lv_obj_set_style_text_color(font_small, lv_color_hex(0xCCCCCC), 0);
|
||||
lv_obj_align(font_small, LV_ALIGN_BOTTOM_LEFT, 20, -80);
|
||||
|
||||
// Medium font
|
||||
lv_obj_t * font_medium = lv_label_create(parent);
|
||||
lv_obj_set_style_text_font(font_medium, &lv_font_montserrat_18, 0);
|
||||
lv_label_set_text(font_medium, "Montserrat 18px");
|
||||
lv_obj_set_style_text_color(font_medium, lv_color_hex(0xCCCCCC), 0);
|
||||
lv_obj_align(font_medium, LV_ALIGN_BOTTOM_LEFT, 20, -50);
|
||||
|
||||
// Large font
|
||||
lv_obj_t * font_large = lv_label_create(parent);
|
||||
lv_obj_set_style_text_font(font_large, &lv_font_montserrat_42, 0);
|
||||
lv_label_set_text(font_large, "42");
|
||||
lv_obj_set_style_text_color(font_large, lv_color_hex(0x999999), 0);
|
||||
lv_obj_align(font_large, LV_ALIGN_BOTTOM_RIGHT, -30, -30);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update the touch coordinates label
|
||||
*/
|
||||
void updateTouchDisplay() {
|
||||
if (!touch_label) return;
|
||||
|
||||
int16_t x, y;
|
||||
if (display.getTouchCoordinates(x, y)) {
|
||||
lv_label_set_text_fmt(touch_label, "Touch: (%d, %d)", x, y);
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool isPressed(uint8_t pinIndex) {
|
||||
// Entradas con pull-up externa: activo en LOW
|
||||
int v = ioexp.read1(pinIndex);
|
||||
return (v != TCA9555_INVALID_READ) && (v == LOW);
|
||||
}
|
||||
|
||||
static inline bool isGpioPressed(int gpio) {
|
||||
return digitalRead(gpio) == LOW; // activo en LOW por pull-up externa
|
||||
}
|
||||
|
||||
void updateButtonDisplay() {
|
||||
if (!button_label) return;
|
||||
|
||||
const char* status = "none";
|
||||
|
||||
if (isGpioPressed(BUTTON_TOP)) {
|
||||
status = "BUTTON_TOP";
|
||||
} else if (isPressed(EXPANDER_BUTTON_BOTTOM)) {
|
||||
status = "BUTTON_BOTTOM";
|
||||
} else if (isPressed(EXPANDER_PAD_TOP)) {
|
||||
status = "PAD_TOP";
|
||||
} else if (isPressed(EXPANDER_PAD_BOTTOM)) {
|
||||
status = "PAD_BOTTOM";
|
||||
} else if (isPressed(EXPANDER_PAD_LEFT)) {
|
||||
status = "PAD_LEFT";
|
||||
} else if (isPressed(EXPANDER_PAD_RIGHT)) {
|
||||
status = "PAD_RIGHT";
|
||||
}
|
||||
|
||||
lv_label_set_text_fmt(button_label, "Button: %s", status);
|
||||
|
||||
// Actualizar NeoPixel según botón
|
||||
uint32_t color = pixel.Color(0, 0, 0);
|
||||
if (status == (const char*)"BUTTON_TOP") {
|
||||
color = pixel.Color(255, 0, 0); // rojo
|
||||
} else if (status == (const char*)"BOTON_INFERIOR") {
|
||||
color = pixel.Color(0, 0, 255); // azul
|
||||
} else if (status == (const char*)"PAD_TOP") {
|
||||
color = pixel.Color(255, 255, 0); // amarillo
|
||||
} else if (status == (const char*)"PAD_BOTTOM") {
|
||||
color = pixel.Color(255, 0, 255); // magenta
|
||||
} else if (status == (const char*)"PAD_LEFT") {
|
||||
color = pixel.Color(0, 255, 0); // verde
|
||||
} else if (status == (const char*)"PAD_RIGHT") {
|
||||
color = pixel.Color(0, 255, 255); // cian
|
||||
}
|
||||
pixel.setPixelColor(0, color);
|
||||
pixel.show();
|
||||
}
|
||||
Reference in New Issue
Block a user