亮闪闪 ESP32-S3 固件
点按钮复制完整代码,然后到 Arduino IDE 里全选旧代码并粘贴。
复制完整固件代码
#include
#include
#include
#include
#include
// Match the web app defaults in app.js. static const char *DEVICE_NAME = "亮闪闪"; static const char *SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"; static const char *WRITE_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"; // Change this to the GPIO connected to your LED matrix data input. static const uint8_t LED_PIN = 18; static const uint8_t WIDTH = 11; static const uint8_t HEIGHT = 20; static const uint16_t NUM_LEDS = WIDTH * HEIGHT; static const uint16_t FRAME_BYTES = NUM_LEDS * 3; static const uint8_t SELF_TEST_BRIGHTNESS = 24; static const uint8_t IDLE_BRIGHTNESS = 20; static const uint8_t CONNECT_BRIGHTNESS = 32; static const uint8_t FRAME_BRIGHTNESS = 50; // Row direction, top to bottom. // true = left to right // false = right to left // // Your ESP32-S3 matrix is mostly serpentine, with these left-to-right runs: // row 1, rows 5/6, rows 10/11. Rows here are written 1-based in comments. static const bool ROW_LEFT_TO_RIGHT[HEIGHT] = { true, // row 1 false, // row 2 true, // row 3 false, // row 4 true, // row 5 true, // row 6 false, // row 7 true, // row 8 false, // row 9 true, // row 10 true, // row 11 false, // row 12 true, // row 13 false, // row 14 true, // row 15 false, // row 16 true, // row 17 false, // row 18 true, // row 19 false // row 20 }; Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); uint8_t frame[FRAME_BYTES]; uint16_t frameOffset = 0; bool hasReceivedFrame = false; bool deviceConnected = false; bool pendingConnectStatus = false; bool pendingDisconnectStatus = false; uint16_t matrixIndex(uint8_t x, uint8_t y) { if (!ROW_LEFT_TO_RIGHT[y]) { x = WIDTH - 1 - x; } return y * WIDTH + x; } void clearFrame() { frameOffset = 0; memset(frame, 0, sizeof(frame)); } void setMatrixPixel(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b) { if (x >= WIDTH || y >= HEIGHT) return; strip.setPixelColor(matrixIndex(x, y), strip.Color(r, g, b)); } void renderPowerOnSelfTest() { strip.setBrightness(SELF_TEST_BRIGHTNESS); for (uint16_t i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(32, 0, 0)); } strip.show(); delay(450); for (uint16_t i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(0, 24, 0)); } strip.show(); delay(450); for (uint16_t i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(0, 0, 32)); } strip.show(); delay(450); } void renderIdleStar() { strip.setBrightness(IDLE_BRIGHTNESS); strip.clear(); // Low-brightness eight-point star for power-on board health. setMatrixPixel(5, 5, 255, 230, 120); setMatrixPixel(5, 6, 255, 210, 80); setMatrixPixel(5, 7, 255, 250, 180); setMatrixPixel(5, 8, 255, 210, 80); setMatrixPixel(5, 9, 255, 230, 120); setMatrixPixel(3, 7, 255, 210, 80); setMatrixPixel(4, 7, 255, 250, 180); setMatrixPixel(6, 7, 255, 250, 180); setMatrixPixel(7, 7, 255, 210, 80); setMatrixPixel(4, 6, 120, 220, 255); setMatrixPixel(6, 6, 120, 220, 255); setMatrixPixel(4, 8, 120, 220, 255); setMatrixPixel(6, 8, 120, 220, 255); setMatrixPixel(2, 3, 70, 180, 255); setMatrixPixel(8, 4, 70, 180, 255); setMatrixPixel(2, 14, 70, 180, 255); setMatrixPixel(8, 16, 70, 180, 255); strip.show(); } void renderConnectedStatus() { strip.setBrightness(CONNECT_BRIGHTNESS); strip.clear(); // Small check mark and glow: BLE connected. setMatrixPixel(2, 10, 34, 197, 94); setMatrixPixel(3, 11, 34, 197, 94); setMatrixPixel(4, 12, 34, 197, 94); setMatrixPixel(5, 11, 34, 197, 94); setMatrixPixel(6, 10, 34, 197, 94); setMatrixPixel(7, 9, 34, 197, 94); setMatrixPixel(8, 8, 34, 197, 94); setMatrixPixel(5, 5, 56, 189, 248); setMatrixPixel(5, 6, 56, 189, 248); setMatrixPixel(4, 6, 56, 189, 248); setMatrixPixel(6, 6, 56, 189, 248); setMatrixPixel(5, 7, 255, 250, 180); strip.show(); } void renderFrame() { strip.setBrightness(FRAME_BRIGHTNESS); for (uint8_t y = 0; y < HEIGHT; y++) { for (uint8_t x = 0; x < WIDTH; x++) { uint16_t source = (y * WIDTH + x) * 3; uint8_t r = frame[source]; uint8_t g = frame[source + 1]; uint8_t b = frame[source + 2]; setMatrixPixel(x, y, r, g, b); } } strip.show(); hasReceivedFrame = true; } class ServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer *server) override { deviceConnected = true; pendingConnectStatus = true; Serial.println("BLE connected"); } void onDisconnect(BLEServer *server) override { deviceConnected = false; pendingDisconnectStatus = true; BLEDevice::startAdvertising(); Serial.println("BLE disconnected"); } }; class WriteCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *characteristic) override { String value = characteristic->getValue(); if (value.length() == 0) return; uint8_t command = value[0]; if (command == 'R') { clearFrame(); return; } if (command == 'D') { for (int i = 1; i < value.length() && frameOffset < FRAME_BYTES; i++) { frame[frameOffset++] = static_cast
(value[i]); } return; } if (command == 'E') { if (frameOffset == FRAME_BYTES) { renderFrame(); } return; } } }; void setupBle() { BLEDevice::init(DEVICE_NAME); BLEServer *server = BLEDevice::createServer(); server->setCallbacks(new ServerCallbacks()); BLEService *service = server->createService(SERVICE_UUID); BLECharacteristic *writeCharacteristic = service->createCharacteristic( WRITE_UUID, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR ); writeCharacteristic->setCallbacks(new WriteCallbacks()); service->start(); BLEAdvertising *advertising = BLEDevice::getAdvertising(); advertising->addServiceUUID(SERVICE_UUID); advertising->setScanResponse(true); advertising->start(); } void setup() { Serial.begin(115200); clearFrame(); strip.begin(); renderPowerOnSelfTest(); renderIdleStar(); setupBle(); Serial.println("PixelForge BLE matrix ready"); } void loop() { if (pendingConnectStatus) { pendingConnectStatus = false; renderConnectedStatus(); delay(800); if (!hasReceivedFrame) { renderIdleStar(); } } if (pendingDisconnectStatus) { pendingDisconnectStatus = false; if (!hasReceivedFrame) { renderIdleStar(); } } delay(20); }