Prześlij pliki do "firmware/pylonisamp_v0_2_5"

This commit is contained in:
admin committed 2026-10-05 09:47:41 +02:00
1 parent b412e60593
commit 2408859f39
1 file changed
+260
@@ -0,0 +1,260 @@
/**
* PylonisAmp — Firmware RP2040
* Wersja: v0.2.5 (Poprawiona komunikacja USB Serial)
*/
#include <Arduino.h>
#include <ArduinoJson.h>
#include <TFT_eSPI.h>
#include <Adafruit_NeoPixel.h>
// ── Piny ──────────────────────────────────────────
#define PIN_WS2812 2
#define PIN_TFT_BL 22
// ── Konfiguracja ──────────────────────────────────
#define FW_VERSION "v0.2.5"
#define NUM_LEDS 8
#define HB_TIMEOUT 10000
#define HB_INTERVAL 3000
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite bottomBar = TFT_eSprite(&tft);
Adafruit_NeoPixel ring(NUM_LEDS, PIN_WS2812, NEO_GRB + NEO_KHZ800);
// Statyczny bufor 240×240 RGB565 (115 200 B)
static uint16_t img_buffer[240 * 240];
uint32_t last_hb_rx = 0;
uint32_t last_hb_tx = 0;
bool rpi_connected = false;
bool was_disconnected = true;
// Stan odtwarzania
char np_station[64] = "RADIO";
char np_title[128] = "Czekam na dane...";
char np_artist[128] = "";
int np_volume = 0;
char np_time[16] = "00:00";
uint32_t last_touch_ms = 0;
// ── Funkcje pomocnicze ────────────────────────────
void tx_json(JsonDocument &doc) {
serializeJson(doc, Serial);
Serial.println();
}
// ── Setup ──────────────────────────────────────────
void setup() {
Serial.begin(115200); // USB CDC (Komunikacja z RPi)
ring.begin();
ring.setBrightness(40);
ring.show();
pinMode(PIN_TFT_BL, OUTPUT);
digitalWrite(PIN_TFT_BL, HIGH);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
bottomBar.createSprite(480, 60);
// Splash Screen
tft.setTextColor(TFT_CYAN, TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.drawString("Pylonis Amp", 240, 130, 4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(FW_VERSION, 240, 170, 2);
tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
tft.drawString("Czekam na Pi...", 240, 200, 2);
last_hb_rx = millis();
// Sygnał gotowości
JsonDocument doc;
doc["evt"] = "ready";
doc["fw"] = FW_VERSION;
tx_json(doc);
}
// ── Loop ───────────────────────────────────────────
void loop() {
if (Serial.available()) {
String line = Serial.readStringUntil('\n');
line.trim();
if (line.length() > 0) handle_json(line);
}
if (millis() - last_hb_tx > HB_INTERVAL) {
JsonDocument doc;
doc["evt"] = "hb";
doc["ok"] = 1;
tx_json(doc);
last_hb_tx = millis();
}
connection_watchdog();
check_touch();
}
void handle_json(String& line) {
JsonDocument doc;
if (deserializeJson(doc, line)) return;
const char* cmd = doc["cmd"] | "";
if (strcmp(cmd, "hb") == 0) {
rpi_connected = true;
last_hb_rx = millis();
return;
}
if (strcmp(cmd, "reboot_bootloader") == 0) {
rp2040.rebootToBootloader();
return;
}
if (strcmp(cmd, "img_start") == 0) {
uint32_t sz = doc["size"] | 0;
if (sz == 0 || sz > sizeof(img_buffer)) return;
Serial.setTimeout(2000);
size_t got = Serial.readBytes((char*)img_buffer, sz);
Serial.setTimeout(1000);
if (got == sz) {
tft.pushImage(0, 40, 240, 240, img_buffer);
}
return;
}
if (strcmp(cmd, "state") == 0) {
rpi_connected = true;
last_hb_rx = millis();
if (doc["station"]) strncpy(np_station, doc["station"] | "RADIO", 63);
if (doc["title"]) strncpy(np_title, doc["title"] | "---", 127);
if (doc["artist"]) strncpy(np_artist, doc["artist"] | "", 127);
if (doc["volume"]) np_volume = doc["volume"] | 0;
draw_full_ui();
draw_fast_bottom();
return;
}
if (strcmp(cmd, "tick") == 0) {
rpi_connected = true;
last_hb_rx = millis();
if (doc["volume"]) np_volume = doc["volume"] | 0;
if (doc["time"]) strncpy(np_time, doc["time"] | "00:00", 15);
draw_fast_bottom();
return;
}
if (strcmp(cmd, "meters") == 0) {
draw_meters(doc["data"]);
return;
}
if (strcmp(cmd, "led") == 0) {
JsonArray data = doc["data"];
for (int i = 0; i < min((int)data.size(), NUM_LEDS); i++) {
uint8_t v = data[i];
ring.setPixelColor(i, ring.Color(0, v/4, v));
}
ring.show();
return;
}
}
void draw_full_ui() {
if (was_disconnected) {
tft.fillScreen(TFT_BLACK);
was_disconnected = false;
}
tft.fillRect(0, 0, 480, 38, 0x0841);
tft.drawFastHLine(0, 38, 480, TFT_DARKGREY);
tft.setTextColor(TFT_CYAN, 0x0841);
tft.setTextDatum(MC_DATUM);
tft.drawString(np_station, 240, 19, 2);
tft.fillRect(245, 40, 235, 120, TFT_BLACK);
tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
tft.setTextDatum(TL_DATUM);
tft.drawString("ARTYSTA", 248, 48, 1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(np_artist, 248, 62, 2);
tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
tft.drawString("TYTUL", 248, 110, 1);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.drawString(np_title, 248, 124, 2);
tft.fillRect(245, 160, 235, 20, TFT_BLACK);
tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
tft.drawString("ANALIZATOR", 248, 165, 1);
}
void draw_meters(JsonArray data) {
int x_start = 250;
int y_start = 180;
int bar_w = 5;
int max_h = 80;
// Rysujemy bezpośrednio na ekranie (szybciej dla RAM)
for (int i = 0; i < min((int)data.size(), 32); i++) {
float val = data[i];
int h = map(constrain((int)val, -60, 0), -60, 0, 0, max_h);
uint16_t color = (val > -10) ? TFT_RED : ((val > -30) ? TFT_YELLOW : 0x07E0); // Neonowa zieleń
// Czyść tło nad słupkiem i rysuj słupek
tft.fillRect(x_start + i*7, y_start, bar_w, max_h - h, TFT_BLACK);
tft.fillRect(x_start + i*7, y_start + (max_h - h), bar_w, h, color);
}
}
void draw_fast_bottom() {
bottomBar.fillSprite(TFT_BLACK);
char vbuf[12]; snprintf(vbuf, sizeof(vbuf), "VOL %d%%", np_volume);
bottomBar.setTextColor(TFT_WHITE, TFT_BLACK);
bottomBar.setTextDatum(ML_DATUM);
bottomBar.drawString(np_time, 12, 18, 4);
bottomBar.setTextDatum(MR_DATUM);
bottomBar.drawString(vbuf, 468, 18, 4);
int bw = map(np_volume, 0, 100, 0, 456);
bottomBar.fillRect(12, 38, 456, 8, 0x18E3);
bottomBar.fillRoundRect(12, 38, bw, 8, 2, TFT_CYAN);
bottomBar.pushSprite(0, 282);
}
void check_touch() {
uint16_t tx, ty;
if (tft.getTouch(&tx, &ty)) {
uint32_t now = millis();
if (now - last_touch_ms < 500) return;
last_touch_ms = now;
ty = 320 - ty;
JsonDocument doc;
doc["evt"] = "touch";
doc["x"] = tx;
doc["y"] = ty;
tx_json(doc);
}
}
void draw_disconnected() {
if (!was_disconnected) {
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.drawString("BRAK POLACZENIA", 240, 140, 4);
was_disconnected = true;
}
}
void connection_watchdog() {
if (rpi_connected && (millis() - last_hb_rx > HB_TIMEOUT)) {
rpi_connected = false;
draw_disconnected();
}
}