BayEOSArduino Library
BaySerial.h
1 
18 #ifndef BaySerial_h
19 #define BaySerial_h
20 
21 #define START_BYTE 0x7e
22 #define ESCAPE 0x7d
23 #define XON 0x11
24 #define XOFF 0x13
25 
26 #define API_DATA 0x1
27 #define API_ACK 0x2
28 
29 #define TX_OK 0x1
30 #define TX_CHECKSUM_FAILED 0x2
31 #define TX_BREAK 0x3
32 #define TX_BUSY 0x4
33 
34 #define BaySerialESP_NAME 0x1
35 #define BaySerialESP_GATEWAY 0x2
36 #define BaySerialESP_USER 0x3
37 #define BaySerialESP_PW 0x4
38 
39 #include <inttypes.h>
40 #include <BayEOS.h>
41 #include <BayEOSCommands.h>
42 #include <HardwareSerial.h>
43 #include <Arduino.h>
44 
45 class BaySerialInterface : public BayEOS {
46 public:
55  uint8_t sendPayload(void);
56 
61  void sendFrame(void);
62 
69  uint8_t readIntoPayload(int timeout=0);
70 
71  void sendTXBreak(void){
72  sendAck(TX_BREAK);
73  }
74 
75  virtual int i_available(void)=0;
76  virtual void begin(long baud)=0;
77  virtual int read(void)=0;
78  virtual size_t write(uint8_t c)=0;
79  virtual void flush(void)=0;
80  virtual void end(void)=0;
81 
82 protected:
83  uint8_t readPacket(uint8_t type=API_DATA, int timeout=0);
84  void sendByte(uint8_t b, bool escape);
85  uint8_t readByte(bool escape);
86  void sendAck(uint8_t b);
87 
88  bool _escape;
89  bool _read_timeout;
90  unsigned long _start;
91  uint16_t _timeout;
92  uint16_t _current_timeout;
93  uint8_t _length;
94  uint8_t _checksumTotal;
95  uint8_t _api;
96  uint8_t _ack;
97  uint8_t _pos;
98  uint8_t _break;
99  uint8_t _cts_pin=0;
100  uint8_t _retries=0;
101  long _baud;
102 
103 
104 };
105 
107 protected:
108  HardwareSerial* _serial; //Pointer to existing serial object!!
109 public:
113  BaySerial(HardwareSerial& serial,int timeout=1000,long baud=38400,uint8_t cts_pin=0);
114  //BaySerial(void);
115 
116  int available(void);
117  int i_available(void);
118  void begin(long baud);
119  void flush(void);
120  void end(void);
121  int read(void);
122  size_t write(uint8_t c);
123 
124 };
125 
126 /*
127  * BaySerialESP uses BaySerial to communicate with a
128  * specially programmed ESP-Router-Chip (BayESP8266 Library)
129  *
130  * The ESP-Chip can be attached to the normal FTDI port of the
131  * BayEOS-Board.
132  *
133  */
134 
135 class BaySerialESP : public BaySerial{
136 public:
137  /*
138  * Constructor
139  */
140  BaySerialESP(HardwareSerial& serial, uint8_t ch_pd_pin=0, int timeout=1000);
141  /*
142  * Sends data from buffer to esp and calls send command
143  *
144  * returns:
145  * 0 == OK
146  * other result codes see implementation
147  */
148  uint8_t sendMultiFromBuffer(uint16_t maxsize=1000);
149 
150  /*
151  * Checks if Router is responding
152  * returns
153  * 0 == OK
154  * 1 == not connected to WIFI
155  * 2 == no ack
156  * 3 == no response
157  */
158  uint8_t isReady();
159 
160  /*
161  * Sets the Name of the client an returns the strlen + 10
162  * results < 10 indicate errors
163  * requires BaySerialRouterESP 1.2
164  */
165  uint8_t setName(char* name);
166 
167  /*
168  * Sets the Config of the client an returns the strlen + 10
169  * results < 10 indicate errors
170  * requires BaySerialRouterESP 1.3
171  */
172  uint8_t setConfig(char* value, uint8_t field);
173 
174 
175  /*
176  * Power Up ESP via ch_pd-Pin and call isReady()
177  */
178  uint8_t powerUp(uint8_t tries=20);
179 
180  void powerDown(void);
181 
182 
183 private:
184  uint8_t _ch_pd_pin;
185 };
186 
187 #endif
Definition: BayEOS.h:188
Definition: BaySerial.h:135
Definition: BaySerial.h:106
int available(void)
Definition: BaySerial.cpp:172
BaySerial(HardwareSerial &serial, int timeout=1000, long baud=38400, uint8_t cts_pin=0)
Definition: BaySerial.cpp:165
Definition: BaySerial.h:45
uint8_t readIntoPayload(int timeout=0)
Definition: BaySerial.cpp:95
void sendFrame(void)
Definition: BaySerial.cpp:49
uint8_t sendPayload(void)
Definition: BaySerial.cpp:63