From 89e2cf95cf951928a75c5b4f2990a100370c552c Mon Sep 17 00:00:00 2001 From: Guo-Rong <5484552+gkoh@users.noreply.github.com> Date: Fri, 15 Mar 2024 09:15:02 +1030 Subject: [PATCH] 85 disable connect and delete saved if there are no connections (#87) * Disable 'Connect' and 'Delete Saved' if there are no connections. Also, fix a bug with deleting down to zero connections by removing the preference entry. * Fix README --- README.md | 4 ++-- lib/furble/CameraList.cpp | 14 +++++++++++++- lib/furble/CameraList.h | 5 +++++ src/furble.cpp | 15 ++++++++++++--- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eb8d272..7f8ac87 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,9 @@ More details are on the wiki: [PlatformIO](https://github.com/gkoh/furble/wiki/L ## Usage The top level menu has the following entries: -- `Connect` +- `Connect` (if there are saved connections) - `Scan` -- `Delete Saved` +- `Delete Saved` (if there are saved connections) - `Settings` - `Power Off` diff --git a/lib/furble/CameraList.cpp b/lib/furble/CameraList.cpp index c617c42..398a4ab 100644 --- a/lib/furble/CameraList.cpp +++ b/lib/furble/CameraList.cpp @@ -26,7 +26,11 @@ typedef struct { } index_entry_t; static void save_index(std::vector &index) { - m_Prefs.putBytes(FURBLE_PREF_INDEX, index.data(), sizeof(index[0]) * index.size()); + if (index.size() > 0) { + m_Prefs.putBytes(FURBLE_PREF_INDEX, index.data(), sizeof(index[0]) * index.size()); + } else { + m_Prefs.remove(FURBLE_PREF_INDEX); + } } static std::vector load_index(void) { @@ -149,6 +153,14 @@ void CameraList::load(void) { m_Prefs.end(); } +size_t CameraList::getSaveCount(void) { + m_Prefs.begin(FURBLE_STR, false); + auto index = load_index(); + m_Prefs.end(); + + return index.size(); +} + void CameraList::match(NimBLEAdvertisedDevice *pDevice) { if (Fujifilm::matches(pDevice)) { m_ConnectList.push_back(new Furble::Fujifilm(pDevice)); diff --git a/lib/furble/CameraList.h b/lib/furble/CameraList.h index d760298..a03ba38 100644 --- a/lib/furble/CameraList.h +++ b/lib/furble/CameraList.h @@ -24,6 +24,11 @@ class CameraList { */ static void load(void); + /** + * Get number of saved connections. + */ + static size_t getSaveCount(void); + /** * Add matching devices to the list. */ diff --git a/src/furble.cpp b/src/furble.cpp index d276ef1..d45489b 100644 --- a/src/furble.cpp +++ b/src/furble.cpp @@ -303,13 +303,22 @@ void setup() { } void loop() { + size_t save_count = Furble::CameraList::getSaveCount(); + ezMenu mainmenu(FURBLE_STR); mainmenu.buttons("OK#down"); - mainmenu.addItem("Connect", do_saved); + if (save_count > 0) { + mainmenu.addItem("Connect", do_saved); + } mainmenu.addItem("Scan", do_scan); - mainmenu.addItem("Delete Saved", menu_delete); + if (save_count > 0) { + mainmenu.addItem("Delete Saved", menu_delete); + } mainmenu.addItem("Settings", menu_settings); mainmenu.addItem("Power Off", mainmenu_poweroff); mainmenu.downOnLast("first"); - mainmenu.run(); + + do { + mainmenu.runOnce(); + } while (Furble::CameraList::getSaveCount() == save_count); }