Skip to content

Commit

Permalink
add uart detach
Browse files Browse the repository at this point in the history
  • Loading branch information
brentru committed Sep 7, 2023
1 parent 805e43c commit 11bec50
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,8 +1519,8 @@ bool cbDecodeUARTMessage(pb_istream_t *stream, const pb_field_t *field,
}

// Have we previously initialized the UART bus?
if (! WS._uartComponent->is_bus_initialized)
WS._uartComponent->initUARTBus(&msgUARTInitReq); // Init. UART bus
if (!WS._uartComponent->is_bus_initialized)
WS._uartComponent->initUARTBus(&msgUARTInitReq); // Init. UART bus

// Attach UART device to the bus specified in the message
bool did_begin = WS._uartComponent->initUARTDevice(&msgUARTInitReq);
Expand Down Expand Up @@ -1549,6 +1549,20 @@ bool cbDecodeUARTMessage(pb_istream_t *stream, const pb_field_t *field,
1);
WS_DEBUG_PRINTLN("Published!");

} else if (field->tag ==
wippersnapper_signal_v1_UARTRequest_req_uart_device_detach_tag) {
WS_DEBUG_PRINTLN("[New Message] UART Detach");
// attempt to decode uart detach request message
wippersnapper_uart_v1_UARTDeviceDetachRequest msgUARTDetachReq =
wippersnapper_uart_v1_UARTDeviceDetachRequest_init_zero;
if (!pb_decode(stream, wippersnapper_uart_v1_UARTDeviceDetachRequest_fields,
&msgUARTDetachReq)) {
WS_DEBUG_PRINTLN("ERROR: Could not decode message!");
return false;
}
// detach UART device
WS._uartComponent->detachUARTDevice(&msgUARTDetachReq);
WS_DEBUG_PRINTLN("Detached uart device from bus");
} else {
WS_DEBUG_PRINTLN("ERROR: UART message type not found!");
return false;
Expand Down
3 changes: 1 addition & 2 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include "components/servo/ws_servo.h"
#include "components/uart/ws_uart.h"


// ESP32-IDF components and macros
#ifdef ARDUINO_ARCH_ESP32
#define MEMCK \
Expand All @@ -85,8 +84,8 @@
#endif // ARDUINO_ARCH_ESP32

// Note: These might be better off in their respective wrappers
#include <SPI.h>
#include "Adafruit_SleepyDog.h"
#include <SPI.h>

#if defined(USE_TINYUSB)
#include "provisioning/tinyusb/Wippersnapper_FS.h"
Expand Down
18 changes: 18 additions & 0 deletions src/components/uart/drivers/ws_uart_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ class ws_uart_drv {
/*******************************************************************************/
void setPrvPollTime(long curTime) { lastPoll = curTime; }

/*******************************************************************************/
/*!
@brief Gets the UART device's unique identifier.
@returns The UART device's unique identifier.
*/
/*******************************************************************************/
const char *getDeviceID() { return _deviceID; }

/*******************************************************************************/
/*!
@brief Sets the UART device's unique identifier.
@param id
The UART device's unique identifier.
*/
/*******************************************************************************/
void setDeviceID(const char *id) { _deviceID = id; }

/*******************************************************************************/
/*!
@brief Sets the MQTT client used by the uart device driver for
Expand Down Expand Up @@ -119,6 +136,7 @@ class ws_uart_drv {

long pollingInterval; ///< UART device's polling interval, in milliseconds
long lastPoll; ///< Last time the UART device was polled, in milliseconds
const char *_deviceID = nullptr; ///< UART device's ID
Adafruit_MQTT *mqttClient = nullptr; ///< Pointer to MQTT client object
};

Expand Down
2 changes: 2 additions & 0 deletions src/components/uart/drivers/ws_uart_drv_pm25aqi.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
return false;
}
#endif
// Set device's ID
setDeviceID("pm25aqi");
return true;
}

Expand Down
22 changes: 19 additions & 3 deletions src/components/uart/ws_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ bool ws_uart::initUARTDevicePM25AQI(HardwareSerial *hwSerial,
}
// Set MQTT client in driver TODO: This should be handled better, elsewhere!?
_pm25aqi->set_mqtt_client(WS._mqtt);
uartDrivers.push_back(_pm25aqi);
return true;
}

Expand All @@ -105,18 +106,33 @@ bool ws_uart::initUARTDevice(
if (!initUARTDevicePM25AQI(_hwSerial, msgUARTRequest->polling_interval))
return false;
WS_DEBUG_PRINTLN("[INFO, UART]: PM25 UART driver initialized!");
uartDrivers.push_back(_pm25aqi);
} else {
WS_DEBUG_PRINTLN("[ERROR, UART]: Could not find UART device type");
return false;
}
return true;
}

void ws_uart::detachUARTDevice(
wippersnapper_uart_v1_UARTDeviceDetachRequest *msgUARTDetachReq) {
// this is the device ID and the only thing in the msg:
// msgUARTDetachReq->device_id;
for (ws_uart_drv *ptrUARTDriver : uartDrivers) {
// TODO: Add getDeviceID() function to ws_uart_drv
if (strcmp(ptrUARTDriver->getDeviceID(), msgUARTDetachReq->device_id) ==
0) {
// TODO:
// Remove the driver from the vector of drivers
// TODO:
// Then, delete the driver and assign nullptr
}
}
}

/*******************************************************************************/
/*!
@brief Checks each UART driver's polling interval and sends an update of
the device's state to IO.
@brief Checks each UART driver's polling interval and sends an update
of the device's state to IO.
*/
/*******************************************************************************/
void ws_uart::update() {
Expand Down
3 changes: 3 additions & 0 deletions src/components/uart/ws_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class ws_uart {
bool initUARTDevice(wippersnapper_uart_v1_UARTDeviceAttachRequest
*msgUARTRequest); ///< Initializes a UART driver.
bool initUARTDevicePM25AQI(HardwareSerial *hwSerial, int32_t pollingInterval);
void detachUARTDevice(
wippersnapper_uart_v1_UARTDeviceDetachRequest
*msgUARTDetachReq); ///< Detaches a UART device from the UART bus
void update(); ///< Updates the UART device at every polling interval, must be
///< called by main app.
bool is_bus_initialized = false; ///< True if UART bus is initialized
Expand Down

0 comments on commit 11bec50

Please sign in to comment.