Adafruit Library
ws_uart_drv.h
Go to the documentation of this file.
1 
16 #ifndef WS_UART_DRV_H
17 #define WS_UART_DRV_H
18 #include "Wippersnapper.h"
19 #include <Adafruit_Sensor.h>
20 
21 // ESP8266 platform uses SoftwareSerial
22 // so does RP2040 (note that this has differences from the pure softwareserial
23 // library, see: https://arduino-pico.readthedocs.io/en/latest/piouart.html)
24 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_RP2040)
25 #define USE_SW_UART
26 #include <SoftwareSerial.h>
27 #else
28 #include <HardwareSerial.h>
29 #endif
30 
31 /**************************************************************************/
35 /**************************************************************************/
36 class ws_uart_drv {
37 public:
38 #ifdef USE_SW_UART
39  /*******************************************************************************/
47  /*******************************************************************************/
48  ws_uart_drv(SoftwareSerial *swSerial, int32_t interval){};
49 #else
50  /*******************************************************************************/
58  /*******************************************************************************/
59  ws_uart_drv(HardwareSerial *hwSerial, int32_t interval){};
60 #endif
61  ~ws_uart_drv(void) {}
62 
63  /*******************************************************************************/
69  /*******************************************************************************/
70  bool isReady() {
71  if (millis() - _prvPoll > pollingInterval) {
72  return true;
73  }
74  return false;
75  }
76 
77  /*******************************************************************************/
83  /*******************************************************************************/
84  void setPrvPollTime(unsigned long curTime) { _prvPoll = curTime; }
85 
86  /*******************************************************************************/
91  /*******************************************************************************/
92  const char *getDriverID() { return _driverID; }
93 
94  /*******************************************************************************/
100  /*******************************************************************************/
101  void setDriverID(const char *id) { _driverID = strdup(id); }
102 
103  /*******************************************************************************/
112  /*******************************************************************************/
113  virtual void set_mqtt_client(Adafruit_MQTT *AMQTT, const char *mqtt_topic) {
114  mqttClient = AMQTT;
115  uartTopic = mqtt_topic;
116  }
117 
118  /*******************************************************************************/
124  /*******************************************************************************/
125  virtual bool begin() { return false; }
126 
127  /*******************************************************************************/
132  /*******************************************************************************/
133  virtual bool read_data() { return false; }
134 
135  /*******************************************************************************/
147  /*******************************************************************************/
148  void packUARTResponse(wippersnapper_signal_v1_UARTResponse *msgUARTResponse,
149  int event_index,
150  wippersnapper_i2c_v1_SensorType sensor_type,
151  float sensor_value) {
152  msgUARTResponse->payload.resp_uart_device_event.sensor_event[event_index]
153  .type = sensor_type;
154  msgUARTResponse->payload.resp_uart_device_event.sensor_event[event_index]
155  .value = sensor_value;
156  }
157 
158  /*******************************************************************************/
162  /*******************************************************************************/
163  virtual void send_data() {};
164 
165  const char *uartTopic = nullptr;
166  Adafruit_MQTT *mqttClient = nullptr;
167  unsigned long
169 private:
170  long _prvPoll = millis() - (24 * 60 * 60 * 1000);
172  const char *_driverID = nullptr;
173 };
174 
175 #endif // WS_UART_DRV_H
const char * getDriverID()
Gets the UART device&#39;s unique identifier.
Definition: ws_uart_drv.h:92
virtual bool read_data()
Checks if the UART device&#39;s data is ready.
Definition: ws_uart_drv.h:133
virtual void send_data()
Reads the UART device&#39;s data then packs and sends it to IO.
Definition: ws_uart_drv.h:163
virtual void set_mqtt_client(Adafruit_MQTT *AMQTT, const char *mqtt_topic)
Provides the UART device driver with an instance of the application&#39;s MQTT configuration.
Definition: ws_uart_drv.h:113
unsigned long pollingInterval
UART device&#39;s polling interval, in milliseconds.
Definition: ws_uart_drv.h:168
const char * uartTopic
UART device&#39;s MQTT topic.
Definition: ws_uart_drv.h:165
ws_uart_drv(HardwareSerial *hwSerial, int32_t interval)
Initializes a UART device driver.
Definition: ws_uart_drv.h:59
virtual bool begin()
Initializes the UART device driver.
Definition: ws_uart_drv.h:125
Adafruit_MQTT * mqttClient
Pointer to MQTT client object.
Definition: ws_uart_drv.h:166
void setPrvPollTime(unsigned long curTime)
Sets the last time a UART device driver was polled.
Definition: ws_uart_drv.h:84
void packUARTResponse(wippersnapper_signal_v1_UARTResponse *msgUARTResponse, int event_index, wippersnapper_i2c_v1_SensorType sensor_type, float sensor_value)
Packs the UART device&#39;s data into a UARTResponse message.
Definition: ws_uart_drv.h:148
bool isReady()
Checks if the UART device is ready to be polled at its time interval.
Definition: ws_uart_drv.h:70
Base class for UART Device Drivers.
Definition: ws_uart_drv.h:36
void setDriverID(const char *id)
Sets the UART driver&#39;s identifer.
Definition: ws_uart_drv.h:101