229 lines
6.6 KiB
Arduino
229 lines
6.6 KiB
Arduino
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
#include <TFT_eSPI.h>
|
|
#include <U8g2_for_TFT_eSPI.h>
|
|
#include <Adafruit_NeoPixel.h>
|
|
|
|
#define FW_VERSION "0.2.13"
|
|
|
|
// <<< DO NOT MODIFY HARDWARE CONFIG START >>>
|
|
#define BAUD_RATE 500000
|
|
#define NUM_LEDS 8
|
|
#define PIN_WS2812 2
|
|
#define PIN_TFT_BL 22
|
|
#define PYLONIS_BLUE 0x249E
|
|
#define PYLONIS_DARK 0x0841
|
|
|
|
TFT_eSPI tft = TFT_eSPI();
|
|
U8g2_for_TFT_eSPI u8g2;
|
|
Adafruit_NeoPixel ring(NUM_LEDS, PIN_WS2812, NEO_GRB + NEO_KHZ800);
|
|
TFT_eSprite bottomBar = TFT_eSprite(&tft);
|
|
TFT_eSprite spectrumSprite = TFT_eSprite(&tft);
|
|
// <<< DO NOT MODIFY HARDWARE CONFIG END >>>
|
|
|
|
// GLOBALNE ZMIENNE
|
|
char np_station[64] = "RADIO", np_title[128] = "Czekam...", np_artist[128] = "";
|
|
String display_mode = "spectrum";
|
|
uint32_t last_data_rx = 0;
|
|
bool is_warning_state = false;
|
|
bool img_loaded = false;
|
|
uint32_t last_hb = 0;
|
|
|
|
// Bufor szeregowy
|
|
char serial_buf[256];
|
|
int serial_idx = 0;
|
|
|
|
// <<< STABLE CORE: TOUCH & METERS START >>>
|
|
void draw_meters(JsonArray data) {
|
|
if (display_mode == "none") {
|
|
spectrumSprite.fillSprite(TFT_BLACK);
|
|
spectrumSprite.pushSprite(245, 180);
|
|
return;
|
|
}
|
|
spectrumSprite.fillSprite(TFT_BLACK);
|
|
|
|
// FIX #6: UV mode — Pico dostaje 2 wartości [rms_l, rms_r] zamiast 32 pasm
|
|
if (display_mode == "uv") {
|
|
// Dla UV: data powinno mieć co najmniej 2 elementy
|
|
int l = 0, r = 0;
|
|
if (data.size() >= 2) {
|
|
l = map(constrain(data[0].as<int>(), -60, 0), -60, 0, 0, 90);
|
|
r = map(constrain(data[1].as<int>(), -60, 0), -60, 0, 0, 90);
|
|
}
|
|
spectrumSprite.fillRect(20, 90-l, 80, l, PYLONIS_BLUE);
|
|
spectrumSprite.fillRect(135, 90-r, 80, r, PYLONIS_BLUE);
|
|
} else {
|
|
// Spectrum: 32 pasma (lub mniej jeśli mniej danych)
|
|
int num_bands = min((int)data.size(), 32);
|
|
int bar_w = max(2, 235 / max(num_bands, 1) - 2); // dynamiczna szerokość
|
|
for (int i = 0; i < num_bands; i++) {
|
|
int h = map(constrain(data[i].as<int>(), -60, 0), -60, 0, 0, 90);
|
|
int x = i * (bar_w + 2);
|
|
spectrumSprite.fillRect(x, 90 - h, bar_w, h, PYLONIS_BLUE);
|
|
}
|
|
}
|
|
spectrumSprite.pushSprite(245, 180);
|
|
}
|
|
|
|
void check_touch() {
|
|
uint16_t tx, ty;
|
|
if (tft.getTouch(&tx, &ty)) {
|
|
static uint32_t last_touch_ms = 0;
|
|
if (millis() - last_touch_ms > 400) {
|
|
last_touch_ms = millis();
|
|
if (display_mode == "spectrum") display_mode = "uv";
|
|
else if (display_mode == "uv") display_mode = "none";
|
|
else display_mode = "spectrum";
|
|
Serial.printf("{\"evt\":\"touch\",\"x\":%d,\"y\":%d,\"mode\":\"%s\"}\n", tx, ty, display_mode.c_str());
|
|
}
|
|
}
|
|
}
|
|
// <<< STABLE CORE: TOUCH & METERS END >>>
|
|
|
|
void setup() {
|
|
Serial.begin(BAUD_RATE);
|
|
while(Serial.available() > 0) { Serial.read(); }
|
|
|
|
ring.begin();
|
|
ring.setBrightness(25);
|
|
ring.show();
|
|
pinMode(PIN_TFT_BL, OUTPUT);
|
|
digitalWrite(PIN_TFT_BL, HIGH);
|
|
tft.init();
|
|
tft.setRotation(1);
|
|
tft.fillScreen(TFT_BLACK);
|
|
uint16_t calData[5] = { 280, 3500, 350, 3300, 6 };
|
|
tft.setTouch(calData);
|
|
bottomBar.createSprite(480, 60);
|
|
spectrumSprite.createSprite(235, 100);
|
|
|
|
u8g2.begin(tft);
|
|
u8g2.setFont(u8g2_font_unifont_t_polish);
|
|
u8g2.setForegroundColor(TFT_WHITE);
|
|
u8g2.setBackgroundColor(TFT_BLACK);
|
|
|
|
// Test czcionki
|
|
strncpy(np_title, "Zażółć gęślą jaźń", 127);
|
|
strncpy(np_artist, "TEST UTF-8", 127);
|
|
draw_full_ui();
|
|
}
|
|
|
|
void loop() {
|
|
// 1. Emergency flush: jeśli Pico nie nadąża, czyścimy bufor
|
|
if (Serial.available() > 256) {
|
|
while(Serial.available() > 64) Serial.read();
|
|
}
|
|
|
|
// 2. Heartbeat: Pico prosi o dane (co 100ms)
|
|
static uint32_t last_req = 0;
|
|
if (millis() - last_req > 100) {
|
|
Serial.println("{\"cmd\":\"req\"}");
|
|
last_req = millis();
|
|
}
|
|
|
|
check_touch();
|
|
update_leds();
|
|
|
|
while (Serial.available() > 0) {
|
|
char c = Serial.read();
|
|
if (c == '\n') {
|
|
serial_buf[serial_idx] = 0;
|
|
handle_json(String(serial_buf));
|
|
serial_idx = 0;
|
|
} else if (serial_idx < 255) {
|
|
serial_buf[serial_idx++] = c;
|
|
}
|
|
}
|
|
|
|
// FIX #7: Resetuj watchdog przy każdym odbiorze danych (nie tylko cmd)
|
|
// Watchdog
|
|
if (millis() - last_data_rx > 2000) {
|
|
is_warning_state = true;
|
|
}
|
|
|
|
delay(1); // Pozwala stosowi USB na przetworzenie danych
|
|
}
|
|
|
|
void update_leds() {
|
|
uint32_t now = millis();
|
|
static uint32_t last_led_move = 0;
|
|
static int current_led = 0;
|
|
|
|
if (is_warning_state) {
|
|
// Ostrzeżenie: Pomarańczowy puls
|
|
ring.clear();
|
|
for(int i=0; i<NUM_LEDS; i++) ring.setPixelColor(i, ring.Color(50, 20, 0));
|
|
ring.show();
|
|
return;
|
|
}
|
|
|
|
if (now - last_led_move > 500) {
|
|
last_led_move = now;
|
|
current_led = (current_led + 1) % NUM_LEDS;
|
|
}
|
|
|
|
uint32_t cycle_ms = now % 2000;
|
|
int v = (cycle_ms < 1000) ? map(cycle_ms, 0, 1000, 10, 50) : map(cycle_ms, 1000, 2000, 50, 10);
|
|
|
|
ring.clear();
|
|
for(int i=0; i<NUM_LEDS; i++) {
|
|
int bri = (i == current_led) ? v : v/5;
|
|
ring.setPixelColor(i, ring.Color(0, bri/2, bri));
|
|
}
|
|
ring.show();
|
|
}
|
|
|
|
void handle_json(String line) {
|
|
JsonDocument doc;
|
|
if (deserializeJson(doc, line)) return;
|
|
const char* cmd = doc["cmd"] | "";
|
|
|
|
// FIX #7b: Resetuj watchdog przy KAŻDYM poprawnym JSONie (nie tylko cmd)
|
|
last_data_rx = millis();
|
|
is_warning_state = false;
|
|
|
|
if (strcmp(cmd, "state") == 0) {
|
|
const char* new_artist = doc["artist"] | "";
|
|
const char* new_title = doc["title"] | "";
|
|
|
|
// Dirty checking: jeśli dane się zmieniły, resetuj okładkę
|
|
if (strcmp(np_artist, new_artist) != 0 || strcmp(np_title, new_title) != 0) {
|
|
strncpy(np_artist, new_artist, 127);
|
|
strncpy(np_title, new_title, 127);
|
|
strncpy(np_station, doc["station"] | "", 63);
|
|
|
|
img_loaded = false; // Reset statusu okładki
|
|
tft.fillRect(245, 120, 235, 120, TFT_BLACK); // Wyczyść obszar okładki
|
|
draw_full_ui();
|
|
}
|
|
} else if (strcmp(cmd, "meters") == 0) {
|
|
// FIX #8: Sprawdź czy data istnieje
|
|
if (doc.containsKey("data")) {
|
|
draw_meters(doc["data"].as<JsonArray>());
|
|
}
|
|
} else if (strcmp(cmd, "fast") == 0) {
|
|
// FIX #9: Obsługa fast update (volume, time) bez pełnego renderowania
|
|
// Można tu dodać aktualizację volume na ekranie jeśli potrzebna
|
|
} else if (strcmp(cmd, "eq") == 0) {
|
|
// FIX #10: Obsługa EQ od Pythona
|
|
// TODO: Zaimplementować wyświetlanie EQ na ekranie
|
|
} else if (strcmp(cmd, "img_qoi") == 0) {
|
|
// FIX #11: Obsługa transferu okładki
|
|
// TODO: Zaimplementować odbiór chunków QOI
|
|
}
|
|
}
|
|
|
|
void draw_full_ui() {
|
|
tft.fillRect(0, 0, 480, 38, PYLONIS_DARK);
|
|
tft.drawFastHLine(0, 38, 480, TFT_DARKGREY);
|
|
|
|
u8g2.setCursor(10, 25);
|
|
u8g2.print(np_station);
|
|
|
|
// Dodatkowe rysowanie tytułu i artysty
|
|
u8g2.setCursor(10, 55);
|
|
u8g2.print(np_title);
|
|
u8g2.setCursor(10, 80);
|
|
u8g2.print(np_artist);
|
|
}
|