Adafruit Library
dispDrvSt7789.h
Go to the documentation of this file.
1 
15 #ifndef WS_DISP_DRV_ST7789
16 #define WS_DISP_DRV_ST7789
17 
18 #include "dispDrvBase.h"
19 #include <Adafruit_ST7789.h>
20 
21 #define ST7789_STATUSBAR_HEIGHT 20
22 #define ST7789_STATUSBAR_ICON_SZ 16
23 #define ST7789_STATUSBAR_ICON_SPACING \
24  4
25 #define ST7789_STATUSBAR_ICON_MARGIN \
26  5
27 
28 
31 class dispDrvSt7789 : public dispDrvBase {
32 public:
48  dispDrvSt7789(int16_t cs, int16_t dc, int16_t mosi, int16_t sck,
49  int16_t rst = -1, int16_t miso = -1)
50  : dispDrvBase(cs, dc, mosi, sck, rst, miso), _display(nullptr) {}
51 
56  if (_display) {
57  // "Clear" the display before deleting
58  _display->fillScreen(ST77XX_BLACK);
59  delete _display;
60  _display = nullptr;
61  }
62 // Turn off backlight
63 #if defined(ARDUINO_FUNHOUSE_ESP32S2)
64  digitalWrite(TFT_BACKLIGHT, LOW);
65 #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S3_REVTFT) || \
66  defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S3_TFT) || \
67  defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT) || \
68  defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT)
69  digitalWrite(TFT_BACKLITE, LOW);
70 #endif
71  }
72 
77  bool begin() override {
78 
79  _display = new Adafruit_ST7789(_pin_cs, _pin_dc, _pin_rst);
80  if (!_display)
81  return false;
82 
83  _display->init(_width, _height);
84  _display->setRotation(_rotation);
85  _display->fillScreen(ST77XX_BLACK);
86  _display->setTextColor(ST77XX_WHITE);
87  _display->setTextWrap(false);
88 
89  // Turn on backlight
90 #if defined(ARDUINO_FUNHOUSE_ESP32S2)
91  pinMode(TFT_BACKLIGHT, OUTPUT);
92  digitalWrite(TFT_BACKLIGHT, HIGH);
93 #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S3_REVTFT) || \
94  defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S3_TFT) || \
95  defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT) || \
96  defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT)
97  pinMode(TFT_BACKLITE, OUTPUT);
98  digitalWrite(TFT_BACKLITE, HIGH);
99 #endif
100 
101  return true;
102  }
103 
107  void showSplash() override {
108  if (!_display)
109  return;
110 
111  // Display the appropriate splash screen based on resolution
112  if (_width == 240 && _height == 240) {
113  _display->drawBitmap(0, 0, tft_bmp_logo_240240, 240, 240, ST77XX_WHITE);
114  } else if (_width == 135 && _height == 240) {
115  _display->drawBitmap(0, 0, tft_bmp_logo_240135, 240, 135, ST77XX_WHITE);
116  } else {
117  // Unsupported resolution detected, skip splash screen
118  return;
119  }
120 
121  delay(500);
122  }
123 
129  virtual void drawStatusBar(const char *io_username) override {
130  if (!_display)
131  return;
132 
133  // Clear the entire display buffer to remove splash screen
134  _display->fillScreen(ST77XX_BLACK);
135 
136  // Draw status bar
137  _display->fillRect(0, 0, _display->width(), ST7789_STATUSBAR_HEIGHT,
138  ST77XX_WHITE);
139 
140  // Draw username on left side of the status bar
141  _display->setTextSize(1);
142  _display->setTextColor(ST77XX_BLACK);
143  _display->setCursor(5, 6);
144  _display->print(io_username);
145 
146  // Calculate icon positions (rightmost side of status bar), center
147  // vertically
158  // Draw icons
159  _display->drawBitmap(_statusbar_icon_cloud_x, _statusbar_icons_y,
161  ST7789_STATUSBAR_ICON_SZ, ST77XX_BLACK);
162  _display->drawBitmap(_statusbar_icon_wifi_x, _statusbar_icons_y,
164  ST7789_STATUSBAR_ICON_SZ, ST77XX_BLACK);
165  _display->drawBitmap(_statusbar_icon_battery_x, _statusbar_icons_y,
167  ST7789_STATUSBAR_ICON_SZ, ST77XX_BLACK);
168 
169  // Reset text color and size for main text area
170  _display->setTextColor(ST77XX_WHITE);
171  _display->setTextSize(_text_sz);
172  }
173 
184  void updateStatusBar(int8_t rssi, uint8_t bat, bool mqtt_status) override {
185  if (!_display)
186  return;
187 
188  // Only update wifi icon if the RSSI has changed significantly (+/-3dB)
189  bool update_rssi = abs(rssi - _statusbar_rssi) >= 3;
190  // Only update cloud icon if MQTT status has changed
191  bool update_mqtt = mqtt_status != _statusbar_mqtt_connected;
192 
193  // No need to update if nothing has changed
194  if (!update_rssi && !update_mqtt)
195  return;
196 
197  if (update_mqtt) {
198  // Clear and draw the new cloud icon, based on MQTT connection status
199  _display->fillRect(_statusbar_icon_cloud_x, _statusbar_icons_y,
201  ST77XX_WHITE);
202  if (mqtt_status) {
203  _display->drawBitmap(_statusbar_icon_cloud_x, _statusbar_icons_y,
205  ST7789_STATUSBAR_ICON_SZ, ST77XX_BLACK);
206  } else {
207  _display->drawBitmap(_statusbar_icon_cloud_x, _statusbar_icons_y,
208  epd_bmp_cloud_offline, ST7789_STATUSBAR_ICON_SZ,
209  ST7789_STATUSBAR_ICON_SZ, ST77XX_BLACK);
210  }
211  _statusbar_mqtt_connected = mqtt_status;
212  }
213 
214  // Update WiFi icon only if RSSI has changed significantly (+/-3dB)
215  if (update_rssi) {
216  const unsigned char *wifi_icon = epd_bmp_wifi_no_signal;
217  if (rssi >= -50) {
218  wifi_icon = epd_bmp_wifi_full;
219  } else if (rssi < -50 && rssi >= -60) {
220  wifi_icon = epd_bmp_wifi_fair;
221  } else if (rssi < -60 && rssi >= -70) {
222  wifi_icon = epd_bmp_wifi_weak;
223  } else if (rssi < -70 && rssi >= -80) {
224  wifi_icon = epd_bmp_wifi_no_signal;
225  } else {
226  wifi_icon = epd_bmp_wifi_no_signal;
227  }
228  // Clear and draw the new WiFi icon, based on RSSI
229  _display->fillRect(_statusbar_icon_wifi_x, _statusbar_icons_y,
231  ST77XX_WHITE);
232  _display->drawBitmap(_statusbar_icon_wifi_x, _statusbar_icons_y,
233  wifi_icon, ST7789_STATUSBAR_ICON_SZ,
234  ST7789_STATUSBAR_ICON_SZ, ST77XX_BLACK);
235  _statusbar_rssi = rssi;
236  }
237  }
238 
246  virtual void writeMessage(const char *message) override {
247  if (_display == nullptr)
248  return;
249 
250  // Clear only the area below the status bar
251  _display->fillRect(0, ST7789_STATUSBAR_HEIGHT, _display->width(),
252  _display->height() - ST7789_STATUSBAR_HEIGHT,
253  ST77XX_BLACK);
254  int16_t y_idx = ST7789_STATUSBAR_HEIGHT;
255 
256  // Calculate the line height based on the text size (NOTE: base height is
257  // 8px)
258  int16_t line_height = 8 * _text_sz;
259  uint16_t c_idx = 0;
260  size_t msg_size = strlen(message);
261  // Begin with a small offset from status bar
262  y_idx += 5;
263  _display->setCursor(0, y_idx);
264  for (size_t i = 0; i < msg_size && c_idx < msg_size; i++) {
265  if (y_idx + line_height > _height)
266  break;
267  if (message[i] == '\\' && i + 1 < msg_size &&
268  (message[i + 1] == 'n' || message[i + 1] == 'r')) {
269  // Handle \r\n sequence as a single newline
270  if (message[i + 1] == 'r' && i + 3 < msg_size &&
271  message[i + 2] == '\\' && message[i + 3] == 'n') {
272  // Skip to the next line
273  if (y_idx + line_height > _height)
274  break;
275  y_idx += line_height;
276  _display->setCursor(0, y_idx);
277  i += 3;
278  } else if (message[i + 1] == 'n') {
279  // Skip to the next line
280  if (y_idx + line_height > _height)
281  break;
282  y_idx += line_height;
283  _display->setCursor(0, y_idx);
284  i++;
285  }
286  } else if (message[i] == 0xC2 && message[i + 1] == 0xB0) {
287  // Degree symbol - tested on Feather RevTFT S3
288  _display->write(char(247));
289  i++;
290  } else {
291  _display->print(message[i]);
292  }
293  }
294  }
295 
296 private:
297  Adafruit_ST7789 *_display;
298 };
299 
300 #endif // WS_DISP_DRV_ST7789
void updateStatusBar(int8_t rssi, uint8_t bat, bool mqtt_status) override
Updates the status bar with current information (battery level, connectivity status, etc).
Definition: dispDrvSt7789.h:184
const unsigned char epd_bmp_bat_full[]
battery-full-thin-full icon from FontAwesome (16x16px)
Definition: icons.h:70
int _statusbar_icons_y
Y position of status bar icons.
Definition: dispDrvBase.h:178
const unsigned char epd_bmp_wifi_fair[]
wifi-fair-thin-full icon from FontAwesome (16x16px)
Definition: icons.h:46
int _statusbar_icon_battery_x
X position of battery icon.
Definition: dispDrvBase.h:179
Driver for ST7789-based TFT displays.
Definition: dispDrvSt7789.h:31
const unsigned char epd_bmp_wifi_no_signal[]
wifi-slash-thin-full icon from FontAwesome (16x16px)
Definition: icons.h:62
#define ST7789_STATUSBAR_ICON_MARGIN
Default margin from edge of display to status bar icons.
Definition: dispDrvSt7789.h:25
uint8_t _text_sz
Text size for displaying a message.
Definition: dispDrvBase.h:173
#define ST7789_STATUSBAR_HEIGHT
Default status bar height.
Definition: dispDrvSt7789.h:21
dispDrvSt7789(int16_t cs, int16_t dc, int16_t mosi, int16_t sck, int16_t rst=-1, int16_t miso=-1)
Constructor for the ST7789 display driver.
Definition: dispDrvSt7789.h:48
int8_t _statusbar_rssi
RSSI value for status bar.
Definition: dispDrvBase.h:182
#define ST7789_STATUSBAR_ICON_SZ
Default status bar icon size.
Definition: dispDrvSt7789.h:22
#define ST7789_STATUSBAR_ICON_SPACING
Default spacing between status bar icons.
Definition: dispDrvSt7789.h:23
uint8_t _rotation
Rotation of the display.
Definition: dispDrvBase.h:176
bool begin() override
Attempts to initialize the ST7789 TFT driver.
Definition: dispDrvSt7789.h:77
int _statusbar_icon_cloud_x
X position of cloud icon.
Definition: dispDrvBase.h:181
int16_t _pin_rst
Reset pin.
Definition: dispDrvBase.h:166
void showSplash() override
Displays the splash screen on the display.
Definition: dispDrvSt7789.h:107
int16_t _width
Width of the display.
Definition: dispDrvBase.h:175
const unsigned char epd_bmp_wifi_full[]
wifi-thin-full icon from FontAwesome (16x16px)
Definition: icons.h:38
const unsigned char tft_bmp_logo_240135[]
Wippersnapper logo, 296x128px.
Definition: splash.h:22
virtual void writeMessage(const char *message) override
Writes a message to the display.
Definition: dispDrvSt7789.h:246
const unsigned char epd_bmp_cloud_online[]
cloud-thin-full icon from FontAwesome (16x16px)
Definition: icons.h:22
int16_t _height
Height of the display.
Definition: dispDrvBase.h:174
bool _statusbar_mqtt_connected
MQTT connection status for the status bar.
Definition: dispDrvBase.h:185
Abstract base class for display drivers. This class provides a common interface for all display drive...
Definition: dispDrvBase.h:28
~dispDrvSt7789()
Destructor for the ST7789 display driver.
Definition: dispDrvSt7789.h:55
int16_t _pin_cs
Chip Select pin.
Definition: dispDrvBase.h:167
virtual void drawStatusBar(const char *io_username) override
Draws a status bar at the top of the display.
Definition: dispDrvSt7789.h:129
const unsigned char epd_bmp_wifi_weak[]
wifi-weak-thin-full icon from FontAwesome (16x16px)
Definition: icons.h:54
int16_t _pin_dc
Data/Command pin.
Definition: dispDrvBase.h:165
int _statusbar_icon_wifi_x
X position of WiFi icon.
Definition: dispDrvBase.h:180