Skip to content

Commit 092c796

Browse files
committed
Fully integrate SSD1306 driver, missing write func
1 parent 0e19aa0 commit 092c796

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,21 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
878878
}
879879
_drivers_out.push_back(_sevenSeg);
880880
WS_DEBUG_PRINTLN("7-Segement LED Matrix Initialized Successfully!");
881+
} else if (strcmp("ssd1306", msgDeviceInitReq->i2c_device_name) == 0) {
882+
_ssd1306 = new WipperSnapper_I2C_Driver_Out_Ssd1306(this->_i2c, i2cAddress);
883+
_ssd1306->ConfigureSSD1306(
884+
(uint8_t)msgDeviceInitReq->i2c_output_add.config.ssd1306_config.width,
885+
(uint8_t)msgDeviceInitReq->i2c_output_add.config.ssd1306_config.height,
886+
(uint8_t)
887+
msgDeviceInitReq->i2c_output_add.config.ssd1306_config.text_size);
888+
if (!_ssd1306->begin()) {
889+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize ssd1306!");
890+
_busStatusResponse =
891+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
892+
return false;
893+
}
894+
_drivers_out.push_back(_ssd1306);
895+
WS_DEBUG_PRINTLN("SSD1306 display initialized Successfully!");
881896
} else {
882897
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
883898
_busStatusResponse =
@@ -1150,6 +1165,7 @@ bool WipperSnapper_Component_I2C::Handle_I2cDeviceOutputWrite(
11501165
// Create a ptr to the base driver out
11511166
WipperSnapper_I2C_Driver_Out *driver_out = nullptr;
11521167
// Find the matching driver by address in the _drivers_out vector
1168+
// TODO: Refactor this outwards
11531169
WS_DEBUG_PRINT("Searching for i2c output driver with address: ");
11541170
WS_DEBUG_PRINT(msgDeviceWrite->i2c_device_address);
11551171
for (size_t i = 0; i < _drivers_out.size(); i++) {
@@ -1172,6 +1188,10 @@ bool WipperSnapper_Component_I2C::Handle_I2cDeviceOutputWrite(
11721188
} else if (msgDeviceWrite->which_output_msg ==
11731189
wippersnapper_i2c_v1_I2CDeviceOutputWrite_write_char_lcd_tag) {
11741190
driver_out->WriteMessageCharLCD(&msgDeviceWrite->output_msg.write_char_lcd);
1191+
} else if (msgDeviceWrite->which_output_msg ==
1192+
wippersnapper_i2c_v1_I2CDeviceOutputWrite_write_ssd1306_tag) {
1193+
driver_out->WriteMessageSSD1306(
1194+
msgDeviceWrite->output_msg.write_ssd1306.message);
11751195
} else {
11761196
WS_DEBUG_PRINTLN("ERROR: Unknown output message type!");
11771197
return false;

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "drivers/WipperSnapper_I2C_Driver_Out_7Seg.h"
5555
#include "drivers/WipperSnapper_I2C_Driver_Out_CharLcd.h"
5656
#include "drivers/WipperSnapper_I2C_Driver_Out_QuadAlphaNum.h"
57+
#include "drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h"
5758
#include "drivers/WipperSnapper_I2C_Driver_PCT2075.h"
5859
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
5960
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
@@ -203,6 +204,7 @@ class WipperSnapper_Component_I2C {
203204
WipperSnapper_I2C_Driver_Out_QuadAlphaNum *_quadAlphaNum = nullptr;
204205
WipperSnapper_I2C_Driver_Out_CharLcd *_charLcd = nullptr;
205206
WipperSnapper_I2C_Driver_Out_7Seg *_sevenSeg = nullptr;
207+
WipperSnapper_I2C_Driver_Out_Ssd1306 *_ssd1306 = nullptr;
206208
};
207209
extern Wippersnapper WS;
208210

src/components/i2c/drivers/WipperSnapper_I2C_Driver_Out.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ class WipperSnapper_I2C_Driver_Out : public WipperSnapper_I2C_Driver {
5353
// noop
5454
}
5555

56+
/*!
57+
@brief Configures a SSD1306 OLED display. Must be called before driver
58+
begin()
59+
@param width
60+
The width of the display in pixels.
61+
@param height
62+
The height of the display in pixels.
63+
@param i2c_address
64+
The I2C address of the display.
65+
*/
66+
virtual void ConfigureSSD1306(uint8_t width, uint8_t height,
67+
uint8_t text_size) {
68+
// noop
69+
}
70+
71+
/*!
72+
@brief Writes a message to the SSD1306 display.
73+
@param message
74+
The message to be displayed.
75+
*/
76+
virtual void WriteMessageSSD1306(const char *message) {
77+
// noop
78+
}
79+
5680
/*!
5781
@brief Configures a LED backpack.
5882
@param brightness

src/components/i2c/drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#define DEFAULT_WIDTH 128
2424
#define DEFAULT_HEIGHT 64
25-
#define DEFAULT_ADDR 0x3C
26-
#define PIN_OLED_RESET -1
2725

2826
/*!
2927
@brief Class that provides a driver interface for a SSD1306
@@ -42,8 +40,7 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
4240
7-bit device address.
4341
*/
4442
/*******************************************************************************/
45-
WipperSnapper_I2C_Driver_Out_Ssd1306(TwoWire *i2c,
46-
uint16_t sensorAddress)
43+
WipperSnapper_I2C_Driver_Out_Ssd1306(TwoWire *i2c, uint16_t sensorAddress)
4744
: WipperSnapper_I2C_Driver_Out(i2c, sensorAddress) {
4845
_i2c = i2c;
4946
_sensorAddress = sensorAddress;
@@ -66,9 +63,9 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
6663
@returns True if initialized successfully, False otherwise.
6764
*/
6865
bool begin() {
69-
_display = new Adafruit_SSD1306(_width, _height, &_i2c, PIN_OLED_RESET);
70-
bool did_begin = _display->begin(SSD1306_SWITCHCAPVCC, DEFAULT_ADDR);
71-
if (! did_begin)
66+
_display = new Adafruit_SSD1306(_width, _height, _i2c);
67+
bool did_begin = _display->begin(SSD1306_SWITCHCAPVCC, _sensorAddress);
68+
if (!did_begin)
7269
return false;
7370

7471
// Show initial display buffer contents on the screen --
@@ -77,14 +74,42 @@ class WipperSnapper_I2C_Driver_Out_Ssd1306
7774
delay(2000);
7875
// Clear the buffer
7976
_display->clearDisplay();
80-
// Set the text size
81-
77+
// Configure the text size and color
78+
_display->setTextSize(_text_sz);
79+
_display->setTextColor(SSD1306_WHITE);
80+
// Reset the cursor position
81+
_display->setCursor(0, 0);
8282
return true;
8383
}
8484

85+
/*!
86+
@brief Configures a SSD1306 OLED display. Must be called before driver
87+
begin()
88+
@param width
89+
The width of the display in pixels.
90+
@param height
91+
The height of the display in pixels.
92+
@param i2c_address
93+
The I2C address of the display.
94+
*/
95+
void ConfigureSSD1306(uint8_t width, uint8_t height, uint8_t text_size) {
96+
_width = width;
97+
_height = height;
98+
_text_sz = text_size;
99+
}
100+
101+
/*!
102+
@brief Writes a message to the SSD1306 display.
103+
@param message
104+
The message to be displayed.
105+
*/
106+
void WriteMessageSSD1306(const char *message) {
107+
// noop
108+
}
85109

86110
protected:
87-
Adafruit_SSD1306 *_display = nullptr; ///< Pointer to the Adafruit_SSD1306 object
111+
Adafruit_SSD1306 *_display =
112+
nullptr; ///< Pointer to the Adafruit_SSD1306 object
88113
uint8_t _width, _height, _text_sz; ///< Width and height of the display
89114
};
90115

0 commit comments

Comments
 (0)