From 53b0f549071c8af64d9fe233df1fea51e14a2e2d Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 16 Jul 2021 11:18:35 -0400 Subject: [PATCH 01/95] add topics for i2c --- src/Wippersnapper.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/Wippersnapper.h | 11 ++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 5b24d6a6d..1220801b4 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -609,6 +609,18 @@ bool Wippersnapper::buildWSTopics() { sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) + strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) + strlen("broker") + 1); + // Topic for i2c signals from device to broker + WS._topic_signal_i2c_brkr = (char *)malloc( + sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) + + strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) + strlen("broker") + + strlen(TOPIC_I2C) + 1); + + // Topic for i2c signals from broker to device + WS._topic_signal_i2c_device = (char *)malloc( + sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) + + strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) + strlen("device") + + strlen(TOPIC_I2C) + 1); + // Create global registration topic if (WS._topic_description) { strcpy(WS._topic_description, WS._username); @@ -657,6 +669,32 @@ bool Wippersnapper::buildWSTopics() { is_success = false; } + // Create device-to-broker i2c signal topic + if (WS._topic_signal_i2c_brkr) { + strcpy(WS._topic_signal_i2c_brkr, WS._username); + strcat(WS._topic_signal_i2c_brkr, TOPIC_WS); + strcat(WS._topic_signal_i2c_brkr, _device_uid); + strcat(WS._topic_signal_i2c_brkr, TOPIC_SIGNALS); + strcat(WS._topic_signal_i2c_brkr, "broker"); + strcat(WS._topic_signal_i2c_brkr, TOPIC_I2C); + } else { // malloc failed + WS._topic_signal_i2c_brkr = 0; + is_success = false; + } + + // Create broker-to-device i2c signal topic + if (WS._topic_signal_i2c_device) { + strcpy(WS._topic_signal_i2c_brkr, WS._username); + strcat(WS._topic_signal_i2c_brkr, TOPIC_WS); + strcat(WS._topic_signal_i2c_brkr, _device_uid); + strcat(WS._topic_signal_i2c_brkr, TOPIC_SIGNALS); + strcat(WS._topic_signal_i2c_brkr, "device"); + strcat(WS._topic_signal_i2c_brkr, TOPIC_I2C); + } else { // malloc failed + WS._topic_signal_i2c_brkr = 0; + is_success = false; + } + return is_success; } diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 5c74be614..6be2ed312 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -70,9 +70,10 @@ #define TOPIC_IO_ERRORS "/errors" ///< Adafruit IO Error MQTT Topic // Reserved Wippersnapper topics -#define TOPIC_WS "/wprsnpr/" ///< Global /wprsnpr/ topic -#define TOPIC_DESCRIPTION "/info/" ///< Device description topic -#define TOPIC_SIGNALS "/signals/" ///< Device signals topic +#define TOPIC_WS "/wprsnpr/" ///< WipperSnapper topic +#define TOPIC_DESCRIPTION "/info/" ///< Registration sub-topic +#define TOPIC_SIGNALS "/signals/" ///< Signals sub-topic +#define TOPIC_I2C "/i2c/" ///< I2C sub-topic #define WS_DEBUG ///< Define to enable debugging to serial terminal #define WS_PRINTER Serial ///< Where debug messages will be printed @@ -285,6 +286,10 @@ class Wippersnapper { char *_topic_description_status; /*!< MQTT subtopic carrying the description status resp. from the broker */ char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */ + char *_topic_signal_i2c_brkr; /*!< Topic carries messages from a device to a + broker. */ + char *_topic_signal_i2c_device; /*!< Topic carries messages from a broker to a + device. */ Adafruit_MQTT_Subscribe *_topic_description_sub; /*!< Subscription for registration topic. */ From e23479f157b8725e6347cacc4f155ac5b04c1576 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 16 Jul 2021 11:35:24 -0400 Subject: [PATCH 02/95] add printMsgBuffer, cb for i2c topic --- src/Wippersnapper.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Wippersnapper.h | 3 +++ 2 files changed, 46 insertions(+) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 1220801b4..e16c3317e 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -120,6 +120,31 @@ void Wippersnapper::set_user_key() { #endif } +/**************************************************************************/ +/*! + @brief Prints a message buffer to the serial output + @param Buffer + Desired message to print. + @pram bufLen + Length of buffer to print. +*/ +/**************************************************************************/ +void printMsgBuffer(char *buffer, uint16_t bufLen) { + WS_DEBUG_PRINTLN("Buffer Contents:"); + WS_DEBUG_PRINT('\t'); + for (uint16_t i = 0; i < bufLen; i++) { + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(F(" [0x")); + WS_DEBUG_PRINT(buffer[i], HEX); + WS_DEBUG_PRINT("], "); + if (i % 8 == 7) { + WS_DEBUG_PRINT("\n\t"); + } + } + WS_DEBUG_PRINT("Buffer Length: ") WS_DEBUG_PRINTLN(bufLen); + WS_DEBUG_PRINTLN(""); +} + // Decoders // /****************************************************************************/ /*! @@ -358,6 +383,7 @@ bool Wippersnapper::decodeSignalMsg( /**************************************************************************/ void cbSignalTopic(char *data, uint16_t len) { WS_DEBUG_PRINTLN("cbSignalTopic: New Msg on Signal Topic"); + printMsgBuffer(data, len); WS_DEBUG_PRINT(len); WS_DEBUG_PRINTLN(" bytes."); // zero-out current buffer @@ -375,6 +401,18 @@ void cbSignalTopic(char *data, uint16_t len) { } } +/**************************************************************************/ +/*! + @brief Called when i2c signal sub-topic receives a new message. + Fills a shared buffer with data from payload. +*/ +/**************************************************************************/ +void cbSignalI2CTopic(char *data, uint16_t len) { + WS_DEBUG_PRINTLN("* New Msg on Signal-I2C: "); + printMsgBuffer(data, len); + +} + /**************************************************************************/ /*! @brief Called when broker responds to a device's publish across @@ -710,6 +748,11 @@ void Wippersnapper::subscribeWSTopics() { WS._mqtt->subscribe(_topic_signal_brkr_sub); _topic_signal_brkr_sub->setCallback(cbSignalTopic); + // Subscribe to signal's I2C sub-topic + _topic_signal_i2c_sub = new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_signal_i2c_brkr, 1); + WS._mqtt->subscribe(_topic_signal_i2c_sub); + _topic_signal_i2c_sub->setCallback(cbSignalI2CTopic); + // Subscribe to registration status topic _topic_description_sub = new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_description_status, 1); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 6be2ed312..0c72cb517 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -297,6 +297,9 @@ class Wippersnapper { *_topic_signal_device_pub; /*!< Subscription for D2C signal topic. */ Adafruit_MQTT_Subscribe *_topic_signal_brkr_sub; /*!< Subscription for C2D signal topic. */ + Adafruit_MQTT_Subscribe + *_topic_signal_i2c_sub; /*!< Subscribes to signal's I2C topic. */ + char *_err_topic; /*!< Adafruit IO MQTT error message topic. */ char *_throttle_topic; /*!< Adafruit IO MQTT throttle message topic. */ From aa107ee48bf2b0cb8f16ea132db72c1c9f129e09 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 16 Jul 2021 15:51:41 -0400 Subject: [PATCH 03/95] add I2C skell --- src/Wippersnapper.cpp | 21 +++++++++------- src/components/WipperSnapper_I2C.cpp | 33 +++++++++++++++++++++++++ src/components/WipperSnapper_I2C.h | 37 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 src/components/WipperSnapper_I2C.cpp create mode 100644 src/components/WipperSnapper_I2C.h diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index e16c3317e..b255cb0bc 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -70,6 +70,7 @@ Wippersnapper::~Wippersnapper() { void Wippersnapper::provision() { // init. LED for status signaling statusLEDInit(); + setStatusLEDColor(LED_HW_INIT); #ifdef USE_TINYUSB // init new filesystem _fileSystem = new Wippersnapper_FS(); @@ -320,7 +321,7 @@ bool cbSignalMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) { // decode each ConfigurePinRequest sub-message if (!pb_decode(stream, wippersnapper_pin_v1_ConfigurePinRequests_fields, &msg)) { - WS_DEBUG_PRINTLN("ERROR: Could not decode CreateSign2alRequest") + WS_DEBUG_PRINTLN("ERROR: Could not decode CreateSignalRequest") is_success = false; } } else if (field->tag == @@ -382,10 +383,8 @@ bool Wippersnapper::decodeSignalMsg( */ /**************************************************************************/ void cbSignalTopic(char *data, uint16_t len) { - WS_DEBUG_PRINTLN("cbSignalTopic: New Msg on Signal Topic"); + WS_DEBUG_PRINTLN("* New Msg on Signal"); printMsgBuffer(data, len); - WS_DEBUG_PRINT(len); - WS_DEBUG_PRINTLN(" bytes."); // zero-out current buffer memset(WS._buffer, 0, sizeof(WS._buffer)); // copy data to buffer @@ -407,10 +406,17 @@ void cbSignalTopic(char *data, uint16_t len) { Fills a shared buffer with data from payload. */ /**************************************************************************/ -void cbSignalI2CTopic(char *data, uint16_t len) { +void cbSignalI2CReq(char *data, uint16_t len) { WS_DEBUG_PRINTLN("* New Msg on Signal-I2C: "); printMsgBuffer(data, len); + // zero-out current buffer + memset(WS._buffer, 0, sizeof(WS._buffer)); + // copy data to buffer + memcpy(WS._buffer, data, len); + WS.bufSize = len; + // Empty struct for storing the I2C request message + // TODO: Decoding cbs here or pass into i2c functions } /**************************************************************************/ @@ -751,7 +757,7 @@ void Wippersnapper::subscribeWSTopics() { // Subscribe to signal's I2C sub-topic _topic_signal_i2c_sub = new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_signal_i2c_brkr, 1); WS._mqtt->subscribe(_topic_signal_i2c_sub); - _topic_signal_i2c_sub->setCallback(cbSignalI2CTopic); + _topic_signal_i2c_sub->setCallback(cbSignalI2CReq); // Subscribe to registration status topic _topic_description_sub = @@ -768,9 +774,6 @@ void Wippersnapper::subscribeWSTopics() { void Wippersnapper::connect() { WS_DEBUG_PRINTLN("connect()"); - /* statusLEDInit(); - setStatusLEDColor(LED_HW_INIT); */ - _status = WS_IDLE; WS._boardStatus = WS_BOARD_DEF_IDLE; diff --git a/src/components/WipperSnapper_I2C.cpp b/src/components/WipperSnapper_I2C.cpp new file mode 100644 index 000000000..4c14ff6ec --- /dev/null +++ b/src/components/WipperSnapper_I2C.cpp @@ -0,0 +1,33 @@ +/*! + * @file Wippersnapper_I2C.cpp + * + * This file provides functions for interfacing + * with the I2C bus. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ + +#include "WipperSnapper_I2C.h" + +WipperSnapper_I2C::WipperSnapper_I2C(uint32_t frequency) { + // TODO - init! +} + +~WipperSnapper_I2C::WipperSnapper_I2C() { + // todo - DTOR! +} + +bool WipperSnapper_I2C::scanForAddress(uint32_t address) { + // TODO: scanner code +} + +bool WipperSnapper_I2C::setFrequency(uint32_t frequency) { + // TODO: frequency setter code +} diff --git a/src/components/WipperSnapper_I2C.h b/src/components/WipperSnapper_I2C.h new file mode 100644 index 000000000..2887a49c0 --- /dev/null +++ b/src/components/WipperSnapper_I2C.h @@ -0,0 +1,37 @@ +/*! + * @file WipperSnapper_I2C.h + * + * This file provides functions for interfacing + * with the I2C bus. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#ifndef WIPPERSNAPPER_I2C_H +#define WIPPERSNAPPER_I2C_H + +#include "Wippersnapper.h" + +// forward decl. +class Wippersnapper; + +class WipperSnapper_I2C() { + public: + WipperSnapper_I2C(uint32_t frequency); + ~WipperSnapper_I2C(); + bool scanForAddress(uint32_t address); + bool setFrequency(uint32_t frequency); + + private: + //TwoWire *_i2c = NULL; /*!< Default Arduino I2C bus */ + +}; +extern Wippersnapper WS; + +#endif //WIPPERSNAPPER_I2C_H \ No newline at end of file From 975d8f8f2bb1c3517b1c112b304d7c6627dd494d Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 11:19:31 -0400 Subject: [PATCH 04/95] add init, validate pullups on sda/scl --- src/components/WipperSnapper_I2C.cpp | 21 +++++++++++++++++++-- src/components/WipperSnapper_I2C.h | 8 ++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/WipperSnapper_I2C.cpp b/src/components/WipperSnapper_I2C.cpp index 4c14ff6ec..ce21aa3c5 100644 --- a/src/components/WipperSnapper_I2C.cpp +++ b/src/components/WipperSnapper_I2C.cpp @@ -16,8 +16,25 @@ #include "WipperSnapper_I2C.h" -WipperSnapper_I2C::WipperSnapper_I2C(uint32_t frequency) { - // TODO - init! +WipperSnapper_I2C::WipperSnapper_I2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId) { + // check if sda/scl have pullups enabled + if (digitalRead(sdaPin) == LOW) { + pinMode(sdaPin, INPUT_PULLUP); + } + if (digitalRead(sclPin) == LOW) { + pinMode(sclPin, INPUT_PULLUP); + } + + // Initialize default I2C bus (busId = 1) + if (busId == 1 && isInitI2COne = false) { + _i2cOne->begin(sdaPin, sclPin, frequency); + } // Initialize second I2C bus (busId = 2) + else if (busId == 1 && isInitI2CTwo = false) { + _i2cTwo->begin(sdaPin, sclPin, frequency); + } else { + WS_DEBUG_PRINTLN("Skipping initialization, I2C bus already configured."); + } + } ~WipperSnapper_I2C::WipperSnapper_I2C() { diff --git a/src/components/WipperSnapper_I2C.h b/src/components/WipperSnapper_I2C.h index 2887a49c0..3ee1a0e62 100644 --- a/src/components/WipperSnapper_I2C.h +++ b/src/components/WipperSnapper_I2C.h @@ -17,19 +17,23 @@ #define WIPPERSNAPPER_I2C_H #include "Wippersnapper.h" +#include // forward decl. class Wippersnapper; class WipperSnapper_I2C() { public: - WipperSnapper_I2C(uint32_t frequency); + WipperSnapper_I2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId); ~WipperSnapper_I2C(); bool scanForAddress(uint32_t address); bool setFrequency(uint32_t frequency); private: - //TwoWire *_i2c = NULL; /*!< Default Arduino I2C bus */ + TwoWire *_i2cOne; + TwoWire *_i2cTwo; + bool isInitI2COne; // True if I2C w/busId 1 already initialized + bool isInitI2CTwo; // True if I2C w/busId 2 already initialized }; extern Wippersnapper WS; From 27d82cef84b74cb4aaa0d4f607e183056a09126a Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 12:11:59 -0400 Subject: [PATCH 05/95] add I2C as component using device-driver-component model --- .../WipperSnapper_Component_I2C.cpp | 49 ++++++++++++++++++ src/components/WipperSnapper_Component_I2C.h | 40 +++++++++++++++ src/components/WipperSnapper_I2C.cpp | 50 ------------------- src/components/WipperSnapper_I2C.h | 41 --------------- 4 files changed, 89 insertions(+), 91 deletions(-) create mode 100644 src/components/WipperSnapper_Component_I2C.cpp create mode 100644 src/components/WipperSnapper_Component_I2C.h delete mode 100644 src/components/WipperSnapper_I2C.cpp delete mode 100644 src/components/WipperSnapper_I2C.h diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp new file mode 100644 index 000000000..7224f8a86 --- /dev/null +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -0,0 +1,49 @@ +/*! + * @file Wippersnapper_Component_I2C.cpp + * + * This component initiates I2C operations + * using the Arduino generic TwoWire driver. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ + +#include "WipperSnapper_Component_I2C.h" + +WipperSnapper_Component_I2C::WipperSnapper_Component_I2C() { + _i2c= &Wire; + // TODO: allow 2nd bus to be used for ESP32 +} + +~WipperSnapper_Component_I2C::WipperSnapper_Component_I2C() { + // todo - DTOR! +} + +bool WipperSnapper_Component_I2C::initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId) { + // validate if pins are pulled HI + if (digitalRead(sdaPin) == LOW) { + pinMode(sdaPin, INPUT_PULLUP); + } + if (digitalRead(sclPin) == LOW) { + pinMode(sclPin, INPUT_PULLUP); + } + // init bus + _i2c.begin(sdaPin, sclPin); + _i2c.setClock(frequency); + _i2cInitialized = true; + yield(); +} + +bool WipperSnapper_Component_I2C::scanForAddress(uint32_t address) { + // TODO: scanner code +} + +bool WipperSnapper_Component_I2C::setFrequency(uint32_t frequency, int32 busId) { + // TODO! +} diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h new file mode 100644 index 000000000..20045cda8 --- /dev/null +++ b/src/components/WipperSnapper_Component_I2C.h @@ -0,0 +1,40 @@ +/*! + * @file WipperSnapper_Component_I2C.h + * + * This component initiates I2C operations + * using the Arduino generic TwoWire driver. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#ifndef WipperSnapper_Component_I2C_H +#define WipperSnapper_Component_I2C_H + +#include "Wippersnapper.h" +#include + +// forward decl. +class Wippersnapper; + +class WipperSnapper_Component_I2C() { + public: + WipperSnapper_Component_I2C(); + ~WipperSnapper_Component_I2C(); + void initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId); + bool scanForAddress(uint32_t address); + bool setFrequency(uint32_t frequency, int32_t busId); + + private: + TwoWire *_i2c; + bool _i2cInitialized; + +}; +extern Wippersnapper WS; + +#endif //WipperSnapper_Component_I2C_H \ No newline at end of file diff --git a/src/components/WipperSnapper_I2C.cpp b/src/components/WipperSnapper_I2C.cpp deleted file mode 100644 index ce21aa3c5..000000000 --- a/src/components/WipperSnapper_I2C.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/*! - * @file Wippersnapper_I2C.cpp - * - * This file provides functions for interfacing - * with the I2C bus. - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ - -#include "WipperSnapper_I2C.h" - -WipperSnapper_I2C::WipperSnapper_I2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId) { - // check if sda/scl have pullups enabled - if (digitalRead(sdaPin) == LOW) { - pinMode(sdaPin, INPUT_PULLUP); - } - if (digitalRead(sclPin) == LOW) { - pinMode(sclPin, INPUT_PULLUP); - } - - // Initialize default I2C bus (busId = 1) - if (busId == 1 && isInitI2COne = false) { - _i2cOne->begin(sdaPin, sclPin, frequency); - } // Initialize second I2C bus (busId = 2) - else if (busId == 1 && isInitI2CTwo = false) { - _i2cTwo->begin(sdaPin, sclPin, frequency); - } else { - WS_DEBUG_PRINTLN("Skipping initialization, I2C bus already configured."); - } - -} - -~WipperSnapper_I2C::WipperSnapper_I2C() { - // todo - DTOR! -} - -bool WipperSnapper_I2C::scanForAddress(uint32_t address) { - // TODO: scanner code -} - -bool WipperSnapper_I2C::setFrequency(uint32_t frequency) { - // TODO: frequency setter code -} diff --git a/src/components/WipperSnapper_I2C.h b/src/components/WipperSnapper_I2C.h deleted file mode 100644 index 3ee1a0e62..000000000 --- a/src/components/WipperSnapper_I2C.h +++ /dev/null @@ -1,41 +0,0 @@ -/*! - * @file WipperSnapper_I2C.h - * - * This file provides functions for interfacing - * with the I2C bus. - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ -#ifndef WIPPERSNAPPER_I2C_H -#define WIPPERSNAPPER_I2C_H - -#include "Wippersnapper.h" -#include - -// forward decl. -class Wippersnapper; - -class WipperSnapper_I2C() { - public: - WipperSnapper_I2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId); - ~WipperSnapper_I2C(); - bool scanForAddress(uint32_t address); - bool setFrequency(uint32_t frequency); - - private: - TwoWire *_i2cOne; - TwoWire *_i2cTwo; - bool isInitI2COne; // True if I2C w/busId 1 already initialized - bool isInitI2CTwo; // True if I2C w/busId 2 already initialized - -}; -extern Wippersnapper WS; - -#endif //WIPPERSNAPPER_I2C_H \ No newline at end of file From 5f4fc86036c8d6c7023752136357710958d1606c Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 12:17:01 -0400 Subject: [PATCH 06/95] allow multibus init --- .gitignore | 3 ++- src/.vscode/settings.json | 3 +++ src/components/WipperSnapper_Component_I2C.cpp | 14 ++++++++++---- src/components/WipperSnapper_Component_I2C.h | 6 ++++-- 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 src/.vscode/settings.json diff --git a/.gitignore b/.gitignore index 5746eb4e2..b3fd5db17 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,5 @@ *.py # VSCode artifacts -./vscode/* \ No newline at end of file +./vscode/* +src/.vscode/settings.json \ No newline at end of file diff --git a/src/.vscode/settings.json b/src/.vscode/settings.json new file mode 100644 index 000000000..06b08423f --- /dev/null +++ b/src/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.dimInactiveRegions": false +} \ No newline at end of file diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 7224f8a86..8e7ea0a51 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -16,16 +16,22 @@ #include "WipperSnapper_Component_I2C.h" -WipperSnapper_Component_I2C::WipperSnapper_Component_I2C() { - _i2c= &Wire; - // TODO: allow 2nd bus to be used for ESP32 +WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t busId) { + if (_isInitBus0 == false) { + _i2c0 = new TwoWire(busId); + _isInitBus0 = true; + } + if (_isInitBus1 == false) { + _i2c1 = new TwoWire(busId); + _isInitBus1 = true; + } } ~WipperSnapper_Component_I2C::WipperSnapper_Component_I2C() { // todo - DTOR! } -bool WipperSnapper_Component_I2C::initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId) { +bool WipperSnapper_Component_I2C::initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin) { // validate if pins are pulled HI if (digitalRead(sdaPin) == LOW) { pinMode(sdaPin, INPUT_PULLUP); diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 20045cda8..ec4f51681 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -31,8 +31,10 @@ class WipperSnapper_Component_I2C() { bool setFrequency(uint32_t frequency, int32_t busId); private: - TwoWire *_i2c; - bool _i2cInitialized; + TwoWire *_i2c0; + TwoWire *_i2c1; + bool _isInitBus0; + bool _isInitBus1; }; extern Wippersnapper WS; From 4692dda50a60e9bf7bc3ccc8c5671d13ba784cde Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 13:52:30 -0400 Subject: [PATCH 07/95] small changes --- src/components/WipperSnapper_Component_I2C.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 8e7ea0a51..1664872d2 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -17,6 +17,7 @@ #include "WipperSnapper_Component_I2C.h" WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t busId) { + // instead of initing twowire, just set the adapter's ID? if (_isInitBus0 == false) { _i2c0 = new TwoWire(busId); _isInitBus0 = true; @@ -32,7 +33,7 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t busId) { } bool WipperSnapper_Component_I2C::initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin) { - // validate if pins are pulled HI +/* // validate if pins are pulled HI if (digitalRead(sdaPin) == LOW) { pinMode(sdaPin, INPUT_PULLUP); } @@ -43,7 +44,7 @@ bool WipperSnapper_Component_I2C::initI2C(uint32_t frequency, int32_t sdaPin, in _i2c.begin(sdaPin, sclPin); _i2c.setClock(frequency); _i2cInitialized = true; - yield(); + yield(); */ } bool WipperSnapper_Component_I2C::scanForAddress(uint32_t address) { From 40e5e27c1fe186e26443aee5a1737788cd1aa4d7 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 13:54:47 -0400 Subject: [PATCH 08/95] change smever format --- src/Wippersnapper.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 0c72cb517..40e34cc11 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -63,7 +63,7 @@ #endif // Library version (semver-formatted) -#define WS_VERSION "1.0.0-beta+3" +#define WS_VERSION "1.0.0-beta.3" // Reserved Adafruit IO MQTT topics #define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic From edbbbeead05689df0abe5c87dd071d1276e950af Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 16:20:54 -0400 Subject: [PATCH 09/95] link component-class, initialize --- src/Wippersnapper.cpp | 4 +++ src/Wippersnapper.h | 8 +++++ .../WipperSnapper_Component_I2C.cpp | 36 +++++++------------ src/components/WipperSnapper_Component_I2C.h | 13 +++---- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index b255cb0bc..d8fccf67e 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1042,6 +1042,10 @@ ws_status_t Wippersnapper::run() { // Process all incoming packets from Wippersnapper MQTT Broker WS._mqtt->processPackets(10); + // TODO: Implement correct I2C when we have topic added + // for now, this is mocking the initialization assuming we got a message + _i2cPort0 = new WipperSnapper_Component_I2C(34, 33); + // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 40e34cc11..7df56b741 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -37,6 +37,9 @@ #include "Wippersnapper_AnalogIO.h" #include "Wippersnapper_DigitalGPIO.h" +// WipperSnapper I2C Component +#include "components/WipperSnapper_Component_I2C.h" + // External libraries #include "Adafruit_MQTT.h" // MQTT Client #include "Arduino.h" // Wiring @@ -149,6 +152,7 @@ class Wippersnapper_DigitalGPIO; class Wippersnapper_AnalogIO; class Wippersnapper_FS; class Wippersnapper_ESP32_nvs; +class WipperSnapper_Component_I2C; /**************************************************************************/ /*! @@ -238,6 +242,10 @@ class Wippersnapper { Wippersnapper_FS *_fileSystem; ///< Instance of filesystem class Wippersnapper_ESP32_nvs *_nvs; ///< Instance of nvs + // I2C WIP - dirty! + WipperSnapper_Component_I2C *_i2cPort0 = NULL; + WipperSnapper_Component_I2C *_i2cPort1 = NULL; + uint8_t _uid[6]; /*!< Unique network iface identifier */ char sUID[9]; /*!< Unique network iface identifier */ const char *_boardId; /*!< Adafruit IO+ board string */ diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 1664872d2..9003641c1 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -16,41 +16,31 @@ #include "WipperSnapper_Component_I2C.h" -WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t busId) { - // instead of initing twowire, just set the adapter's ID? - if (_isInitBus0 == false) { - _i2c0 = new TwoWire(busId); - _isInitBus0 = true; - } - if (_isInitBus1 == false) { - _i2c1 = new TwoWire(busId); - _isInitBus1 = true; - } -} - -~WipperSnapper_Component_I2C::WipperSnapper_Component_I2C() { - // todo - DTOR! -} +WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, int32_t portNum, uint32_t frequency) { + // initialize using portNum + _i2c = new TwoWire(int32_t portNum); -bool WipperSnapper_Component_I2C::initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin) { -/* // validate if pins are pulled HI + // validate if pins are pulled HI if (digitalRead(sdaPin) == LOW) { pinMode(sdaPin, INPUT_PULLUP); } if (digitalRead(sclPin) == LOW) { pinMode(sclPin, INPUT_PULLUP); } - // init bus + // begin i2c _i2c.begin(sdaPin, sclPin); _i2c.setClock(frequency); - _i2cInitialized = true; - yield(); */ + _isInit = true; + yield(); +} + +WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { + // todo - DTOR! } bool WipperSnapper_Component_I2C::scanForAddress(uint32_t address) { + bool is_success = false; // TODO: scanner code + return is_success; } -bool WipperSnapper_Component_I2C::setFrequency(uint32_t frequency, int32 busId) { - // TODO! -} diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index ec4f51681..53675c96d 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -22,20 +22,15 @@ // forward decl. class Wippersnapper; -class WipperSnapper_Component_I2C() { +class WipperSnapper_Component_I2C { public: - WipperSnapper_Component_I2C(); + WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, int32_t portNum=0, uint32_t frequency=100000); ~WipperSnapper_Component_I2C(); - void initI2C(uint32_t frequency, int32_t sdaPin, int32_t sclPin, int32_t busId); bool scanForAddress(uint32_t address); - bool setFrequency(uint32_t frequency, int32_t busId); private: - TwoWire *_i2c0; - TwoWire *_i2c1; - bool _isInitBus0; - bool _isInitBus1; - + TwoWire *_i2c = NULL; + bool _isInit; }; extern Wippersnapper WS; From 4da1913b157033b2432e298eaea61838b42329f3 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 16:21:37 -0400 Subject: [PATCH 10/95] ptr --- src/components/WipperSnapper_Component_I2C.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 9003641c1..6a129258c 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -18,7 +18,7 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, int32_t portNum, uint32_t frequency) { // initialize using portNum - _i2c = new TwoWire(int32_t portNum); + _i2c = new TwoWire(portNum); // validate if pins are pulled HI if (digitalRead(sdaPin) == LOW) { @@ -28,8 +28,8 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, int32_t pinMode(sclPin, INPUT_PULLUP); } // begin i2c - _i2c.begin(sdaPin, sclPin); - _i2c.setClock(frequency); + _i2c->begin(sdaPin, sclPin); + _i2c->setClock(frequency); _isInit = true; yield(); } From 0a132d4ae582d4d0d8095decc2bb7dac91724d96 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 16:48:16 -0400 Subject: [PATCH 11/95] prototype of vectord i2c components --- src/Wippersnapper.cpp | 7 ++- src/Wippersnapper.h | 6 ++- .../WipperSnapper_Component_I2C.cpp | 44 ++++++++++--------- src/components/WipperSnapper_Component_I2C.h | 19 ++++---- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index d8fccf67e..a233fefc6 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -755,7 +755,8 @@ void Wippersnapper::subscribeWSTopics() { _topic_signal_brkr_sub->setCallback(cbSignalTopic); // Subscribe to signal's I2C sub-topic - _topic_signal_i2c_sub = new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_signal_i2c_brkr, 1); + _topic_signal_i2c_sub = + new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_signal_i2c_brkr, 1); WS._mqtt->subscribe(_topic_signal_i2c_sub); _topic_signal_i2c_sub->setCallback(cbSignalI2CReq); @@ -1044,7 +1045,11 @@ ws_status_t Wippersnapper::run() { // TODO: Implement correct I2C when we have topic added // for now, this is mocking the initialization assuming we got a message + + // New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) _i2cPort0 = new WipperSnapper_Component_I2C(34, 33); + // add new port to vector of i2c components + i2cComponents.push_back(_i2cPort0); // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 7df56b741..1e895c193 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -18,6 +18,8 @@ #ifndef WIPPERSNAPPER_H #define WIPPERSNAPPER_H +#include + // Nanopb dependencies #include #include @@ -243,6 +245,7 @@ class Wippersnapper { Wippersnapper_ESP32_nvs *_nvs; ///< Instance of nvs // I2C WIP - dirty! + std::vector i2cComponents; WipperSnapper_Component_I2C *_i2cPort0 = NULL; WipperSnapper_Component_I2C *_i2cPort1 = NULL; @@ -306,8 +309,7 @@ class Wippersnapper { Adafruit_MQTT_Subscribe *_topic_signal_brkr_sub; /*!< Subscription for C2D signal topic. */ Adafruit_MQTT_Subscribe - *_topic_signal_i2c_sub; /*!< Subscribes to signal's I2C topic. */ - + *_topic_signal_i2c_sub; /*!< Subscribes to signal's I2C topic. */ char *_err_topic; /*!< Adafruit IO MQTT error message topic. */ char *_throttle_topic; /*!< Adafruit IO MQTT throttle message topic. */ diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 6a129258c..d4a12b4c6 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -16,31 +16,35 @@ #include "WipperSnapper_Component_I2C.h" -WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, int32_t portNum, uint32_t frequency) { - // initialize using portNum - _i2c = new TwoWire(portNum); +WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, + int32_t sclPin, + int32_t portNum, + uint32_t frequency) { + // initialize using portNum + _i2c = new TwoWire(portNum); - // validate if pins are pulled HI - if (digitalRead(sdaPin) == LOW) { - pinMode(sdaPin, INPUT_PULLUP); - } - if (digitalRead(sclPin) == LOW) { - pinMode(sclPin, INPUT_PULLUP); - } - // begin i2c - _i2c->begin(sdaPin, sclPin); - _i2c->setClock(frequency); - _isInit = true; - yield(); + // validate if pins are pulled HI + if (digitalRead(sdaPin) == LOW) { + pinMode(sdaPin, INPUT_PULLUP); + } + if (digitalRead(sclPin) == LOW) { + pinMode(sclPin, INPUT_PULLUP); + } + // begin i2c + _i2c->begin(sdaPin, sclPin); + _i2c->setClock(frequency); + _portNum = portNum; + _isInit = true; + yield(); } WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { - // todo - DTOR! + _portNum = NULL; + _isInit = false; } bool WipperSnapper_Component_I2C::scanForAddress(uint32_t address) { - bool is_success = false; - // TODO: scanner code - return is_success; + bool is_success = false; + // TODO: scanner code + return is_success; } - diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 53675c96d..4cd24434b 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -23,15 +23,18 @@ class Wippersnapper; class WipperSnapper_Component_I2C { - public: - WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, int32_t portNum=0, uint32_t frequency=100000); - ~WipperSnapper_Component_I2C(); - bool scanForAddress(uint32_t address); +public: + WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, + int32_t portNum = 0, uint32_t frequency = 100000); + ~WipperSnapper_Component_I2C(); + bool scanForAddress(uint32_t address); - private: - TwoWire *_i2c = NULL; - bool _isInit; + int32_t _portNum; + bool _isInit; + +private: + TwoWire *_i2c = NULL; }; extern Wippersnapper WS; -#endif //WipperSnapper_Component_I2C_H \ No newline at end of file +#endif // WipperSnapper_Component_I2C_H \ No newline at end of file From 935ccbd303a16a3cb23d333eed78dc7ae57ebe74 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 19 Jul 2021 17:06:36 -0400 Subject: [PATCH 12/95] add WDT lib includes for ESP32 and SAMD51 --- src/Wippersnapper.h | 9 +++++++++ src/components/WipperSnapper_Component_I2C.cpp | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 1e895c193..614bbb456 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -45,6 +45,15 @@ // External libraries #include "Adafruit_MQTT.h" // MQTT Client #include "Arduino.h" // Wiring + +#if defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || ADAFRUIT_PYPORTAL + #include +#else // ESP32/ESP32-S2 + #include +#endif + + +// Note: These might be better off in their respective wrappers #include #include #include diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index d4a12b4c6..fad0d7614 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -39,7 +39,7 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, } WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { - _portNum = NULL; + _portNum = 100; // Invalid = 100 _isInit = false; } From 5346566315cd5e92b0d000a89c6ff917569aead9 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 20 Jul 2021 12:08:20 -0400 Subject: [PATCH 13/95] add i2c port0/port1 to vector of components --- src/Wippersnapper.cpp | 19 +++++++++++++++---- src/Wippersnapper.h | 9 ++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index a233fefc6..c1b066902 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1046,10 +1046,8 @@ ws_status_t Wippersnapper::run() { // TODO: Implement correct I2C when we have topic added // for now, this is mocking the initialization assuming we got a message - // New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) - _i2cPort0 = new WipperSnapper_Component_I2C(34, 33); - // add new port to vector of i2c components - i2cComponents.push_back(_i2cPort0); + // Message: New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) + addNewI2CComponent(34, 33); // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); @@ -1060,6 +1058,19 @@ ws_status_t Wippersnapper::run() { return status(); } +bool Wippersnapper::addNewI2CComponent(int32_t sdaPin, int32_t sclPin, + int32_t portNum, uint32_t frequency) { + if (_i2cPort0 != NULL && portNum == 0) { + _i2cPort0 = new WipperSnapper_Component_I2C(sdaPin, sclPin, portNum); + i2cComponents.push_back(_i2cPort0); + } else if (_i2cPort1 != NULL && portNum == 1) { + _i2cPort1 = new WipperSnapper_Component_I2C(sdaPin, sclPin, portNum); + i2cComponents.push_back(_i2cPort1); + } else { + WS_DEBUG_PRINTLN("Unable to allocate new I2C Component") + } +} + /**************************************************************************/ /*! @brief Sends board description message to Wippersnapper diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 614bbb456..4a704f273 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -47,12 +47,11 @@ #include "Arduino.h" // Wiring #if defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || ADAFRUIT_PYPORTAL - #include +#include #else // ESP32/ESP32-S2 - #include +#include #endif - // Note: These might be better off in their respective wrappers #include #include @@ -237,6 +236,10 @@ class Wippersnapper { // Pin configure message bool configurePinRequest(wippersnapper_pin_v1_ConfigurePinRequest *pinMsg); + // I2C + bool addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, + uint32_t frequency = 100000U); + uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback payload */ uint8_t From f7797c470bc7d18f686bae30745a1d48f241d2e1 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 20 Jul 2021 12:32:49 -0400 Subject: [PATCH 14/95] wip event-loop for i2c --- src/Wippersnapper.cpp | 8 ++++++-- src/Wippersnapper.h | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index c1b066902..86d76a424 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -415,8 +415,12 @@ void cbSignalI2CReq(char *data, uint16_t len) { memcpy(WS._buffer, data, len); WS.bufSize = len; + // Detect tag? // Empty struct for storing the I2C request message - // TODO: Decoding cbs here or pass into i2c functions + // DEPENDING on tag, alloc + // Note: setup events get pushed to the front of the deque + WS.wsEvents.push_front(wsEventSetupI2C); + } /**************************************************************************/ @@ -1047,7 +1051,7 @@ ws_status_t Wippersnapper::run() { // for now, this is mocking the initialization assuming we got a message // Message: New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) - addNewI2CComponent(34, 33); + //addNewI2CComponent(34, 33); // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 4a704f273..6c27aecfd 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -19,6 +19,7 @@ #define WIPPERSNAPPER_H #include +#include // Nanopb dependencies #include @@ -149,6 +150,13 @@ typedef enum { WS_BOARD_DEF_UNSPECIFIED } ws_board_status_t; +// WIP - event-driven loop for i2c +enum wsEvent +{ + wsEventSetupI2C, + wsEventScanI2C, +}; + /* MQTT Configuration */ #define WS_KEEPALIVE_INTERVAL 4 ///< Session keepalive interval time, in seconds #define WS_KEEPALIVE_INTERVAL_MS \ @@ -239,6 +247,7 @@ class Wippersnapper { // I2C bool addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, uint32_t frequency = 100000U); + std::deque wsEvents; // global accessed by the object. uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback payload */ From eb8c86eb590c1e3b2471ac914b4eef78c00dd0f5 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 20 Jul 2021 16:56:03 -0400 Subject: [PATCH 15/95] add msg to properties --- src/Wippersnapper.cpp | 6 ++++++ src/Wippersnapper.h | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 86d76a424..953f54fcb 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -415,6 +415,12 @@ void cbSignalI2CReq(char *data, uint16_t len) { memcpy(WS._buffer, data, len); WS.bufSize = len; + // Clear existing incoming I2C signal message + WS.msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; + + // Set event to setupI2C, front since its a setup event + WS.wsEvents.push_front() + // Detect tag? // Empty struct for storing the I2C request message // DEPENDING on tag, alloc diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 6c27aecfd..520fb0bdc 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -150,7 +150,7 @@ typedef enum { WS_BOARD_DEF_UNSPECIFIED } ws_board_status_t; -// WIP - event-driven loop for i2c +// WIP - event-driven loop handler for i2c enum wsEvent { wsEventSetupI2C, @@ -294,6 +294,10 @@ class Wippersnapper { wippersnapper_signal_v1_CreateSignalRequest _incomingSignalMsg; /*!< Incoming signal message from broker */ + // i2c signal msg + wippersnapper_signal_v1_I2CRequest msgSignalI2C; + + char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in milliseconds. */ From 472058073474734711777479db952ce802014828 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 20 Jul 2021 17:20:29 -0400 Subject: [PATCH 16/95] add cb for detecting type, decoding signal i2c msg, link it into the main event loop --- src/Wippersnapper.cpp | 42 +++++++++++++++++++++++++++++++----------- src/Wippersnapper.h | 13 ++++++------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 953f54fcb..7b0001d92 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -415,18 +415,30 @@ void cbSignalI2CReq(char *data, uint16_t len) { memcpy(WS._buffer, data, len); WS.bufSize = len; - // Clear existing incoming I2C signal message - WS.msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; + // Clear (zero-out) existing incoming I2C signal message + // TODO: uncomment this? + // WS.msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; - // Set event to setupI2C, front since its a setup event - WS.wsEvents.push_front() + // Set event to decode the I2C signal, push_front because decoder requires + // buffer contents + WS.wsEvents.push_front(wsEventDecodeSignalMsgI2C); +} + +void cbSignalI2C() { + // detec ttype +} - // Detect tag? - // Empty struct for storing the I2C request message - // DEPENDING on tag, alloc - // Note: setup events get pushed to the front of the deque - WS.wsEvents.push_front(wsEventSetupI2C); +void setupDecodeSignalMsgI2C() { + /* + WS.msgSignalI2C.cb_payload.funcs.decode = cbSignalI2C; + // Decode the I2C Signal Message + pb_istream_t stream = pb_istream_from_buffer(WS._buffer, WS.bufSize); + if (!pb_decode(&stream, wippersnapper_signal_v1_I2CRequest_fields, + WS.msgSignalI2C)) { + WS_DEBUG_PRINTLN( + "ERROR (decodeSignalMsg):, Could not decode CreateSignalRequest") + } */ } /**************************************************************************/ @@ -1057,7 +1069,15 @@ ws_status_t Wippersnapper::run() { // for now, this is mocking the initialization assuming we got a message // Message: New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) - //addNewI2CComponent(34, 33); + // addNewI2CComponent(34, 33); + + // Event processing loop + switch (wsEvents.front()) { + case wsEventDecodeSignalMsgI2C: + // Priority - HIGH + setupDecodeSignalMsgI2C(); + break; + } // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); @@ -1068,7 +1088,7 @@ ws_status_t Wippersnapper::run() { return status(); } -bool Wippersnapper::addNewI2CComponent(int32_t sdaPin, int32_t sclPin, +void Wippersnapper::addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum, uint32_t frequency) { if (_i2cPort0 != NULL && portNum == 0) { _i2cPort0 = new WipperSnapper_Component_I2C(sdaPin, sclPin, portNum); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 520fb0bdc..bb58cbb60 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -18,8 +18,8 @@ #ifndef WIPPERSNAPPER_H #define WIPPERSNAPPER_H -#include #include +#include // Nanopb dependencies #include @@ -151,10 +151,10 @@ typedef enum { } ws_board_status_t; // WIP - event-driven loop handler for i2c -enum wsEvent -{ - wsEventSetupI2C, - wsEventScanI2C, +enum wsEvent { + wsEventDecodeSignalMsgI2C, + wsEventSetupI2C, + wsEventScanI2C, }; /* MQTT Configuration */ @@ -245,7 +245,7 @@ class Wippersnapper { bool configurePinRequest(wippersnapper_pin_v1_ConfigurePinRequest *pinMsg); // I2C - bool addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, + void addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, uint32_t frequency = 100000U); std::deque wsEvents; // global accessed by the object. @@ -297,7 +297,6 @@ class Wippersnapper { // i2c signal msg wippersnapper_signal_v1_I2CRequest msgSignalI2C; - char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in milliseconds. */ From 0f5f3b49b748b8cee6e5c2483481f9b4bad687af Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 21 Jul 2021 12:09:26 -0400 Subject: [PATCH 17/95] decode signal message --- src/Wippersnapper.cpp | 28 +++++++++++----------------- src/Wippersnapper.h | 5 +++++ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 7b0001d92..6dddb60ee 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -415,30 +415,24 @@ void cbSignalI2CReq(char *data, uint16_t len) { memcpy(WS._buffer, data, len); WS.bufSize = len; - // Clear (zero-out) existing incoming I2C signal message - // TODO: uncomment this? - // WS.msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; - - // Set event to decode the I2C signal, push_front because decoder requires - // buffer contents + // Set next event to decode the I2C signal WS.wsEvents.push_front(wsEventDecodeSignalMsgI2C); } +// TODO: Do these need to be void we can access the buffer from WS._ anyways? void cbSignalI2C() { - // detec ttype + // detect field/type } -void setupDecodeSignalMsgI2C() { - /* - WS.msgSignalI2C.cb_payload.funcs.decode = cbSignalI2C; +void Wippersnapper::setupDecodeSignalMsgI2C() { + // Zero-out existing I2C incoming signal msg. + msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; + // Decode buffer into msgSignalI2C + pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); + if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, &msgSignalI2C)) + WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); - // Decode the I2C Signal Message - pb_istream_t stream = pb_istream_from_buffer(WS._buffer, WS.bufSize); - if (!pb_decode(&stream, wippersnapper_signal_v1_I2CRequest_fields, - WS.msgSignalI2C)) { - WS_DEBUG_PRINTLN( - "ERROR (decodeSignalMsg):, Could not decode CreateSignalRequest") - } */ + // Query which payload? } /**************************************************************************/ diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index bb58cbb60..174530033 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -247,7 +247,12 @@ class Wippersnapper { // I2C void addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, uint32_t frequency = 100000U); + + void setupDecodeSignalMsgI2C(); + + // Event-loop std::deque wsEvents; // global accessed by the object. + uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback payload */ From 303996b6736d03546bd6ddff497437a351a3966f Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 21 Jul 2021 12:28:50 -0400 Subject: [PATCH 18/95] further work on i2c decode --- src/Wippersnapper.cpp | 39 +++++++++++++++++++++++++++------------ src/Wippersnapper.h | 15 ++++++++------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 6dddb60ee..c80c7ff15 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -416,23 +416,36 @@ void cbSignalI2CReq(char *data, uint16_t len) { WS.bufSize = len; // Set next event to decode the I2C signal - WS.wsEvents.push_front(wsEventDecodeSignalMsgI2C); + WS.wsEventDecodeMsgs.push_front(wsEventDecodeMsgSignalI2C); } -// TODO: Do these need to be void we can access the buffer from WS._ anyways? -void cbSignalI2C() { - // detect field/type -} - -void Wippersnapper::setupDecodeSignalMsgI2C() { +/**************************************************************************/ +/*! + @brief Called when i2c signal sub-topic receives a new message. + Fills a shared buffer with data from payload. +*/ +/**************************************************************************/ +void Wippersnapper::decodeMsgSignalI2C() { // Zero-out existing I2C incoming signal msg. msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; // Decode buffer into msgSignalI2C pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, &msgSignalI2C)) WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); + // Query the payload type + WS_DEBUG_PRINT("I2C Payload Type: "); + WS_DEBUG_PRINTLN(msgSignalI2C.which_payload); + // NOTE: Two event types? Hardware events and Message events? + // Order of processing: poll-> Message (decode, get to the last step) -> hardware event + // Set hardware event based on payload type + if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CInitRequest) { + // Call something which will the I2C bus here? Add an i2c component as well? + WS_DEBUG_PRINTLN("Found: I2C Init Request Msg"); + } else if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CScanRequest) { + // init the I2C scan here? + WS_DEBUG_PRINTLN("Found: I2C Scan Request Msg"); + } - // Query which payload? } /**************************************************************************/ @@ -1065,14 +1078,16 @@ ws_status_t Wippersnapper::run() { // Message: New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) // addNewI2CComponent(34, 33); - // Event processing loop - switch (wsEvents.front()) { - case wsEventDecodeSignalMsgI2C: + // Message event processing loop + switch (wsEventDecodeMsgs.front()) { + case wsEventDecodeMsgSignalI2C: // Priority - HIGH - setupDecodeSignalMsgI2C(); + decodeMsgSignalI2C(); break; } + // Hardware event processing loop + // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 174530033..384e6d1fd 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -151,10 +151,10 @@ typedef enum { } ws_board_status_t; // WIP - event-driven loop handler for i2c -enum wsEvent { - wsEventDecodeSignalMsgI2C, - wsEventSetupI2C, - wsEventScanI2C, +// Events for decoding incoming messages +enum wsEventDecodeMsg { + wsEventDecodeMsgSignal, + wsEventDecodeMsgSignalI2C, }; /* MQTT Configuration */ @@ -247,11 +247,12 @@ class Wippersnapper { // I2C void addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, uint32_t frequency = 100000U); - - void setupDecodeSignalMsgI2C(); + + // Decoder for i2c signal incoming + void decodeMsgSignalI2C(); // Event-loop - std::deque wsEvents; // global accessed by the object. + std::deque wsEventDecodeMsgs; // global accessed by the object. uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback From 7ac647fd3ccd778da0c29a568f97bfc38ffca043 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 21 Jul 2021 12:49:00 -0400 Subject: [PATCH 19/95] Digital/AnalogGPIO Patch - reset all values in pin array when done using it --- src/Wippersnapper.cpp | 4 ++-- src/Wippersnapper.h | 2 +- src/Wippersnapper_AnalogIO.cpp | 1 + src/Wippersnapper_DigitalGPIO.cpp | 6 ++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index c80c7ff15..c902e15bc 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -438,13 +438,13 @@ void Wippersnapper::decodeMsgSignalI2C() { // NOTE: Two event types? Hardware events and Message events? // Order of processing: poll-> Message (decode, get to the last step) -> hardware event // Set hardware event based on payload type - if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CInitRequest) { +/* if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CInitRequest) { // Call something which will the I2C bus here? Add an i2c component as well? WS_DEBUG_PRINTLN("Found: I2C Init Request Msg"); } else if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CScanRequest) { // init the I2C scan here? WS_DEBUG_PRINTLN("Found: I2C Scan Request Msg"); - } + } */ } diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 384e6d1fd..bdf1e63fd 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -60,7 +60,7 @@ #include // Uncomment for staging builds -// #define USE_STAGING +#define USE_STAGING #ifdef USE_STAGING #define IO_MQTT_SERVER "io.adafruit.us" diff --git a/src/Wippersnapper_AnalogIO.cpp b/src/Wippersnapper_AnalogIO.cpp index f98bbd686..612c241b5 100644 --- a/src/Wippersnapper_AnalogIO.cpp +++ b/src/Wippersnapper_AnalogIO.cpp @@ -118,6 +118,7 @@ void Wippersnapper_AnalogIO::deinitAnalogInputPinObj(int pin) { // de-allocate the pin within digital_input_pins[] for (int i = 0; i < _totalAnalogInputPins; i++) { if (_analog_input_pins[i].pinName == pin) { + _analog_input_pins[i].pinName = 0; _analog_input_pins[i].period = -1; _analog_input_pins[i].prvPinVal = 0.0; _analog_input_pins[i].prvPeriod = 0L; diff --git a/src/Wippersnapper_DigitalGPIO.cpp b/src/Wippersnapper_DigitalGPIO.cpp index 0a4f99a6c..2c700a22e 100644 --- a/src/Wippersnapper_DigitalGPIO.cpp +++ b/src/Wippersnapper_DigitalGPIO.cpp @@ -29,7 +29,10 @@ Wippersnapper_DigitalGPIO::Wippersnapper_DigitalGPIO( _digital_input_pins = new digitalInputPin[_totalDigitalInputPins]; // turn input sampling off for all digital pins for (int i = 0; i < _totalDigitalInputPins; i++) { + _digital_input_pins[i].pinName = -1; _digital_input_pins[i].period = -1; + _digital_input_pins[i].prvPeriod = 0L; + _digital_input_pins[i].prvPinVal = 0; } } @@ -120,7 +123,10 @@ void Wippersnapper_DigitalGPIO::deinitDigitalPin( // de-allocate the pin within digital_input_pins[] for (int i = 0; i < _totalDigitalInputPins; i++) { if (_digital_input_pins[i].pinName == pinName) { + _digital_input_pins[i].pinName = -1; _digital_input_pins[i].period = -1; + _digital_input_pins[i].prvPeriod = 0L; + _digital_input_pins[i].prvPinVal = 0; break; } } From 6e614f014e52424174d14252b566a49c85503f5d Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 21 Jul 2021 17:51:18 -0400 Subject: [PATCH 20/95] decode and process in-order, ditch the queue idea for now bc dont want to dynamically alloc. memory --- src/Wippersnapper.cpp | 77 ++++++++++++++++++++++++++----------------- src/Wippersnapper.h | 11 ------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index c902e15bc..11d97fa0b 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -415,8 +415,40 @@ void cbSignalI2CReq(char *data, uint16_t len) { memcpy(WS._buffer, data, len); WS.bufSize = len; - // Set next event to decode the I2C signal - WS.wsEventDecodeMsgs.push_front(wsEventDecodeMsgSignalI2C); + // Zero-out existing I2C signal msg. + msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; + + /* Set up the payload callback, which will set up the callbacks for + each oneof payload field once the field tag is known */ + msgSignalI2C->cb_payload.funcs.decode = cbI2CMsgFields; + + // Decode buffer into msgSignalI2C + pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); + if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, + &msgSignalI2C)) + WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); +} + +bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { + bool is_success = true; + WS_DEBUG_PRINTLN(cbI2CMsgFields); + pb_size_t arr_sz = field->array_size; + WS_DEBUG_PRINT("Sub-messages found: "); + WS_DEBUG_PRINTLN(arr_sz); + + if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { + WS_DEBUG_PRINTLN("I2C Init Request Found!"); + // TODO: + // Create I2C request message + // Decode I2C request message into struct. + // Pass I2C request message struct. to I2C component init + } else if (field->tag == + wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { + WS_DEBUG_PRINTLN("I2C Scan Request Found!"); + } else { + WS_DEBUG_PRINTLN("ERROR: Unknown i2c msg tag"); + } + return is_success; } /**************************************************************************/ @@ -430,22 +462,22 @@ void Wippersnapper::decodeMsgSignalI2C() { msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; // Decode buffer into msgSignalI2C pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); - if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, &msgSignalI2C)) - WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); + if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, + &msgSignalI2C)) + WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); // Query the payload type WS_DEBUG_PRINT("I2C Payload Type: "); WS_DEBUG_PRINTLN(msgSignalI2C.which_payload); // NOTE: Two event types? Hardware events and Message events? - // Order of processing: poll-> Message (decode, get to the last step) -> hardware event - // Set hardware event based on payload type -/* if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CInitRequest) { - // Call something which will the I2C bus here? Add an i2c component as well? - WS_DEBUG_PRINTLN("Found: I2C Init Request Msg"); - } else if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CScanRequest) { - // init the I2C scan here? - WS_DEBUG_PRINTLN("Found: I2C Scan Request Msg"); - } */ - + // Order of processing: poll-> Message (decode, get to the last step) -> + // hardware event Set hardware event based on payload type + /* if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CInitRequest) { + // Call something which will the I2C bus here? Add an i2c component as + well? WS_DEBUG_PRINTLN("Found: I2C Init Request Msg"); } else if + (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CScanRequest) { + // init the I2C scan here? + WS_DEBUG_PRINTLN("Found: I2C Scan Request Msg"); + } */ } /**************************************************************************/ @@ -1072,22 +1104,7 @@ ws_status_t Wippersnapper::run() { // Process all incoming packets from Wippersnapper MQTT Broker WS._mqtt->processPackets(10); - // TODO: Implement correct I2C when we have topic added - // for now, this is mocking the initialization assuming we got a message - - // Message: New I2CInitRequest w/pins 34&33 (default esp32s2 funhouse) - // addNewI2CComponent(34, 33); - - // Message event processing loop - switch (wsEventDecodeMsgs.front()) { - case wsEventDecodeMsgSignalI2C: - // Priority - HIGH - decodeMsgSignalI2C(); - break; - } - - // Hardware event processing loop - + // TODO: Loop thru components // Process digital inputs, digitalGPIO module WS._digitalGPIO->processDigitalInputs(); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index bdf1e63fd..90d24e4e1 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -150,13 +150,6 @@ typedef enum { WS_BOARD_DEF_UNSPECIFIED } ws_board_status_t; -// WIP - event-driven loop handler for i2c -// Events for decoding incoming messages -enum wsEventDecodeMsg { - wsEventDecodeMsgSignal, - wsEventDecodeMsgSignalI2C, -}; - /* MQTT Configuration */ #define WS_KEEPALIVE_INTERVAL 4 ///< Session keepalive interval time, in seconds #define WS_KEEPALIVE_INTERVAL_MS \ @@ -251,10 +244,6 @@ class Wippersnapper { // Decoder for i2c signal incoming void decodeMsgSignalI2C(); - // Event-loop - std::deque wsEventDecodeMsgs; // global accessed by the object. - - uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback payload */ uint8_t From e974e6302265374fb380a63b695c69c62633d6ab Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 11:05:31 -0400 Subject: [PATCH 21/95] process packet until callback+packet executed, then return --- src/Wippersnapper.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 11d97fa0b..ee031e828 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1101,8 +1101,10 @@ ws_status_t Wippersnapper::run() { // Handle MQTT connection checkMQTTConnection(curTime); - // Process all incoming packets from Wippersnapper MQTT Broker - WS._mqtt->processPackets(10); + // Poll for packets from broker queue, return immediately + if(!WS._mqtt->processPacketsUntilCallback(100)) { + WS_DEBUG_PRINTLN("No packets found!"); + } // TODO: Loop thru components // Process digital inputs, digitalGPIO module From 600cae8de4d6ce8ead5fa9ae709383fb00c98ff0 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 11:20:47 -0400 Subject: [PATCH 22/95] reference to WS object instead --- src/Wippersnapper.cpp | 48 ++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index ee031e828..b0d380e9d 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -400,6 +400,28 @@ void cbSignalTopic(char *data, uint16_t len) { } } +bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { + bool is_success = true; + WS_DEBUG_PRINTLN("cbI2CMsgFields"); + pb_size_t arr_sz = field->array_size; + WS_DEBUG_PRINT("Sub-messages found: "); + WS_DEBUG_PRINTLN(arr_sz); + + if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { + WS_DEBUG_PRINTLN("I2C Init Request Found!"); + // TODO: + // Create I2C request message + // Decode I2C request message into struct. + // Pass I2C request message struct. to I2C component init + } else if (field->tag == + wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { + WS_DEBUG_PRINTLN("I2C Scan Request Found!"); + } else { + WS_DEBUG_PRINTLN("ERROR: Unknown i2c msg tag"); + } + return is_success; +} + /**************************************************************************/ /*! @brief Called when i2c signal sub-topic receives a new message. @@ -416,40 +438,20 @@ void cbSignalI2CReq(char *data, uint16_t len) { WS.bufSize = len; // Zero-out existing I2C signal msg. - msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; + WS.msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; /* Set up the payload callback, which will set up the callbacks for each oneof payload field once the field tag is known */ - msgSignalI2C->cb_payload.funcs.decode = cbI2CMsgFields; + WS.msgSignalI2C.cb_payload.funcs.decode = cbI2CMsgFields; // Decode buffer into msgSignalI2C pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, - &msgSignalI2C)) + &WS.msgSignalI2C)) WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); } -bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { - bool is_success = true; - WS_DEBUG_PRINTLN(cbI2CMsgFields); - pb_size_t arr_sz = field->array_size; - WS_DEBUG_PRINT("Sub-messages found: "); - WS_DEBUG_PRINTLN(arr_sz); - if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { - WS_DEBUG_PRINTLN("I2C Init Request Found!"); - // TODO: - // Create I2C request message - // Decode I2C request message into struct. - // Pass I2C request message struct. to I2C component init - } else if (field->tag == - wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { - WS_DEBUG_PRINTLN("I2C Scan Request Found!"); - } else { - WS_DEBUG_PRINTLN("ERROR: Unknown i2c msg tag"); - } - return is_success; -} /**************************************************************************/ /*! From 1eac6926a30081b46a801a6724244af2ee5bcb79 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 11:32:23 -0400 Subject: [PATCH 23/95] init request decode --- src/Wippersnapper.cpp | 16 ++++++++++++++++ src/Wippersnapper.h | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index b0d380e9d..eba46e0dc 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -411,8 +411,24 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); // TODO: // Create I2C request message + WS.msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; // Decode I2C request message into struct. // Pass I2C request message struct. to I2C component init + + + // array to store the decoded CreateSignalRequests data + wippersnapper_pin_v1_ConfigurePinRequests msg = + wippersnapper_pin_v1_ConfigurePinRequests_init_zero; + // set up callback + msg.list.funcs.decode = cbDecodePinConfigMsg; + msg.list.arg = field->pData; + // decode each ConfigurePinRequest sub-message + if (!pb_decode(stream, wippersnapper_pin_v1_ConfigurePinRequests_fields, + &msg)) { + WS_DEBUG_PRINTLN("ERROR: Could not decode CreateSignalRequest") + is_success = false; + } + } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 90d24e4e1..3981f9fb4 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -290,7 +290,8 @@ class Wippersnapper { _incomingSignalMsg; /*!< Incoming signal message from broker */ // i2c signal msg - wippersnapper_signal_v1_I2CRequest msgSignalI2C; + wippersnapper_signal_v1_I2CRequest msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; + wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in From bcc4d9e2f658a07eeca336d55a3c28d9fdbdc9c3 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 13:47:08 -0400 Subject: [PATCH 24/95] decode, init based on message, add to vector --- src/Wippersnapper.cpp | 79 ++++--------------- src/Wippersnapper.h | 10 +-- .../WipperSnapper_Component_I2C.cpp | 28 +++---- src/components/WipperSnapper_Component_I2C.h | 3 +- 4 files changed, 34 insertions(+), 86 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index eba46e0dc..74c580a8a 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -403,32 +403,28 @@ void cbSignalTopic(char *data, uint16_t len) { bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { bool is_success = true; WS_DEBUG_PRINTLN("cbI2CMsgFields"); - pb_size_t arr_sz = field->array_size; - WS_DEBUG_PRINT("Sub-messages found: "); - WS_DEBUG_PRINTLN(arr_sz); if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); - // TODO: // Create I2C request message WS.msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; - // Decode I2C request message into struct. - // Pass I2C request message struct. to I2C component init - - - // array to store the decoded CreateSignalRequests data - wippersnapper_pin_v1_ConfigurePinRequests msg = - wippersnapper_pin_v1_ConfigurePinRequests_init_zero; - // set up callback - msg.list.funcs.decode = cbDecodePinConfigMsg; - msg.list.arg = field->pData; - // decode each ConfigurePinRequest sub-message - if (!pb_decode(stream, wippersnapper_pin_v1_ConfigurePinRequests_fields, - &msg)) { - WS_DEBUG_PRINTLN("ERROR: Could not decode CreateSignalRequest") + // Decode i2c request message into struct + if (!pb_decode(stream, wippersnapper_i2c_v1_I2CInitRequest_fields, + &WS.msgI2cInitRequest)) { + WS_DEBUG_PRINTLN("ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); + is_success = false; + } + // Finally, create an I2C object using the message as a constructor? + if (WS._i2cPort0 != NULL) { + WS._i2cPort0 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); + WS.i2cComponents.push_back(WS._i2cPort0); + } else if (WS._i2cPort1 != NULL) { + WS._i2cPort1 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); + WS.i2cComponents.push_back(WS._i2cPort1); + } else { + WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); is_success = false; } - } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); @@ -438,6 +434,7 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { return is_success; } + /**************************************************************************/ /*! @brief Called when i2c signal sub-topic receives a new message. @@ -467,37 +464,6 @@ void cbSignalI2CReq(char *data, uint16_t len) { WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); } - - -/**************************************************************************/ -/*! - @brief Called when i2c signal sub-topic receives a new message. - Fills a shared buffer with data from payload. -*/ -/**************************************************************************/ -void Wippersnapper::decodeMsgSignalI2C() { - // Zero-out existing I2C incoming signal msg. - msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; - // Decode buffer into msgSignalI2C - pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); - if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, - &msgSignalI2C)) - WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); - // Query the payload type - WS_DEBUG_PRINT("I2C Payload Type: "); - WS_DEBUG_PRINTLN(msgSignalI2C.which_payload); - // NOTE: Two event types? Hardware events and Message events? - // Order of processing: poll-> Message (decode, get to the last step) -> - // hardware event Set hardware event based on payload type - /* if (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CInitRequest) { - // Call something which will the I2C bus here? Add an i2c component as - well? WS_DEBUG_PRINTLN("Found: I2C Init Request Msg"); } else if - (msgSignalI2C.which_payload == wippersnapper_i2c_v1_I2CScanRequest) { - // init the I2C scan here? - WS_DEBUG_PRINTLN("Found: I2C Scan Request Msg"); - } */ -} - /**************************************************************************/ /*! @brief Called when broker responds to a device's publish across @@ -1134,19 +1100,6 @@ ws_status_t Wippersnapper::run() { return status(); } -void Wippersnapper::addNewI2CComponent(int32_t sdaPin, int32_t sclPin, - int32_t portNum, uint32_t frequency) { - if (_i2cPort0 != NULL && portNum == 0) { - _i2cPort0 = new WipperSnapper_Component_I2C(sdaPin, sclPin, portNum); - i2cComponents.push_back(_i2cPort0); - } else if (_i2cPort1 != NULL && portNum == 1) { - _i2cPort1 = new WipperSnapper_Component_I2C(sdaPin, sclPin, portNum); - i2cComponents.push_back(_i2cPort1); - } else { - WS_DEBUG_PRINTLN("Unable to allocate new I2C Component") - } -} - /**************************************************************************/ /*! @brief Sends board description message to Wippersnapper diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 3981f9fb4..9d3c47cf0 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -240,9 +240,12 @@ class Wippersnapper { // I2C void addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, uint32_t frequency = 100000U); - // Decoder for i2c signal incoming void decodeMsgSignalI2C(); + // I2C WIP - dirty! + std::vector i2cComponents; + WipperSnapper_Component_I2C *_i2cPort0 = NULL; + WipperSnapper_Component_I2C *_i2cPort1 = NULL; uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback payload */ @@ -260,11 +263,6 @@ class Wippersnapper { Wippersnapper_FS *_fileSystem; ///< Instance of filesystem class Wippersnapper_ESP32_nvs *_nvs; ///< Instance of nvs - // I2C WIP - dirty! - std::vector i2cComponents; - WipperSnapper_Component_I2C *_i2cPort0 = NULL; - WipperSnapper_Component_I2C *_i2cPort1 = NULL; - uint8_t _uid[6]; /*!< Unique network iface identifier */ char sUID[9]; /*!< Unique network iface identifier */ const char *_boardId; /*!< Adafruit IO+ board string */ diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index fad0d7614..1f3df12c8 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -16,24 +16,22 @@ #include "WipperSnapper_Component_I2C.h" -WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(int32_t sdaPin, - int32_t sclPin, - int32_t portNum, - uint32_t frequency) { +WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { + WS_DEBUG_PRINTLN("NEW WipperSnapper_Component_I2C"); // initialize using portNum - _i2c = new TwoWire(portNum); - - // validate if pins are pulled HI - if (digitalRead(sdaPin) == LOW) { - pinMode(sdaPin, INPUT_PULLUP); + _i2c = new TwoWire(msgInitRequest->i2c_port_number); + // validate if SDA & SCL has pullup + if (digitalRead(msgInitRequest->i2c_pin_sda) == LOW) { + pinMode(msgInitRequest->i2c_pin_sda, INPUT_PULLUP); } - if (digitalRead(sclPin) == LOW) { - pinMode(sclPin, INPUT_PULLUP); + if (digitalRead(msgInitRequest->i2c_pin_scl) == LOW) { + pinMode(msgInitRequest->i2c_pin_scl, INPUT_PULLUP); } - // begin i2c - _i2c->begin(sdaPin, sclPin); - _i2c->setClock(frequency); - _portNum = portNum; + // set up i2c + _i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl); + _i2c->setClock(msgInitRequest->i2c_frequency); + // set i2c obj. properties + _portNum = msgInitRequest->i2c_port_number; _isInit = true; yield(); } diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 4cd24434b..2ed6955f1 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -24,8 +24,7 @@ class Wippersnapper; class WipperSnapper_Component_I2C { public: - WipperSnapper_Component_I2C(int32_t sdaPin, int32_t sclPin, - int32_t portNum = 0, uint32_t frequency = 100000); + WipperSnapper_Component_I2C(wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); bool scanForAddress(uint32_t address); From a0f7983c59a3251cf259bc3334d439d18827ba7c Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 14:42:04 -0400 Subject: [PATCH 25/95] Add init response, full decode within the component --- src/Wippersnapper.cpp | 35 +++++++++++++++++--- src/Wippersnapper.h | 11 +++--- src/components/WipperSnapper_Component_I2C.h | 3 +- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 74c580a8a..233bd848c 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -411,20 +411,48 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { // Decode i2c request message into struct if (!pb_decode(stream, wippersnapper_i2c_v1_I2CInitRequest_fields, &WS.msgI2cInitRequest)) { - WS_DEBUG_PRINTLN("ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); + WS_DEBUG_PRINTLN( + "ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); is_success = false; } - // Finally, create an I2C object using the message as a constructor? + // Create an I2C object using the message as a constructor? if (WS._i2cPort0 != NULL) { WS._i2cPort0 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); WS.i2cComponents.push_back(WS._i2cPort0); + // did we init. the port successfully? + is_success = WS._i2cPort0->_isInit; } else if (WS._i2cPort1 != NULL) { WS._i2cPort1 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); + // did we init. the port successfully? + is_success = WS._i2cPort1->_isInit; WS.i2cComponents.push_back(WS._i2cPort1); } else { WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); is_success = false; } + // Create I2C init. response + wippersnapper_signal_v1_I2CResponse msgi2cResponse = + wippersnapper_signal_v1_I2CResponse_init_zero; + msgi2cResponse.which_payload = + wippersnapper_signal_v1_I2CResponse_resp_i2c_init_tag; + msgi2cResponse.payload.resp_i2c_init.is_initialized = is_success; + // TODO: Zero-out the outgoing buffer here + // Encode I2C init. response + pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); + is_success = false; + } + // Obtain size and only write out buffer to end + size_t msgSz = pb_get_encoded_size( + &msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); + // publish event data + WS_DEBUG_PRINT("Publishing I2C init response...") + WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); + WS_DEBUG_PRINTLN("Published!"); + } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); @@ -434,7 +462,6 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { return is_success; } - /**************************************************************************/ /*! @brief Called when i2c signal sub-topic receives a new message. @@ -1086,7 +1113,7 @@ ws_status_t Wippersnapper::run() { checkMQTTConnection(curTime); // Poll for packets from broker queue, return immediately - if(!WS._mqtt->processPacketsUntilCallback(100)) { + if (!WS._mqtt->processPacketsUntilCallback(100)) { WS_DEBUG_PRINTLN("No packets found!"); } diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 9d3c47cf0..c8af71317 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -284,12 +284,17 @@ class Wippersnapper { char *_topic_signal_device; /*!< Device->Wprsnpr messages */ + char *_topic_signal_i2c_brkr; /*!< Topic carries messages from a device to a + broker. */ + wippersnapper_signal_v1_CreateSignalRequest _incomingSignalMsg; /*!< Incoming signal message from broker */ // i2c signal msg - wippersnapper_signal_v1_I2CRequest msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; - wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; + wippersnapper_signal_v1_I2CRequest msgSignalI2C = + wippersnapper_signal_v1_I2CRequest_init_zero; + wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = + wippersnapper_i2c_v1_I2CInitRequest_init_zero; char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in @@ -315,8 +320,6 @@ class Wippersnapper { char *_topic_description_status; /*!< MQTT subtopic carrying the description status resp. from the broker */ char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */ - char *_topic_signal_i2c_brkr; /*!< Topic carries messages from a device to a - broker. */ char *_topic_signal_i2c_device; /*!< Topic carries messages from a broker to a device. */ diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 2ed6955f1..b4ba60572 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -24,7 +24,8 @@ class Wippersnapper; class WipperSnapper_Component_I2C { public: - WipperSnapper_Component_I2C(wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); + WipperSnapper_Component_I2C( + wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); bool scanForAddress(uint32_t address); From 0c999183a74a85e342cae0ee45b21dbd57170d8a Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 14:43:29 -0400 Subject: [PATCH 26/95] zero-out the output buffer first --- src/Wippersnapper.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 233bd848c..98d961f8d 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -431,12 +431,14 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { is_success = false; } // Create I2C init. response + // TODO: We should make this a funcn. wippersnapper_signal_v1_I2CResponse msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_init_tag; msgi2cResponse.payload.resp_i2c_init.is_initialized = is_success; - // TODO: Zero-out the outgoing buffer here + // Zero-out buffer before writing + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); // Encode I2C init. response pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); @@ -452,7 +454,6 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINT("Publishing I2C init response...") WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); WS_DEBUG_PRINTLN("Published!"); - } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); From a0a3488874f6c78eadb0921004d2ed57a696e0bb Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 14:53:23 -0400 Subject: [PATCH 27/95] start adding to scan.. --- .../WipperSnapper_Component_I2C.cpp | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 1f3df12c8..e49830907 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -30,6 +30,12 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(wippersnapper_i2c_v1_I2 // set up i2c _i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl); _i2c->setClock(msgInitRequest->i2c_frequency); + WS_DEBUG_PRINTLN("Init. new I2C Bus: "); + WS_DEBUG_PRINT("Port#: ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_port_number); + WS_DEBUG_PRINT("SDA: ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_sda); + WS_DEBUG_PRINT("SCL: ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_scl); + WS_DEBUG_PRINT("Frequency (Hz): ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); + WS_DEBUG_PRINTLN("Init. new I2C Bus: "); // set i2c obj. properties _portNum = msgInitRequest->i2c_port_number; _isInit = true; @@ -42,7 +48,19 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { } bool WipperSnapper_Component_I2C::scanForAddress(uint32_t address) { - bool is_success = false; - // TODO: scanner code + bool is_detected = false; + WS_DEBUG_PRINT("EXEC: I2C Scan, Port ("); WS_DEBUG_PRINT(_portNum); WS_DEBUG_PRINTLN(")"); + // do i2c scan + // A basic scanner, see if it ACK's + // TODO: Remove address, use request protobuf struct. typedef + _wire->beginTransmission(address); + if (_wire->endTransmission() == 0) { + WS_DEBUG_PRINTLN("I2C device detected!"); + is_detected = true; + } else { + WS_DEBUG_PRINTLN("I2C device not detected!"); + } + // TODO + // encode response and publish to broker return is_success; } From e056def087f2f10a554bf11944a50b6ae46c96d9 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 16:36:58 -0400 Subject: [PATCH 28/95] loop scan all addresses provided --- src/Wippersnapper.cpp | 11 +++++++- .../WipperSnapper_Component_I2C.cpp | 27 +++++++++++-------- src/components/WipperSnapper_Component_I2C.h | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 98d961f8d..e5c9289aa 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -457,8 +457,17 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); + wippersnapper_i2c_v1_I2CScanRequest msgScanReq = wippersnapper_i2c_v1_I2CScanRequest_init_zero; + // Decode i2c scan request message into struct + if (!pb_decode(stream, wippersnapper_i2c_v1_I2CScanRequest_fields, &msgScanReq)) { + WS_DEBUG_PRINTLN( + "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); + is_success = false; + } + // TODO: Check which port and select the correct object out of the i2c component vector + WS_DEBUG_PRINT("Port"); WS_DEBUG_PRINTLN(msgScanReq.i2c_port_number); } else { - WS_DEBUG_PRINTLN("ERROR: Unknown i2c msg tag"); + WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); } return is_success; } diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index e49830907..9404afa18 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -47,20 +47,25 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { _isInit = false; } -bool WipperSnapper_Component_I2C::scanForAddress(uint32_t address) { +bool WipperSnapper_Component_I2C::scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { + // decode stream into i2c request bool is_detected = false; + uint16_t scanAddr; WS_DEBUG_PRINT("EXEC: I2C Scan, Port ("); WS_DEBUG_PRINT(_portNum); WS_DEBUG_PRINTLN(")"); - // do i2c scan - // A basic scanner, see if it ACK's - // TODO: Remove address, use request protobuf struct. typedef - _wire->beginTransmission(address); - if (_wire->endTransmission() == 0) { - WS_DEBUG_PRINTLN("I2C device detected!"); - is_detected = true; - } else { - WS_DEBUG_PRINTLN("I2C device not detected!"); + for (int i = 0; i < msgScanReq.address_count; i++) { + scanAddr = msgScanReq.address[i]; + WS_DEBUG_PRINT("Scanning address "); WS_DEBUG_PRINTLN(scanAddr); + _i2c->beginTransmission(scanAddr); + if (_i2c->endTransmission() == 0) { + // found it! + WS_DEBUG_PRINTLN("I2C device detected!"); + is_detected = true; + break; + } else { + WS_DEBUG_PRINTLN("I2C device not detected!"); + } } // TODO // encode response and publish to broker - return is_success; + return is_detected; } diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index b4ba60572..7484cd313 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -27,7 +27,7 @@ class WipperSnapper_Component_I2C { WipperSnapper_Component_I2C( wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); - bool scanForAddress(uint32_t address); + bool scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); int32_t _portNum; bool _isInit; From 19c2739f237151e50d5568ccfd3a605915044817 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 22 Jul 2021 16:41:31 -0400 Subject: [PATCH 29/95] ret. uint16_t address found back to application for resp. --- src/Wippersnapper.cpp | 3 +++ src/components/WipperSnapper_Component_I2C.cpp | 10 ++++------ src/components/WipperSnapper_Component_I2C.h | 3 +-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index e5c9289aa..b12cead9b 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -466,6 +466,9 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { } // TODO: Check which port and select the correct object out of the i2c component vector WS_DEBUG_PRINT("Port"); WS_DEBUG_PRINTLN(msgScanReq.i2c_port_number); + // Scan all requested addresses on i2cportX and ret. address found, -1 otherwise. + uint16_t addressFound = WS._i2cPort0->scanAddresses(msgScanReq); + // Create address found response } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); } diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 9404afa18..41cfd4e45 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -47,9 +47,9 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { _isInit = false; } -bool WipperSnapper_Component_I2C::scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { +uint16_t WipperSnapper_Component_I2C::scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { // decode stream into i2c request - bool is_detected = false; + uint16_t addrFound = -1; uint16_t scanAddr; WS_DEBUG_PRINT("EXEC: I2C Scan, Port ("); WS_DEBUG_PRINT(_portNum); WS_DEBUG_PRINTLN(")"); for (int i = 0; i < msgScanReq.address_count; i++) { @@ -59,13 +59,11 @@ bool WipperSnapper_Component_I2C::scanAddresses(wippersnapper_i2c_v1_I2CScanRequ if (_i2c->endTransmission() == 0) { // found it! WS_DEBUG_PRINTLN("I2C device detected!"); - is_detected = true; + addrFound = scanAddr; break; } else { WS_DEBUG_PRINTLN("I2C device not detected!"); } } - // TODO - // encode response and publish to broker - return is_detected; + return addrFound; } diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 7484cd313..2ada7329c 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -27,11 +27,10 @@ class WipperSnapper_Component_I2C { WipperSnapper_Component_I2C( wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); - bool scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); int32_t _portNum; bool _isInit; - private: TwoWire *_i2c = NULL; }; From c2608afe9943fcb245a9d427db429ec022bdb882 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 23 Jul 2021 10:10:38 -0400 Subject: [PATCH 30/95] add portscan response --- src/Wippersnapper.cpp | 41 +++++++++++++++++--- src/components/WipperSnapper_Component_I2C.h | 1 + 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index b12cead9b..2c89c01a4 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -457,18 +457,47 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); - wippersnapper_i2c_v1_I2CScanRequest msgScanReq = wippersnapper_i2c_v1_I2CScanRequest_init_zero; + wippersnapper_i2c_v1_I2CScanRequest msgScanReq = + wippersnapper_i2c_v1_I2CScanRequest_init_zero; // Decode i2c scan request message into struct - if (!pb_decode(stream, wippersnapper_i2c_v1_I2CScanRequest_fields, &msgScanReq)) { + if (!pb_decode(stream, wippersnapper_i2c_v1_I2CScanRequest_fields, + &msgScanReq)) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); is_success = false; } - // TODO: Check which port and select the correct object out of the i2c component vector - WS_DEBUG_PRINT("Port"); WS_DEBUG_PRINTLN(msgScanReq.i2c_port_number); - // Scan all requested addresses on i2cportX and ret. address found, -1 otherwise. + // TODO: Check which port and select the correct object out of the i2c + // component vector + WS_DEBUG_PRINT("Port"); + WS_DEBUG_PRINTLN(msgScanReq.i2c_port_number); + // Scan all requested addresses on i2cportX and ret. address found, -1 + // otherwise. uint16_t addressFound = WS._i2cPort0->scanAddresses(msgScanReq); - // Create address found response + // Create response + wippersnapper_signal_v1_I2CResponse msgi2cResponse = + wippersnapper_signal_v1_I2CResponse_init_zero; + msgi2cResponse.which_payload = + wippersnapper_i2c_v1_I2CScanResponse_which_address_found_tag; + msgi2cResponse.payload.resp_i2c_scan.which_address_found = + (uint32_t)addressFound; + // Zero-out buffer before writing + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + // Encode I2C init. response + pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); + is_success = false; + } + // Obtain size and only write out buffer to end + size_t msgSz = pb_get_encoded_size( + &msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); + // publish event data + WS_DEBUG_PRINT("Publishing I2C scan response...") + WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); + WS_DEBUG_PRINTLN("Published!"); + } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); } diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 2ada7329c..21fdd16ed 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -31,6 +31,7 @@ class WipperSnapper_Component_I2C { int32_t _portNum; bool _isInit; + private: TwoWire *_i2c = NULL; }; From 01649f9fc6042772f76b41fae1cf62cf0142468d Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 23 Jul 2021 10:27:07 -0400 Subject: [PATCH 31/95] split out publishing, share the signal --- src/Wippersnapper.cpp | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 2c89c01a4..286242aa2 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -400,10 +400,20 @@ void cbSignalTopic(char *data, uint16_t len) { } } +void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { + size_t msgSz; + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse); + // Publish i2c response message to broker + WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); +} + bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { bool is_success = true; WS_DEBUG_PRINTLN("cbI2CMsgFields"); - + // alloc. response we'll need + wippersnapper_signal_v1_I2CResponse msgi2cResponse = + wippersnapper_signal_v1_I2CResponse_init_zero; if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); // Create I2C request message @@ -432,8 +442,6 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { } // Create I2C init. response // TODO: We should make this a funcn. - wippersnapper_signal_v1_I2CResponse msgi2cResponse = - wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_init_tag; msgi2cResponse.payload.resp_i2c_init.is_initialized = is_success; @@ -447,13 +455,6 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); is_success = false; } - // Obtain size and only write out buffer to end - size_t msgSz = pb_get_encoded_size( - &msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); - // publish event data - WS_DEBUG_PRINT("Publishing I2C init response...") - WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); - WS_DEBUG_PRINTLN("Published!"); } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); @@ -474,8 +475,6 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { // otherwise. uint16_t addressFound = WS._i2cPort0->scanAddresses(msgScanReq); // Create response - wippersnapper_signal_v1_I2CResponse msgi2cResponse = - wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = wippersnapper_i2c_v1_I2CScanResponse_which_address_found_tag; msgi2cResponse.payload.resp_i2c_scan.which_address_found = @@ -490,17 +489,13 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); is_success = false; } - // Obtain size and only write out buffer to end - size_t msgSz = pb_get_encoded_size( - &msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); - // publish event data - WS_DEBUG_PRINT("Publishing I2C scan response...") - WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); - WS_DEBUG_PRINTLN("Published!"); - } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); } + // Publish i2c scan response back to broker + WS_DEBUG_PRINT("Publishing I2C scan response...") + publishI2CResponse(&msgi2cResponse); + WS_DEBUG_PRINTLN("Published!"); return is_success; } From 2940b5b45464cf7b352646fcd913c1bfab8b28c4 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 23 Jul 2021 11:03:23 -0400 Subject: [PATCH 32/95] add encode response, doxygen strings --- src/Wippersnapper.cpp | 100 ++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 32 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 286242aa2..994fe4be2 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -400,7 +400,15 @@ void cbSignalTopic(char *data, uint16_t len) { } } -void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { +/************************************************************************************/ +/*! + @brief Publishes an I2C signal response message to the broker's + I2C signal MQTT topic. + @param msgi2cResponse + I2CResponse signal message to publish +*/ +/************************************************************************************/ +void publishSignalI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); @@ -408,9 +416,46 @@ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); } -bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { +/************************************************************************************/ +/*! + @brief Encodes an I2C signal response message. Assumes the fields and type + have already been filled out in another function. + @param msgi2cResponse + I2CResponse signal message to encode. + @returns True if message is successfully encoded, False otherwise. +*/ +/************************************************************************************/ +bool encodeSignalI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { bool is_success = true; - WS_DEBUG_PRINTLN("cbI2CMsgFields"); + // Zero-out buffer before writing + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + // Encode I2C response + pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); + is_success = false; + } + return is_success; +} + +/******************************************************************************************/ +/*! + @brief Decodes an I2C signal request message and executes the + callback based on the message's tag. If successful, + publishes an I2C signal response back to the broker. + @param stream + Incoming data stream from buffer. + @param field + Protobuf message's tag type. + @param arg + Optional arguments from decoder calling function. +*/ +/******************************************************************************************/ +bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, void **arg) { + bool is_success = true; + WS_DEBUG_PRINTLN("cbDecodeSignalRequestI2C"); // alloc. response we'll need wippersnapper_signal_v1_I2CResponse msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; @@ -424,6 +469,7 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); is_success = false; + return is_success; } // Create an I2C object using the message as a constructor? if (WS._i2cPort0 != NULL) { @@ -440,20 +486,9 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); is_success = false; } - // Create I2C init. response - // TODO: We should make this a funcn. - msgi2cResponse.which_payload = - wippersnapper_signal_v1_I2CResponse_resp_i2c_init_tag; - msgi2cResponse.payload.resp_i2c_init.is_initialized = is_success; - // Zero-out buffer before writing - memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - // Encode I2C init. response - pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, - sizeof(WS._buffer_outgoing)); - if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); + if (!encodeSignalI2CResponse(&msgi2cResponse)) { is_success = false; + return is_success; } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { @@ -466,6 +501,7 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); is_success = false; + return false; } // TODO: Check which port and select the correct object out of the i2c // component vector @@ -479,30 +515,30 @@ bool cbI2CMsgFields(pb_istream_t *stream, const pb_field_t *field, void **arg) { wippersnapper_i2c_v1_I2CScanResponse_which_address_found_tag; msgi2cResponse.payload.resp_i2c_scan.which_address_found = (uint32_t)addressFound; - // Zero-out buffer before writing - memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - // Encode I2C init. response - pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, - sizeof(WS._buffer_outgoing)); - if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); + if (!encodeSignalI2CResponse(&msgi2cResponse)) { is_success = false; + return is_success; } } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); + is_success = false; + return is_success; } // Publish i2c scan response back to broker WS_DEBUG_PRINT("Publishing I2C scan response...") - publishI2CResponse(&msgi2cResponse); + publishSignalI2CResponse(&msgi2cResponse); WS_DEBUG_PRINTLN("Published!"); return is_success; } /**************************************************************************/ /*! - @brief Called when i2c signal sub-topic receives a new message. - Fills a shared buffer with data from payload. + @brief Called when i2c signal sub-topic receives a new message and + attempts to decode a signal request message. + @param data + Incoming data from MQTT broker. + @param len + Length of incoming data. */ /**************************************************************************/ void cbSignalI2CReq(char *data, uint16_t len) { @@ -510,18 +546,18 @@ void cbSignalI2CReq(char *data, uint16_t len) { printMsgBuffer(data, len); // zero-out current buffer memset(WS._buffer, 0, sizeof(WS._buffer)); - // copy data to buffer + // copy mqtt data into buffer memcpy(WS._buffer, data, len); WS.bufSize = len; // Zero-out existing I2C signal msg. WS.msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; - /* Set up the payload callback, which will set up the callbacks for - each oneof payload field once the field tag is known */ - WS.msgSignalI2C.cb_payload.funcs.decode = cbI2CMsgFields; + // Set up the payload callback, which will set up the callbacks for + // each oneof payload field once the field tag is known + WS.msgSignalI2C.cb_payload.funcs.decode = cbDecodeSignalRequestI2C; - // Decode buffer into msgSignalI2C + // Decode I2C signal request pb_istream_t istream = pb_istream_from_buffer(WS._buffer, WS.bufSize); if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, &WS.msgSignalI2C)) From 90cf09ca8146b8274ee59b63bcdb6c2efe5f1bac Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 23 Jul 2021 15:44:16 -0400 Subject: [PATCH 33/95] update tag, type, remove queue STD --- src/Wippersnapper.cpp | 4 ++-- src/Wippersnapper.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 994fe4be2..b9ad46500 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -512,8 +512,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, voi uint16_t addressFound = WS._i2cPort0->scanAddresses(msgScanReq); // Create response msgi2cResponse.which_payload = - wippersnapper_i2c_v1_I2CScanResponse_which_address_found_tag; - msgi2cResponse.payload.resp_i2c_scan.which_address_found = + wippersnapper_i2c_v1_I2CScanResponse_address_found_tag; + msgi2cResponse.payload.resp_i2c_scan.address_found = (uint32_t)addressFound; if (!encodeSignalI2CResponse(&msgi2cResponse)) { is_success = false; diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 8ad33b29c..db6e11d75 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -18,7 +18,7 @@ #ifndef WIPPERSNAPPER_H #define WIPPERSNAPPER_H -#include +// Cpp STD #include // Nanopb dependencies From 768f8db73ab6535db5a244e4a9e21cbce1a9eee1 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 26 Jul 2021 11:09:21 -0400 Subject: [PATCH 34/95] doxygen --- src/Wippersnapper.cpp | 42 ++++++++--- src/Wippersnapper.h | 33 ++++---- src/Wippersnapper_DigitalGPIO.cpp | 2 + .../WipperSnapper_Component_I2C.cpp | 75 ++++++++++++++----- src/components/WipperSnapper_Component_I2C.h | 1 + 5 files changed, 107 insertions(+), 46 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index b9ad46500..006e1ce2e 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -67,6 +67,11 @@ Wippersnapper::~Wippersnapper() { free(_throttle_sub); } +/**************************************************************************/ +/*! + @brief Performs the filesystem-backed provisioning workflow +*/ +/**************************************************************************/ void Wippersnapper::provision() { // init. LED for status signaling statusLEDInit(); @@ -408,7 +413,8 @@ void cbSignalTopic(char *data, uint16_t len) { I2CResponse signal message to publish */ /************************************************************************************/ -void publishSignalI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { +void publishSignalI2CResponse( + wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); @@ -418,22 +424,23 @@ void publishSignalI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cRespons /************************************************************************************/ /*! - @brief Encodes an I2C signal response message. Assumes the fields and type - have already been filled out in another function. + @brief Encodes an I2C signal response message. Assumes the fields and + type have already been filled out in another function. @param msgi2cResponse I2CResponse signal message to encode. @returns True if message is successfully encoded, False otherwise. */ /************************************************************************************/ -bool encodeSignalI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { +bool encodeSignalI2CResponse( + wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { bool is_success = true; // Zero-out buffer before writing memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); // Encode I2C response - pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, - sizeof(WS._buffer_outgoing)); + pb_ostream_t stream = + pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse)) { + &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); is_success = false; } @@ -453,7 +460,8 @@ bool encodeSignalI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse Optional arguments from decoder calling function. */ /******************************************************************************************/ -bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, void **arg) { +bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, + void **arg) { bool is_success = true; WS_DEBUG_PRINTLN("cbDecodeSignalRequestI2C"); // alloc. response we'll need @@ -513,16 +521,26 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, voi // Create response msgi2cResponse.which_payload = wippersnapper_i2c_v1_I2CScanResponse_address_found_tag; - msgi2cResponse.payload.resp_i2c_scan.address_found = - (uint32_t)addressFound; + msgi2cResponse.payload.resp_i2c_scan.address_found = (uint32_t)addressFound; if (!encodeSignalI2CResponse(&msgi2cResponse)) { is_success = false; return is_success; } + } else if (field->tag = wippersnapper_signal_v1_I2CRequest_req_aht_init_tag) { + WS_DEBUG_PRINTLN("AHTX Init Request Found!"); + // TODO: allow this to be implemented on multiple ports, but for now use + // port0 Initialize I2C device WS._i2cPort0->attachI2CDevice(address) + // Configure sensors + // WS._i2cPort0->enableSensor(temperature); + // WS._i2cPort0->enableSensor(humidity); + // Configure send interval + // WS._i2cPort0->sendInterval(period); + // Publish back response + // TODO } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); - is_success = false; - return is_success; + is_success = false; + return is_success; } // Publish i2c scan response back to broker WS_DEBUG_PRINT("Publishing I2C scan response...") diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index db6e11d75..179fd9c15 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -63,9 +63,9 @@ // define USE_STAGING #ifdef USE_STAGING -#define IO_MQTT_SERVER "io.adafruit.us" +#define IO_MQTT_SERVER "io.adafruit.us" ///< Staging (development) mqtt server #else -#define IO_MQTT_SERVER "io.adafruit.com" +#define IO_MQTT_SERVER "io.adafruit.com" ///< Production mqtt server #endif #ifdef USE_TINYUSB @@ -76,8 +76,7 @@ #include "provisioning/Wippersnapper_ESP32_nvs.h" #endif -// Library version (semver-formatted) -#define WS_VERSION "1.0.0-beta.3" +#define WS_VERSION "1.0.0-beta.3" ///< Library version (semver-formatted) // Reserved Adafruit IO MQTT topics #define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic @@ -184,10 +183,10 @@ class Wippersnapper { void setStatusLEDColor(uint32_t color); void statusLEDBlink(ws_led_status_t statusState); bool usingStatusNeoPixel = - false; // True if status LED is using the status neopixel + false; ///< True if status LED is using the status neopixel bool usingStatusDotStar = - false; // True if status LED is using the status dotstar - bool usingStatusLED = false; // True if status LED is using the built-in LED + false; ///< True if status LED is using the status dotstar + bool usingStatusLED = false; ///< True if status LED is using the built-in LED void set_user_key(const char *aio_username, const char *aio_key); void set_user_key(); @@ -238,14 +237,12 @@ class Wippersnapper { bool configurePinRequest(wippersnapper_pin_v1_ConfigurePinRequest *pinMsg); // I2C - void addNewI2CComponent(int32_t sdaPin, int32_t sclPin, int32_t portNum = 0, - uint32_t frequency = 100000U); - // Decoder for i2c signal incoming - void decodeMsgSignalI2C(); - // I2C WIP - dirty! - std::vector i2cComponents; - WipperSnapper_Component_I2C *_i2cPort0 = NULL; - WipperSnapper_Component_I2C *_i2cPort1 = NULL; + std::vector + i2cComponents; ///< Vector containing all I2C components + WipperSnapper_Component_I2C *_i2cPort0 = + NULL; ///< WipperSnapper I2C Component for I2C port #0 + WipperSnapper_Component_I2C *_i2cPort1 = + NULL; ///< WipperSnapper I2C Component for I2C port #1 uint8_t _buffer[WS_MQTT_MAX_PAYLOAD_SIZE]; /*!< Shared buffer to save callback payload */ @@ -292,9 +289,11 @@ class Wippersnapper { // i2c signal msg wippersnapper_signal_v1_I2CRequest msgSignalI2C = - wippersnapper_signal_v1_I2CRequest_init_zero; + wippersnapper_signal_v1_I2CRequest_init_zero; ///< I2C request wrapper + ///< message wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = - wippersnapper_i2c_v1_I2CInitRequest_init_zero; + wippersnapper_i2c_v1_I2CInitRequest_init_zero; ///< I2C initialization + ///< request message char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in diff --git a/src/Wippersnapper_DigitalGPIO.cpp b/src/Wippersnapper_DigitalGPIO.cpp index 2c700a22e..e627fffe4 100644 --- a/src/Wippersnapper_DigitalGPIO.cpp +++ b/src/Wippersnapper_DigitalGPIO.cpp @@ -54,6 +54,8 @@ Wippersnapper_DigitalGPIO::~Wippersnapper_DigitalGPIO() { The pin's name. @param period The pin's period, in seconds. + @param pull + The pin's pull mode. */ /*******************************************************************************************************************************/ void Wippersnapper_DigitalGPIO::initDigitalPin( diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 41cfd4e45..6811f0990 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -16,9 +16,17 @@ #include "WipperSnapper_Component_I2C.h" -WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { +/***************************************************************************************************************/ +/*! + @brief Creates a new WipperSnapper I2C component. + @param msgInitRequest + The I2C initialization request message. +*/ +/***************************************************************************************************************/ +WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( + wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { WS_DEBUG_PRINTLN("NEW WipperSnapper_Component_I2C"); - // initialize using portNum + // initialize using desired portNum _i2c = new TwoWire(msgInitRequest->i2c_port_number); // validate if SDA & SCL has pullup if (digitalRead(msgInitRequest->i2c_pin_sda) == LOW) { @@ -27,43 +35,76 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(wippersnapper_i2c_v1_I2 if (digitalRead(msgInitRequest->i2c_pin_scl) == LOW) { pinMode(msgInitRequest->i2c_pin_scl, INPUT_PULLUP); } - // set up i2c + // set up i2c port _i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl); _i2c->setClock(msgInitRequest->i2c_frequency); - WS_DEBUG_PRINTLN("Init. new I2C Bus: "); - WS_DEBUG_PRINT("Port#: ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_port_number); - WS_DEBUG_PRINT("SDA: ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_sda); - WS_DEBUG_PRINT("SCL: ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_scl); - WS_DEBUG_PRINT("Frequency (Hz): ");WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); - WS_DEBUG_PRINTLN("Init. new I2C Bus: "); + WS_DEBUG_PRINTLN("Init. new I2C Port: "); + WS_DEBUG_PRINT("Port#: "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_port_number); + WS_DEBUG_PRINT("SDA: "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_sda); + WS_DEBUG_PRINT("SCL: "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_scl); + WS_DEBUG_PRINT("Frequency (Hz): "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); // set i2c obj. properties _portNum = msgInitRequest->i2c_port_number; _isInit = true; yield(); } +/*************************************************************/ +/*! + @brief Destructor for a WipperSnapper I2C component. +*/ +/*************************************************************/ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { _portNum = 100; // Invalid = 100 _isInit = false; } -uint16_t WipperSnapper_Component_I2C::scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { +/*****************************************************************************************************/ +/*! + @brief Destructor for a WipperSnapper I2C component. + @param msgScanReq + A decoded I2C scan request message. + @returns The address which an I2C device is located, -1 otherwise. +*/ +/*****************************************************************************************************/ +uint16_t WipperSnapper_Component_I2C::scanAddresses( + wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { // decode stream into i2c request uint16_t addrFound = -1; uint16_t scanAddr; - WS_DEBUG_PRINT("EXEC: I2C Scan, Port ("); WS_DEBUG_PRINT(_portNum); WS_DEBUG_PRINTLN(")"); + WS_DEBUG_PRINT("EXEC: I2C Scan, Port ("); + WS_DEBUG_PRINT(_portNum); + WS_DEBUG_PRINTLN(")"); for (int i = 0; i < msgScanReq.address_count; i++) { scanAddr = msgScanReq.address[i]; - WS_DEBUG_PRINT("Scanning address "); WS_DEBUG_PRINTLN(scanAddr); + WS_DEBUG_PRINT("Scanning address "); + WS_DEBUG_PRINTLN(scanAddr); _i2c->beginTransmission(scanAddr); if (_i2c->endTransmission() == 0) { - // found it! - WS_DEBUG_PRINTLN("I2C device detected!"); - addrFound = scanAddr; - break; + // found it! + WS_DEBUG_PRINTLN("I2C device detected!"); + addrFound = scanAddr; + break; } else { - WS_DEBUG_PRINTLN("I2C device not detected!"); + WS_DEBUG_PRINTLN("I2C device not detected!"); } } return addrFound; } + +/*****************************************************************************************************/ +/*! + @brief Destructor for a WipperSnapper I2C component. + @param msgScanReq + A decoded I2C scan request message. + @returns The address which an I2C device is located, -1 otherwise. +*/ +/*****************************************************************************************************/ +bool WipperSnapper_Component_I2C::attachI2CDevice(uint32_t deviceAddress) { + bool attachSuccess = false; + return attachSuccess; +} \ No newline at end of file diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 21fdd16ed..81d78ec92 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -28,6 +28,7 @@ class WipperSnapper_Component_I2C { wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + bool attachI2CDevice(uint32_t deviceAddress); int32_t _portNum; bool _isInit; From 11ca6fc10835a0348b26256c6fd999d0d6e6545b Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 26 Jul 2021 11:52:08 -0400 Subject: [PATCH 35/95] decode new init device request --- src/Wippersnapper.cpp | 21 ++++++++++++-- .../WipperSnapper_Component_I2C.cpp | 28 +++++++++++++------ src/components/WipperSnapper_Component_I2C.h | 2 +- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 006e1ce2e..f6db7d878 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -526,10 +526,25 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, is_success = false; return is_success; } - } else if (field->tag = wippersnapper_signal_v1_I2CRequest_req_aht_init_tag) { + } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { WS_DEBUG_PRINTLN("AHTX Init Request Found!"); - // TODO: allow this to be implemented on multiple ports, but for now use - // port0 Initialize I2C device WS._i2cPort0->attachI2CDevice(address) + // Decode stream into an I2CDeviceInitRequest + // Create I2C request message + wippersnapper_i2c_v1_I2CDeviceInitRequest msgI2CDeviceInitRequest = wippersnapper_i2c_v1_I2CDeviceInitRequest_init_zero; + // Decode stream into struct, msgI2CDeviceInitRequest + if (!pb_decode(stream, wippersnapper_i2c_v1_I2CDeviceInitRequest_fields, + &msgI2CDeviceInitRequest)) { + WS_DEBUG_PRINTLN( + "ERROR: Could not decode I2CDeviceInitRequest message."); + is_success = false; + return is_success; + } + // Attach device to I2C port + // TODO: allow this to be implemented with multiple ports + WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); + + + // TODO // Configure sensors // WS._i2cPort0->enableSensor(temperature); // WS._i2cPort0->enableSensor(humidity); diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 6811f0990..b862baccd 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -96,15 +96,25 @@ uint16_t WipperSnapper_Component_I2C::scanAddresses( return addrFound; } -/*****************************************************************************************************/ -/*! - @brief Destructor for a WipperSnapper I2C component. - @param msgScanReq - A decoded I2C scan request message. - @returns The address which an I2C device is located, -1 otherwise. -*/ -/*****************************************************************************************************/ -bool WipperSnapper_Component_I2C::attachI2CDevice(uint32_t deviceAddress) { + +bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) { bool attachSuccess = false; + + // Determine which sensor-specific callback to utilize + if (msgDeviceInitReq->has_aht_init) { + WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); + // Call AHTX init + // and pass it wippersnapper_i2c_v1_AHTInitRequest + // TODO! + attachSuccess = true; + } else if (msgDeviceInitReq->has_dps310_init) { + WS_DEBUG_PRINTLN("Initializing DPS310 sensor!"); + // Call DPS310 init + // TODO! + attachSuccess = true; + } else { + WS_DEBUG_PRINTLN("ERROR: Sensor not found") + } + return attachSuccess; } \ No newline at end of file diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 81d78ec92..faa8cc484 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -28,7 +28,7 @@ class WipperSnapper_Component_I2C { wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); - bool attachI2CDevice(uint32_t deviceAddress); + bool attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); int32_t _portNum; bool _isInit; From a864be23f0599da3eeb46ca04dade82ea03c0eed Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 26 Jul 2021 15:32:07 -0400 Subject: [PATCH 36/95] add generic I2C device driver, specific AHTx driver which uses generic --- .../WipperSnapper_Component_I2C.cpp | 11 +++--- src/drivers/I2C_Driver.cpp | 24 +++++++++++++ src/drivers/I2C_Driver.h | 34 +++++++++++++++++++ src/drivers/I2C_Driver_AHTX0.cpp | 20 +++++++++++ src/drivers/I2C_Driver_AHTX0.h | 33 ++++++++++++++++++ 5 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 src/drivers/I2C_Driver.cpp create mode 100644 src/drivers/I2C_Driver.h create mode 100644 src/drivers/I2C_Driver_AHTX0.cpp create mode 100644 src/drivers/I2C_Driver_AHTX0.h diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index b862baccd..0b0b7ebbd 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -99,18 +99,17 @@ uint16_t WipperSnapper_Component_I2C::scanAddresses( bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) { bool attachSuccess = false; - // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); // Call AHTX init // and pass it wippersnapper_i2c_v1_AHTInitRequest + // or not... // TODO! - attachSuccess = true; - } else if (msgDeviceInitReq->has_dps310_init) { - WS_DEBUG_PRINTLN("Initializing DPS310 sensor!"); - // Call DPS310 init - // TODO! + // Unpack address + // Pass to new driver: _i2c object, address + // Detect + // Init attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp new file mode 100644 index 000000000..24e76268a --- /dev/null +++ b/src/drivers/I2C_Driver.cpp @@ -0,0 +1,24 @@ +/*! + * @file I2C_Driver.cpp + * + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#include "I2C_Driver.h" + +I2C_Driver::I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress) { + _i2cComponent = i2cComponent; + _i2cAddress = i2cAddress; +} + +bool I2C_Driver::init() { + // Base implementation of I2C device driver init func. + return true; +} \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h new file mode 100644 index 000000000..683ac02fa --- /dev/null +++ b/src/drivers/I2C_Driver.h @@ -0,0 +1,34 @@ +/*! + * @file I2C_driver.h + * + * Generic I2C device driver + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#ifndef I2C_Driver_H +#define I2C_Driver_H + +#include "Wippersnapper.h" +#include "../components/WipperSnapper_Component_I2C.h" + +// forward decl. +class Wippersnapper; + +class I2C_Driver { + public: + I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress); + virtual bool init(); + private: + WipperSnapper_Component_I2C *_i2cComponent = NULL; + uint16_t _i2cAddress; +}; +extern Wippersnapper WS; + +#endif // I2C_Driver_H \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp new file mode 100644 index 000000000..bea658ca7 --- /dev/null +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -0,0 +1,20 @@ +/*! + * @file I2C_Driver.cpp + * + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#include "I2C_Driver.h" + +bool I2C_Driver_AHTX0::init() override { + WS_DEBUG_PRINTLN("Initialize AHTX0 sensor"); + _ahtx0 = new Adafruit_AHTX0(this->_i2cAddress); + return true; +} \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h new file mode 100644 index 000000000..2a015546c --- /dev/null +++ b/src/drivers/I2C_Driver_AHTX0.h @@ -0,0 +1,33 @@ +/*! + * @file I2C_driver.h + * + * I2C device driver for AHTX0 temperature and humidity sensors. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#ifndef I2C_Driver_H +#define I2C_Driver_H +#include "Wippersnapper.h" +#include "I2C_Driver.h" +#include + +// forward decl. +class Wippersnapper; + +class I2C_Driver_AHTX0 : public I2C_Driver { + public: + //I2C_Driver_AHTX0(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress); + bool init() override; + private: + Adafruit_AHTX0 *_ahtx0 = NULL; +}; +extern Wippersnapper WS; + +#endif // I2C_Driver_H \ No newline at end of file From 01691cde61f97dd2937800fd15a7fb36ba43dfa4 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 26 Jul 2021 15:45:34 -0400 Subject: [PATCH 37/95] fix errors wrt driver generic --- src/drivers/I2C_Driver.cpp | 2 +- src/drivers/I2C_Driver.h | 2 +- src/drivers/I2C_Driver_AHTX0.cpp | 6 ++++-- src/drivers/I2C_Driver_AHTX0.h | 11 ++++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 24e76268a..387f18fc1 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -18,7 +18,7 @@ I2C_Driver::I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAd _i2cAddress = i2cAddress; } -bool I2C_Driver::init() { +bool I2C_Driver::initDriver() { // Base implementation of I2C device driver init func. return true; } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 683ac02fa..000bb2547 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -24,7 +24,7 @@ class Wippersnapper; class I2C_Driver { public: I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress); - virtual bool init(); + virtual bool initDriver(); private: WipperSnapper_Component_I2C *_i2cComponent = NULL; uint16_t _i2cAddress; diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp index bea658ca7..7a432598e 100644 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -12,9 +12,11 @@ * */ #include "I2C_Driver.h" +#include "I2C_Driver_AHTX0.h" -bool I2C_Driver_AHTX0::init() override { + +bool I2C_Driver_AHTX0::initDriver() { WS_DEBUG_PRINTLN("Initialize AHTX0 sensor"); - _ahtx0 = new Adafruit_AHTX0(this->_i2cAddress); + //_ahtx0 = new Adafruit_AHTX0(this->_i2cAddress); return true; } \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h index 2a015546c..fb639f1d9 100644 --- a/src/drivers/I2C_Driver_AHTX0.h +++ b/src/drivers/I2C_Driver_AHTX0.h @@ -12,8 +12,8 @@ * BSD license, all text here must be included in any redistribution. * */ -#ifndef I2C_Driver_H -#define I2C_Driver_H +#ifndef I2C_Driver_AHTX0_H +#define I2C_Driver_AHTX0_H #include "Wippersnapper.h" #include "I2C_Driver.h" #include @@ -23,11 +23,12 @@ class Wippersnapper; class I2C_Driver_AHTX0 : public I2C_Driver { public: - //I2C_Driver_AHTX0(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress); - bool init() override; + // Generic overrides + bool initDriver() override; + // Sensor-specific, TODO private: Adafruit_AHTX0 *_ahtx0 = NULL; }; extern Wippersnapper WS; -#endif // I2C_Driver_H \ No newline at end of file +#endif // I2C_Driver_AHTX0_H \ No newline at end of file From a1ebae0038e8b6cda1bbb6bfb2a12b5145d18424 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 26 Jul 2021 16:04:27 -0400 Subject: [PATCH 38/95] dont overwrrite broker topic --- src/Wippersnapper.cpp | 22 +++++++++----------- src/Wippersnapper.h | 2 +- src/components/WipperSnapper_Component_I2C.h | 1 - src/drivers/I2C_Driver.cpp | 4 ++-- src/drivers/I2C_Driver.h | 5 ++--- src/drivers/I2C_Driver_AHTX0.cpp | 9 +++++++- src/drivers/I2C_Driver_AHTX0.h | 2 ++ 7 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index f6db7d878..3e71867bf 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -419,7 +419,7 @@ void publishSignalI2CResponse( pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); // Publish i2c response message to broker - WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); + //WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); } /************************************************************************************/ @@ -898,7 +898,7 @@ bool Wippersnapper::buildWSTopics() { strcat(WS._topic_signal_i2c_brkr, _device_uid); strcat(WS._topic_signal_i2c_brkr, TOPIC_SIGNALS); strcat(WS._topic_signal_i2c_brkr, "broker"); - strcat(WS._topic_signal_i2c_brkr, TOPIC_I2C); + strcat(WS._topic_signal_i2c_brkr, "/i2c"); } else { // malloc failed WS._topic_signal_i2c_brkr = 0; is_success = false; @@ -906,14 +906,14 @@ bool Wippersnapper::buildWSTopics() { // Create broker-to-device i2c signal topic if (WS._topic_signal_i2c_device) { - strcpy(WS._topic_signal_i2c_brkr, WS._username); - strcat(WS._topic_signal_i2c_brkr, TOPIC_WS); - strcat(WS._topic_signal_i2c_brkr, _device_uid); - strcat(WS._topic_signal_i2c_brkr, TOPIC_SIGNALS); - strcat(WS._topic_signal_i2c_brkr, "device"); - strcat(WS._topic_signal_i2c_brkr, TOPIC_I2C); + strcpy(WS._topic_signal_i2c_device, WS._username); + strcat(WS._topic_signal_i2c_device, TOPIC_WS); + strcat(WS._topic_signal_i2c_device, _device_uid); + strcat(WS._topic_signal_i2c_device, TOPIC_SIGNALS); + strcat(WS._topic_signal_i2c_device, "device"); + strcat(WS._topic_signal_i2c_device, TOPIC_I2C); } else { // malloc failed - WS._topic_signal_i2c_brkr = 0; + WS._topic_signal_i2c_device = 0; is_success = false; } @@ -1219,9 +1219,7 @@ ws_status_t Wippersnapper::run() { checkMQTTConnection(curTime); // Poll for packets from broker queue, return immediately - if (!WS._mqtt->processPacketsUntilCallback(100)) { - WS_DEBUG_PRINTLN("No packets found!"); - } + WS._mqtt->processPacketsUntilCallback(100); // TODO: Loop thru components // Process digital inputs, digitalGPIO module diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 179fd9c15..81741b41e 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -60,7 +60,7 @@ #include // Uncomment for staging builds -// define USE_STAGING +#define USE_STAGING #ifdef USE_STAGING #define IO_MQTT_SERVER "io.adafruit.us" ///< Staging (development) mqtt server diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index faa8cc484..19871d968 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -32,7 +32,6 @@ class WipperSnapper_Component_I2C { int32_t _portNum; bool _isInit; - private: TwoWire *_i2c = NULL; }; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 387f18fc1..3e3960d0c 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -14,8 +14,8 @@ #include "I2C_Driver.h" I2C_Driver::I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress) { - _i2cComponent = i2cComponent; - _i2cAddress = i2cAddress; + i2cComponent = i2cComponent; + i2cAddress = i2cAddress; } bool I2C_Driver::initDriver() { diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 000bb2547..fd842e953 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -25,9 +25,8 @@ class I2C_Driver { public: I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress); virtual bool initDriver(); - private: - WipperSnapper_Component_I2C *_i2cComponent = NULL; - uint16_t _i2cAddress; + WipperSnapper_Component_I2C *i2cComponent = NULL; + uint16_t i2cAddress; }; extern Wippersnapper WS; diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp index 7a432598e..612cf5e87 100644 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -17,6 +17,13 @@ bool I2C_Driver_AHTX0::initDriver() { WS_DEBUG_PRINTLN("Initialize AHTX0 sensor"); - //_ahtx0 = new Adafruit_AHTX0(this->_i2cAddress); + // TODO: Use address propery from I2CDriver generic + // TODO: Get bus from I2CComponent + WS_DEBUG_PRINT("ON PORT: ") + WS_DEBUG_PRINTLN(this->i2cComponent->_portNum); + //_ahtx0 = new Adafruit_AHTX0(this->i2cComponent->_i2c); +/* if (!_ahtx0->begin()) { + return false; + } */ return true; } \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h index fb639f1d9..1c996eb1d 100644 --- a/src/drivers/I2C_Driver_AHTX0.h +++ b/src/drivers/I2C_Driver_AHTX0.h @@ -28,6 +28,8 @@ class I2C_Driver_AHTX0 : public I2C_Driver { // Sensor-specific, TODO private: Adafruit_AHTX0 *_ahtx0 = NULL; + Adafruit_Sensor *_ahtx0_humidity = NULL; + Adafruit_Sensor *_ahtx0_temperature = NULL; }; extern Wippersnapper WS; From dba6529267b94562e5e69a410fa538bcf20a0be3 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 26 Jul 2021 16:41:33 -0400 Subject: [PATCH 39/95] pub/resp working against broker --- src/Wippersnapper.cpp | 22 ++++++++++---------- src/Wippersnapper.h | 5 ++--- src/components/WipperSnapper_Component_I2C.h | 4 +++- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 3e71867bf..33e9ec476 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -419,7 +419,7 @@ void publishSignalI2CResponse( pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); // Publish i2c response message to broker - //WS._mqtt->publish(WS._topic_signal_i2c_brkr, WS._buffer_outgoing, msgSz, 1); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); } /************************************************************************************/ @@ -480,12 +480,12 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, return is_success; } // Create an I2C object using the message as a constructor? - if (WS._i2cPort0 != NULL) { + if (WS._i2cPort0 == NULL) { WS._i2cPort0 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); WS.i2cComponents.push_back(WS._i2cPort0); // did we init. the port successfully? is_success = WS._i2cPort0->_isInit; - } else if (WS._i2cPort1 != NULL) { + } else if (WS._i2cPort1 == NULL) { WS._i2cPort1 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); // did we init. the port successfully? is_success = WS._i2cPort1->_isInit; @@ -526,16 +526,17 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, is_success = false; return is_success; } - } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { + } else if (field->tag == + wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { WS_DEBUG_PRINTLN("AHTX Init Request Found!"); // Decode stream into an I2CDeviceInitRequest // Create I2C request message - wippersnapper_i2c_v1_I2CDeviceInitRequest msgI2CDeviceInitRequest = wippersnapper_i2c_v1_I2CDeviceInitRequest_init_zero; + wippersnapper_i2c_v1_I2CDeviceInitRequest msgI2CDeviceInitRequest = + wippersnapper_i2c_v1_I2CDeviceInitRequest_init_zero; // Decode stream into struct, msgI2CDeviceInitRequest if (!pb_decode(stream, wippersnapper_i2c_v1_I2CDeviceInitRequest_fields, &msgI2CDeviceInitRequest)) { - WS_DEBUG_PRINTLN( - "ERROR: Could not decode I2CDeviceInitRequest message."); + WS_DEBUG_PRINTLN("ERROR: Could not decode I2CDeviceInitRequest message."); is_success = false; return is_success; } @@ -543,7 +544,6 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, // TODO: allow this to be implemented with multiple ports WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); - // TODO // Configure sensors // WS._i2cPort0->enableSensor(temperature); @@ -558,7 +558,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, return is_success; } // Publish i2c scan response back to broker - WS_DEBUG_PRINT("Publishing I2C scan response...") + WS_DEBUG_PRINT("Publishing I2C response...") publishSignalI2CResponse(&msgi2cResponse); WS_DEBUG_PRINTLN("Published!"); return is_success; @@ -841,7 +841,7 @@ bool Wippersnapper::buildWSTopics() { WS._topic_signal_i2c_device = (char *)malloc( sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) + strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) + strlen("device") + - strlen(TOPIC_I2C) + 1); + strlen("/i2c") + 1); // Create global registration topic if (WS._topic_description) { @@ -911,7 +911,7 @@ bool Wippersnapper::buildWSTopics() { strcat(WS._topic_signal_i2c_device, _device_uid); strcat(WS._topic_signal_i2c_device, TOPIC_SIGNALS); strcat(WS._topic_signal_i2c_device, "device"); - strcat(WS._topic_signal_i2c_device, TOPIC_I2C); + strcat(WS._topic_signal_i2c_device, "/i2c"); } else { // malloc failed WS._topic_signal_i2c_device = 0; is_success = false; diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 81741b41e..f19791027 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -298,7 +298,8 @@ class Wippersnapper { char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in milliseconds. */ - + char *_topic_signal_i2c_device; /*!< Topic carries messages from a broker to a + device. */ private: void _init(); @@ -319,8 +320,6 @@ class Wippersnapper { char *_topic_description_status; /*!< MQTT subtopic carrying the description status resp. from the broker */ char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */ - char *_topic_signal_i2c_device; /*!< Topic carries messages from a broker to a - device. */ Adafruit_MQTT_Subscribe *_topic_description_sub; /*!< Subscription for registration topic. */ diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 19871d968..2f4970da6 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -28,10 +28,12 @@ class WipperSnapper_Component_I2C { wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); - bool attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); + bool + attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); int32_t _portNum; bool _isInit; + private: TwoWire *_i2c = NULL; }; From acbb12784a068c7d0d90fad48f07f6df2b161b95 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 11:00:12 -0400 Subject: [PATCH 40/95] properly publish/encode --- src/Wippersnapper.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 33e9ec476..5ff98ee6c 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -436,6 +436,7 @@ bool encodeSignalI2CResponse( bool is_success = true; // Zero-out buffer before writing memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + // Encode I2C response pb_ostream_t stream = pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); @@ -494,10 +495,19 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); is_success = false; } - if (!encodeSignalI2CResponse(&msgi2cResponse)) { - is_success = false; - return is_success; + // Create i2c init response + msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag; + msgi2cResponse.payload.resp_i2c_init.is_initialized = true; + // Encode message + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); + if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); + is_success = false; } + size_t msgSz; + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); @@ -557,10 +567,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, is_success = false; return is_success; } - // Publish i2c scan response back to broker - WS_DEBUG_PRINT("Publishing I2C response...") - publishSignalI2CResponse(&msgi2cResponse); - WS_DEBUG_PRINTLN("Published!"); + return is_success; } From 3d5f36c072d6adef1bb429e00dd4376fa5524002 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 12:10:03 -0400 Subject: [PATCH 41/95] encode and publish responses for scan and init, verified on broker-side --- src/Wippersnapper.cpp | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 5ff98ee6c..fa641ebaf 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -496,18 +496,25 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, is_success = false; } // Create i2c init response - msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag; + msgi2cResponse.which_payload = + wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag; msgi2cResponse.payload.resp_i2c_init.is_initialized = true; // Encode message memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); - if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - is_success = false; + pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); + is_success = false; } size_t msgSz; - pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse); - WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse); + WS_DEBUG_PRINT("Publishing i2c init response..."); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, + 1); + WS_DEBUG_PRINTLN("Published!"); } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); @@ -529,13 +536,27 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, // otherwise. uint16_t addressFound = WS._i2cPort0->scanAddresses(msgScanReq); // Create response + msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = - wippersnapper_i2c_v1_I2CScanResponse_address_found_tag; + wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; msgi2cResponse.payload.resp_i2c_scan.address_found = (uint32_t)addressFound; - if (!encodeSignalI2CResponse(&msgi2cResponse)) { + // Encode message + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); is_success = false; - return is_success; } + size_t msgSz; + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse); + // Publish + WS_DEBUG_PRINT("Publishing i2c response..."); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, + 1); + WS_DEBUG_PRINTLN("Published!"); } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { WS_DEBUG_PRINTLN("AHTX Init Request Found!"); From a8696514486b17f36464a11468949015ce4f137a Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 12:50:59 -0400 Subject: [PATCH 42/95] WIP removing errors --- src/Wippersnapper.cpp | 26 ------------------- .../WipperSnapper_Component_I2C.cpp | 2 ++ src/components/WipperSnapper_Component_I2C.h | 3 +++ src/drivers/I2C_Driver.cpp | 6 ++--- src/drivers/I2C_Driver.h | 9 ++++--- 5 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index fa641ebaf..b68ae6893 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -422,32 +422,6 @@ void publishSignalI2CResponse( WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); } -/************************************************************************************/ -/*! - @brief Encodes an I2C signal response message. Assumes the fields and - type have already been filled out in another function. - @param msgi2cResponse - I2CResponse signal message to encode. - @returns True if message is successfully encoded, False otherwise. -*/ -/************************************************************************************/ -bool encodeSignalI2CResponse( - wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { - bool is_success = true; - // Zero-out buffer before writing - memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - - // Encode I2C response - pb_ostream_t stream = - pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); - if (!pb_encode(&stream, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode i2cresponse message"); - is_success = false; - } - return is_success; -} - /******************************************************************************************/ /*! @brief Decodes an I2C signal request message and executes the diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 0b0b7ebbd..85053449e 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -102,6 +102,8 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); + //aht = new I2C_Driver_AHTX0(this, msgDeviceInitReq->aht_init.address); + //aht.initDriver(); // Call AHTX init // and pass it wippersnapper_i2c_v1_AHTInitRequest // or not... diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 2f4970da6..653473ceb 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -19,6 +19,8 @@ #include "Wippersnapper.h" #include +#include "drivers/I2C_Driver_AHTX0.h" + // forward decl. class Wippersnapper; @@ -36,6 +38,7 @@ class WipperSnapper_Component_I2C { private: TwoWire *_i2c = NULL; + // List of sensors }; extern Wippersnapper WS; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 3e3960d0c..37b5bca93 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -13,9 +13,9 @@ */ #include "I2C_Driver.h" -I2C_Driver::I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress) { - i2cComponent = i2cComponent; - i2cAddress = i2cAddress; +I2C_Driver::I2C_Driver(WipperSnapper_Component_I2C *i2c, uint16_t deviceAddress) { + i2cComponent = i2c; + i2cAddress = (uint16_t) deviceAddress; } bool I2C_Driver::initDriver() { diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index fd842e953..4d14ecef4 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -16,17 +16,18 @@ #define I2C_Driver_H #include "Wippersnapper.h" -#include "../components/WipperSnapper_Component_I2C.h" +#include "components/WipperSnapper_Component_I2C.h" -// forward decl. class Wippersnapper; +class WipperSnapper_Component_I2C; class I2C_Driver { public: - I2C_Driver(WipperSnapper_Component_I2C *i2cComponent, uint16_t i2cAddress); + I2C_Driver(WipperSnapper_Component_I2C *i2c, uint16_t deviceAddress); virtual bool initDriver(); WipperSnapper_Component_I2C *i2cComponent = NULL; - uint16_t i2cAddress; + uint16_t address; + }; extern Wippersnapper WS; From a54e6518d0c9063857fbd902ef7d9d703873454c Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 13:09:42 -0400 Subject: [PATCH 43/95] prune imports, extraneous code, reverting --- .../WipperSnapper_Component_I2C.cpp | 1 + src/components/WipperSnapper_Component_I2C.h | 3 +- src/drivers/I2C_Driver.cpp | 5 ++- src/drivers/I2C_Driver.h | 4 +-- src/drivers/I2C_Driver_AHTX0.cpp | 29 --------------- src/drivers/I2C_Driver_AHTX0.h | 36 ------------------- 6 files changed, 5 insertions(+), 73 deletions(-) delete mode 100644 src/drivers/I2C_Driver_AHTX0.cpp delete mode 100644 src/drivers/I2C_Driver_AHTX0.h diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 85053449e..bebce8e67 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -102,6 +102,7 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); + //sensorDriver = new I2C_Driver(this, msgDeviceInitReq->aht_init.address); //aht = new I2C_Driver_AHTX0(this, msgDeviceInitReq->aht_init.address); //aht.initDriver(); // Call AHTX init diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 653473ceb..167c0dd3c 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -19,7 +19,7 @@ #include "Wippersnapper.h" #include -#include "drivers/I2C_Driver_AHTX0.h" +// #include "drivers/I2C_Driver.h" // forward decl. class Wippersnapper; @@ -38,7 +38,6 @@ class WipperSnapper_Component_I2C { private: TwoWire *_i2c = NULL; - // List of sensors }; extern Wippersnapper WS; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 37b5bca93..f30ec7f3e 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -13,9 +13,8 @@ */ #include "I2C_Driver.h" -I2C_Driver::I2C_Driver(WipperSnapper_Component_I2C *i2c, uint16_t deviceAddress) { - i2cComponent = i2c; - i2cAddress = (uint16_t) deviceAddress; +I2C_Driver::I2C_Driver(uint16_t deviceAddress) { + // Base implementation } bool I2C_Driver::initDriver() { diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 4d14ecef4..359a56eed 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -19,13 +19,11 @@ #include "components/WipperSnapper_Component_I2C.h" class Wippersnapper; -class WipperSnapper_Component_I2C; class I2C_Driver { public: - I2C_Driver(WipperSnapper_Component_I2C *i2c, uint16_t deviceAddress); + I2C_Driver(uint16_t deviceAddress); virtual bool initDriver(); - WipperSnapper_Component_I2C *i2cComponent = NULL; uint16_t address; }; diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp deleted file mode 100644 index 612cf5e87..000000000 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/*! - * @file I2C_Driver.cpp - * - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ -#include "I2C_Driver.h" -#include "I2C_Driver_AHTX0.h" - - -bool I2C_Driver_AHTX0::initDriver() { - WS_DEBUG_PRINTLN("Initialize AHTX0 sensor"); - // TODO: Use address propery from I2CDriver generic - // TODO: Get bus from I2CComponent - WS_DEBUG_PRINT("ON PORT: ") - WS_DEBUG_PRINTLN(this->i2cComponent->_portNum); - //_ahtx0 = new Adafruit_AHTX0(this->i2cComponent->_i2c); -/* if (!_ahtx0->begin()) { - return false; - } */ - return true; -} \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h deleted file mode 100644 index 1c996eb1d..000000000 --- a/src/drivers/I2C_Driver_AHTX0.h +++ /dev/null @@ -1,36 +0,0 @@ -/*! - * @file I2C_driver.h - * - * I2C device driver for AHTX0 temperature and humidity sensors. - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ -#ifndef I2C_Driver_AHTX0_H -#define I2C_Driver_AHTX0_H -#include "Wippersnapper.h" -#include "I2C_Driver.h" -#include - -// forward decl. -class Wippersnapper; - -class I2C_Driver_AHTX0 : public I2C_Driver { - public: - // Generic overrides - bool initDriver() override; - // Sensor-specific, TODO - private: - Adafruit_AHTX0 *_ahtx0 = NULL; - Adafruit_Sensor *_ahtx0_humidity = NULL; - Adafruit_Sensor *_ahtx0_temperature = NULL; -}; -extern Wippersnapper WS; - -#endif // I2C_Driver_AHTX0_H \ No newline at end of file From 5bc8f5a23c3ae552ccc33d77149a7e1170ab731a Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 13:35:56 -0400 Subject: [PATCH 44/95] remove circular imports --- src/components/WipperSnapper_Component_I2C.cpp | 1 + src/components/WipperSnapper_Component_I2C.h | 5 +++-- src/drivers/I2C_Driver.cpp | 5 ++--- src/drivers/I2C_Driver.h | 8 +------- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index bebce8e67..50b8b78c4 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -102,6 +102,7 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); + //sensorDriver = new I2C_Driver(this, msgDeviceInitReq->aht_init.address); //aht = new I2C_Driver_AHTX0(this, msgDeviceInitReq->aht_init.address); //aht.initDriver(); diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 167c0dd3c..6acdc66e0 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -19,10 +19,9 @@ #include "Wippersnapper.h" #include -// #include "drivers/I2C_Driver.h" - // forward decl. class Wippersnapper; +class I2C_Driver; class WipperSnapper_Component_I2C { public: @@ -36,6 +35,8 @@ class WipperSnapper_Component_I2C { int32_t _portNum; bool _isInit; + I2C_Driver* sensor = NULL; + private: TwoWire *_i2c = NULL; }; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index f30ec7f3e..3926ea17b 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -17,7 +17,6 @@ I2C_Driver::I2C_Driver(uint16_t deviceAddress) { // Base implementation } -bool I2C_Driver::initDriver() { - // Base implementation of I2C device driver init func. - return true; +I2C_Driver::~I2C_Driver() { + // Base implementation } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 359a56eed..477fc4454 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -16,17 +16,11 @@ #define I2C_Driver_H #include "Wippersnapper.h" -#include "components/WipperSnapper_Component_I2C.h" - -class Wippersnapper; class I2C_Driver { public: I2C_Driver(uint16_t deviceAddress); - virtual bool initDriver(); - uint16_t address; - + ~I2C_Driver(); }; -extern Wippersnapper WS; #endif // I2C_Driver_H \ No newline at end of file From 0b3287a68f960ae4fc194651d018ccd9de699726 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 14:18:10 -0400 Subject: [PATCH 45/95] init generic --- src/components/WipperSnapper_Component_I2C.cpp | 4 +++- src/components/WipperSnapper_Component_I2C.h | 4 ++-- src/drivers/I2C_Driver.cpp | 4 +++- src/drivers/I2C_Driver.h | 4 +++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 50b8b78c4..73096d5e8 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -102,7 +102,9 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); - + uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; + I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); + //sensorDriver = new I2C_Driver(this, msgDeviceInitReq->aht_init.address); //aht = new I2C_Driver_AHTX0(this, msgDeviceInitReq->aht_init.address); //aht.initDriver(); diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 6acdc66e0..8be493c81 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -19,9 +19,10 @@ #include "Wippersnapper.h" #include +#include "drivers/I2C_Driver.h" + // forward decl. class Wippersnapper; -class I2C_Driver; class WipperSnapper_Component_I2C { public: @@ -35,7 +36,6 @@ class WipperSnapper_Component_I2C { int32_t _portNum; bool _isInit; - I2C_Driver* sensor = NULL; private: TwoWire *_i2c = NULL; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 3926ea17b..74401b02d 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -13,8 +13,10 @@ */ #include "I2C_Driver.h" -I2C_Driver::I2C_Driver(uint16_t deviceAddress) { +I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { // Base implementation + WS_DEBUG_PRINTLN("I2CDriver Initialized!"); + WS_DEBUG_PRINT("I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); } I2C_Driver::~I2C_Driver() { diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 477fc4454..1873f5358 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -19,8 +19,10 @@ class I2C_Driver { public: - I2C_Driver(uint16_t deviceAddress); + I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); + + TwoWire *i2c = NULL; }; #endif // I2C_Driver_H \ No newline at end of file From a1c58741a431978b67f485b88686b798cffa1e20 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 15:47:18 -0400 Subject: [PATCH 46/95] Add: generic init sensor, specific enable temperature object Adafruit_Sensor --- .../WipperSnapper_Component_I2C.cpp | 19 ++++++-------- src/components/WipperSnapper_Component_I2C.h | 1 - src/drivers/I2C_Driver.cpp | 26 +++++++++++++++++++ src/drivers/I2C_Driver.h | 16 +++++++++++- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 73096d5e8..784587f91 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -103,19 +103,16 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; + // Allocate space for driver I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); + // Attempt to initialize the sensor + if (!p1->initSensor()) { + attachSuccess = false; + } + // Set sensor properties + p1->enableSensorTemperature(); + p1->enableSensorHumidity(); - //sensorDriver = new I2C_Driver(this, msgDeviceInitReq->aht_init.address); - //aht = new I2C_Driver_AHTX0(this, msgDeviceInitReq->aht_init.address); - //aht.initDriver(); - // Call AHTX init - // and pass it wippersnapper_i2c_v1_AHTInitRequest - // or not... - // TODO! - // Unpack address - // Pass to new driver: _i2c object, address - // Detect - // Init attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 8be493c81..c209ad35e 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -36,7 +36,6 @@ class WipperSnapper_Component_I2C { int32_t _portNum; bool _isInit; - private: TwoWire *_i2c = NULL; }; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 74401b02d..2fa3bf60b 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -17,8 +17,34 @@ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { // Base implementation WS_DEBUG_PRINTLN("I2CDriver Initialized!"); WS_DEBUG_PRINT("I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); + _i2c = i2c; + _pollPeriod = 0.0; } I2C_Driver::~I2C_Driver() { // Base implementation +} + +bool I2C_Driver::initSensor() { + bool is_success = true; + _ahtx0 = new Adafruit_AHTX0(); + if (!_ahtx0->begin(_i2c)) { + WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); + is_success = false; + }; + WS_DEBUG_PRINTLN("AHT initialized successfully!"); + return is_success; +} + +void I2C_Driver::setPeriod(float periodMs) { + _pollPeriod = periodMs; +} + +// AHT-Specific +void I2C_Driver::enableSensorTemperature() { + _aht_temperature = _ahtx0->getTemperatureSensor(); +} + +void I2C_Driver::enableSensorHumidity() { + _aht_temperature = _ahtx0->getHumiditySensor(); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 1873f5358..c58662b27 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -16,13 +16,27 @@ #define I2C_Driver_H #include "Wippersnapper.h" +#include class I2C_Driver { public: I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); + // GENERIC, virtual + bool initSensor(); + void setPeriod(float periodMs); + // AHT-Specific functions + void enableSensorTemperature(); + void enableSensorHumidity(); - TwoWire *i2c = NULL; + TwoWire *_i2c = NULL; + Adafruit_AHTX0 *_ahtx0 = NULL; + private: + // Generic + float _pollPeriod; + // AHT-Specific + Adafruit_Sensor *_aht_temperature = NULL; + Adafruit_Sensor *_aht_humidity = NULL; }; #endif // I2C_Driver_H \ No newline at end of file From d57cd926052d3c67675ebb18c8364ab052d2d1fc Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 15:56:49 -0400 Subject: [PATCH 47/95] add vector for activeDrivers --- src/components/WipperSnapper_Component_I2C.cpp | 4 ++-- src/components/WipperSnapper_Component_I2C.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 784587f91..5504aa589 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -112,11 +112,11 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // Set sensor properties p1->enableSensorTemperature(); p1->enableSensorHumidity(); - + // Push to vector containing sensors? + activeDrivers.push_back(p1); attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") } - return attachSuccess; } \ No newline at end of file diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index c209ad35e..bef0630c6 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -23,6 +23,7 @@ // forward decl. class Wippersnapper; +class I2C_Driver; class WipperSnapper_Component_I2C { public: @@ -35,6 +36,8 @@ class WipperSnapper_Component_I2C { int32_t _portNum; bool _isInit; + // I2C_Driver + std::vector activeDrivers; private: TwoWire *_i2c = NULL; From fdb5fe553fc417c00e905f134da3836efd026a86 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 27 Jul 2021 16:10:00 -0400 Subject: [PATCH 48/95] add overhead for polling eventually --- src/components/WipperSnapper_Component_I2C.cpp | 13 ++++++++----- src/drivers/I2C_Driver.cpp | 18 ++++++++++++++---- src/drivers/I2C_Driver.h | 17 ++++++++++------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 5504aa589..bf323e15f 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -103,20 +103,23 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; - // Allocate space for driver + // Allocate space for I2C_Driver I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); - // Attempt to initialize the sensor + // Attempt to initialize the sensor driver if (!p1->initSensor()) { attachSuccess = false; } - // Set sensor properties + // Set sensor driver properties p1->enableSensorTemperature(); p1->enableSensorHumidity(); - // Push to vector containing sensors? + // Push to vector containing sensor drivers activeDrivers.push_back(p1); attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") } return attachSuccess; -} \ No newline at end of file +} + +// TODO! +// Poll attached i2c sensor devices for new values here diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 2fa3bf60b..2eeb1ed01 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -1,6 +1,7 @@ /*! * @file I2C_Driver.cpp * + * Base class for a generic I2C sensor device driver. * * Adafruit invests time and resources providing this open source code, * please support Adafruit and open-source hardware by purchasing @@ -13,6 +14,7 @@ */ #include "I2C_Driver.h" +// Generic I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { // Base implementation WS_DEBUG_PRINTLN("I2CDriver Initialized!"); @@ -21,10 +23,17 @@ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { _pollPeriod = 0.0; } +// Generic I2C_Driver::~I2C_Driver() { // Base implementation } +// Generic +void I2C_Driver::setPeriod(float periodMs) { + _pollPeriod = periodMs; +} + +// Specific, virtual bool I2C_Driver::initSensor() { bool is_success = true; _ahtx0 = new Adafruit_AHTX0(); @@ -36,15 +45,16 @@ bool I2C_Driver::initSensor() { return is_success; } -void I2C_Driver::setPeriod(float periodMs) { - _pollPeriod = periodMs; +// Specific, virtual +void I2C_Driver::pollSensor() { + // TODO - Validate sensors and poll them } // AHT-Specific void I2C_Driver::enableSensorTemperature() { - _aht_temperature = _ahtx0->getTemperatureSensor(); + _ahtTemperature = _ahtx0->getTemperatureSensor(); } void I2C_Driver::enableSensorHumidity() { - _aht_temperature = _ahtx0->getHumiditySensor(); + _ahtHumidity = _ahtx0->getHumiditySensor(); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index c58662b27..9f6078d40 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -1,7 +1,7 @@ /*! * @file I2C_driver.h * - * Generic I2C device driver + * Base class for a generic I2C sensor device driver. * * Adafruit invests time and resources providing this open source code, * please support Adafruit and open-source hardware by purchasing @@ -22,21 +22,24 @@ class I2C_Driver { public: I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); - // GENERIC, virtual - bool initSensor(); + // GENERIC, shared void setPeriod(float periodMs); - // AHT-Specific functions + // AHT-Specific functions, virtual + bool initSensor(); + void pollSensor(); void enableSensorTemperature(); void enableSensorHumidity(); + // Generic TwoWire *_i2c = NULL; + // Specific (can be private maybe?) Adafruit_AHTX0 *_ahtx0 = NULL; private: // Generic float _pollPeriod; - // AHT-Specific - Adafruit_Sensor *_aht_temperature = NULL; - Adafruit_Sensor *_aht_humidity = NULL; + // AHT-Specific sensor properties + Adafruit_Sensor *_ahtTemperature = NULL; + Adafruit_Sensor *_ahtHumidity = NULL; }; #endif // I2C_Driver_H \ No newline at end of file From ea608f0fe13a4ca47fe234eaf2b9df56dc9085eb Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 28 Jul 2021 14:42:40 -0400 Subject: [PATCH 49/95] processuntil, remove ProcessPackets for initial commands --- src/Wippersnapper.cpp | 3 --- src/drivers/I2C_Driver.cpp | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index b68ae6893..5d597ad0c 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1029,9 +1029,6 @@ void Wippersnapper::connect() { WS_DEBUG_PRINTLN("Registered board with Wippersnapper."); statusLEDBlink(WS_LED_STATUS_CONNECTED); statusLEDDeinit(); - - // Attempt to process initial sync packets from broker - WS._mqtt->processPackets(500); } /**************************************************************************/ diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 2eeb1ed01..934e45230 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -53,8 +53,10 @@ void I2C_Driver::pollSensor() { // AHT-Specific void I2C_Driver::enableSensorTemperature() { _ahtTemperature = _ahtx0->getTemperatureSensor(); + WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); } void I2C_Driver::enableSensorHumidity() { _ahtHumidity = _ahtx0->getHumiditySensor(); + WS_DEBUG_PRINTLN("Enabled AHTX0 humidity sensor"); } \ No newline at end of file From b630816b0b2086a9d8e3536f1aa613a5e6a75a90 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 28 Jul 2021 16:10:26 -0400 Subject: [PATCH 50/95] make I2c_driver a parent, i2c_driver_ahtx0 a child class --- .../WipperSnapper_Component_I2C.cpp | 3 - src/drivers/I2C_Driver.cpp | 22 +------ src/drivers/I2C_Driver.h | 17 ++---- src/drivers/I2C_Driver_AHTX0.cpp | 61 +++++++++++++++++++ src/drivers/I2C_Driver_AHTX0.h | 47 ++++++++++++++ 5 files changed, 114 insertions(+), 36 deletions(-) create mode 100644 src/drivers/I2C_Driver_AHTX0.cpp create mode 100644 src/drivers/I2C_Driver_AHTX0.h diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index bf323e15f..9bb38823d 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -109,9 +109,6 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (!p1->initSensor()) { attachSuccess = false; } - // Set sensor driver properties - p1->enableSensorTemperature(); - p1->enableSensorHumidity(); // Push to vector containing sensor drivers activeDrivers.push_back(p1); attachSuccess = true; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 934e45230..30729f9ff 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -33,30 +33,12 @@ void I2C_Driver::setPeriod(float periodMs) { _pollPeriod = periodMs; } -// Specific, virtual bool I2C_Driver::initSensor() { - bool is_success = true; - _ahtx0 = new Adafruit_AHTX0(); - if (!_ahtx0->begin(_i2c)) { - WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); - is_success = false; - }; - WS_DEBUG_PRINTLN("AHT initialized successfully!"); - return is_success; + // base calls/* */ + return true; } // Specific, virtual void I2C_Driver::pollSensor() { // TODO - Validate sensors and poll them -} - -// AHT-Specific -void I2C_Driver::enableSensorTemperature() { - _ahtTemperature = _ahtx0->getTemperatureSensor(); - WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); -} - -void I2C_Driver::enableSensorHumidity() { - _ahtHumidity = _ahtx0->getHumiditySensor(); - WS_DEBUG_PRINTLN("Enabled AHTX0 humidity sensor"); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 9f6078d40..bc544b7d5 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -1,5 +1,5 @@ /*! - * @file I2C_driver.h + * @file I2C_Driver.h * * Base class for a generic I2C sensor device driver. * @@ -16,30 +16,21 @@ #define I2C_Driver_H #include "Wippersnapper.h" -#include class I2C_Driver { public: + // GENERIC, shared I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); - // GENERIC, shared void setPeriod(float periodMs); // AHT-Specific functions, virtual - bool initSensor(); - void pollSensor(); - void enableSensorTemperature(); - void enableSensorHumidity(); - + virtual bool initSensor(); + virtual void pollSensor(); // Generic TwoWire *_i2c = NULL; - // Specific (can be private maybe?) - Adafruit_AHTX0 *_ahtx0 = NULL; private: // Generic float _pollPeriod; - // AHT-Specific sensor properties - Adafruit_Sensor *_ahtTemperature = NULL; - Adafruit_Sensor *_ahtHumidity = NULL; }; #endif // I2C_Driver_H \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp new file mode 100644 index 000000000..b4665c241 --- /dev/null +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -0,0 +1,61 @@ +/*! + * @file I2C_Driver_AHTX0.cpp + * + * Subclass for an AHT10 & AHT20 Humidity and Temperature Sensor. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#include "I2C_Driver_AHTX0.h" + +/* // Generic +I2C_Driver_AHTX0::I2C_Driver_AHTX0(uint16_t deviceAddress, TwoWire *i2c) { + // Base implementation + WS_DEBUG_PRINTLN("I2CDriver Initialized!"); + WS_DEBUG_PRINT("I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); + _i2c = i2c; + _pollPeriod = 0.0; +} + +// Generic +I2C_Driver_AHTX0::~I2C_Driver_AHTX0() { + // Base implementation +} + */ + +// Specific, virtual +bool I2C_Driver_AHTX0::initSensor() { + bool is_success = true; + _ahtx0 = new Adafruit_AHTX0(); + if (!_ahtx0->begin(_i2c)) { + WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); + is_success = false; + }; + // TODO: Enable temp and humid too + // enableSensorTemperature(); + //enableSensorHumidity(); + WS_DEBUG_PRINTLN("AHT initialized successfully!"); + return is_success; +} + +// Specific, virtual +void I2C_Driver_AHTX0::pollSensor() { + // TODO - Validate sensors and poll them +} + +// AHT-Specific +void I2C_Driver_AHTX0::enableSensorTemperature() { + _ahtTemperature = _ahtx0->getTemperatureSensor(); + WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); +} + +void I2C_Driver_AHTX0::enableSensorHumidity() { + _ahtHumidity = _ahtx0->getHumiditySensor(); + WS_DEBUG_PRINTLN("Enabled AHTX0 humidity sensor"); +} \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h new file mode 100644 index 000000000..fd3ca2e56 --- /dev/null +++ b/src/drivers/I2C_Driver_AHTX0.h @@ -0,0 +1,47 @@ +/*! + * @file I2C_Driver_AHTX0.h + * + * Subclass for an AHT10 & AHT20 Humidity and Temperature Sensor. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ +#ifndef I2C_Driver_AHTX0_H +#define I2C_Driver_AHTX0_H + +#include "Wippersnapper.h" +#include "I2C_Driver.h" +#include + +class I2C_Driver_AHTX0 : public I2C_Driver { + public: + //I2C_Driver_AHTX0(uint16_t deviceAddress, TwoWire *i2c); + //~I2C_Driver_AHTX0(); + using I2C_Driver::I2C_Driver; + // GENERIC, shared +// void setPeriod(float periodMs); + bool initSensor() override; + void pollSensor() override; + // AHT-specific + void enableSensorTemperature(); + void enableSensorHumidity(); + + // Generic + TwoWire *_i2c = NULL; + // Specific (can be private maybe?) + Adafruit_AHTX0 *_ahtx0 = NULL; + private: + // Generic + float _pollPeriod; + // AHT-Specific sensor properties + Adafruit_Sensor *_ahtTemperature = NULL; + Adafruit_Sensor *_ahtHumidity = NULL; +}; + +#endif // I2C_Driver_AHTX0_H \ No newline at end of file From 877d0e3311b859ae3c5bded54f036755a3be5527 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 28 Jul 2021 17:15:10 -0400 Subject: [PATCH 51/95] documentation, destructor --- .../WipperSnapper_Component_I2C.cpp | 10 ++++ src/drivers/I2C_Driver.cpp | 57 +++++++++++++++---- src/drivers/I2C_Driver.h | 3 +- src/drivers/I2C_Driver_AHTX0.cpp | 39 +++++++------ 4 files changed, 78 insertions(+), 31 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 9bb38823d..dd9b520a9 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -97,12 +97,22 @@ uint16_t WipperSnapper_Component_I2C::scanAddresses( } +/*******************************************************************************/ +/*! + @brief Initializes I2C device driver and attaches its object to the "bus" + @param msgDeviceInitReq + A decoded I2CDevice initialization request message. + @returns True if I2C device is initialized and attached, False otherwise. +*/ +/*******************************************************************************/ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) { bool attachSuccess = false; // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; + // TODO: Allocate I2C Driver AHT!! + // Allocate space for I2C_Driver I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 30729f9ff..230b20511 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -14,31 +14,64 @@ */ #include "I2C_Driver.h" -// Generic +/*******************************************************************************/ +/*! + @brief Creates a generic I2C Driver object. + @param deviceAddress + The i2c device's address. + @param _i2c + The desired i2c port. +*/ +/*******************************************************************************/ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { - // Base implementation - WS_DEBUG_PRINTLN("I2CDriver Initialized!"); - WS_DEBUG_PRINT("I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); + WS_DEBUG_PRINTLN("* New I2CDriver!"); + WS_DEBUG_PRINT("* I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); _i2c = i2c; _pollPeriod = 0.0; } -// Generic +/*************************************/ +/*! + @brief I2C driver destructor. +*/ +/*************************************/ I2C_Driver::~I2C_Driver() { - // Base implementation + _i2c = NULL; + _pollPeriod = 0.0; } -// Generic -void I2C_Driver::setPeriod(float periodMs) { - _pollPeriod = periodMs; +/*******************************************/ +/*! + @brief Sets an I2C device's + polling period, in seconds. +*/ +/*******************************************/ +void I2C_Driver::setPeriod(float period) { + _pollPeriod = period; } +/*************************************************/ +/*! + @brief Initializes an I2C driver. Must + be implemented in an I2C_Driver_x class. + @return True if I2C device driver initialized + successfully, False otherwise. +*/ +/*************************************************/ bool I2C_Driver::initSensor() { - // base calls/* */ + WS_DEBUG("I2C_Driver initSensor()"); return true; } -// Specific, virtual +/*************************************************/ +/*! + @brief Polls an I2C driver for new sensor + messages. Must be implemented in an + I2C_Driver_x class. + @return True if I2C device driver initialized + successfully, False otherwise. +*/ +/*************************************************/ void I2C_Driver::pollSensor() { - // TODO - Validate sensors and poll them + WS_DEBUG("I2C_Driver PollSensor()"); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index bc544b7d5..70f70f2c2 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -22,8 +22,7 @@ class I2C_Driver { // GENERIC, shared I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); - void setPeriod(float periodMs); - // AHT-Specific functions, virtual + void setPeriod(float period); virtual bool initSensor(); virtual void pollSensor(); // Generic diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp index b4665c241..9aa627a00 100644 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -14,22 +14,14 @@ */ #include "I2C_Driver_AHTX0.h" -/* // Generic -I2C_Driver_AHTX0::I2C_Driver_AHTX0(uint16_t deviceAddress, TwoWire *i2c) { - // Base implementation - WS_DEBUG_PRINTLN("I2CDriver Initialized!"); - WS_DEBUG_PRINT("I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); - _i2c = i2c; - _pollPeriod = 0.0; -} - -// Generic -I2C_Driver_AHTX0::~I2C_Driver_AHTX0() { - // Base implementation -} - */ -// Specific, virtual +/*************************************************/ +/*! + @brief Initializes an AHTX0 sensor. + @return True if AHTX0 device driver initialized + successfully, False otherwise. +*/ +/*************************************************/ bool I2C_Driver_AHTX0::initSensor() { bool is_success = true; _ahtx0 = new Adafruit_AHTX0(); @@ -44,17 +36,30 @@ bool I2C_Driver_AHTX0::initSensor() { return is_success; } -// Specific, virtual +/*************************************************/ +/*! + @brief Polls the AHTX0 for new data. +*/ +/*************************************************/ void I2C_Driver_AHTX0::pollSensor() { // TODO - Validate sensors and poll them } -// AHT-Specific +/**************************************************/ +/*! + @brief Enables the AHTX0's temperature sensor. +*/ +/**************************************************/ void I2C_Driver_AHTX0::enableSensorTemperature() { _ahtTemperature = _ahtx0->getTemperatureSensor(); WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); } +/**************************************************/ +/*! + @brief Enables the AHTX0's humidity sensor. +*/ +/**************************************************/ void I2C_Driver_AHTX0::enableSensorHumidity() { _ahtHumidity = _ahtx0->getHumiditySensor(); WS_DEBUG_PRINTLN("Enabled AHTX0 humidity sensor"); From 5a01cc4fef02684c66778da526cf5ecc2f848786 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 28 Jul 2021 17:52:51 -0400 Subject: [PATCH 52/95] parse out the enableX fields --- .../WipperSnapper_Component_I2C.cpp | 20 ++++++++++++------- src/components/WipperSnapper_Component_I2C.h | 1 + src/drivers/I2C_Driver_AHTX0.cpp | 3 --- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index dd9b520a9..47969ff80 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -111,14 +111,23 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; - // TODO: Allocate I2C Driver AHT!! - - // Allocate space for I2C_Driver + // TODO: Create I2C Driver using the AHT driver class!! + // Create new AHTX0 i2c sensor driver I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver if (!p1->initSensor()) { attachSuccess = false; } + // Initialize device-specific sensors + if (msgDeviceInitReq->aht_init.enable_temperature == True) { + WS_DEBUG_PRINT("Enabled: AHTX0 temperature sensor"); + // TODO, AHTX object.enable here! + } + if (msgDeviceInitReq->aht_init.enable_humidity == True) { + WS_DEBUG_PRINT("Enabled: AHTX0 humidity sensor"); + // TODO, AHTX object.enable here! + } + // NOTE: This is generic // Push to vector containing sensor drivers activeDrivers.push_back(p1); attachSuccess = true; @@ -126,7 +135,4 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice WS_DEBUG_PRINTLN("ERROR: Sensor not found") } return attachSuccess; -} - -// TODO! -// Poll attached i2c sensor devices for new values here +} \ No newline at end of file diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index bef0630c6..f01f9d385 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -20,6 +20,7 @@ #include #include "drivers/I2C_Driver.h" +#include "drivers/I2C_Driver_AHTX0.h" // forward decl. class Wippersnapper; diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp index 9aa627a00..ac974a919 100644 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -29,9 +29,6 @@ bool I2C_Driver_AHTX0::initSensor() { WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); is_success = false; }; - // TODO: Enable temp and humid too - // enableSensorTemperature(); - //enableSensorHumidity(); WS_DEBUG_PRINTLN("AHT initialized successfully!"); return is_success; } From 1a4baa3a32e69a7bc63e33275facad21be6e6863 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 11:06:49 -0400 Subject: [PATCH 53/95] remove circular import i2c_driver --- src/components/WipperSnapper_Component_I2C.cpp | 14 ++++++++++---- src/components/WipperSnapper_Component_I2C.h | 1 - src/drivers/I2C_Driver_AHTX0.cpp | 5 ++++- src/drivers/I2C_Driver_AHTX0.h | 5 ----- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 47969ff80..cf4de27b0 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -113,24 +113,30 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; // TODO: Create I2C Driver using the AHT driver class!! // Create new AHTX0 i2c sensor driver - I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); + //I2C_Driver_AHTX0 * aht = new I2C_Driver_AHTX0(addr, this->_i2c); + + + + +/* I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver if (!p1->initSensor()) { attachSuccess = false; + return attachSuccess; } // Initialize device-specific sensors - if (msgDeviceInitReq->aht_init.enable_temperature == True) { + if (msgDeviceInitReq->aht_init.enable_temperature == true) { WS_DEBUG_PRINT("Enabled: AHTX0 temperature sensor"); // TODO, AHTX object.enable here! } - if (msgDeviceInitReq->aht_init.enable_humidity == True) { + if (msgDeviceInitReq->aht_init.enable_humidity == true) { WS_DEBUG_PRINT("Enabled: AHTX0 humidity sensor"); // TODO, AHTX object.enable here! } // NOTE: This is generic // Push to vector containing sensor drivers activeDrivers.push_back(p1); - attachSuccess = true; + attachSuccess = true; */ } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") } diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index f01f9d385..bef0630c6 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -20,7 +20,6 @@ #include #include "drivers/I2C_Driver.h" -#include "drivers/I2C_Driver_AHTX0.h" // forward decl. class Wippersnapper; diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp index ac974a919..7f8f1c4dd 100644 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ b/src/drivers/I2C_Driver_AHTX0.cpp @@ -39,7 +39,8 @@ bool I2C_Driver_AHTX0::initSensor() { */ /*************************************************/ void I2C_Driver_AHTX0::pollSensor() { - // TODO - Validate sensors and poll them + // TODO - poll them + WS_DEBUG_PRINTLN("I2C_Driver_AHTX0::pollSensor"); } /**************************************************/ @@ -49,6 +50,7 @@ void I2C_Driver_AHTX0::pollSensor() { /**************************************************/ void I2C_Driver_AHTX0::enableSensorTemperature() { _ahtTemperature = _ahtx0->getTemperatureSensor(); + _ahtTemperature->printSensorDetails(); WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); } @@ -59,5 +61,6 @@ void I2C_Driver_AHTX0::enableSensorTemperature() { /**************************************************/ void I2C_Driver_AHTX0::enableSensorHumidity() { _ahtHumidity = _ahtx0->getHumiditySensor(); + _ahtHumidity->printSensorDetails(); WS_DEBUG_PRINTLN("Enabled AHTX0 humidity sensor"); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h index fd3ca2e56..0ec6d2ea8 100644 --- a/src/drivers/I2C_Driver_AHTX0.h +++ b/src/drivers/I2C_Driver_AHTX0.h @@ -21,11 +21,6 @@ class I2C_Driver_AHTX0 : public I2C_Driver { public: - //I2C_Driver_AHTX0(uint16_t deviceAddress, TwoWire *i2c); - //~I2C_Driver_AHTX0(); - using I2C_Driver::I2C_Driver; - // GENERIC, shared -// void setPeriod(float periodMs); bool initSensor() override; void pollSensor() override; // AHT-specific From b1b293fdd630e5edee58e7ba1c7697620293b6c5 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 11:38:08 -0400 Subject: [PATCH 54/95] add i2c.h for applicaiton --- src/Wippersnapper.h | 3 +++ src/components/WipperSnapper_Component_I2C.cpp | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index f19791027..e170e8491 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -42,6 +42,9 @@ // WipperSnapper I2C Component #include "components/WipperSnapper_Component_I2C.h" +// TODO - break this out +// I2C Sensor Drivers +#include "drivers/I2C_Driver_AHTX0.h" // External libraries #include "Adafruit_MQTT.h" // MQTT Client diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index cf4de27b0..8dba2bbb4 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -115,9 +115,6 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // Create new AHTX0 i2c sensor driver //I2C_Driver_AHTX0 * aht = new I2C_Driver_AHTX0(addr, this->_i2c); - - - /* I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver if (!p1->initSensor()) { From fa256983d248ef25e92cca32654dadc808c5102b Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 12:14:06 -0400 Subject: [PATCH 55/95] use I2C driver, get rid of aht class, make less generic --- src/Wippersnapper.h | 2 +- .../WipperSnapper_Component_I2C.cpp | 13 ++-- src/components/WipperSnapper_Component_I2C.h | 2 - src/drivers/I2C_Driver.cpp | 19 ++++++ src/drivers/I2C_Driver.h | 9 +++ src/drivers/I2C_Driver_AHTX0.cpp | 66 ------------------- src/drivers/I2C_Driver_AHTX0.h | 42 ------------ 7 files changed, 34 insertions(+), 119 deletions(-) delete mode 100644 src/drivers/I2C_Driver_AHTX0.cpp delete mode 100644 src/drivers/I2C_Driver_AHTX0.h diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index e170e8491..77a9bfed4 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -44,7 +44,7 @@ #include "components/WipperSnapper_Component_I2C.h" // TODO - break this out // I2C Sensor Drivers -#include "drivers/I2C_Driver_AHTX0.h" +//#include "drivers/I2C_Driver_AHTX0.h" // External libraries #include "Adafruit_MQTT.h" // MQTT Client diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 8dba2bbb4..9057a224f 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -111,20 +111,17 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (msgDeviceInitReq->has_aht_init) { WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; - // TODO: Create I2C Driver using the AHT driver class!! - // Create new AHTX0 i2c sensor driver - //I2C_Driver_AHTX0 * aht = new I2C_Driver_AHTX0(addr, this->_i2c); - -/* I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); + // TODO: Create I2C Driver using the an AHT driver sub-class! + I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver - if (!p1->initSensor()) { + if (!p1->initAHTX0()) { attachSuccess = false; return attachSuccess; } // Initialize device-specific sensors if (msgDeviceInitReq->aht_init.enable_temperature == true) { WS_DEBUG_PRINT("Enabled: AHTX0 temperature sensor"); - // TODO, AHTX object.enable here! + void enableAHTX0Temperature(); } if (msgDeviceInitReq->aht_init.enable_humidity == true) { WS_DEBUG_PRINT("Enabled: AHTX0 humidity sensor"); @@ -133,7 +130,7 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // NOTE: This is generic // Push to vector containing sensor drivers activeDrivers.push_back(p1); - attachSuccess = true; */ + attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") } diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index bef0630c6..382f669ee 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -38,8 +38,6 @@ class WipperSnapper_Component_I2C { bool _isInit; // I2C_Driver std::vector activeDrivers; - -private: TwoWire *_i2c = NULL; }; extern Wippersnapper WS; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 230b20511..3ec2fa086 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -74,4 +74,23 @@ bool I2C_Driver::initSensor() { /*************************************************/ void I2C_Driver::pollSensor() { WS_DEBUG("I2C_Driver PollSensor()"); +} + +// Sensor-Specific, AHTX0 +bool I2C_Driver::initAHTX0() { + WS_DEBUG_PRINTLN("I2C_Driver::initAHTX0"); + bool is_success = true; + _ahtx0 = new Adafruit_AHTX0(); + if (!_ahtx0->begin(_i2c)) { + WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); + is_success = false; + }; + WS_DEBUG_PRINTLN("AHT initialized successfully!"); + return is_success; +} + +void I2C_Driver::enableAHTX0Temperature() { + _ahtTemperature = _ahtx0->getTemperatureSensor(); + _ahtTemperature->printSensorDetails(); + WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 70f70f2c2..8b7a22175 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -17,6 +17,8 @@ #include "Wippersnapper.h" +#include + class I2C_Driver { public: // GENERIC, shared @@ -25,8 +27,15 @@ class I2C_Driver { void setPeriod(float period); virtual bool initSensor(); virtual void pollSensor(); + + bool initAHTX0(); + void enableAHTX0Temperature(); // Generic TwoWire *_i2c = NULL; + + Adafruit_AHTX0 *_ahtx0 = NULL; + Adafruit_Sensor *_ahtTemperature = NULL; + private: // Generic float _pollPeriod; diff --git a/src/drivers/I2C_Driver_AHTX0.cpp b/src/drivers/I2C_Driver_AHTX0.cpp deleted file mode 100644 index 7f8f1c4dd..000000000 --- a/src/drivers/I2C_Driver_AHTX0.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/*! - * @file I2C_Driver_AHTX0.cpp - * - * Subclass for an AHT10 & AHT20 Humidity and Temperature Sensor. - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ -#include "I2C_Driver_AHTX0.h" - - -/*************************************************/ -/*! - @brief Initializes an AHTX0 sensor. - @return True if AHTX0 device driver initialized - successfully, False otherwise. -*/ -/*************************************************/ -bool I2C_Driver_AHTX0::initSensor() { - bool is_success = true; - _ahtx0 = new Adafruit_AHTX0(); - if (!_ahtx0->begin(_i2c)) { - WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); - is_success = false; - }; - WS_DEBUG_PRINTLN("AHT initialized successfully!"); - return is_success; -} - -/*************************************************/ -/*! - @brief Polls the AHTX0 for new data. -*/ -/*************************************************/ -void I2C_Driver_AHTX0::pollSensor() { - // TODO - poll them - WS_DEBUG_PRINTLN("I2C_Driver_AHTX0::pollSensor"); -} - -/**************************************************/ -/*! - @brief Enables the AHTX0's temperature sensor. -*/ -/**************************************************/ -void I2C_Driver_AHTX0::enableSensorTemperature() { - _ahtTemperature = _ahtx0->getTemperatureSensor(); - _ahtTemperature->printSensorDetails(); - WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); -} - -/**************************************************/ -/*! - @brief Enables the AHTX0's humidity sensor. -*/ -/**************************************************/ -void I2C_Driver_AHTX0::enableSensorHumidity() { - _ahtHumidity = _ahtx0->getHumiditySensor(); - _ahtHumidity->printSensorDetails(); - WS_DEBUG_PRINTLN("Enabled AHTX0 humidity sensor"); -} \ No newline at end of file diff --git a/src/drivers/I2C_Driver_AHTX0.h b/src/drivers/I2C_Driver_AHTX0.h deleted file mode 100644 index 0ec6d2ea8..000000000 --- a/src/drivers/I2C_Driver_AHTX0.h +++ /dev/null @@ -1,42 +0,0 @@ -/*! - * @file I2C_Driver_AHTX0.h - * - * Subclass for an AHT10 & AHT20 Humidity and Temperature Sensor. - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ -#ifndef I2C_Driver_AHTX0_H -#define I2C_Driver_AHTX0_H - -#include "Wippersnapper.h" -#include "I2C_Driver.h" -#include - -class I2C_Driver_AHTX0 : public I2C_Driver { - public: - bool initSensor() override; - void pollSensor() override; - // AHT-specific - void enableSensorTemperature(); - void enableSensorHumidity(); - - // Generic - TwoWire *_i2c = NULL; - // Specific (can be private maybe?) - Adafruit_AHTX0 *_ahtx0 = NULL; - private: - // Generic - float _pollPeriod; - // AHT-Specific sensor properties - Adafruit_Sensor *_ahtTemperature = NULL; - Adafruit_Sensor *_ahtHumidity = NULL; -}; - -#endif // I2C_Driver_AHTX0_H \ No newline at end of file From 08b4f196126a9b7be6254952ef66caee9c9537d8 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 13:45:57 -0400 Subject: [PATCH 56/95] Working: init ahtx0 sensor from component, generic driver --- .../WipperSnapper_Component_I2C.cpp | 19 ++++++---- src/drivers/I2C_Driver.cpp | 38 +++++++++---------- src/drivers/I2C_Driver.h | 34 ++++++++--------- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 9057a224f..542fe1b20 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -109,30 +109,33 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice bool attachSuccess = false; // Determine which sensor-specific callback to utilize if (msgDeviceInitReq->has_aht_init) { - WS_DEBUG_PRINTLN("Initializing AHTx sensor!"); uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; + WS_DEBUG_PRINTLN("Requesting to initialize AHTx sensor"); + WS_DEBUG_PRINT("\tSensor Addr: ");WS_DEBUG_PRINTLN(addr); + WS_DEBUG_PRINT("\tTemperature sensor enabled? ");WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_temperature); + WS_DEBUG_PRINT("\tHumidity sensor enabled? ");WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_humidity); + // TODO: Create I2C Driver using the an AHT driver sub-class! - I2C_Driver * p1 = new I2C_Driver(addr, this->_i2c); + I2C_Driver * aht = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver - if (!p1->initAHTX0()) { + if (!aht->initAHTX0()) { attachSuccess = false; return attachSuccess; } // Initialize device-specific sensors if (msgDeviceInitReq->aht_init.enable_temperature == true) { - WS_DEBUG_PRINT("Enabled: AHTX0 temperature sensor"); - void enableAHTX0Temperature(); + aht->enableAHTX0Temperature(); } if (msgDeviceInitReq->aht_init.enable_humidity == true) { - WS_DEBUG_PRINT("Enabled: AHTX0 humidity sensor"); - // TODO, AHTX object.enable here! + aht->enableAHTX0Humidity(); } // NOTE: This is generic // Push to vector containing sensor drivers - activeDrivers.push_back(p1); + activeDrivers.push_back(aht); attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") } + WS_DEBUG_PRINTLN("Successfully initialized AHTX0 sensor!"); return attachSuccess; } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 3ec2fa086..c6108c590 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -24,10 +24,8 @@ */ /*******************************************************************************/ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { - WS_DEBUG_PRINTLN("* New I2CDriver!"); - WS_DEBUG_PRINT("* I2CDriver Device Addr: ");WS_DEBUG_PRINTLN(deviceAddress); - _i2c = i2c; - _pollPeriod = 0.0; + _i2c = i2c; + _pollPeriod = 0.0; } /*************************************/ @@ -36,8 +34,8 @@ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { */ /*************************************/ I2C_Driver::~I2C_Driver() { - _i2c = NULL; - _pollPeriod = 0.0; + _i2c = NULL; + _pollPeriod = 0.0; } /*******************************************/ @@ -46,9 +44,7 @@ I2C_Driver::~I2C_Driver() { polling period, in seconds. */ /*******************************************/ -void I2C_Driver::setPeriod(float period) { - _pollPeriod = period; -} +void I2C_Driver::setPeriod(float period) { _pollPeriod = period; } /*************************************************/ /*! @@ -59,8 +55,8 @@ void I2C_Driver::setPeriod(float period) { */ /*************************************************/ bool I2C_Driver::initSensor() { - WS_DEBUG("I2C_Driver initSensor()"); - return true; + WS_DEBUG("I2C_Driver initSensor()"); + return true; } /*************************************************/ @@ -72,18 +68,18 @@ bool I2C_Driver::initSensor() { successfully, False otherwise. */ /*************************************************/ -void I2C_Driver::pollSensor() { - WS_DEBUG("I2C_Driver PollSensor()"); -} +void I2C_Driver::pollSensor() { WS_DEBUG("I2C_Driver PollSensor()"); } + +/* Sensor-Specific Funcs */ -// Sensor-Specific, AHTX0 +// AHTX0 Sensor // bool I2C_Driver::initAHTX0() { WS_DEBUG_PRINTLN("I2C_Driver::initAHTX0"); - bool is_success = true; + bool is_success = true; _ahtx0 = new Adafruit_AHTX0(); if (!_ahtx0->begin(_i2c)) { - WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); - is_success = false; + WS_DEBUG_PRINTLN("Error: AHTx0 not initialized"); + is_success = false; }; WS_DEBUG_PRINTLN("AHT initialized successfully!"); return is_success; @@ -92,5 +88,9 @@ bool I2C_Driver::initAHTX0() { void I2C_Driver::enableAHTX0Temperature() { _ahtTemperature = _ahtx0->getTemperatureSensor(); _ahtTemperature->printSensorDetails(); - WS_DEBUG_PRINTLN("Enabled AHTX0 temperature sensor"); +} + +void I2C_Driver::enableAHTX0Humidity() { + _ahtHumidity = _ahtx0->getHumiditySensor(); + _ahtHumidity->printSensorDetails(); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 8b7a22175..d8435254d 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -20,25 +20,25 @@ #include class I2C_Driver { - public: - // GENERIC, shared - I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); - ~I2C_Driver(); - void setPeriod(float period); - virtual bool initSensor(); - virtual void pollSensor(); +public: + // GENERIC, shared + I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); + ~I2C_Driver(); + void setPeriod(float period); + virtual bool initSensor(); + virtual void pollSensor(); - bool initAHTX0(); - void enableAHTX0Temperature(); - // Generic - TwoWire *_i2c = NULL; + // AHTX0-specific + bool initAHTX0(); + void enableAHTX0Temperature(); + void enableAHTX0Humidity(); + Adafruit_AHTX0 *_ahtx0 = NULL; + Adafruit_Sensor *_ahtTemperature = NULL; + Adafruit_Sensor *_ahtHumidity = NULL; - Adafruit_AHTX0 *_ahtx0 = NULL; - Adafruit_Sensor *_ahtTemperature = NULL; - - private: - // Generic - float _pollPeriod; +private: + float _pollPeriod; + TwoWire *_i2c = NULL; }; #endif // I2C_Driver_H \ No newline at end of file From ba386d199cb0a503b923962cd0a7c1c5317479a0 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 15:28:30 -0400 Subject: [PATCH 57/95] validate port # from message --- src/Wippersnapper.cpp | 16 ++++--------- .../WipperSnapper_Component_I2C.cpp | 7 +++--- src/drivers/I2C_Driver.cpp | 24 +++++++++++++++++-- src/drivers/I2C_Driver.h | 10 +++++--- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 5d597ad0c..1bb70392a 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -454,13 +454,13 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, is_success = false; return is_success; } - // Create an I2C object using the message as a constructor? - if (WS._i2cPort0 == NULL) { + // Create an I2C object using the message as a constructor + if (WS.msgI2cInitRequest.i2c_port_number == 0) { WS._i2cPort0 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); WS.i2cComponents.push_back(WS._i2cPort0); // did we init. the port successfully? is_success = WS._i2cPort0->_isInit; - } else if (WS._i2cPort1 == NULL) { + } else if (WS.msgI2cInitRequest.i2c_port_number == 1) { WS._i2cPort1 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); // did we init. the port successfully? is_success = WS._i2cPort1->_isInit; @@ -548,15 +548,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, // Attach device to I2C port // TODO: allow this to be implemented with multiple ports WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); - - // TODO - // Configure sensors - // WS._i2cPort0->enableSensor(temperature); - // WS._i2cPort0->enableSensor(humidity); - // Configure send interval - // WS._i2cPort0->sendInterval(period); - // Publish back response - // TODO + // TODO: Response back to broker! } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); is_success = false; diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 542fe1b20..c591acede 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -108,6 +108,8 @@ uint16_t WipperSnapper_Component_I2C::scanAddresses( bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) { bool attachSuccess = false; // Determine which sensor-specific callback to utilize + + // AHTX0 Sensor if (msgDeviceInitReq->has_aht_init) { uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; WS_DEBUG_PRINTLN("Requesting to initialize AHTx sensor"); @@ -118,7 +120,7 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice // TODO: Create I2C Driver using the an AHT driver sub-class! I2C_Driver * aht = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver - if (!aht->initAHTX0()) { + if (aht->initAHTX0()) { attachSuccess = false; return attachSuccess; } @@ -129,8 +131,7 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (msgDeviceInitReq->aht_init.enable_humidity == true) { aht->enableAHTX0Humidity(); } - // NOTE: This is generic - // Push to vector containing sensor drivers + // Push to vector for sensor drivers activeDrivers.push_back(aht); attachSuccess = true; } else { diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index c6108c590..40efaa071 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -25,6 +25,7 @@ /*******************************************************************************/ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { _i2c = i2c; + _deviceAddr = deviceAddress; _pollPeriod = 0.0; } @@ -35,6 +36,7 @@ I2C_Driver::I2C_Driver(uint16_t deviceAddress, TwoWire *i2c) { /*************************************/ I2C_Driver::~I2C_Driver() { _i2c = NULL; + _deviceAddr = 0; _pollPeriod = 0.0; } @@ -73,6 +75,14 @@ void I2C_Driver::pollSensor() { WS_DEBUG("I2C_Driver PollSensor()"); } /* Sensor-Specific Funcs */ // AHTX0 Sensor // +/*************************************************/ +/*! + @brief Initializes an AHTX0 temperature + + humidity sensor. + @return True if AHTX0 sensor initialized + successfully, False otherwise. +*/ +/*************************************************/ bool I2C_Driver::initAHTX0() { WS_DEBUG_PRINTLN("I2C_Driver::initAHTX0"); bool is_success = true; @@ -85,12 +95,22 @@ bool I2C_Driver::initAHTX0() { return is_success; } +/***************************************************/ +/*! + @brief Enables the AHTX0's temperature sensor. +*/ +/***************************************************/ void I2C_Driver::enableAHTX0Temperature() { _ahtTemperature = _ahtx0->getTemperatureSensor(); - _ahtTemperature->printSensorDetails(); + //_ahtTemperature->printSensorDetails(); } +/***************************************************/ +/*! + @brief Enables the AHTX0's humidity sensor. +*/ +/***************************************************/ void I2C_Driver::enableAHTX0Humidity() { _ahtHumidity = _ahtx0->getHumiditySensor(); - _ahtHumidity->printSensorDetails(); + //_ahtHumidity->printSensorDetails(); } \ No newline at end of file diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index d8435254d..419cca2e3 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -25,6 +25,7 @@ class I2C_Driver { I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); void setPeriod(float period); + // TODO virtual bool initSensor(); virtual void pollSensor(); @@ -32,13 +33,16 @@ class I2C_Driver { bool initAHTX0(); void enableAHTX0Temperature(); void enableAHTX0Humidity(); - Adafruit_AHTX0 *_ahtx0 = NULL; - Adafruit_Sensor *_ahtTemperature = NULL; - Adafruit_Sensor *_ahtHumidity = NULL; private: + // General device driver properties + int16_t _deviceAddr; float _pollPeriod; TwoWire *_i2c = NULL; + // ATHX0 + Adafruit_AHTX0 *_ahtx0 = NULL; + Adafruit_Sensor *_ahtTemperature = NULL; + Adafruit_Sensor *_ahtHumidity = NULL; }; #endif // I2C_Driver_H \ No newline at end of file From 1e3e506e1c09a9e373ce6ca35fe1f432b83109c7 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 15:38:52 -0400 Subject: [PATCH 58/95] scan on a desired port # --- src/Wippersnapper.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 1bb70392a..dc029fd43 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -508,7 +508,15 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS_DEBUG_PRINTLN(msgScanReq.i2c_port_number); // Scan all requested addresses on i2cportX and ret. address found, -1 // otherwise. - uint16_t addressFound = WS._i2cPort0->scanAddresses(msgScanReq); + uint16_t addressFound; + if (msgScanReq.i2c_port_number == 0) { + addressFound = WS._i2cPort0->scanAddresses(msgScanReq); + } else if (msgScanReq.i2c_port_number == 1) { + addressFound = WS._i2cPort1->scanAddresses(msgScanReq); + } else { + WS_DEBUG_PRINTLN("ERROR:: I2C Scan: Invalid port number"); + return false; + } // Create response msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = @@ -546,9 +554,11 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, return is_success; } // Attach device to I2C port - // TODO: allow this to be implemented with multiple ports + // TODO: We should check port # here, not currently implemented in protobuf, + // PR in WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); // TODO: Response back to broker! + // NOT currently implemented in protobuf, waiting for generic response } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); is_success = false; From ecb63be5ad7467f4cf673f6e192176763be00488 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 15:46:19 -0400 Subject: [PATCH 59/95] clean up prints --- .../WipperSnapper_Component_I2C.cpp | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index c591acede..026d0898a 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -24,9 +24,18 @@ */ /***************************************************************************************************************/ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( - wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { - WS_DEBUG_PRINTLN("NEW WipperSnapper_Component_I2C"); - // initialize using desired portNum + wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { + WS_DEBUG_PRINTLN("EXEC: New I2C Port "); + WS_DEBUG_PRINT("\tPort #: "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_port_number); + WS_DEBUG_PRINT("\tSDA Pin: "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_sda); + WS_DEBUG_PRINT("\tSCL Pin: "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_scl); + WS_DEBUG_PRINT("\tFrequency (Hz): "); + WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); + + // initialize TwoWire w/ desired portNum _i2c = new TwoWire(msgInitRequest->i2c_port_number); // validate if SDA & SCL has pullup if (digitalRead(msgInitRequest->i2c_pin_sda) == LOW) { @@ -38,15 +47,6 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( // set up i2c port _i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl); _i2c->setClock(msgInitRequest->i2c_frequency); - WS_DEBUG_PRINTLN("Init. new I2C Port: "); - WS_DEBUG_PRINT("Port#: "); - WS_DEBUG_PRINTLN(msgInitRequest->i2c_port_number); - WS_DEBUG_PRINT("SDA: "); - WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_sda); - WS_DEBUG_PRINT("SCL: "); - WS_DEBUG_PRINTLN(msgInitRequest->i2c_pin_scl); - WS_DEBUG_PRINT("Frequency (Hz): "); - WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); // set i2c obj. properties _portNum = msgInitRequest->i2c_port_number; _isInit = true; @@ -63,25 +63,23 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { _isInit = false; } -/*****************************************************************************************************/ +/************************************************************************/ /*! @brief Destructor for a WipperSnapper I2C component. @param msgScanReq A decoded I2C scan request message. @returns The address which an I2C device is located, -1 otherwise. */ -/*****************************************************************************************************/ +/************************************************************************/ uint16_t WipperSnapper_Component_I2C::scanAddresses( wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { + WS_DEBUG_PRINT("EXEC: I2C Scan on port "); WS_DEBUG_PRINTLN(_portNum); // decode stream into i2c request uint16_t addrFound = -1; uint16_t scanAddr; - WS_DEBUG_PRINT("EXEC: I2C Scan, Port ("); - WS_DEBUG_PRINT(_portNum); - WS_DEBUG_PRINTLN(")"); for (int i = 0; i < msgScanReq.address_count; i++) { scanAddr = msgScanReq.address[i]; - WS_DEBUG_PRINT("Scanning address "); + WS_DEBUG_PRINT("* Scanning address "); WS_DEBUG_PRINTLN(scanAddr); _i2c->beginTransmission(scanAddr); if (_i2c->endTransmission() == 0) { @@ -113,14 +111,14 @@ bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDevice if (msgDeviceInitReq->has_aht_init) { uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; WS_DEBUG_PRINTLN("Requesting to initialize AHTx sensor"); - WS_DEBUG_PRINT("\tSensor Addr: ");WS_DEBUG_PRINTLN(addr); + WS_DEBUG_PRINT("\tSensor Addr: ");WS_DEBUG_PRINTLN(addr, HEX); WS_DEBUG_PRINT("\tTemperature sensor enabled? ");WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_temperature); WS_DEBUG_PRINT("\tHumidity sensor enabled? ");WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_humidity); // TODO: Create I2C Driver using the an AHT driver sub-class! I2C_Driver * aht = new I2C_Driver(addr, this->_i2c); // Attempt to initialize the sensor driver - if (aht->initAHTX0()) { + if (!aht->initAHTX0()) { attachSuccess = false; return attachSuccess; } From 023d9478987b92c635a91c0fb4edebcb8f76868d Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 16:22:11 -0400 Subject: [PATCH 60/95] refactor object properties --- src/Wippersnapper.h | 5 ---- src/components/WipperSnapper_Component_I2C.h | 25 ++++++++++---------- src/drivers/I2C_Driver.cpp | 24 ------------------- src/drivers/I2C_Driver.h | 3 --- 4 files changed, 12 insertions(+), 45 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 77a9bfed4..4f988b43b 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -50,11 +50,6 @@ #include "Adafruit_MQTT.h" // MQTT Client #include "Arduino.h" // Wiring -#if defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || ADAFRUIT_PYPORTAL -#include -#else // ESP32/ESP32-S2 -#include -#endif // Note: These might be better off in their respective wrappers #include diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 382f669ee..3426e7e64 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -26,19 +26,18 @@ class Wippersnapper; class I2C_Driver; class WipperSnapper_Component_I2C { -public: - WipperSnapper_Component_I2C( - wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); - ~WipperSnapper_Component_I2C(); - uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); - bool - attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); - - int32_t _portNum; - bool _isInit; - // I2C_Driver - std::vector activeDrivers; - TwoWire *_i2c = NULL; + public: + WipperSnapper_Component_I2C( + wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); + ~WipperSnapper_Component_I2C(); + uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + bool + attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); + bool _isInit; + private: + int32_t _portNum; + TwoWire *_i2c = NULL; + std::vector activeDrivers; }; extern Wippersnapper WS; diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 40efaa071..7da121226 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -48,30 +48,6 @@ I2C_Driver::~I2C_Driver() { /*******************************************/ void I2C_Driver::setPeriod(float period) { _pollPeriod = period; } -/*************************************************/ -/*! - @brief Initializes an I2C driver. Must - be implemented in an I2C_Driver_x class. - @return True if I2C device driver initialized - successfully, False otherwise. -*/ -/*************************************************/ -bool I2C_Driver::initSensor() { - WS_DEBUG("I2C_Driver initSensor()"); - return true; -} - -/*************************************************/ -/*! - @brief Polls an I2C driver for new sensor - messages. Must be implemented in an - I2C_Driver_x class. - @return True if I2C device driver initialized - successfully, False otherwise. -*/ -/*************************************************/ -void I2C_Driver::pollSensor() { WS_DEBUG("I2C_Driver PollSensor()"); } - /* Sensor-Specific Funcs */ // AHTX0 Sensor // diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index 419cca2e3..fc0064f59 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -25,9 +25,6 @@ class I2C_Driver { I2C_Driver(uint16_t deviceAddress, TwoWire *i2c); ~I2C_Driver(); void setPeriod(float period); - // TODO - virtual bool initSensor(); - virtual void pollSensor(); // AHTX0-specific bool initAHTX0(); From 49fc1101944b32f89881681bb8d38778681914de Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 16:25:42 -0400 Subject: [PATCH 61/95] init device on port# --- src/Wippersnapper.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index dc029fd43..85f4e95f7 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -405,23 +405,6 @@ void cbSignalTopic(char *data, uint16_t len) { } } -/************************************************************************************/ -/*! - @brief Publishes an I2C signal response message to the broker's - I2C signal MQTT topic. - @param msgi2cResponse - I2CResponse signal message to publish -*/ -/************************************************************************************/ -void publishSignalI2CResponse( - wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { - size_t msgSz; - pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse); - // Publish i2c response message to broker - WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); -} - /******************************************************************************************/ /*! @brief Decodes an I2C signal request message and executes the @@ -554,9 +537,11 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, return is_success; } // Attach device to I2C port - // TODO: We should check port # here, not currently implemented in protobuf, - // PR in - WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); + if (msgI2CDeviceInitRequest.i2c_port_number == 0) { + WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); + } else if (msgI2CDeviceInitRequest.i2c_port_number == 1) { + WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); + } // TODO: Response back to broker! // NOT currently implemented in protobuf, waiting for generic response } else { From 4421dfc5dea7435ff7b1eb1334264aad08aae5f5 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 30 Jul 2021 16:52:46 -0400 Subject: [PATCH 62/95] use the correct payload, attach correctly, e2e working --- src/Wippersnapper.cpp | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 85f4e95f7..fdfc49956 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -537,13 +537,35 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, return is_success; } // Attach device to I2C port + bool deviceInitSuccess = false; if (msgI2CDeviceInitRequest.i2c_port_number == 0) { - WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); + deviceInitSuccess = WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); } else if (msgI2CDeviceInitRequest.i2c_port_number == 1) { - WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); + deviceInitSuccess =WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); } - // TODO: Response back to broker! - // NOT currently implemented in protobuf, waiting for generic response + // Create response + msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; + msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_device_init_tag; + msgi2cResponse.payload.resp_i2c_device_init.is_success = deviceInitSuccess; + // Encode message + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); + is_success = false; + } + size_t msgSz; + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, + &msgi2cResponse); + // Publish + WS_DEBUG_PRINT("Publishing i2c response..."); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, + 1); + WS_DEBUG_PRINTLN("Published!"); + + } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); is_success = false; From 52ea1473d565db35d9cf2a273c20dadc3beb49c6 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 12:32:04 -0400 Subject: [PATCH 63/95] fail out of signal decode on unrecoverable failures --- src/Wippersnapper.cpp | 51 +++++++++----------- src/Wippersnapper.h | 8 +-- src/components/WipperSnapper_Component_I2C.h | 25 +++++----- 3 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index fdfc49956..6f2cf21b0 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -69,7 +69,8 @@ Wippersnapper::~Wippersnapper() { /**************************************************************************/ /*! - @brief Performs the filesystem-backed provisioning workflow + @brief Executes the provisioning workflow. + NOTE: Must be called prior to connect(). */ /**************************************************************************/ void Wippersnapper::provision() { @@ -130,9 +131,9 @@ void Wippersnapper::set_user_key() { /*! @brief Prints a message buffer to the serial output @param Buffer - Desired message to print. - @pram bufLen - Length of buffer to print. + Desired message to print to the serial output. + @param bufLen + Length of message buffer. */ /**************************************************************************/ void printMsgBuffer(char *buffer, uint16_t bufLen) { @@ -388,7 +389,7 @@ bool Wippersnapper::decodeSignalMsg( */ /**************************************************************************/ void cbSignalTopic(char *data, uint16_t len) { - WS_DEBUG_PRINTLN("* New Msg on Signal"); + WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal]: "); printMsgBuffer(data, len); // zero-out current buffer memset(WS._buffer, 0, sizeof(WS._buffer)); @@ -434,8 +435,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, &WS.msgI2cInitRequest)) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); - is_success = false; - return is_success; + return false; // fail out } // Create an I2C object using the message as a constructor if (WS.msgI2cInitRequest.i2c_port_number == 0) { @@ -450,7 +450,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS.i2cComponents.push_back(WS._i2cPort1); } else { WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); - is_success = false; + return false; // fail out } // Create i2c init response msgi2cResponse.which_payload = @@ -463,7 +463,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - is_success = false; + return false; // fail out } size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, @@ -482,13 +482,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, &msgScanReq)) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); - is_success = false; - return false; + return false; // fail out } - // TODO: Check which port and select the correct object out of the i2c - // component vector - WS_DEBUG_PRINT("Port"); - WS_DEBUG_PRINTLN(msgScanReq.i2c_port_number); // Scan all requested addresses on i2cportX and ret. address found, -1 // otherwise. uint16_t addressFound; @@ -498,7 +493,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, addressFound = WS._i2cPort1->scanAddresses(msgScanReq); } else { WS_DEBUG_PRINTLN("ERROR:: I2C Scan: Invalid port number"); - return false; + return false; // fail out } // Create response msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; @@ -512,7 +507,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - is_success = false; + return false; // fail out } size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, @@ -533,19 +528,21 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_decode(stream, wippersnapper_i2c_v1_I2CDeviceInitRequest_fields, &msgI2CDeviceInitRequest)) { WS_DEBUG_PRINTLN("ERROR: Could not decode I2CDeviceInitRequest message."); - is_success = false; - return is_success; + return false; // fail out } // Attach device to I2C port bool deviceInitSuccess = false; if (msgI2CDeviceInitRequest.i2c_port_number == 0) { - deviceInitSuccess = WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); + deviceInitSuccess = + WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); } else if (msgI2CDeviceInitRequest.i2c_port_number == 1) { - deviceInitSuccess =WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); + deviceInitSuccess = + WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); } // Create response msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; - msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_device_init_tag; + msgi2cResponse.which_payload = + wippersnapper_signal_v1_I2CResponse_resp_i2c_device_init_tag; msgi2cResponse.payload.resp_i2c_device_init.is_success = deviceInitSuccess; // Encode message memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); @@ -554,7 +551,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - is_success = false; + return false; // fail out } size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, @@ -564,14 +561,10 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); WS_DEBUG_PRINTLN("Published!"); - - } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); - is_success = false; - return is_success; + return false; // fail out } - return is_success; } @@ -586,7 +579,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, */ /**************************************************************************/ void cbSignalI2CReq(char *data, uint16_t len) { - WS_DEBUG_PRINTLN("* New Msg on Signal-I2C: "); + WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal-I2C]: "); printMsgBuffer(data, len); // zero-out current buffer memset(WS._buffer, 0, sizeof(WS._buffer)); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 4f988b43b..eb797e392 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -42,15 +42,11 @@ // WipperSnapper I2C Component #include "components/WipperSnapper_Component_I2C.h" -// TODO - break this out -// I2C Sensor Drivers -//#include "drivers/I2C_Driver_AHTX0.h" // External libraries #include "Adafruit_MQTT.h" // MQTT Client #include "Arduino.h" // Wiring - // Note: These might be better off in their respective wrappers #include #include @@ -58,7 +54,7 @@ #include // Uncomment for staging builds -#define USE_STAGING +// #define USE_STAGING #ifdef USE_STAGING #define IO_MQTT_SERVER "io.adafruit.us" ///< Staging (development) mqtt server @@ -74,7 +70,7 @@ #include "provisioning/Wippersnapper_ESP32_nvs.h" #endif -#define WS_VERSION "1.0.0-beta.3" ///< Library version (semver-formatted) +#define WS_VERSION "1.0.0-beta.4" ///< Library version (semver-formatted) // Reserved Adafruit IO MQTT topics #define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 3426e7e64..f556dcdda 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -26,18 +26,19 @@ class Wippersnapper; class I2C_Driver; class WipperSnapper_Component_I2C { - public: - WipperSnapper_Component_I2C( - wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); - ~WipperSnapper_Component_I2C(); - uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); - bool - attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); - bool _isInit; - private: - int32_t _portNum; - TwoWire *_i2c = NULL; - std::vector activeDrivers; +public: + WipperSnapper_Component_I2C( + wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); + ~WipperSnapper_Component_I2C(); + uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + bool + attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); + bool _isInit; + +private: + int32_t _portNum; + TwoWire *_i2c = NULL; + std::vector activeDrivers; }; extern Wippersnapper WS; From ee74011fd1804a110c3636f925050802c82eca35 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 12:37:13 -0400 Subject: [PATCH 64/95] check if port is initialized prior to doing any operations with it --- src/Wippersnapper.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 6f2cf21b0..762ba4f44 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -487,12 +487,12 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, // Scan all requested addresses on i2cportX and ret. address found, -1 // otherwise. uint16_t addressFound; - if (msgScanReq.i2c_port_number == 0) { + if (msgScanReq.i2c_port_number == 0 && WS._i2cPort0->_isInit == true) { addressFound = WS._i2cPort0->scanAddresses(msgScanReq); - } else if (msgScanReq.i2c_port_number == 1) { + } else if (msgScanReq.i2c_port_number == 1 && WS._i2cPort1->_isInit == true) { addressFound = WS._i2cPort1->scanAddresses(msgScanReq); } else { - WS_DEBUG_PRINTLN("ERROR:: I2C Scan: Invalid port number"); + WS_DEBUG_PRINTLN("ERROR: Could not execute I2C scan, the I2C port was not initialized"); return false; // fail out } // Create response @@ -532,10 +532,10 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } // Attach device to I2C port bool deviceInitSuccess = false; - if (msgI2CDeviceInitRequest.i2c_port_number == 0) { + if (msgI2CDeviceInitRequest.i2c_port_number == 0 && WS._i2cPort0->_isInit == true) { deviceInitSuccess = WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); - } else if (msgI2CDeviceInitRequest.i2c_port_number == 1) { + } else if (msgI2CDeviceInitRequest.i2c_port_number == 1 && WS._i2cPort1->_isInit == true) { deviceInitSuccess = WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); } From 15ec7b7e37854a8faccbe53bb08a54def89f0a41 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:02:20 -0400 Subject: [PATCH 65/95] encapsulate i2c publish response --- src/Wippersnapper.cpp | 60 ++++++++++++++++++------------------------- src/Wippersnapper.h | 2 +- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 762ba4f44..108e1bae9 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -406,6 +406,16 @@ void cbSignalTopic(char *data, uint16_t len) { } } +void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { + // TODO: Zero-out outgoing buffer + size_t msgSz; + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, + msgi2cResponse); + WS_DEBUG_PRINT("Publishing Message: I2CResponse..."); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); + WS_DEBUG_PRINTLN("Published!"); +} + /******************************************************************************************/ /*! @brief Decodes an I2C signal request message and executes the @@ -450,12 +460,12 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS.i2cComponents.push_back(WS._i2cPort1); } else { WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); - return false; // fail out + is_success = false; } // Create i2c init response msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag; - msgi2cResponse.payload.resp_i2c_init.is_initialized = true; + msgi2cResponse.payload.resp_i2c_init.is_initialized = is_success; // Encode message memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, @@ -463,15 +473,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - return false; // fail out + return false; // fail out if we can't encode the response } - size_t msgSz; - pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse); - WS_DEBUG_PRINT("Publishing i2c init response..."); - WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, - 1); - WS_DEBUG_PRINTLN("Published!"); } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { WS_DEBUG_PRINTLN("I2C Scan Request Found!"); @@ -482,7 +485,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, &msgScanReq)) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); - return false; // fail out + return false; // fail out if we can't decode the request } // Scan all requested addresses on i2cportX and ret. address found, -1 // otherwise. @@ -493,7 +496,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, addressFound = WS._i2cPort1->scanAddresses(msgScanReq); } else { WS_DEBUG_PRINTLN("ERROR: Could not execute I2C scan, the I2C port was not initialized"); - return false; // fail out + addressFound = -1; } // Create response msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; @@ -507,28 +510,19 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - return false; // fail out + return false; // fail out if we can't encode a response } - size_t msgSz; - pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse); - // Publish - WS_DEBUG_PRINT("Publishing i2c response..."); - WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, - 1); - WS_DEBUG_PRINTLN("Published!"); } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { - WS_DEBUG_PRINTLN("AHTX Init Request Found!"); + WS_DEBUG_PRINTLN("I2C Device Init Request Found!"); // Decode stream into an I2CDeviceInitRequest - // Create I2C request message wippersnapper_i2c_v1_I2CDeviceInitRequest msgI2CDeviceInitRequest = wippersnapper_i2c_v1_I2CDeviceInitRequest_init_zero; // Decode stream into struct, msgI2CDeviceInitRequest if (!pb_decode(stream, wippersnapper_i2c_v1_I2CDeviceInitRequest_fields, &msgI2CDeviceInitRequest)) { WS_DEBUG_PRINTLN("ERROR: Could not decode I2CDeviceInitRequest message."); - return false; // fail out + return false; // fail out if we can't decode } // Attach device to I2C port bool deviceInitSuccess = false; @@ -551,20 +545,14 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - return false; // fail out + return false; // fail out if we cant encode } - size_t msgSz; - pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse); - // Publish - WS_DEBUG_PRINT("Publishing i2c response..."); - WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, - 1); - WS_DEBUG_PRINTLN("Published!"); + WS_DEBUG("TEST??"); } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); - return false; // fail out + return false; // fail out, we didn't encode anything to publish } + publishI2CResponse(&msgi2cResponse); return is_success; } @@ -1220,7 +1208,7 @@ ws_status_t Wippersnapper::run() { checkMQTTConnection(curTime); // Poll for packets from broker queue, return immediately - WS._mqtt->processPacketsUntilCallback(100); + WS._mqtt->processPacketsUntilCallback(10); // TODO: Loop thru components // Process digital inputs, digitalGPIO module @@ -1301,7 +1289,9 @@ ws_status_t Wippersnapper::mqttStatus() { if (WS._mqtt->connected()) { // ping within keepalive to keep connection open if (millis() > (_prv_ping + WS_KEEPALIVE_INTERVAL_MS)) { + WS_DEBUG_PRINT("PINGREQ..."); ping(); + WS_DEBUG_PRINT("PINGRESP!"); _prv_ping = millis(); } // blink status LED every STATUS_LED_KAT_BLINK_TIME millis diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index eb797e392..7877c7d5a 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -54,7 +54,7 @@ #include // Uncomment for staging builds -// #define USE_STAGING +#define USE_STAGING #ifdef USE_STAGING #define IO_MQTT_SERVER "io.adafruit.us" ///< Staging (development) mqtt server From 296427f2c6b6978ba7fd0143c1f2c64fd645fa95 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:10:15 -0400 Subject: [PATCH 66/95] doxy --- src/Wippersnapper.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 108e1bae9..ef366544d 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -406,8 +406,14 @@ void cbSignalTopic(char *data, uint16_t len) { } } +/******************************************************************************************/ +/*! + @brief Publishes an I2C response signal message to the broker. + @param msgi2cResponse + A pointer to an I2C response message typedef. +*/ +/******************************************************************************************/ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { - // TODO: Zero-out outgoing buffer size_t msgSz; pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, msgi2cResponse); From 2d4da5109fecee830a4f86457ba761d82a75d0a3 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:27:57 -0400 Subject: [PATCH 67/95] remove non-shared variable from hdr --- src/Wippersnapper.cpp | 14 +++++++------- src/Wippersnapper.h | 3 --- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index ef366544d..d13ed9c41 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -444,23 +444,24 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, wippersnapper_signal_v1_I2CResponse_init_zero; if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); + wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; // Create I2C request message - WS.msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; + msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; // Decode i2c request message into struct if (!pb_decode(stream, wippersnapper_i2c_v1_I2CInitRequest_fields, - &WS.msgI2cInitRequest)) { + &msgI2cInitRequest)) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); return false; // fail out } // Create an I2C object using the message as a constructor - if (WS.msgI2cInitRequest.i2c_port_number == 0) { - WS._i2cPort0 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); + if (msgI2cInitRequest.i2c_port_number == 0) { + WS._i2cPort0 = new WipperSnapper_Component_I2C(&msgI2cInitRequest); WS.i2cComponents.push_back(WS._i2cPort0); // did we init. the port successfully? is_success = WS._i2cPort0->_isInit; - } else if (WS.msgI2cInitRequest.i2c_port_number == 1) { - WS._i2cPort1 = new WipperSnapper_Component_I2C(&WS.msgI2cInitRequest); + } else if (msgI2cInitRequest.i2c_port_number == 1) { + WS._i2cPort1 = new WipperSnapper_Component_I2C(&msgI2cInitRequest); // did we init. the port successfully? is_success = WS._i2cPort1->_isInit; WS.i2cComponents.push_back(WS._i2cPort1); @@ -553,7 +554,6 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); return false; // fail out if we cant encode } - WS_DEBUG("TEST??"); } else { WS_DEBUG_PRINTLN("ERROR: Undefined I2C message tag"); return false; // fail out, we didn't encode anything to publish diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 7877c7d5a..515faa5d7 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -285,9 +285,6 @@ class Wippersnapper { wippersnapper_signal_v1_I2CRequest msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; ///< I2C request wrapper ///< message - wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = - wippersnapper_i2c_v1_I2CInitRequest_init_zero; ///< I2C initialization - ///< request message char *throttleMessage; /*!< Pointer to throttle message data. */ int throttleTime; /*!< Total amount of time to throttle the device, in From 564133d9b6230de189e085e7f27e0a0ee5f3dd35 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:29:16 -0400 Subject: [PATCH 68/95] add i2c topic define --- src/Wippersnapper.cpp | 6 +++--- src/Wippersnapper.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index d13ed9c41..0b70409d0 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -839,7 +839,7 @@ bool Wippersnapper::buildWSTopics() { WS._topic_signal_i2c_device = (char *)malloc( sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) + strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) + strlen("device") + - strlen("/i2c") + 1); + strlen(TOPIC_I2C) + 1); // Create global registration topic if (WS._topic_description) { @@ -896,7 +896,7 @@ bool Wippersnapper::buildWSTopics() { strcat(WS._topic_signal_i2c_brkr, _device_uid); strcat(WS._topic_signal_i2c_brkr, TOPIC_SIGNALS); strcat(WS._topic_signal_i2c_brkr, "broker"); - strcat(WS._topic_signal_i2c_brkr, "/i2c"); + strcat(WS._topic_signal_i2c_brkr, TOPIC_I2C); } else { // malloc failed WS._topic_signal_i2c_brkr = 0; is_success = false; @@ -909,7 +909,7 @@ bool Wippersnapper::buildWSTopics() { strcat(WS._topic_signal_i2c_device, _device_uid); strcat(WS._topic_signal_i2c_device, TOPIC_SIGNALS); strcat(WS._topic_signal_i2c_device, "device"); - strcat(WS._topic_signal_i2c_device, "/i2c"); + strcat(WS._topic_signal_i2c_device, TOPIC_I2C); } else { // malloc failed WS._topic_signal_i2c_device = 0; is_success = false; diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 515faa5d7..14d296add 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -80,7 +80,7 @@ #define TOPIC_WS "/wprsnpr/" ///< WipperSnapper topic #define TOPIC_DESCRIPTION "/info/" ///< Registration sub-topic #define TOPIC_SIGNALS "/signals/" ///< Signals sub-topic -#define TOPIC_I2C "/i2c/" ///< I2C sub-topic +#define TOPIC_I2C "/i2c" ///< I2C sub-topic #define WS_DEBUG ///< Define to enable debugging to serial terminal #define WS_PRINTER Serial ///< Where debug messages will be printed From 17048e4baa97a0b6e5d03370f4ab919d9a079920 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:35:57 -0400 Subject: [PATCH 69/95] add getter for private variable --- src/Wippersnapper.cpp | 34 +++++++++++-------- src/Wippersnapper.h | 4 +-- .../WipperSnapper_Component_I2C.cpp | 9 +++++ src/components/WipperSnapper_Component_I2C.h | 3 +- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 0b70409d0..7cb5d3ae6 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -414,12 +414,12 @@ void cbSignalTopic(char *data, uint16_t len) { */ /******************************************************************************************/ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { - size_t msgSz; - pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, - msgi2cResponse); - WS_DEBUG_PRINT("Publishing Message: I2CResponse..."); - WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); - WS_DEBUG_PRINTLN("Published!"); + size_t msgSz; + pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields, + msgi2cResponse); + WS_DEBUG_PRINT("Publishing Message: I2CResponse..."); + WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing, msgSz, 1); + WS_DEBUG_PRINTLN("Published!"); } /******************************************************************************************/ @@ -444,7 +444,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, wippersnapper_signal_v1_I2CResponse_init_zero; if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); - wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; + wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = + wippersnapper_i2c_v1_I2CInitRequest_init_zero; // Create I2C request message msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; // Decode i2c request message into struct @@ -459,11 +460,11 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS._i2cPort0 = new WipperSnapper_Component_I2C(&msgI2cInitRequest); WS.i2cComponents.push_back(WS._i2cPort0); // did we init. the port successfully? - is_success = WS._i2cPort0->_isInit; + is_success = WS._i2cPort0->isInitialized(); } else if (msgI2cInitRequest.i2c_port_number == 1) { WS._i2cPort1 = new WipperSnapper_Component_I2C(&msgI2cInitRequest); // did we init. the port successfully? - is_success = WS._i2cPort1->_isInit; + is_success = WS._i2cPort1->isInitialized(); WS.i2cComponents.push_back(WS._i2cPort1); } else { WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); @@ -497,12 +498,15 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, // Scan all requested addresses on i2cportX and ret. address found, -1 // otherwise. uint16_t addressFound; - if (msgScanReq.i2c_port_number == 0 && WS._i2cPort0->_isInit == true) { + if (msgScanReq.i2c_port_number == 0 && + WS._i2cPort0->isInitialized() == true) { addressFound = WS._i2cPort0->scanAddresses(msgScanReq); - } else if (msgScanReq.i2c_port_number == 1 && WS._i2cPort1->_isInit == true) { + } else if (msgScanReq.i2c_port_number == 1 && + WS._i2cPort1->isInitialized() == true) { addressFound = WS._i2cPort1->scanAddresses(msgScanReq); } else { - WS_DEBUG_PRINTLN("ERROR: Could not execute I2C scan, the I2C port was not initialized"); + WS_DEBUG_PRINTLN("ERROR: Could not execute I2C scan, the I2C port was " + "not initialized"); addressFound = -1; } // Create response @@ -533,10 +537,12 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } // Attach device to I2C port bool deviceInitSuccess = false; - if (msgI2CDeviceInitRequest.i2c_port_number == 0 && WS._i2cPort0->_isInit == true) { + if (msgI2CDeviceInitRequest.i2c_port_number == 0 && + WS._i2cPort0->isInitialized() == true) { deviceInitSuccess = WS._i2cPort0->attachI2CDevice(&msgI2CDeviceInitRequest); - } else if (msgI2CDeviceInitRequest.i2c_port_number == 1 && WS._i2cPort1->_isInit == true) { + } else if (msgI2CDeviceInitRequest.i2c_port_number == 1 && + WS._i2cPort1->isInitialized() == true) { deviceInitSuccess = WS._i2cPort1->attachI2CDevice(&msgI2CDeviceInitRequest); } diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 14d296add..893ae6439 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -54,7 +54,7 @@ #include // Uncomment for staging builds -#define USE_STAGING +// #define USE_STAGING #ifdef USE_STAGING #define IO_MQTT_SERVER "io.adafruit.us" ///< Staging (development) mqtt server @@ -80,7 +80,7 @@ #define TOPIC_WS "/wprsnpr/" ///< WipperSnapper topic #define TOPIC_DESCRIPTION "/info/" ///< Registration sub-topic #define TOPIC_SIGNALS "/signals/" ///< Signals sub-topic -#define TOPIC_I2C "/i2c" ///< I2C sub-topic +#define TOPIC_I2C "/i2c" ///< I2C sub-topic #define WS_DEBUG ///< Define to enable debugging to serial terminal #define WS_PRINTER Serial ///< Where debug messages will be printed diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 026d0898a..b97091b61 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -63,6 +63,15 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { _isInit = false; } +/*****************************************************/ +/*! + @brief Returns if port is already initialized. +*/ +/*****************************************************/ +bool WipperSnapper_Component_I2C::isInitialized() { + return _isInit; +} + /************************************************************************/ /*! @brief Destructor for a WipperSnapper I2C component. diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index f556dcdda..3262acb91 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -33,9 +33,10 @@ class WipperSnapper_Component_I2C { uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); bool attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); - bool _isInit; + bool isInitialized(); private: + bool _isInit; int32_t _portNum; TwoWire *_i2c = NULL; std::vector activeDrivers; From 28d8a2319a02e10c1cc3ac9ed5d4005df09146f0 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:43:55 -0400 Subject: [PATCH 70/95] add Adafruit AHTX0 to library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index d1e1793bc..76b34c681 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino library to access WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_IO_Arduino architectures=* -depends=Adafruit MQTT Library, Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar \ No newline at end of file +depends=Adafruit MQTT Library, Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit AHTX0 \ No newline at end of file From 247811164eb64044d97dfe266c4f23da41429d35 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 13:51:21 -0400 Subject: [PATCH 71/95] process packets fix, not pulled into MQTT yet --- src/Wippersnapper.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 7cb5d3ae6..08f684bd8 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1219,8 +1219,9 @@ ws_status_t Wippersnapper::run() { // Handle MQTT connection checkMQTTConnection(curTime); + WS._mqtt->processPackets(100); // Poll for packets from broker queue, return immediately - WS._mqtt->processPacketsUntilCallback(10); + //WS._mqtt->processPacketsUntilCallback(10); // TODO: Loop thru components // Process digital inputs, digitalGPIO module From eba0a347081d257d412a0865a8067678d2231a51 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 14:50:01 -0400 Subject: [PATCH 72/95] i2c for non esp32? --- src/Wippersnapper.h | 4 ++-- src/components/WipperSnapper_Component_I2C.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 893ae6439..d4c94582b 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -254,8 +254,8 @@ class Wippersnapper { Wippersnapper_FS *_fileSystem; ///< Instance of filesystem class Wippersnapper_ESP32_nvs *_nvs; ///< Instance of nvs - uint8_t _uid[6]; /*!< Unique network iface identifier */ - char sUID[9]; /*!< Unique network iface identifier */ + uint8_t _uid[10]; /*!< Unique network iface identifier */ + char sUID[10]; /*!< Unique network iface identifier */ const char *_boardId; /*!< Adafruit IO+ board string */ Adafruit_MQTT *_mqtt; /*!< Reference to Adafruit_MQTT, _mqtt. */ char *_topic_description; /*!< MQTT topic for the device description */ diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index b97091b61..141008376 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -35,8 +35,10 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( WS_DEBUG_PRINT("\tFrequency (Hz): "); WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); - // initialize TwoWire w/ desired portNum - _i2c = new TwoWire(msgInitRequest->i2c_port_number); + // initialize TwoWire w/ desired portNum if ESP32-S2 + #if defined(ARDUINO_FEATHER_ESP32) + _i2c = new TwoWire(msgInitRequest->i2c_port_number); + #endif // validate if SDA & SCL has pullup if (digitalRead(msgInitRequest->i2c_pin_sda) == LOW) { pinMode(msgInitRequest->i2c_pin_sda, INPUT_PULLUP); From ba8db39a493230998caa1f0bc0ef1b3c872c6207 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 15:08:42 -0400 Subject: [PATCH 73/95] clang run --- src/Wippersnapper.cpp | 2 +- src/Wippersnapper.h | 4 +- .../WipperSnapper_Component_I2C.cpp | 68 ++++++++++--------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 08f684bd8..22c99c32e 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1221,7 +1221,7 @@ ws_status_t Wippersnapper::run() { WS._mqtt->processPackets(100); // Poll for packets from broker queue, return immediately - //WS._mqtt->processPacketsUntilCallback(10); + // WS._mqtt->processPacketsUntilCallback(10); // TODO: Loop thru components // Process digital inputs, digitalGPIO module diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index d4c94582b..3e3361e4d 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -254,8 +254,8 @@ class Wippersnapper { Wippersnapper_FS *_fileSystem; ///< Instance of filesystem class Wippersnapper_ESP32_nvs *_nvs; ///< Instance of nvs - uint8_t _uid[10]; /*!< Unique network iface identifier */ - char sUID[10]; /*!< Unique network iface identifier */ + uint8_t _uid[10]; /*!< Unique network iface identifier */ + char sUID[10]; /*!< Unique network iface identifier */ const char *_boardId; /*!< Adafruit IO+ board string */ Adafruit_MQTT *_mqtt; /*!< Reference to Adafruit_MQTT, _mqtt. */ char *_topic_description; /*!< MQTT topic for the device description */ diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 141008376..89ba809b8 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -24,7 +24,7 @@ */ /***************************************************************************************************************/ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( - wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { + wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest) { WS_DEBUG_PRINTLN("EXEC: New I2C Port "); WS_DEBUG_PRINT("\tPort #: "); WS_DEBUG_PRINTLN(msgInitRequest->i2c_port_number); @@ -35,10 +35,10 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( WS_DEBUG_PRINT("\tFrequency (Hz): "); WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); - // initialize TwoWire w/ desired portNum if ESP32-S2 - #if defined(ARDUINO_FEATHER_ESP32) - _i2c = new TwoWire(msgInitRequest->i2c_port_number); - #endif +// initialize TwoWire w/ desired portNum if ESP32-S2 +#if defined(ARDUINO_FEATHER_ESP32) + _i2c = new TwoWire(msgInitRequest->i2c_port_number); +#endif // validate if SDA & SCL has pullup if (digitalRead(msgInitRequest->i2c_pin_sda) == LOW) { pinMode(msgInitRequest->i2c_pin_sda, INPUT_PULLUP); @@ -70,9 +70,7 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { @brief Returns if port is already initialized. */ /*****************************************************/ -bool WipperSnapper_Component_I2C::isInitialized() { - return _isInit; -} +bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } /************************************************************************/ /*! @@ -84,7 +82,8 @@ bool WipperSnapper_Component_I2C::isInitialized() { /************************************************************************/ uint16_t WipperSnapper_Component_I2C::scanAddresses( wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { - WS_DEBUG_PRINT("EXEC: I2C Scan on port "); WS_DEBUG_PRINTLN(_portNum); + WS_DEBUG_PRINT("EXEC: I2C Scan on port "); + WS_DEBUG_PRINTLN(_portNum); // decode stream into i2c request uint16_t addrFound = -1; uint16_t scanAddr; @@ -105,7 +104,6 @@ uint16_t WipperSnapper_Component_I2C::scanAddresses( return addrFound; } - /*******************************************************************************/ /*! @brief Initializes I2C device driver and attaches its object to the "bus" @@ -114,35 +112,39 @@ uint16_t WipperSnapper_Component_I2C::scanAddresses( @returns True if I2C device is initialized and attached, False otherwise. */ /*******************************************************************************/ -bool WipperSnapper_Component_I2C::attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) { +bool WipperSnapper_Component_I2C::attachI2CDevice( + wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) { bool attachSuccess = false; // Determine which sensor-specific callback to utilize // AHTX0 Sensor if (msgDeviceInitReq->has_aht_init) { - uint16_t addr = (uint16_t) msgDeviceInitReq->aht_init.address; - WS_DEBUG_PRINTLN("Requesting to initialize AHTx sensor"); - WS_DEBUG_PRINT("\tSensor Addr: ");WS_DEBUG_PRINTLN(addr, HEX); - WS_DEBUG_PRINT("\tTemperature sensor enabled? ");WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_temperature); - WS_DEBUG_PRINT("\tHumidity sensor enabled? ");WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_humidity); + uint16_t addr = (uint16_t)msgDeviceInitReq->aht_init.address; + WS_DEBUG_PRINTLN("Requesting to initialize AHTx sensor"); + WS_DEBUG_PRINT("\tSensor Addr: "); + WS_DEBUG_PRINTLN(addr, HEX); + WS_DEBUG_PRINT("\tTemperature sensor enabled? "); + WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_temperature); + WS_DEBUG_PRINT("\tHumidity sensor enabled? "); + WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_humidity); - // TODO: Create I2C Driver using the an AHT driver sub-class! - I2C_Driver * aht = new I2C_Driver(addr, this->_i2c); - // Attempt to initialize the sensor driver - if (!aht->initAHTX0()) { - attachSuccess = false; - return attachSuccess; - } - // Initialize device-specific sensors - if (msgDeviceInitReq->aht_init.enable_temperature == true) { - aht->enableAHTX0Temperature(); - } - if (msgDeviceInitReq->aht_init.enable_humidity == true) { - aht->enableAHTX0Humidity(); - } - // Push to vector for sensor drivers - activeDrivers.push_back(aht); - attachSuccess = true; + // TODO: Create I2C Driver using the an AHT driver sub-class! + I2C_Driver *aht = new I2C_Driver(addr, this->_i2c); + // Attempt to initialize the sensor driver + if (!aht->initAHTX0()) { + attachSuccess = false; + return attachSuccess; + } + // Initialize device-specific sensors + if (msgDeviceInitReq->aht_init.enable_temperature == true) { + aht->enableAHTX0Temperature(); + } + if (msgDeviceInitReq->aht_init.enable_humidity == true) { + aht->enableAHTX0Humidity(); + } + // Push to vector for sensor drivers + activeDrivers.push_back(aht); + attachSuccess = true; } else { WS_DEBUG_PRINTLN("ERROR: Sensor not found") } From 64b60a55f5d9b0637084705dfa61bc3b7e5d767c Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 2 Aug 2021 16:39:04 -0400 Subject: [PATCH 74/95] check esp32 arch not board --- src/Wippersnapper.cpp | 2 +- src/components/WipperSnapper_Component_I2C.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 22c99c32e..20b0c723e 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -1304,7 +1304,7 @@ ws_status_t Wippersnapper::mqttStatus() { if (millis() > (_prv_ping + WS_KEEPALIVE_INTERVAL_MS)) { WS_DEBUG_PRINT("PINGREQ..."); ping(); - WS_DEBUG_PRINT("PINGRESP!"); + WS_DEBUG_PRINTLN("PINGRESP!"); _prv_ping = millis(); } // blink status LED every STATUS_LED_KAT_BLINK_TIME millis diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 89ba809b8..25fa13c84 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -36,7 +36,7 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); // initialize TwoWire w/ desired portNum if ESP32-S2 -#if defined(ARDUINO_FEATHER_ESP32) +#if defined(ARDUINO_ARCH_ESP32) _i2c = new TwoWire(msgInitRequest->i2c_port_number); #endif // validate if SDA & SCL has pullup From 4ab3114dc411d691192f699935efbc689a4c789f Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 20 Sep 2021 11:26:58 -0400 Subject: [PATCH 75/95] switch build to staging --- src/Wippersnapper.cpp | 11 +++-------- src/Wippersnapper.h | 8 ++++---- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 814ff96cf..2c34c2748 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -69,19 +69,13 @@ Wippersnapper::~Wippersnapper() { /**************************************************************************/ /*! -<<<<<<< HEAD - @brief Executes the provisioning workflow. - NOTE: Must be called prior to connect(). -======= @brief Provisions a WipperSnapper device with its network configuration and Adafruit IO credentials. ->>>>>>> adafruit/master */ /**************************************************************************/ void Wippersnapper::provision() { // init. LED for status signaling statusLEDInit(); - setStatusLEDColor(LED_HW_INIT); #ifdef USE_TINYUSB // init new filesystem _fileSystem = new Wippersnapper_FS(); @@ -457,8 +451,9 @@ bool Wippersnapper::decodeSignalMsg( */ /**************************************************************************/ void cbSignalTopic(char *data, uint16_t len) { - WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal]: "); - printMsgBuffer(data, len); + WS_DEBUG_PRINTLN("cbSignalTopic: New Msg on Signal Topic"); + WS_DEBUG_PRINT(len); + WS_DEBUG_PRINTLN(" bytes."); // zero-out current buffer memset(WS._buffer, 0, sizeof(WS._buffer)); // copy data to buffer diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 7e82e936a..bf8717d46 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -57,11 +57,11 @@ #endif // Uncomment the following use the staging IO server // -//#define USE_STAGING -//#define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server +#define USE_STAGING +#define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server -#define IO_MQTT_SERVER \ - "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) +//#define IO_MQTT_SERVER \ +// "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) #ifdef USE_TINYUSB #include "provisioning/tinyusb/Wippersnapper_FS.h" From adf614552336b7525edf853ebb5f488e18a71867 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 20 Sep 2021 15:40:24 -0400 Subject: [PATCH 76/95] fix build errors --- src/Wippersnapper.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 2c34c2748..31e456dbf 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -643,7 +643,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, /**************************************************************************/ void cbSignalI2CReq(char *data, uint16_t len) { WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal-I2C]: "); - printMsgBuffer(data, len); + WS_DEBUG_PRINT(len); + WS_DEBUG_PRINTLN(" bytes."); // zero-out current buffer memset(WS._buffer, 0, sizeof(WS._buffer)); // copy mqtt data into buffer @@ -663,6 +664,8 @@ void cbSignalI2CReq(char *data, uint16_t len) { &WS.msgSignalI2C)) WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); // TODO: This should be moved into digitalGPIO! +} + /****************************************************************************/ /*! @brief Handles MQTT messages on signal topic until timeout. From f74573fc10821ea0d2539e826ea2eff50d2d8387 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 20 Sep 2021 16:04:56 -0400 Subject: [PATCH 77/95] unsigned to sign scanaddress to match updated protobuf --- src/Wippersnapper.cpp | 2 +- src/components/WipperSnapper_Component_I2C.cpp | 6 +++--- src/components/WipperSnapper_Component_I2C.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 31e456dbf..d8dc5fbef 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -560,7 +560,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } // Scan all requested addresses on i2cportX and ret. address found, -1 // otherwise. - uint16_t addressFound; + int16_t addressFound; if (msgScanReq.i2c_port_number == 0 && WS._i2cPort0->isInitialized() == true) { addressFound = WS._i2cPort0->scanAddresses(msgScanReq); diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/WipperSnapper_Component_I2C.cpp index 25fa13c84..da00890c3 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/WipperSnapper_Component_I2C.cpp @@ -80,13 +80,13 @@ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } @returns The address which an I2C device is located, -1 otherwise. */ /************************************************************************/ -uint16_t WipperSnapper_Component_I2C::scanAddresses( +int16_t WipperSnapper_Component_I2C::scanAddresses( wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { WS_DEBUG_PRINT("EXEC: I2C Scan on port "); WS_DEBUG_PRINTLN(_portNum); // decode stream into i2c request - uint16_t addrFound = -1; - uint16_t scanAddr; + int16_t addrFound = -1; + int16_t scanAddr; for (int i = 0; i < msgScanReq.address_count; i++) { scanAddr = msgScanReq.address[i]; WS_DEBUG_PRINT("* Scanning address "); diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/WipperSnapper_Component_I2C.h index 3262acb91..27c34d225 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/WipperSnapper_Component_I2C.h @@ -30,7 +30,7 @@ class WipperSnapper_Component_I2C { WipperSnapper_Component_I2C( wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); - uint16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + int16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); bool attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); bool isInitialized(); From a56237e204ea9cdeec6bc8ab958720d1711b855c Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 20 Sep 2021 16:13:48 -0400 Subject: [PATCH 78/95] include AHTX0 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 2dfc20765..23e8caaa5 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino library to access WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_IO_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library +depends=Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit_AHTX0 From f122de489a630a3868d080502aa94e02c42103b9 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 27 Sep 2021 15:04:18 -0400 Subject: [PATCH 79/95] use Adafruit AHTX0 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 0cb4bd93d..6411d3989 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino library to access WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_IO_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library +depends=Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit AHTX0 From 0ac06fecc04a95059c6c780a0f7c4117b3791d1a Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 27 Sep 2021 15:33:34 -0400 Subject: [PATCH 80/95] scan a range of addresses --- src/Wippersnapper.cpp | 18 +++++---- src/Wippersnapper.h | 10 ++--- .../WipperSnapper_I2C.cpp} | 38 +++++++++---------- .../WipperSnapper_I2C.h} | 4 +- 4 files changed, 36 insertions(+), 34 deletions(-) rename src/components/{WipperSnapper_Component_I2C.cpp => i2c/WipperSnapper_I2C.cpp} (86%) rename src/components/{WipperSnapper_Component_I2C.h => i2c/WipperSnapper_I2C.h} (89%) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 82254f4b0..307b8ca8a 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -548,7 +548,10 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { - WS_DEBUG_PRINTLN("I2C Scan Request Found!"); + WS_DEBUG_PRINTLN("EXEC: I2C Scan"); + // TODO: Pull this out into Wippersnapper_I2C + /* + wippersnapper_i2c_v1_I2CScanRequest msgScanReq = wippersnapper_i2c_v1_I2CScanRequest_init_zero; // Decode i2c scan request message into struct @@ -558,12 +561,12 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); return false; // fail out if we can't decode the request } - // Scan all requested addresses on i2cportX and ret. address found, -1 - // otherwise. - int16_t addressFound; + // init. a scan response with a zero'd out response + wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; + if (msgScanReq.i2c_port_number == 0 && WS._i2cPort0->isInitialized() == true) { - addressFound = WS._i2cPort0->scanAddresses(msgScanReq); + WS._i2cPort0->scanAddresses(msgScanReq); } else if (msgScanReq.i2c_port_number == 1 && WS._i2cPort1->isInitialized() == true) { addressFound = WS._i2cPort1->scanAddresses(msgScanReq); @@ -572,6 +575,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, "not initialized"); addressFound = -1; } + // TODO! // Create response msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = @@ -584,8 +588,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - return false; // fail out if we can't encode a response - } + return false; // fail out if we can't encode a response */ + } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { WS_DEBUG_PRINTLN("I2C Device Init Request Found!"); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 2e8283293..9b81523c9 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -35,12 +35,10 @@ #include "Wippersnapper_Boards.h" #include "components/statusLED/Wippersnapper_StatusLED_Colors.h" -// Wippersnapper GPIO Components +// Wippersnapper components #include "components/analogIO/Wippersnapper_AnalogIO.h" #include "components/digitalIO/Wippersnapper_DigitalGPIO.h" - -// WipperSnapper I2C Component -#include "components/WipperSnapper_Component_I2C.h" +#include "components/i2c/WipperSnapper_I2C.h" // External libraries #include "Adafruit_MQTT.h" // MQTT Client @@ -73,7 +71,7 @@ #endif #define WS_VERSION \ - "1.0.0-beta.11" ///< WipperSnapper app. version (semver-formatted) + "1.0.0-beta.12" ///< WipperSnapper app. version (semver-formatted) // Reserved Adafruit IO MQTT topics #define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic @@ -81,7 +79,7 @@ // Reserved Wippersnapper topics #define TOPIC_WS "/wprsnpr/" ///< WipperSnapper topic -#define TOPIC_DESCRIPTION "/info/" ///< Registration sub-topic +#define TOPIC_INFO "/info/" ///< Registration sub-topic #define TOPIC_SIGNALS "/signals/" ///< Signals sub-topic #define TOPIC_I2C "/i2c" ///< I2C sub-topic diff --git a/src/components/WipperSnapper_Component_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp similarity index 86% rename from src/components/WipperSnapper_Component_I2C.cpp rename to src/components/i2c/WipperSnapper_I2C.cpp index da00890c3..c64126698 100644 --- a/src/components/WipperSnapper_Component_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -1,5 +1,5 @@ /*! - * @file Wippersnapper_Component_I2C.cpp + * @file WipperSnapper_I2C.cpp * * This component initiates I2C operations * using the Arduino generic TwoWire driver. @@ -14,7 +14,7 @@ * */ -#include "WipperSnapper_Component_I2C.h" +#include "WipperSnapper_I2C.h" /***************************************************************************************************************/ /*! @@ -74,34 +74,34 @@ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } /************************************************************************/ /*! - @brief Destructor for a WipperSnapper I2C component. + @brief Scans addresses on an I2C bus. @param msgScanReq A decoded I2C scan request message. @returns The address which an I2C device is located, -1 otherwise. */ /************************************************************************/ -int16_t WipperSnapper_Component_I2C::scanAddresses( +wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses( wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { WS_DEBUG_PRINT("EXEC: I2C Scan on port "); WS_DEBUG_PRINTLN(_portNum); - // decode stream into i2c request - int16_t addrFound = -1; - int16_t scanAddr; - for (int i = 0; i < msgScanReq.address_count; i++) { - scanAddr = msgScanReq.address[i]; - WS_DEBUG_PRINT("* Scanning address "); - WS_DEBUG_PRINTLN(scanAddr); - _i2c->beginTransmission(scanAddr); + + // init. a scan response with a zero'd out response + wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; + + // Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that respond. + int addrFoundIdx = 0; + for (int addr = 0; addr < 0x77; addr++) { + _i2c->beginTransmission(addr); + // Address ACKed if (_i2c->endTransmission() == 0) { - // found it! - WS_DEBUG_PRINTLN("I2C device detected!"); - addrFound = scanAddr; - break; - } else { - WS_DEBUG_PRINTLN("I2C device not detected!"); + WS_DEBUG_PRINT("Found I2C Device on: "); + WS_DEBUG_PRINTLN(addr); + scanResp.addresses_found[addrFoundIdx] = addr; + addrFoundIdx++; } } - return addrFound; + + return scanResp; } /*******************************************************************************/ diff --git a/src/components/WipperSnapper_Component_I2C.h b/src/components/i2c/WipperSnapper_I2C.h similarity index 89% rename from src/components/WipperSnapper_Component_I2C.h rename to src/components/i2c/WipperSnapper_I2C.h index 27c34d225..70ea85bab 100644 --- a/src/components/WipperSnapper_Component_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -1,5 +1,5 @@ /*! - * @file WipperSnapper_Component_I2C.h + * @file WipperSnapper_I2C.h * * This component initiates I2C operations * using the Arduino generic TwoWire driver. @@ -30,7 +30,7 @@ class WipperSnapper_Component_I2C { WipperSnapper_Component_I2C( wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); - int16_t scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + wippersnapper_i2c_v1_I2CScanResponse scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); bool attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); bool isInitialized(); From e8d6e2c3f08b113e9a56890a7c7027428fe945b4 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 27 Sep 2021 15:43:51 -0400 Subject: [PATCH 81/95] scan into the scanresp --- library.properties | 2 +- src/Wippersnapper.cpp | 25 +++++++++++++++++++++++- src/components/i2c/WipperSnapper_I2C.cpp | 6 ++---- src/components/i2c/WipperSnapper_I2C.h | 2 +- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/library.properties b/library.properties index 6411d3989..7ae87171b 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino library to access WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_IO_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit TinyUSB Library, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit AHTX0 +depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit AHTX0 diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 307b8ca8a..6645a630d 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -548,7 +548,30 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { - WS_DEBUG_PRINTLN("EXEC: I2C Scan"); + WS_DEBUG_PRINTLN("Handling I2C Scan Request"); + // Decode request + wippersnapper_i2c_v1_I2CScanRequest msgScanReq = + wippersnapper_i2c_v1_I2CScanRequest_init_zero; + if (!pb_decode(stream, wippersnapper_i2c_v1_I2CScanRequest_fields, + &msgScanReq)) { + WS_DEBUG_PRINTLN( + "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); + return false; // fail out if we can't decode the request + } + // Create a scan response + wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; + // Handle request + if (msgScanReq.i2c_port_number == 0 && WS._i2cPort0->isInitialized() == true) { + scanResp = WS._i2cPort0->scanAddresses(); + } else if (msgScanReq.i2c_port_number == 1 && WS._i2cPort1->isInitialized() == true) { + scanResp = WS._i2cPort1->scanAddresses(); + } else { + // fail out + WS_DEBUG_PRINTLN("ERROR: The I2C port requested is not initialized!"); + return false; + } + + // TODO: Pull this out into Wippersnapper_I2C /* diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index c64126698..4d0c5e273 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -80,10 +80,8 @@ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } @returns The address which an I2C device is located, -1 otherwise. */ /************************************************************************/ -wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses( - wippersnapper_i2c_v1_I2CScanRequest msgScanReq) { - WS_DEBUG_PRINT("EXEC: I2C Scan on port "); - WS_DEBUG_PRINTLN(_portNum); +wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses() { + WS_DEBUG_PRINT("EXEC: I2C Scan on port "); WS_DEBUG_PRINTLN(_portNum); // init. a scan response with a zero'd out response wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 70ea85bab..25a6fa78f 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -30,7 +30,7 @@ class WipperSnapper_Component_I2C { WipperSnapper_Component_I2C( wippersnapper_i2c_v1_I2CInitRequest *msgInitRequest); ~WipperSnapper_Component_I2C(); - wippersnapper_i2c_v1_I2CScanResponse scanAddresses(wippersnapper_i2c_v1_I2CScanRequest msgScanReq); + wippersnapper_i2c_v1_I2CScanResponse scanAddresses(); bool attachI2CDevice(wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq); bool isInitialized(); From 290c392ad2a09bbcc38e4310148ace7bf3005b83 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 27 Sep 2021 15:50:29 -0400 Subject: [PATCH 82/95] encode the response prior to publishing it --- src/Wippersnapper.cpp | 42 +++++++----------------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 6645a630d..e35ee342e 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -570,49 +570,21 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS_DEBUG_PRINTLN("ERROR: The I2C port requested is not initialized!"); return false; } - - - // TODO: Pull this out into Wippersnapper_I2C - /* - - wippersnapper_i2c_v1_I2CScanRequest msgScanReq = - wippersnapper_i2c_v1_I2CScanRequest_init_zero; - // Decode i2c scan request message into struct - if (!pb_decode(stream, wippersnapper_i2c_v1_I2CScanRequest_fields, - &msgScanReq)) { - WS_DEBUG_PRINTLN( - "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); - return false; // fail out if we can't decode the request - } - // init. a scan response with a zero'd out response - wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; - - if (msgScanReq.i2c_port_number == 0 && - WS._i2cPort0->isInitialized() == true) { - WS._i2cPort0->scanAddresses(msgScanReq); - } else if (msgScanReq.i2c_port_number == 1 && - WS._i2cPort1->isInitialized() == true) { - addressFound = WS._i2cPort1->scanAddresses(msgScanReq); - } else { - WS_DEBUG_PRINTLN("ERROR: Could not execute I2C scan, the I2C port was " - "not initialized"); - addressFound = -1; - } - // TODO! - // Create response + // Encode I2C response message msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; - msgi2cResponse.payload.resp_i2c_scan.address_found = (uint32_t)addressFound; - // Encode message + memcpy(msgi2cResponse.payload.resp_i2c_scan.addresses_found, scanResp.addresses_found, sizeof(scanResp.addresses_found)); + msgi2cResponse.payload.resp_i2c_scan.addresses_found_count = scanResp.addresses_found_count; memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); + // Encode if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - return false; // fail out if we can't encode a response */ - + WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message!"); + return false; // fail out if we can't encode a response + } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { WS_DEBUG_PRINTLN("I2C Device Init Request Found!"); From ebef45707b1e0a7900035247bb5c0ca24fc1fce9 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 28 Sep 2021 16:29:14 -0400 Subject: [PATCH 83/95] check if init first, add root CA for staging --- src/Wippersnapper.cpp | 28 ++++++++++------- src/network_interfaces/Wippersnapper_ESP32.h | 33 +++++++++++++++++++- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index e35ee342e..1ce347eb9 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -548,8 +548,9 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { - WS_DEBUG_PRINTLN("Handling I2C Scan Request"); - // Decode request + WS_DEBUG_PRINTLN("DECODING: I2C Scan Request"); + + // Decode I2CScanRequest wippersnapper_i2c_v1_I2CScanRequest msgScanReq = wippersnapper_i2c_v1_I2CScanRequest_init_zero; if (!pb_decode(stream, wippersnapper_i2c_v1_I2CScanRequest_fields, @@ -558,28 +559,33 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, "ERROR: Could not decode wippersnapper_i2c_v1_I2CScanRequest"); return false; // fail out if we can't decode the request } - // Create a scan response + + // Check if I2C ports were previously initialized + if (!WS._i2cPort0->isInitialized() && !WS._i2cPort0->isInitialized()) { + WS_DEBUG_PRINTLN("ERROR: I2C Ports were not initialized prior to scanning!"); + return false; + } + + // Perform I2C scan wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; - // Handle request - if (msgScanReq.i2c_port_number == 0 && WS._i2cPort0->isInitialized() == true) { + if (msgScanReq.i2c_port_number == 0) { scanResp = WS._i2cPort0->scanAddresses(); - } else if (msgScanReq.i2c_port_number == 1 && WS._i2cPort1->isInitialized() == true) { - scanResp = WS._i2cPort1->scanAddresses(); } else { - // fail out - WS_DEBUG_PRINTLN("ERROR: The I2C port requested is not initialized!"); - return false; + scanResp = WS._i2cPort1->scanAddresses(); } // Encode I2C response message + // TODO: This should be refactored outwards + // General msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; + // ScanResp memcpy(msgi2cResponse.payload.resp_i2c_scan.addresses_found, scanResp.addresses_found, sizeof(scanResp.addresses_found)); msgi2cResponse.payload.resp_i2c_scan.addresses_found_count = scanResp.addresses_found_count; + // General memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); - // Encode if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, &msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message!"); diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 475ea8ae2..4b1fb2abd 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -139,7 +139,37 @@ class Wippersnapper_ESP32 : public Wippersnapper { uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; WiFiClientSecure *_mqtt_client; - // io.adafruit.com root CA +#ifdef USE_STAGING + // io.adafruit.us + const char *_aio_root_ca = + "-----BEGIN CERTIFICATE-----\n" + "MIIEZTCCA02gAwIBAgIQQAF1BIMUpMghjISpDBbN3zANBgkqhkiG9w0BAQsFADA/\n" + "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" + "DkRTVCBSb290IENBIFgzMB4XDTIwMTAwNzE5MjE0MFoXDTIxMDkyOTE5MjE0MFow\n" + "MjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxCzAJBgNVBAMT\n" + "AlIzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuwIVKMz2oJTTDxLs\n" + "jVWSw/iC8ZmmekKIp10mqrUrucVMsa+Oa/l1yKPXD0eUFFU1V4yeqKI5GfWCPEKp\n" + "Tm71O8Mu243AsFzzWTjn7c9p8FoLG77AlCQlh/o3cbMT5xys4Zvv2+Q7RVJFlqnB\n" + "U840yFLuta7tj95gcOKlVKu2bQ6XpUA0ayvTvGbrZjR8+muLj1cpmfgwF126cm/7\n" + "gcWt0oZYPRfH5wm78Sv3htzB2nFd1EbjzK0lwYi8YGd1ZrPxGPeiXOZT/zqItkel\n" + "/xMY6pgJdz+dU/nPAeX1pnAXFK9jpP+Zs5Od3FOnBv5IhR2haa4ldbsTzFID9e1R\n" + "oYvbFQIDAQABo4IBaDCCAWQwEgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8E\n" + "BAMCAYYwSwYIKwYBBQUHAQEEPzA9MDsGCCsGAQUFBzAChi9odHRwOi8vYXBwcy5p\n" + "ZGVudHJ1c3QuY29tL3Jvb3RzL2RzdHJvb3RjYXgzLnA3YzAfBgNVHSMEGDAWgBTE\n" + "p7Gkeyxx+tvhS5B1/8QVYIWJEDBUBgNVHSAETTBLMAgGBmeBDAECATA/BgsrBgEE\n" + "AYLfEwEBATAwMC4GCCsGAQUFBwIBFiJodHRwOi8vY3BzLnJvb3QteDEubGV0c2Vu\n" + "Y3J5cHQub3JnMDwGA1UdHwQ1MDMwMaAvoC2GK2h0dHA6Ly9jcmwuaWRlbnRydXN0\n" + "LmNvbS9EU1RST09UQ0FYM0NSTC5jcmwwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYf\n" + "r52LFMLGMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjANBgkqhkiG9w0B\n" + "AQsFAAOCAQEA2UzgyfWEiDcx27sT4rP8i2tiEmxYt0l+PAK3qB8oYevO4C5z70kH\n" + "ejWEHx2taPDY/laBL21/WKZuNTYQHHPD5b1tXgHXbnL7KqC401dk5VvCadTQsvd8\n" + "S8MXjohyc9z9/G2948kLjmE6Flh9dDYrVYA9x2O+hEPGOaEOa1eePynBgPayvUfL\n" + "qjBstzLhWVQLGAkXXmNs+5ZnPBxzDJOLxhF2JIbeQAcH5H0tZrUlo5ZYyOqA7s9p\n" + "O5b85o3AM/OJ+CktFBQtfvBhcJVd9wvlwPsk+uyOy2HI7mNxKKgsBTt375teA2Tw\n" + "UdHkhVNcsAKX1H7GNNLOEADksd86wuoXvg==\n" + "-----END CERTIFICATE-----\n"; +#else + // io.adafruit.com const char *_aio_root_ca = "-----BEGIN CERTIFICATE-----\n" "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" @@ -163,6 +193,7 @@ class Wippersnapper_ESP32 : public Wippersnapper { "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" "-----END CERTIFICATE-----\n"; +#endif /**************************************************************************/ /*! From 47cbcb5b920f4f0a6a8fed28d0ec0b1be7ef1887 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 29 Sep 2021 12:23:29 -0400 Subject: [PATCH 84/95] scan all --- src/Wippersnapper.cpp | 4 ++++ src/components/i2c/WipperSnapper_I2C.cpp | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 1ce347eb9..0ffea49ff 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -573,6 +573,10 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } else { scanResp = WS._i2cPort1->scanAddresses(); } + + WS_DEBUG_PRINT("# of addresses found on bus: "); + WS_DEBUG_PRINTLN(scanResp.addresses_found_count); + // Encode I2C response message // TODO: This should be refactored outwards // General diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 4d0c5e273..65703d10b 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -86,19 +86,25 @@ wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses( // init. a scan response with a zero'd out response wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; - // Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that respond. - int addrFoundIdx = 0; - for (int addr = 0; addr < 0x77; addr++) { + // Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of those that respond. + byte error; + for (uint16_t addr = 0x08; addr < 0x7F; addr++) { + WS_DEBUG_PRINT("SCANNING: "); + WS_DEBUG_PRINTLN(addr); _i2c->beginTransmission(addr); // Address ACKed if (_i2c->endTransmission() == 0) { WS_DEBUG_PRINT("Found I2C Device on: "); WS_DEBUG_PRINTLN(addr); - scanResp.addresses_found[addrFoundIdx] = addr; - addrFoundIdx++; + scanResp.addresses_found[scanResp.addresses_found_count] = (uint32_t)addr; + scanResp.addresses_found_count++; } } + // DEBUG ONLY + WS_DEBUG_PRINT("# of addresses found on bus: "); + WS_DEBUG_PRINTLN(scanResp.addresses_found_count); + return scanResp; } From 1804934dfd6a347c68dbe4f7701bc4b92f5e9529 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 29 Sep 2021 14:30:44 -0400 Subject: [PATCH 85/95] refactor --- src/Wippersnapper.cpp | 68 ++++++++++++------------ src/components/i2c/WipperSnapper_I2C.cpp | 19 ++----- 2 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 0ffea49ff..bfb0f5533 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -485,6 +485,18 @@ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { WS_DEBUG_PRINTLN("Published!"); } +bool encodeI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { + memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); + pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, + sizeof(WS._buffer_outgoing)); + if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, + msgi2cResponse)) { + WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message!"); + return false; + } + return true; +} + /******************************************************************************************/ /*! @brief Decodes an I2C signal request message and executes the @@ -502,23 +514,21 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, void **arg) { bool is_success = true; WS_DEBUG_PRINTLN("cbDecodeSignalRequestI2C"); - // alloc. response we'll need + // Create I2C Response wippersnapper_signal_v1_I2CResponse msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); - wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = - wippersnapper_i2c_v1_I2CInitRequest_init_zero; - // Create I2C request message - msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; - // Decode i2c request message into struct + // Decode I2CInitRequest + wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; if (!pb_decode(stream, wippersnapper_i2c_v1_I2CInitRequest_fields, &msgI2cInitRequest)) { WS_DEBUG_PRINTLN( "ERROR: Could not decode wippersnapper_i2c_v1_I2CInitRequest"); return false; // fail out } - // Create an I2C object using the message as a constructor + + // Create a new I2C Component if (msgI2cInitRequest.i2c_port_number == 0) { WS._i2cPort0 = new WipperSnapper_Component_I2C(&msgI2cInitRequest); WS.i2cComponents.push_back(WS._i2cPort0); @@ -533,22 +543,19 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, WS_DEBUG_PRINTLN("ERROR: Both I2C ports are in-use"); is_success = false; } - // Create i2c init response + + // Pack I2CResponse message msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag; msgi2cResponse.payload.resp_i2c_init.is_initialized = is_success; - // Encode message - memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, - sizeof(WS._buffer_outgoing)); - if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message"); - return false; // fail out if we can't encode the response + + // Encode I2CResponse message + if (!encodeI2CResponse(&msgi2cResponse)) { + return false; } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) { - WS_DEBUG_PRINTLN("DECODING: I2C Scan Request"); + WS_DEBUG_PRINTLN("I2C Scan Request"); // Decode I2CScanRequest wippersnapper_i2c_v1_I2CScanRequest msgScanReq = @@ -560,7 +567,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, return false; // fail out if we can't decode the request } - // Check if I2C ports were previously initialized + // Check if I2C components were previously initialized if (!WS._i2cPort0->isInitialized() && !WS._i2cPort0->isInitialized()) { WS_DEBUG_PRINTLN("ERROR: I2C Ports were not initialized prior to scanning!"); return false; @@ -573,27 +580,18 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, } else { scanResp = WS._i2cPort1->scanAddresses(); } - - WS_DEBUG_PRINT("# of addresses found on bus: "); + WS_DEBUG_PRINTLN("Scan Complete!"); + WS_DEBUG_PRINT("\t# of addresses found on bus: "); WS_DEBUG_PRINTLN(scanResp.addresses_found_count); - // Encode I2C response message - // TODO: This should be refactored outwards - // General - msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero; - msgi2cResponse.which_payload = - wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; - // ScanResp + // Pack I2CResponse + msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; memcpy(msgi2cResponse.payload.resp_i2c_scan.addresses_found, scanResp.addresses_found, sizeof(scanResp.addresses_found)); msgi2cResponse.payload.resp_i2c_scan.addresses_found_count = scanResp.addresses_found_count; - // General - memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, - sizeof(WS._buffer_outgoing)); - if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, - &msgi2cResponse)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message!"); - return false; // fail out if we can't encode a response + + // Encode I2C Response + if (!encodeI2CResponse(&msgi2cResponse)) { + return false; } } else if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_device_init_tag) { diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 65703d10b..4515b6643 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -74,23 +74,18 @@ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } /************************************************************************/ /*! - @brief Scans addresses on an I2C bus. - @param msgScanReq - A decoded I2C scan request message. - @returns The address which an I2C device is located, -1 otherwise. + @brief Scans all I2C addresses on the bus between 0x08 and 0x7F + inclusive and returns an array of the devices found. + @returns wippersnapper_i2c_v1_I2CScanResponse */ /************************************************************************/ wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses() { - WS_DEBUG_PRINT("EXEC: I2C Scan on port "); WS_DEBUG_PRINTLN(_portNum); - - // init. a scan response with a zero'd out response + WS_DEBUG_PRINT("EXEC: I2C Scan on port # "); WS_DEBUG_PRINTLN(_portNum); + // Create response wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; // Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of those that respond. - byte error; for (uint16_t addr = 0x08; addr < 0x7F; addr++) { - WS_DEBUG_PRINT("SCANNING: "); - WS_DEBUG_PRINTLN(addr); _i2c->beginTransmission(addr); // Address ACKed if (_i2c->endTransmission() == 0) { @@ -101,10 +96,6 @@ wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses( } } - // DEBUG ONLY - WS_DEBUG_PRINT("# of addresses found on bus: "); - WS_DEBUG_PRINTLN(scanResp.addresses_found_count); - return scanResp; } From 5ad6136baea2f3fddce8727af6517892a555535c Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 29 Sep 2021 15:34:04 -0400 Subject: [PATCH 86/95] encode docstring --- src/Wippersnapper.cpp | 39 ++++++++++++++++-------- src/components/i2c/WipperSnapper_I2C.cpp | 12 +++++--- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index bfb0f5533..7de35da72 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -485,12 +485,20 @@ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { WS_DEBUG_PRINTLN("Published!"); } +/******************************************************************************************/ +/*! + @brief Encodes an wippersnapper_signal_v1_I2CResponse message. + @param msgi2cResponse + A pointer to an wippersnapper_signal_v1_I2CResponse. + @return True if encoded successfully, False otherwise. +*/ +/******************************************************************************************/ bool encodeI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { memset(WS._buffer_outgoing, 0, sizeof(WS._buffer_outgoing)); - pb_ostream_t ostream = pb_ostream_from_buffer(WS._buffer_outgoing, - sizeof(WS._buffer_outgoing)); + pb_ostream_t ostream = + pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); if (!pb_encode(&ostream, wippersnapper_signal_v1_I2CResponse_fields, - msgi2cResponse)) { + msgi2cResponse)) { WS_DEBUG_PRINTLN("ERROR: Unable to encode I2C response message!"); return false; } @@ -520,7 +528,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) { WS_DEBUG_PRINTLN("I2C Init Request Found!"); // Decode I2CInitRequest - wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = wippersnapper_i2c_v1_I2CInitRequest_init_zero; + wippersnapper_i2c_v1_I2CInitRequest msgI2cInitRequest = + wippersnapper_i2c_v1_I2CInitRequest_init_zero; if (!pb_decode(stream, wippersnapper_i2c_v1_I2CInitRequest_fields, &msgI2cInitRequest)) { WS_DEBUG_PRINTLN( @@ -569,25 +578,30 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, // Check if I2C components were previously initialized if (!WS._i2cPort0->isInitialized() && !WS._i2cPort0->isInitialized()) { - WS_DEBUG_PRINTLN("ERROR: I2C Ports were not initialized prior to scanning!"); - return false; + WS_DEBUG_PRINTLN( + "ERROR: I2C Ports were not initialized prior to scanning!"); + return false; } // Perform I2C scan - wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; + wippersnapper_i2c_v1_I2CScanResponse scanResp = + wippersnapper_i2c_v1_I2CScanResponse_init_zero; if (msgScanReq.i2c_port_number == 0) { - scanResp = WS._i2cPort0->scanAddresses(); + scanResp = WS._i2cPort0->scanAddresses(); } else { - scanResp = WS._i2cPort1->scanAddresses(); + scanResp = WS._i2cPort1->scanAddresses(); } WS_DEBUG_PRINTLN("Scan Complete!"); WS_DEBUG_PRINT("\t# of addresses found on bus: "); WS_DEBUG_PRINTLN(scanResp.addresses_found_count); // Pack I2CResponse - msgi2cResponse.which_payload = wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; - memcpy(msgi2cResponse.payload.resp_i2c_scan.addresses_found, scanResp.addresses_found, sizeof(scanResp.addresses_found)); - msgi2cResponse.payload.resp_i2c_scan.addresses_found_count = scanResp.addresses_found_count; + msgi2cResponse.which_payload = + wippersnapper_signal_v1_I2CResponse_resp_i2c_scan_tag; + memcpy(msgi2cResponse.payload.resp_i2c_scan.addresses_found, + scanResp.addresses_found, sizeof(scanResp.addresses_found)); + msgi2cResponse.payload.resp_i2c_scan.addresses_found_count = + scanResp.addresses_found_count; // Encode I2C Response if (!encodeI2CResponse(&msgi2cResponse)) { @@ -670,7 +684,6 @@ void cbSignalI2CReq(char *data, uint16_t len) { if (!pb_decode(&istream, wippersnapper_signal_v1_I2CRequest_fields, &WS.msgSignalI2C)) WS_DEBUG_PRINTLN("ERROR: Unable to decode I2C message"); -// TODO: This should be moved into digitalGPIO! } /****************************************************************************/ diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 4515b6643..1976a57c5 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -79,12 +79,16 @@ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } @returns wippersnapper_i2c_v1_I2CScanResponse */ /************************************************************************/ -wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses() { - WS_DEBUG_PRINT("EXEC: I2C Scan on port # "); WS_DEBUG_PRINTLN(_portNum); +wippersnapper_i2c_v1_I2CScanResponse +WipperSnapper_Component_I2C::scanAddresses() { + WS_DEBUG_PRINT("EXEC: I2C Scan on port # "); + WS_DEBUG_PRINTLN(_portNum); // Create response - wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; + wippersnapper_i2c_v1_I2CScanResponse scanResp = + wippersnapper_i2c_v1_I2CScanResponse_init_zero; - // Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of those that respond. + // Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of + // those that respond. for (uint16_t addr = 0x08; addr < 0x7F; addr++) { _i2c->beginTransmission(addr); // Address ACKed From b07a0874138276c4eebe861f3c735347ba242792 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 6 Oct 2021 12:39:09 -0400 Subject: [PATCH 87/95] switch off staging --- src/Wippersnapper.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 9b81523c9..8e891177a 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -55,12 +55,10 @@ #endif // Uncomment the following use the staging IO server // -#define USE_STAGING -#define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server - -//#define IO_MQTT_SERVER \ -// "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) +//#define USE_STAGING +//#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server +#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) #ifdef USE_TINYUSB #include "provisioning/tinyusb/Wippersnapper_FS.h" @@ -71,7 +69,7 @@ #endif #define WS_VERSION \ - "1.0.0-beta.12" ///< WipperSnapper app. version (semver-formatted) + "1.0.0-beta.13" ///< WipperSnapper app. version (semver-formatted) // Reserved Adafruit IO MQTT topics #define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic From 68d08cf7856dc426d358664b57f7582bb27033d8 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 6 Oct 2021 15:12:59 -0400 Subject: [PATCH 88/95] staging test --- src/Wippersnapper.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 8e891177a..20e06f8b3 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -55,10 +55,10 @@ #endif // Uncomment the following use the staging IO server // -//#define USE_STAGING -//#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server +#define USE_STAGING +#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server -#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) +//#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) #ifdef USE_TINYUSB #include "provisioning/tinyusb/Wippersnapper_FS.h" From 464666a6a56849a56bc2348a500aad1cb2c9b6b4 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 6 Oct 2021 15:18:58 -0400 Subject: [PATCH 89/95] fix staging URL --- src/Wippersnapper.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 20e06f8b3..631111a23 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -56,7 +56,7 @@ // Uncomment the following use the staging IO server // #define USE_STAGING -#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server +#define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server //#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) From 2cec9326a375120193355dec43103cf41f5887f2 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Oct 2021 14:39:46 -0400 Subject: [PATCH 90/95] fix for samd --- src/Wippersnapper.h | 13 ++++++------- src/components/i2c/WipperSnapper_I2C.cpp | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index e326e9ecc..1117627a0 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -58,7 +58,8 @@ #define USE_STAGING #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server -//#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server (Production) +//#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server +//(Production) #ifdef USE_TINYUSB #include "provisioning/tinyusb/Wippersnapper_FS.h" @@ -76,10 +77,10 @@ #define TOPIC_IO_ERRORS "/errors" ///< Adafruit IO Error MQTT Topic // Reserved Wippersnapper topics -#define TOPIC_WS "/wprsnpr/" ///< WipperSnapper topic -#define TOPIC_INFO "/info/" ///< Registration sub-topic -#define TOPIC_SIGNALS "/signals/" ///< Signals sub-topic -#define TOPIC_I2C "/i2c" ///< I2C sub-topic +#define TOPIC_WS "/wprsnpr/" ///< WipperSnapper topic +#define TOPIC_INFO "/info/" ///< Registration sub-topic +#define TOPIC_SIGNALS "/signals/" ///< Signals sub-topic +#define TOPIC_I2C "/i2c" ///< I2C sub-topic #define WS_DEBUG ///< Define to enable debugging to serial terminal #define WS_PRINTER Serial ///< Where debug messages will be printed @@ -302,7 +303,6 @@ class Wippersnapper { wippersnapper_signal_v1_CreateSignalRequest _incomingSignalMsg; /*!< Incoming signal message from broker */ - // i2c signal msg wippersnapper_signal_v1_I2CRequest msgSignalI2C = wippersnapper_signal_v1_I2CRequest_init_zero; ///< I2C request wrapper @@ -315,7 +315,6 @@ class Wippersnapper { device. */ bool pinCfgCompleted = false; - private: void _init(); diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 1976a57c5..4338f175a 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -35,10 +35,6 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( WS_DEBUG_PRINT("\tFrequency (Hz): "); WS_DEBUG_PRINTLN(msgInitRequest->i2c_frequency); -// initialize TwoWire w/ desired portNum if ESP32-S2 -#if defined(ARDUINO_ARCH_ESP32) - _i2c = new TwoWire(msgInitRequest->i2c_port_number); -#endif // validate if SDA & SCL has pullup if (digitalRead(msgInitRequest->i2c_pin_sda) == LOW) { pinMode(msgInitRequest->i2c_pin_sda, INPUT_PULLUP); @@ -46,8 +42,19 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C( if (digitalRead(msgInitRequest->i2c_pin_scl) == LOW) { pinMode(msgInitRequest->i2c_pin_scl, INPUT_PULLUP); } - // set up i2c port + +// Initialize TwoWire interface +#if defined(ARDUINO_ARCH_ESP32) + // ESP32, ESP32-S2 + _i2c = new TwoWire(msgInitRequest->i2c_port_number); _i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl); +#else + // SAMD + _i2c = new TwoWire(&PERIPH_WIRE, msgInitRequest->i2c_pin_sda, + msgInitRequest->i2c_pin_scl); + _i2c->begin(); +#endif + _i2c->setClock(msgInitRequest->i2c_frequency); // set i2c obj. properties _portNum = msgInitRequest->i2c_port_number; @@ -81,8 +88,8 @@ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } /************************************************************************/ wippersnapper_i2c_v1_I2CScanResponse WipperSnapper_Component_I2C::scanAddresses() { - WS_DEBUG_PRINT("EXEC: I2C Scan on port # "); - WS_DEBUG_PRINTLN(_portNum); + WS_DEBUG_PRINT("EXEC: I2C Scan"); + // Create response wippersnapper_i2c_v1_I2CScanResponse scanResp = wippersnapper_i2c_v1_I2CScanResponse_init_zero; From b21b71ad64125a76256825fc3520aa115b38a9f7 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Oct 2021 15:05:32 -0400 Subject: [PATCH 91/95] doxygen! --- src/Wippersnapper.cpp | 1 + src/Wippersnapper.h | 4 ++-- src/components/i2c/WipperSnapper_I2C.cpp | 3 ++- src/components/i2c/WipperSnapper_I2C.h | 5 +++++ src/drivers/I2C_Driver.cpp | 6 +++++- src/drivers/I2C_Driver.h | 5 +++++ 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 370b88872..800c853f9 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -522,6 +522,7 @@ bool encodeI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) { Protobuf message's tag type. @param arg Optional arguments from decoder calling function. + @returns True if decoded successfully, False otherwise. */ /******************************************************************************************/ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field, diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 1117627a0..d6403a5cc 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -55,7 +55,7 @@ #endif // Uncomment the following use the staging IO server // -#define USE_STAGING +#define USE_STAGING ///< Use Adafruit IO staging certificate #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server //#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server @@ -313,7 +313,7 @@ class Wippersnapper { milliseconds. */ char *_topic_signal_i2c_device; /*!< Topic carries messages from a broker to a device. */ - bool pinCfgCompleted = false; + bool pinCfgCompleted = false; /*!< Did initial pin sync complete? */ private: void _init(); diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 4338f175a..4e25775da 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -74,7 +74,8 @@ WipperSnapper_Component_I2C::~WipperSnapper_Component_I2C() { /*****************************************************/ /*! - @brief Returns if port is already initialized. + @brief Returns if i2c port is initialized. + @returns True if initialized, False otherwise. */ /*****************************************************/ bool WipperSnapper_Component_I2C::isInitialized() { return _isInit; } diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 25a6fa78f..27b688c66 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -25,6 +25,11 @@ class Wippersnapper; class I2C_Driver; +/**************************************************************************/ +/*! + @brief Class that provides an interface with the I2C bus. +*/ +/**************************************************************************/ class WipperSnapper_Component_I2C { public: WipperSnapper_Component_I2C( diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 7da121226..672a9167b 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -19,7 +19,7 @@ @brief Creates a generic I2C Driver object. @param deviceAddress The i2c device's address. - @param _i2c + @param i2c The desired i2c port. */ /*******************************************************************************/ @@ -44,6 +44,10 @@ I2C_Driver::~I2C_Driver() { /*! @brief Sets an I2C device's polling period, in seconds. + @param Period + The time elapsed between + polling the I2C sensor for new + data. */ /*******************************************/ void I2C_Driver::setPeriod(float period) { _pollPeriod = period; } diff --git a/src/drivers/I2C_Driver.h b/src/drivers/I2C_Driver.h index fc0064f59..34a5a7f2d 100644 --- a/src/drivers/I2C_Driver.h +++ b/src/drivers/I2C_Driver.h @@ -19,6 +19,11 @@ #include +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver interface for the AHTX0. +*/ +/**************************************************************************/ class I2C_Driver { public: // GENERIC, shared From 53f3def9ec693600047c253d268cfebce1782d4a Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Oct 2021 15:15:59 -0400 Subject: [PATCH 92/95] clangg --- src/Wippersnapper.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index d6403a5cc..6d223bd38 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -55,7 +55,7 @@ #endif // Uncomment the following use the staging IO server // -#define USE_STAGING ///< Use Adafruit IO staging certificate +#define USE_STAGING ///< Use Adafruit IO staging certificate #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server //#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server @@ -313,7 +313,7 @@ class Wippersnapper { milliseconds. */ char *_topic_signal_i2c_device; /*!< Topic carries messages from a broker to a device. */ - bool pinCfgCompleted = false; /*!< Did initial pin sync complete? */ + bool pinCfgCompleted = false; /*!< Did initial pin sync complete? */ private: void _init(); From 1bf5405528e7507fbbd20b6f0137735c532cf371 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Oct 2021 15:24:32 -0400 Subject: [PATCH 93/95] fix capitalization --- src/drivers/I2C_Driver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/drivers/I2C_Driver.cpp b/src/drivers/I2C_Driver.cpp index 672a9167b..9a33a4075 100644 --- a/src/drivers/I2C_Driver.cpp +++ b/src/drivers/I2C_Driver.cpp @@ -44,7 +44,7 @@ I2C_Driver::~I2C_Driver() { /*! @brief Sets an I2C device's polling period, in seconds. - @param Period + @param period The time elapsed between polling the I2C sensor for new data. From 6e12866b0eb14320410ea285447a39cf45a88cb9 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Oct 2021 15:28:06 -0400 Subject: [PATCH 94/95] use master prod --- src/Wippersnapper.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 6d223bd38..9a7275efe 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -55,10 +55,11 @@ #endif // Uncomment the following use the staging IO server // -#define USE_STAGING ///< Use Adafruit IO staging certificate -#define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server +//#define USE_STAGING ///< Use Adafruit IO staging +//certificate #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT +//Server -//#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server +#define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server //(Production) #ifdef USE_TINYUSB From 4dae9b77f3004298695816febb6b4e49f5225e68 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Oct 2021 15:38:48 -0400 Subject: [PATCH 95/95] ...clang --- src/Wippersnapper.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 9a7275efe..49a7c6f81 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -56,8 +56,8 @@ // Uncomment the following use the staging IO server // //#define USE_STAGING ///< Use Adafruit IO staging -//certificate #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT -//Server +// certificate #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT +// Server #define IO_MQTT_SERVER "io.adafruit.com" ///< Adafruit IO MQTT Server //(Production)