source: rtos_arduino/trunk/arduino_lib/libraries/Ciao/src/lib/espduino.cpp@ 175

Last change on this file since 175 was 175, checked in by ertl-honda, 8 years ago

ライブラリを Arduino IDE 1.7.9 にupdate

File size: 5.6 KB
Line 
1/**
2 * \file
3 * ESP8266 bridge arduino library
4 * \author
5 * Tuan PM <tuanpm@live.com>
6 */
7#include "espduino.h"
8
9RESPONSE::RESPONSE(void * response)
10{
11 cmd = (PACKET_CMD*)response;
12 arg_ptr = (uint8_t*)&cmd->args;
13 arg_num = 0;
14}
15
16uint16_t RESPONSE::getArgc()
17{
18 return cmd->argc;
19}
20uint16_t RESPONSE::argLen()
21{
22 return *(uint16_t*)arg_ptr;
23}
24int32_t RESPONSE::popArgs(uint8_t *data, uint16_t maxLen)
25{
26 uint16_t length, len, incLen = 0;
27
28 if(arg_num >= cmd->argc)
29 return -1;
30
31 length = *(uint16_t*)arg_ptr;
32 len = length;
33 arg_ptr += 2;
34
35 while(length --){
36 *data ++ = *arg_ptr ++;
37 incLen ++;
38 if(incLen > maxLen){
39 arg_num ++;
40 return maxLen;
41 }
42
43 }
44 arg_num ++;
45 return len;
46}
47
48String RESPONSE::popString()
49{
50 String ret;
51 uint16_t len = *(uint16_t*)arg_ptr;
52 arg_ptr += 2;
53 while(len --)
54 ret += (char)*arg_ptr ++;
55 arg_num ++;
56 return ret;
57}
58void RESPONSE::popString(String* data)
59{
60 uint16_t len = *(uint16_t*)arg_ptr;
61 arg_ptr += 2;
62 while(len --)
63 data->concat( (char)*arg_ptr ++);
64 arg_num ++;
65}
66
67void ESP::protoCompletedCb(void)
68{
69 PACKET_CMD *cmd = (PACKET_CMD*)_proto.buf;
70 uint16_t crc = 0, argc, len, resp_crc;
71 uint8_t *data_ptr;
72 argc = cmd->argc;
73 data_ptr = (uint8_t*)&cmd->args ;
74 crc = crc16_data((uint8_t*)&cmd->cmd, 12, crc);
75 while(argc--){
76 len = *((uint16_t*)data_ptr);
77 crc = crc16_data(data_ptr, 2, crc);
78 data_ptr += 2;
79 while(len --){
80 crc = crc16_data(data_ptr, 1, crc);
81 data_ptr ++;
82 }
83 }
84 resp_crc = *(uint16_t*)data_ptr;
85 if(crc != resp_crc) {
86 INFO("ARDUINO: Invalid CRC");
87 return;
88 }
89
90 FP<void, void*> *fp;
91 if(cmd->callback != 0){
92 fp = (FP<void, void*>*)cmd->callback;
93
94 return_cmd = cmd->cmd;
95 return_value = cmd->_return;
96
97 if(fp->attached())
98 (*fp)((void*)cmd);
99 } else {
100 if(cmd->argc == 0) {
101 is_return = true;
102 return_cmd = cmd->cmd;
103 return_value = cmd->_return;
104 }
105
106 }
107}
108
109void ESP::wifiConnect(const char* ssid, const char* password)
110{
111 uint16_t crc;
112 crc = request(CMD_WIFI_CONNECT, (uint32_t)&wifiCb, 0, 2);
113 crc = request(crc,(uint8_t*)ssid, strlen(ssid));
114 crc = request(crc,(uint8_t*)password, strlen(password));
115 request(crc);
116}
117void ESP::write(uint8_t data)
118{
119 switch(data){
120 case SLIP_START:
121 case SLIP_END:
122 case SLIP_REPL:
123 _serial->write(SLIP_REPL);
124 _serial->write(SLIP_ESC(data));
125 break;
126 default:
127 _serial->write(data);
128 }
129}
130void ESP::write(uint8_t* data, uint16_t len)
131{
132 while(len --)
133 write(*data ++);
134}
135uint16_t ESP::request(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc)
136{
137 uint16_t crc = 0;
138 _serial->write(0x7E);
139 write((uint8_t*)&cmd, 2);
140 crc = crc16_data((uint8_t*)&cmd, 2, crc);
141
142 write((uint8_t*)&callback, 4);
143 crc = crc16_data((uint8_t*)&callback, 4, crc);
144
145 write((uint8_t*)&_return, 4);
146 crc = crc16_data((uint8_t*)&_return, 4, crc);
147
148 write((uint8_t*)&argc, 2);
149 crc = crc16_data((uint8_t*)&argc, 2, crc);
150 return crc;
151}
152
153uint16_t ESP::request(uint16_t crc_in, uint8_t* data, uint16_t len)
154{
155 uint8_t temp = 0;
156 uint16_t pad_len = len;
157 while(pad_len % 4 != 0)
158 pad_len++;
159 write((uint8_t*)&pad_len, 2);
160 crc_in = crc16_data((uint8_t*)&pad_len, 2, crc_in);
161 while(len --){
162 write(*data);
163 crc_in = crc16_data((uint8_t*)data, 1, crc_in);
164 data ++;
165 if(pad_len > 0) pad_len --;
166 }
167
168 while(pad_len --){
169 write(temp);
170 crc_in = crc16_data((uint8_t*)&temp, 1, crc_in);
171 }
172 return crc_in;
173}
174uint16_t ESP::request(uint16_t crc)
175{
176 write((uint8_t*)&crc, 2);
177 _serial->write(0x7F);
178}
179
180void ESP::init()
181{
182 _proto.buf = _protoBuf;
183 _proto.bufSize = sizeof(_protoBuf);
184 _proto.dataLen = 0;
185 _proto.isEsc = 0;
186 pinMode(_chip_pd, OUTPUT);
187
188}
189
190ESP::ESP(Stream *serial, int chip_pd):
191_serial(serial), _chip_pd(chip_pd)
192{
193 _debugEn = false;
194 init();
195}
196
197ESP::ESP(Stream *serial, Stream* debug, int chip_pd):
198_serial(serial), _debug(debug), _chip_pd(chip_pd)
199{
200 _debugEn = true;
201 //_serial = _debug;
202 init();
203}
204void ESP::enable()
205{
206 digitalWrite(_chip_pd, HIGH);
207}
208void ESP::disable()
209{
210 digitalWrite(_chip_pd, LOW);
211}
212void ESP::INFO(String info)
213{
214 if(_debugEn)
215 _debug->println(info);
216}
217
218void ESP::reset()
219{
220 uint16_t crc = request(CMD_RESET, 0, 0, 0);
221 request(crc);
222}
223
224boolean ESP::ready()
225{
226 uint32_t wait;
227
228 for(uint8_t wait_time=5; wait_time>0; wait_time--){
229 is_return = false;
230 return_value = 0;
231 uint16_t crc = request(CMD_IS_READY, 0, 1, 0);
232 request(crc);
233 wait = millis();
234 while(is_return == false && (millis() - wait < 1000)) {
235 process();
236 }
237 if(is_return && return_value)
238 return true;
239 }
240 return false;
241
242}
243boolean ESP::waitReturn(uint32_t timeout)
244{
245 is_return = false;
246 return_value = 0;
247 return_cmd = 0;
248 uint32_t wait = millis();
249 while(is_return == false && (millis() - wait < timeout)) {
250 process();
251 }
252 return is_return;
253}
254boolean ESP::waitReturn()
255{
256 return waitReturn(ESP_TIMEOUT);
257}
258void ESP::process()
259{
260 char value;
261 while(_serial->available()) {
262 value = _serial->read();
263 switch(value){
264 case 0x7D:
265 _proto.isEsc = 1;
266 break;
267
268 case 0x7E:
269 _proto.dataLen = 0;
270 _proto.isEsc = 0;
271 _proto.isBegin = 1;
272 break;
273
274 case 0x7F:
275 protoCompletedCb();
276 _proto.isBegin = 0;
277 break;
278
279 default:
280 if(_proto.isBegin == 0) {
281 if(_debugEn) {
282 _debug->write(value);
283 }
284 break;
285 }
286 if(_proto.isEsc){
287 value ^= 0x20;
288 _proto.isEsc = 0;
289 }
290
291 if(_proto.dataLen < _proto.bufSize)
292 _proto.buf[_proto.dataLen++] = value;
293
294 break;
295 }
296 }
297}
Note: See TracBrowser for help on using the repository browser.