From a4e71e7481a16a3486d44d1f2dd4f6dc41bc05a4 Mon Sep 17 00:00:00 2001 From: Tinyu Date: Sun, 24 Oct 2021 10:48:19 +0800 Subject: [PATCH] fix some error --- .../Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino | 155 ++--- examples/Unit/FINGER_FPC1020A/finger.cpp | 234 +++++++ examples/Unit/FINGER_FPC1020A/finger.h | 77 +++ .../Unit/Zigbee_CC2630/P2P_TEST/P2P_TEST.ino | 642 +++++++++--------- 4 files changed, 700 insertions(+), 408 deletions(-) create mode 100644 examples/Unit/FINGER_FPC1020A/finger.cpp create mode 100644 examples/Unit/FINGER_FPC1020A/finger.h diff --git a/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino b/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino index 3c1c416..623e72e 100644 --- a/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino +++ b/examples/Unit/FINGER_FPC1020A/FINGER_FPC1020A.ino @@ -1,99 +1,92 @@ -/* -******************************************************************************* -* Copyright (c) 2021 by M5Stack -* Equipped with M5Core sample source code -* 配套 M5Core 示例源代码 -* Visit the website for more information:https://docs.m5stack.com/en/unit/finger -* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/finger -* -* describe: Finger. -* date:2021/8/26 -******************************************************************************* -*/ #include -#include "M5_FPC1020A.h" +#include "finger.h" uint8_t userNum; //User number FingerPrint FP_M; void CleanScreen() { - M5.Lcd.fillRect(0,50,400,300,BLACK); - M5.Lcd.setCursor(0, 50); - M5.Lcd.setTextSize(2); - userNum = FP_M.fpm_getUserNum(); //Get users Numbers. 获取用户数量 - M5.Lcd.print("userNum:"); - M5.Lcd.println(userNum); + M5.Lcd.setTextColor(WHITE); + M5.Lcd.fillRect(0,50,400,300,BLACK); + M5.Lcd.setCursor(0, 50); + M5.Lcd.setTextSize(2); + userNum = FP_M.fpm_getUserNum(); + M5.Lcd.print("userNum:"); + M5.Lcd.println(userNum); } void setup() { - M5.begin(); - M5.Power.begin(); - Serial.begin(115200); - Serial2.begin(19200, SERIAL_8N1, 16, 17); - M5.Lcd.clear(BLACK); - M5.Lcd.setTextColor(YELLOW); - M5.Lcd.setTextSize(3); - M5.Lcd.setCursor(20, 0); - M5.Lcd.println("Finger example"); - M5.Lcd.setTextColor(WHITE); - M5.Lcd.fillRect(0,50,400,300,BLACK); - M5.Lcd.setCursor(0, 50); - M5.Lcd.setTextSize(2); - userNum = FP_M.fpm_getUserNum(); - M5.Lcd.print("userNum:"); - M5.Lcd.println(userNum); + M5.begin(); + M5.Power.begin(); + Serial.begin(115200); + Serial2.begin(19200, SERIAL_8N1, 16, 17); + M5.Lcd.clear(BLACK); + M5.Lcd.setTextColor(YELLOW); + M5.Lcd.setTextFont(2); + M5.Lcd.setTextSize(3); + M5.Lcd.setCursor(20, 0); + M5.Lcd.println("Finger example"); + Serial.printf("Finger example\n"); + M5.Lcd.setTextColor(WHITE); + M5.Lcd.fillRect(0,50,400,300,BLACK); + M5.Lcd.setCursor(0, 50); + M5.Lcd.setTextSize(2); + userNum = FP_M.fpm_getUserNum(); + M5.Lcd.print("userNum:"); + M5.Lcd.println(userNum); } +//ButtonA: Add user +//ButtonB: Matching +//ButtonC: Delete All User void loop(){ - M5.update(); - uint8_t res1; - //ButtonA: Add user. 添加用户 - if(M5.BtnA.wasPressed()){ - CleanScreen(); - M5.Lcd.println("Fingerprint Typing"); + uint8_t res1; + if(M5.BtnA.wasPressed()){ + CleanScreen(); + M5.Lcd.println("Fingerprint Typing"); + + res1 = FP_M.fpm_addUser(userNum,1); + if(res1 == ACK_SUCCESS){ + M5.Lcd.println("Success"); + } + else if(res1 == ACK_FAIL){ + M5.Lcd.println("Fail"); + } + else if(res1 == ACK_FULL){ + M5.Lcd.println("Full"); + } + else{ + M5.Lcd.println("Timeout"); + } + userNum++; + } - res1 = FP_M.fpm_addUser(userNum,1); - if(res1 == ACK_SUCCESS){ - M5.Lcd.println("Success"); + if(M5.BtnB.wasPressed()){ + CleanScreen(); + M5.Lcd.println("Matching"); + + res1 = FP_M.fpm_compareFinger(); + if(res1 == ACK_SUCCESS){ + M5.Lcd.println("Success"); + } + if(res1 == ACK_NOUSER){ + M5.Lcd.println("No Such User"); + } + if(res1 == ACK_TIMEOUT){ + M5.Lcd.println("Timeout"); + } } - else if(res1 == ACK_FAIL){ - M5.Lcd.println("Fail"); - } - else if(res1 == ACK_FULL){ - M5.Lcd.println("Full"); - } - else{ - M5.Lcd.println("Timeout"); - } - userNum++; - } -//ButtonB: Matching. 匹配指纹 - if(M5.BtnB.wasPressed()){ - CleanScreen(); - M5.Lcd.println("Matching"); - res1 = FP_M.fpm_compareFinger(); - if(res1 == ACK_SUCCESS){ - M5.Lcd.println("Success"); + if(M5.BtnC.wasPressed()){ + res1 = FP_M.fpm_deleteAllUser(); + CleanScreen(); + + if(res1 == ACK_SUCCESS){ + M5.Lcd.println("Delete All User Successful"); + } + else{ + M5.Lcd.println("Delete All User Failed"); + } } - if(res1 == ACK_NOUSER){ - M5.Lcd.println("No Such User"); - } - if(res1 == ACK_TIMEOUT){ - M5.Lcd.println("Timeout"); - } - } -//ButtonC: Delete All User. 删除所有用户 - if(M5.BtnC.wasPressed()){ - res1 = FP_M.fpm_deleteAllUser(); - CleanScreen(); - - if(res1 == ACK_SUCCESS){ - M5.Lcd.println("Delete All User Successful"); - } - else{ - M5.Lcd.println("Delete All User Failed"); - } - } + M5.update(); } diff --git a/examples/Unit/FINGER_FPC1020A/finger.cpp b/examples/Unit/FINGER_FPC1020A/finger.cpp new file mode 100644 index 0000000..95ddce4 --- /dev/null +++ b/examples/Unit/FINGER_FPC1020A/finger.cpp @@ -0,0 +1,234 @@ +/* + Description: FINGER UNIT use case: Press the left button to enter the fingerprint entry mode. Press the middle button to enter the fingerprint identification mode. +*/ + +#include +#include "finger.h" + + +FingerPrint::FingerPrint(void){ + +} + +FingerPrint FP; + +#define F_Time 10000 + +uint8_t FingerPrint::fpm_sendAndReceive(uint16_t timeout) +{ + uint8_t i, j; + uint8_t checkSum = 0; + + FP.RxCnt = 0; + FP.TxBuf[5] = 0; + + Serial2.write(CMD_HEAD); + for (i = 1; i < 6; i++) + { + Serial2.write(FP.TxBuf[i]); + checkSum ^= FP.TxBuf[i]; + } + Serial2.write(checkSum); + Serial2.write(CMD_TAIL); + + while ((!Serial2.available()) && timeout > 0) + { + delay(1); + timeout--; + } + + uint8_t ch; + for(i=0;i<8;i++) + { + if(Serial2.available()){ + ch = Serial2.read(); + FP.RxCnt++; + FP.RxBuf[i] = ch; + } + } + + if (FP.RxCnt != 8) {FP.RxCnt = 0;return ACK_TIMEOUT;} + if (FP.RxBuf[HEAD] != CMD_HEAD) return ACK_FAIL; + if (FP.RxBuf[TAIL] != CMD_TAIL) return ACK_FAIL; + if (FP.RxBuf[CMD] != (FP.TxBuf[CMD])) return ACK_FAIL; + + checkSum = 0; + for (j = 1; j < CHK; j++) { + checkSum ^= FP.RxBuf[j]; + } + if (checkSum != FP.RxBuf[CHK]) { + return ACK_FAIL; + } + return ACK_SUCCESS; +} + +uint8_t FingerPrint::fpm_sleep(void) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_SLEEP_MODE; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = 0; + FP.TxBuf[P3] = 0; + + res = fpm_sendAndReceive(500); + + if(res == ACK_SUCCESS) { + return ACK_SUCCESS; + } + else { + return ACK_FAIL; + } + +} + +uint8_t FingerPrint::fpm_setAddMode(uint8_t fpm_mode) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_ADD_MODE; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = fpm_mode; + FP.TxBuf[P3] = 0; + + res = fpm_sendAndReceive(200); + + if(res == ACK_SUCCESS && RxBuf[Q3] == ACK_SUCCESS) { + return ACK_SUCCESS; + } + else { + return ACK_FAIL; + } +} + +uint8_t FingerPrint::fpm_readAddMode(void) +{ + FP.TxBuf[CMD] = CMD_ADD_MODE; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = 0; + FP.TxBuf[P3] = 0X01; + + fpm_sendAndReceive(200); + + return RxBuf[Q2]; +} + +uint16_t FingerPrint::fpm_getUserNum(void) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_USER_CNT; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = 0; + FP.TxBuf[P3] = 0; + + res = fpm_sendAndReceive(200); + + if(res == ACK_SUCCESS && FP.RxBuf[Q3] == ACK_SUCCESS) { + return FP.RxBuf[Q2]; + } + else { + return 0XFF; + } + +} + +uint8_t FingerPrint::fpm_deleteAllUser(void) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_DEL_ALL; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = 0; + FP.TxBuf[P3] = 0; + + res = fpm_sendAndReceive(200); + + if(res == ACK_SUCCESS && RxBuf[Q3] == ACK_SUCCESS) { + return ACK_SUCCESS; + } + else { + return ACK_FAIL; + } +} + +uint8_t FingerPrint::fpm_deleteUser(uint8_t userNum) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_DEL; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = userNum; + FP.TxBuf[P3] = 0; + + res = fpm_sendAndReceive(200); + + if(res == ACK_SUCCESS && RxBuf[Q3] == ACK_SUCCESS) { + return ACK_SUCCESS; + } + else { + return ACK_FAIL; + } +} + +uint8_t FingerPrint::fpm_addUser(uint8_t userNum, uint8_t userPermission) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_ADD_1; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = userNum; + FP.TxBuf[P3] = userPermission; + + res = fpm_sendAndReceive(F_Time); + + if(res == ACK_SUCCESS) { + if(FP.RxBuf[Q3] == ACK_SUCCESS) { + FP.TxBuf[CMD] = CMD_ADD_2; + + res = fpm_sendAndReceive(F_Time); + + if(res == ACK_SUCCESS) { + if(FP.RxBuf[Q3] == ACK_SUCCESS) { + FP.TxBuf[CMD] = CMD_ADD_3; + + res = fpm_sendAndReceive(F_Time); + + if(res == ACK_SUCCESS) { + return FP.RxBuf[Q3]; + } + } + } + } + } + return res; + +} + +uint8_t FingerPrint::fpm_compareFinger(void) +{ + uint8_t res; + + FP.TxBuf[CMD] = CMD_MATCH; + FP.TxBuf[P1] = 0; + FP.TxBuf[P2] = 0; + FP.TxBuf[P3] = 0; + + res = fpm_sendAndReceive(8000); + + if(res == ACK_SUCCESS) + { + if(FP.RxBuf[Q3] == ACK_NOUSER) { + return ACK_NOUSER; + } + if(FP.RxBuf[Q3] == ACK_TIMEOUT) { + return ACK_TIMEOUT; + } + if((FP.RxBuf[Q2] != 0) && (FP.RxBuf[Q3] == 1 || FP.RxBuf[Q3] == 2 || FP.RxBuf[Q3] == 3)) { + return ACK_SUCCESS; + } + } + return res; +} + + diff --git a/examples/Unit/FINGER_FPC1020A/finger.h b/examples/Unit/FINGER_FPC1020A/finger.h new file mode 100644 index 0000000..dee89e7 --- /dev/null +++ b/examples/Unit/FINGER_FPC1020A/finger.h @@ -0,0 +1,77 @@ +#ifndef __TFS_M64_H +#define __TFS_M64_H + +#define TRUE 1 +#define FALSE 0 + +#define ACK_SUCCESS 0x00 +#define ACK_FAIL 0x01 +#define ACK_FULL 0x04 +#define ACK_NOUSER 0x05 +#define ACK_USER_EXIST 0x07 +#define ACK_TIMEOUT 0x08 + +#define ACK_GO_OUT 0x0F + +#define ACK_ALL_USER 0x00 +#define ACK_GUEST_USER 0x01 +#define ACK_NORMAL_USER 0x02 +#define ACK_MASTER_USER 0x03 + +#define USER_MAX_CNT 50 + +#define HEAD 0 +#define CMD 1 +#define CHK 6 +#define TAIL 7 + +#define P1 2 +#define P2 3 +#define P3 4 +#define Q1 2 +#define Q2 3 +#define Q3 4 + +#define CMD_HEAD 0xF5 +#define CMD_TAIL 0xF5 +#define CMD_ADD_1 0x01 +#define CMD_ADD_2 0x02 +#define CMD_ADD_3 0x03 +#define CMD_MATCH 0x0C +#define CMD_DEL 0x04 +#define CMD_DEL_ALL 0x05 +#define CMD_USER_CNT 0x09 +#define CMD_SLEEP_MODE 0x2C +#define CMD_ADD_MODE 0x2D + +#define CMD_FINGER_DETECTED 0x14 + + +class FingerPrint { + + public: + FingerPrint(void); + uint8_t fpm_sendAndReceive(uint16_t delayMs); + uint8_t fpm_sleep(void); + uint8_t fpm_setAddMode(uint8_t fpm_mode); + uint8_t fpm_readAddMode(void); + uint16_t fpm_getUserNum(void); + uint8_t fpm_deleteAllUser(void); + uint8_t fpm_deleteUser(uint8_t userNum); + uint8_t fpm_addUser(uint8_t userNum, uint8_t userPermission); + uint8_t fpm_compareFinger(void); + public: + uint8_t TxBuf[9]; + uint8_t RxBuf[9]; + uint8_t RxCnt; + private: + + private: + +}; + + +#endif /* __TFS-M64_H */ + + + diff --git a/examples/Unit/Zigbee_CC2630/P2P_TEST/P2P_TEST.ino b/examples/Unit/Zigbee_CC2630/P2P_TEST/P2P_TEST.ino index 4799bbb..40f2fd1 100644 --- a/examples/Unit/Zigbee_CC2630/P2P_TEST/P2P_TEST.ino +++ b/examples/Unit/Zigbee_CC2630/P2P_TEST/P2P_TEST.ino @@ -1,23 +1,10 @@ /* -******************************************************************************* -* Copyright (c) 2021 by M5Stack -* Equipped with M5Core sample source code -* 配套 M5Core 示例源代码 -* Visit the website for more information:https://docs.m5stack.com/en/unit/zigbee -* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/zigbee -* -* describe: zigbee. -* date:2021/8/27 -******************************************************************************* - Please connect to Port C,Supports configuring the Zigbee module into 3 working modes `Coordinator`, `Router`, `End Device` - 请连接端口C,支持将Zigbee模块配置为3种工作模式“协调器”、“路由器”、“终端设备” - You need to configure a `Coordinator` when using, and other devices can be configured as `End Device` for data sending and receiving. - 使用时需要配置一个“Coordinator”,其他设备可以配置为“End Device”,用于数据的发送和接收。 - Note: 16 and 17 of the DIP switch are set to ON. This case needs to use the Keyboard in the FACE Kit for information input - 注:拨码开关16、17为ON。这种情况需要使用FACE Kit中的键盘进行信息输入 + Description: + Supports configuring the Zigbee module into 3 working modes `Coordinator`, `Router`, `End Device` + You need to configure a `Coordinator` when using, and other devices can be configured as `End Device` for data sending and receiving. + Note: 16 and 17 of the DIP switch are set to ON. This case needs to use the Keyboard in the FACE Kit for information input */ - #include "M5Stack.h" #include "byteArray.h" #include "DRFZigbee.h" @@ -31,20 +18,20 @@ DRFZigbee zigbee; uint16_t atNow = 29999; uint16_t iconpos[3][6] = { - {0, 40, 120, 140, 53, 0}, - {100, 40, 120, 140, 0, 0}, - {257, 40, 63, 140, 0, 0}, + {0, 40, 120, 140, 53, 0}, + {100, 40, 120, 140, 0, 0}, + {257, 40, 63, 140, 0, 0}, }; const uint8_t *iconptrbuff[3] = { - coordinator_jpeg_120x140, - endDevice_jpeg_120x140, - router_jpeg_120x140, + coordinator_jpeg_120x140, + endDevice_jpeg_120x140, + router_jpeg_120x140, }; size_t iconSizeBuff[3] = { - 27537, - 24609, - 26433, + 27537, + 24609, + 26433, }; char asciiHexList[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; @@ -53,351 +40,352 @@ bool flushFlag = true; int drawChatBubbles(String str,uint16_t addr) { - uint16_t posY = 76; - M5.Lcd.drawJpg(chatBubblesTop, 10561, 60, 66, 229, 10); - M5.Lcd.setTextDatum(TL_DATUM); - M5.Lcd.setTextColor(M5.Lcd.color565(0x44, 0x44, 0x44)); - while (str.length() > 0) - { - M5.Lcd.fillRect(60, posY, 228, 30, M5.Lcd.color565(0x88, 0xd1, 0xff)); - String dispalyStr = str.substring(0, 24); - M5.Lcd.drawString(dispalyStr, 75, posY + 2, 2); - str.remove(0, 24); - posY += 20; - } - M5.Lcd.drawJpg(chatBubblesBottom, 11546, 60, posY, 229, 20); + uint16_t posY = 76; + M5.Lcd.drawJpg(chatBubblesTop, 10561, 60, 66, 229, 10); + M5.Lcd.setTextDatum(TL_DATUM); + M5.Lcd.setTextColor(M5.Lcd.color565(0x44, 0x44, 0x44)); + while (str.length() > 0) + { + M5.Lcd.fillRect(60, posY, 228, 30, M5.Lcd.color565(0x88, 0xd1, 0xff)); + String dispalyStr = str.substring(0, 24); + M5.Lcd.drawString(dispalyStr, 75, posY + 2, 2); + str.remove(0, 24); + posY += 20; + } + M5.Lcd.drawJpg(chatBubblesBottom, 11546, 60, posY, 229, 20); - M5.Lcd.fillEllipse(27,posY+18,23,23,M5.Lcd.color565(200, 200, 200)); + M5.Lcd.fillEllipse(27,posY+18,23,23,M5.Lcd.color565(200, 200, 200)); - char panIDchar[5] = {0, 0, 0, 0, 0}; - for (int i = 0; i < 4; i++) - { - panIDchar[i] = asciiHexList[addr & 0x000f]; - addr >>= 4; - } + char panIDchar[5] = {0, 0, 0, 0, 0}; + for (int i = 0; i < 4; i++) + { + panIDchar[i] = asciiHexList[addr & 0x000f]; + addr >>= 4; + } - M5.Lcd.setTextDatum(CC_DATUM); - M5.Lcd.setTextColor(M5.Lcd.color565(20, 20, 20)); - M5.Lcd.drawString(panIDchar,27,posY+20); - return posY; + M5.Lcd.setTextDatum(CC_DATUM); + M5.Lcd.setTextColor(M5.Lcd.color565(20, 20, 20)); + M5.Lcd.drawString(panIDchar,27,posY+20); + return posY; } void setup() { - M5.begin(); - Serial2.begin(38400, SERIAL_8N1, 16, 17); + M5.begin(); + Serial2.begin(38400, SERIAL_8N1, 16, 17); - zigbee.begin(Serial2); + zigbee.begin(Serial2); - M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); + M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); - M5.Lcd.drawJpg(router_jpeg_120x140, 26433, 0, 40, 120, 140, 53, 0); - M5.Lcd.drawJpg(coordinator_jpeg_120x140, 26433, 100, 40, 120, 140, 0, 0); - M5.Lcd.drawJpg(endDevice_jpeg_120x140, 26433, 257, 40, 63, 140, 0, 0); + M5.Lcd.drawJpg(router_jpeg_120x140, 26433, 0, 40, 120, 140, 53, 0); + M5.Lcd.drawJpg(coordinator_jpeg_120x140, 26433, 100, 40, 120, 140, 0, 0); + M5.Lcd.drawJpg(endDevice_jpeg_120x140, 26433, 257, 40, 63, 140, 0, 0); - DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; - zigbee.linkMoudle(); - zigbee.readModuleparm(arg); + DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; + zigbee.linkMoudle(); + zigbee.readModuleparm(arg); - Serial.printf("PAIN ID:%04X\r\n", arg->main_PANID); + Serial.printf("PAIN ID:%04X\r\n", arg->main_PANID); + + //drawChatBubbles("Fuck STC!!!!!"); + + /* + xTaskCreate([](void *arg){ + uint8_t readbuff[128]; + while(1){ + int length = Serial2.available(); + if( length > 0) { Serial2.readBytes(readbuff,length);Serial.write(readbuff,length);} + delay(10); + } + }, "reviceTask", 2048, nullptr, 2, nullptr); + */ + + /* + if( zigbee.sendCMDAndWaitRevice(0xfc,ZIGBEE_CMD_LINKMODULE) != DRFZigbee::kReviceOK ) + { + Serial.printf("Link Zigbee module faild! code:%d\r\n",zigbee.lastErrorcode); + while(1) delay(100); + } + + byteArray array; + if( zigbee.sendCMDAndWaitRevice(0xfc,ZIGBEE_CMD_READPARM,&array) != DRFZigbee::kReviceOK ) + { + Serial.printf("Read module pram faild! code:%d\r\n",zigbee.lastErrorcode); + while(1) delay(100); + } + + if(( array.at(0) != 0x0A )||( array.at(1) != 0x0E )) + { + Serial.println("Read module pram faild!"); + while(1) delay(100); + } + array = array.mid(2); + + array.printfArray(&Serial); + + DRFZigbee::zigbee_arg arg; + memcpy(arg.Wave,array.dataptr(),sizeof(DRFZigbee::zigbee_arg)); + + arg.main_pointType = DRFZigbee::kCoordinator; + arg.main_PANID = DRFZigbee::swap(0x6889); + arg.main_channel = 20; + arg.main_ATN = DRFZigbee::kANTEXP; + arg.main_transmissionMode = DRFZigbee::kN2Ntransmission; + arg.main_customID = DRFZigbee::swap(0x1213); + arg.res3 = 0x01; + + byteArray sendArray; + sendArray += 0x07; + sendArray += byteArray(&arg.Wave[0],16); + sendArray += byteArray(&arg.Wave[24],16); + sendArray += byteArray(&arg.Wave[42],6); + + sendArray.printfArray(&Serial); + + if( zigbee.sendCMDAndWaitRevice(0xfc,sendArray,&array) != DRFZigbee::kReviceOK ) + { + Serial.printf("Read module pram faild! code:%d\r\n",zigbee.lastErrorcode); + while(1) delay(100); + } + + if( zigbee.sendCMDAndWaitRevice(0xfc,{0x06,0x44,0x54,0x4b,0xaa,0xbb}) != DRFZigbee::kReviceOK ) + { + Serial.printf("reboot Zigbee module faild! code:%d\r\n",zigbee.lastErrorcode); + while(1) delay(100); + } + + delay(1000); + */ + + //zigbee.sendDataP2P(DRFZigbee::kP2PShortAddrMode,0x0000,{0x01,0x02,0x03,0x04}); + + delay(1000); + /* + zigbee.nodeList.clear(); + for (int i = 0; i < 10; i++) + { + zigbee.getNetworksTopology(); + delay(100); + } + */ + + /* + M5.Lcd.fillRect(0,0,320,240,TFT_BLACK); + int count = 0; + for (int i = 0; i < 20; i++) + { + int n = ( count < 3 ) ? 0 : (int)log2(1-(((count + 3) * (-1)/3))); + int angle2 = 120 * pow(0.5,n); + int angle = (n > 0) ? angle2 + ( count - 3 * pow(2,n - 1))* angle2 * 2 : ( count % 3 ) * 120; + //int angle = ( count < 3 ) ? ( count % 3 ) * 120 : ((120 / (((count / 3) * 2))) + ( count % 3 ) * 120); + double posX = cos(PI*angle/180) * 100 + 160; + double posY = sin(PI*angle/180) * 100 + 120; + + Serial.printf("Point %02d:%03d,%03d\r\n",count,(int)posX,(int)posY); + + M5.Lcd.fillEllipse((int)posX,(int)posY,10,10,TFT_WHITE); + delay(200); + count++; + } + */ + + /* + if( !zigbee.nodeList.empty() ) + { + int count = 0; + std::map::iterator iter; + for( iter = zigbee.nodeList.begin(); iter != zigbee.nodeList.end();iter ++ ) + { - /* - xTaskCreate([](void *arg){ - uint8_t readbuff[128]; - while(1){ - int length = Serial2.available(); - if( length > 0) { Serial2.readBytes(readbuff,length);Serial.write(readbuff,length);} - delay(10); - } - }, "reviceTask", 2048, nullptr, 2, nullptr); - */ - - /* - if( zigbee.sendCMDAndWaitRevice(0xfc,ZIGBEE_CMD_LINKMODULE) != DRFZigbee::kReviceOK ) - { - Serial.printf("Link Zigbee module faild! code:%d\r\n",zigbee.lastErrorcode); - while(1) delay(100); - } - - byteArray array; - if( zigbee.sendCMDAndWaitRevice(0xfc,ZIGBEE_CMD_READPARM,&array) != DRFZigbee::kReviceOK ) - { - Serial.printf("Read module pram faild! code:%d\r\n",zigbee.lastErrorcode); - while(1) delay(100); - } - - if(( array.at(0) != 0x0A )||( array.at(1) != 0x0E )) - { - Serial.println("Read module pram faild!"); - while(1) delay(100); - } - array = array.mid(2); - - array.printfArray(&Serial); - - DRFZigbee::zigbee_arg arg; - memcpy(arg.Wave,array.dataptr(),sizeof(DRFZigbee::zigbee_arg)); - - arg.main_pointType = DRFZigbee::kCoordinator; - arg.main_PANID = DRFZigbee::swap(0x6889); - arg.main_channel = 20; - arg.main_ATN = DRFZigbee::kANTEXP; - arg.main_transmissionMode = DRFZigbee::kN2Ntransmission; - arg.main_customID = DRFZigbee::swap(0x1213); - arg.res3 = 0x01; - - byteArray sendArray; - sendArray += 0x07; - sendArray += byteArray(&arg.Wave[0],16); - sendArray += byteArray(&arg.Wave[24],16); - sendArray += byteArray(&arg.Wave[42],6); - - sendArray.printfArray(&Serial); - - if( zigbee.sendCMDAndWaitRevice(0xfc,sendArray,&array) != DRFZigbee::kReviceOK ) - { - Serial.printf("Read module pram faild! code:%d\r\n",zigbee.lastErrorcode); - while(1) delay(100); - } - - if( zigbee.sendCMDAndWaitRevice(0xfc,{0x06,0x44,0x54,0x4b,0xaa,0xbb}) != DRFZigbee::kReviceOK ) - { - Serial.printf("reboot Zigbee module faild! code:%d\r\n",zigbee.lastErrorcode); - while(1) delay(100); - } - - delay(1000); - */ - - //zigbee.sendDataP2P(DRFZigbee::kP2PShortAddrMode,0x0000,{0x01,0x02,0x03,0x04}); - - delay(1000); - /* - zigbee.nodeList.clear(); - for (int i = 0; i < 10; i++) - { - zigbee.getNetworksTopology(); - delay(100); - } - */ - - /* - M5.Lcd.fillRect(0,0,320,240,TFT_BLACK); - int count = 0; - for (int i = 0; i < 20; i++) - { - int n = ( count < 3 ) ? 0 : (int)log2(1-(((count + 3) * (-1)/3))); - int angle2 = 120 * pow(0.5,n); - int angle = (n > 0) ? angle2 + ( count - 3 * pow(2,n - 1))* angle2 * 2 : ( count % 3 ) * 120; - //int angle = ( count < 3 ) ? ( count % 3 ) * 120 : ((120 / (((count / 3) * 2))) + ( count % 3 ) * 120); - double posX = cos(PI*angle/180) * 100 + 160; - double posY = sin(PI*angle/180) * 100 + 120; - - Serial.printf("Point %02d:%03d,%03d\r\n",count,(int)posX,(int)posY); - - M5.Lcd.fillEllipse((int)posX,(int)posY,10,10,TFT_WHITE); - delay(200); - count++; - } - */ - - /* - if( !zigbee.nodeList.empty() ) - { - int count = 0; - std::map::iterator iter; - for( iter = zigbee.nodeList.begin(); iter != zigbee.nodeList.end();iter ++ ) - { - - - iter->first; - iter->second; - count++; - } - } - */ + iter->first; + iter->second; + count++; + } + } + */ } void AppCoordinator() { - Serial.printf("AppCoordinator\r\n"); - M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); - M5.Lcd.drawJpg(coordinatorTitle, 21823, 0, 10, 320, 35); + Serial.printf("AppCoordinator\r\n"); + M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); + M5.Lcd.drawJpg(coordinatorTitle, 21823, 0, 10, 320, 35); - DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; - zigbee.readModuleparm(arg); - arg->main_pointType = DRFZigbee::kCoordinator; + DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; + zigbee.readModuleparm(arg); + arg->main_pointType = DRFZigbee::kCoordinator; - M5.Lcd.setTextColor(TFT_WHITE); - M5.Lcd.setTextDatum(TC_DATUM); + M5.Lcd.setTextColor(TFT_WHITE); + M5.Lcd.setTextDatum(TC_DATUM); - char panIDchar[5] = {0, 0, 0, 0, 0}; - uint16_t panID = DRFZigbee::swap(arg->main_PANID); - for (int i = 0; i < 4; i++) - { - panIDchar[i] = asciiHexList[panID & 0x000f]; - panID >>= 4; - } + char panIDchar[5] = {0, 0, 0, 0, 0}; + uint16_t panID = DRFZigbee::swap(arg->main_PANID); + for (int i = 0; i < 4; i++) + { + panIDchar[i] = asciiHexList[panID & 0x000f]; + panID >>= 4; + } - M5.Lcd.setTextColor(M5.Lcd.color565(30, 30, 30)); - M5.Lcd.drawString(panIDchar, 165, 95, 8); - M5.Lcd.setTextColor(TFT_WHITE); - M5.Lcd.drawString(panIDchar, 160, 90, 8); - zigbee.setModuleparm(*arg); - zigbee.rebootModule(); - while (1) - { - M5.update(); - if (M5.BtnB.wasPressed()) - break; - } + M5.Lcd.setTextColor(M5.Lcd.color565(30, 30, 30)); + M5.Lcd.drawString(panIDchar, 165, 95, 8); + M5.Lcd.setTextColor(TFT_WHITE); + M5.Lcd.drawString(panIDchar, 160, 90, 8); + zigbee.setModuleparm(*arg); + zigbee.rebootModule(); + while (1) + { + M5.update(); + if (M5.BtnB.wasPressed()) + break; + } - delete arg; + delete arg; } void AppRouter() { - Serial.printf("AppRouter\r\n"); - M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); - M5.Lcd.drawJpg(EndDeviceTitle, 17470, 0, 10, 320, 35); + Serial.printf("AppRouter\r\n"); + M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); + M5.Lcd.drawJpg(EndDeviceTitle, 17470, 0, 10, 320, 35); - DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; - zigbee.readModuleparm(arg); - arg->main_pointType = DRFZigbee::kRouter; - arg->main_PANID = DRFZigbee::swap(0x1617); - arg->main_transmissionMode = DRFZigbee::kN2Ntransmission; - arg->main_channel = 20; - arg->main_ATN = DRFZigbee::kANTEXP; - zigbee.setModuleparm(*arg); - zigbee.rebootModule(); + DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; + zigbee.readModuleparm(arg); + arg->main_pointType = DRFZigbee::kRouter; + arg->main_PANID = DRFZigbee::swap(0x1617); + arg->main_transmissionMode = DRFZigbee::kN2Ntransmission; + arg->main_channel = 20; + arg->main_ATN = DRFZigbee::kANTEXP; + zigbee.setModuleparm(*arg); + zigbee.rebootModule(); - pinMode(5, INPUT_PULLUP); + pinMode(5, INPUT_PULLUP); - while (1) - { - DRFZigbee::reviceData_t revice; - if (zigbee.reviceData(&revice) == DRFZigbee::kReviceOK) + while (1) { - revice.array->printfArray(&Serial); + DRFZigbee::reviceData_t revice; + if (zigbee.reviceData(&revice) == DRFZigbee::kReviceOK) + { + revice.array->printfArray(&Serial); + } } - } } void AppEndDevice() { - Serial.printf("AppEndDevice\r\n"); - M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); - M5.Lcd.drawJpg(EndDeviceTitle, 17470, 0, 10, 320, 35); + Serial.printf("AppEndDevice\r\n"); + M5.Lcd.fillRect(0, 0, 320, 240, M5.Lcd.color565(56, 56, 56)); + M5.Lcd.drawJpg(EndDeviceTitle, 17470, 0, 10, 320, 35); - DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; - zigbee.readModuleparm(arg); - arg->main_pointType = DRFZigbee::kEndDevice; - arg->main_PANID = DRFZigbee::swap(0x1617); - arg->main_transmissionMode = DRFZigbee::kN2Ntransmission; - zigbee.setModuleparm(*arg); - zigbee.rebootModule(); + DRFZigbee::zigbee_arg_t *arg = new DRFZigbee::zigbee_arg_t; + zigbee.readModuleparm(arg); + arg->main_pointType = DRFZigbee::kEndDevice; + arg->main_PANID = DRFZigbee::swap(0x1617); + arg->main_transmissionMode = DRFZigbee::kN2Ntransmission; + zigbee.setModuleparm(*arg); + zigbee.rebootModule(); - pinMode(5, INPUT_PULLUP); - Wire.begin(21,22); + pinMode(5, INPUT_PULLUP); + Wire.begin(21,22); - M5.Lcd.fillRect(20, 200, 280, 30, M5.Lcd.color565(56, 56, 56)); - M5.Lcd.drawRect(20, 200, 280, 30, M5.Lcd.color565(200, 200, 200)); + M5.Lcd.fillRect(20, 200, 280, 30, M5.Lcd.color565(56, 56, 56)); + M5.Lcd.drawRect(20, 200, 280, 30, M5.Lcd.color565(200, 200, 200)); - uint8_t senduff[256]; - char revicechar[256]; - uint16_t charPos = 0; + uint8_t senduff[256]; + char revicechar[256]; + uint16_t charPos = 0; - memset(senduff,0,256); + memset(senduff,0,256); - while (1) - { - DRFZigbee::reviceData_t revice; - if (zigbee.reviceData(&revice,10) == DRFZigbee::kReviceOK) - { - revice.array->printfArray(&Serial); - M5.Lcd.fillRect(0, 66, 320, 128, M5.Lcd.color565(56, 56, 56)); - memset(revicechar,0,256); - memcpy(revicechar,revice.array->dataptr(),revice.length); - drawChatBubbles(String(revicechar),revice.fromAddr); - } + while (1) + { + DRFZigbee::reviceData_t revice; + if (zigbee.reviceData(&revice,10) == DRFZigbee::kReviceOK) + { + revice.array->printfArray(&Serial); + M5.Lcd.fillRect(0, 66, 320, 128, M5.Lcd.color565(56, 56, 56)); + memset(revicechar,0,256); + memcpy(revicechar,revice.array->dataptr(),revice.length); + drawChatBubbles(String(revicechar),revice.fromAddr); + } - if (digitalRead(5) == LOW) - { - Wire.requestFrom(0x08, 1); - while (Wire.available()) - { - uint8_t key_val = Wire.read(); - if (key_val != 0) - { - if (key_val >= 0x20 && key_val < 0x7F) - { - senduff[charPos] = key_val; - Serial.print((char)key_val); - M5.Lcd.setTextDatum(TL_DATUM); - M5.Lcd.setTextColor(M5.Lcd.color565(200, 200, 200)); - M5.Lcd.drawString(String((char*)senduff),30,206,2); - charPos++; - } - else if( key_val == 0x0d) - { - M5.Lcd.fillRect(20, 200, 280, 30, M5.Lcd.color565(56, 56, 56)); - M5.Lcd.drawRect(20, 200, 280, 30, M5.Lcd.color565(200, 200, 200)); - zigbee.sendDataP2P(DRFZigbee::kP2PShortAddrMode,0xffff,senduff,charPos); - memset(senduff,0,256); - charPos = 0; - } - } - } - } - } + if (digitalRead(5) == LOW) + { + Wire.requestFrom(0x08, 1); + while (Wire.available()) + { + uint8_t key_val = Wire.read(); + if (key_val != 0) + { + if (key_val >= 0x20 && key_val < 0x7F) + { + senduff[charPos] = key_val; + Serial.print((char)key_val); + M5.Lcd.setTextDatum(TL_DATUM); + M5.Lcd.setTextColor(M5.Lcd.color565(200, 200, 200)); + M5.Lcd.drawString(String((char*)senduff),30,206,2); + charPos++; + } + else if( key_val == 0x0d) + { + M5.Lcd.fillRect(20, 200, 280, 30, M5.Lcd.color565(56, 56, 56)); + M5.Lcd.drawRect(20, 200, 280, 30, M5.Lcd.color565(200, 200, 200)); + zigbee.sendDataP2P(DRFZigbee::kP2PShortAddrMode,0xffff,senduff,charPos); + memset(senduff,0,256); + charPos = 0; + } + } + } + } + } } void loop() { - if (M5.BtnA.wasPressed()) - { - atNow = (atNow >= 60000) ? 30000 : atNow; - atNow++; - flushFlag = true; - } - else if (M5.BtnB.wasPressed()) - { - switch ((atNow + 2) % 3) - { - case 0: - AppRouter(); - break; - case 1: - AppCoordinator(); - break; - case 2: - AppEndDevice(); - break; - } - flushFlag = true; - } - else if (M5.BtnC.wasPressed()) - { - atNow = (atNow == 0) ? 30000 : atNow; - atNow--; - flushFlag = true; - } - if (flushFlag) - { - flushFlag = false; - for (int i = 0; i < 3; i++) - { - M5.Lcd.drawJpg(iconptrbuff[(atNow + i) % 3], - iconSizeBuff[(atNow + i) % 3], - iconpos[i][0], - iconpos[i][1], - iconpos[i][2], - iconpos[i][3], - iconpos[i][4], - iconpos[i][5]); - } - } - M5.update(); - delay(10); -} + if (M5.BtnA.wasPressed()) + { + atNow = (atNow >= 60000) ? 30000 : atNow; + atNow++; + flushFlag = true; + } + else if (M5.BtnB.wasPressed()) + { + switch ((atNow + 2) % 3) + { + case 0: + AppRouter(); + break; + case 1: + AppCoordinator(); + break; + case 2: + AppEndDevice(); + break; + } + flushFlag = true; + } + else if (M5.BtnC.wasPressed()) + { + atNow = (atNow == 0) ? 30000 : atNow; + atNow--; + flushFlag = true; + } + if (flushFlag) + { + flushFlag = false; + for (int i = 0; i < 3; i++) + { + M5.Lcd.drawJpg(iconptrbuff[(atNow + i) % 3], + iconSizeBuff[(atNow + i) % 3], + iconpos[i][0], + iconpos[i][1], + iconpos[i][2], + iconpos[i][3], + iconpos[i][4], + iconpos[i][5]); + } + } + M5.update(); + delay(10); +} \ No newline at end of file