1 | #include <arduino.h>
|
---|
2 | #include <mruby.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <fcntl.h>
|
---|
6 | #include "../llbruby.h"
|
---|
7 |
|
---|
8 | serial_t *Serial;
|
---|
9 | serial_t *Serial1; /*1:Serial1*/
|
---|
10 | serial_t *Serial2; /*2:Serial3*/
|
---|
11 | serial_t *Serial3; /*3:Serial2*/
|
---|
12 | serial_t *Serial4; /*4:Serial6*/
|
---|
13 | serial_t *Serial5; /*5:Serial7*/
|
---|
14 |
|
---|
15 | i2c_t Wire1;
|
---|
16 | i2c_t Wire2;
|
---|
17 | i2c_t Wire3;
|
---|
18 | i2c_t Wire4;
|
---|
19 | i2c_t Wire5;
|
---|
20 |
|
---|
21 | char ExeFilename[64];
|
---|
22 |
|
---|
23 | void arduino_init()
|
---|
24 | {
|
---|
25 | }
|
---|
26 |
|
---|
27 | void randomSeed(long seed)
|
---|
28 | {
|
---|
29 | srand(seed);
|
---|
30 | }
|
---|
31 |
|
---|
32 | long arduino_random(long min, long max)
|
---|
33 | {
|
---|
34 | return (long)(((double)rand() / (double)RAND_MAX) / ((double)max - (double)min)) + min;
|
---|
35 | }
|
---|
36 |
|
---|
37 | struct pin_node pin_list;
|
---|
38 |
|
---|
39 | static struct pin_node *find_pin(PinType type, PinName pin)
|
---|
40 | {
|
---|
41 | struct pin_node *ret = &pin_list;
|
---|
42 |
|
---|
43 | while (ret != NULL) {
|
---|
44 | if ((ret->type == type) && (ret->pin == pin)) {
|
---|
45 | break;
|
---|
46 | }
|
---|
47 | ret = ret->next;
|
---|
48 | }
|
---|
49 |
|
---|
50 | return ret;
|
---|
51 | }
|
---|
52 |
|
---|
53 | static struct pin_node *new_pin(PinType type, PinName pin)
|
---|
54 | {
|
---|
55 | struct pin_node *ret = calloc(1, sizeof(struct pin_node));
|
---|
56 |
|
---|
57 | ret->next = pin_list.next;
|
---|
58 | ret->type = type;
|
---|
59 | ret->pin = pin;
|
---|
60 |
|
---|
61 | pin_list.next = ret;
|
---|
62 |
|
---|
63 | return ret;
|
---|
64 | }
|
---|
65 |
|
---|
66 | struct pin_node *get_pin(PinType type, PinName pinno)
|
---|
67 | {
|
---|
68 | struct pin_node *pin;
|
---|
69 |
|
---|
70 | if (pinno == -1)
|
---|
71 | return NULL;
|
---|
72 |
|
---|
73 | pin = find_pin(type, pinno);
|
---|
74 | if (pin != NULL)
|
---|
75 | return pin;
|
---|
76 |
|
---|
77 | pin = new_pin(type, pinno);
|
---|
78 | if (pin == NULL)
|
---|
79 | return NULL;
|
---|
80 |
|
---|
81 | switch (type) {
|
---|
82 | case pintype_gpio:
|
---|
83 | gpio_init(&pin->gpio, pinno);
|
---|
84 | break;
|
---|
85 | case pintype_dac:
|
---|
86 | #if DEVICE_ANALOGOUT
|
---|
87 | analogout_init(&pin->dac, pinno);
|
---|
88 | #endif
|
---|
89 | break;
|
---|
90 | case pintype_pwmout:
|
---|
91 | pwmout_init(&pin->pwmout, pinno);
|
---|
92 | pin->period = 491;
|
---|
93 | break;
|
---|
94 | case pintype_analogin:
|
---|
95 | analogin_init(&pin->analogin, pinno);
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return pin;
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool portToPins(SCI_PORT port, PinName *txpin, PinName *rxpin)
|
---|
103 | {
|
---|
104 | switch (port) {
|
---|
105 | case SCI_USB0:
|
---|
106 | *txpin = 201;
|
---|
107 | *rxpin = 202;
|
---|
108 | break;
|
---|
109 | case SCI_SCI0P2x:
|
---|
110 | *txpin = 1;
|
---|
111 | *rxpin = 2;
|
---|
112 | break;
|
---|
113 | case SCI_SCI2B:
|
---|
114 | *txpin = 1;
|
---|
115 | *rxpin = 2;
|
---|
116 | break;
|
---|
117 | case SCI_SCI6B:
|
---|
118 | *txpin = 1;
|
---|
119 | *rxpin = 2;
|
---|
120 | break;
|
---|
121 | case SCI_SCI2A:
|
---|
122 | *txpin = 1;
|
---|
123 | *rxpin = 2;
|
---|
124 | break;
|
---|
125 | default:
|
---|
126 | *txpin = 0;
|
---|
127 | *rxpin = 0;
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | PinType get_pin_type(PinName pin)
|
---|
135 | {
|
---|
136 | struct pin_node *ret = &pin_list;
|
---|
137 |
|
---|
138 | while (ret != NULL) {
|
---|
139 | if (ret->pin == pin) {
|
---|
140 | return ret->type;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | return pintype_none;
|
---|
145 | }
|
---|
146 |
|
---|
147 | void analogReference(int mode)
|
---|
148 | {
|
---|
149 |
|
---|
150 | }
|
---|
151 |
|
---|
152 | void setPinModeDac(int pin)
|
---|
153 | {
|
---|
154 |
|
---|
155 | }
|
---|
156 |
|
---|
157 | void tone(int pin, int frequency, int duration)
|
---|
158 | {
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | void noTone(int pin)
|
---|
163 | {
|
---|
164 |
|
---|
165 | }
|
---|
166 |
|
---|
167 | int serial_write(serial_t *serial, const unsigned char *buf, int len)
|
---|
168 | {
|
---|
169 | const char *c = (const char *)buf;
|
---|
170 |
|
---|
171 | for (int i = 0; i < len; i++) {
|
---|
172 | serial_putc(serial, c[i]);
|
---|
173 | }
|
---|
174 |
|
---|
175 | return len;
|
---|
176 | }
|
---|
177 |
|
---|
178 | int serial_print(serial_t *serial, const char *str)
|
---|
179 | {
|
---|
180 | int i;
|
---|
181 |
|
---|
182 | for (i = 0; ; i++) {
|
---|
183 | char c = str[i];
|
---|
184 | if (c == '\0')
|
---|
185 | break;
|
---|
186 |
|
---|
187 | serial_putc(serial, c);
|
---|
188 | }
|
---|
189 |
|
---|
190 | return i;
|
---|
191 | }
|
---|
192 |
|
---|
193 | int serial_println(serial_t *serial, const char *str)
|
---|
194 | {
|
---|
195 | int i;
|
---|
196 |
|
---|
197 | for (i = 0; ; i++) {
|
---|
198 | char c = str[i];
|
---|
199 | if (c == '\0')
|
---|
200 | break;
|
---|
201 |
|
---|
202 | serial_putc(serial, c);
|
---|
203 | }
|
---|
204 |
|
---|
205 | i++;
|
---|
206 | serial_putc(serial, '\r');
|
---|
207 |
|
---|
208 | i++;
|
---|
209 | serial_putc(serial, '\n');
|
---|
210 |
|
---|
211 | return i;
|
---|
212 | }
|
---|
213 |
|
---|
214 | uint8_t EEPROM_write(int addr, uint8_t data)
|
---|
215 | {
|
---|
216 | return 0;
|
---|
217 | }
|
---|
218 |
|
---|
219 | uint8_t EEPROM_read(int addr)
|
---|
220 | {
|
---|
221 | return 0xFF;
|
---|
222 | }
|
---|
223 |
|
---|
224 | int EEP_fopen(FILEEEP *fp, const char *str, char mode)
|
---|
225 | {
|
---|
226 | int ret;
|
---|
227 | int fm;
|
---|
228 |
|
---|
229 | switch (mode){
|
---|
230 | case 0:
|
---|
231 | fm = O_RDONLY;
|
---|
232 | break;
|
---|
233 | case 1:
|
---|
234 | fm = O_RDWR | O_CREAT;
|
---|
235 | break;
|
---|
236 | case 2:
|
---|
237 | fm = O_RDWR;
|
---|
238 | break;
|
---|
239 | default:
|
---|
240 | return -1;
|
---|
241 | }
|
---|
242 |
|
---|
243 | ret = open(str, fm);
|
---|
244 | if (ret < 0) {
|
---|
245 | fp->fd = 0;
|
---|
246 | return -1;
|
---|
247 | }
|
---|
248 | fp->fd = ret;
|
---|
249 |
|
---|
250 | return 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | int EEP_fwrite(FILEEEP *fp, byte *data, int *len)
|
---|
254 | {
|
---|
255 | int ret;
|
---|
256 |
|
---|
257 | ret = write(fp->fd, data, *len);
|
---|
258 | if (ret < 0) {
|
---|
259 | *len = 0;
|
---|
260 | return -1;
|
---|
261 | }
|
---|
262 | *len = ret;
|
---|
263 |
|
---|
264 | return 0;
|
---|
265 | }
|
---|
266 |
|
---|
267 | int EEP_fread(FILEEEP *fp)
|
---|
268 | {
|
---|
269 | int ret;
|
---|
270 | char c;
|
---|
271 |
|
---|
272 | ret = read(fp->fd, &c, 1);
|
---|
273 | if (ret < 0)
|
---|
274 | return -1;
|
---|
275 |
|
---|
276 | return c;
|
---|
277 | }
|
---|
278 |
|
---|
279 | void EEP_fclose(FILEEEP *fp)
|
---|
280 | {
|
---|
281 | close(fp->fd);
|
---|
282 | }
|
---|
283 |
|
---|
284 | int EEP_fseek(FILEEEP *fp, int pos, int mode)
|
---|
285 | {
|
---|
286 | int ret;
|
---|
287 |
|
---|
288 | switch (mode){
|
---|
289 | case EEP_SEEKTOP:
|
---|
290 | ret = lseek(fp->fd, pos, SEEK_SET);
|
---|
291 | break;
|
---|
292 | case EEP_SEEKCUR:
|
---|
293 | ret = lseek(fp->fd, pos, SEEK_CUR);
|
---|
294 | break;
|
---|
295 | case EEP_SEEKEND:
|
---|
296 | ret = lseek(fp->fd, pos, SEEK_END);
|
---|
297 | break;
|
---|
298 | default:
|
---|
299 | return -1;
|
---|
300 | break;
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (ret < 0)
|
---|
304 | return false;
|
---|
305 |
|
---|
306 | return true;
|
---|
307 | }
|
---|
308 |
|
---|
309 | int EEP_ffilesize(const char *str)
|
---|
310 | {
|
---|
311 | int ret;
|
---|
312 | struct stat fno;
|
---|
313 |
|
---|
314 | ret = stat(str, &fno);
|
---|
315 | if (ret < 0)
|
---|
316 | return 0;
|
---|
317 |
|
---|
318 | return fno.st_size;
|
---|
319 | }
|
---|
320 |
|
---|
321 | bool EEP_fEof(FILEEEP *fp)
|
---|
322 | {
|
---|
323 | int ret, pos;
|
---|
324 | struct stat fno;
|
---|
325 |
|
---|
326 | ret = fstat(fp->fd, &fno);
|
---|
327 | if (ret < 0)
|
---|
328 | return false;
|
---|
329 |
|
---|
330 | pos = lseek(fp->fd, 0, SEEK_CUR);
|
---|
331 |
|
---|
332 | return pos == fno.st_size;
|
---|
333 | }
|
---|
334 |
|
---|
335 | bool EEP_fexist(const char *path)
|
---|
336 | {
|
---|
337 | int ret;
|
---|
338 | struct stat fno;
|
---|
339 |
|
---|
340 | ret = stat(path, &fno);
|
---|
341 |
|
---|
342 | return (ret == 0);
|
---|
343 | }
|
---|
344 |
|
---|
345 | bool EEP_fcopy(const char *src, const char *dst)
|
---|
346 | {
|
---|
347 | int fsrc, fdst;
|
---|
348 | unsigned char buffer[512];
|
---|
349 | int res = true;
|
---|
350 | int br, bw;
|
---|
351 |
|
---|
352 | fsrc = open(src, O_RDONLY);
|
---|
353 | if (fsrc < 0)
|
---|
354 | return false;
|
---|
355 |
|
---|
356 | fdst = open(dst, O_RDWR | O_CREAT);
|
---|
357 | if (fdst < 0){
|
---|
358 | close(fsrc);
|
---|
359 | return false;
|
---|
360 | }
|
---|
361 |
|
---|
362 | for (;;) {
|
---|
363 | br = read(fsrc, buffer, sizeof(buffer));
|
---|
364 | if (br < 0) {
|
---|
365 | res = false;
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | if (br == 0)
|
---|
369 | break;
|
---|
370 |
|
---|
371 | bw = write(fdst, buffer, br);
|
---|
372 | if ((bw < 0) || bw < br) {
|
---|
373 | res = false;
|
---|
374 | break;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | close(fsrc);
|
---|
379 | close(fdst);
|
---|
380 |
|
---|
381 | return res;
|
---|
382 | }
|
---|
383 |
|
---|
384 | bool EEP_fdelete(const char *path)
|
---|
385 | {
|
---|
386 | int res;
|
---|
387 |
|
---|
388 | res = unlink(path);
|
---|
389 |
|
---|
390 | return (res == 0);
|
---|
391 | }
|
---|
392 |
|
---|
393 | bool SD_open(File *file, const char *path, int mode)
|
---|
394 | {
|
---|
395 | int ret;
|
---|
396 |
|
---|
397 | if (!SD_begin())
|
---|
398 | return false;
|
---|
399 |
|
---|
400 | ret = open(path, mode);
|
---|
401 | if (ret < 0) {
|
---|
402 | file->fd = 0;
|
---|
403 | return false;
|
---|
404 | }
|
---|
405 |
|
---|
406 | file->fd = ret;
|
---|
407 | return true;
|
---|
408 | }
|
---|
409 |
|
---|
410 | bool SD_rmdir(const char *path)
|
---|
411 | {
|
---|
412 | int ret;
|
---|
413 |
|
---|
414 | if (!SD_begin())
|
---|
415 | return false;
|
---|
416 |
|
---|
417 | ret = rmdir(path);
|
---|
418 |
|
---|
419 | return ret == 0;
|
---|
420 | }
|
---|
421 |
|
---|
422 | bool SD_rename(const char *path1, const char *path2)
|
---|
423 | {
|
---|
424 | int ret;
|
---|
425 |
|
---|
426 | if (!SD_begin())
|
---|
427 | return false;
|
---|
428 |
|
---|
429 | ret = rename(path1, path2);
|
---|
430 |
|
---|
431 | return ret == 0;
|
---|
432 | }
|
---|
433 |
|
---|
434 | bool SD_remove(const char *path)
|
---|
435 | {
|
---|
436 | int ret;
|
---|
437 |
|
---|
438 | if (!SD_begin())
|
---|
439 | return false;
|
---|
440 |
|
---|
441 | ret = unlink(path);
|
---|
442 |
|
---|
443 | return ret == 0;
|
---|
444 | }
|
---|
445 |
|
---|
446 | bool SD_exists(const char *path)
|
---|
447 | {
|
---|
448 | int ret;
|
---|
449 | struct stat fno;
|
---|
450 |
|
---|
451 | if (!SD_begin())
|
---|
452 | return false;
|
---|
453 |
|
---|
454 | ret = stat(path, &fno);
|
---|
455 |
|
---|
456 | return ret == 0;
|
---|
457 | }
|
---|
458 |
|
---|
459 | bool SD_mkdir(const char *path)
|
---|
460 | {
|
---|
461 | int ret;
|
---|
462 |
|
---|
463 | if (!SD_begin())
|
---|
464 | return false;
|
---|
465 |
|
---|
466 | ret = mkdir(path, 0777);
|
---|
467 |
|
---|
468 | return ret == 0;
|
---|
469 | }
|
---|
470 |
|
---|
471 | int file_write(File *fp, unsigned char *str, int len)
|
---|
472 | {
|
---|
473 | int ret;
|
---|
474 |
|
---|
475 | ret = write(fp->fd, str, len);
|
---|
476 | if (ret < 0)
|
---|
477 | return -1;
|
---|
478 |
|
---|
479 | return ret;
|
---|
480 | }
|
---|
481 |
|
---|
482 | void file_close(File *fp)
|
---|
483 | {
|
---|
484 | close(fp->fd);
|
---|
485 | }
|
---|
486 |
|
---|
487 | int file_size(File *fp)
|
---|
488 | {
|
---|
489 | int ret;
|
---|
490 | struct stat fno;
|
---|
491 |
|
---|
492 | ret = fstat(fp->fd, &fno);
|
---|
493 | if (ret < 0)
|
---|
494 | return -1;
|
---|
495 |
|
---|
496 | return fno.st_size;
|
---|
497 | }
|
---|
498 |
|
---|
499 | int file_position(File *fp)
|
---|
500 | {
|
---|
501 | int ret;
|
---|
502 | struct stat fno;
|
---|
503 |
|
---|
504 | ret = lseek(fp->fd, 0, SEEK_CUR);
|
---|
505 | if (ret < 0)
|
---|
506 | return -1;
|
---|
507 |
|
---|
508 | return ret;
|
---|
509 | }
|
---|
510 |
|
---|
511 | void file_flush(File *fp)
|
---|
512 | {
|
---|
513 | fsync(fp->fd);
|
---|
514 | }
|
---|
515 |
|
---|
516 | bool file_seek(File *fp, int pos)
|
---|
517 | {
|
---|
518 | int ret;
|
---|
519 |
|
---|
520 | ret = lseek(fp->fd, pos, SEEK_SET);
|
---|
521 |
|
---|
522 | return ret == 0;
|
---|
523 | }
|
---|
524 |
|
---|
525 | int file_read(File *fp)
|
---|
526 | {
|
---|
527 | int ret;
|
---|
528 | char c;
|
---|
529 |
|
---|
530 | ret = read(fp->fd, &c, 1);
|
---|
531 | if (ret < 0)
|
---|
532 | return -1;
|
---|
533 |
|
---|
534 | return c;
|
---|
535 | }
|
---|
536 |
|
---|
537 | void system_reboot(int mode)
|
---|
538 | {
|
---|
539 | exit(mode);
|
---|
540 | }
|
---|
541 |
|
---|
542 | int fileloader(const char *progVer, const char *binVer)
|
---|
543 | {
|
---|
544 | return 0;
|
---|
545 | }
|
---|