#include "NDP.h" #include // BLE Service & Characteristic UUIDs BLEService matchService("12345678-1234-1234-1234-1234567890AB"); BLEByteCharacteristic matchChar("87654321-4321-4321-4321-BA0987654321", BLERead | BLENotify); volatile uint8_t matchID = 0; volatile bool matchPending = false; volatile int lastLabelIndex = -1; // letztes gesendetes Label merken // Alle Labels in Array definieren const char* labels[] = { "Combat", "Helmet", "Iron Man", "Repulsor", "Shutdown", "Startup", "Stones", "noise", "unknown" }; // Callback bei Match void sendMatch(int labelIndex) { // Nur neues Label senden, um Dauerfeuer zu verhindern if (labelIndex == lastLabelIndex) return; lastLabelIndex = labelIndex; if (labelIndex < 0 || labelIndex >= 9) return; const char* label = labels[labelIndex]; Serial.print("Detected label: "); Serial.println(label); // LED kurz blau aufleuchten nicla::leds.begin(); nicla::leds.setColor(blue); delay(200); nicla::leds.setColor(off); nicla::leds.end(); // BLE Byte setzen je nach Label if (strcmp(label, "Helmet") == 0) matchID = 0x01; else if (strcmp(label, "Iron Man") == 0) matchID = 0x02; else if (strcmp(label, "Shutdown") == 0) matchID = 0x03; else if (strcmp(label, "Startup") == 0) matchID = 0x04; else if (strcmp(label, "Repulsor") == 0) matchID = 0x05; else if (strcmp(label, "Stones") == 0) matchID = 0x06; else if (strcmp(label, "Combat") == 0) matchID = 0x07; // Neues Match zur Übertragung markieren matchPending = true; } void setup() { Serial.begin(115200); // Nicla Setup nicla::begin(); nicla::disableLDO(); nicla::leds.begin(); // NDP Setup NDP.begin("mcu_fw_120_v91.synpkg"); NDP.load("dsp_firmware_v91.synpkg"); NDP.load("ei_model.synpkg"); NDP.turnOnMicrophone(); NDP.interrupts(); // Callback korrekt registrieren (Index statt char*) NDP.onMatch(sendMatch); // BLE starten if (!BLE.begin()) { Serial.println("BLE failed!"); while (1); } BLE.setLocalName("NiclaVoiceBLE"); BLE.setAdvertisedService(matchService); matchService.addCharacteristic(matchChar); BLE.addService(matchService); BLE.advertise(); Serial.println("BLE Peripheral started"); } void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); while (central.connected()) { // Nur senden, wenn ein neues Match vorliegt if (matchPending && matchID != 0) { matchChar.writeValue(matchID); // sendet das Byte automatisch über BLE Serial.print("Sent matchID: 0x"); Serial.println(matchID, HEX); matchID = 0; // zurücksetzen, damit nicht mehrfach gesendet wird matchPending = false; // Match wurde übertragen } delay(10); } Serial.println("Central disconnected"); } }