seeed のモーションセンサーは、室内への人の出入りや室内の人の存在をじっとしている人も含めて検出できるというのでポチッてみた。
24GHz mmWave Radar Sensor – Human Static Presence Module
※必要なのかどうかはわからないが技適マークはないようなので取り扱いには注意してほしい。
試してみたところ人間のみ検出するということであったがセンサーの検出方向にあるエアコンのファンの回転を検出してしまい室内に誰もいなくなっても誰かがいるという判断になってしまったりする。室内空間の広さとか家具等の配置なども影響するのかもしれないがシビア過ぎるのかなかなか正しい判断をしてくれなくて正直なところ使い物にはならないかもと思ってしまった。
今後の改良に期待したいところではあるがドキュメントに記載されている機能を一通り試してみた感じでは一部の機能が機能しなかったり仕様が違ったりなどいくつかあったのでまだまだ開発途上ということなのかもしれない。
それと公開されているサンプルプログラムが単純すぎてやりたいことができなかったため検証のために全機能を網羅したライブラリを作ってみたがファームウェアがUART経由で更新できる機能などは素敵かも。実際に動作するのかどうかはわからないが...
【サンプルスケッチ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include "mr24xxb1.h" MR24XXB1 sensor(Serial1); void setup() { Serial1.begin(9600); // MR24XXB1 Serial.begin(9600); // USB pinMode(LED_BUILTIN, OUTPUT); } void loop() { if (sensor.handle() == MR24XXB1::STATUS_OK) { String msg; Serial.println(sensor.decode(msg).c_str()); digitalWrite(LED_BUILTIN, !sensor.signs()); } } |
【ライブラリ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
/* mr24xxb1.h - Seeed 24GHz mmWave Radar Sensor Library for Arduino Copyright (c) 2022 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdint.h> #include <stdbool.h> #include <string.h> #define MR24XXB1_START_CODE 0x55 class MR24XXB1 { public: typedef enum { STATUS_NONE, STATUS_OK, STATUS_TIMEOUT, STATUS_LEN_ERROR, STATUS_CRC_ERROR, STATUS_UPGRADE_FAILURE, } STATUS; typedef enum { ENVIRONMENT_STATE_UNOCCUPIED = 0x00FFFF, ENVIRONMENT_STATE_STATIONARY = 0x0100FF, ENVIRONMENT_STATE_EXERCISE = 0x010101, } ENVIRONMENT_STATE; typedef enum { APPOACHING_AWAY_STATE_NONE = 0x010101, APPOACHING_AWAY_STATE_CLOSE = 0x010102, APPOACHING_AWAY_STATE_AWAY = 0x010103, } APPOACHING_AWAY_STATE; typedef enum { THRESHOLD_GEAR_1 = 0x01, THRESHOLD_GEAR_2 = 0x02, THRESHOLD_GEAR_3 = 0x03, THRESHOLD_GEAR_4 = 0x04, THRESHOLD_GEAR_5 = 0x05, THRESHOLD_GEAR_6 = 0x06, THRESHOLD_GEAR_7 = 0x07, THRESHOLD_GEAR_8 = 0x08, THRESHOLD_GEAR_9 = 0x09, THRESHOLD_GEAR_10 = 0x0A, THRESHOLD_GEAR_DEFAULT = THRESHOLD_GEAR_7, } THRESHOLD_GEAR; typedef enum { SCENE_SETTING_DEFAULT = 0x00, SCENE_SETTING_AREA_DETECTION = 0x01, // (top loading) SCENE_SETTING_BATHROOM = 0x02, // (top mounted) SCENE_SETTING_BEDROOM = 0x03, // (top loading) SCENE_SETTING_LIVINGROOM = 0x04, // (top mounted) SCENE_SETTING_OFFICE = 0x05, // (top loading) SCENE_SETTING_HOTEL = 0x06, // (top loading) } SCENE_SETTING; typedef enum { FALL_FUNCTION_SWITCH_OFF = 0x00, FALL_FUNCTION_SWITCH_ON = 0x01, } FALL_FUNCTION_SWITCH; typedef enum { FALL_ALARM_TIME_1MIN = 0x00, FALL_ALARM_TIME_2MIN = 0x01, FALL_ALARM_TIME_3MIN = 0x02, FALL_ALARM_TIME_4MIN = 0x03, FALL_ALARM_TIME_5MIN = 0x04, FALL_ALARM_TIME_6MIN = 0x05, FALL_ALARM_TIME_7MIN = 0x06, FALL_ALARM_TIME_10MIN = 0x07, FALL_ALARM_TIME_15MIN = 0x08, FALL_ALARM_TIME_30MIN = 0x09, } FALL_ALARM_TIME; typedef enum { FALL_SENSITIVITY_1 = 0x01, FALL_SENSITIVITY_2 = 0x02, FALL_SENSITIVITY_3 = 0x03, FALL_SENSITIVITY_4 = 0x04, FALL_SENSITIVITY_5 = 0x05, FALL_SENSITIVITY_6 = 0x06, FALL_SENSITIVITY_7 = 0x07, FALL_SENSITIVITY_8 = 0x08, FALL_SENSITIVITY_9 = 0x09, FALL_SENSITIVITY_10 = 0x0A, FALL_SENSITIVITY_DEFAULT = FALL_SENSITIVITY_4, } FALL_SENSITIVITY; MR24XXB1(HardwareSerial& serial) : _serial(serial) , _function(0) , _address(0) , _signs(0) , _envState(0) , _awayState(0) , _fall_alarm(0) , _dwell_alarm(0) { } virtual ~MR24XXB1(void) { } STATUS getDeviceId(String& result, uint32_t timeout = 1000000) { command(0x01, 0x0101); return stringValue(result, 0x03, 0x0101, timeout); } STATUS getSoftwareVersion(String& result, uint32_t timeout = 1000000) { command(0x01, 0x0102); return stringValue(result, 0x03, 0x0102, timeout); } STATUS getHardwareVersion(String& result, uint32_t timeout = 1000000) { command(0x01, 0x0103); return stringValue(result, 0x03, 0x0103, timeout); } STATUS getProtocolVersion(String& result, uint32_t timeout = 1000000) { command(0x01, 0x0104); STATUS rv = stringValue(result, 0x03, 0x0104, timeout); if ((rv == STATUS_OK) && (size() == 1) && (*data() < '0')) result = (size_t)*data(); return rv; } STATUS getEnvironmentalState(ENVIRONMENT_STATE& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x0305); STATUS rv = integerValue(value, 0x03, 0x0305, timeout); result = (ENVIRONMENT_STATE)value; return rv; } STATUS getSignsParameters(float& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x0306); STATUS rv = integerValue(value, 0x03, 0x0306, timeout); _signs = result = uint32tofloat(value); return rv; } STATUS getThresholdGear(THRESHOLD_GEAR& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x040C); STATUS rv = integerValue(value, 0x03, 0x040C, timeout); result = (THRESHOLD_GEAR)value; return rv; } bool setThresholdGear(THRESHOLD_GEAR value = THRESHOLD_GEAR_DEFAULT) { _packet.data[0] = value; return command(0x02, 0x040C, nullptr, sizeof(_packet.data[0])); } STATUS getSceneSetting(SCENE_SETTING& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x0410); STATUS rv = integerValue(value, 0x03, 0x0410, timeout); result = (SCENE_SETTING)value; return rv; } bool setSceneSetting(SCENE_SETTING value = SCENE_SETTING_DEFAULT) { _packet.data[0] = value; return command(0x02, 0x0410, nullptr, sizeof(_packet.data[0])); } bool reboot(void) { return command(0x02, 0x0504); } STATUS getFallFunctionSwitch(FALL_FUNCTION_SWITCH& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x050B); STATUS rv = integerValue(value, 0x03, 0x050B, timeout); result = (FALL_FUNCTION_SWITCH)value; return rv; } bool setFallFunctionSwitch(FALL_FUNCTION_SWITCH value) { _packet.data[0] = value; return command(0x02, 0x050B, nullptr, sizeof(_packet.data[0])); } STATUS getFallAlarmTime(FALL_ALARM_TIME& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x050C); STATUS rv = integerValue(value, 0x03, 0x050C, timeout); result = (FALL_ALARM_TIME)value; return rv; } bool setFallAlarmTime(FALL_ALARM_TIME value) { _packet.data[0] = value; return command(0x02, 0x050C, nullptr, sizeof(_packet.data[0])); } STATUS getFallSensitivity(FALL_SENSITIVITY& result, uint32_t timeout = 1000000) { uint32_t value; command(0x01, 0x050E); STATUS rv = integerValue(value, 0x03, 0x050E, timeout); result = (FALL_SENSITIVITY)value; return rv; } bool setFallSensitivity(FALL_SENSITIVITY value = FALL_SENSITIVITY_DEFAULT) { _packet.data[0] = value; return command(0x02, 0x050E, nullptr, sizeof(_packet.data[0])); } STATUS upgrade(uint32_t fwSize, uint8_t *fwImage, uint8_t *fwVer, uint8_t fwVerLen) { uint32_t result; STATUS rv = startUpgrade(result, fwSize, fwVer, fwVerLen); if (rv == STATUS_OK) { if (result == 0) rv = STATUS_UPGRADE_FAILURE; else { for (uint32_t off = 0; off < fwSize; off += sizeof(_packet.data) - sizeof(off)) { uint32_t len = min(fwSize - off, sizeof(_packet.data) - sizeof(off)); rv = transferUpgradeData(result, off, fwImage + off, len); if (rv != STATUS_OK) break; if (result != 0x0F) { rv = STATUS_UPGRADE_FAILURE; break; } } if (rv == STATUS_OK) rv = endUpgrade(); else endUpgrade(); } } return rv; } STATUS handle(uint32_t timeout = 3000) { uint32_t start, now; uint8_t *buf = (uint8_t *)&_packet; size_t len = offsetof(typeof(_packet), data); start = now = micros(); for (size_t i = 0; i < len; ) { int c = recv(); if (i == 0) { if (c != MR24XXB1_START_CODE) return STATUS_NONE; } if (c >= 0) { *buf++ = (uint8_t)c; if (++i == offsetof(typeof(_packet), func)) { len = read16(_packet.dlen) + offsetof(typeof(_packet), dlen); if (len >= sizeof(_packet)) return STATUS_LEN_ERROR; } start = now; } else if (now - start > timeout) return STATUS_TIMEOUT; yield(); now = micros(); } STATUS rv = crc16((uint8_t *)&_packet, len) ? STATUS_CRC_ERROR : STATUS_OK; if (rv == STATUS_OK) { _function = function(); _address = address(); switch (_function) { // // Proactive reporting // case 0x04: switch (_address) { // // Heartbeat Pack // case 0x0501: // // Environment status // case 0x0305: _envState = readState(data()); break; // // Body motion amplitude // case 0x0306: _signs = uint32tofloat(read32(data())); break; // // Approaching away state // case 0x0307: _awayState = readState(data()); break; } break; // // Fall radar data reporting // case 0x06: switch (_address) { // // Fall alarm // case 0x0101: _fall_alarm = *data(); break; // // Dwell alarm // case 0x0102: _dwell_alarm = *data(); break; } break; } } return rv; } String& decode(String& str) { str = ""; switch (_function) { // // Proactive reporting // case 0x04: switch (_address) { // // Heartbeat Pack // case 0x0501: // // Environment status // case 0x0305: switch (_envState) { case ENVIRONMENT_STATE_UNOCCUPIED: str += "nobody"; break; case ENVIRONMENT_STATE_STATIONARY: str += "somebody stop"; break; case ENVIRONMENT_STATE_EXERCISE: str += "somebody move"; break; } break; // // Approaching away state // case 0x0307: switch (_awayState) { case APPOACHING_AWAY_STATE_NONE: str += "no move"; break; case APPOACHING_AWAY_STATE_CLOSE: str += "somebody close"; break; case APPOACHING_AWAY_STATE_AWAY: str += "somebody away"; break; } break; // // Body motion amplitude // case 0x0306: str += _signs; break; } break; // // Fall radar data reporting // case 0x06: switch (_address) { // // Fall alarm // case 0x0101: switch (_fall_alarm) { case 0x00: str += "SUSPECTED FALL"; break; case 0x01: str += "REAL FALL"; break; case 0x02: str += "NO FALL"; break; } break; // // Dwell alarm // case 0x0102: switch (_dwell_alarm) { case 0x00: str += "NO WARNING"; break; case 0x01: str += "FIRST WARNING"; break; case 0x02: str += "SECOND WARNING"; break; case 0x03: str += "THIRD WARNING"; break; case 0x04: str += "FORTH WARNING"; break; } break; } } return str.length() ? str : dump(str); } String& dump(String& str) { static const char HEX_CHARS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; uint8_t *buf = (uint8_t *)&_packet; uint16_t len = read16(_packet.dlen) + offsetof(typeof(_packet), dlen); str = ""; while (len--) { uint8_t b = *buf++; str += HEX_CHARS[(b >> 4) & 0x0F]; str += HEX_CHARS[(b >> 0) & 0x0F]; } return str; } // // Body motion amplitude (0 - 100%) // // 0% None Environmental unmanned // 1% Stationary (sleep) Only breathing without body movement // 2% - 30% Micro-Movements Only minor head or limb movements Movement // 31% - 60% Walking/fast body movements Slower body movements // 61% - 100% Running/close range big moves Rapid body movement // float signs(void) { return _signs; } uint32_t envState(void) { return _envState; } uint32_t awayState(void) { return _awayState; } uint8_t fallAlarm(void) { return _fall_alarm; } uint8_t dwellAlarm(void) { return _dwell_alarm; } protected: HardwareSerial& _serial; uint8_t _function; uint16_t _address; float _signs; uint32_t _envState; uint32_t _awayState; uint8_t _fall_alarm; uint8_t _dwell_alarm; struct __attribute__ ((packed)) { uint8_t start; uint8_t dlen[2]; uint8_t func; uint8_t addr[2]; uint8_t data[4 + 1024]; uint8_t crc[2]; } _packet; uint16_t readAddress(const uint8_t *buf) { return ((uint16_t)buf[0] << 8) | ((uint8_t )buf[1] << 0); } void writeAddress(uint8_t *buf, uint16_t addr) { buf[0] = (uint8_t)(addr >> 8); buf[1] = (uint8_t)(addr >> 0); } uint32_t readState(const uint8_t *buf) { return ((uint32_t)buf[0] << 16) | ((uint16_t)buf[1] << 8) | ((uint8_t )buf[2] << 0); } void writeState(uint8_t *buf, uint32_t val) { buf[0] = (uint8_t)(val >> 16); buf[1] = (uint8_t)(val >> 8); buf[2] = (uint8_t)(val >> 0); } uint16_t read16(const uint8_t *buf) { return ((uint8_t )buf[0] << 0) | ((uint16_t)buf[1] << 8); } void write16(uint8_t *buf, uint16_t val) { buf[0] = (uint8_t)(val >> 0); buf[1] = (uint8_t)(val >> 8); } uint32_t read32(const uint8_t *buf) { return ((uint8_t )buf[0] << 0) | ((uint16_t)buf[1] << 8) | ((uint32_t)buf[2] << 16) | ((uint32_t)buf[3] << 24); } void write32(uint8_t *buf, uint32_t val) { buf[0] = (uint8_t)(val >> 0); buf[1] = (uint8_t)(val >> 8); buf[2] = (uint8_t)(val >> 16); buf[3] = (uint8_t)(val >> 24); } float uint32tofloat(uint32_t value) { union { uint32_t i; float f; } rv; rv.i = value; return rv.f; } static uint16_t crc16(const uint8_t *buf, size_t len) { static const uint8_t HI[256] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; static const uint8_t LO[256] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 }; uint8_t h = 0xFF; uint8_t l = 0xFF; uint8_t i = 0; while (len--) { i = l ^ *buf++; l = (uint8_t)(h ^ HI[i]); h = LO[i]; } return (uint16_t)(((uint16_t)h << 8) | l); } STATUS startUpgrade(uint32_t&result, uint32_t fwSize, uint8_t *fwVer, uint8_t fwVerLen, uint32_t timeout = 1000000) { size_t len = sizeof(fwSize) + fwVerLen; write32(_packet.data, fwSize); memcpy(_packet.data + sizeof(fwSize), fwVer, len - sizeof(fwSize)); command(0x02, 0x0508, nullptr, len); return integerValue(result, 0x03, 0x0508, timeout); } STATUS transferUpgradeData(uint32_t&result, uint32_t off, void *buf, uint32_t len, uint32_t timeout = 3000000) { write32(_packet.data, off); memcpy(_packet.data + sizeof(off), buf, len); command(0x02, 0x0509, nullptr, sizeof(off) + len); return integerValue(result, 0x03, 0x0509, timeout); } STATUS endUpgrade(uint32_t timeout = 1000000) { _packet.data[0] = 0x0F; command(0x02, 0x050A, nullptr, sizeof(_packet.data[0])); return response(0x04, 0x0102, timeout); } bool command(uint8_t func, uint16_t addr, const uint8_t *buf = nullptr, size_t len = 0) { if (buf && len) memcpy(_packet.data, buf, len); len += offsetof(typeof(_packet), data) + sizeof(_packet.crc); _packet.start = MR24XXB1_START_CODE; write16(_packet.dlen, len - offsetof(typeof(_packet), dlen)); _packet.func = func; writeAddress(_packet.addr, addr); write16(((uint8_t *)&_packet) + len - sizeof(_packet.crc), crc16((uint8_t *)&_packet, len - sizeof(_packet.crc))); return send((uint8_t *)&_packet, len) == len; } STATUS response(uint8_t func, uint16_t addr, uint32_t timeout) { uint32_t t = micros(); do { if (handle() == STATUS_OK) { if ((function() == func) && (address() == addr)) return STATUS_OK; } yield(); } while (micros() - t < timeout); return STATUS_TIMEOUT; } STATUS stringValue(String& result, uint8_t func, uint16_t addr, uint32_t timeout = 1000000) { result = ""; STATUS rv = response(func, addr, timeout); if (rv == STATUS_OK) { char buf[16]; size_t len = min(size(), sizeof(buf) - 1); memcpy(buf, data(), len); buf[len] = 0; result = buf; } return rv; } STATUS integerValue(uint32_t& result, uint8_t func, uint16_t addr, uint32_t timeout = 1000000) { result = 0; STATUS rv = response(func, addr, timeout); if (rv == STATUS_OK) { switch (size()) { case 1: result = *data(); break; case 2: result = read16(data()); break; case 3: result = readState(data()); break; case 4: result = read32(data()); break; } } return rv; } uint8_t function(void) { return _packet.func; } uint16_t address(void) { return readAddress(_packet.addr); } const uint8_t *data(void) { return _packet.data; } uint16_t size(void) { return read16(_packet.dlen) - (offsetof(typeof(_packet), data) - offsetof(typeof(_packet), dlen) + sizeof(_packet.crc)); } int recv(void) { return _serial.read(); } size_t send(uint8_t *buf, size_t len) { return _serial.write(buf, len); } uint32_t micros(void) { return ::micros(); } void yield(void) { ::yield(); } }; |