|
| 1 | +/* |
| 2 | + *SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + *SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | + |
| 7 | +#include "M5Chain.h" |
| 8 | + |
| 9 | +#define TXD_PIN GPIO_NUM_21 // Tx |
| 10 | +#define RXD_PIN GPIO_NUM_22 // Rx |
| 11 | + |
| 12 | +Chain M5Chain; |
| 13 | + |
| 14 | +device_list_t *devices_list = NULL; |
| 15 | +uint16_t device_nums = 0; |
| 16 | +uint8_t operation_status = 0; |
| 17 | +chain_status_t chain_status = CHAIN_OK; |
| 18 | + |
| 19 | +uint8_t rgb_test[5][3] = { |
| 20 | + {0xFF, 0x00, 0x00}, {0x00, 0xFF, 0x00}, {0x00, 0x00, 0xFF}, {0xFF, 0xFF, 0xFF}, {0x00, 0x00, 0x00}, |
| 21 | +}; |
| 22 | + |
| 23 | +uint8_t uid32[4] = {0}; |
| 24 | +uint8_t uid96[12] = {0}; |
| 25 | + |
| 26 | +uint8_t keep_seconds = 5; |
| 27 | + |
| 28 | +void printDeviceList(device_list_t *devices) |
| 29 | +{ |
| 30 | + if (devices == NULL) { |
| 31 | + Serial.println("devices is NULL"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + Serial.print("devices count: "); |
| 36 | + Serial.println(devices->count); |
| 37 | + |
| 38 | + for (uint8_t i = 0; i < devices->count; i++) { |
| 39 | + Serial.print("devices ID: "); |
| 40 | + Serial.println(devices->devices[i].id); |
| 41 | + Serial.print("devices type: "); |
| 42 | + Serial.println(devices->devices[i].device_type); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void setup() |
| 47 | +{ |
| 48 | + Serial.begin(115200); |
| 49 | + Serial.println("M5Chain PIR Test"); |
| 50 | + M5Chain.begin(&Serial2, 115200, RXD_PIN, TXD_PIN); |
| 51 | + |
| 52 | + if (M5Chain.isDeviceConnected()) { |
| 53 | + Serial.println("devices is connected"); |
| 54 | + chain_status = M5Chain.getDeviceNum(&device_nums); |
| 55 | + if (chain_status == CHAIN_OK) { |
| 56 | + devices_list = (device_list_t *)malloc(sizeof(device_list_t)); |
| 57 | + if (devices_list == NULL) { |
| 58 | + Serial.println("devices list malloc failed"); |
| 59 | + return; |
| 60 | + } |
| 61 | + devices_list->count = device_nums; |
| 62 | + devices_list->devices = (device_info_t *)malloc(sizeof(device_info_t) * device_nums); |
| 63 | + if (devices_list->devices == NULL) { |
| 64 | + Serial.println("devices info malloc failed"); |
| 65 | + free(devices_list); |
| 66 | + devices_list = NULL; |
| 67 | + return; |
| 68 | + } |
| 69 | + if (M5Chain.getDeviceList(devices_list)) { |
| 70 | + Serial.println("get devices list success"); |
| 71 | + printDeviceList(devices_list); |
| 72 | + } else { |
| 73 | + Serial.println("get devices list failed"); |
| 74 | + } |
| 75 | + } else { |
| 76 | + Serial.printf("error status:%d \r\n", chain_status); |
| 77 | + Serial.printf("devices num get failed.\r\n"); |
| 78 | + } |
| 79 | + } else { |
| 80 | + Serial.println("devices is not connected."); |
| 81 | + } |
| 82 | + |
| 83 | + if (devices_list) { |
| 84 | + for (uint8_t i = 0; i < devices_list->count; i++) { |
| 85 | + if (devices_list->devices[i].device_type == CHAIN_PIR_TYPE_CODE) { |
| 86 | + chain_status = M5Chain.setRGBLight(devices_list->devices[i].id, 100, &operation_status); |
| 87 | + if (chain_status == CHAIN_OK && operation_status) { |
| 88 | + Serial.printf("ID[%d] set rgb light success\r\n", devices_list->devices[i].id); |
| 89 | + } else { |
| 90 | + Serial.printf("ID[%d] set rgb light failed, chain_status:%d operation_status:%d \r\n", |
| 91 | + devices_list->devices[i].id, chain_status, operation_status); |
| 92 | + } |
| 93 | + for (uint8_t j = 0; j < 5; j++) { |
| 94 | + uint8_t rgb[3] = {0}; |
| 95 | + chain_status = |
| 96 | + M5Chain.setRGBValue(devices_list->devices[i].id, 0, 1, rgb_test[j], 3, &operation_status); |
| 97 | + if (chain_status == CHAIN_OK && operation_status) { |
| 98 | + Serial.printf("ID[%d] set rgb %d %d %d success\r\n", devices_list->devices[i].id, |
| 99 | + rgb_test[j][0], rgb_test[j][1], rgb_test[j][2]); |
| 100 | + } else { |
| 101 | + Serial.printf("ID[%d] set rgb %d %d %d failed, chain_status:%d operation_status:%d \r\n", |
| 102 | + devices_list->devices[i].id, rgb_test[j][0], rgb_test[j][1], rgb_test[j][2], |
| 103 | + chain_status, operation_status); |
| 104 | + } |
| 105 | + chain_status = M5Chain.getRGBValue(devices_list->devices[i].id, 0, 1, rgb, 3, &operation_status); |
| 106 | + if (chain_status == CHAIN_OK && operation_status) { |
| 107 | + Serial.printf("ID[%d] get rgb %d %d %d success \r\n", devices_list->devices[i].id, rgb[0], |
| 108 | + rgb[1], rgb[2]); |
| 109 | + } else { |
| 110 | + Serial.printf("ID[%d] get rgb %d %d %d failed, chain_status:%d operation_status:%d \r\n", |
| 111 | + devices_list->devices[i].id, rgb[0], rgb[1], rgb[2], chain_status, |
| 112 | + operation_status); |
| 113 | + } |
| 114 | + delay(500); |
| 115 | + } |
| 116 | + |
| 117 | + uint8_t fw_version = 0; |
| 118 | + chain_status = M5Chain.getFirmwareVersion(devices_list->devices[i].id, &fw_version, 100); |
| 119 | + if (chain_status == CHAIN_OK) { |
| 120 | + Serial.printf("ID[%d] get firmware version: 0x%02x\r\n", devices_list->devices[i].id, fw_version); |
| 121 | + } else { |
| 122 | + Serial.printf("ID[%d] get firmware version failed, chain_status:%d \r\n", |
| 123 | + devices_list->devices[i].id, chain_status); |
| 124 | + } |
| 125 | + |
| 126 | + chain_status = M5Chain.setPIRDetectTriggerMode(devices_list->devices[i].id, CHAIN_DETECT_REPORT_MODE, |
| 127 | + &operation_status); |
| 128 | + if (chain_status == CHAIN_OK && operation_status) { |
| 129 | + Serial.printf("ID[%d] set pir detect trigger mode success\r\n", devices_list->devices[i].id); |
| 130 | + } else { |
| 131 | + Serial.printf( |
| 132 | + "ID[%d] set pir detect trigger mode failed, chain_status:%d operation_status:%d \r\n", |
| 133 | + devices_list->devices[i].id, chain_status, operation_status); |
| 134 | + } |
| 135 | + |
| 136 | + uint8_t saveToFlash = CHAIN_SAVE_FLASH_DISABLE; |
| 137 | + chain_status = M5Chain.setPIRComeTriggerKeepSeconds(devices_list->devices[i].id, keep_seconds, |
| 138 | + &operation_status, saveToFlash); |
| 139 | + if (chain_status == CHAIN_OK && operation_status) { |
| 140 | + Serial.printf( |
| 141 | + "ID[%d] set pir person-come trigger keep seconds success, keep %us, operate_status: %u\r\n", |
| 142 | + devices_list->devices[i].id, keep_seconds, operation_status); |
| 143 | + } else { |
| 144 | + Serial.printf( |
| 145 | + "ID[%d] set pir person-come trigger keep seconds failed, chain_status:%d " |
| 146 | + "operation_status:%d\r\n", |
| 147 | + devices_list->devices[i].id, chain_status, operation_status); |
| 148 | + } |
| 149 | + |
| 150 | + chain_status = M5Chain.getPIRComeTriggerKeepSeconds(devices_list->devices[i].id, &keep_seconds); |
| 151 | + if (chain_status == CHAIN_OK) { |
| 152 | + Serial.printf("ID[%d] get pir person-come trigger keep seconds success, keep %us\r\n", |
| 153 | + devices_list->devices[i].id, keep_seconds); |
| 154 | + } else { |
| 155 | + Serial.printf("ID[%d] get pir person-come trigger keep seconds failed, chain_status:%d\r\n", |
| 156 | + devices_list->devices[i].id, chain_status); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } else { |
| 161 | + Serial.println("devices list is NULL"); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +void loop() |
| 166 | +{ |
| 167 | + if (devices_list) { |
| 168 | + for (uint8_t i = 0; i < devices_list->count; i++) { |
| 169 | + if (devices_list->devices[i].device_type == CHAIN_PIR_TYPE_CODE) { |
| 170 | + chain_detect_mode_t pir_detect_report_mode; |
| 171 | + pir_detect_result_t pir_detect_res; |
| 172 | + pir_detect_report_t pir_detect_report_res; |
| 173 | + chain_status = M5Chain.getPIRDetectTriggerMode(devices_list->devices[i].id, &pir_detect_report_mode); |
| 174 | + if (chain_status == CHAIN_OK) { |
| 175 | + Serial.printf("Switch ID[%d] pir detect_trigger mode:%d \r\n", devices_list->devices[i].id, |
| 176 | + pir_detect_report_mode); |
| 177 | + } else { |
| 178 | + Serial.printf("Switch ID[%d] get pir_detect_trigger_mode failed, chain_status:%d \r\n", |
| 179 | + devices_list->devices[i].id, chain_status); |
| 180 | + } |
| 181 | + chain_status = M5Chain.getIRStatus(devices_list->devices[i].id, &pir_detect_res); |
| 182 | + if (chain_status == CHAIN_OK) { |
| 183 | + Serial.printf("PIR ID[%d] detect result is: %d\r\n", devices_list->devices[i].id, pir_detect_res); |
| 184 | + } else { |
| 185 | + Serial.printf("PIR ID[%d] get ir status failed, chain_status:%d \r\n", devices_list->devices[i].id, |
| 186 | + chain_status); |
| 187 | + } |
| 188 | + while (M5Chain.getPIRDetectTrigger(devices_list->devices[i].id, &pir_detect_report_res)) { |
| 189 | + switch (pir_detect_report_res) { |
| 190 | + case CHAIN_PIR_REPORT_PERSON_LEAVE: |
| 191 | + Serial.printf("ENCODER ID[%d] detect result is: non person \r\n", |
| 192 | + devices_list->devices[i].id); |
| 193 | + break; |
| 194 | + case CHAIN_PIR_REPORT_PERSON_COME: |
| 195 | + Serial.printf("ENCODER ID[%d] detect result is: have person \r\n", |
| 196 | + devices_list->devices[i].id); |
| 197 | + break; |
| 198 | + default: |
| 199 | + Serial.printf("ENCODER ID[%d] detect result is: unknown , pir_detect_res:%04x\r\n", |
| 200 | + devices_list->devices[i].id, pir_detect_res); |
| 201 | + break; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + delay(500); |
| 208 | +} |
0 commit comments