Skip to content

Commit f06ac06

Browse files
committed
Implement begin and configure for a char lcd
1 parent 878d186 commit f06ac06

File tree

6 files changed

+111
-19
lines changed

6 files changed

+111
-19
lines changed

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ lib_deps =
9090
https://github.com/bblanchon/ArduinoStreamUtils.git
9191
https://github.com/Sensirion/arduino-i2c-scd4x.git
9292
https://github.com/adafruit/Adafruit_LED_Backpack.git
93+
https://github.com/adafruit/Adafruit_LiquidCrystal.git
9394

9495
; Common build environment for ESP32 platform
9596
[common:esp32]

src/components/i2c/controller.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ static const std::map<std::string, FnCreateI2cOutputDrv> I2cFactoryOutput = {
365365
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
366366
const char *driver_name) -> drvOutputBase * {
367367
return new drvOutQuadAlphaNum(i2c, addr, mux_channel, driver_name);
368+
}},
369+
{"charlcd",
370+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
371+
const char *driver_name) -> drvOutputBase * {
372+
return new drvOutCharLcd(i2c, addr, mux_channel, driver_name);
368373
}}}; ///< I2C output driver factory
369374

370375
/*!
@@ -822,14 +827,14 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
822827

823828
// Determine which driver cb function to use
824829
if (_i2c_model->GetI2cDeviceOutputWriteMsg()->has_led_backpack_write) {
825-
WS_DEBUG_PRINTLN("[i2c] Writing message to LED backpack...");
826830
if (!driver->LedBackpackWrite(
827831
&_i2c_model->GetI2cDeviceOutputWriteMsg()->led_backpack_write)) {
828832
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to write to LED backpack!");
829833
return false;
830834
}
831835
} else if (_i2c_model->GetI2cDeviceOutputWriteMsg()->has_char_lcd_write) {
832836
WS_DEBUG_PRINTLN("[i2c] Char LCD write not implemented yet!");
837+
// TODO!
833838
} else {
834839
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to determine I2C Output Write type!");
835840
return false;
@@ -1053,7 +1058,6 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
10531058
_i2c_drivers_output.push_back(drv_out);
10541059
}
10551060

1056-
10571061
WS_DEBUG_PRINTLN("[i2c] Driver initialized and added to controller: ");
10581062
WS_DEBUG_PRINTLN(device_name);
10591063

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "drivers/drvMprls.h"
4646
#include "drivers/drvMs8607.h"
4747
#include "drivers/drvNau7802.h"
48+
#include "drivers/drvOutCharLcd.h"
4849
#include "drivers/drvOutQuadAlphaNum.h"
4950
#include "drivers/drvOutputBase.h" ///< Base i2c output driver class
5051
#include "drivers/drvPct2075.h"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*!
2+
* @file drvOutCharLcd.h
3+
*
4+
* Device driver for I2C Character LCDs (HD44780)
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell for Adafruit Industries 2025
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#ifndef DRV_OUT_CHAR_LCD
17+
#define DRV_OUT_CHAR_LCD
18+
19+
#include "drvOutputBase.h"
20+
#include <Adafruit_LiquidCrystal.h>
21+
#include <Arduino.h>
22+
23+
/*!
24+
@brief Class that provides a driver interface for a lcd character display.
25+
This class is a wrapper around the Adafruit_LiquidCrystal library.
26+
*/
27+
class drvOutCharLcd : public drvOutputBase {
28+
public:
29+
/*!
30+
@brief Constructor for a lcd character display.
31+
@param i2c
32+
The I2C interface.
33+
@param sensorAddress
34+
7-bit device address.
35+
@param mux_channel
36+
The I2C multiplexer channel.
37+
@param driver_name
38+
The name of the driver.
39+
*/
40+
drvOutCharLcd(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
41+
const char *driver_name)
42+
: drvOutputBase(i2c, sensorAddress, mux_channel, driver_name) {
43+
// Initialization handled by drvOutPutBase constructor
44+
}
45+
46+
/*!
47+
@brief Destructor for a quad alphanumeric display.
48+
*/
49+
~drvOutCharLcd() {
50+
if (_lcd) {
51+
delete _lcd;
52+
_lcd = nullptr;
53+
}
54+
}
55+
56+
/*!
57+
@brief Initializes an I2C character LCD and begins I2C communication.
58+
@returns True if initialized successfully, False otherwise.
59+
*/
60+
bool begin() override {
61+
_lcd = new Adafruit_LiquidCrystal(_address, _i2c);
62+
bool did_begin = _lcd->begin(_cols, _rows);
63+
if (did_begin && _enable_backlight) {
64+
_lcd->setBacklight(LOW);
65+
}
66+
return did_begin;
67+
}
68+
69+
/*!
70+
@brief Writes a message to the LCD.
71+
@note MUST be called prior to begin() to configure the LCD's size
72+
@param message
73+
The message to be displayed.
74+
*/
75+
void ConfigureCharLcd(uint8_t rows, uint8_t cols, bool enable_backlight) {
76+
_rows = rows;
77+
_cols = cols;
78+
_enable_backlight = enable_backlight;
79+
}
80+
81+
protected:
82+
Adafruit_LiquidCrystal
83+
*_lcd; ///< Pointer to the Adafruit_LiquidCrystal object
84+
uint8_t _rows = 2; ///< Number of rows in the display
85+
uint8_t _cols = 16; ///< Number of columns in the display
86+
bool _enable_backlight; ///< Flag to enable/disable backlight
87+
};
88+
89+
#endif // DRV_OUT_CHAR_LCD

src/components/i2c/drivers/drvOutQuadAlphaNum.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,6 @@ class drvOutQuadAlphaNum : public drvOutputBase {
133133
pos_start = LED_MAX_CHARS - seg_chars;
134134
}
135135

136-
137-
WS_DEBUG_PRINT("Message to display: ");
138-
WS_DEBUG_PRINTLN(message);
139-
WS_DEBUG_PRINT(" with len_display: ");
140-
WS_DEBUG_PRINTLN(len_display);
141-
WS_DEBUG_PRINT(" at pos_start: ");
142-
WS_DEBUG_PRINTLN(pos_start);
143-
144-
// TODO FRIDAY
145-
// NOTE: If there's a ., increment len_display by 1 to account for the decimal
146-
147136
// Write to the display's buffer
148137
int cur_idx = pos_start;
149138
for (size_t i = 0; i < len_display; i++) {
@@ -159,12 +148,6 @@ class drvOutQuadAlphaNum : public drvOutputBase {
159148
}
160149

161150
// Write the character to the display buffer
162-
WS_DEBUG_PRINT("Writing char: ");
163-
WS_DEBUG_PRINT(ch);
164-
WS_DEBUG_PRINT(" at index: ");
165-
WS_DEBUG_PRINT(cur_idx);
166-
WS_DEBUG_PRINT(" with dot: ");
167-
WS_DEBUG_PRINTLN(display_dot);
168151
_alpha4->writeDigitAscii(cur_idx, ch, display_dot);
169152
cur_idx++;
170153
}

src/components/i2c/drivers/drvOutputBase.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ class drvOutputBase : public drvBase {
8787
// noop
8888
}
8989

90+
/*!
91+
@brief Configures a character LCD.
92+
@param rows
93+
The number of rows in the LCD.
94+
@param cols
95+
The number of columns in the LCD.
96+
@param enable_backlight
97+
True if the backlight is enabled, False otherwise.
98+
*/
99+
virtual void ConfigureCharLcd(uint32_t rows, uint32_t cols,
100+
bool enable_backlight) {
101+
// noop
102+
}
103+
90104
/*!
91105
@brief Sets the brightness of the LED backpack.
92106
@param b

0 commit comments

Comments
 (0)