BayEOSArduino Library
SDI12.h
Go to the documentation of this file.
1 
113 /*** ==================== Code Organization ======================
114  * - Includes, Defines, & Variable Declarations
115  * - Buffer Setup
116  * - Reading from the SDI-12 Buffer
117  * - Constructor, Destructor, Begins, and Setters
118  * - Using more than one SDI-12 object, isActive() and setActive()
119  * - Setting Proper Data Line States
120  * - Waking up and Talking to the Sensors
121  * - Interrupt Service Routine (getting the data into the buffer)
122  */
123 
124 
125 #ifndef SRC_SDI12_H_
126 #define SRC_SDI12_H_
127 
128 // Import Required Libraries
129 #include <inttypes.h> // integer types library
130 #include <Arduino.h> // Arduino core library
131 #include <Stream.h> // Arduino Stream library
132 #include "SDI12_boards.h" // Include timer information
133 
135 typedef const __FlashStringHelper* FlashString;
136 
138 #define NO_IGNORE_CHAR '\x01'
139 
140 #ifndef SDI12_BUFFER_SIZE
151 #define SDI12_BUFFER_SIZE 81
152 #endif
153 
154 #if defined(ESP32) || defined(ESP8266)
164 enum LookaheadMode {
166  SKIP_ALL,
169  SKIP_NONE,
171  SKIP_WHITESPACE
172 };
183 #define READTIME sdi12timer.SDI12TimerRead()
184 #else
188 #define READTIME TCNTX
189 #endif // defined(ESP32) || defined(ESP8266)
190 
194 class SDI12 : public Stream {
201  private:
205  static SDI12* _activeObject;
209  static SDI12Timer sdi12timer;
215  static const uint16_t bitWidth_micros;
220  static const uint16_t lineBreak_micros;
224  static const uint16_t marking_micros;
225 
229  static const uint8_t txBitWidth;
233  static const uint8_t rxWindowWidth;
237  static const uint8_t bitsPerTick_Q10;
241  static const uint8_t WAITING_FOR_START_BIT;
242 
246  static uint16_t prevBitTCNT;
253  static uint8_t rxState;
260  static uint8_t rxMask;
264  static uint8_t rxValue;
265 
274  static uint16_t mul8x8to16(uint8_t x, uint8_t y);
275 
291  static uint16_t bitTimes(uint8_t dt);
317  private:
326  static uint8_t _rxBuffer[SDI12_BUFFER_SIZE];
330  static volatile uint8_t _rxBufferTail;
334  static volatile uint8_t _rxBufferHead;
338  bool _bufferOverflow;
361  public:
405  int available() override;
416  int peek() override;
423  void clearBuffer();
434  int read() override;
438  void flush() override {}
439 
459  long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
460 
480  float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
481 
482  protected:
492  int peekNextDigit(LookaheadMode lookahead, bool detectDecimal);
502  private:
506  uint8_t _dataPin;
507 
508  public:
518  SDI12();
527  explicit SDI12(int8_t dataPin);
539  ~SDI12();
547  void begin();
557  void begin(int8_t dataPin);
568  void end();
580  int16_t TIMEOUT;
594  void setTimeoutValue(int16_t value);
600  int8_t getDataPin();
606  void setDataPin(int8_t dataPin);
640  public:
655  bool setActive();
656 
665  bool isActive();
713  private:
717  typedef enum SDI12_STATES {
719  SDI12_DISABLED,
721  SDI12_ENABLED,
723  SDI12_HOLDING,
726  SDI12_TRANSMITTING,
729  SDI12_LISTENING
730  } SDI12_STATES;
731 
732 #ifndef __AVR__
746  static uint8_t parity_even_bit(uint8_t v);
747 #endif
748 
756  void setPinInterrupts(bool enable);
765  void setState(SDI12_STATES state);
766 
767  public:
775  void forceHold();
783  void forceListen();
794  private:
832  void wakeSensors(int8_t extraWakeTime = 0);
849  void writeChar(uint8_t out);
850 
851  public:
863  virtual size_t write(uint8_t byte);
867 
882  void sendCommand(String& cmd, int8_t extraWakeTime = 0);
883  void sendCommand(const char* cmd, int8_t extraWakeTime = 0);
884  void sendCommand(FlashString cmd, int8_t extraWakeTime = 0);
887 
898  void sendResponse(String& resp);
899  void sendResponse(const char* resp);
900  void sendResponse(FlashString resp);
902 
903 
913  private:
917  void startChar();
932  void receiveISR();
938  void charToBuffer(uint8_t c);
939 
940  public:
947  static void handleInterrupt();
948 
950  // #define SDI12_EXTERNAL_PCINT
952 };
953 
954 #endif // SRC_SDI12_H_
const __FlashStringHelper * FlashString
Helper for strings stored in flash.
Definition: SDI12.h:135
#define SDI12_BUFFER_SIZE
The buffer size for incoming SDI-12 data.
Definition: SDI12.h:151
#define NO_IGNORE_CHAR
a char not found in a valid ASCII numeric field
Definition: SDI12.h:138
This file defines the timing units needed for various Arduino boards.
The main class for SDI 12 instances.
Definition: SDI12.h:194
The class used to define the processor timer for the SDI-12 serial emulation.
Definition: SDI12_boards.h:33
void sendCommand(String &cmd, int8_t extraWakeTime=0)
Send a command out on the data line, acting as a datalogger (master)
Definition: SDI12.cpp:490
void sendResponse(String &resp)
Send a response out on the data line (for slave use)
Definition: SDI12.cpp:519
virtual size_t write(uint8_t byte)
Write out a byte on the SDI-12 line.
Definition: SDI12.cpp:482
void setDataPin(int8_t dataPin)
Set the data pin for the current SDI-12 instance.
Definition: SDI12.cpp:279
int8_t getDataPin()
Get the data pin for the current SDI-12 instance.
Definition: SDI12.cpp:284
~SDI12()
Destroy the SDI12 object.
Definition: SDI12.cpp:243
SDI12()
Construct a new SDI12 instance with no data pin set.
Definition: SDI12.cpp:213
void end()
Disable the SDI-12 object (but do not destroy it).
Definition: SDI12.cpp:265
int16_t TIMEOUT
The value to return if a parse or read times out with no return from the sensor.
Definition: SDI12.h:580
void begin()
Begin the SDI-12 object.
Definition: SDI12.cpp:252
void setTimeoutValue(int16_t value)
Set the value to return if a parse int or parse float times out with no return from the sensor.
Definition: SDI12.cpp:274
static void handleInterrupt()
Intermediary used by the ISR - passes off responsibility for the interrupt to the active object.
Definition: SDI12.cpp:560
void forceListen()
Set line state to SDI12_LISTENING.
Definition: SDI12.cpp:400
void forceHold()
Set line state to SDI12_HOLDING.
Definition: SDI12.cpp:395
bool isActive()
Check if this instance is active.
Definition: SDI12.cpp:301
bool setActive()
Set this instance as the active SDI-12 instance.
Definition: SDI12.cpp:291
void flush() override
Wait for sending to finish - because no TX buffering, does nothing.
Definition: SDI12.h:438
int available() override
Return the number of bytes available in the Rx buffer.
Definition: SDI12.cpp:100
long parseInt(LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
Return the first valid (long) integer value from the current position.
Definition: SDI12.cpp:152
float parseFloat(LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
Return the first valid float value from the current position.
Definition: SDI12.cpp:178
int peek() override
Reveal next byte in the Rx buffer without consuming it.
Definition: SDI12.cpp:106
void clearBuffer()
Clear the Rx buffer by setting the head and tail pointers to the same value.
Definition: SDI12.cpp:113
int peekNextDigit(LookaheadMode lookahead, bool detectDecimal)
Return the next numeric digit in the stream or -1 if timeout.
Definition: SDI12.cpp:128
int read() override
Return next byte in the Rx buffer, consuming it.
Definition: SDI12.cpp:119