Skip to content

Commit cc48585

Browse files
committed
Pull status bar into controller, support on ST79x
1 parent 0e67abb commit cc48585

File tree

9 files changed

+125
-62
lines changed

9 files changed

+125
-62
lines changed

src/Wippersnapper.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ bool cbSignalMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
423423
is_success = false;
424424
WS.pinCfgCompleted = false;
425425
}
426-
// If this is the initial configuration
426+
// Is this the initial configuration?
427427
if (!WS.pinCfgCompleted) {
428428
WS_DEBUG_PRINTLN("Initial Pin Configuration Complete!");
429429
WS.pinCfgCompleted = true;
@@ -520,10 +520,10 @@ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) {
520520
size_t msgSz;
521521
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields,
522522
msgi2cResponse);
523-
WS_DEBUG_PRINT("Publishing Message: I2CResponse...");
523+
WS_DEBUG_PRINTLN("Publishing Message: I2CResponse...");
524524
if (!WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing,
525-
msgSz, 1)) {
526-
WS_DEBUG_PRINTLN("ERROR: Failed to publish I2C Response!");
525+
msgSz, 0)) {
526+
WS_DEBUG_PRINTLN("\tERROR: Failed to publish I2C Response!");
527527
} else {
528528
WS_DEBUG_PRINTLN("Published!");
529529
}
@@ -1579,10 +1579,10 @@ bool cbDecodeDisplayMsg(pb_istream_t *stream, const pb_field_t *field,
15791579
size_t msgSz;
15801580
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_DisplayResponse_fields,
15811581
&msgResp);
1582-
WS_DEBUG_PRINT("Publishing DisplayResponse Message...");
1582+
WS_DEBUG_PRINTLN("Publishing DisplayResponse Message...");
15831583
if (!WS._mqtt->publish(WS._topic_signal_display_device, WS._buffer_outgoing,
1584-
msgSz, 1)) {
1585-
WS_DEBUG_PRINTLN("ERROR: Failed to DisplayResponse Response!");
1584+
msgSz, 0)) {
1585+
WS_DEBUG_PRINTLN("ERROR: Failed to Publish DisplayResponse!");
15861586
} else {
15871587
WS_DEBUG_PRINTLN("Published!");
15881588
}
@@ -2826,7 +2826,6 @@ void Wippersnapper::connect() {
28262826
WS._analogIO = new Wippersnapper_AnalogIO(5, 3.3);
28272827

28282828
// Configure hardware
2829-
// WS.pinCfgCompleted = true;
28302829
while (!WS.pinCfgCompleted) {
28312830
WS_DEBUG_PRINTLN(
28322831
"Polling for message containing hardware configuration...");
@@ -2873,8 +2872,12 @@ void Wippersnapper::publishPinConfigComplete() {
28732872

28742873
// Publish message
28752874
WS_DEBUG_PRINTLN("Publishing to pin config complete...");
2876-
WS.publish(WS._topic_device_pin_config_complete, _message_buffer,
2877-
_message_len, 1);
2875+
if (!WS._mqtt->publish(WS._topic_device_pin_config_complete, _message_buffer,
2876+
_message_len, 0)) {
2877+
WS_DEBUG_PRINTLN("Failed to publish pin config complete message!");
2878+
} else {
2879+
WS_DEBUG_PRINTLN("Published pin config complete message!");
2880+
}
28782881
}
28792882

28802883
/**************************************************************************/
@@ -2915,7 +2918,7 @@ ws_status_t Wippersnapper::run() {
29152918
WS.feedWDT();
29162919

29172920
// Process display controller events, if initialized
2918-
//WS._displayController->update(getRSSI(), networkStatus() == WS_CONNECTED);
2921+
WS._displayController->update(getRSSI(), networkStatus() == WS_CONNECTED);
29192922
WS.feedWDT();
29202923

29212924
return WS_NET_CONNECTED; // TODO: Make this funcn void!

src/Wippersnapper_demo.ino.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmpx8r34gur"
2+
#include <Arduino.h>
3+
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
4+
# 16 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
5+
#include "Wippersnapper_Networking.h"
6+
Wippersnapper_WiFi wipper;
7+
8+
9+
#define WS_DEBUG
10+
void setup();
11+
void loop();
12+
#line 22 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
13+
void setup() {
14+
15+
wipper.provision();
16+
17+
Serial.begin(115200);
18+
19+
20+
wipper.connect();
21+
22+
}
23+
24+
void loop() {
25+
wipper.run();
26+
}

src/components/display/controller.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
/*!
1818
@brief Constructs a new DisplayController object
1919
*/
20-
DisplayController::DisplayController() {}
20+
DisplayController::DisplayController() {
21+
_last_bar_update = 0;
22+
}
2123

2224
/*!
2325
@brief Destructor
@@ -77,6 +79,8 @@ bool DisplayController::Handle_Display_AddOrReplace(
7779

7880
_hw_instances.push_back(display); // Store the display instance
7981
WS_DEBUG_PRINTLN("[display] Display added or replaced successfully!");
82+
WS.feedWDT();
83+
WS.runNetFSM();
8084
return true;
8185
}
8286

@@ -128,19 +132,47 @@ bool DisplayController::Handle_Display_Write(
128132
WS_DEBUG_PRINT("[display] Writing message to display: ");
129133
WS_DEBUG_PRINTLN(msgWrite->message);
130134
display->writeMessage(msgWrite->message);
135+
WS.feedWDT();
136+
WS.runNetFSM();
131137
return true;
132138
}
133139

140+
/*!
141+
@brief Updates the status bar on all managed displays.
142+
@param rssi
143+
The current WiFi RSSI value.
144+
@param is_connected
145+
The current MQTT connection status.
146+
*/
134147
void DisplayController::update(int32_t rssi, bool is_connected) {
135148
// if _hw_instances is empty, early out
136149
if (_hw_instances.size() == 0)
137150
return;
138151

152+
// Only update the status bar every 60 seconds
153+
unsigned long now = millis();
154+
if (now - _last_bar_update < 60000)
155+
return;
156+
_last_bar_update = now;
157+
158+
// Early-out if nothing has changed
159+
// and only consider WiFi changes +/-3dB
160+
bool rssi_changed = abs(rssi - _statusbar_rssi) >= 3;
161+
if (!rssi_changed && is_connected == _statusbar_mqtt_connected && 100 == _statusbar_bat) {
162+
return;
163+
}
164+
139165
// Get the driver instance for the display
140-
DisplayHardware *display = nullptr;
141-
for (auto &hw_instance : _hw_instances) {
166+
for (DisplayHardware *hw_instance : _hw_instances) {
142167
// Note: For now, battery is always 100% as we don't have a way to read it
143168
// yet.
144169
hw_instance->updateStatusBar(rssi, 100, is_connected);
145170
}
171+
172+
// Update the cached status bar values
173+
_statusbar_rssi = rssi;
174+
_statusbar_mqtt_connected = is_connected;
175+
_statusbar_bat = 100; // NOTE: Always 100%, for now
176+
WS.feedWDT();
177+
WS.runNetFSM();
146178
}

src/components/display/controller.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class DisplayController {
4040
private:
4141
std::vector<DisplayHardware *>
4242
_hw_instances; ///< Holds pointers to DisplayHardware instances
43+
int8_t _statusbar_rssi; ///< RSSI value for status bar
44+
uint8_t _statusbar_bat; ///< Battery level, as a percentage, for the status bar
45+
bool _statusbar_mqtt_connected; ///< MQTT connection status for the status bar
46+
unsigned long _last_bar_update; ///< Timestamp of last status bar update
4347
};
4448
extern Wippersnapper Ws; ///< Global WS instance
4549
#endif

src/components/display/drivers/dispDrvBase.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ class dispDrvBase {
175175
int16_t _height; ///< Height of the display
176176
int16_t _width; ///< Width of the display
177177
uint8_t _rotation; ///< Rotation of the display
178-
int8_t _statusbar_rssi; ///< RSSI value for status bar
179-
uint8_t
180-
_statusbar_bat; ///< Battery level, as a percentage, for the status bar
181-
bool _statusbar_mqtt_connected; ///< MQTT connection status for the status bar
182178
};
183179

184180
#endif // WS_DISP_DRV_BASE_H

src/components/display/drivers/dispDrvSt7789.h

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ class dispDrvSt7789 : public dispDrvBase {
107107
if (_width == 240 && _height == 240) {
108108
_display->drawBitmap(0, 0, tft_bmp_logo_240240, 240, 240, ST77XX_WHITE);
109109
} else if (_width == 240 && _height == 135) {
110-
_display->drawBitmap(0, 0, tft_bmp_logo_240135, 240, 135, EPD_BLACK);
110+
_display->drawBitmap(0, 0, tft_bmp_logo_240135, 240, 135, ST77XX_BLACK);
111111
} else {
112112
// Unsupported resolution
113113
return;
114114
}
115115

116-
delay(1000);
116+
delay(500);
117117
}
118118

119119
/*!
@@ -149,7 +149,7 @@ class dispDrvSt7789 : public dispDrvBase {
149149

150150
// Draw username on left side of the status bar
151151
_display->setTextSize(1);
152-
_display->setTextColor(EPD_BLACK);
152+
_display->setTextColor(ST77XX_BLACK);
153153
_display->setCursor(5, 6);
154154
_display->print(io_username);
155155

@@ -162,11 +162,11 @@ class dispDrvSt7789 : public dispDrvBase {
162162
if (_height == 240) {
163163
// Draw 16px icons on right side of the status bar
164164
_display->drawBitmap(cloudX, iconY, epd_bmp_cloud_online,
165-
_status_bar_icon_sz, _status_bar_icon_sz, EPD_BLACK);
165+
_status_bar_icon_sz, _status_bar_icon_sz, ST77XX_BLACK);
166166
_display->drawBitmap(wifiX, iconY, epd_bmp_wifi_full, _status_bar_icon_sz,
167-
_status_bar_icon_sz, EPD_BLACK);
167+
_status_bar_icon_sz, ST77XX_BLACK);
168168
_display->drawBitmap(batteryX, iconY, epd_bmp_bat_full,
169-
_status_bar_icon_sz, _status_bar_icon_sz, EPD_BLACK);
169+
_status_bar_icon_sz, _status_bar_icon_sz, ST77XX_BLACK);
170170
} else if (_height == 135) {
171171
// TODO: Draw 12px icons on right side of the status bar
172172
} else {
@@ -189,7 +189,41 @@ class dispDrvSt7789 : public dispDrvBase {
189189
if (!_display)
190190
return;
191191

192-
// TODO - needs to be implemented!
192+
WS_DEBUG_PRINTLN("Updating ST7789 status bar");
193+
194+
int iconY = (_status_bar_height - _status_bar_icon_sz) / 2;
195+
int batteryX = _display->width() - _status_bar_icon_sz - _status_bar_icon_margin;
196+
int wifiX = batteryX - _status_bar_icon_sz - _status_bar_icon_spacing;
197+
int cloudX = wifiX - _status_bar_icon_sz - _status_bar_icon_spacing;
198+
199+
// Clear and draw the new cloud icon, based on MQTT connection status
200+
_display->fillRect(cloudX, iconY, _status_bar_icon_sz, _status_bar_icon_sz,
201+
ST77XX_WHITE);
202+
if (mqtt_status == 21) {
203+
_display->drawBitmap(cloudX, iconY, epd_bmp_cloud_online,
204+
_status_bar_icon_sz, _status_bar_icon_sz, ST77XX_BLACK);
205+
} else {
206+
_display->drawBitmap(cloudX, iconY, epd_bmp_cloud_offline,
207+
_status_bar_icon_sz, _status_bar_icon_sz, ST77XX_BLACK);
208+
}
209+
210+
// Update WiFi icon only if RSSI has changed significantly (+/-3dB)
211+
const unsigned char* wifi_icon = epd_bmp_wifi_no_signal;
212+
if (rssi >= -50) {
213+
wifi_icon = epd_bmp_wifi_full;
214+
} else if (rssi < -50 && rssi >= -60) {
215+
wifi_icon = epd_bmp_wifi_fair;
216+
} else if (rssi < -60 && rssi >= -70) {
217+
wifi_icon = epd_bmp_wifi_weak;
218+
} else if (rssi < -70 && rssi >= -80) {
219+
wifi_icon = epd_bmp_wifi_no_signal;
220+
} else {
221+
wifi_icon = epd_bmp_wifi_no_signal;
222+
}
223+
// Clear and draw the new WiFi icon, based on RSSI
224+
_display->fillRect(wifiX, iconY, _status_bar_icon_sz, _status_bar_icon_sz,
225+
ST77XX_WHITE);
226+
_display->drawBitmap(wifiX, iconY, wifi_icon, _status_bar_icon_sz, _status_bar_icon_sz, ST77XX_BLACK);
193227
}
194228

195229
/*!

src/components/display/drivers/dispDrvThinkInkGrayscale4Eaamfgn.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ class drvDispThinkInkGrayscale4Eaamfgn : public dispDrvBase {
152152
// For EPD - redraws take 1-2 seconds, so only update the cloud state
153153

154154
// Update cloud icon only if it changed state
155-
if (mqtt_status != _statusbar_mqtt_connected) {
156155
int iconSpacing = 4;
157156
int rightMargin = 5;
158157
int iconY =
@@ -173,12 +172,8 @@ class drvDispThinkInkGrayscale4Eaamfgn : public dispDrvBase {
173172
_display->drawBitmap(cloudX, iconY, epd_bmp_cloud_offline,
174173
STATUS_BAR_ICON_SZ, STATUS_BAR_ICON_SZ, EPD_BLACK);
175174
}
176-
_statusbar_mqtt_connected = mqtt_status;
177-
do_update = true;
178-
}
179175

180-
if (do_update)
181-
_display->display();
176+
_display->display();
182177
}
183178

184179
/*!

src/components/display/hardware.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -329,46 +329,16 @@ bool DisplayHardware::beginTft(
329329
miso = parsePin(spi_config->pin_miso);
330330
}
331331

332-
// Configure text size based on suffix in driver name
332+
// TODO: Implement Text_size based on future Protobuf that includes it
333333
uint8_t text_sz; // Default text size
334-
if (strstr(_name, "-lg") != nullptr) {
335-
// Larger text size for displays with -lg suffix
336-
text_sz = 4;
337-
removeSuffix("-lg");
338-
} else if (strstr(_name, "-md") != nullptr) {
339-
// Larger text size for displays with -md suffix
340-
text_sz = 3;
341-
removeSuffix("-md");
342-
} else {
343-
text_sz = 1;
344-
}
345334

346335
// Create display driver object using the factory function
347-
WS_DEBUG_PRINTLN("[display] Creating TFT display driver with pinout: ");
348-
WS_DEBUG_PRINT(" CS: D"); WS_DEBUG_PRINTLN(cs);
349-
WS_DEBUG_PRINT(" DC: D"); WS_DEBUG_PRINTLN(dc);
350-
WS_DEBUG_PRINT(" MOSI: D");WS_DEBUG_PRINTLN(mosi);
351-
WS_DEBUG_PRINT(" SCK: D"); WS_DEBUG_PRINTLN(sck);
352-
if (rst != -1) {
353-
WS_DEBUG_PRINT(" RST: D"); WS_DEBUG_PRINTLN(rst);
354-
}
355-
if (miso != -1) {
356-
WS_DEBUG_PRINT(" MISO: D"); WS_DEBUG_PRINTLN(miso);
357-
}
358336
_drvDisp = CreateDrvDispTft(_name, cs, dc, mosi, sck, rst, miso);
359337
if (!_drvDisp) {
360338
WS_DEBUG_PRINTLN("[display] Failed to create display driver!");
361339
return false;
362340
}
363341

364-
// Print configuration
365-
WS_DEBUG_PRINTLN("[display] Successfully created tft display driver!");
366-
WS_DEBUG_PRINTLN("[display] TFT configuration:");
367-
WS_DEBUG_PRINT(" Width: "); WS_DEBUG_PRINTLN(config->width);
368-
WS_DEBUG_PRINT(" Height: ");WS_DEBUG_PRINTLN(config->height);
369-
WS_DEBUG_PRINT(" Rotation: ");WS_DEBUG_PRINTLN(config->rotation);
370-
WS_DEBUG_PRINT(" Text Size: ");WS_DEBUG_PRINTLN(text_sz);
371-
372342
_drvDisp->setWidth(config->width);
373343
_drvDisp->setHeight(config->height);
374344
_drvDisp->setRotation(config->rotation);

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(
103103
_portNum = msgInitRequest->i2c_port_number;
104104
_busStatusResponse = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS;
105105
}
106+
107+
WS.runNetFSM();
106108
}
107109

108110
/*************************************************************/
@@ -1067,6 +1069,7 @@ void WipperSnapper_Component_I2C::updateI2CDeviceProperties(
10671069

10681070
// set response OK
10691071
_busStatusResponse = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS;
1072+
WS.runNetFSM();
10701073
}
10711074

10721075
/*******************************************************************************/

0 commit comments

Comments
 (0)