Skip to content

[API v1]: Add support for I2C Output Components #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ examples/Wippersnapper_demo/build/
.pio/

# Secrets
data/
data/

# Misc. Data
tests/
venv/
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
depends=SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, 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
depends=SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, 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, Adafruit LED Backpack Library, Adafruit LiquidCrystal
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ lib_deps =
stm32duino/STM32duino VL53L4CD
stm32duino/STM32duino VL53L4CX
adafruit/Adafruit_VL6180X
adafruit/Adafruit PM25 AQI Sensor
adafruit/Adafruit VEML7700 Library
adafruit/Adafruit LC709203F
adafruit/Adafruit LPS2X
Expand All @@ -91,6 +90,9 @@ lib_deps =
https://github.com/adafruit/WiFiNINA.git
https://github.com/Starmbi/hp_BH1750.git
https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git
https://github.com/adafruit/Adafruit_LED_Backpack.git
https://github.com/adafruit/Adafruit_LiquidCrystal.git
https://github.com/adafruit/Adafruit_PM25AQI.git



Expand Down
15 changes: 15 additions & 0 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,21 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
if (!encodeI2CResponse(&msgi2cResponse)) {
return false;
}
} else if (field->tag ==
wippersnapper_signal_v1_I2CRequest_req_i2c_device_out_write_tag) {
WS_DEBUG_PRINTLN("[app] I2C Device Output Write");
// Decode stream into an I2CDeviceDeinitRequest
wippersnapper_i2c_v1_I2CDeviceOutputWrite msgDeviceWrite =
wippersnapper_i2c_v1_I2CDeviceOutputWrite_init_zero;
// Decode stream into struct, msgI2CDeviceDeinitRequest
if (!ws_pb_decode(stream, wippersnapper_i2c_v1_I2CDeviceOutputWrite_fields,
&msgDeviceWrite)) {
WS_DEBUG_PRINTLN(
"[app] ERROR: Failed decoding I2CDeviceOutputWrite message.");
return false;
}
WS._i2cPort0->Handle_I2cDeviceOutputWrite(&msgDeviceWrite);
WS_DEBUG_PRINTLN("[app] I2C Device Output Write Done");
} else {
WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag");
return false; // fail out, we didn't encode anything to publish
Expand Down
109 changes: 107 additions & 2 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,46 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_adt7410->configureDriver(msgDeviceInitReq);
drivers.push_back(_adt7410);
WS_DEBUG_PRINTLN("ADT7410 Initialized Successfully!");
} else if (strcmp("quadalphanum", msgDeviceInitReq->i2c_device_name) == 0) {
_quadAlphaNum =
new WipperSnapper_I2C_Driver_Out_QuadAlphaNum(this->_i2c, i2cAddress);
_quadAlphaNum->ConfigureI2CBackpack(
msgDeviceInitReq->i2c_output_add.config.led_backpack_config.brightness,
msgDeviceInitReq->i2c_output_add.config.led_backpack_config.alignment);
if (!_quadAlphaNum->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize Quad Alphanum. Display!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_drivers_out.push_back(_quadAlphaNum);
WS_DEBUG_PRINTLN("Quad Alphanum. Display Initialized Successfully!");
} else if (strcmp("charlcd", msgDeviceInitReq->i2c_device_name) == 0) {
_charLcd = new WipperSnapper_I2C_Driver_Out_CharLcd(this->_i2c, i2cAddress);
_charLcd->ConfigureCharLcd(
msgDeviceInitReq->i2c_output_add.config.char_lcd_config.rows,
msgDeviceInitReq->i2c_output_add.config.char_lcd_config.columns, true);
if (!_charLcd->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize Character LCD!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_drivers_out.push_back(_charLcd);
WS_DEBUG_PRINTLN("Char LCD Display Initialized Successfully!");
} else if (strcmp("7seg", msgDeviceInitReq->i2c_device_name) == 0) {
_sevenSeg = new WipperSnapper_I2C_Driver_Out_7Seg(this->_i2c, i2cAddress);
_sevenSeg->ConfigureCharLcd(
msgDeviceInitReq->i2c_output_add.config.char_lcd_config.rows,
msgDeviceInitReq->i2c_output_add.config.char_lcd_config.columns, true);
if (!_sevenSeg->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize 7-Segement LED Matrix!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_drivers_out.push_back(_sevenSeg);
WS_DEBUG_PRINTLN("7-Segement LED Matrix Initialized Successfully!");
} else {
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
_busStatusResponse =
Expand Down Expand Up @@ -886,8 +926,9 @@ void WipperSnapper_Component_I2C::updateI2CDeviceProperties(
void WipperSnapper_Component_I2C::deinitI2CDevice(
wippersnapper_i2c_v1_I2CDeviceDeinitRequest *msgDeviceDeinitReq) {
uint16_t deviceAddr = (uint16_t)msgDeviceDeinitReq->i2c_device_address;
std::vector<WipperSnapper_I2C_Driver *>::iterator iter, end;

// Check input (sensor) drivers
std::vector<WipperSnapper_I2C_Driver *>::iterator iter, end;
for (iter = drivers.begin(), end = drivers.end(); iter != end; ++iter) {
if ((*iter)->getI2CAddress() == deviceAddr) {
// Delete the object that iter points to
Expand All @@ -905,6 +946,28 @@ void WipperSnapper_Component_I2C::deinitI2CDevice(
WS_DEBUG_PRINTLN("I2C Device De-initialized!");
}
}

// Check for output drivers
std::vector<WipperSnapper_I2C_Driver_Out *>::iterator out_iter, out_end;
for (out_iter = _drivers_out.begin(), out_end = _drivers_out.end();
out_iter != out_end; ++out_iter) {
if ((*out_iter)->getI2CAddress() == deviceAddr) {
// Set the driver to nullptr
*out_iter = nullptr;
// ESP-IDF, Erase–remove iter ptr from driver vector
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
*out_iter = nullptr;
_drivers_out.erase(
remove(_drivers_out.begin(), _drivers_out.end(), nullptr),
_drivers_out.end());
#else
// Arduino can not erase-remove, erase only
_drivers_out.erase(out_iter);
#endif
WS_DEBUG_PRINTLN("I2C Device De-initialized!");
}
}

_busStatusResponse = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS;
}

Expand Down Expand Up @@ -1073,14 +1136,56 @@ void WipperSnapper_Component_I2C::displayDeviceEventMessage(
}
}

/*******************************************************************************/
/*!
@brief Handles an I2CDeviceOutputWrite message.
@param msgDeviceWrite
A decoded I2CDeviceOutputWrite message.
@returns True if the message was handled successfully, false otherwise.
*/
/*******************************************************************************/
bool WipperSnapper_Component_I2C::Handle_I2cDeviceOutputWrite(
wippersnapper_i2c_v1_I2CDeviceOutputWrite *msgDeviceWrite) {

// Create a ptr to the base driver out
WipperSnapper_I2C_Driver_Out *driver_out = nullptr;
// Find the matching driver by address in the _drivers_out vector
WS_DEBUG_PRINT("Searching for i2c output driver with address: ");
WS_DEBUG_PRINT(msgDeviceWrite->i2c_device_address);
for (size_t i = 0; i < _drivers_out.size(); i++) {
if (_drivers_out[i]->getI2CAddress() ==
msgDeviceWrite->i2c_device_address) {
driver_out = _drivers_out[i];
break;
}
}
if (driver_out == nullptr) {
WS_DEBUG_PRINTLN("ERROR: Driver not found within drivers_out!");
return false;
}

// Call the output_msg
if (msgDeviceWrite->which_output_msg ==
wippersnapper_i2c_v1_I2CDeviceOutputWrite_write_led_backpack_tag) {
driver_out->WriteLedBackpack(
&msgDeviceWrite->output_msg.write_led_backpack);
} else if (msgDeviceWrite->which_output_msg ==
wippersnapper_i2c_v1_I2CDeviceOutputWrite_write_char_lcd_tag) {
driver_out->WriteMessageCharLCD(&msgDeviceWrite->output_msg.write_char_lcd);
} else {
WS_DEBUG_PRINTLN("ERROR: Unknown output message type!");
return false;
}
return true;
}

/*******************************************************************************/
/*!
@brief Queries all I2C device drivers for new values. Fills and sends an
I2CSensorEvent with the sensor event data.
*/
/*******************************************************************************/
void WipperSnapper_Component_I2C::update() {

// Create response message
wippersnapper_signal_v1_I2CResponse msgi2cResponse =
wippersnapper_signal_v1_I2CResponse_init_zero;
Expand Down
15 changes: 14 additions & 1 deletion src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
#include "drivers/WipperSnapper_I2C_Driver_MPRLS.h"
#include "drivers/WipperSnapper_I2C_Driver_MS8607.h"
#include "drivers/WipperSnapper_I2C_Driver_NAU7802.h"
#include "drivers/WipperSnapper_I2C_Driver_Out.h"
#include "drivers/WipperSnapper_I2C_Driver_Out_7Seg.h"
#include "drivers/WipperSnapper_I2C_Driver_Out_CharLcd.h"
#include "drivers/WipperSnapper_I2C_Driver_Out_QuadAlphaNum.h"
#include "drivers/WipperSnapper_I2C_Driver_PCT2075.h"
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
Expand Down Expand Up @@ -104,6 +108,9 @@ class WipperSnapper_Component_I2C {

void update();

bool Handle_I2cDeviceOutputWrite(
wippersnapper_i2c_v1_I2CDeviceOutputWrite *msgDeviceWrite);

void sensorEventRead(
std::vector<WipperSnapper_I2C_Driver *>::iterator &iter,
unsigned long curTime,
Expand Down Expand Up @@ -134,7 +141,10 @@ class WipperSnapper_Component_I2C {
int32_t _portNum;
TwoWire *_i2c = nullptr;
wippersnapper_i2c_v1_BusResponse _busStatusResponse;
std::vector<WipperSnapper_I2C_Driver *> drivers; ///< List of sensor drivers
std::vector<WipperSnapper_I2C_Driver *>
drivers; ///< List of i2c sensor drivers
std::vector<WipperSnapper_I2C_Driver_Out *>
_drivers_out; ///< List of i2c output drivers
// Sensor driver objects
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
Expand Down Expand Up @@ -190,6 +200,9 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_VL6180X *_vl6180x = nullptr;
WipperSnapper_I2C_Driver_MAX17048 *_max17048 = nullptr;
WipperSnapper_I2C_Driver_ADT7410 *_adt7410 = nullptr;
WipperSnapper_I2C_Driver_Out_QuadAlphaNum *_quadAlphaNum = nullptr;
WipperSnapper_I2C_Driver_Out_CharLcd *_charLcd = nullptr;
WipperSnapper_I2C_Driver_Out_7Seg *_sevenSeg = nullptr;
};
extern Wippersnapper WS;

Expand Down
Loading
Loading