Skip to content

Commit

Permalink
Merge pull request #407 from brentru/fix-analog-in
Browse files Browse the repository at this point in the history
Fix and refactor analog input class
  • Loading branch information
brentru authored Feb 7, 2023
2 parents 2001af0 + d7b47ac commit 480a034
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 268 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit WipperSnapper
version=1.0.0-beta.59
version=1.0.0-beta.60
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino client for Adafruit.io WipperSnapper
Expand Down
107 changes: 57 additions & 50 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,67 +215,70 @@ void Wippersnapper::set_user_key() {
}

// Decoders //

/****************************************************************************/
/*!
@brief Configures a pin according to a
@brief Configures an analog input pin according to a
wippersnapper_pin_v1_ConfigurePinRequest message.
@param pinMsg
Pointer to a wippersnapper_pin_v1_ConfigurePinRequest message.
@returns True if pin configured successfully, False otherwise.
@returns True if analog pin configured successfully, False otherwise.
*/
/****************************************************************************/
bool Wippersnapper::configurePinRequest(
bool Wippersnapper::configAnalogInPinReq(
wippersnapper_pin_v1_ConfigurePinRequest *pinMsg) {
WS_DEBUG_PRINTLN("configurePinRequest");

bool is_success = true;

#if defined(ARDUINO_ARCH_RP2040)
char *pinName = pinMsg->pin_name + 1;
int pin = atoi(pinName);
#else
char *pinName = pinMsg->pin_name + 1;
int pin = atoi(pinName);
#endif

// Decode pin mode
if (pinMsg->mode == wippersnapper_pin_v1_Mode_MODE_DIGITAL) {
if (pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_CREATE) {
// Initialize GPIO pin
WS._digitalGPIO->initDigitalPin(pinMsg->direction, pin, pinMsg->period,
pinMsg->pull);
} else if (
pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_DELETE) {
// Delete digital GPIO pin
WS._digitalGPIO->deinitDigitalPin(pinMsg->direction, pin);
} else {
WS_DEBUG_PRINTLN("ERROR: Could not decode digital pin request type");
}
if (pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_CREATE) {
WS._analogIO->initAnalogInputPin(pin, pinMsg->period, pinMsg->pull,
pinMsg->analog_read_mode);
} else if (
pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_DELETE) {
WS._analogIO->deinitAnalogPin(pinMsg->direction, pin);
} else {
WS_DEBUG_PRINTLN("ERROR: Could not decode analog pin request!");
is_success = false;
}
return is_success;
}

else if (pinMsg->mode == wippersnapper_pin_v1_Mode_MODE_ANALOG) {
if (pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_CREATE) {
// Initialize analog io pin
if (pinMsg->direction ==
wippersnapper_pin_v1_ConfigurePinRequest_Direction_DIRECTION_INPUT) {
WS._analogIO->initAnalogInputPin(pin, pinMsg->period, pinMsg->pull,
pinMsg->analog_read_mode);
} else if (
pinMsg->direction ==
wippersnapper_pin_v1_ConfigurePinRequest_Direction_DIRECTION_OUTPUT) {
WS._analogIO->initAnalogOutputPin(pin);
} else {
WS_DEBUG_PRINTLN("ERROR: Unable to decode analog pin direction.")
is_success = false;
}
} else if (
pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_DELETE) {
// Delete analog io pin
WS._analogIO->deinitAnalogPin(pinMsg->direction, pin);
} else {
WS_DEBUG_PRINTLN("ERROR: Could not decode digital pin request type");
}
/****************************************************************************/
/*!
@brief Configures a pin according to a
wippersnapper_pin_v1_ConfigurePinRequest message.
@param pinMsg
Pointer to a wippersnapper_pin_v1_ConfigurePinRequest message.
@returns True if pin configured successfully, False otherwise.
*/
/****************************************************************************/
bool Wippersnapper::configureDigitalPinReq(
wippersnapper_pin_v1_ConfigurePinRequest *pinMsg) {
bool is_success = true;
char *pinName = pinMsg->pin_name + 1;
int pin = atoi(pinName);

if (pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_CREATE) {
// Initialize GPIO pin
WS._digitalGPIO->initDigitalPin(pinMsg->direction, pin, pinMsg->period,
pinMsg->pull);
} else if (
pinMsg->request_type ==
wippersnapper_pin_v1_ConfigurePinRequest_RequestType_REQUEST_TYPE_DELETE) {
// Delete digital GPIO pin
WS._digitalGPIO->deinitDigitalPin(pinMsg->direction, pin);
} else {
WS_DEBUG_PRINTLN("ERROR: Could not decode pin mode")
is_success = false;
WS_DEBUG_PRINTLN("ERROR: Could not decode digital pin request type");
}

return is_success;
Expand Down Expand Up @@ -307,9 +310,13 @@ bool cbDecodePinConfigMsg(pb_istream_t *stream, const pb_field_t *field,
is_success = false;
}

// Pass ConfigurePinRequest message
if (!WS.configurePinRequest(&pinReqMsg)) {
WS_DEBUG_PRINTLN("Unable to configure pin");
// Decode pin configuration request msg
if (pinReqMsg.mode == wippersnapper_pin_v1_Mode_MODE_DIGITAL) {
is_success = WS.configureDigitalPinReq(&pinReqMsg);
} else if (pinReqMsg.mode == wippersnapper_pin_v1_Mode_MODE_ANALOG) {
is_success = WS.configAnalogInPinReq(&pinReqMsg);
} else {
WS_DEBUG_PRINTLN("ERROR: Pin mode invalid!");
is_success = false;
}

Expand Down Expand Up @@ -2390,7 +2397,7 @@ ws_status_t Wippersnapper::run() {
WS.feedWDT();

// Process analog inputs
WS._analogIO->processAnalogInputs();
WS._analogIO->update();
WS.feedWDT();

// Process I2C sensor events
Expand Down
5 changes: 3 additions & 2 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
#endif

#define WS_VERSION \
"1.0.0-beta.59" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.60" ///< WipperSnapper app. version (semver-formatted)

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
Expand Down Expand Up @@ -279,7 +279,8 @@ class Wippersnapper {
uint8_t pinName, int pinVal);

// Pin configure message
bool configurePinRequest(wippersnapper_pin_v1_ConfigurePinRequest *pinMsg);
bool configureDigitalPinReq(wippersnapper_pin_v1_ConfigurePinRequest *pinMsg);
bool configAnalogInPinReq(wippersnapper_pin_v1_ConfigurePinRequest *pinMsg);

// I2C
std::vector<WipperSnapper_Component_I2C *>
Expand Down
Loading

0 comments on commit 480a034

Please sign in to comment.