Quellcode durchsuchen

UI: battery indicator, boot screen, radio settings

Jacob Quatier vor 1 Jahr
Ursprung
Commit
c34dd2a40c

+ 22 - 0
examples/companion_radio/NodePrefs.h

@@ -0,0 +1,22 @@
+#ifndef NODE_PREFS_H
+#define NODE_PREFS_H
+
+#include <cstdint> // For uint8_t, uint32_t
+
+struct NodePrefs {  // persisted to file
+  float airtime_factor;
+  char node_name[32];
+  double node_lat, node_lon;
+  float freq;
+  uint8_t sf;
+  uint8_t cr;
+  uint8_t reserved1;
+  uint8_t manual_add_contacts;
+  float bw;
+  uint8_t tx_power_dbm;
+  uint8_t unused[3];
+  float rx_delay_base;
+  uint32_t ble_pin;
+};
+
+#endif // NODE_PREFS_H

+ 68 - 17
examples/companion_radio/UITask.cpp

@@ -1,8 +1,10 @@
 #include "UITask.h"
 #include <Arduino.h>
 #include <helpers/TxtDataHelpers.h>
+#include "NodePrefs.h"
 
-#define AUTO_OFF_MILLIS   15000   // 15 seconds
+#define AUTO_OFF_MILLIS     15000   // 15 seconds
+#define BOOT_SCREEN_MILLIS   4000   // 4 seconds
 
 #ifndef USER_BTN_PRESSED
 #define USER_BTN_PRESSED LOW
@@ -25,11 +27,11 @@ static const uint8_t meshcore_logo [] PROGMEM = {
     0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8, 
 };
 
-void UITask::begin(DisplayDriver* display, const char* node_name, const char* build_date, const char* firmware_version, uint32_t pin_code) {
+void UITask::begin(DisplayDriver* display, NodePrefs* node_prefs, const char* build_date, const char* firmware_version, uint32_t pin_code) {
   _display = display;
   _auto_off = millis() + AUTO_OFF_MILLIS;
   clearMsgPreview();
-  _node_name = node_name;
+  _node_prefs = node_prefs;
   _pin_code = pin_code;
   if (_display != NULL) {
     _display->turnOn();
@@ -81,16 +83,42 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i
   }
 }
 
+void renderBatteryIndicator(DisplayDriver* _display, uint16_t batteryMilliVolts) {
+  // Convert millivolts to percentage
+  const int minMilliVolts = 3000; // Minimum voltage (e.g., 3.0V)
+  const int maxMilliVolts = 4200; // Maximum voltage (e.g., 4.2V)
+  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(DisplayDriver::GREEN);
+
+  // 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 - 2)) / 100;
+  _display->fillRect(iconX + 1, iconY + 1, fillWidth, iconHeight - 2);
+}
+
 void UITask::renderCurrScreen() {
   if (_display == NULL) return;  // assert() ??
 
   char tmp[80];
-  if (_origin[0] && _msg[0]) {
+  if (_origin[0] && _msg[0]) { // message preview
     // render message preview
     _display->setCursor(0, 0);
     _display->setTextSize(1);
     _display->setColor(DisplayDriver::GREEN);
-    _display->print(_node_name);
+    _display->print(_node_prefs->node_name);
 
     _display->setCursor(0, 12);
     _display->setColor(DisplayDriver::YELLOW);
@@ -104,23 +132,41 @@ void UITask::renderCurrScreen() {
     _display->setColor(DisplayDriver::ORANGE);
     sprintf(tmp, "%d", _msgcount);
     _display->print(tmp);
-  } else {
-    // render 'home' screen
+  } else if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
+    // meshcore logo
     _display->setColor(DisplayDriver::BLUE);
-    _display->drawXbm(0, 0, meshcore_logo, 128, 13);
-    _display->setCursor(0, 20);
-    _display->setTextSize(1);
+    int logoWidth = 128;
+    _display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
 
+    // version info
     _display->setColor(DisplayDriver::LIGHT);
-    _display->print(_node_name);
-    
-    _display->setCursor(0, 32);
+    _display->setTextSize(1);
+    int textWidth = strlen(_version_info) * 6; // Assuming each character is 6 pixels wide
+    _display->setCursor((_display->width() - textWidth) / 2, 22);
     _display->print(_version_info);
+  } else {  // home screen
+    // node name
+    _display->setCursor(0, 0);
+    _display->setTextSize(1);
+    _display->setColor(DisplayDriver::GREEN);
+    _display->print(_node_prefs->node_name);
+
+    // battery voltage
+    renderBatteryIndicator(_display, _board->getBattMilliVolts());
+
+    // freq / sf
+    _display->setCursor(0, 20);
+    _display->setColor(DisplayDriver::YELLOW);
+    sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf);
+    _display->print(tmp);
 
-    if (_connected) {
-      //_display->printf("freq : %03.2f sf %d\n", _prefs.freq, _prefs.sf);
-      //_display->printf("bw   : %03.2f cr %d\n", _prefs.bw, _prefs.cr);
-    } else if (_pin_code != 0) {
+    // bw / cr
+    _display->setCursor(0, 30);
+    sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
+    _display->print(tmp);
+
+    // BT pin
+    if (_pin_code != 0) {
       _display->setColor(DisplayDriver::RED);
       _display->setTextSize(2);
       _display->setCursor(0, 43);
@@ -198,6 +244,11 @@ void UITask::loop() {
   userLedHandler();
 
   if (_display != NULL && _display->isOn()) {
+    static bool _firstBoot = true;
+    if(_firstBoot && millis() >= BOOT_SCREEN_MILLIS) {
+      _need_refresh = true;
+      _firstBoot = false;
+    }
     if (millis() >= _next_refresh && _need_refresh) {
       _display->startFrame();
       renderCurrScreen();

+ 4 - 2
examples/companion_radio/UITask.h

@@ -4,13 +4,15 @@
 #include <helpers/ui/DisplayDriver.h>
 #include <stddef.h>
 
+#include "NodePrefs.h"
+
 class UITask {
   DisplayDriver* _display;
   mesh::MainBoard* _board;
   unsigned long _next_refresh, _auto_off;
   bool _connected;
   uint32_t _pin_code;
-  const char* _node_name;
+  NodePrefs* _node_prefs;
   char _version_info[32];
   char _origin[62];
   char _msg[80];
@@ -27,7 +29,7 @@ public:
       _next_refresh = 0; 
       _connected = false;
   }
-  void begin(DisplayDriver* display, const char* node_name, const char* build_date, const char* firmware_version, uint32_t pin_code);
+  void begin(DisplayDriver* display, NodePrefs* node_prefs, const char* build_date, const char* firmware_version, uint32_t pin_code);
 
   void setHasConnection(bool connected) { _connected = connected; }
   bool hasDisplay() const { return _display != NULL; }

+ 5 - 17
examples/companion_radio/main.cpp

@@ -12,6 +12,7 @@
 #include <helpers/SimpleMeshTables.h>
 #include <helpers/IdentityStore.h>
 #include <helpers/BaseSerialInterface.h>
+#include "NodePrefs.h"
 #include <RTClib.h>
 #include <target.h>
 
@@ -181,22 +182,6 @@ static uint32_t _atoi(const char* sp) {
 
 #define MAX_SIGN_DATA_LEN    (8*1024)   // 8K
 
-struct NodePrefs {  // persisted to file
-  float airtime_factor;
-  char node_name[32];
-  double node_lat, node_lon;
-  float freq;
-  uint8_t sf;
-  uint8_t cr;
-  uint8_t reserved1;
-  uint8_t manual_add_contacts;
-  float bw;
-  uint8_t tx_power_dbm;
-  uint8_t unused[3];
-  float rx_delay_base;
-  uint32_t ble_pin;
-};
-
 class MyMesh : public BaseChatMesh {
   FILESYSTEM* _fs;
   IdentityStore* _identity_store;
@@ -837,6 +822,9 @@ public:
   }
 
   const char* getNodeName() { return _prefs.node_name; }
+  NodePrefs* getNodePrefs() { 
+    return &_prefs; 
+  }
   uint32_t getBLEPin() { return _active_ble_pin; }
 
   void startInterface(BaseSerialInterface& serial) {
@@ -1529,7 +1517,7 @@ void setup() {
 #endif
 
 #ifdef HAS_UI
-  ui_task.begin(disp, the_mesh.getNodeName(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION, the_mesh.getBLEPin());
+  ui_task.begin(disp, the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION, the_mesh.getBLEPin());
 #endif
 }
 

+ 41 - 14
examples/simple_repeater/UITask.cpp

@@ -1,7 +1,9 @@
 #include "UITask.h"
 #include <Arduino.h>
+#include <helpers/CommonCLI.h>
 
-#define AUTO_OFF_MILLIS   20000   // 20 seconds
+#define AUTO_OFF_MILLIS      20000  // 20 seconds
+#define BOOT_SCREEN_MILLIS   4000   // 4 seconds
 
 // 'meshcore', 128x13px
 static const uint8_t meshcore_logo [] PROGMEM = {
@@ -20,10 +22,10 @@ static const uint8_t meshcore_logo [] PROGMEM = {
     0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8, 
 };
 
-void UITask::begin(const char* node_name, const char* build_date, const char* firmware_version) {
+void UITask::begin(NodePrefs* node_prefs, const char* build_date, const char* firmware_version) {
   _prevBtnState = HIGH;
   _auto_off = millis() + AUTO_OFF_MILLIS;
-  _node_name = node_name;
+  _node_prefs = node_prefs;
   _display->turnOn();
 
   // strip off dash and commit hash by changing dash to null terminator
@@ -39,18 +41,43 @@ void UITask::begin(const char* node_name, const char* build_date, const char* fi
 }
 
 void UITask::renderCurrScreen() {
-  // render 'home' screen
-  _display->drawXbm(0, 0, meshcore_logo, 128, 13);
-  _display->setCursor(0, 20);
-  _display->setTextSize(1);
-  _display->print(_node_name);
+  char tmp[80];
+  if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
+    // meshcore logo
+    _display->setColor(DisplayDriver::BLUE);
+    int logoWidth = 128;
+    _display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
 
-  _display->setCursor(0, 32);
-  _display->print(_version_info);
-  _display->setCursor(0, 43);
-  _display->print("< Repeater >");
-  //_display->printf("freq : %03.2f sf %d\n", _prefs.freq, _prefs.sf);
-  //_display->printf("bw   : %03.2f cr %d\n", _prefs.bw, _prefs.cr);
+    // version info
+    _display->setColor(DisplayDriver::LIGHT);
+    _display->setTextSize(1);
+    int versionWidth = strlen(_version_info) * 6;
+    _display->setCursor((_display->width() - versionWidth) / 2, 22);
+    _display->print(_version_info);
+
+    // node type
+    const char* node_type = "< Repeater >";
+    int nodeTypeWidth = strlen(node_type) * 6;
+    _display->setCursor((_display->width() - nodeTypeWidth) / 2, 35);
+    _display->print(node_type);
+  } else {  // home screen
+    // node name
+    _display->setCursor(0, 0);
+    _display->setTextSize(1);
+    _display->setColor(DisplayDriver::GREEN);
+    _display->print(_node_prefs->node_name);
+
+    // freq / sf
+    _display->setCursor(0, 20);
+    _display->setColor(DisplayDriver::YELLOW);
+    sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf);
+    _display->print(tmp);
+
+    // bw / cr
+    _display->setCursor(0, 30);
+    sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
+    _display->print(tmp);
+  }
 }
 
 void UITask::loop() {

+ 3 - 2
examples/simple_repeater/UITask.h

@@ -1,18 +1,19 @@
 #pragma once
 
 #include <helpers/ui/DisplayDriver.h>
+#include <helpers/CommonCLI.h>
 
 class UITask {
   DisplayDriver* _display;
   unsigned long _next_read, _next_refresh, _auto_off;
   int _prevBtnState;
-  const char* _node_name;
+  NodePrefs* _node_prefs;
   char _version_info[32];
 
   void renderCurrScreen();
 public:
   UITask(DisplayDriver& display) : _display(&display) { _next_read = _next_refresh = 0; }
-  void begin(const char* node_name, const char* build_date, const char* firmware_version);
+  void begin(NodePrefs* node_prefs, const char* build_date, const char* firmware_version);
 
   void loop();
 };

+ 4 - 1
examples/simple_repeater/main.cpp

@@ -516,6 +516,9 @@ public:
   const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
   const char* getRole() override { return FIRMWARE_ROLE; }
   const char* getNodeName() { return _prefs.node_name; }
+  NodePrefs* getNodePrefs() { 
+    return &_prefs; 
+  }
 
   void savePrefs() override {
     _cli.savePrefs(_fs);
@@ -658,7 +661,7 @@ void setup() {
   the_mesh.begin(fs);
 
 #ifdef DISPLAY_CLASS
-  ui_task.begin(the_mesh.getNodeName(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
+  ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
 #endif
 
   // send out initial Advertisement to the mesh

+ 41 - 14
examples/simple_room_server/UITask.cpp

@@ -1,7 +1,9 @@
 #include "UITask.h"
 #include <Arduino.h>
+#include <helpers/CommonCLI.h>
 
-#define AUTO_OFF_MILLIS   20000   // 20 seconds
+#define AUTO_OFF_MILLIS      20000  // 20 seconds
+#define BOOT_SCREEN_MILLIS   4000   // 4 seconds
 
 // 'meshcore', 128x13px
 static const uint8_t meshcore_logo [] PROGMEM = {
@@ -20,10 +22,10 @@ static const uint8_t meshcore_logo [] PROGMEM = {
     0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8, 
 };
 
-void UITask::begin(const char* node_name, const char* build_date, const char* firmware_version) {
+void UITask::begin(NodePrefs* node_prefs, const char* build_date, const char* firmware_version) {
   _prevBtnState = HIGH;
   _auto_off = millis() + AUTO_OFF_MILLIS;
-  _node_name = node_name;
+  _node_prefs = node_prefs;
   _display->turnOn();
 
   // strip off dash and commit hash by changing dash to null terminator
@@ -39,18 +41,43 @@ void UITask::begin(const char* node_name, const char* build_date, const char* fi
 }
 
 void UITask::renderCurrScreen() {
-  // render 'home' screen
-  _display->drawXbm(0, 0, meshcore_logo, 128, 13);
-  _display->setCursor(0, 20);
-  _display->setTextSize(1);
-  _display->print(_node_name);
+  char tmp[80];
+  if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
+    // meshcore logo
+    _display->setColor(DisplayDriver::BLUE);
+    int logoWidth = 128;
+    _display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13);
 
-  _display->setCursor(0, 32);
-  _display->print(_version_info);
-  _display->setCursor(0, 43);
-  _display->print("< Room Server >");
-  //_display->printf("freq : %03.2f sf %d\n", _prefs.freq, _prefs.sf);
-  //_display->printf("bw   : %03.2f cr %d\n", _prefs.bw, _prefs.cr);
+    // version info
+    _display->setColor(DisplayDriver::LIGHT);
+    _display->setTextSize(1);
+    int versionWidth = strlen(_version_info) * 6;
+    _display->setCursor((_display->width() - versionWidth) / 2, 22);
+    _display->print(_version_info);
+
+    // node type
+    const char* node_type = "< Room Server >";
+    int nodeTypeWidth = strlen(node_type) * 6;
+    _display->setCursor((_display->width() - nodeTypeWidth) / 2, 35);
+    _display->print(node_type);
+  } else {  // home screen
+    // node name
+    _display->setCursor(0, 0);
+    _display->setTextSize(1);
+    _display->setColor(DisplayDriver::GREEN);
+    _display->print(_node_prefs->node_name);
+
+    // freq / sf
+    _display->setCursor(0, 20);
+    _display->setColor(DisplayDriver::YELLOW);
+    sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf);
+    _display->print(tmp);
+
+    // bw / cr
+    _display->setCursor(0, 30);
+    sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr);
+    _display->print(tmp);
+  }
 }
 
 void UITask::loop() {

+ 3 - 2
examples/simple_room_server/UITask.h

@@ -1,18 +1,19 @@
 #pragma once
 
 #include <helpers/ui/DisplayDriver.h>
+#include <helpers/CommonCLI.h>
 
 class UITask {
   DisplayDriver* _display;
   unsigned long _next_read, _next_refresh, _auto_off;
   int _prevBtnState;
-  const char* _node_name;
+  NodePrefs* _node_prefs;
   char _version_info[32];
 
   void renderCurrScreen();
 public:
   UITask(DisplayDriver& display) : _display(&display) { _next_read = _next_refresh = 0; }
-  void begin(const char* node_name, const char* build_date, const char* firmware_version);
+  void begin(NodePrefs* node_prefs, const char* build_date, const char* firmware_version);
 
   void loop();
 };

+ 4 - 1
examples/simple_room_server/main.cpp

@@ -717,6 +717,9 @@ public:
   const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
   const char* getRole() override { return FIRMWARE_ROLE; }
   const char* getNodeName() { return _prefs.node_name; }
+  NodePrefs* getNodePrefs() { 
+    return &_prefs; 
+  }
 
   void savePrefs() override {
     _cli.savePrefs(_fs);
@@ -899,7 +902,7 @@ void setup() {
   the_mesh.begin(fs);
 
 #ifdef DISPLAY_CLASS
-  ui_task.begin(the_mesh.getNodeName(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
+  ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
 #endif
 
   // send out initial Advertisement to the mesh