smart_shell_esp32.ino 24.6 KB
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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
/*

     jason example      60                    // calibraton order
                        {"ADDR":"1","led":[5],"RGB":[100,100,100]}
                        {"scan":"true","led":[5],"RGB":[100,100,100]}
                        {"ADDR":"1","led":[5,10,11],"RGB":[128,200,50]}
                        {"ADDR":"1","led":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],"RGB":[100,255,50]}
                        {"scan":"true","ADDR":"1","led":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],"RGB":[0,0,0]}
                        {"ADDR":"99","setcalibrate":[1,100,600,150],"threshold":65}
                        {"ADDR":"99","setcalibrate":[1,100,600,150],"threshold":45}
*/
#define ver 2.13

#include "config.h"
#include <ArduinoJson.h>
#include <ArduinoUniqueID.h>
#include "matrixSelector.h"
#include <Streaming.h>
#define RXD2 26
#define TXD2 21
#include <HardwareSerial.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>

#define PIN 36
#define NUMPIXELS 100
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
//#define DELAYVAL 5

#define maincontrol Serial1
// For led chips like WS2812, which have a data line, ground, and power
// Define the array of leds
// CRGB leds[NUM_LEDS];
String DeviceUniqueID = "";
String ShelfPartName = "";
int ADDR = 1;     //此处修改地址
bool status[101]; // {1,1,1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,1,0,0,0,0,1,1,0,1,0,1,0,0,1,0,1,0,0}; // status of 100 sensores
String inputString = "";
String inputString2 = "";
String commandString = "";
bool diagnosMode = false;
char json[320];
matrixSelector senScan(30);
short RowWidth = 0;

#ifdef _16MM
int ss[] = {0, 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};
#endif
#ifdef _25MM
int ss[] = {0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25, 26, 27, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 47, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66, 67, 71, 72, 73, 74, 75, 76, 81, 82, 83, 84, 85, 86, 87, 91, 92, 93, 94, 95, 96};
// 7,7,7,6,7,6,7,6,7,6
#endif
#ifdef _54MM
int ss[] = {0, 1, 2, 3, 4, 5, 21, 22, 23, 24, 25, 26, 27, 41, 42, 43, 44, 45, 46, 47, 61, 62, 63, 64, 65, 66, 67, 81, 82, 83, 84, 85, 86, 87};
// 5,7,7,7,7
#endif

const int LedType_16MM = 1;
int LedNum_16MM_1[] = {10, 10};
int LedNum_16MM_2[] = {10, 10};

const int LedType_25MM = 2;
int LedNum_25MM_1[] = {7, 7};
int LedNum_25MM_2[] = {7, 6};

const int LedType_35MM = 3;
int LedNum_35MM_1[] = {10, 0};
int LedNum_35MM_2[] = {10, 0};

const int LedType_45MM = 4;
int LedNum_45MM_1[] = {8, 0};
int LedNum_45MM_2[] = {8, 0};

const int LedType_54MM = 5;
int LedNum_54MM_1[] = {5, 0};
int LedNum_54MM_2[] = {7, 0};

long startms;
int sscount = 1;
void setup()
{
    for (int i = 0; i < 101; i++)
        ss[i] = 0;

    maincontrol.begin(115200, SERIAL_8N1, RXD2, TXD2);
    // Initialize serial port
    Serial.begin(115200);
    Serial.println("start up");

    pixels.begin();
    for (size_t i = 0; i < UniqueIDsize; i++)
        DeviceUniqueID += String(UniqueID[i], HEX);
    senScan.initial();
    ShelfPartName = EEPROM.readString(ShelfPartAddress);
    RowWidth = senScan.readShortFromEEPROM(RowWidthAddress);
    Serial.println("DeviceUniqueID=" + DeviceUniqueID);
    Serial.println("ShelfPartName=" + ShelfPartName);
    Serial.println("RowWidth=" + RowWidth);

    int addrt = senScan.readIntFromEEPROM(ADDREPAddress);
    if (addrt > 0 && addrt < 50)
        ADDR = addrt;
    else
    {
        for (int j = 1; j <= 100; j++) // 没有设置过站号, 所有灯全亮
            LED(j, 30, 0, 0);
        pixels.show();
        return;
    }
    Serial.println("_ADDR=" + String(ADDR));
    SetupLed(&Serial);
    // for (int ii = 1; ii <= sscount; ii++){
    //     LED(ii, 10, 10, 10);
    // }
    short cp = CheckList(&Serial);
    short fm = senScan.readShortFromEEPROM(FactoryModeAddress);
    // Serial.println(fm);
    int lt = LedBoardTest(&Serial);
    //验证感应器数量
    if (lt > 4)
    {
        Serial.println("Led Board Test Fail.");
        LED(1, 30, 0, 0);
        // startms = 0;
    }
    else if (fm == 1)
    {
        Serial.println("factoryMode");

        LED(1, 0, 0, 30);
    }
    else
    {
        //验证完成度
        if (cp == sscount)
        {
            for (int j = 1; j <= 3; j++) // 100%完成的亮3个灯
                LED(j, 0, 30, 0);
            senScan.writeShortIntoEEPROM(BoardIsOK, 1);
        }
        else
        {
            for (int j = 1; j <= 3; j++) //没有完成但是关闭了出厂模式的的亮3个灯
                LED(j, 30, 0, 0);
        }
    }
    pixels.show();
    startms = millis();
}
short CheckList(HardwareSerial *sl)
{
    short cp = 0;
    sl->print("Check list:");
    for (int ii = 1; ii <= sscount; ii++)
    {
        short s = senScan.readShortFromEEPROM(SensorCheckAddress + ii);
        if (s > 1)
            s = 0;
        cp = cp + s;
        sl->print(s);
    }
    sl->println(",present=" + String(cp * 100 / sscount) + "%");
    return cp;
}
void SetupLed(HardwareSerial *sl)
{
    sscount = 1;
    int ledtype1 = ReadLedType(1);
    int ledtype2 = ReadLedType(2);
    int ledtype3 = ReadLedType(3);
    int ledtype4 = ReadLedType(4);
    int ledtype5 = ReadLedType(5);

    int lednums[2];
    sl->print("LedType=");
    GetLedType(ledtype1, true, lednums, sl);
    SetSS(lednums[0], lednums[1], 1);

    GetLedType(ledtype2, false, lednums, sl);
    SetSS(lednums[0], lednums[1], 2);

    GetLedType(ledtype3, false, lednums, sl);
    SetSS(lednums[0], lednums[1], 3);

    GetLedType(ledtype4, false, lednums, sl);
    SetSS(lednums[0], lednums[1], 4);

    GetLedType(ledtype5, false, lednums, sl);
    SetSS(lednums[0], lednums[1], 5);
    sscount--;
    sl->print("//");
    sl->print("Led Count=" + String(sscount) + "//");
    senScan.ledcount = sscount;
    sl->print("Led List=");
    for (int i = 1; i <= sscount; i++)
    {
        sl->print(String(ss[i]) + ",");
    }
    sl->print("//");
    sl->print("RowWidth=");
    sl->print(RowWidth);
    sl->print("//");
}
int ReadLedType(int address)
{
    address--;
    int t = senScan.readIntFromEEPROM(LedTypeEPAddress + address * 2);
    if (t >= 0 && t < 10)
    {
        Serial.print("ReadLedType " + String(address) + "=" + String(t) + "//");
        return t;
    }
    Serial.print("ReadLedType " + String(address) + "=fail" + "//");
    return 1;
}
void WriteLedType(int address, int ledtype)
{
    address--;
    senScan.writeIntIntoEEPROM(LedTypeEPAddress + address * 2, ledtype, true);
    Serial.print("LedType Write " + String(address) + "=" + String(ledtype) + "//");
}
void GetLedType(int ledtype, bool isfirst, int num[], HardwareSerial *sl)
{
    switch (ledtype)
    {
    case LedType_16MM:
        num[0] = isfirst ? LedNum_16MM_1[0] : LedNum_16MM_2[0];
        num[1] = isfirst ? LedNum_16MM_1[1] : LedNum_16MM_2[1];
        break;
    case LedType_25MM:
        num[0] = isfirst ? LedNum_25MM_1[0] : LedNum_25MM_2[0];
        num[1] = isfirst ? LedNum_25MM_1[1] : LedNum_25MM_2[1];
        break;
    case LedType_35MM:
        num[0] = isfirst ? LedNum_35MM_1[0] : LedNum_35MM_2[0];
        num[1] = isfirst ? LedNum_35MM_1[1] : LedNum_35MM_2[1];
        break;
    case LedType_45MM:
        num[0] = isfirst ? LedNum_45MM_1[0] : LedNum_45MM_2[0];
        num[1] = isfirst ? LedNum_45MM_1[1] : LedNum_45MM_2[1];
        break;
    case LedType_54MM:
        num[0] = isfirst ? LedNum_54MM_1[0] : LedNum_54MM_2[0];
        num[1] = isfirst ? LedNum_54MM_1[1] : LedNum_54MM_2[1];
        break;
    case 0:
        num[0] = isfirst ? LedNum_16MM_1[0] : 0;
        num[1] = isfirst ? LedNum_16MM_1[1] : 0;
        break;
    }
    sl->print(String(num[0]) + "," + String(num[1]) + ",");
}
void SetSS(int n1, int n2, int index)
{
    index--;
    int start = index * 20;
    for (int i = 1; i <= n1; i++)
    {
        ss[sscount] = start + i;
        sscount++;
    }
    start = index * 20 + 10;
    for (int i = 1; i <= n2; i++)
    {
        ss[sscount] = start + i;
        sscount++;
    }
    // Serial.print(String(sscount)+",");
}
long dur;

void loop()
{
    static int i = 1;
    dur = millis();
    if (Serial.available() > 0)
    {
        char inChar = (char)Serial.read();
        inputString2 += inChar;
        if (inChar == '\n')
        {
            inputString2.trim();
            commandString = inputString2;
            inputString2 = "";
            CommandProcess(&Serial);
        }
    }

    if (startms != 0 && millis() - startms > 1000 * 12)
    {
        startms = 0;
        short fm = senScan.readShortFromEEPROM(FactoryModeAddress);
        if (fm == 1)
        {
            Serial.println("factoryMode calibration start");
            // delay(3000);
            for (int j = 1; j <= 100; j++)
                LED(j, 0, 0, 30);

            pixels.show();
            delay(10);
            calibration(1); // mode 1 is max
            delay(500);
            calibration(-1); // mode -1 is min

            for (int j = 1; j <= 100; j++)
                LED(j, 0, 0, 0);
            pixels.show();
            delay(500);
            senScan.factoryMode = true;
            return;
        }
    }

    if (diagnosMode || senScan.factoryMode)
    {
        bool closefactory = true;
        for (int ii = 1; ii <= sscount; ii++)
        {
            short s = senScan.readShortFromEEPROM(SensorCheckAddress + ii);
            if (status[ii])
            {
                // Serial << "NO." << ii << ' ' << ss[ii] << " ";
                LED(ii, 0, 50, 0);
                if (senScan.factoryMode)
                {
                    if (s != 1)
                    {
                        senScan.writeShortIntoEEPROM(SensorCheckAddress + ii, 1);
                    }
                }
            }
            else
            {
                if (senScan.factoryMode)
                {
                    LED(ii, 10, 10, 10);
                    if (s != 1)
                        closefactory = false;
                    else
                        LED(ii, 0, 0, 30);
                }
            }
        }
        pixels.show();
        if (senScan.factoryMode && closefactory)
        {
            pixels.show();
            senScan.writeShortIntoEEPROM(FactoryModeAddress, 0);
            Serial.println("factoryMode OFF");
            sleep(10);
            senScan.factoryMode = false;
            ESP.restart();
        }
    }

    status[i] = senScan.getstatus(ss[i]);
    if (i >= sscount)
        i = 1;
    else
        i++;
    /*
      for (int j = 1; j <= sscount ; j++) {
        LED(j,0, 0, 0);
      }
      LED(i,30, 0, 0);
    */
    // pixels.show();

    serialEvent();
}

void serialEvent()
{
    // Serial.println(millis());

    //
    while (maincontrol.available() > 0)
    {
        // get the new byte:
        char inChar = (char)maincontrol.read();
        // Serial.println("{" + String(inChar) + "}");
        // maincontrol.println("{" + String(inChar) + "}");
        inputString += inChar;
        if (inChar == '\n')
        {
            inputString.trim();
            commandString = inputString;
            inputString = "";
            CommandProcess(&maincontrol);
            Serial << " time to prosess " << (millis() - dur) << endl;
            break;
        }
    }
}
void CommandProcess(HardwareSerial *sl)
{
    int addr = ADDR;
    String str = "A" + String(addr);       // + "\n" ;
    String str1 = "B" + String(addr - 10); // + "\n" ;
    String str2 = "H" + String(addr);      // + "\n" ;
    // Serial.println(commandString.length());
    // Serial.println(commandString);
    //  if (inputString.indexOf(str) >= 0 ) {
    if (commandString == str)
    {
        Serial.println("quick scan req");
        scan(sl);
    }
    else if (commandString == str1)
    {
        Serial.println("quick scan req");
        scan(sl);
    }
    else if (commandString == str2)
    {
        Serial.println("quick hexscan req");
        hexgen();
    }
    else if (commandString == "diagnosMode")
    {
        diagnosMode = true;
        senScan.diagnosMode = true;
        sl->println("diagnosMode on");
    }
    else if (commandString == "INFO")
    {
        info(sl);
    }
    else if (commandString.startsWith("ADDR="))
    {
        int addrt = commandString.substring(5).toInt();
        if (addrt > 0 && addrt < 50)
        {
            senScan.writeIntIntoEEPROM(ADDREPAddress, addrt);
            ADDR = addrt;
            sl->println("Set ADDR=" + String(addrt) + " OK");
        }
        else
            sl->println("Set ADDR=" + String(addrt) + " fail.");
    }
    else if (commandString.startsWith("{"))
    {
        commandString.toCharArray(json, 320);
        jsonBreak(sl);
    }
    commandString = "";
}
void info(HardwareSerial *sl)
{
    sl->print("Version=" + String(ver) + "//");
    sl->print("DeviceUniqueID=" + DeviceUniqueID + "//");
    sl->print("ADDR=" + String(ADDR) + "//");
    sl->print("ShelfPartName=" + ShelfPartName + "//");
    sl->print("BoardIsOK=" + String(senScan.readShortFromEEPROM(BoardIsOK)) + "//");
    SetupLed(sl);
    sl->println();
}
void jsonBreak(HardwareSerial *sl)
{
    StaticJsonDocument<1000> doc;
    // Allocate the JSON document
    //
    // Inside the brackets, 200 is the capacity of the memory pool in bytes.
    // Don't forget to change this value to match your JSON document.
    // Use arduinojson.org/v6/assistant to compute the capacity.
    // StaticJsonDocument<200> doc;
    // Serial.print(F("Braking >> "));
    // Serial.println(json);

    DeserializationError error = deserializeJson(doc, json);

    // Test if parsing succeeds.
    if (error)
    {
        sl->print(F("sensor board deserializeJson() failed: "));
        sl->println(error.f_str());
        return;
    }
    int addr = doc["ADDR"];
    // Serial.println(millis());
    if (ADDR == addr || addr == 99)
    {
        // Serial.println("true addr flag");
        //  Fetch values.
        //
        //  Most of the time, you can rely on the implicit casts.
        //  In other case, you can do doc["time"].as<long>();
        String scanreq = doc["scan"];
        if (scanreq == "all" || scanreq == "ALL")
        {
            sl->print("A");
            sl->print(ADDR);
            scan(sl);
            return;
        }
        else if (ADDR == addr)
            maincontrol << "ADDR:" << ADDR << " done" << endl;

        String calibrate = doc["calibrate"];
        String action = doc["action"];
        // int red = doc["RGB"][0];
        // int green = doc["RGB"][1];
        // int blue = doc["RGB"][2];

        if (doc["color"][0][0] > 0)
        {
            for (int i = 0; i < 100; i++)
            {
                int indexofcolor = doc["color"][i][0];
                if (indexofcolor == 0)
                    break;

                for (int j = 1; j < 100; j++)
                {
                    int indexofled = doc["color"][i][j];
                    if (indexofled == 0)
                        break;

                    colorget(indexofcolor, indexofled);
                }
            }
            pixels.show();
            return;
        }
        // Serial.println(test);
        /*
            if ( doc["led"][0]>0)
            for (int i = 0; i <= 100; i++)
            {
              int a = doc["led"][i];
              if (a > 0)
                LED(a, red, green, blue);
              // colorget(doc["led"][0],a);
            }
        */
        if (doc["ledrange"][0] > 0)
        {
            for (int i = doc["ledrange"][1]; i <= doc["ledrange"][2]; i++)
            {
                int a = i;
                if (a > 0)
                    // LED (a , red , green , blue );
                    colorget(doc["ledrange"][0], a);
            }
            pixels.show();
            return;
        }
        if (doc["colorset"][0][0] > 0)
        {
            for (int i = 0; i < 100; i++)
            {
                int indexofcolor = doc["colorset"][i][0];
                if (indexofcolor == 0)
                    break;
                int redcolor = doc["colorset"][i][1];
                int greencolor = doc["colorset"][i][2];
                int bluecolor = doc["colorset"][i][3];
                colorset(indexofcolor, redcolor, greencolor, bluecolor);
            }
            EEPROM.commit();
            return;
        }
        if (scanreq == "true")
        {
            scan(sl);
            return;
        }
        if (calibrate == "autotrain")
        {
            for (int j = 0; j < 100; j++)
                pixels.setPixelColor(j, pixels.Color(0, 0, 30));

            pixels.show();
            delay(10);
            calibration(1); // mode 1 is max
            delay(500);
            calibration(-1); // mode -1 is min

            for (int j = 0; j < 100; j++)
                pixels.setPixelColor(j, pixels.Color(0, 0, 0));
            pixels.show();
            return;
        }
        if (calibrate == "max")
        {
            calibration(1); // mode 1 is max
            return;
        }
        if (calibrate == "min")
        {
            calibration(-1); // mode -1 is min
            return;
        }
        if (doc["setcalibrate"][0] > 0)
        {
            senScan.setcalnum(sl, doc["setcalibrate"][0], ss[(int)doc["setcalibrate"][1]], ss[(int)doc["setcalibrate"][2]], doc["setcalibrate"][3], doc["setcalibrate"][4]); // start pin , end pin , max , min ,threshold
            return;
        }
        if (doc["getcalibrate"][0] > 0)
        {
            senScan.getcalnum(sl, doc["getcalibrate"][0], doc["getcalibrate"][1], doc["getcalibrate"][2], doc["getcalibrate"][3], doc["getcalibrate"][4]); // start pin , end pin , max , min ,threshold
            sl->println();
            return;
        }

        if (action == "siteid")
        {
            String hwid = doc["hwid"];
            if (hwid.equals(DeviceUniqueID))
            {
                int addrt = doc["addr"];
                senScan.writeIntIntoEEPROM(ADDREPAddress, addrt);
                ADDR = addrt;
                sl->println("Set ADDR=" + String(addrt) + " OK");
            }
            return;
        }
        if (action == "info")
        {
            info(sl);
            return;
        }
        if (action == "diagnosMode")
        {
            diagnosMode = true;
            senScan.diagnosMode = true;
            sl->println("diagnosMode on addr=" + String(ADDR));
            return;
        }
        if (doc["ledtype"][0] > 0)
        {
            for (int i = 0; i < 5; i++)
            {
                WriteLedType(i + 1, doc["ledtype"][i]);
            }
            RowWidth = doc["ledtype"][5];
            senScan.writeShortIntoEEPROM(RowWidthAddress, RowWidth);
            senScan.EEPROMcommit();
            SetupLed(sl);
            sl->println();
            return;
        }
        if (action == "report")
        {
            delay((ADDR-1)*30+random(1, 30));
            sl->print("_ADDR=" + String(ADDR) + "//");
            sl->print("Version=" + String(ver) + "//");
            sl->println("DeviceUniqueID=" + DeviceUniqueID + "//");
            return;
        }
        if (action == "factoryModeOFFFF")
        {
            senScan.writeShortIntoEEPROM(FactoryModeAddress, 0);
            sl->println("set factoryMode OFF");
            senScan.factoryMode = false;
            return;
        }
        if (action == "factoryModeON")
        {
            senScan.writeShortIntoEEPROM(FactoryModeAddress, 1,true);
            for (int i = 1; i <= 100; i++)
            {
                senScan.calibrate(i, 1);
            }
            senScan.EEPROMcommit();
            sl->println("set factoryMode ON");
            senScan.factoryMode = true;
            delay(1000);
            ESP.restart();
            return;
        }
        if (action == "CheckList")
        {
            CheckList(sl);
            return;
        }
        if (action == "ClearCheckList")
        {
            for (int i = 1; i <= 100; i++)
            {
                senScan.writeShortIntoEEPROM(SensorCheckAddress + i, 0, true);
            }
            senScan.writeShortIntoEEPROM(BoardIsOK, 0, true);
            senScan.EEPROMcommit();
            *sl << "ClearCheckList OK\r\n";
            return;
        }
        if (action == "PrintStatus")
        {
            for (int i = 1; i <= sscount; i++)
            {
                int pin = ss[i];
                float max = senScan.getmax(pin);
                float min = senScan.getmin(pin);
                float sample = senScan.getadc(pin, true);

                float result = ((max - sample) / (max - min)) * 100;
                *sl << "\r\npin:" << pin << " max:" << max << " min:" << min << " sample:" << sample << " sen%:" << result;
            }
            return;
        }
        if (action == "LedBoardTest")
        {
            LedBoardTest(sl);
            return;
        }
        if (action == "ShelfPart")
        {
            String partname = doc["name"];
            EEPROM.writeString(ShelfPartAddress, partname);
            EEPROM.commit();
            delay(100);
            *sl << "Set ShelfPartName OK";
            return;
        }
    }
}

int LedBoardTest(HardwareSerial *sl)
{
    int testfailcount = 0;
    int ssc = 1;
    for (int i = 1; i <= 100; i++)
    {
        long a = senScan.getadc(i, true);
        bool hasled = false;
        if (ss[ssc] == i)
        {
            hasled = true;
            ssc++;
        }
        if (hasled)
        {
            if (a <= 300)
                testfailcount = testfailcount + 10;
        }
        else
        {
            if (a > 1000)
                testfailcount++;
        }
        sl->print(i);
        if (hasled)
            sl->print("*");
        else
            sl->print("-");
        if (a <= 300)
            sl->print("NG");
        else
            sl->print(a);
        sl->print(",");
    }

    sl->println("testfailcount:" + String(testfailcount));
    return testfailcount;
}

void LED(int led_num, int Red, int green, int blue)
{
    // maincontrol.print("flag");
    // leds[led_num - 1 ] = CRGB(Red, green, blue);
    pixels.setPixelColor(led_num - 1, pixels.Color(green, Red, blue));
    //
    // Serial <<"LED NO."<< led_num << 'R' << Red << 'G' << green << 'B' << blue << endl ;
    // delay(1);
}

void scan(HardwareSerial *sl)
{
    sl->print('[');
    // Serial.print('[');
    for (int i = 1; i <= 100; i++)
    {
        // status[i] = senScan.getstatus(i);
        sl->print(status[i]);
        // maincontrol.print("asd");
        // Serial.print(status[i]);
    }
    sl->println(']');
    // Serial.println(']');
}

void calibration(int mode)
{
    // Serial.println("calibrating start");
    for (int i = 1; i <= 100; i++)
    {
        senScan.calibrate(i, mode);
        delay(10);
        Serial.print("^ calibrating of : ");
        Serial.println(i);
    }
    Serial.println("end of calibration");
    senScan.EEPROMcommit();
}

void colorset(int index, int R, int G, int B)
{
    // const int memBasecoloradr          = 800;
    //  EEPROM.setMemPool(memBasecoloradr, EEPROMSizeMega);
    int coloradr = ColorsetAddress + (index * 4);
    int redadr = coloradr + 1;
    int greenadr = coloradr + 2;
    int blueadr = coloradr + 3;
    //  while (!EEPROM.isReady()) {}
    EEPROM.write(redadr, R);
    // while (!EEPROM.isReady()) {}
    EEPROM.write(greenadr, G);
    /// while (!EEPROM.isReady()) {}
    EEPROM.write(blueadr, B);
    Serial << "set index : " << index << "red : " << R << " green : " << G << " blue : " << B << endl;
    // EEPROM.commit();
}

void colorget(int indexofcolor, int indexofLED)
{
    // const int memBase          = 800;
    //  EEPROM.setMemPool(memBase, EEPROMSizeMega);
    // long addressByte = 1000;
    // Serial.println(addressByte);
    int coloradr = ColorsetAddress + (indexofcolor * 4);
    int redadr = coloradr + 1;
    int greenadr = coloradr + 2;
    int blueadr = coloradr + 3;
    // while (!EEPROM.isReady()) {}
    int R = EEPROM.read(redadr);
    // while (!EEPROM.isReady()) {}
    int G = EEPROM.read(greenadr);
    // while (!EEPROM.isReady()) {}
    int B = EEPROM.read(blueadr);
    // Serial << "indexofLED : " << indexofLED << " red : " << R << " green : " << G << " blue : " << B << endl;
    LED(indexofLED, R, G, B);
}

void hexgen()
{
    // unsigned long time = millis();
    maincontrol.print("A");
    maincontrol.print(ADDR);
    maincontrol.print("[");
    for (int i = 1; i <= 100; i += 4)
    {
        int sum = status[i] + (status[i + 1] * 2) + (status[i + 2] * 4) + (status[i + 3] * 8);

        maincontrol.print(sum, HEX);
        // Serial.println(sum,HEX);
    }
    maincontrol.println("]");

    // Serial.println(millis()-time);
}