[270] | 1 | #include <arduino.h>
|
---|
| 2 | #include <mruby.h>
|
---|
[279] | 3 | #include <stdlib.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <fcntl.h>
|
---|
[270] | 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 | {
|
---|
[279] | 29 | srand(seed);
|
---|
[270] | 30 | }
|
---|
| 31 |
|
---|
| 32 | long arduino_random(long min, long max)
|
---|
| 33 | {
|
---|
[279] | 34 | return (long)(((double)rand() / (double)RAND_MAX) / ((double)max - (double)min)) + min;
|
---|
[270] | 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 | {
|
---|
[279] | 226 | int ret;
|
---|
| 227 | int fm;
|
---|
[270] | 228 |
|
---|
| 229 | switch (mode){
|
---|
| 230 | case 0:
|
---|
[279] | 231 | fm = O_RDONLY;
|
---|
[270] | 232 | break;
|
---|
| 233 | case 1:
|
---|
[279] | 234 | fm = O_RDWR | O_CREAT;
|
---|
[270] | 235 | break;
|
---|
| 236 | case 2:
|
---|
[279] | 237 | fm = O_RDWR;
|
---|
[270] | 238 | break;
|
---|
| 239 | default:
|
---|
| 240 | return -1;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[279] | 243 | ret = open(str, fm);
|
---|
| 244 | if (ret < 0) {
|
---|
| 245 | fp->fd = 0;
|
---|
| 246 | return -1;
|
---|
| 247 | }
|
---|
| 248 | fp->fd = ret;
|
---|
[270] | 249 |
|
---|
[279] | 250 | return 0;
|
---|
[270] | 251 | }
|
---|
| 252 |
|
---|
| 253 | int EEP_fwrite(FILEEEP *fp, byte *data, int *len)
|
---|
| 254 | {
|
---|
[279] | 255 | int ret;
|
---|
[270] | 256 |
|
---|
[279] | 257 | ret = write(fp->fd, data, *len);
|
---|
| 258 | if (ret < 0) {
|
---|
| 259 | *len = 0;
|
---|
| 260 | return -1;
|
---|
| 261 | }
|
---|
| 262 | *len = ret;
|
---|
[270] | 263 |
|
---|
[279] | 264 | return 0;
|
---|
[270] | 265 | }
|
---|
| 266 |
|
---|
| 267 | int EEP_fread(FILEEEP *fp)
|
---|
| 268 | {
|
---|
[279] | 269 | int ret;
|
---|
[270] | 270 | char c;
|
---|
| 271 |
|
---|
[279] | 272 | ret = read(fp->fd, &c, 1);
|
---|
| 273 | if (ret < 0)
|
---|
[270] | 274 | return -1;
|
---|
| 275 |
|
---|
| 276 | return c;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | void EEP_fclose(FILEEEP *fp)
|
---|
| 280 | {
|
---|
[279] | 281 | close(fp->fd);
|
---|
[270] | 282 | }
|
---|
| 283 |
|
---|
| 284 | int EEP_fseek(FILEEEP *fp, int pos, int mode)
|
---|
| 285 | {
|
---|
[279] | 286 | int ret;
|
---|
[270] | 287 |
|
---|
| 288 | switch (mode){
|
---|
| 289 | case EEP_SEEKTOP:
|
---|
[279] | 290 | ret = lseek(fp->fd, pos, SEEK_SET);
|
---|
[270] | 291 | break;
|
---|
| 292 | case EEP_SEEKCUR:
|
---|
[279] | 293 | ret = lseek(fp->fd, pos, SEEK_CUR);
|
---|
[270] | 294 | break;
|
---|
| 295 | case EEP_SEEKEND:
|
---|
[279] | 296 | ret = lseek(fp->fd, pos, SEEK_END);
|
---|
[270] | 297 | break;
|
---|
| 298 | default:
|
---|
[279] | 299 | return -1;
|
---|
[270] | 300 | break;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[279] | 303 | if (ret < 0)
|
---|
| 304 | return false;
|
---|
[270] | 305 |
|
---|
[279] | 306 | return true;
|
---|
[270] | 307 | }
|
---|
| 308 |
|
---|
| 309 | int EEP_ffilesize(const char *str)
|
---|
| 310 | {
|
---|
[279] | 311 | int ret;
|
---|
| 312 | struct stat fno;
|
---|
[270] | 313 |
|
---|
[279] | 314 | ret = stat(str, &fno);
|
---|
| 315 | if (ret < 0)
|
---|
[270] | 316 | return 0;
|
---|
| 317 |
|
---|
[279] | 318 | return fno.st_size;
|
---|
[270] | 319 | }
|
---|
| 320 |
|
---|
| 321 | bool EEP_fEof(FILEEEP *fp)
|
---|
| 322 | {
|
---|
[279] | 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;
|
---|
[270] | 333 | }
|
---|
| 334 |
|
---|
| 335 | bool EEP_fexist(const char *path)
|
---|
| 336 | {
|
---|
[279] | 337 | int ret;
|
---|
| 338 | struct stat fno;
|
---|
[270] | 339 |
|
---|
[279] | 340 | ret = stat(path, &fno);
|
---|
[270] | 341 |
|
---|
[279] | 342 | return (ret == 0);
|
---|
[270] | 343 | }
|
---|
| 344 |
|
---|
| 345 | bool EEP_fcopy(const char *src, const char *dst)
|
---|
| 346 | {
|
---|
[279] | 347 | int fsrc, fdst;
|
---|
| 348 | unsigned char buffer[512];
|
---|
| 349 | int res = true;
|
---|
| 350 | int br, bw;
|
---|
[270] | 351 |
|
---|
[279] | 352 | fsrc = open(src, O_RDONLY);
|
---|
| 353 | if (fsrc < 0)
|
---|
[270] | 354 | return false;
|
---|
| 355 |
|
---|
[279] | 356 | fdst = open(dst, O_RDWR | O_CREAT);
|
---|
| 357 | if (fdst < 0){
|
---|
| 358 | close(fsrc);
|
---|
[270] | 359 | return false;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | for (;;) {
|
---|
[279] | 363 | br = read(fsrc, buffer, sizeof(buffer));
|
---|
| 364 | if (br < 0) {
|
---|
| 365 | res = false;
|
---|
[270] | 366 | break;
|
---|
[279] | 367 | }
|
---|
| 368 | if (br == 0)
|
---|
| 369 | break;
|
---|
[270] | 370 |
|
---|
[279] | 371 | bw = write(fdst, buffer, br);
|
---|
| 372 | if ((bw < 0) || bw < br) {
|
---|
| 373 | res = false;
|
---|
[270] | 374 | break;
|
---|
[279] | 375 | }
|
---|
[270] | 376 | }
|
---|
| 377 |
|
---|
[279] | 378 | close(fsrc);
|
---|
| 379 | close(fdst);
|
---|
[270] | 380 |
|
---|
[279] | 381 | return res;
|
---|
[270] | 382 | }
|
---|
| 383 |
|
---|
| 384 | bool EEP_fdelete(const char *path)
|
---|
| 385 | {
|
---|
[279] | 386 | int res;
|
---|
[270] | 387 |
|
---|
[279] | 388 | res = unlink(path);
|
---|
[270] | 389 |
|
---|
[279] | 390 | return (res == 0);
|
---|
[270] | 391 | }
|
---|
| 392 |
|
---|
[279] | 393 | bool SD_open(File *file, const char *path, int mode)
|
---|
[270] | 394 | {
|
---|
[279] | 395 | int ret;
|
---|
[270] | 396 |
|
---|
[279] | 397 | if (!SD_begin())
|
---|
[270] | 398 | return false;
|
---|
| 399 |
|
---|
[279] | 400 | ret = open(path, mode);
|
---|
| 401 | if (ret < 0) {
|
---|
| 402 | file->fd = 0;
|
---|
[270] | 403 | return false;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[279] | 406 | file->fd = ret;
|
---|
[270] | 407 | return true;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | bool SD_rmdir(const char *path)
|
---|
| 411 | {
|
---|
[279] | 412 | int ret;
|
---|
[270] | 413 |
|
---|
| 414 | if (!SD_begin())
|
---|
| 415 | return false;
|
---|
| 416 |
|
---|
[279] | 417 | ret = rmdir(path);
|
---|
[270] | 418 |
|
---|
[279] | 419 | return ret == 0;
|
---|
[270] | 420 | }
|
---|
| 421 |
|
---|
| 422 | bool SD_rename(const char *path1, const char *path2)
|
---|
| 423 | {
|
---|
[279] | 424 | int ret;
|
---|
[270] | 425 |
|
---|
| 426 | if (!SD_begin())
|
---|
| 427 | return false;
|
---|
| 428 |
|
---|
[279] | 429 | ret = rename(path1, path2);
|
---|
[270] | 430 |
|
---|
[279] | 431 | return ret == 0;
|
---|
[270] | 432 | }
|
---|
| 433 |
|
---|
| 434 | bool SD_remove(const char *path)
|
---|
| 435 | {
|
---|
[279] | 436 | int ret;
|
---|
[270] | 437 |
|
---|
| 438 | if (!SD_begin())
|
---|
| 439 | return false;
|
---|
| 440 |
|
---|
[279] | 441 | ret = unlink(path);
|
---|
[270] | 442 |
|
---|
[279] | 443 | return ret == 0;
|
---|
[270] | 444 | }
|
---|
| 445 |
|
---|
| 446 | bool SD_exists(const char *path)
|
---|
| 447 | {
|
---|
[279] | 448 | int ret;
|
---|
| 449 | struct stat fno;
|
---|
[270] | 450 |
|
---|
| 451 | if (!SD_begin())
|
---|
| 452 | return false;
|
---|
| 453 |
|
---|
[279] | 454 | ret = stat(path, &fno);
|
---|
[270] | 455 |
|
---|
[279] | 456 | return ret == 0;
|
---|
[270] | 457 | }
|
---|
| 458 |
|
---|
| 459 | bool SD_mkdir(const char *path)
|
---|
| 460 | {
|
---|
[279] | 461 | int ret;
|
---|
[270] | 462 |
|
---|
| 463 | if (!SD_begin())
|
---|
| 464 | return false;
|
---|
| 465 |
|
---|
[279] | 466 | ret = mkdir(path, 0777);
|
---|
[270] | 467 |
|
---|
[279] | 468 | return ret == 0;
|
---|
[270] | 469 | }
|
---|
| 470 |
|
---|
[279] | 471 | int file_write(File *fp, unsigned char *str, int len)
|
---|
[270] | 472 | {
|
---|
[279] | 473 | int ret;
|
---|
[270] | 474 |
|
---|
[279] | 475 | ret = write(fp->fd, str, len);
|
---|
| 476 | if (ret < 0)
|
---|
[270] | 477 | return -1;
|
---|
| 478 |
|
---|
[279] | 479 | return ret;
|
---|
[270] | 480 | }
|
---|
| 481 |
|
---|
[279] | 482 | void file_close(File *fp)
|
---|
[270] | 483 | {
|
---|
[279] | 484 | close(fp->fd);
|
---|
[270] | 485 | }
|
---|
| 486 |
|
---|
[279] | 487 | int file_size(File *fp)
|
---|
[270] | 488 | {
|
---|
[279] | 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;
|
---|
[270] | 497 | }
|
---|
| 498 |
|
---|
[279] | 499 | int file_position(File *fp)
|
---|
[270] | 500 | {
|
---|
[279] | 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;
|
---|
[270] | 509 | }
|
---|
| 510 |
|
---|
[279] | 511 | void file_flush(File *fp)
|
---|
[270] | 512 | {
|
---|
[279] | 513 | fsync(fp->fd);
|
---|
[270] | 514 | }
|
---|
| 515 |
|
---|
[279] | 516 | bool file_seek(File *fp, int pos)
|
---|
[270] | 517 | {
|
---|
[279] | 518 | int ret;
|
---|
[270] | 519 |
|
---|
[279] | 520 | ret = lseek(fp->fd, pos, SEEK_SET);
|
---|
[270] | 521 |
|
---|
[279] | 522 | return ret == 0;
|
---|
[270] | 523 | }
|
---|
| 524 |
|
---|
[279] | 525 | int file_read(File *fp)
|
---|
[270] | 526 | {
|
---|
[279] | 527 | int ret;
|
---|
[270] | 528 | char c;
|
---|
| 529 |
|
---|
[279] | 530 | ret = read(fp->fd, &c, 1);
|
---|
| 531 | if (ret < 0)
|
---|
[270] | 532 | return -1;
|
---|
| 533 |
|
---|
| 534 | return c;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | void system_reboot(int mode)
|
---|
| 538 | {
|
---|
[279] | 539 | exit(mode);
|
---|
[270] | 540 | }
|
---|
| 541 |
|
---|
| 542 | int fileloader(const char *progVer, const char *binVer)
|
---|
| 543 | {
|
---|
| 544 | return 0;
|
---|
| 545 | }
|
---|