This commit is contained in:
todd-herbert 2025-04-21 02:16:16 +00:00 committed by GitHub
commit c6dc44af7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 67 additions and 6 deletions

View File

@ -2840,9 +2840,6 @@ int Screen::handleInputEvent(const InputEvent *event)
int Screen::handleAdminMessage(const meshtastic_AdminMessage *arg)
{
// Note: only selected admin messages notify this observer
// If you wish to handle a new type of message, you should modify AdminModule.cpp first
switch (arg->which_payload_variant) {
// Node removed manually (i.e. via app)
case meshtastic_AdminMessage_remove_by_nodenum_tag:

View File

@ -135,6 +135,29 @@ template <typename T> class FlashData
}
};
// Erase contents of the NicheGraphics data directory
inline void clearFlashData()
{
#ifdef FSCom
File dir = FSCom.open("/NicheGraphics"); // Open the directory
File file = dir.openNextFile(); // Attempt to open the first file in the directory
// While the directory still contains files
while (file) {
std::string path = "/NicheGraphics/";
path += file.name();
LOG_DEBUG("Erasing %s", path.c_str());
file.close();
FSCom.remove(path.c_str());
file = dir.openNextFile();
}
#else
LOG_ERROR("ERROR: Filesystem not implemented\n");
#endif
}
} // namespace NicheGraphics
#endif

View File

@ -3,11 +3,13 @@
#include "./Events.h"
#include "RTC.h"
#include "modules/AdminModule.h"
#include "modules/TextMessageModule.h"
#include "sleep.h"
#include "./Applet.h"
#include "./SystemApplet.h"
#include "graphics/niche/FlashData.h"
using namespace NicheGraphics;
@ -25,6 +27,9 @@ void InkHUD::Events::begin()
deepSleepObserver.observe(&notifyDeepSleep);
rebootObserver.observe(&notifyReboot);
textMessageObserver.observe(textMessageModule);
#if !MESHTASTIC_EXCLUDE_ADMIN
adminMessageObserver.observe(adminModule);
#endif
#ifdef ARCH_ESP32
lightSleepObserver.observe(&notifyLightSleep);
#endif
@ -117,8 +122,13 @@ int InkHUD::Events::beforeReboot(void *unused)
sa->onReboot();
}
// Save settings to flash, or erase if factory reset in progress
if (!eraseOnReboot) {
inkhud->persistence->saveSettings();
inkhud->persistence->saveLatestMessage();
} else {
NicheGraphics::clearFlashData();
}
// Note: no forceUpdate call here
// We don't have any final screen to draw, although LogoApplet::onReboot did already display a "rebooting" screen
@ -171,6 +181,23 @@ int InkHUD::Events::onReceiveTextMessage(const meshtastic_MeshPacket *packet)
return 0; // Tell caller to continue notifying other observers. (No reason to abort this event)
}
int InkHUD::Events::onAdminMessage(const meshtastic_AdminMessage *message)
{
switch (message->which_payload_variant) {
// Factory reset
// Two possible messages. One preserves BLE bonds, other wipes. Both should clear InkHUD data.
case meshtastic_AdminMessage_factory_reset_device_tag:
case meshtastic_AdminMessage_factory_reset_config_tag:
eraseOnReboot = true;
break;
default:
break;
}
return 0; // Tell caller to continue notifying other observers. (No reason to abort this event)
}
#ifdef ARCH_ESP32
// Callback for lightSleepObserver
// Make sure the display is not partway through an update when we begin light sleep

View File

@ -33,6 +33,7 @@ class Events
int beforeDeepSleep(void *unused); // Prepare for shutdown
int beforeReboot(void *unused); // Prepare for reboot
int onReceiveTextMessage(const meshtastic_MeshPacket *packet); // Store most recent text message
int onAdminMessage(const meshtastic_AdminMessage *message); // Handle incoming admin messages
#ifdef ARCH_ESP32
int beforeLightSleep(void *unused); // Prepare for light sleep
#endif
@ -52,10 +53,17 @@ class Events
CallbackObserver<Events, const meshtastic_MeshPacket *> textMessageObserver =
CallbackObserver<Events, const meshtastic_MeshPacket *>(this, &Events::onReceiveTextMessage);
// Get notified of incoming admin messages, and handle any which are relevant to InkHUD
CallbackObserver<Events, const meshtastic_AdminMessage *> adminMessageObserver =
CallbackObserver<Events, const meshtastic_AdminMessage *>(this, &Events::onAdminMessage);
#ifdef ARCH_ESP32
// Get notified when the system is entering light sleep
CallbackObserver<Events, void *> lightSleepObserver = CallbackObserver<Events, void *>(this, &Events::beforeLightSleep);
#endif
// If set, InkHUD's data will be erased during onReboot
bool eraseOnReboot = false;
};
} // namespace NicheGraphics::InkHUD

View File

@ -502,6 +502,10 @@ Applets themselves do also listen separately for various events, but for the pur
Button input is sometimes handled by a system applet. `InkHUD::Events` determines whether the button should be handled by a specific system applet, or should instead trigger a default behavior
#### Factory Reset
The Events class handles the admin messages(s) which trigger factory reset. We set `Events::eraseOnReboot = true`, which causes `Events::onReboot` to erase the contents of InkHUD's data directory. We do this because some applets (e.g. ThreadedMessageApplet) save their own data to flash, so if we erased earlier, that data would get re-written during reboot.
---
### `InkHUD::Applet`

View File

@ -284,7 +284,6 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
case meshtastic_AdminMessage_remove_by_nodenum_tag: {
LOG_INFO("Client received remove_nodenum command");
nodeDB->removeNodeByNum(r->remove_by_nodenum);
this->notifyObservers(r); // Observed by screen
break;
}
case meshtastic_AdminMessage_set_favorite_node_tag: {
@ -444,6 +443,9 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
myReply = allocErrorResponse(meshtastic_Routing_Error_NONE, &mp);
}
// Allow any observers (e.g. the UI) to respond to this event
notifyObservers(r);
return handled;
}