Skip to content

Commit 5a06e1a

Browse files
authored
feat: statically defining LCD classes, removing post-init checks (#21)
* Added simulator for web server * added some GIF stuff. other improvments * Added comments * Removed LCD classes from heap, now they are statically defined * Removed file that was from another commit...oops * removed uneeded get functions for system-level defines removed yield functions inside of init, not needed * removed unused config parameter * Removed not defined functions from removal * Turns out CS is just tied low in HW, so no need for any special bus handler. Removed tft library's init as we are doing our own init anyways * Made clang-tidy happy * Rewording on readme Thanks to https://github.com/Electro707
1 parent 8d258ba commit 5a06e1a

10 files changed

Lines changed: 96 additions & 565 deletions

File tree

include/config/ConfigManager.h

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,17 @@
2424
#include "config/SecureStorage.h"
2525
#include <string>
2626
#include <cstdint>
27+
#include <SPI.h>
2728

2829
// LCD configuration defaults for hellocubic lite
29-
static constexpr bool LCD_ENABLE = true;
3030
static constexpr int16_t LCD_W = 240;
3131
static constexpr int16_t LCD_H = 240;
3232
static constexpr uint8_t LCD_ROTATION = 4;
3333
static constexpr int8_t LCD_MOSI_GPIO = 13;
3434
static constexpr int8_t LCD_SCK_GPIO = 14;
35-
static constexpr int8_t LCD_CS_GPIO = 2;
3635
static constexpr int8_t LCD_DC_GPIO = 0;
37-
static constexpr int8_t LCD_RST_GPIO = 15;
38-
static constexpr bool LCD_CS_ACTIVE_HIGH = true;
39-
static constexpr bool LCD_DC_CMD_HIGH = false;
40-
static constexpr uint8_t LCD_SPI_MODE = 0;
36+
static constexpr int8_t LCD_RST_GPIO = 2;
37+
static constexpr uint8_t LCD_SPI_MODE = SPI_MODE3;
4138
static constexpr uint32_t LCD_SPI_HZ = 40000000;
4239
static constexpr int8_t LCD_BACKLIGHT_GPIO = 5;
4340
static constexpr bool LCD_BACKLIGHT_ACTIVE_LOW = true;
@@ -50,63 +47,17 @@ class ConfigManager {
5047
void setWiFi(const char* newSsid, const char* newPassword);
5148
const char* getSSID() const;
5249
const char* getPassword() const;
53-
bool getLCDEnable() const;
54-
int16_t getLCDWidth() const;
55-
int16_t getLCDHeight() const;
5650
uint8_t getLCDRotation() const;
57-
int8_t getLCDMosiGpio() const;
58-
int8_t getLCDSckGpio() const;
59-
int8_t getLCDCsGpio() const;
60-
int8_t getLCDDcGpio() const;
61-
int8_t getLCDRstGpio() const;
62-
bool getLCDCsActiveHigh() const;
63-
bool getLCDDcCmdHigh() const;
64-
uint8_t getLCDSpiMode() const;
65-
bool getLCDKeepCsAsserted() const;
6651
uint32_t getLCDSpiHz() const;
67-
int8_t getLCDBacklightGpio() const;
68-
bool getLCDBacklightActiveLow() const;
6952
bool migrateWiFiToSecureStorage(String ssid, String password);
7053

7154
public:
72-
bool getLCDEnableSafe() const { return lcd_enable; }
73-
int16_t getLCDWidthSafe() const { return (lcd_w > 0) ? lcd_w : LCD_W; }
74-
int16_t getLCDHeightSafe() const { return (lcd_h > 0) ? lcd_h : LCD_H; }
7555
uint8_t getLCDRotationSafe() const { return lcd_rotation; }
76-
int8_t getLCDMosiGpioSafe() const { return (lcd_mosi_gpio >= 0) ? lcd_mosi_gpio : LCD_MOSI_GPIO; }
77-
int8_t getLCDSckGpioSafe() const { return (lcd_sck_gpio >= 0) ? lcd_sck_gpio : LCD_SCK_GPIO; }
78-
int8_t getLCDCsGpioSafe() const { return (lcd_cs_gpio >= 0) ? lcd_cs_gpio : LCD_CS_GPIO; }
79-
int8_t getLCDDcGpioSafe() const { return (lcd_dc_gpio >= 0) ? lcd_dc_gpio : LCD_DC_GPIO; }
80-
int8_t getLCDRstGpioSafe() const { return (lcd_rst_gpio >= 0) ? lcd_rst_gpio : LCD_RST_GPIO; }
81-
bool getLCDCsActiveHighSafe() const { return lcd_cs_active_high; }
82-
bool getLCDDcCmdHighSafe() const { return lcd_dc_cmd_high; }
83-
uint8_t getLCDSpiModeSafe() const { return lcd_spi_mode; }
84-
bool getLCDKeepCsAssertedSafe() const { return lcd_keep_cs_asserted; }
85-
uint32_t getLCDSpiHzSafe() const { return (lcd_spi_hz > 0) ? lcd_spi_hz : LCD_SPI_HZ; }
86-
int8_t getLCDBacklightGpioSafe() const {
87-
return (lcd_backlight_gpio >= 0) ? lcd_backlight_gpio : LCD_BACKLIGHT_GPIO;
88-
}
89-
bool getLCDBacklightActiveLowSafe() const { return lcd_backlight_active_low; }
9056
std::string ssid;
9157
std::string password;
9258
std::string filename;
9359
SecureStorage secure;
94-
bool lcd_enable = true;
95-
int16_t lcd_w = 240;
96-
int16_t lcd_h = 240;
9760
uint8_t lcd_rotation = 4;
98-
int8_t lcd_mosi_gpio = 13;
99-
int8_t lcd_sck_gpio = 14;
100-
int8_t lcd_cs_gpio = 2;
101-
int8_t lcd_dc_gpio = 0;
102-
int8_t lcd_rst_gpio = 15;
103-
bool lcd_cs_active_high = true;
104-
bool lcd_dc_cmd_high = false;
105-
uint8_t lcd_spi_mode = 0;
106-
bool lcd_keep_cs_asserted = true;
107-
uint32_t lcd_spi_hz = 40000000;
108-
int8_t lcd_backlight_gpio = 5;
109-
bool lcd_backlight_active_low = true;
11061
};
11162

11263
#endif // CONFIG_MANAGER_H

include/display/DisplayManager.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ static constexpr int THREE_LINES_SPACE = 60;
3636
class DisplayManager {
3737
public:
3838
static void begin();
39-
static bool isReady();
40-
static void ensureInit();
4139
static Arduino_GFX* getGfx();
4240
static void drawStartup(String currentIP);
4341
static void drawTextWrapped(int16_t xPos, int16_t yPos, const String& text, uint8_t textSize, uint16_t fgColor,

include/display/GeekMagicSPIBus.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

readme.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
- **Resolution**: 240x240 pixels
7171
- **Color Format**: RGB565 (16-bit color)
7272
- **Interface**: SPI (Serial Peripheral Interface)
73-
- **SPI Speed**: up to 80 MHz (40 MHz is more stable)
73+
- **SPI Speed**: 40 MHz (80 MHz is possible, but unstable and outside datasheet spec)
7474
- **Rotation**: Upside-down for cube display, normal for the small tv
7575

7676
### Pin wiring
@@ -83,9 +83,9 @@ The display is connected to the ESP8266 using the following GPIO pins:
8383
| ------------- | -------- | ----------------------------------------------------- |
8484
| **MOSI** | GPIO 13 | SPI Master Out Slave In (data from ESP8266 to screen) |
8585
| **SCK** | GPIO 14 | SPI Clock |
86-
| **CS** | GPIO 2 | Chip Select (Active HIGH) |
86+
| **CS** | GND | Chip Select, tied permanently to GND |
8787
| **DC** | GPIO 0 | Data/Command select (LOW=command, HIGH=data) |
88-
| **RST** | GPIO 15 | Reset pin |
88+
| **RST** | GPIO 2 | Reset pin |
8989
| **Backlight** | GPIO 5 | Backlight control (Active LOW) |
9090

9191
<div align="center">
@@ -96,9 +96,9 @@ The display is connected to the ESP8266 using the following GPIO pins:
9696

9797
### Important configuration details
9898

99-
**Chip select (CS) polarity**: This board uses **active-high** CS, which is non-standard. Most SPI displays use active-low CS. The CS pin must be driven HIGH to select the display
99+
**Chip select (CS) polarity**: This board ties CS of the display permanently to GND.
100100

101-
**SPI mode**: SPI Mode 0 (CPOL=0, CPHA=0)
101+
**SPI mode**: SPI Mode 3 (CPOL=1, CPHA=1)
102102

103103
**Data/command pin**: LOW for commands, HIGH for data
104104

@@ -109,8 +109,8 @@ The display is connected to the ESP8266 using the following GPIO pins:
109109
### Initialization sequence
110110

111111
1. **Backlight control**: GPIO 5 is set as output and driven LOW to turn on the backlight
112-
2. **Hardware reset**: The RST pin (GPIO 15) is toggled to reset the ST7789 controller
113-
3. **SPI bus setup**: Hardware SPI is initialized with 80 MHz clock speed and Mode 0
112+
2. **Hardware reset**: The RST pin (GPIO 2) is toggled to reset the ST7789 controller
113+
3. **SPI bus setup**: Hardware SPI is initialized with 40 MHz clock speed and Mode 3
114114
4. **Display controller init**: The ST7789 is configured using a vendor-specific initialization sequence that includes:
115115
- Sleep out (0x11)
116116
- Porch settings (0xB2)
@@ -128,19 +128,15 @@ The display is connected to the ESP8266 using the following GPIO pins:
128128

129129
The display uses **SPI** protocol for communication:
130130

131-
1. **Chip select**: CS is driven HIGH to select the display
132-
2. **Command/data mode**: The DC pin indicates whether the data on MOSI is a command (DC=LOW) or pixel data (DC=HIGH)
133-
3. **Data transfer**: Data is shifted out on the MOSI pin, synchronized with the SCK clock signal
134-
4. **Chip deselect**: CS can be kept HIGH during continuous operations for better performance, or dropped LOW between operations
131+
1. **Command/data mode**: The DC pin indicates whether the data on MOSI is a command (DC=LOW) or pixel data (DC=HIGH)
132+
2. **Data transfer**: Data is shifted out on the MOSI pin, synchronized with the SCK clock signal
135133

136134
### Drawing to the screen
137135

138136
The firmware uses the Arduino_GFX library with a custom ESP8266SPIWithCustomCS bus driver that handles the active-high CS polarity. Graphics operations:
139137

140-
1. **Begin write**: Assert CS (set HIGH)
141-
2. **Write commands**: Set DC LOW, send command bytes via SPI
142-
3. **Write data**: Set DC HIGH, send pixel data via SPI
143-
4. **End write**: Optionally deassert CS (set LOW) - can be kept asserted for continuous operations
138+
1. **Write commands**: Set DC LOW, send command bytes via SPI
139+
2. **Write data**: Set DC HIGH, send pixel data via SPI
144140

145141
### Color format
146142

@@ -160,8 +156,7 @@ Example colors:
160156

161157
### Performance optimizations
162158

163-
- **High SPI speed**: 80 MHz clock for fast data transfer
164-
- **CS kept asserted**: During continuous operations, CS stays HIGH to reduce overhead
159+
- **High SPI speed**: 40 MHz clock for fast data transfer
165160
- **Hardware SPI**: Uses ESP8266's hardware SPI peripheral for efficient transfers
166161
- **Batch writes**: Multiple operations are batched between beginWrite/endWrite calls
167162
- **Direct frame buffer writes**: GIF frames are streamed directly to avoid intermediate buffering

src/config/ConfigManager.cpp

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,7 @@ auto ConfigManager::load() -> bool {
6565
String ssid = doc["wifi_ssid"] | "";
6666
String password = doc["wifi_password"] | "";
6767

68-
this->lcd_enable = doc["lcd_enable"] | lcd_enable;
69-
this->lcd_w = doc["lcd_w"] | lcd_w;
70-
this->lcd_h = doc["lcd_h"] | lcd_h;
7168
this->lcd_rotation = doc["lcd_rotation"] | lcd_rotation;
72-
this->lcd_mosi_gpio = doc["lcd_mosi_gpio"] | lcd_mosi_gpio;
73-
this->lcd_sck_gpio = doc["lcd_sck_gpio"] | lcd_sck_gpio;
74-
this->lcd_cs_gpio = doc["lcd_cs_gpio"] | lcd_cs_gpio;
75-
this->lcd_dc_gpio = doc["lcd_dc_gpio"] | lcd_dc_gpio;
76-
this->lcd_rst_gpio = doc["lcd_rst_gpio"] | lcd_rst_gpio;
77-
this->lcd_cs_active_high = doc["lcd_cs_active_high"] | lcd_cs_active_high;
78-
this->lcd_dc_cmd_high = doc["lcd_dc_cmd_high"] | lcd_dc_cmd_high;
79-
this->lcd_spi_mode = doc["lcd_spi_mode"] | lcd_spi_mode;
80-
this->lcd_keep_cs_asserted = doc["lcd_keep_cs_asserted"] | lcd_keep_cs_asserted;
81-
this->lcd_spi_hz = doc["lcd_spi_hz"] | lcd_spi_hz;
82-
this->lcd_backlight_gpio = doc["lcd_backlight_gpio"] | lcd_backlight_gpio;
83-
this->lcd_backlight_active_low = doc["lcd_backlight_active_low"] | lcd_backlight_active_low;
8469

8570
String nvs_ssid = secure.get("wifi_ssid", "");
8671
String nvs_password = secure.get("wifi_password", "");
@@ -118,115 +103,13 @@ auto ConfigManager::getSSID() const -> const char* { return ssid.c_str(); }
118103
*/
119104
auto ConfigManager::getPassword() const -> const char* { return password.c_str(); }
120105

121-
/**
122-
* @brief Returns the current status of the LCD enable flag
123-
*
124-
* @return true if the LCD is enabled false otherwise
125-
*/
126-
auto ConfigManager::getLCDEnable() const -> bool { return lcd_enable; }
127-
128-
/**
129-
* @brief Retrieves the LCD width
130-
*
131-
* @return The width of the LCD in pixels
132-
*/
133-
auto ConfigManager::getLCDWidth() const -> int16_t { return lcd_w; }
134-
/**
135-
* @brief Retrieves the LCD height
136-
*
137-
* @return The height of the LCD in pixels
138-
*/
139-
auto ConfigManager::getLCDHeight() const -> int16_t { return lcd_h; }
140106
/**
141107
* @brief Retrieves the LCD rotation setting
142108
*
143109
* @return The rotation of the LCD
144110
*/
145111
auto ConfigManager::getLCDRotation() const -> uint8_t { return lcd_rotation; }
146112

147-
/**
148-
* @brief Retrieves the GPIO pin number for LCD MOSI
149-
*
150-
* @return The GPIO pin number for LCD MOSI
151-
*/
152-
auto ConfigManager::getLCDMosiGpio() const -> int8_t { return lcd_mosi_gpio; }
153-
154-
/**
155-
* @brief Retrieves the GPIO pin number for LCD SCK
156-
*
157-
* @return The GPIO pin number for LCD SCK
158-
*/
159-
auto ConfigManager::getLCDSckGpio() const -> int8_t { return lcd_sck_gpio; }
160-
161-
/**
162-
* @brief Retrieves the GPIO pin number for LCD CS
163-
*
164-
* @return The GPIO pin number for LCD CS
165-
*/
166-
auto ConfigManager::getLCDCsGpio() const -> int8_t { return lcd_cs_gpio; }
167-
168-
/**
169-
* @brief Retrieves the GPIO pin number for LCD DC
170-
*
171-
* @return The GPIO pin number for LCD DC
172-
*/
173-
auto ConfigManager::getLCDDcGpio() const -> int8_t { return lcd_dc_gpio; }
174-
175-
/**
176-
* @brief Retrieves the GPIO pin number for LCD RST
177-
*
178-
* @return The GPIO pin number for LCD RST
179-
*/
180-
auto ConfigManager::getLCDRstGpio() const -> int8_t { return lcd_rst_gpio; }
181-
182-
/**
183-
* @brief Returns whether the LCD CS pin is active high
184-
*
185-
* @return true if the LCD CS pin is active high false otherwise
186-
*/
187-
auto ConfigManager::getLCDCsActiveHigh() const -> bool { return lcd_cs_active_high; }
188-
/**
189-
* @brief Returns whether the LCD DC pin is command high
190-
*
191-
* @return true if the LCD DC pin is command high false otherwise
192-
*/
193-
auto ConfigManager::getLCDDcCmdHigh() const -> bool { return lcd_dc_cmd_high; }
194-
195-
/**
196-
* @brief Retrieves the LCD SPI mode
197-
*
198-
* @return The SPI mode of the LCD
199-
*/
200-
auto ConfigManager::getLCDSpiMode() const -> uint8_t { return lcd_spi_mode; }
201-
202-
/**
203-
* @brief Returns whether the LCD CS pin is kept asserted
204-
*
205-
* @return true if the LCD CS pin is kept asserted false otherwise
206-
*/
207-
auto ConfigManager::getLCDKeepCsAsserted() const -> bool { return lcd_keep_cs_asserted; }
208-
209-
/**
210-
* @brief Retrieves the SPI clock frequency for the LCD
211-
*
212-
* @return The SPI clock frequency in Hz
213-
*/
214-
auto ConfigManager::getLCDSpiHz() const -> uint32_t { return lcd_spi_hz; }
215-
216-
/**
217-
* @brief Retrieves the GPIO pin number for the LCD backlight
218-
*
219-
* @return The GPIO pin number for the LCD backlight
220-
*/
221-
auto ConfigManager::getLCDBacklightGpio() const -> int8_t { return lcd_backlight_gpio; }
222-
223-
/**
224-
* @brief Returns whether the LCD backlight pin is active low
225-
*
226-
* @return true if the LCD backlight pin is active low false otherwise
227-
*/
228-
auto ConfigManager::getLCDBacklightActiveLow() const -> bool { return lcd_backlight_active_low; }
229-
230113
/**
231114
* @brief Set WiFi credentials in memory
232115
* @param newSsid The SSID

0 commit comments

Comments
 (0)