1+ /*
2+ *SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3+ *
4+ *SPDX-License-Identifier: MIT
5+ */
6+
7+ #include " M5Chain.h"
8+
9+ #define TXD_PIN GPIO_NUM_6 // Tx
10+ #define RXD_PIN GPIO_NUM_5 // 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+ void printDeviceList (device_list_t *devices)
24+ {
25+ if (devices == NULL ) {
26+ Serial.println (" devices is NULL" );
27+ return ;
28+ }
29+
30+ Serial.print (" devices count: " );
31+ Serial.println (devices->count );
32+
33+ for (uint8_t i = 0 ; i < devices->count ; i++) {
34+ Serial.print (" devices ID: " );
35+ Serial.println (devices->devices [i].id );
36+ Serial.print (" devices type: " );
37+ Serial.println (devices->devices [i].device_type );
38+ }
39+ }
40+
41+ void setup ()
42+ {
43+ Serial.begin (115200 );
44+ Serial.println (" M5Chain Joystick Test" );
45+ M5Chain.begin (&Serial2, 115200 , RXD_PIN, TXD_PIN);
46+
47+ if (M5Chain.isDeviceConnected ()) {
48+ Serial.println (" devices is connected" );
49+ chain_status = M5Chain.getDeviceNum (&device_nums);
50+ if (chain_status == CHAIN_OK) {
51+ devices_list = (device_list_t *)malloc (sizeof (device_list_t ));
52+ devices_list->count = device_nums;
53+ devices_list->devices = (device_info_t *)malloc (sizeof (device_info_t ) * device_nums);
54+ if (M5Chain.getDeviceList (devices_list)) {
55+ Serial.println (" get devices list success" );
56+ printDeviceList (devices_list);
57+ } else {
58+ Serial.println (" get devices list failed" );
59+ }
60+ } else {
61+ Serial.printf (" error status:%d \r\n " , chain_status);
62+ Serial.printf (" devices num get failed.\r\n " );
63+ }
64+ } else {
65+ Serial.println (" devices is not connected." );
66+ }
67+
68+ if (devices_list) {
69+ for (uint8_t i = 0 ; i < devices_list->count ; i++) {
70+ if (devices_list->devices [i].device_type == CHAIN_JOYSTICK_TYPE_CODE) {
71+ chain_status = M5Chain.setRGBLight (devices_list->devices [i].id , 100 , &operation_status);
72+ if (chain_status == CHAIN_OK && operation_status) {
73+ Serial.printf (" ID[%d] set rgb light success\r\n " , devices_list->devices [i].id );
74+ } else {
75+ Serial.printf (" ID[%d] set rgb light failed, chain_status:%d operation_status:%d \r\n " ,
76+ devices_list->devices [i].id , chain_status, operation_status);
77+ }
78+ for (uint8_t j = 0 ; j < 5 ; j++) {
79+ uint8_t rgb[3 ] = {0 };
80+ chain_status =
81+ M5Chain.setRGBValue (devices_list->devices [i].id , 0 , 1 , rgb_test[j], 3 , &operation_status);
82+ if (chain_status == CHAIN_OK && operation_status) {
83+ Serial.printf (" ID[%d] set rgb %d %d %d success\r\n " , devices_list->devices [i].id ,
84+ rgb_test[j][0 ], rgb_test[j][1 ], rgb_test[j][2 ]);
85+ } else {
86+ Serial.printf (" ID[%d] set rgb %d %d %d failed, chain_status:%d operation_status:%d \r\n " ,
87+ devices_list->devices [i].id , rgb_test[j][0 ], rgb_test[j][1 ], rgb_test[j][2 ],
88+ chain_status, operation_status);
89+ }
90+ chain_status = M5Chain.getRGBValue (devices_list->devices [i].id , 0 , 1 , rgb, 3 , &operation_status);
91+ if (chain_status == CHAIN_OK && operation_status) {
92+ Serial.printf (" ID[%d] get rgb %d %d %d success \r\n " , devices_list->devices [i].id , rgb[0 ],
93+ rgb[1 ], rgb[2 ]);
94+ } else {
95+ Serial.printf (" ID[%d] get rgb %d %d %d failed, chain_status:%d operation_status:%d \r\n " ,
96+ devices_list->devices [i].id , rgb[0 ], rgb[1 ], rgb[2 ], chain_status,
97+ operation_status);
98+ }
99+ delay (500 );
100+ }
101+ chain_status =
102+ M5Chain.setKeyButtonTriggerInterval (devices_list->devices [i].id , BUTTON_DOUBLE_CLICK_TIME_200MS,
103+ BUTTON_LONG_PRESS_TIME_3S, &operation_status);
104+ if (chain_status == CHAIN_OK && operation_status) {
105+ Serial.printf (" JOYSTICK ID[%d] set key button trigger interval success\r\n " ,
106+ devices_list->devices [i].id );
107+ } else {
108+ Serial.printf (
109+ " JOYSTICK ID[%d] set key button trigger interval failed, chain_status:%d operation_status:%d "
110+ " \r\n " ,
111+ devices_list->devices [i].id , chain_status, operation_status);
112+ }
113+ chain_status =
114+ M5Chain.setKeyButtonMode (devices_list->devices [i].id , CHAIN_BUTTON_REPORT_MODE, &operation_status);
115+ if (chain_status == CHAIN_OK && operation_status) {
116+ Serial.printf (" JOYSTICK ID[%d] set key button mode success\r\n " , devices_list->devices [i].id );
117+ } else {
118+ Serial.printf (
119+ " JOYSTICK ID[%d] set key button mode failed, chain_status:%d operation_status:%d \r\n " ,
120+ devices_list->devices [i].id , chain_status, operation_status);
121+ }
122+ }
123+ }
124+ } else {
125+ Serial.println (" devices list is NULL" );
126+ }
127+ }
128+
129+ void loop ()
130+ {
131+ if (devices_list) {
132+ for (uint8_t i = 0 ; i < devices_list->count ; i++) {
133+ if (devices_list->devices [i].device_type == CHAIN_JOYSTICK_TYPE_CODE) {
134+ uint16_t xAdcValue = 0 ;
135+ uint16_t yAdcValue = 0 ;
136+ uint8_t xAdcValue8 = 0 ;
137+ uint8_t yAdcValue8 = 0 ;
138+ uint16_t mapRange[8 ];
139+ int16_t xMapAdcValue = 0 ;
140+ int16_t yMapAdcValue = 0 ;
141+ int8_t xMapAdcValue8 = 0 ;
142+ int8_t yMapAdcValue8 = 0 ;
143+ uint8_t button_status = 0 ;
144+ button_double_click_time_t button_double_click_time;
145+ button_long_press_time_t button_long_press_time;
146+ chain_button_press_type_t button_press_type;
147+ chain_button_mode_t button_mode;
148+ chain_status = M5Chain.getJoystick16Adc (devices_list->devices [i].id , &xAdcValue, &yAdcValue);
149+ if (chain_status == CHAIN_OK) {
150+ Serial.printf (" JOYSTICK ID[%d] xAdcValue:%d, yAdcValue:%d \r\n " , devices_list->devices [i].id , xAdcValue, yAdcValue);
151+ } else {
152+ Serial.printf (" JOYSTICK ID[%d] get 16 adc value failed, chain_status:%d \r\n " , devices_list->devices [i].id , chain_status);
153+ }
154+ chain_status = M5Chain.getJoystick8Adc (devices_list->devices [i].id , &xAdcValue8, &yAdcValue8);
155+ if (chain_status == CHAIN_OK) {
156+ Serial.printf (" JOYSTICK ID[%d] xAdcValue8:%d, yAdcValue8:%d \r\n " , devices_list->devices [i].id , xAdcValue8, yAdcValue8);
157+ } else {
158+ Serial.printf (" JOYSTICK ID[%d] get 8 adc value failed, chain_status:%d \r\n " , devices_list->devices [i].id , chain_status);
159+ }
160+
161+ chain_status = M5Chain.getJoystickMappedRange (devices_list->devices [i].id , mapRange, JOYSTICK_MAP_SIZE);
162+ if (chain_status == CHAIN_OK) {
163+ Serial.printf (" JOYSTICK ID[%d] mapRange:%d %d %d %d %d %d %d %d \r\n " , devices_list->devices [i].id , mapRange[0 ], mapRange[1 ], mapRange[2 ], mapRange[3 ], mapRange[4 ], mapRange[5 ], mapRange[6 ], mapRange[7 ]);
164+ } else {
165+ Serial.printf (" JOYSTICK ID[%d] get 16 adc map range failed, chain_status:%d \r\n " , devices_list->devices [i].id , chain_status);
166+ }
167+ chain_status = M5Chain.getJoystickMappedInt16Value (devices_list->devices [i].id , &xMapAdcValue, &yMapAdcValue);
168+ if (chain_status == CHAIN_OK) {
169+ Serial.printf (" JOYSTICK ID[%d] xMapAdcValue:%d, yMapAdcValue:%d \r\n " , devices_list->devices [i].id , xMapAdcValue, yMapAdcValue);
170+ } else {
171+ Serial.printf (" JOYSTICK ID[%d] get 16 adc map value failed, chain_status:%d \r\n " , devices_list->devices [i].id , chain_status);
172+ }
173+ chain_status = M5Chain.getJoystickMappedInt8Value (devices_list->devices [i].id , &xMapAdcValue8, &yMapAdcValue8);
174+ if (chain_status == CHAIN_OK) {
175+ Serial.printf (" JOYSTICK ID[%d] xMapAdcValue8:%d, yMapAdcValue8:%d \r\n " , devices_list->devices [i].id , xMapAdcValue8, yMapAdcValue8);
176+ } else {
177+ Serial.printf (" JOYSTICK ID[%d] get 8 adc map value failed, chain_status:%d \r\n " , devices_list->devices [i].id , chain_status);
178+ }
179+
180+ chain_status = M5Chain.getJoystickButtonStatus (devices_list->devices [i].id , &button_status);
181+ if (chain_status == CHAIN_OK) {
182+ Serial.printf (" JOYSTICK ID[%d] button status:%d \r\n " , devices_list->devices [i].id , button_status);
183+ } else {
184+ Serial.printf (" JOYSTICK ID[%d] get button status failed, chain_status:%d \r\n " ,
185+ devices_list->devices [i].id , chain_status);
186+ }
187+ chain_status = M5Chain.getJoystickButtonTriggerInterval (
188+ devices_list->devices [i].id , &button_double_click_time, &button_long_press_time);
189+ if (chain_status == CHAIN_OK) {
190+ Serial.printf (" JOYSTICK ID[%d] button double click time:%d, button long press time:%d \r\n " ,
191+ devices_list->devices [i].id , button_double_click_time, button_long_press_time);
192+ } else {
193+ Serial.printf (" JOYSTICK ID[%d] get button trigger interval failed, chain_status:%d \r\n " ,
194+ devices_list->devices [i].id , chain_status);
195+ }
196+ chain_status = M5Chain.getJoystickButtonMode (devices_list->devices [i].id , &button_mode);
197+ if (chain_status == CHAIN_OK) {
198+ Serial.printf (" JOYSTICK ID[%d] button mode:%d \r\n " , devices_list->devices [i].id , button_mode);
199+ } else {
200+ Serial.printf (" JOYSTICK ID[%d] get button mode failed, chain_status:%d \r\n " ,
201+ devices_list->devices [i].id , chain_status);
202+ }
203+ while (M5Chain.getJoystickButtonPressStatus (devices_list->devices [i].id , &button_press_type)) {
204+ switch (button_press_type) {
205+ case CHAIN_BUTTON_PRESS_SINGLE:
206+ Serial.printf (" JOYSTICK ID[%d] button press type: single \r\n " ,
207+ devices_list->devices [i].id );
208+ break ;
209+ case CHAIN_BUTTON_PRESS_DOUBLE:
210+ Serial.printf (" JOYSTICK ID[%d] button press type: double \r\n " ,
211+ devices_list->devices [i].id );
212+ break ;
213+ case CHAIN_BUTTON_PRESS_LONG:
214+ Serial.printf (" JOYSTICK ID[%d] button press type: long \r\n " , devices_list->devices [i].id );
215+ break ;
216+ }
217+ }
218+ }
219+ }
220+ }
221+ delay (100 );
222+ }
0 commit comments