Skip to content

Commit 263fbfa

Browse files
committed
Implement WriteX funcs for each value type and link within drvOutputBase
1 parent 888a3c0 commit 263fbfa

File tree

6 files changed

+119
-33
lines changed

6 files changed

+119
-33
lines changed

src/components/i2c/controller.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,10 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
818818
if (_i2c_model->GetI2cDeviceOutputWriteMsg()->has_led_backpack_write) {
819819
// TODO
820820
WS_DEBUG_PRINTLN("[i2c] LED backpack write!");
821+
if (!driver->LedBackpackWrite(&_i2c_model->GetI2cDeviceOutputWriteMsg()->led_backpack_write)) {
822+
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to write to LED backpack!");
823+
return false;
824+
}
821825
} else if (_i2c_model->GetI2cDeviceOutputWriteMsg()->has_char_lcd_write) {
822826
WS_DEBUG_PRINTLN("[i2c] Char LCD write not implemented yet!");
823827
} else {

src/components/i2c/drivers/drvOutQuadAlphaNum.h

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
#include "drvOutputBase.h"
2020
#include <Adafruit_LEDBackpack.h>
21+
#include <Arduino.h>
2122

2223
#define LED_BACKPACK_ALIGNMENT_UNSPECIFIED 0
2324
#define LED_BACKPACK_ALIGNMENT_LEFT 1
2425
#define LED_BACKPACK_ALIGNMENT_RIGHT 2
26+
#define LED_MAX_CHARS 4
2527

2628
/*!
2729
@brief Class that provides a driver interface for Quad Alphanumeric
@@ -64,6 +66,7 @@ class drvOutQuadAlphaNum : public drvOutputBase {
6466
_alpha4 = new Adafruit_AlphaNum4();
6567
bool did_begin = _alpha4->begin(_address, _i2c);
6668
_alpha4->setBrightness(_brightness);
69+
return did_begin;
6770
}
6871

6972
/*!
@@ -83,19 +86,65 @@ class drvOutQuadAlphaNum : public drvOutputBase {
8386
_brightness = brightness;
8487
}
8588

89+
/*!
90+
@brief Sets the brightness of the LED backpack.
91+
@param b
92+
The brightness value, from 0 (off) to 15 (full brightness).
93+
*/
94+
void SetLedBackpackBrightness(uint8_t b) {
95+
if (_alpha4 == nullptr) {
96+
return;
97+
}
98+
_alpha4->setBrightness(b);
99+
}
100+
86101
/*!
87102
@brief Writes the first four characters of a message to the quad
88103
alphanumeric display.
89104
@param message
90105
The message to be displayed.
91106
*/
92107
void WriteMessage(const char *message) {
93-
if (_alpha4 == nullptr) {
108+
if (_alpha4 == nullptr || message == nullptr) {
94109
return;
95110
}
96-
for (size_t i = 0; i < 4; i++) {
97-
_alpha4->writeDigitAscii(i, message[i]);
111+
// Clear before writing
112+
_alpha4->clear();
113+
114+
// Calculate the number of characters to display
115+
size_t len_display = min(strlen(message), (size_t)LED_MAX_CHARS);
116+
117+
// Set the starting position based on alignment
118+
int pos_start;
119+
if (_alignment == LED_BACKPACK_ALIGNMENT_LEFT) {
120+
pos_start = 0; // start at the leftmost position of the display
121+
} else {
122+
// Exclude decimal points from the character count because those get
123+
// displayed on a "special" segment of the LED display
124+
int seg_chars = 0;
125+
for (size_t i = 0; i < len_display; i++) {
126+
if (message[i] != '.') {
127+
seg_chars++;
128+
}
129+
}
130+
// start at the rightmost position of the display
131+
pos_start = LED_MAX_CHARS - seg_chars;
132+
}
133+
134+
// Write to the display's buffer
135+
int cur_idx = pos_start;
136+
for (size_t i = 0; i < len_display; i++) {
137+
// Look-ahead for a decimal point to attach to the current character
138+
bool display_dot = false;
139+
if (i + 1 < len_display && message[i + 1] == '.') {
140+
display_dot = true;
141+
i++;
142+
}
143+
// Write the character to the display buffer
144+
_alpha4->writeDigitAscii(cur_idx, message[i], display_dot);
145+
cur_idx++;
98146
}
147+
// Issue the buffered data in RAM to the display
99148
_alpha4->writeDisplay();
100149
}
101150

@@ -106,10 +155,9 @@ class drvOutQuadAlphaNum : public drvOutputBase {
106155
displayed.
107156
*/
108157
void WriteValue(float value) {
109-
if (_alpha4 == nullptr) {
110-
return;
111-
}
112-
// TODO!
158+
char message[LED_MAX_CHARS + 1];
159+
snprintf(message, sizeof(message), "%.4f", value);
160+
WriteMessage(message);
113161
}
114162

115163
/*!
@@ -119,17 +167,19 @@ class drvOutQuadAlphaNum : public drvOutputBase {
119167
displayed.
120168
*/
121169
void WriteValue(int32_t value) {
122-
if (_alpha4 == nullptr) {
123-
return;
124-
}
125-
// TODO!
170+
char message[LED_MAX_CHARS + 1];
171+
snprintf(message, sizeof(message), "%ld", value);
172+
WriteMessage(message);
126173
}
127174

128175
protected:
129176
Adafruit_AlphaNum4 *_alpha4 =
130-
nullptr; ///< ptr to a 4-digit alphanumeric display object
131-
int32_t _brightness;
132-
uint32_t _alignment;
177+
nullptr; ///< ptr to a 4-digit alphanumeric display object
178+
int32_t _brightness; ///< Brightness of the LED backpack, from 0 (off) to 15
179+
///< (full brightness)
180+
uint32_t _alignment =
181+
LED_BACKPACK_ALIGNMENT_RIGHT; ///< Determines L/R alignment of the message
182+
///< displayed
133183
};
134184

135185
#endif // DRV_OUT_QUAD_ALPHANUM_H

src/components/i2c/drivers/drvOutputBase.h

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,45 @@ class drvOutputBase : public drvBase {
8888
}
8989

9090
/*!
91-
@brief High-level fn, executes a call to the appropriate driver function(s)
92-
based on the message data type to write.
91+
@brief Sets the brightness of the LED backpack.
92+
@param b
93+
The brightness value, from 0 (off) to 15 (full brightness).
94+
*/
95+
virtual void SetLedBackpackBrightness(uint8_t b) {
96+
// noop
97+
}
98+
99+
/*!
100+
@brief High-level fn, executes a call to the appropriate driver
101+
function(s) based on the message data type to write.
93102
@param msg_backpack_write
94103
Pointer to a wippersnapper_i2c_output_LedBackpackWrite message.
104+
@returns True if the message was written successfully, False otherwise.
95105
*/
96-
void LedBackpackWrite(wippersnapper_i2c_output_LedBackpackWrite *msg_backpack_write) {
106+
bool LedBackpackWrite(
107+
wippersnapper_i2c_output_LedBackpackWrite *msg_backpack_write) {
108+
// Check if we should adjust brightness
109+
if (msg_backpack_write->adjust_brightness)
110+
SetLedBackpackBrightness((uint8_t)msg_backpack_write->brightness);
97111

112+
// Write the message to a LED backpack
113+
switch (msg_backpack_write->which_message) {
114+
case wippersnapper_i2c_output_LedBackpackWrite_text_tag:
115+
WriteMessage(msg_backpack_write->message.text);
116+
break;
117+
case wippersnapper_i2c_output_LedBackpackWrite_number_int_tag:
118+
WriteValue(msg_backpack_write->message.number_int);
119+
break;
120+
case wippersnapper_i2c_output_LedBackpackWrite_number_float_tag:
121+
WriteValue(msg_backpack_write->message.number_float);
122+
break;
123+
default:
124+
return false;
125+
break;
126+
}
127+
return true;
98128
}
99129

100130
protected:
101-
// TODO
102131
};
103132
#endif // DRV_OUTPUT_BASE_H

src/protos/i2c.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ extern const pb_msgdesc_t wippersnapper_i2c_I2cDeviceOutputWrite_msg;
335335
#define wippersnapper_i2c_I2cDeviceAddOrReplace_size 141
336336
#define wippersnapper_i2c_I2cDeviceAddedOrReplaced_size 56
337337
#define wippersnapper_i2c_I2cDeviceDescriptor_size 50
338-
#define wippersnapper_i2c_I2cDeviceOutputWrite_size 145
338+
#define wippersnapper_i2c_I2cDeviceOutputWrite_size 147
339339
#define wippersnapper_i2c_I2cDeviceRemove_size 52
340340
#define wippersnapper_i2c_I2cDeviceRemoved_size 54
341341
#if defined(wippersnapper_sensor_SensorEvent_size)

src/protos/i2c_output.pb.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ typedef struct _wippersnapper_i2c_output_LedBackpackWrite {
6464
int32_t number_int; /* * Number to write to the LED backpack. * */
6565
float number_float; /* * Float to write to the LED backpack. * */
6666
} message;
67+
bool adjust_brightness; /* * Optionally used to enable the brightness tag. * */
6768
int32_t brightness; /* * Optionally adjusts the brightness from 0 (off) to 15 (full brightness). * */
6869
wippersnapper_i2c_output_LedBackpackBlinkRate blink_rate; /* * Optionally sets the blink rate for the LED backpack. * */
6970
bool enable_scroll_marquee; /* * Optionally enables automatic text scrolling * */
@@ -111,12 +112,12 @@ extern "C" {
111112
#define wippersnapper_i2c_output_LedBackpackConfig_init_default {0, _wippersnapper_i2c_output_LedBackpackAlignment_MIN}
112113
#define wippersnapper_i2c_output_CharLCDConfig_init_default {0, 0, 0, ""}
113114
#define wippersnapper_i2c_output_I2cOutputAdd_init_default {0, {wippersnapper_i2c_output_LedBackpackConfig_init_default}}
114-
#define wippersnapper_i2c_output_LedBackpackWrite_init_default {0, {""}, 0, _wippersnapper_i2c_output_LedBackpackBlinkRate_MIN, 0, 0, 0}
115+
#define wippersnapper_i2c_output_LedBackpackWrite_init_default {0, {""}, 0, 0, _wippersnapper_i2c_output_LedBackpackBlinkRate_MIN, 0, 0, 0}
115116
#define wippersnapper_i2c_output_CharLCDWrite_init_default {0, {""}, 0, "", 0}
116117
#define wippersnapper_i2c_output_LedBackpackConfig_init_zero {0, _wippersnapper_i2c_output_LedBackpackAlignment_MIN}
117118
#define wippersnapper_i2c_output_CharLCDConfig_init_zero {0, 0, 0, ""}
118119
#define wippersnapper_i2c_output_I2cOutputAdd_init_zero {0, {wippersnapper_i2c_output_LedBackpackConfig_init_zero}}
119-
#define wippersnapper_i2c_output_LedBackpackWrite_init_zero {0, {""}, 0, _wippersnapper_i2c_output_LedBackpackBlinkRate_MIN, 0, 0, 0}
120+
#define wippersnapper_i2c_output_LedBackpackWrite_init_zero {0, {""}, 0, 0, _wippersnapper_i2c_output_LedBackpackBlinkRate_MIN, 0, 0, 0}
120121
#define wippersnapper_i2c_output_CharLCDWrite_init_zero {0, {""}, 0, "", 0}
121122

122123
/* Field tags (for use in manual encoding/decoding) */
@@ -131,11 +132,12 @@ extern "C" {
131132
#define wippersnapper_i2c_output_LedBackpackWrite_text_tag 1
132133
#define wippersnapper_i2c_output_LedBackpackWrite_number_int_tag 2
133134
#define wippersnapper_i2c_output_LedBackpackWrite_number_float_tag 3
134-
#define wippersnapper_i2c_output_LedBackpackWrite_brightness_tag 4
135-
#define wippersnapper_i2c_output_LedBackpackWrite_blink_rate_tag 5
136-
#define wippersnapper_i2c_output_LedBackpackWrite_enable_scroll_marquee_tag 6
137-
#define wippersnapper_i2c_output_LedBackpackWrite_scroll_marquee_speed_tag 7
138-
#define wippersnapper_i2c_output_LedBackpackWrite_enable_ampm_dot_tag 8
135+
#define wippersnapper_i2c_output_LedBackpackWrite_adjust_brightness_tag 4
136+
#define wippersnapper_i2c_output_LedBackpackWrite_brightness_tag 5
137+
#define wippersnapper_i2c_output_LedBackpackWrite_blink_rate_tag 6
138+
#define wippersnapper_i2c_output_LedBackpackWrite_enable_scroll_marquee_tag 7
139+
#define wippersnapper_i2c_output_LedBackpackWrite_scroll_marquee_speed_tag 8
140+
#define wippersnapper_i2c_output_LedBackpackWrite_enable_ampm_dot_tag 9
139141
#define wippersnapper_i2c_output_CharLCDWrite_text_tag 1
140142
#define wippersnapper_i2c_output_CharLCDWrite_number_int_tag 2
141143
#define wippersnapper_i2c_output_CharLCDWrite_number_float_tag 3
@@ -170,11 +172,12 @@ X(a, STATIC, ONEOF, MESSAGE, (config,char_lcd_config,config.char_lcd_confi
170172
X(a, STATIC, ONEOF, STRING, (message,text,message.text), 1) \
171173
X(a, STATIC, ONEOF, INT32, (message,number_int,message.number_int), 2) \
172174
X(a, STATIC, ONEOF, FLOAT, (message,number_float,message.number_float), 3) \
173-
X(a, STATIC, SINGULAR, INT32, brightness, 4) \
174-
X(a, STATIC, SINGULAR, UENUM, blink_rate, 5) \
175-
X(a, STATIC, SINGULAR, BOOL, enable_scroll_marquee, 6) \
176-
X(a, STATIC, SINGULAR, FLOAT, scroll_marquee_speed, 7) \
177-
X(a, STATIC, SINGULAR, BOOL, enable_ampm_dot, 8)
175+
X(a, STATIC, SINGULAR, BOOL, adjust_brightness, 4) \
176+
X(a, STATIC, SINGULAR, INT32, brightness, 5) \
177+
X(a, STATIC, SINGULAR, UENUM, blink_rate, 6) \
178+
X(a, STATIC, SINGULAR, BOOL, enable_scroll_marquee, 7) \
179+
X(a, STATIC, SINGULAR, FLOAT, scroll_marquee_speed, 8) \
180+
X(a, STATIC, SINGULAR, BOOL, enable_ampm_dot, 9)
178181
#define wippersnapper_i2c_output_LedBackpackWrite_CALLBACK NULL
179182
#define wippersnapper_i2c_output_LedBackpackWrite_DEFAULT NULL
180183

@@ -207,7 +210,7 @@ extern const pb_msgdesc_t wippersnapper_i2c_output_CharLCDWrite_msg;
207210
#define wippersnapper_i2c_output_CharLCDWrite_size 46
208211
#define wippersnapper_i2c_output_I2cOutputAdd_size 32
209212
#define wippersnapper_i2c_output_LedBackpackConfig_size 13
210-
#define wippersnapper_i2c_output_LedBackpackWrite_size 43
213+
#define wippersnapper_i2c_output_LedBackpackWrite_size 45
211214

212215
#ifdef __cplusplus
213216
} /* extern "C" */

src/protos/signal.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ extern const pb_msgdesc_t wippersnapper_signal_DeviceToBroker_msg;
257257

258258
/* Maximum encoded size of messages (where known) */
259259
#if defined(wippersnapper_digitalio_DigitalIOEvent_size) && defined(wippersnapper_digitalio_DigitalIOWrite_size) && defined(wippersnapper_uart_UARTAdd_size) && defined(wippersnapper_uart_UARTRemove_size)
260-
union wippersnapper_signal_BrokerToDevice_payload_size_union {char f12[(6 + wippersnapper_digitalio_DigitalIOEvent_size)]; char f13[(6 + wippersnapper_digitalio_DigitalIOWrite_size)]; char f80[(7 + wippersnapper_uart_UARTAdd_size)]; char f81[(7 + wippersnapper_uart_UARTRemove_size)]; char f0[149];};
260+
union wippersnapper_signal_BrokerToDevice_payload_size_union {char f12[(6 + wippersnapper_digitalio_DigitalIOEvent_size)]; char f13[(6 + wippersnapper_digitalio_DigitalIOWrite_size)]; char f80[(7 + wippersnapper_uart_UARTAdd_size)]; char f81[(7 + wippersnapper_uart_UARTRemove_size)]; char f0[151];};
261261
#endif
262262
#if defined(wippersnapper_digitalio_DigitalIOEvent_size) && defined(wippersnapper_analogio_AnalogIOEvent_size) && defined(wippersnapper_ds18x20_Ds18x20Event_size) && defined(wippersnapper_uart_UARTAdded_size) && defined(wippersnapper_uart_UARTEvent_size) && defined(wippersnapper_i2c_I2cDeviceEvent_size)
263263
union wippersnapper_signal_DeviceToBroker_payload_size_union {char f10[(6 + wippersnapper_digitalio_DigitalIOEvent_size)]; char f20[(7 + wippersnapper_analogio_AnalogIOEvent_size)]; char f80[(7 + wippersnapper_ds18x20_Ds18x20Event_size)]; char f90[(7 + wippersnapper_uart_UARTAdded_size)]; char f100[(7 + wippersnapper_uart_UARTEvent_size)]; char f113[(7 + wippersnapper_i2c_I2cDeviceEvent_size)]; char f0[6246];};

0 commit comments

Comments
 (0)