Skip to content

Commit 538ce3b

Browse files
committed
Add libraries
1 parent 93dd43d commit 538ce3b

File tree

6 files changed

+229
-6
lines changed

6 files changed

+229
-6
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=SdFat - Adafruit Fork, Adafruit SPIFlash, Adafruit NeoPixel, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, RTClib, StreamUtils, Adafruit SHT4x Library
10+
depends=SdFat - Adafruit Fork, Adafruit SPIFlash, Adafruit NeoPixel, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal

src/components/i2c/controller.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ static const std::map<std::string, FnCreateI2cOutputDrv> I2cFactoryOutput = {
366366
const char *driver_name) -> drvOutputBase * {
367367
return new drvOutQuadAlphaNum(i2c, addr, mux_channel, driver_name);
368368
}},
369+
{"7seg",
370+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
371+
const char *driver_name) -> drvOutputBase * {
372+
return new drvOut7Seg(i2c, addr, mux_channel, driver_name);
373+
}},
369374
{"charlcd",
370375
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
371376
const char *driver_name) -> drvOutputBase * {
@@ -828,13 +833,15 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
828833
// Determine which driver cb function to use
829834
if (_i2c_model->GetI2cDeviceOutputWriteMsg()->which_output_msg ==
830835
wippersnapper_i2c_I2cDeviceOutputWrite_write_led_backpack_tag) {
836+
WS_DEBUG_PRINTLN("[i2c] Writing to LED backpack...");
831837
if (!driver->LedBackpackWrite(&_i2c_model->GetI2cDeviceOutputWriteMsg()
832838
->output_msg.write_led_backpack)) {
833839
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to write to LED backpack!");
834840
return false;
835841
}
836842
} else if (_i2c_model->GetI2cDeviceOutputWriteMsg()->which_output_msg ==
837843
wippersnapper_i2c_I2cDeviceOutputWrite_write_char_lcd_tag) {
844+
WS_DEBUG_PRINTLN("[i2c] Writing to char LCD...");
838845
if (!driver->WriteMessageCharLCD(&_i2c_model->GetI2cDeviceOutputWriteMsg()
839846
->output_msg.write_char_lcd)) {
840847
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to write to char LCD!");
@@ -1026,8 +1033,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
10261033
wippersnapper_i2c_output_LedBackpackConfig cfg =
10271034
_i2c_model->GetI2cDeviceAddOrReplaceMsg()
10281035
->i2c_output_add.config.led_backpack_config;
1029-
WS_DEBUG_PRINT("[i2c] Got cfg, calling ConfigureLEDBackpack...");
1030-
drv_out->ConfigureLEDBackpack(cfg.brightness, cfg.alignment);
1036+
WS_DEBUG_PRINT("[i2c] Got cfg, calling ConfigureI2CBackpack...");
1037+
drv_out->ConfigureI2CBackpack(cfg.brightness, cfg.alignment);
10311038
WS_DEBUG_PRINTLN("OK!");
10321039
} else if (config ==
10331040
wippersnapper_i2c_output_I2cOutputAdd_char_lcd_config_tag) {

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/drvOut7Seg.h"
4849
#include "drivers/drvOutCharLcd.h"
4950
#include "drivers/drvOutQuadAlphaNum.h"
5051
#include "drivers/drvOutputBase.h" ///< Base i2c output driver class
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*!
2+
* @file drvOut7Seg.h
3+
*
4+
* Device driver designed specifically to work with the Adafruit LED 7-Segment
5+
* I2C backpacks:
6+
* ----> http://www.adafruit.com/products/881
7+
* ----> http://www.adafruit.com/products/880
8+
* ----> http://www.adafruit.com/products/879
9+
* ----> http://www.adafruit.com/products/878
10+
*
11+
* Adafruit invests time and resources providing this open source code,
12+
* please support Adafruit and open-source hardware by purchasing
13+
* products from Adafruit!
14+
*
15+
* Copyright (c) Brent Rubell for Adafruit Industries 2025
16+
*
17+
* MIT license, all text here must be included in any redistribution.
18+
*
19+
*/
20+
21+
#ifndef DRV_OUT_7SEG_H
22+
#define DRV_OUT_7SEG_H
23+
24+
#include "drvOutputBase.h"
25+
#include <Adafruit_LEDBackpack.h>
26+
#include <Arduino.h>
27+
28+
#define LED_BACKPACK_ALIGNMENT_UNSPECIFIED 0
29+
#define LED_BACKPACK_ALIGNMENT_LEFT 1
30+
#define LED_BACKPACK_ALIGNMENT_RIGHT 2
31+
#define LED_BACKPACK_ALIGNMENT_DEFAULT LED_BACKPACK_ALIGNMENT_LEFT
32+
#define LED_MAX_CHARS 4
33+
34+
/*!
35+
@brief Class that provides a driver for an Adafruit 7-Segment LED matrix
36+
w/I2C backpack.
37+
*/
38+
class drvOut7Seg : public drvOutputBase {
39+
public:
40+
/*!
41+
@brief Constructor for an Adafruit 7-Segment LED matrix w/I2C backpack.
42+
@param i2c
43+
The I2C interface.
44+
@param sensorAddress
45+
7-bit device address.
46+
@param mux_channel
47+
The I2C multiplexer channel.
48+
@param driver_name
49+
The name of the driver.
50+
*/
51+
drvOut7Seg(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
52+
const char *driver_name)
53+
: drvOutputBase(i2c, sensorAddress, mux_channel, driver_name) {
54+
// Initialization handled by drvBase constructor
55+
}
56+
57+
/*!
58+
@brief Destructor for an Adafruit 7-Segment LED matrix w/I2C backpack.
59+
*/
60+
~drvOut7Seg() {
61+
if (_matrix) {
62+
delete _matrix;
63+
_matrix = nullptr;
64+
}
65+
}
66+
67+
/*!
68+
@brief Initializes the 7-segment LED matrix and begins I2C
69+
communication.
70+
@returns True if initialized successfully, False otherwise.
71+
*/
72+
bool begin() override {
73+
_matrix = new Adafruit_7segment();
74+
bool did_begin = _matrix->begin(0x70, _i2c);
75+
return did_begin;
76+
}
77+
78+
/*!
79+
@brief Configures the LED matrix's I2C backpack.
80+
@param brightness
81+
The brightness of the i2c backpack (0-15).
82+
@param alignment
83+
The alignment of the i2c backpack's LED matrix (left/right).
84+
*/
85+
void ConfigureI2CBackpack(int32_t brightness, uint32_t alignment) {
86+
if (alignment == LED_BACKPACK_ALIGNMENT_RIGHT) {
87+
_alignment = LED_BACKPACK_ALIGNMENT_RIGHT;
88+
} else {
89+
_alignment = LED_BACKPACK_ALIGNMENT_DEFAULT;
90+
}
91+
92+
// Note: Adafruit_LEDBackpack normalizes only > 15, but not < 0,
93+
// so, here we're normalizing < 0 to 8 (median brightness)
94+
if (brightness < 0) {
95+
brightness = 8; // Set to median brightness if out of range
96+
}
97+
}
98+
99+
/*!
100+
@brief Sets the brightness of the LED backpack.
101+
@param b
102+
The brightness value, from 0 (off) to 15 (full brightness).
103+
*/
104+
void SetLedBackpackBrightness(uint8_t b) {
105+
if (_matrix == nullptr) {
106+
return;
107+
}
108+
_matrix->setBrightness(b);
109+
}
110+
111+
/*!
112+
@brief Writes the first four characters of a message to the Adafruit
113+
7-segment LED matrix.
114+
@param message
115+
The message to be displayed.
116+
*/
117+
void WriteMessage(const char *message) {
118+
if (_matrix == nullptr || message == nullptr) {
119+
return;
120+
}
121+
122+
// Clear before writing
123+
_matrix->clear();
124+
125+
// Calculate the number of characters to display
126+
size_t len_display = min(strlen(message), (size_t)LED_MAX_CHARS);
127+
128+
// Set the starting position based on alignment
129+
int pos_start;
130+
if (_alignment == LED_BACKPACK_ALIGNMENT_LEFT) {
131+
pos_start = 0; // start at the leftmost position of the display
132+
} else {
133+
// Exclude decimal points from the character count because those get
134+
// displayed on a "special" segment of the LED display
135+
int seg_chars = 0;
136+
for (size_t i = 0; i < len_display; i++) {
137+
if (message[i] != '.') {
138+
seg_chars++;
139+
}
140+
}
141+
// start at the rightmost position of the display
142+
pos_start = LED_MAX_CHARS - seg_chars;
143+
}
144+
145+
// Write to the display's buffer
146+
int cur_idx = pos_start;
147+
for (size_t i = 0; i < len_display; i++) {
148+
// skip position 2
149+
if (cur_idx == 2) {
150+
cur_idx++;
151+
}
152+
// Save the character because if there's a decimal, we need to skip it in
153+
// the buffer
154+
char ch = message[i];
155+
156+
// Look-ahead for a decimal point to attach to the current character
157+
bool display_dot = false;
158+
if (i + 1 < len_display && message[i + 1] == '.') {
159+
display_dot = true;
160+
i++;
161+
len_display++;
162+
}
163+
164+
// Write the character to the display buffer
165+
_matrix->writeDigitAscii(cur_idx, ch, display_dot);
166+
cur_idx++;
167+
}
168+
// Issue the buffered data in RAM to the display
169+
_matrix->writeDisplay();
170+
}
171+
172+
/*!
173+
@brief Writes a floating point value to the Adafruit 7-segment LED
174+
matrix.
175+
@param value
176+
The value to be displayed. Only the first four digits are
177+
displayed.
178+
*/
179+
void WriteValue(float value) {
180+
char message[8 + 1];
181+
snprintf(message, sizeof(message), "%.5f", value);
182+
WriteMessage(message);
183+
}
184+
185+
/*!
186+
@brief Writes an integer value to the Adafruit 7-segment LED matrix.
187+
@param value
188+
The value to be displayed. Only the first four digits are
189+
displayed.
190+
*/
191+
void WriteValue(int32_t value) {
192+
char message[LED_MAX_CHARS + 1];
193+
snprintf(message, sizeof(message), "%ld", value);
194+
WriteMessage(message);
195+
}
196+
197+
protected:
198+
Adafruit_7segment *_matrix =
199+
nullptr; ///< ptr to a 7-segment LED matrix object
200+
int32_t _brightness; ///< Brightness of the LED backpack, from 0 (off) to 15
201+
///< (full brightness)
202+
uint32_t _alignment =
203+
LED_BACKPACK_ALIGNMENT_DEFAULT; ///< Determines L/R alignment of the
204+
///< message displayed
205+
};
206+
207+
#endif // DRV_OUT_7SEG_H

src/components/i2c/drivers/drvOutQuadAlphaNum.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class drvOutQuadAlphaNum : public drvOutputBase {
7777
@param alignment
7878
The alignment of the LED backpack.
7979
*/
80-
void ConfigureLEDBackpack(int32_t brightness, uint32_t alignment) {
81-
WS_DEBUG_PRINTLN("[i2c] drvOutQuadAlphaNum::ConfigureLEDBackpack() called");
80+
void ConfigureI2CBackpack(int32_t brightness, uint32_t alignment) {
81+
WS_DEBUG_PRINTLN("[i2c] drvOutQuadAlphaNum::ConfigureI2CBackpack() called");
8282
if (alignment == LED_BACKPACK_ALIGNMENT_RIGHT) {
8383
_alignment = LED_BACKPACK_ALIGNMENT_RIGHT;
8484
} else {

src/components/i2c/drivers/drvOutputBase.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class drvOutputBase : public drvBase {
8383
@param alignment
8484
The alignment of the LED backpack.
8585
*/
86-
virtual void ConfigureLEDBackpack(int32_t brightness, uint32_t alignment) {
86+
virtual void ConfigureI2CBackpack(int32_t brightness, uint32_t alignment) {
8787
// noop
8888
}
8989

@@ -109,6 +109,9 @@ class drvOutputBase : public drvBase {
109109
bool
110110
WriteMessageCharLCD(wippersnapper_i2c_output_CharLCDWrite *write_char_lcd) {
111111
WriteMessage(write_char_lcd->message);
112+
// NOTE: While this isn't calling any other funcs in here and ret'ing true,
113+
// I want to keep this function high-level for when we implement backlight
114+
// color and scrolling.
112115
return true;
113116
}
114117

@@ -137,15 +140,20 @@ class drvOutputBase : public drvBase {
137140
// Write the message to a LED backpack
138141
switch (msg_backpack_write->which_message) {
139142
case wippersnapper_i2c_output_LedBackpackWrite_text_tag:
143+
WS_DEBUG_PRINTLN("[i2c] Writing text to LED backpack...");
140144
WriteMessage(msg_backpack_write->message.text);
141145
break;
142146
case wippersnapper_i2c_output_LedBackpackWrite_number_int_tag:
147+
WS_DEBUG_PRINTLN("[i2c] Writing int to LED backpack...");
143148
WriteValue(msg_backpack_write->message.number_int);
144149
break;
145150
case wippersnapper_i2c_output_LedBackpackWrite_number_float_tag:
151+
WS_DEBUG_PRINTLN("[i2c] Writing float to LED backpack...");
146152
WriteValue(msg_backpack_write->message.number_float);
147153
break;
148154
default:
155+
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to determine LED backpack "
156+
"message type!");
149157
return false;
150158
break;
151159
}

0 commit comments

Comments
 (0)