809 lines
26 KiB
C++
809 lines
26 KiB
C++
#include "UITask.h"
|
|
#if UI_SMALL_MESSAGE_FONT == 1
|
|
#include <helpers/ui/SmallMessageText.h>
|
|
#endif
|
|
#include <Arduino.h>
|
|
#include <helpers/TxtDataHelpers.h>
|
|
#include <helpers/ui/BluetoothPairingUiPolicy.h>
|
|
#include <helpers/ui/RadioProfileDisplayPage.h>
|
|
#include <helpers/ui/RadioProfileSystemStatus.h>
|
|
#include <RadioProfiles.h>
|
|
#include "../MyMesh.h"
|
|
|
|
#define AUTO_OFF_MILLIS 15000 // 15 seconds
|
|
#define BOOT_SCREEN_MILLIS 3000 // 3 seconds
|
|
#ifndef BLE_PAIRING_DISPLAY_MILLIS
|
|
#define BLE_PAIRING_DISPLAY_MILLIS 120000UL
|
|
#endif
|
|
|
|
#if defined(PIN_STATUS_LED) || defined(PIN_STATUS_LED_R)
|
|
#define LED_ON_MILLIS 20
|
|
#define LED_ON_MSG_MILLIS 200
|
|
#define LED_CYCLE_MILLIS 4000
|
|
#endif
|
|
|
|
#if defined(PIN_STATUS_LED_R) && defined(PIN_STATUS_LED_G) && defined(PIN_STATUS_LED_B)
|
|
#define STATUS_LED_RGB 1
|
|
#ifndef LOW_BATT_MILLIVOLTS
|
|
#define LOW_BATT_MILLIVOLTS 3500
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef USER_BTN_PRESSED
|
|
#define USER_BTN_PRESSED LOW
|
|
#endif
|
|
|
|
namespace {
|
|
|
|
mesh::ui::RadioProfileSystemStatus radioProfileSystemStatus(
|
|
const CompanionNodePrefs& prefs, const mesh::MainBoard& board) {
|
|
mesh::ui::RadioProfileSystemStatus status;
|
|
const auto* radio = the_mesh.getProfileRadio();
|
|
const mesh::RadioProfiles* profiles = radio ? radio->profiles() : NULL;
|
|
status.public_key = the_mesh.self_id.pub_key;
|
|
status.powersaving_enabled = prefs.powersaving_enabled != 0;
|
|
#if ENV_INCLUDE_GPS == 1
|
|
status.gps_enabled = prefs.gps_enabled != 0;
|
|
#endif
|
|
status.fem_enabled = board.canControlLoRaFemLna()
|
|
&& board.isLoRaFemLnaEnabled();
|
|
status.rx_boosted_gain = prefs.rx_boosted_gain != 0;
|
|
status.rx_powersaving_enabled = prefs.rx_powersaving_enabled != 0;
|
|
status.cad_enabled = prefs.cad_enabled != 0;
|
|
status.dual_radio_enabled = profiles != NULL && profiles->enabled();
|
|
if (status.dual_radio_enabled) {
|
|
status.secondary_temporary = profiles->secondary_temporary;
|
|
status.secondary_mode = profiles->secondary.mode;
|
|
status.cross = profiles->cross;
|
|
}
|
|
status.noise_floor_1 = radio_driver.getNoiseFloorDbm(0);
|
|
status.noise_floor_1_seconds =
|
|
radio_driver.getNoiseFloorCalibrationSecondsRemaining(0);
|
|
if (status.dual_radio_enabled) {
|
|
status.noise_floor_2 = radio_driver.getNoiseFloorDbm(1);
|
|
status.noise_floor_2_seconds =
|
|
radio_driver.getNoiseFloorCalibrationSecondsRemaining(1);
|
|
}
|
|
return status;
|
|
}
|
|
|
|
uint8_t radioProfileStatusPageCount(DisplayDriver& display,
|
|
bool dual_radio_enabled) {
|
|
display.setTextSize(1);
|
|
return mesh::ui::radioProfileSystemStatusPageCount(display,
|
|
dual_radio_enabled);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// 'meshcore', 128x13px
|
|
static const uint8_t meshcore_logo [] PROGMEM = {
|
|
0x3c, 0x01, 0xe3, 0xff, 0xc7, 0xff, 0x8f, 0x03, 0x87, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe,
|
|
0x3c, 0x03, 0xe3, 0xff, 0xc7, 0xff, 0x8e, 0x03, 0x8f, 0xfe, 0x3f, 0xfe, 0x1f, 0xff, 0x1f, 0xfe,
|
|
0x3e, 0x03, 0xc3, 0xff, 0x8f, 0xff, 0x0e, 0x07, 0x8f, 0xfe, 0x7f, 0xfe, 0x1f, 0xff, 0x1f, 0xfc,
|
|
0x3e, 0x07, 0xc7, 0x80, 0x0e, 0x00, 0x0e, 0x07, 0x9e, 0x00, 0x78, 0x0e, 0x3c, 0x0f, 0x1c, 0x00,
|
|
0x3e, 0x0f, 0xc7, 0x80, 0x1e, 0x00, 0x0e, 0x07, 0x1e, 0x00, 0x70, 0x0e, 0x38, 0x0f, 0x3c, 0x00,
|
|
0x7f, 0x0f, 0xc7, 0xfe, 0x1f, 0xfc, 0x1f, 0xff, 0x1c, 0x00, 0x70, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
|
|
0x7f, 0x1f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x0e, 0x38, 0x0e, 0x3f, 0xf8,
|
|
0x7f, 0x3f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x1e, 0x3f, 0xfe, 0x3f, 0xf0,
|
|
0x77, 0x3b, 0x87, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xfc, 0x38, 0x00,
|
|
0x77, 0xfb, 0x8f, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xf8, 0x38, 0x00,
|
|
0x73, 0xf3, 0x8f, 0xff, 0x0f, 0xff, 0x1c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x78, 0x7f, 0xf8,
|
|
0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfe, 0x3c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x3c, 0x7f, 0xf8,
|
|
0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8,
|
|
};
|
|
|
|
void UITask::begin(DisplayDriver* display, SensorManager* sensors, CompanionNodePrefs* node_prefs) {
|
|
_display = display;
|
|
_sensors = sensors;
|
|
if (_display != NULL) {
|
|
_display->setRotationDegrees(node_prefs->display_rotation_degrees);
|
|
}
|
|
|
|
clearMsgPreview();
|
|
_node_prefs = node_prefs;
|
|
resetRadioProfileDisplayPage();
|
|
if (_display != NULL) {
|
|
_display->servicePower(_board->isExternalPowered() || _board->isUsbHostConnected(), hasConnection());
|
|
}
|
|
|
|
// strip off dash and commit hash by changing dash to null terminator
|
|
// e.g: v1.2.3-abcdef -> v1.2.3
|
|
char *version = strdup(FIRMWARE_VERSION);
|
|
char *dash = strchr(version, '-');
|
|
if (dash) {
|
|
*dash = 0;
|
|
}
|
|
|
|
// v1.2.3 (1 Jan 2025)
|
|
sprintf(_version_info, "%s (%s)", version, FIRMWARE_BUILD_DATE);
|
|
|
|
#ifdef PIN_BUZZER
|
|
buzzer.begin();
|
|
buzzer.quiet(_node_prefs->buzzer_quiet);
|
|
buzzer.startup();
|
|
#endif
|
|
|
|
#ifdef HAS_DRV2605
|
|
vibration.begin();
|
|
vibration.quiet(_node_prefs->vibe_quiet);
|
|
#endif
|
|
|
|
// Initialize digital button if available
|
|
#ifdef PIN_USER_BTN
|
|
_userButton = new Button(PIN_USER_BTN, USER_BTN_PRESSED);
|
|
_userButton->begin();
|
|
|
|
// Set up digital button callbacks
|
|
_userButton->onShortPress([this]() { handleButtonShortPress(); });
|
|
_userButton->onDoublePress([this]() { handleButtonDoublePress(); });
|
|
_userButton->onTriplePress([this]() { handleButtonTriplePress(); });
|
|
_userButton->onQuadruplePress([this]() { handleButtonQuadruplePress(); });
|
|
_userButton->onLongPress([this]() { handleButtonLongPress(); });
|
|
_userButton->onAnyPress([this]() { handleButtonAnyPress(); });
|
|
#endif
|
|
|
|
// Initialize analog button if available
|
|
#ifdef PIN_USER_BTN_ANA
|
|
_userButtonAnalog = new Button(PIN_USER_BTN_ANA, USER_BTN_PRESSED, true, 20);
|
|
_userButtonAnalog->begin();
|
|
|
|
// Set up analog button callbacks
|
|
_userButtonAnalog->onShortPress([this]() { handleButtonShortPress(); });
|
|
_userButtonAnalog->onDoublePress([this]() { handleButtonDoublePress(); });
|
|
_userButtonAnalog->onTriplePress([this]() { handleButtonTriplePress(); });
|
|
_userButtonAnalog->onQuadruplePress([this]() { handleButtonQuadruplePress(); });
|
|
_userButtonAnalog->onLongPress([this]() { handleButtonLongPress(); });
|
|
_userButtonAnalog->onAnyPress([this]() { handleButtonAnyPress(); });
|
|
#endif
|
|
ui_started_at = millis();
|
|
}
|
|
|
|
void UITask::resetRadioProfileDisplayPage() {
|
|
_radio_profile_page_started_at = millis();
|
|
_dual_radio_enabled_seen = the_mesh.isDualRadioActive();
|
|
_radio_profile_display_page = 0;
|
|
}
|
|
|
|
bool UITask::showingSecondaryRadioProfilePage() {
|
|
const bool dual_radio_enabled = the_mesh.isDualRadioActive();
|
|
if (dual_radio_enabled) {
|
|
return mesh::ui::showManualSecondaryRadioProfilePage(
|
|
true, _radio_profile_display_page);
|
|
}
|
|
const uint8_t status_pages = radioProfileStatusPageCount(*_display,
|
|
dual_radio_enabled);
|
|
return mesh::ui::showSecondaryRadioProfilePage(dual_radio_enabled,
|
|
status_pages,
|
|
millis() - _radio_profile_page_started_at);
|
|
}
|
|
|
|
void UITask::serviceRadioProfileDisplayPage() {
|
|
const bool dual_radio_enabled = the_mesh.isDualRadioActive();
|
|
if (dual_radio_enabled != _dual_radio_enabled_seen) {
|
|
resetRadioProfileDisplayPage();
|
|
_need_refresh = true;
|
|
}
|
|
if (dual_radio_enabled) return; // Button presses own all dual-radio pages.
|
|
const uint8_t status_pages = radioProfileStatusPageCount(*_display,
|
|
dual_radio_enabled);
|
|
const uint8_t page = mesh::ui::radioProfileDisplayPageIndex(
|
|
dual_radio_enabled, status_pages,
|
|
millis() - _radio_profile_page_started_at);
|
|
if (page != _radio_profile_display_page) {
|
|
_radio_profile_display_page = page;
|
|
_need_refresh = true;
|
|
}
|
|
}
|
|
|
|
bool UITask::shouldPlayMessageTone() const {
|
|
#ifdef BLE_PIN_CODE
|
|
return !hasBluetoothConnection();
|
|
#else
|
|
// A USB or Ethernet companion connection is not a Bluetooth connection and
|
|
// should not silence the standalone new-message alert.
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
void UITask::notify(UIEventType t) {
|
|
#if defined(PIN_BUZZER)
|
|
switch(t){
|
|
case UIEventType::contactMessage:
|
|
// gemini's pick
|
|
if (shouldPlayMessageTone()) {
|
|
buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
|
|
}
|
|
break;
|
|
case UIEventType::channelMessage:
|
|
if (shouldPlayMessageTone()) {
|
|
buzzer.play("kerplop:d=16,o=6,b=120:32g#,32c#");
|
|
}
|
|
break;
|
|
case UIEventType::ack:
|
|
buzzer.play("ack:d=32,o=8,b=120:c");
|
|
break;
|
|
case UIEventType::roomMessage:
|
|
case UIEventType::newContactMessage:
|
|
case UIEventType::none:
|
|
default:
|
|
break;
|
|
}
|
|
#endif
|
|
// Serial.print("DBG: Alert user -> ");
|
|
// Serial.println((int) t);
|
|
}
|
|
|
|
void UITask::msgRead(int msgcount) {
|
|
_msgcount = msgcount;
|
|
if (msgcount == 0) {
|
|
clearMsgPreview();
|
|
}
|
|
}
|
|
|
|
void UITask::clearMsgPreview() {
|
|
_origin[0] = 0;
|
|
_msg[0] = 0;
|
|
_need_refresh = true;
|
|
}
|
|
|
|
void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text,
|
|
int msgcount, int channel_idx,
|
|
const char* channel_name, int queue_index) {
|
|
(void)queue_index;
|
|
(void)channel_idx;
|
|
(void)channel_name;
|
|
_msgcount = msgcount;
|
|
|
|
#ifdef HAS_DRV2605
|
|
vibration.trigger(); // vibrate even while the app is connected (honors quiet + cooldown)
|
|
#endif
|
|
|
|
if (path_len == 0xFF) {
|
|
sprintf(_origin, "(F) %s", from_name);
|
|
} else {
|
|
sprintf(_origin, "(%d) %s", (uint32_t) path_len, from_name);
|
|
}
|
|
StrHelper::strncpy(_msg, text, sizeof(_msg));
|
|
|
|
if (_display != NULL) {
|
|
if (shouldWakeDisplayForMessage()) {
|
|
_display->wake(mesh::ui::DisplayWake::Message);
|
|
}
|
|
if (_display->isOn()) _need_refresh = true;
|
|
}
|
|
}
|
|
|
|
void UITask::renderBatteryIndicator(uint16_t batteryMilliVolts) {
|
|
// Convert millivolts to percentage
|
|
#ifndef BATT_MIN_MILLIVOLTS
|
|
#define BATT_MIN_MILLIVOLTS 3000
|
|
#endif
|
|
#ifndef BATT_MAX_MILLIVOLTS
|
|
#define BATT_MAX_MILLIVOLTS 4200
|
|
#endif
|
|
const int minMilliVolts = BATT_MIN_MILLIVOLTS;
|
|
const int maxMilliVolts = BATT_MAX_MILLIVOLTS;
|
|
int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
|
|
if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
|
|
if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
|
|
|
|
// battery icon
|
|
int iconWidth = 24;
|
|
int iconHeight = 12;
|
|
int iconX = _display->width() - iconWidth - 5; // Position the icon near the top-right corner
|
|
int iconY = 0;
|
|
_display->setColor(UIColor::primary_txt);
|
|
|
|
// battery outline
|
|
_display->drawRect(iconX, iconY, iconWidth, iconHeight);
|
|
|
|
// battery "cap"
|
|
_display->fillRect(iconX + iconWidth, iconY + (iconHeight / 4), 3, iconHeight / 2);
|
|
|
|
// fill the battery based on the percentage
|
|
int fillWidth = (batteryPercentage * (iconWidth - 4)) / 100;
|
|
_display->fillRect(iconX + 2, iconY + 2, fillWidth, iconHeight - 4);
|
|
}
|
|
|
|
void UITask::renderCurrScreen() {
|
|
if (_display == NULL) return; // assert() ??
|
|
|
|
char tmp[80];
|
|
const bool pairing_screen_active = isPairingScreenActive();
|
|
if (!pairing_screen_active && _alert[0]) {
|
|
_display->setTextSize(1.4);
|
|
uint16_t textWidth = _display->getTextWidth(_alert);
|
|
_display->setCursor((_display->width() - textWidth) / 2, 22);
|
|
_display->setColor(UIColor::warning_txt);
|
|
_display->print(_alert);
|
|
_alert[0] = 0;
|
|
_need_refresh = true;
|
|
return;
|
|
} else if (!pairing_screen_active && _origin[0] && _msg[0]) { // message preview
|
|
// render message preview
|
|
_display->setCursor(0, 0);
|
|
_display->setTextSize(1);
|
|
_display->setColor(UIColor::primary_txt);
|
|
_display->print(_node_prefs->node_name);
|
|
|
|
#if UI_SMALL_MESSAGE_FONT == 1
|
|
mesh::ui::drawSmallMessageBody(*_display, _origin, _msg, 12);
|
|
#else
|
|
_display->setCursor(0, 12);
|
|
_display->setColor(UIColor::secondary_txt);
|
|
_display->print(_origin);
|
|
_display->setCursor(0, 24);
|
|
_display->print(_msg);
|
|
|
|
_display->setCursor(_display->width() - 28, 9);
|
|
_display->setTextSize(2);
|
|
_display->setColor(UIColor::primary_txt);
|
|
sprintf(tmp, "%d", _msgcount);
|
|
_display->print(tmp);
|
|
#endif
|
|
_display->setColor(UIColor::secondary_txt); // last color will be kept on T114
|
|
} else if (!pairing_screen_active && (millis() - ui_started_at) < BOOT_SCREEN_MILLIS) { // boot screen
|
|
// meshcore logo
|
|
_display->setColor(UIColor::corp_blue);
|
|
int logoWidth = 128;
|
|
_display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
|
|
|
|
// version info
|
|
_display->setColor(UIColor::primary_txt);
|
|
_display->setTextSize(1);
|
|
uint16_t textWidth = _display->getTextWidth(_version_info);
|
|
_display->setCursor((_display->width() - textWidth) / 2, 22);
|
|
_display->print(_version_info);
|
|
} else { // home screen
|
|
const bool dual_radio_enabled = the_mesh.isDualRadioActive();
|
|
const uint8_t status_pages = radioProfileStatusPageCount(
|
|
*_display, dual_radio_enabled);
|
|
uint8_t status_page_index = 0;
|
|
const bool status_page = dual_radio_enabled
|
|
? mesh::ui::showManualRadioProfileSystemStatusPage(
|
|
true, status_pages, _radio_profile_display_page,
|
|
&status_page_index)
|
|
: mesh::ui::showRadioProfileSystemStatusPage(
|
|
false, status_pages, millis() - _radio_profile_page_started_at,
|
|
&status_page_index);
|
|
if (status_page) {
|
|
mesh::ui::drawRadioProfileSystemStatusPage(*_display,
|
|
radioProfileSystemStatus(*_node_prefs, *_board), status_page_index);
|
|
_need_refresh = false;
|
|
return;
|
|
}
|
|
// node name
|
|
_display->setCursor(0, 0);
|
|
_display->setTextSize(1);
|
|
_display->setColor(UIColor::primary_txt);
|
|
_display->print(_node_prefs->node_name);
|
|
|
|
// battery voltage
|
|
renderBatteryIndicator(_board->getBattMilliVolts());
|
|
|
|
const auto* radio = the_mesh.getProfileRadio();
|
|
const mesh::RadioProfiles* profiles = radio ? radio->profiles() : NULL;
|
|
const bool secondary_page = showingSecondaryRadioProfilePage();
|
|
const bool dual_radio = profiles != NULL && profiles->enabled();
|
|
float freq = _node_prefs->freq;
|
|
float bw = _node_prefs->bw;
|
|
uint8_t sf = _node_prefs->sf;
|
|
uint8_t cr = _node_prefs->cr;
|
|
const char* profile_tag = "";
|
|
if (dual_radio) {
|
|
const uint8_t profile = secondary_page ? 1 : 0;
|
|
const auto& params = profiles->params(profile);
|
|
freq = params.freq;
|
|
bw = params.bw;
|
|
sf = params.sf;
|
|
cr = params.cr;
|
|
profile_tag = mesh::ui::radioProfileDisplayTag(profile,
|
|
profile == 1 ? profiles->secondary_temporary : profiles->primary_temporary);
|
|
}
|
|
|
|
// Compact R1/R2 rows preserve every RF field on 128px displays.
|
|
_display->setCursor(0, 20);
|
|
_display->setColor(UIColor::secondary_txt);
|
|
if (dual_radio) {
|
|
snprintf(tmp, sizeof(tmp), "%s F:%06.3f S:%d", profile_tag, freq, sf);
|
|
} else {
|
|
snprintf(tmp, sizeof(tmp), "FREQ: %06.3f SF%d", freq, sf);
|
|
}
|
|
_display->print(tmp);
|
|
|
|
_display->setCursor(0, 30);
|
|
if (dual_radio) {
|
|
snprintf(tmp, sizeof(tmp), "%s B:%03.2f C:%d", profile_tag, bw, cr);
|
|
} else {
|
|
snprintf(tmp, sizeof(tmp), "BW: %03.2f CR: %d", bw, cr);
|
|
}
|
|
_display->print(tmp);
|
|
|
|
// BT pin
|
|
const bool bluetooth_connected = hasBluetoothConnection();
|
|
const uint32_t bluetooth_pin = the_mesh.getBLEPin();
|
|
if (mesh::ui::shouldDisplayBluetoothPairingPin(
|
|
isBluetoothEnabled(), bluetooth_connected, bluetooth_pin)) {
|
|
_display->setColor(UIColor::warning_txt);
|
|
_display->setTextSize(2);
|
|
_display->setCursor(0, 43);
|
|
sprintf(tmp, "Pin:%u", (unsigned int)bluetooth_pin);
|
|
_display->print(tmp);
|
|
_display->setColor(UIColor::primary_txt);
|
|
} else {
|
|
_display->setColor(UIColor::primary_txt);
|
|
}
|
|
}
|
|
_need_refresh = false;
|
|
}
|
|
|
|
#ifdef STATUS_LED_RGB
|
|
static void statusLedWrite(uint8_t r, uint8_t g, uint8_t b) {
|
|
analogWrite(PIN_STATUS_LED_R, r);
|
|
analogWrite(PIN_STATUS_LED_G, g);
|
|
analogWrite(PIN_STATUS_LED_B, b);
|
|
}
|
|
|
|
static void statusLedWheel(uint8_t pos) { // smooth hue sweep, 0..255
|
|
if (pos < 85) {
|
|
statusLedWrite(255 - pos * 3, pos * 3, 0);
|
|
} else if (pos < 170) {
|
|
pos -= 85;
|
|
statusLedWrite(0, 255 - pos * 3, pos * 3);
|
|
} else {
|
|
pos -= 170;
|
|
statusLedWrite(pos * 3, 0, 255 - pos * 3);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
void UITask::userLedHandler() {
|
|
#ifdef STATUS_LED_RGB
|
|
static bool booted = false;
|
|
static bool flourish_active = false;
|
|
static unsigned long flourish_until = 0;
|
|
static int prev_msgcount = 0;
|
|
static int state = 0;
|
|
static unsigned long next_change = 0;
|
|
static int last_increment = 0;
|
|
static unsigned long next_batt_check = 0;
|
|
static bool low_batt = false;
|
|
|
|
unsigned long cur_time = millis();
|
|
|
|
if (!booted) { // color sweep on boot
|
|
booted = true;
|
|
flourish_until = cur_time + 1200;
|
|
flourish_active = true;
|
|
}
|
|
if (_msgcount > prev_msgcount) { // color sweep when a new message arrives
|
|
flourish_until = cur_time + 800;
|
|
flourish_active = true;
|
|
}
|
|
prev_msgcount = _msgcount;
|
|
|
|
if (flourish_active) {
|
|
if (cur_time < flourish_until) {
|
|
statusLedWheel((cur_time % 1200) * 255 / 1200);
|
|
return;
|
|
}
|
|
statusLedWrite(0, 0, 0);
|
|
flourish_active = false;
|
|
state = 0;
|
|
next_change = cur_time;
|
|
}
|
|
|
|
if (cur_time > next_batt_check) { // battery reads are not free, keep them rare
|
|
low_batt = _board->getBattMilliVolts() < LOW_BATT_MILLIVOLTS;
|
|
next_batt_check = cur_time + 60000;
|
|
}
|
|
|
|
#ifdef EXT_CHRG_DETECT
|
|
// charge status display while docked (skipped when messages are waiting,
|
|
// so the unread indication is never masked)
|
|
static unsigned long next_pwr_check = 0;
|
|
static bool ext_powered = false, ext_charging = false;
|
|
if (cur_time > next_pwr_check) {
|
|
ext_powered = _board->isExternalPowered();
|
|
ext_charging = ext_powered && digitalRead(EXT_CHRG_DETECT) == LOW;
|
|
next_pwr_check = cur_time + 1000;
|
|
}
|
|
if (ext_powered && _msgcount == 0) {
|
|
if (ext_charging) {
|
|
// amber breathing while charging
|
|
int ph = cur_time % 2000;
|
|
int v = ph < 1000 ? ph : 2000 - ph;
|
|
uint8_t lvl = (uint8_t)(v * 255 / 1000);
|
|
statusLedWrite(lvl, (uint8_t)(lvl * 35 / 100), 0);
|
|
} else {
|
|
statusLedWrite(0, 40, 0); // dim solid green: charge complete
|
|
}
|
|
state = 0;
|
|
next_change = cur_time;
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
if (cur_time > next_change) {
|
|
if (state == 0) {
|
|
state = 1;
|
|
last_increment = (_msgcount > 0) ? LED_ON_MSG_MILLIS : LED_ON_MILLIS;
|
|
next_change = cur_time + last_increment;
|
|
if (low_batt) {
|
|
statusLedWrite(255, 0, 0); // red: battery low
|
|
} else if (_msgcount > 0) {
|
|
statusLedWrite(255, 90, 0); // amber: unread messages
|
|
} else if (_connected) {
|
|
statusLedWrite(0, 0, 255); // blue: app connected
|
|
} else {
|
|
statusLedWrite(0, 255, 0); // green: heartbeat
|
|
}
|
|
} else {
|
|
state = 0;
|
|
next_change = cur_time + LED_CYCLE_MILLIS - last_increment;
|
|
statusLedWrite(0, 0, 0);
|
|
}
|
|
}
|
|
#elif defined(PIN_STATUS_LED)
|
|
static int state = 0;
|
|
static int next_change = 0;
|
|
static int last_increment = 0;
|
|
|
|
int cur_time = millis();
|
|
if (cur_time > next_change) {
|
|
if (state == 0) {
|
|
state = 1;
|
|
if (_msgcount > 0) {
|
|
last_increment = LED_ON_MSG_MILLIS;
|
|
} else {
|
|
last_increment = LED_ON_MILLIS;
|
|
}
|
|
next_change = cur_time + last_increment;
|
|
} else {
|
|
state = 0;
|
|
next_change = cur_time + LED_CYCLE_MILLIS - last_increment;
|
|
}
|
|
digitalWrite(PIN_STATUS_LED, state == LED_STATE_ON);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
hardware-agnostic pre-shutdown activity should be done here
|
|
*/
|
|
void UITask::shutdown(bool restart){
|
|
if (!prepareForShutdown()) return;
|
|
|
|
#ifdef PIN_BUZZER
|
|
/* note: we have a choice here -
|
|
we can do a blocking buzzer.loop() with non-deterministic consequences
|
|
or we can set a flag and delay the shutdown for a couple of seconds
|
|
while a non-blocking buzzer.loop() plays out in UITask::loop()
|
|
*/
|
|
buzzer.shutdown();
|
|
uint32_t buzzer_timer = millis(); // fail-safe shutdown
|
|
while (buzzer.isPlaying() && (millis() - 2500) < buzzer_timer)
|
|
buzzer.loop();
|
|
|
|
#endif // PIN_BUZZER
|
|
|
|
if (restart) {
|
|
_board->reboot();
|
|
} else {
|
|
// Power off board including radio, display, GPS and components
|
|
_board->powerOff();
|
|
}
|
|
}
|
|
|
|
bool UITask::isPairingScreenActive() const {
|
|
return mesh::ui::isBluetoothPairingPromptActive(
|
|
isBluetoothEnabled(), hasBluetoothConnection(),
|
|
static_cast<uint32_t>(_pairing_screen_until),
|
|
static_cast<uint32_t>(millis()));
|
|
}
|
|
|
|
void UITask::showPairingPin() {
|
|
if (!isBluetoothEnabled()) return;
|
|
|
|
const unsigned long now = millis();
|
|
_pairing_screen_until = now + BLE_PAIRING_DISPLAY_MILLIS;
|
|
_need_refresh = true;
|
|
_next_refresh = 0;
|
|
if (_display != NULL && !_display->isOn()) {
|
|
_display->servicePower(_board->isExternalPowered() || _board->isUsbHostConnected(), hasConnection(), true);
|
|
}
|
|
}
|
|
|
|
void UITask::finishPairingScreen(bool timed_out) {
|
|
_pairing_screen_until = 0;
|
|
_need_refresh = true;
|
|
_next_refresh = 0;
|
|
if (_display == NULL) return;
|
|
|
|
if (timed_out) {
|
|
_display->dismiss();
|
|
}
|
|
}
|
|
|
|
void UITask::servicePairingState() {
|
|
if (_interfaceManager->takePairingRequest()) {
|
|
showPairingPin();
|
|
}
|
|
|
|
if (_pairing_screen_until != 0) {
|
|
const bool timed_out =
|
|
static_cast<int32_t>(millis() - _pairing_screen_until) >= 0;
|
|
if (!isPairingScreenActive()) {
|
|
finishPairingScreen(timed_out);
|
|
}
|
|
}
|
|
if (_display != NULL && _display->servicePower(
|
|
_board->isExternalPowered() || _board->isUsbHostConnected(),
|
|
hasConnection(), isPairingScreenActive())) {
|
|
_next_refresh = 0;
|
|
_need_refresh = true;
|
|
}
|
|
}
|
|
|
|
void UITask::loop() {
|
|
servicePairingState();
|
|
|
|
#ifdef PIN_USER_BTN
|
|
if (_userButton) {
|
|
_userButton->update();
|
|
}
|
|
#endif
|
|
#ifdef PIN_USER_BTN_ANA
|
|
if (_userButtonAnalog) {
|
|
_userButtonAnalog->update();
|
|
}
|
|
#endif
|
|
userLedHandler();
|
|
|
|
#ifdef PIN_BUZZER
|
|
if (buzzer.isPlaying()) buzzer.loop();
|
|
#endif
|
|
|
|
if (_display != NULL && _display->isOn()) {
|
|
if (!isPairingScreenActive() && !_alert[0] && !(_origin[0] && _msg[0])
|
|
&& (millis() - ui_started_at) >= BOOT_SCREEN_MILLIS) {
|
|
serviceRadioProfileDisplayPage();
|
|
}
|
|
static bool _firstBoot = true;
|
|
if(_firstBoot && (millis() - ui_started_at) >= BOOT_SCREEN_MILLIS) {
|
|
_need_refresh = true;
|
|
_firstBoot = false;
|
|
}
|
|
if (millis() >= _next_refresh && _need_refresh) {
|
|
_display->startFrame();
|
|
renderCurrScreen();
|
|
_display->endFrame();
|
|
|
|
_next_refresh = millis() + 1000; // refresh every second
|
|
}
|
|
}
|
|
}
|
|
|
|
void UITask::handleButtonAnyPress() {
|
|
MESH_DEBUG_PRINTLN("UITask: any press triggered");
|
|
// called on any button press before other events, to wake up the display quickly
|
|
// do not refresh the display here, as it may block the button handler
|
|
if (_display != NULL) {
|
|
_displayWasOn = _display->isOn(); // Track display state before any action
|
|
_display->wake(mesh::ui::DisplayWake::Button);
|
|
if (!_displayWasOn) resetRadioProfileDisplayPage();
|
|
}
|
|
}
|
|
|
|
void UITask::handleButtonShortPress() {
|
|
MESH_DEBUG_PRINTLN("UITask: short press triggered");
|
|
if (_display != NULL) {
|
|
// Only clear message preview if display was already on before button press
|
|
if (_displayWasOn) {
|
|
// If display was on and showing message preview, clear it
|
|
if (_origin[0] && _msg[0]) {
|
|
clearMsgPreview();
|
|
} else {
|
|
if (!_alert[0] && !isPairingScreenActive()
|
|
&& the_mesh.isDualRadioActive()) {
|
|
const uint8_t status_pages = radioProfileStatusPageCount(
|
|
*_display, true);
|
|
const uint8_t page_count = mesh::ui::radioProfileDisplayPageCount(
|
|
true, status_pages);
|
|
_radio_profile_display_page =
|
|
(_radio_profile_display_page + 1) % page_count;
|
|
}
|
|
_need_refresh = true;
|
|
}
|
|
} else {
|
|
_need_refresh = true; // display just turned on, so we need to refresh
|
|
}
|
|
// Note: Display turn-on and auto-off timer extension are handled by handleButtonAnyPress
|
|
}
|
|
}
|
|
|
|
void UITask::handleButtonDoublePress() {
|
|
MESH_DEBUG_PRINTLN("UITask: double press triggered, sending advert");
|
|
// ADVERT
|
|
#ifdef PIN_BUZZER
|
|
notify(UIEventType::ack);
|
|
#endif
|
|
if (the_mesh.advert()) {
|
|
MESH_DEBUG_PRINTLN("Advert sent!");
|
|
sprintf(_alert, "Advert sent!");
|
|
} else {
|
|
MESH_DEBUG_PRINTLN("Advert failed!");
|
|
sprintf(_alert, "Advert failed..");
|
|
}
|
|
_need_refresh = true;
|
|
}
|
|
|
|
void UITask::handleButtonTriplePress() {
|
|
MESH_DEBUG_PRINTLN("UITask: triple press triggered");
|
|
#if defined(PIN_BUZZER) && defined(HAS_DRV2605)
|
|
// cycle alert modes: Buzz+Vibe -> Buzz only -> Vibe only -> Silent
|
|
int mode = (_node_prefs->buzzer_quiet ? 2 : 0) | (_node_prefs->vibe_quiet ? 1 : 0);
|
|
mode = (mode + 1) & 3;
|
|
_node_prefs->buzzer_quiet = (mode & 2) ? 1 : 0;
|
|
_node_prefs->vibe_quiet = (mode & 1) ? 1 : 0;
|
|
buzzer.quiet(_node_prefs->buzzer_quiet);
|
|
vibration.quiet(_node_prefs->vibe_quiet);
|
|
// audible/tactile confirmation of the new mode (no screen on some boards)
|
|
if (!_node_prefs->buzzer_quiet) notify(UIEventType::ack);
|
|
if (!_node_prefs->vibe_quiet) vibration.trigger(true);
|
|
switch (mode) {
|
|
case 0: sprintf(_alert, "Alerts: Buzz+Vibe"); break;
|
|
case 1: sprintf(_alert, "Alerts: Buzz only"); break;
|
|
case 2: sprintf(_alert, "Alerts: Vibe only"); break;
|
|
default: sprintf(_alert, "Alerts: Silent"); break;
|
|
}
|
|
the_mesh.savePrefs();
|
|
_need_refresh = true;
|
|
#elif defined(PIN_BUZZER)
|
|
if (buzzer.isQuiet()) {
|
|
buzzer.quiet(false);
|
|
notify(UIEventType::ack);
|
|
sprintf(_alert, "Buzzer: ON");
|
|
} else {
|
|
buzzer.quiet(true);
|
|
sprintf(_alert, "Buzzer: OFF");
|
|
}
|
|
_node_prefs->buzzer_quiet = buzzer.isQuiet();
|
|
the_mesh.savePrefs();
|
|
_need_refresh = true;
|
|
#endif
|
|
}
|
|
|
|
void UITask::handleButtonQuadruplePress() {
|
|
MESH_DEBUG_PRINTLN("UITask: quad press triggered");
|
|
if (_sensors != NULL) {
|
|
// toggle GPS onn/off
|
|
int num = _sensors->getNumSettings();
|
|
for (int i = 0; i < num; i++) {
|
|
if (strcmp(_sensors->getSettingName(i), "gps") == 0) {
|
|
if (strcmp(_sensors->getSettingValue(i), "1") == 0) {
|
|
_sensors->setSettingValue("gps", "0");
|
|
notify(UIEventType::ack);
|
|
sprintf(_alert, "GPS: Disabled");
|
|
} else {
|
|
_sensors->setSettingValue("gps", "1");
|
|
notify(UIEventType::ack);
|
|
sprintf(_alert, "GPS: Enabled");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
_need_refresh = true;
|
|
}
|
|
|
|
void UITask::handleButtonLongPress() {
|
|
MESH_DEBUG_PRINTLN("UITask: long press triggered");
|
|
if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue
|
|
the_mesh.enterCLIRescue();
|
|
} else {
|
|
shutdown();
|
|
}
|
|
}
|