♻️ refactor: unify UI notification methods into single notify() function

Consolidates soundBuzzer() and triggerVibration() into a unified notify() method
that handles both audio and haptic feedback based on UIEventType.
This commit is contained in:
csrutil 2025-09-17 08:53:50 +08:00
parent 2da50882c0
commit 043f37a08e
6 changed files with 26 additions and 40 deletions

View file

@ -41,9 +41,6 @@ public:
void disableSerial() { _serial->disable(); } void disableSerial() { _serial->disable(); }
virtual void msgRead(int msgcount) = 0; virtual void msgRead(int msgcount) = 0;
virtual void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) = 0; virtual void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) = 0;
virtual void soundBuzzer(UIEventType bet = UIEventType::none) = 0; virtual void notify(UIEventType t = UIEventType::none) = 0;
#ifdef PIN_VIBRATION
virtual void triggerVibration() = 0;
#endif
virtual void loop() = 0; virtual void loop() = 0;
}; };

View file

@ -243,12 +243,7 @@ void MyMesh::onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path
} }
} else { } else {
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
if (_ui) _ui->soundBuzzer(UIEventType::newContactMessage); if (_ui) _ui->notify(UIEventType::newContactMessage);
if (_ui) {
#ifdef PIN_VIBRATION
if (is_new) _ui->triggerVibration();
#endif
}
#endif #endif
} }
@ -358,7 +353,7 @@ void MyMesh::queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packe
if (should_display && _ui) { if (should_display && _ui) {
_ui->newMsg(path_len, from.name, text, offline_queue_len); _ui->newMsg(path_len, from.name, text, offline_queue_len);
if (!_serial->isConnected()) { if (!_serial->isConnected()) {
_ui->soundBuzzer(UIEventType::contactMessage); _ui->notify(UIEventType::contactMessage);
} }
} }
#endif #endif
@ -417,7 +412,7 @@ void MyMesh::onChannelMessageRecv(const mesh::GroupChannel &channel, mesh::Packe
_serial->writeFrame(frame, 1); _serial->writeFrame(frame, 1);
} else { } else {
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
if (_ui) _ui->soundBuzzer(UIEventType::channelMessage); if (_ui) _ui->notify(UIEventType::channelMessage);
#endif #endif
} }
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS

View file

@ -348,9 +348,7 @@ public:
return true; return true;
} }
if (c == KEY_ENTER && _page == HomePage::ADVERT) { if (c == KEY_ENTER && _page == HomePage::ADVERT) {
#ifdef PIN_BUZZER _task->notify(UIEventType::ack);
_task->soundBuzzer(UIEventType::ack);
#endif
if (the_mesh.advert()) { if (the_mesh.advert()) {
_task->showAlert("Advert sent!", 1000); _task->showAlert("Advert sent!", 1000);
} else { } else {
@ -501,9 +499,9 @@ void UITask::showAlert(const char* text, int duration_millis) {
_alert_expiry = millis() + duration_millis; _alert_expiry = millis() + duration_millis;
} }
void UITask::soundBuzzer(UIEventType bet) { void UITask::notify(UIEventType t) {
#if defined(PIN_BUZZER) #if defined(PIN_BUZZER)
switch(bet){ switch(t){
case UIEventType::contactMessage: case UIEventType::contactMessage:
// gemini's pick // gemini's pick
buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7"); buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
@ -521,13 +519,15 @@ switch(bet){
break; break;
} }
#endif #endif
}
#ifdef PIN_VIBRATION #ifdef PIN_VIBRATION
void UITask::triggerVibration() { // Trigger vibration for all UI events except none
vibration.trigger(); if (t != UIEventType::none) {
} vibration.trigger();
}
#endif #endif
}
void UITask::msgRead(int msgcount) { void UITask::msgRead(int msgcount) {
_msgcount = msgcount; _msgcount = msgcount;
@ -542,9 +542,6 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i
((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from_name, text); ((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from_name, text);
setCurrScreen(msg_preview); setCurrScreen(msg_preview);
#ifdef PIN_VIBRATION
triggerVibration();
#endif
if (_display != NULL) { if (_display != NULL) {
if (!_display->isOn()) _display->turnOn(); if (!_display->isOn()) _display->turnOn();
@ -773,11 +770,11 @@ void UITask::toggleGPS() {
if (strcmp(_sensors->getSettingName(i), "gps") == 0) { if (strcmp(_sensors->getSettingName(i), "gps") == 0) {
if (strcmp(_sensors->getSettingValue(i), "1") == 0) { if (strcmp(_sensors->getSettingValue(i), "1") == 0) {
_sensors->setSettingValue("gps", "0"); _sensors->setSettingValue("gps", "0");
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
showAlert("GPS: Disabled", 800); showAlert("GPS: Disabled", 800);
} else { } else {
_sensors->setSettingValue("gps", "1"); _sensors->setSettingValue("gps", "1");
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
showAlert("GPS: Enabled", 800); showAlert("GPS: Enabled", 800);
} }
_next_refresh = 0; _next_refresh = 0;
@ -792,7 +789,7 @@ void UITask::toggleBuzzer() {
#ifdef PIN_BUZZER #ifdef PIN_BUZZER
if (buzzer.isQuiet()) { if (buzzer.isQuiet()) {
buzzer.quiet(false); buzzer.quiet(false);
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
showAlert("Buzzer: ON", 800); showAlert("Buzzer: ON", 800);
} else { } else {
buzzer.quiet(true); buzzer.quiet(true);

View file

@ -77,10 +77,7 @@ public:
// from AbstractUITask // from AbstractUITask
void msgRead(int msgcount) override; void msgRead(int msgcount) override;
void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) override; void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) override;
void soundBuzzer(UIEventType bet = UIEventType::none) override; void notify(UIEventType t = UIEventType::none) override;
#ifdef PIN_VIBRATION
void triggerVibration() override;
#endif
void loop() override; void loop() override;
void shutdown(bool restart = false); void shutdown(bool restart = false);

View file

@ -88,9 +88,9 @@ void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* no
ui_started_at = millis(); ui_started_at = millis();
} }
void UITask::soundBuzzer(UIEventType bet) { void UITask::notify(UIEventType t) {
#if defined(PIN_BUZZER) #if defined(PIN_BUZZER)
switch(bet){ switch(t){
case UIEventType::contactMessage: case UIEventType::contactMessage:
// gemini's pick // gemini's pick
buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7"); buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
@ -108,8 +108,8 @@ switch(bet){
break; break;
} }
#endif #endif
// Serial.print("DBG: Buzzzzzz -> "); // Serial.print("DBG: Alert user -> ");
// Serial.println((int) bet); // Serial.println((int) t);
} }
void UITask::msgRead(int msgcount) { void UITask::msgRead(int msgcount) {
@ -370,7 +370,7 @@ void UITask::handleButtonDoublePress() {
MESH_DEBUG_PRINTLN("UITask: double press triggered, sending advert"); MESH_DEBUG_PRINTLN("UITask: double press triggered, sending advert");
// ADVERT // ADVERT
#ifdef PIN_BUZZER #ifdef PIN_BUZZER
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
#endif #endif
if (the_mesh.advert()) { if (the_mesh.advert()) {
MESH_DEBUG_PRINTLN("Advert sent!"); MESH_DEBUG_PRINTLN("Advert sent!");
@ -388,7 +388,7 @@ void UITask::handleButtonTriplePress() {
#ifdef PIN_BUZZER #ifdef PIN_BUZZER
if (buzzer.isQuiet()) { if (buzzer.isQuiet()) {
buzzer.quiet(false); buzzer.quiet(false);
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
sprintf(_alert, "Buzzer: ON"); sprintf(_alert, "Buzzer: ON");
} else { } else {
buzzer.quiet(true); buzzer.quiet(true);
@ -407,11 +407,11 @@ void UITask::handleButtonQuadruplePress() {
if (strcmp(_sensors->getSettingName(i), "gps") == 0) { if (strcmp(_sensors->getSettingName(i), "gps") == 0) {
if (strcmp(_sensors->getSettingValue(i), "1") == 0) { if (strcmp(_sensors->getSettingValue(i), "1") == 0) {
_sensors->setSettingValue("gps", "0"); _sensors->setSettingValue("gps", "0");
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
sprintf(_alert, "GPS: Disabled"); sprintf(_alert, "GPS: Disabled");
} else { } else {
_sensors->setSettingValue("gps", "1"); _sensors->setSettingValue("gps", "1");
soundBuzzer(UIEventType::ack); notify(UIEventType::ack);
sprintf(_alert, "GPS: Enabled"); sprintf(_alert, "GPS: Enabled");
} }
break; break;

View file

@ -66,7 +66,7 @@ public:
// from AbstractUITask // from AbstractUITask
void msgRead(int msgcount) override; void msgRead(int msgcount) override;
void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) override; void newMsg(uint8_t path_len, const char* from_name, const char* text, int msgcount) override;
void soundBuzzer(UIEventType bet = UIEventType::none) override; void notify(UIEventType t = UIEventType::none) override;
void loop() override; void loop() override;
void shutdown(bool restart = false); void shutdown(bool restart = false);