From b6be2a1af3dc3ba19578451db670244167514dda Mon Sep 17 00:00:00 2001 From: Luca Doglione Date: Mon, 22 Jul 2024 12:29:36 +0200 Subject: [PATCH 1/2] Adding BLE properties --- FlappyLED.ino | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/FlappyLED.ino b/FlappyLED.ino index 7552415..d1432fb 100644 --- a/FlappyLED.ino +++ b/FlappyLED.ino @@ -6,6 +6,8 @@ #include "crash_animation.h" #include "modes_animation.h" #include "intro_animation.h" +#include + //#define DEBUG 1 //Enable debug prints in Serial Monitor : 115200 @@ -30,6 +32,7 @@ #define BUZZER //Modulino Buzzer needed: play sounds while playing #define ANIMATIONS //Enable Animations #define RESET_TIME //Automatically reset after some time +#define BLE_SYNC 1 //Enable BLE Sync /* Game Modes */ /* It is the state of the game*/ @@ -55,6 +58,7 @@ static uint16_t score = 0, time_to_wait = 70; static uint8_t gameMode = MENU; static float tofOldDistance = 0; static bool mute = false; +static int localRecord = 0; byte frame[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, @@ -67,6 +71,15 @@ byte frame[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; + +#ifdef BLE_SYNC +/* BT Service and properties */ +BleSync ble("FlappyLed", "d4d1cb67-d83e-41b6-bfc9-95d25ca6a91d", 2); +BleSyncValue bleRecordEncoder("a3cd7bd3-4d6e-492f-9f18-f9bf181be541", BLERead | BLEWrite); +BleSyncValue bleCounter("17a8a342-41bd-4dc0-98af-62d30a0d4432", BLERead | BLEWrite); +#endif + + void setup() { // put your setup code here, to run once: @@ -113,6 +126,15 @@ void setup() { digitalWrite(LED_BUILTIN, HIGH); #endif + #ifdef BLE_SYNC + + ble.addValue(&bleRecordEncoder); + ble.addValue(&bleCounter); + ble.initBLE(); + + #endif + + } void loop() { @@ -137,7 +159,7 @@ void loop() { if (game_ongoing) { if(score == 0){ - delay(800); + delay(500); } /* Remove the wall from the matrix */ @@ -550,6 +572,16 @@ void resetGameState() buttons.setLeds(false, mute, false); #endif + #ifdef BLE_SYNC + /* Store local record */ + if(score > localRecord){ + localRecord = score; + bleRecordEncoder.setValue(localRecord); + } + + bleCounter.setValue(bleCounter.getValue()+1); + #endif + print_score(score); score = 0; wall_pos_x = WIDTH-1; @@ -564,7 +596,14 @@ void resetGameState() tofOldDistance = 0; /*Wait some time after finish*/ - delay(3000); + #if !defined(BLE_SYNC) + delay(4000); + #endif + + #ifdef BLE_SYNC + ble.sync(4000); + #endif + clear_text(); game_ongoing = 0; } From c2d39f9596964eb344f04e83f5f0984a7a61769c Mon Sep 17 00:00:00 2001 From: Luca Doglione Date: Mon, 22 Jul 2024 17:28:01 +0200 Subject: [PATCH 2/2] Adding all BLE properties --- FlappyLED.ino | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/FlappyLED.ino b/FlappyLED.ino index d1432fb..c3dd15b 100644 --- a/FlappyLED.ino +++ b/FlappyLED.ino @@ -58,7 +58,6 @@ static uint16_t score = 0, time_to_wait = 70; static uint8_t gameMode = MENU; static float tofOldDistance = 0; static bool mute = false; -static int localRecord = 0; byte frame[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, @@ -74,8 +73,11 @@ byte frame[8][12] = { #ifdef BLE_SYNC /* BT Service and properties */ -BleSync ble("FlappyLed", "d4d1cb67-d83e-41b6-bfc9-95d25ca6a91d", 2); +BleSync ble("FlappyLed", "d4d1cb67-d83e-41b6-bfc9-95d25ca6a91d", 5); BleSyncValue bleRecordEncoder("a3cd7bd3-4d6e-492f-9f18-f9bf181be541", BLERead | BLEWrite); +BleSyncValue bleGamesEncoder("59802c1a-c0d8-4b35-9c6c-b258304e078b", BLERead | BLEWrite); +BleSyncValue bleRecordTof("2380e50e-562d-4e46-aeaf-d73b9d258f73", BLERead | BLEWrite); +BleSyncValue bleGamesTof("a8dd61cd-02b9-465b-8f6d-a185aa65bce0", BLERead | BLEWrite); BleSyncValue bleCounter("17a8a342-41bd-4dc0-98af-62d30a0d4432", BLERead | BLEWrite); #endif @@ -130,6 +132,10 @@ void setup() { ble.addValue(&bleRecordEncoder); ble.addValue(&bleCounter); + ble.addValue(&bleGamesEncoder); + ble.addValue(&bleRecordTof); + ble.addValue(&bleGamesTof); + ble.addValue(&bleCounter); ble.initBLE(); #endif @@ -541,6 +547,23 @@ void clear_text() void resetGameState() { + #ifdef BLE_SYNC + /* Store local record */ + + if(gameMode == ENCODER) { + if(score > bleRecordEncoder.getValue()){ + bleRecordEncoder.setValue(score); + } + bleGamesEncoder.setValue(bleGamesEncoder.getValue()+1); + } else if(gameMode == TOF) { + if(score > bleRecordTof.getValue()){ + bleRecordTof.setValue(score); + } + bleGamesTof.setValue(bleGamesTof.getValue()+1); + } + bleCounter.setValue(bleCounter.getValue()+1); + #endif + /* Reset game mode*/ gameMode = MENU; @@ -572,16 +595,6 @@ void resetGameState() buttons.setLeds(false, mute, false); #endif - #ifdef BLE_SYNC - /* Store local record */ - if(score > localRecord){ - localRecord = score; - bleRecordEncoder.setValue(localRecord); - } - - bleCounter.setValue(bleCounter.getValue()+1); - #endif - print_score(score); score = 0; wall_pos_x = WIDTH-1;