Skip to content

Commit 4136fa0

Browse files
任乐涛renletao
authored andcommitted
Merge branch 'dev/chain-PIR-Switch' into 'main'
Normalize PIR and Switch include paths See merge request m5-arduino-library/m5chain!3
2 parents 292f827 + 14a330b commit 4136fa0

11 files changed

Lines changed: 1368 additions & 11 deletions

File tree

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,34 @@ Chain RGB integrates an STM32G031G8U6 core microcontroller and uses a UART seria
112112
https://docs.m5stack.com/en/products/sku/U218
113113

114114
## License
115-
- [MIT](https://github.com/m5stack/M5Chain/blob/main/LICENSE)
115+
- [MIT](https://github.com/m5stack/M5Chain/blob/main/LICENSE)
116+
117+
# Chain PIR
118+
119+
## Overview
120+
Chain PIR is a passive infrared human detection sensor node in the M5Stack Chain series. It can read the current PIR detection status and report person-come or person-leave trigger events. It also supports report mode configuration and a person-come trigger keep time, during which leave events are ignored until the hold time expires.
121+
122+
Chain PIR integrates an STM32G031G8U6 core MCU and uses a UART serial daisy-chain communication protocol. Through two HY2.0-4P expansion interfaces, it can be connected with additional Chain series devices to build richer sensing and automation applications. It is suitable for human presence detection, motion-triggered interaction, smart home sensing, and security monitoring scenarios.
123+
124+
### SKU: U225
125+
126+
## Related Link
127+
https://docs.m5stack.com/en/products/sku/U225
128+
129+
## License
130+
- [MIT](https://github.com/m5stack/M5Chain/blob/main/LICENSE)
131+
132+
# Chain Switch
133+
134+
## Overview
135+
Chain Switch is a slide switch input node in the M5Stack Chain series. It supports reading 12-bit ADC values, 8-bit mapped ADC values, and the current open/close switch status. It also supports configurable open and close thresholds, down-to-up direction settings, and automatic open/close trigger reports.
136+
137+
Chain Switch integrates an STM32G031G8U6 core MCU and uses a UART serial daisy-chain communication protocol. Through two HY2.0-4P expansion interfaces, it can be connected with additional Chain series devices to build richer interactive applications. It is suitable for switch state detection, human-machine interaction, smart home control, and other state monitoring scenarios.
138+
139+
### SKU: U227
140+
141+
## Related Link
142+
https://docs.m5stack.com/en/products/sku/U227
143+
144+
## License
145+
- [MIT](https://github.com/m5stack/M5Chain/blob/main/LICENSE)
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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

Comments
 (0)