From 6d8bac7aff910b1e22d201bd753a4ae6ba406608 Mon Sep 17 00:00:00 2001 From: Matt Hudson Date: Wed, 6 Mar 2024 20:37:51 +1100 Subject: [PATCH 01/10] Added basic disconnect handling --- lib/furble/Device.cpp | 4 ++++ lib/furble/Device.h | 5 +++++ src/furble.ino | 6 +++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/furble/Device.cpp b/lib/furble/Device.cpp index c0bceab..1b49a6b 100644 --- a/lib/furble/Device.cpp +++ b/lib/furble/Device.cpp @@ -180,4 +180,8 @@ void Device::getUUID128(uuid128_t *uuid) { } } +bool Device::isConnected(void){ + return m_Client->isConnected(); +} + } // namespace Furble diff --git a/lib/furble/Device.h b/lib/furble/Device.h index 1132043..c298cde 100644 --- a/lib/furble/Device.h +++ b/lib/furble/Device.h @@ -53,6 +53,11 @@ class Device { */ virtual bool connect(NimBLEClient *pClient, ezProgressBar &progress_bar) = 0; + /** + * Checks if the client is still connected. + */ + virtual bool isConnected(void); + /** * Disconnect from the target. */ diff --git a/src/furble.ino b/src/furble.ino index 0e56e88..0c33ee7 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -146,7 +146,7 @@ static void remote_interval(Furble::Device *device) { ez.msgBox("Interval Release", "", "Stop", false); trigger(device, j); - while (true) { + while (device->isConnected()) { i++; M5.update(); @@ -268,7 +268,7 @@ static void remote_control(Furble::Device *device) { ez.yield(); delay(50); - } while (true); + } while (device->isConnected()); } /** @@ -310,7 +310,7 @@ static void menu_remote(Furble::Device *device) { if (submenu.pickName() == "Interval") { remote_interval(device); } - } while (submenu.pickName() != "Disconnect"); + } while (submenu.pickName() != "Disconnect" && device->isConnected()); device->disconnect(); ez.backlight.inactivity(USER_SET); From c862a1bb9fc3c9680ec24ae4ef83b41bc8733515 Mon Sep 17 00:00:00 2001 From: Matt Hudson Date: Thu, 7 Mar 2024 18:07:36 +1100 Subject: [PATCH 02/10] More advanced reconnect function --- lib/M5ez/src/M5ez.cpp | 6 +++ lib/M5ez/src/M5ez.h | 1 + src/furble.ino | 90 +++++++++++++++++++++++++++++++++---------- 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index aa47bd0..65ec4b7 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -554,6 +554,10 @@ void ezButtons::show(String buttons) { } } +String ezButtons::get() { + return (_btn_a_s + "#" + _btn_b_s + "#" + _btn_c_s); +} + void ezButtons::clear(bool wipe /* = true */) { if (wipe && (_lower_button_row || _upper_button_row)) { M5.Lcd.fillRect(0, ez.canvas.bottom() + 1, TFT_H - ez.canvas.bottom() - 1, TFT_W, @@ -3467,6 +3471,8 @@ ezProgressBar::ezProgressBar(String header /* = "" */, bar_color = ez.theme->progressbar_color; _bar_color = bar_color; ez.screen.clear(); + M5.Lcd.fillRect(0, 0, TFT_W, TFT_H, ez.screen.background()); + if (header != "") ez.header.show(header); ez.buttons.show(buttons); diff --git a/lib/M5ez/src/M5ez.h b/lib/M5ez/src/M5ez.h index a1b8b82..118f7f2 100644 --- a/lib/M5ez/src/M5ez.h +++ b/lib/M5ez/src/M5ez.h @@ -321,6 +321,7 @@ class ezButtons { public: static void begin(); static void show(String buttons); + static String get(); static void clear(bool wipe = true); static void releaseWait(); static String poll(); diff --git a/src/furble.ino b/src/furble.ino index 0c33ee7..f283401 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -11,6 +11,8 @@ static NimBLEScan *pScan = nullptr; static std::vector connect_list; +static Furble::Device *device; + bool load_gps_enable(); static TinyGPSPlus gps; @@ -25,6 +27,8 @@ static const uint8_t GPS_HEADER_POSITION = CURRENT_POSITION + 1; static bool gps_enable = false; static bool gps_has_fix = false; +static bool reconnected = false; + /** * BLE Advertisement callback. */ @@ -61,7 +65,7 @@ static uint16_t service_grove_gps(void) { /** * Update geotag data. */ -static void update_geodata(Furble::Device *device) { +static void update_geodata() { if (!gps_enable) { return; } @@ -129,7 +133,7 @@ static void about(void) { ez.msgBox(FURBLE_STR " - About", "Version: " + version, "Back", true); } -static void trigger(Furble::Device *device, int counter) { +static void trigger(int counter) { device->focusPress(); delay(250); device->shutterPress(); @@ -139,16 +143,22 @@ static void trigger(Furble::Device *device, int counter) { device->shutterRelease(); } -static void remote_interval(Furble::Device *device) { +static void remote_interval(void) { int i = 0; int j = 1; ez.msgBox("Interval Release", "", "Stop", false); - trigger(device, j); + trigger(j); - while (device->isConnected()) { + while (true) { i++; - + + + if (reconnected){ + ez.msgBox("Interval Release", String(j), "Stop", false); + reconnected = false; + } + M5.update(); if (M5.BtnB.wasReleased()) { @@ -159,7 +169,7 @@ static void remote_interval(Furble::Device *device) { i = 0; j++; - trigger(device, j); + trigger(j); } delay(50); @@ -200,7 +210,7 @@ static void show_shutter_control(bool shutter_locked, unsigned long lock_start_m } } -static void remote_control(Furble::Device *device) { +static void remote_control(void) { static unsigned long shutter_lock_start_ms = 0; static bool shutter_lock = false; @@ -211,8 +221,13 @@ static void remote_control(Furble::Device *device) { do { M5.update(); - update_geodata(device); - + update_geodata(); + + if (reconnected){ + show_shutter_control(shutter_lock, shutter_lock_start_ms); + reconnected = false; + } + if (M5.BtnPWR.wasClicked() || M5.BtnC.wasPressed()) { if (shutter_lock) { // ensure shutter is released on exit @@ -268,7 +283,7 @@ static void remote_control(Furble::Device *device) { ez.yield(); delay(50); - } while (device->isConnected()); + } while (true); } /** @@ -291,7 +306,36 @@ static void do_saved(void) { menu_connect(false); } -static void menu_remote(Furble::Device *device) { +uint16_t disconnectDetect(void) { + if (!device) return 0; + + if (device->isConnected()) return 500; + + String buttons; + buttons = ez.buttons.get(); + + String header; + header = ez.header.title(); + + NimBLEClient *pClient = NimBLEDevice::createClient(); + ezProgressBar progress_bar(FURBLE_STR, "Reconnecting ...", ""); + if (device->connect(pClient, progress_bar)){ + ez.screen.clear(); + ez.header.show(header); + ez.buttons.show(buttons); + + reconnected = true; + + ez.redraw(); + return 500; + } + + + ez.screen.clear(); + return 0; +} + +static void menu_remote(void) { ez.backlight.inactivity(NEVER); ezMenu submenu(FURBLE_STR " - Connected"); submenu.buttons("OK#down"); @@ -299,19 +343,23 @@ static void menu_remote(Furble::Device *device) { submenu.addItem("Interval"); submenu.addItem("Disconnect"); submenu.downOnLast("first"); - + + ez.addEvent(disconnectDetect, 500); + do { submenu.runOnce(); if (submenu.pickName() == "Shutter") { - remote_control(device); + remote_control(); } if (submenu.pickName() == "Interval") { - remote_interval(device); + remote_interval(); } - } while (submenu.pickName() != "Disconnect" && device->isConnected()); - + } while (submenu.pickName() != "Disconnect"); + + ez.removeEvent(disconnectDetect); + device->disconnect(); ez.backlight.inactivity(USER_SET); } @@ -329,9 +377,9 @@ static void menu_connect(bool save) { if (i == 0) return; - Furble::Device *device = connect_list[i - 1]; + device = connect_list[i - 1]; - update_geodata(device); + update_geodata(); NimBLEClient *pClient = NimBLEDevice::createClient(); ezProgressBar progress_bar(FURBLE_STR, "Connecting ...", ""); @@ -339,11 +387,11 @@ static void menu_connect(bool save) { if (save) { device->save(); } - menu_remote(device); + menu_remote(); } } -static void menu_delete() { +static void menu_delete(void) { std::vector devices; ezMenu submenu(FURBLE_STR " - Delete"); submenu.buttons("OK#down"); From 9fa9fab6794ff8ec895fc9e9a2d7608f8f5c7242 Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Thu, 7 Mar 2024 20:59:46 +1030 Subject: [PATCH 03/10] Add context argument to addEvent function signature. This allows the event function to have context on call and avoid requiring global variables to maintain state. --- lib/M5ez/src/M5ez.cpp | 16 +++++++-------- lib/M5ez/src/M5ez.h | 17 ++++++++-------- lib/furble/Device.h | 10 +++++----- src/furble.ino | 46 +++++++++++++++++++++---------------------- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 65ec4b7..3c237c6 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -419,7 +419,7 @@ size_t ezCanvas::write(const uint8_t *buffer, size_t size) { return size; } -uint16_t ezCanvas::loop() { +uint16_t ezCanvas::loop(void *private_data) { if (_next_scroll && millis() >= _next_scroll) { ez.setFont(_font); uint8_t h = ez.fontHeight(); @@ -891,7 +891,7 @@ void ezBacklight::activity() { _last_activity = millis(); } -uint16_t ezBacklight::loop() { +uint16_t ezBacklight::loop(void *private_data) { if (!_backlight_off && _inactivity) { if (millis() > _last_activity + 30000 * _inactivity) { _backlight_off = true; @@ -1012,7 +1012,7 @@ void ezClock::menu() { } } -uint16_t ezClock::loop() { +uint16_t ezClock::loop(void *private_data) { ezt::events(); if (_starting && timeStatus() != timeNotSet) { _starting = false; @@ -1566,7 +1566,7 @@ void ezWifi::_askAdd() { } } -uint16_t ezWifi::loop() { +uint16_t ezWifi::loop(void *private_data) { if (millis() > _widget_time + ez.theme->signal_interval) { ez.header.draw("wifi"); _widget_time = millis(); @@ -2113,7 +2113,7 @@ void ezBattery::menu() { } } -uint16_t ezBattery::loop() { +uint16_t ezBattery::loop(void *private_data) { if (!_on) return 0; ez.header.draw("battery"); @@ -2236,7 +2236,7 @@ void M5ez::yield() { M5.update(); for (uint8_t n = 0; n < _events.size(); n++) { if (millis() > _events[n].when) { - uint16_t r = (_events[n].function)(); + uint16_t r = (_events[n].function)(_events[n].private_data); if (r) { _events[n].when = millis() + r - 1; } else { @@ -2250,14 +2250,14 @@ void M5ez::yield() { #endif } -void M5ez::addEvent(uint16_t (*function)(), uint32_t when /* = 1 */) { +void M5ez::addEvent(uint16_t (*function)(void *private_data), void *private_data, uint32_t when /* = 1 */) { event_t n; n.function = function; n.when = millis() + when - 1; _events.push_back(n); } -void M5ez::removeEvent(uint16_t (*function)()) { +void M5ez::removeEvent(uint16_t (*function)(void *private_data)) { uint8_t n = 0; while (n < _events.size()) { if (_events[n].function == function) { diff --git a/lib/M5ez/src/M5ez.h b/lib/M5ez/src/M5ez.h index 118f7f2..f4f5373 100644 --- a/lib/M5ez/src/M5ez.h +++ b/lib/M5ez/src/M5ez.h @@ -293,7 +293,7 @@ class ezCanvas: public Print { uint8_t c); // These three are used to inherint print and println from Print class virtual size_t write(const char *str); virtual size_t write(const uint8_t *buffer, size_t size); - static uint16_t loop(); + static uint16_t loop(void *private_data); private: static std::vector _printed; @@ -502,7 +502,7 @@ class ezBacklight { static void menu(); static void inactivity(uint8_t half_minutes); static void activity(); - static uint16_t loop(); + static uint16_t loop(void *private_data); private: static uint8_t _brightness; @@ -527,7 +527,7 @@ class ezClock { static void begin(); static void restart(); static void menu(); - static uint16_t loop(); + static uint16_t loop(void *private_data); static void clear(); static void draw(uint16_t x, uint16_t w); static bool waitForSync(const uint16_t timeout = 0); @@ -600,7 +600,7 @@ class ezWifi { static void readFlash(); static void writeFlash(); static void menu(); - static uint16_t loop(); + static uint16_t loop(void *private_data); static bool update(String url, const char *root_cert, ezProgressBar *pb = NULL); static String updateError(); @@ -679,7 +679,7 @@ class ezBattery { static void readFlash(); static void writeFlash(); static void menu(); - static uint16_t loop(); + static uint16_t loop(void *private_data); static uint8_t getTransformedBatteryLevel(); static uint16_t getBatteryBarColor(uint8_t batteryLevel); @@ -699,7 +699,8 @@ class ezBattery { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct event_t { - uint16_t (*function)(); + uint16_t (*function)(void *); + void *private_data; uint32_t when; }; class M5ez { @@ -743,8 +744,8 @@ class M5ez { static void yield(); - static void addEvent(uint16_t (*function)(), uint32_t when = 1); - static void removeEvent(uint16_t (*function)()); + static void addEvent(uint16_t (*function)(void *), void *private_data = nullptr, uint32_t when = 1); + static void removeEvent(uint16_t (*function)(void *private_data)); static void redraw(); // ez.msgBox diff --git a/lib/furble/Device.h b/lib/furble/Device.h index c298cde..91b12bc 100644 --- a/lib/furble/Device.h +++ b/lib/furble/Device.h @@ -53,11 +53,6 @@ class Device { */ virtual bool connect(NimBLEClient *pClient, ezProgressBar &progress_bar) = 0; - /** - * Checks if the client is still connected. - */ - virtual bool isConnected(void); - /** * Disconnect from the target. */ @@ -88,6 +83,11 @@ class Device { */ virtual void updateGeoData(gps_t &gps, timesync_t ×ync) = 0; + /** + * Checks if the client is still connected. + */ + bool isConnected(void); + const char *getName(void); void save(void); void remove(void); diff --git a/src/furble.ino b/src/furble.ino index f283401..040b1db 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -11,8 +11,6 @@ static NimBLEScan *pScan = nullptr; static std::vector connect_list; -static Furble::Device *device; - bool load_gps_enable(); static TinyGPSPlus gps; @@ -42,7 +40,7 @@ class AdvertisedCallback: public NimBLEAdvertisedDeviceCallbacks { /** * GPS serial event service handler. */ -static uint16_t service_grove_gps(void) { +static uint16_t service_grove_gps(void *private_data) { if (!gps_enable) { return GPS_SERVICE_MS; } @@ -65,7 +63,7 @@ static uint16_t service_grove_gps(void) { /** * Update geotag data. */ -static void update_geodata() { +static void update_geodata(Furble::Device *device) { if (!gps_enable) { return; } @@ -116,7 +114,7 @@ static void current_draw_widget(uint16_t x, uint16_t y) { M5.Lcd.drawString(s, x + ez.theme->header_hmargin, ez.theme->header_tmargin + 2); } -static uint16_t current_service(void) { +static uint16_t current_service(void *private_data) { ez.header.draw("current"); return 1000; } @@ -133,7 +131,7 @@ static void about(void) { ez.msgBox(FURBLE_STR " - About", "Version: " + version, "Back", true); } -static void trigger(int counter) { +static void trigger(Furble::Device *device, int counter) { device->focusPress(); delay(250); device->shutterPress(); @@ -143,14 +141,14 @@ static void trigger(int counter) { device->shutterRelease(); } -static void remote_interval(void) { +static void remote_interval(Furble::Device *device) { int i = 0; int j = 1; ez.msgBox("Interval Release", "", "Stop", false); - trigger(j); + trigger(device, j); - while (true) { + while (device->isConnected()) { i++; @@ -169,7 +167,7 @@ static void remote_interval(void) { i = 0; j++; - trigger(j); + trigger(device, j); } delay(50); @@ -210,7 +208,7 @@ static void show_shutter_control(bool shutter_locked, unsigned long lock_start_m } } -static void remote_control(void) { +static void remote_control(Furble::Device *device) { static unsigned long shutter_lock_start_ms = 0; static bool shutter_lock = false; @@ -221,7 +219,7 @@ static void remote_control(void) { do { M5.update(); - update_geodata(); + update_geodata(device); if (reconnected){ show_shutter_control(shutter_lock, shutter_lock_start_ms); @@ -283,7 +281,7 @@ static void remote_control(void) { ez.yield(); delay(50); - } while (true); + } while (device->isConnected()); } /** @@ -306,7 +304,9 @@ static void do_saved(void) { menu_connect(false); } -uint16_t disconnectDetect(void) { +uint16_t disconnectDetect(void *private_data) { + Furble::Device *device = (Furble::Device *)private_data; + if (!device) return 0; if (device->isConnected()) return 500; @@ -335,7 +335,7 @@ uint16_t disconnectDetect(void) { return 0; } -static void menu_remote(void) { +static void menu_remote(Furble::Device *device) { ez.backlight.inactivity(NEVER); ezMenu submenu(FURBLE_STR " - Connected"); submenu.buttons("OK#down"); @@ -344,17 +344,17 @@ static void menu_remote(void) { submenu.addItem("Disconnect"); submenu.downOnLast("first"); - ez.addEvent(disconnectDetect, 500); + ez.addEvent(disconnectDetect, device, 500); do { submenu.runOnce(); if (submenu.pickName() == "Shutter") { - remote_control(); + remote_control(device); } if (submenu.pickName() == "Interval") { - remote_interval(); + remote_interval(device); } } while (submenu.pickName() != "Disconnect"); @@ -377,9 +377,9 @@ static void menu_connect(bool save) { if (i == 0) return; - device = connect_list[i - 1]; + Furble::Device *device = connect_list[i - 1]; - update_geodata(); + update_geodata(device); NimBLEClient *pClient = NimBLEDevice::createClient(); ezProgressBar progress_bar(FURBLE_STR, "Connecting ...", ""); @@ -387,7 +387,7 @@ static void menu_connect(bool save) { if (save) { device->save(); } - menu_remote(); + menu_remote(device); } } @@ -442,8 +442,8 @@ void setup() { uint8_t width = 4 * M5.Lcd.textWidth("5") + ez.theme->header_hmargin * 2; ez.header.insert(CURRENT_POSITION, "current", width, current_draw_widget); ez.header.insert(GPS_HEADER_POSITION, "gps", ez.theme->header_height * 0.8, gps_draw_widget); - ez.addEvent(service_grove_gps, millis() + 500); - ez.addEvent(current_service, millis() + 500); + ez.addEvent(service_grove_gps, nullptr, millis() + 500); + ez.addEvent(current_service, nullptr, millis() + 500); NimBLEDevice::init(FURBLE_STR); NimBLEDevice::setSecurityAuth(true, true, true); From fba97d61e472471d1689543b7da1b6e7a41f48e7 Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:01:39 +1030 Subject: [PATCH 04/10] Run clang-format. --- lib/M5ez/src/M5ez.cpp | 6 ++++-- lib/M5ez/src/M5ez.h | 4 +++- src/furble.ino | 44 +++++++++++++++++++++---------------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 3c237c6..2428997 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -2250,7 +2250,9 @@ void M5ez::yield() { #endif } -void M5ez::addEvent(uint16_t (*function)(void *private_data), void *private_data, uint32_t when /* = 1 */) { +void M5ez::addEvent(uint16_t (*function)(void *private_data), + void *private_data, + uint32_t when /* = 1 */) { event_t n; n.function = function; n.when = millis() + when - 1; @@ -3472,7 +3474,7 @@ ezProgressBar::ezProgressBar(String header /* = "" */, _bar_color = bar_color; ez.screen.clear(); M5.Lcd.fillRect(0, 0, TFT_W, TFT_H, ez.screen.background()); - + if (header != "") ez.header.show(header); ez.buttons.show(buttons); diff --git a/lib/M5ez/src/M5ez.h b/lib/M5ez/src/M5ez.h index f4f5373..f52ed94 100644 --- a/lib/M5ez/src/M5ez.h +++ b/lib/M5ez/src/M5ez.h @@ -744,7 +744,9 @@ class M5ez { static void yield(); - static void addEvent(uint16_t (*function)(void *), void *private_data = nullptr, uint32_t when = 1); + static void addEvent(uint16_t (*function)(void *), + void *private_data = nullptr, + uint32_t when = 1); static void removeEvent(uint16_t (*function)(void *private_data)); static void redraw(); diff --git a/src/furble.ino b/src/furble.ino index 040b1db..24921bc 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -150,13 +150,12 @@ static void remote_interval(Furble::Device *device) { while (device->isConnected()) { i++; - - - if (reconnected){ + + if (reconnected) { ez.msgBox("Interval Release", String(j), "Stop", false); reconnected = false; } - + M5.update(); if (M5.BtnB.wasReleased()) { @@ -220,12 +219,12 @@ static void remote_control(Furble::Device *device) { M5.update(); update_geodata(device); - - if (reconnected){ + + if (reconnected) { show_shutter_control(shutter_lock, shutter_lock_start_ms); reconnected = false; } - + if (M5.BtnPWR.wasClicked() || M5.BtnC.wasPressed()) { if (shutter_lock) { // ensure shutter is released on exit @@ -307,30 +306,31 @@ static void do_saved(void) { uint16_t disconnectDetect(void *private_data) { Furble::Device *device = (Furble::Device *)private_data; - if (!device) return 0; - - if (device->isConnected()) return 500; - + if (!device) + return 0; + + if (device->isConnected()) + return 500; + String buttons; buttons = ez.buttons.get(); - + String header; header = ez.header.title(); - + NimBLEClient *pClient = NimBLEDevice::createClient(); ezProgressBar progress_bar(FURBLE_STR, "Reconnecting ...", ""); - if (device->connect(pClient, progress_bar)){ + if (device->connect(pClient, progress_bar)) { ez.screen.clear(); ez.header.show(header); ez.buttons.show(buttons); - + reconnected = true; - + ez.redraw(); return 500; } - - + ez.screen.clear(); return 0; } @@ -343,9 +343,9 @@ static void menu_remote(Furble::Device *device) { submenu.addItem("Interval"); submenu.addItem("Disconnect"); submenu.downOnLast("first"); - + ez.addEvent(disconnectDetect, device, 500); - + do { submenu.runOnce(); @@ -357,9 +357,9 @@ static void menu_remote(Furble::Device *device) { remote_interval(device); } } while (submenu.pickName() != "Disconnect"); - + ez.removeEvent(disconnectDetect); - + device->disconnect(); ez.backlight.inactivity(USER_SET); } From 5e7237f070eca44cf678d44052fafd31a2e08b0a Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:43:50 +1030 Subject: [PATCH 05/10] Appease the clang-format bot. --- lib/furble/Device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/furble/Device.cpp b/lib/furble/Device.cpp index 1b49a6b..624ea4a 100644 --- a/lib/furble/Device.cpp +++ b/lib/furble/Device.cpp @@ -180,7 +180,7 @@ void Device::getUUID128(uuid128_t *uuid) { } } -bool Device::isConnected(void){ +bool Device::isConnected(void) { return m_Client->isConnected(); } From 793d4872e148bbc127164ef6a5c02f07a78d623c Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Fri, 8 Mar 2024 10:00:49 +1030 Subject: [PATCH 06/10] Further refactor to remove global reconnected boolean. --- src/furble.ino | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/furble.ino b/src/furble.ino index 24921bc..12bc13b 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -25,7 +25,10 @@ static const uint8_t GPS_HEADER_POSITION = CURRENT_POSITION + 1; static bool gps_enable = false; static bool gps_has_fix = false; -static bool reconnected = false; +struct FurbleCtx { + Furble::Device *device; + bool reconnected; +}; /** * BLE Advertisement callback. @@ -141,7 +144,8 @@ static void trigger(Furble::Device *device, int counter) { device->shutterRelease(); } -static void remote_interval(Furble::Device *device) { +static void remote_interval(FurbleCtx *fctx) { + Furble::Device *device = fctx->device; int i = 0; int j = 1; @@ -151,9 +155,9 @@ static void remote_interval(Furble::Device *device) { while (device->isConnected()) { i++; - if (reconnected) { + if (fctx->reconnected) { ez.msgBox("Interval Release", String(j), "Stop", false); - reconnected = false; + fctx->reconnected = false; } M5.update(); @@ -207,7 +211,8 @@ static void show_shutter_control(bool shutter_locked, unsigned long lock_start_m } } -static void remote_control(Furble::Device *device) { +static void remote_control(FurbleCtx *fctx) { + Furble::Device *device = fctx->device; static unsigned long shutter_lock_start_ms = 0; static bool shutter_lock = false; @@ -220,9 +225,9 @@ static void remote_control(Furble::Device *device) { update_geodata(device); - if (reconnected) { + if (fctx->reconnected) { show_shutter_control(shutter_lock, shutter_lock_start_ms); - reconnected = false; + fctx->reconnected = false; } if (M5.BtnPWR.wasClicked() || M5.BtnC.wasPressed()) { @@ -304,10 +309,8 @@ static void do_saved(void) { } uint16_t disconnectDetect(void *private_data) { - Furble::Device *device = (Furble::Device *)private_data; - - if (!device) - return 0; + FurbleCtx *fctx = (FurbleCtx *)private_data; + Furble::Device *device = fctx->device; if (device->isConnected()) return 500; @@ -325,7 +328,7 @@ uint16_t disconnectDetect(void *private_data) { ez.header.show(header); ez.buttons.show(buttons); - reconnected = true; + fctx->reconnected = true; ez.redraw(); return 500; @@ -335,7 +338,7 @@ uint16_t disconnectDetect(void *private_data) { return 0; } -static void menu_remote(Furble::Device *device) { +static void menu_remote(FurbleCtx *fctx) { ez.backlight.inactivity(NEVER); ezMenu submenu(FURBLE_STR " - Connected"); submenu.buttons("OK#down"); @@ -344,23 +347,23 @@ static void menu_remote(Furble::Device *device) { submenu.addItem("Disconnect"); submenu.downOnLast("first"); - ez.addEvent(disconnectDetect, device, 500); + ez.addEvent(disconnectDetect, fctx, 500); do { submenu.runOnce(); if (submenu.pickName() == "Shutter") { - remote_control(device); + remote_control(fctx); } if (submenu.pickName() == "Interval") { - remote_interval(device); + remote_interval(fctx); } } while (submenu.pickName() != "Disconnect"); ez.removeEvent(disconnectDetect); - device->disconnect(); + fctx->device->disconnect(); ez.backlight.inactivity(USER_SET); } @@ -377,17 +380,17 @@ static void menu_connect(bool save) { if (i == 0) return; - Furble::Device *device = connect_list[i - 1]; + FurbleCtx fctx = {connect_list[i - 1], false}; - update_geodata(device); + update_geodata(fctx.device); NimBLEClient *pClient = NimBLEDevice::createClient(); ezProgressBar progress_bar(FURBLE_STR, "Connecting ...", ""); - if (device->connect(pClient, progress_bar)) { + if (fctx.device->connect(pClient, progress_bar)) { if (save) { - device->save(); + fctx.device->save(); } - menu_remote(device); + menu_remote(&fctx); } } From 23a56317c09b98e247d1d76fc190c77f0b79e158 Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Fri, 8 Mar 2024 10:24:48 +1030 Subject: [PATCH 07/10] Actually record the private data. --- lib/M5ez/src/M5ez.cpp | 1 + src/furble.ino | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 2428997..c0252d5 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -2255,6 +2255,7 @@ void M5ez::addEvent(uint16_t (*function)(void *private_data), uint32_t when /* = 1 */) { event_t n; n.function = function; + n.private_data = private_data; n.when = millis() + when - 1; _events.push_back(n); } diff --git a/src/furble.ino b/src/furble.ino index 12bc13b..5ac0f8e 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -315,11 +315,8 @@ uint16_t disconnectDetect(void *private_data) { if (device->isConnected()) return 500; - String buttons; - buttons = ez.buttons.get(); - - String header; - header = ez.header.title(); + String buttons = ez.buttons.get(); + String header = ez.header.title(); NimBLEClient *pClient = NimBLEDevice::createClient(); ezProgressBar progress_bar(FURBLE_STR, "Reconnecting ...", ""); From bdb3767fd9731d911c9a580e160cb3aea35d1d39 Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:10:18 +1030 Subject: [PATCH 08/10] Restart on failed reconnect. --- src/furble.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/furble.ino b/src/furble.ino index 5ac0f8e..38f84b7 100644 --- a/src/furble.ino +++ b/src/furble.ino @@ -332,6 +332,8 @@ uint16_t disconnectDetect(void *private_data) { } ez.screen.clear(); + // no recovery, restart device + esp_restart(); return 0; } From 1428ac2f69faa01a48c3616d44c4742a7da92191 Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:19:07 +1030 Subject: [PATCH 09/10] Add incremental progress during notification wait. --- lib/furble/Fujifilm.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/furble/Fujifilm.cpp b/lib/furble/Fujifilm.cpp index 7ad12f0..0aaa1c4 100644 --- a/lib/furble/Fujifilm.cpp +++ b/lib/furble/Fujifilm.cpp @@ -200,6 +200,7 @@ bool Fujifilm::connect(NimBLEClient *pClient, ezProgressBar &progress_bar) { if (m_Configured) { break; } + progress_bar.value(50.0f + (((float)i/5000.0f) * 10.0f)); delay(100); } @@ -224,6 +225,7 @@ bool Fujifilm::connect(NimBLEClient *pClient, ezProgressBar &progress_bar) { m_GeoRequested = false; break; } + progress_bar.value(90.0f + (((float)i/5000.0f) * 10.0f)); delay(100); } From bb8006298874a8d28a120d11e4a14b7e765cb0a1 Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:20:47 +1030 Subject: [PATCH 10/10] Appease the clang-format bot. --- lib/furble/Fujifilm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/furble/Fujifilm.cpp b/lib/furble/Fujifilm.cpp index 0aaa1c4..0d2e25a 100644 --- a/lib/furble/Fujifilm.cpp +++ b/lib/furble/Fujifilm.cpp @@ -200,7 +200,7 @@ bool Fujifilm::connect(NimBLEClient *pClient, ezProgressBar &progress_bar) { if (m_Configured) { break; } - progress_bar.value(50.0f + (((float)i/5000.0f) * 10.0f)); + progress_bar.value(50.0f + (((float)i / 5000.0f) * 10.0f)); delay(100); } @@ -225,7 +225,7 @@ bool Fujifilm::connect(NimBLEClient *pClient, ezProgressBar &progress_bar) { m_GeoRequested = false; break; } - progress_bar.value(90.0f + (((float)i/5000.0f) * 10.0f)); + progress_bar.value(90.0f + (((float)i / 5000.0f) * 10.0f)); delay(100); }