Skip to content

Commit 4f2fa5a

Browse files
committed
update some example
1 parent f9e87b6 commit 4f2fa5a

File tree

5 files changed

+253
-232
lines changed

5 files changed

+253
-232
lines changed

examples/Modules/4RELAY/4RELAY.ino

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,43 @@
33
* Copyright (c) 2022 by M5Stack
44
* Equipped with M5Core sample source code
55
* 配套 M5Core 示例源代码
6-
* Visit the website for more
7-
*information:https://docs.m5stack.com/en/module/4relay
8-
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/module/4relay
6+
* Visit for more information: https://docs.m5stack.com/en/module/4relay
7+
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/module/4relay
98
*
109
* describe: Module 4Relay.
11-
* date2022/04/07
10+
* date: 2022/04/07
1211
*******************************************************************************
1312
*/
1413

14+
#include <M5GFX.h>
1515
#include <M5Stack.h>
16+
1617
#include "MODULE_4RELAY.h"
17-
#include <M5GFX.h>
1818

1919
M5GFX display;
2020
M5Canvas canvas(&display);
2121

2222
MODULE_4RELAY RELAY;
2323

2424
void drwaRect() {
25-
canvas.fillSprite(0);
26-
canvas.drawString("Module 4Relay", 160, 20);
27-
canvas.drawString("A REVERSE", 220, 80);
28-
canvas.drawString("B STEP", 220, 120);
29-
canvas.drawString("C RUNING", 220, 160);
25+
canvas.fillSprite(0);
26+
canvas.drawString("Module 4Relay", 160, 20);
27+
canvas.drawString("A REVERSE", 220, 80);
28+
canvas.drawString("B STEP", 220, 120);
29+
canvas.drawString("C RUNING", 220, 160);
3030

31-
uint8_t state = RELAY.getAllRelayState();
32-
Serial.println(state);
33-
for(uint8_t i=0; i<4; i++){
34-
if(((state>>i) & 0x01) == 1){
35-
canvas.fillRect(20, 50 + 50*i, 100, 20);
36-
}else{
37-
canvas.drawRect(20, 50 + 50*i, 100, 20);
31+
uint8_t state = RELAY.getAllRelayState();
32+
Serial.println(state);
33+
for (uint8_t i = 0; i < 4; i++) {
34+
if (((state >> i) & 0x01) == 1) {
35+
canvas.fillRect(20, 50 + 50 * i, 100, 20);
36+
} else {
37+
canvas.drawRect(20, 50 + 50 * i, 100, 20);
38+
}
3839
}
39-
}
40-
canvas.pushSprite(0,0);
40+
canvas.pushSprite(0, 0);
4141
}
4242

43-
4443
void setup() {
4544
M5.begin(); // Init M5Stack. 初始化M5Stack
4645
// sensor.begin(&Wire, 0x57, 21, 22, 200000L);
@@ -52,80 +51,79 @@ void setup() {
5251
canvas.setTextDatum(MC_DATUM);
5352
canvas.createSprite(display.width(), display.height());
5453
canvas.setPaletteColor(1, ORANGE);
55-
canvas.pushSprite(0,0);
54+
canvas.pushSprite(0, 0);
5655
}
5756

5857
uint8_t mode = 0;
5958

60-
bool delayBtn(unsigned long start){
61-
while(1) {
59+
bool delayBtn(unsigned long start) {
60+
while (1) {
6261
M5.update();
63-
if(M5.BtnA.wasPressed()){
62+
if (M5.BtnA.wasPressed()) {
6463
mode = 0;
6564
return true;
6665
}
67-
if(M5.BtnB.wasPressed()){
66+
if (M5.BtnB.wasPressed()) {
6867
mode = 1;
6968
return true;
7069
}
71-
if(M5.BtnC.wasPressed()){
70+
if (M5.BtnC.wasPressed()) {
7271
mode = 2;
7372
return true;
7473
}
75-
if((millis() - start) > 500){
74+
if ((millis() - start) > 500) {
7675
return false;
7776
}
7877
}
7978
}
8079

8180
void loop() {
82-
switch (mode)
83-
{
84-
case 0:
85-
RELAY.setAllRelay(true);
86-
drwaRect();
87-
if(delayBtn(millis())){
88-
RELAY.setAllRelay(false);
89-
break;
90-
};
91-
RELAY.setAllRelay(false);
92-
drwaRect();
93-
if(delayBtn(millis())){
94-
RELAY.setAllRelay(false);
95-
break;
96-
};
97-
break;
98-
case 1:
99-
for (uint8_t i = 0; i < 4; i++) {
100-
while(!delayBtn(millis()));
101-
if(mode != 1) break;
102-
RELAY.setRelay(i, true);
81+
switch (mode) {
82+
case 0:
83+
RELAY.setAllRelay(true);
10384
drwaRect();
104-
105-
}
106-
for (uint8_t i = 0; i < 4; i++) {
107-
while(!delayBtn(millis()));
108-
if(mode != 1) break;
109-
RELAY.setRelay(i, false);
110-
drwaRect();
111-
}
112-
break;
113-
case 2:
114-
for (uint8_t i = 0; i < 4; i++) {
115-
RELAY.setRelay(i, true);
116-
drwaRect();
117-
if(delayBtn(millis())){
85+
if (delayBtn(millis())) {
86+
RELAY.setAllRelay(false);
11887
break;
11988
};
120-
}
121-
for (uint8_t i = 0; i < 4; i++) {
122-
RELAY.setRelay(i, false);
89+
RELAY.setAllRelay(false);
12390
drwaRect();
124-
if(delayBtn(millis())){
91+
if (delayBtn(millis())) {
92+
RELAY.setAllRelay(false);
12593
break;
12694
};
127-
}
128-
break;
95+
break;
96+
case 1:
97+
for (uint8_t i = 0; i < 4; i++) {
98+
while (!delayBtn(millis()))
99+
;
100+
if (mode != 1) break;
101+
RELAY.setRelay(i, true);
102+
drwaRect();
103+
}
104+
for (uint8_t i = 0; i < 4; i++) {
105+
while (!delayBtn(millis()))
106+
;
107+
if (mode != 1) break;
108+
RELAY.setRelay(i, false);
109+
drwaRect();
110+
}
111+
break;
112+
case 2:
113+
for (uint8_t i = 0; i < 4; i++) {
114+
RELAY.setRelay(i, true);
115+
drwaRect();
116+
if (delayBtn(millis())) {
117+
break;
118+
};
119+
}
120+
for (uint8_t i = 0; i < 4; i++) {
121+
RELAY.setRelay(i, false);
122+
drwaRect();
123+
if (delayBtn(millis())) {
124+
break;
125+
};
126+
}
127+
break;
129128
}
130129
}
131-

examples/Modules/AC-SOCKET/AC-SOCKET.ino

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,65 @@
1414
*/
1515
#include <Arduino.h>
1616
#include <M5Stack.h>
17-
#include "protocol.h"
17+
1818
#include "modbus.h"
19+
#include "protocol.h"
20+
1921
volatile uint32_t tim = 0;
2022

21-
uint32_t time_now = 0;
23+
uint32_t time_now = 0;
2224
uint8_t ucTestFlag = 0;
23-
bool ubCoilState = false;
25+
bool ubCoilState = false;
2426

2527
void setup() {
26-
M5.begin(true, false, true, false);
27-
M5.Power.begin();
28-
mb_init(0xac, 9600, &tim);
29-
Serial1.begin(9600, SERIAL_8N1, 16, 17);
30-
time_now = millis();
31-
// put your setup code here, to run once:
28+
M5.begin(true, false, true, false);
29+
M5.Power.begin();
30+
mb_init(0xac, 9600, &tim);
31+
Serial1.begin(9600, SERIAL_8N1, 16, 17);
32+
time_now = millis();
33+
// put your setup code here, to run once:
3234
}
3335

34-
3536
void loop() {
36-
M5.update();
37-
38-
if(millis() - time_now > 60000UL) {
39-
time_now = millis();
40-
if(ucTestFlag) {
41-
if(ubCoilState) {
37+
M5.update();
38+
39+
if (millis() - time_now > 60000UL) {
40+
time_now = millis();
41+
if (ucTestFlag) {
42+
if (ubCoilState) {
43+
char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
44+
mb_send_frame((uint8_t *)data_str, 6);
45+
} else {
46+
char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
47+
mb_send_frame((uint8_t *)data_str, 6);
48+
}
49+
ubCoilState = 1 - ubCoilState;
50+
}
51+
}
52+
53+
if (M5.BtnA.wasPressed()) {
4254
char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
4355
mb_send_frame((uint8_t *)data_str, 6);
44-
} else {
56+
}
57+
58+
if (M5.BtnB.wasPressed()) {
4559
char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
4660
mb_send_frame((uint8_t *)data_str, 6);
47-
}
48-
ubCoilState = 1 - ubCoilState;
4961
}
50-
}
5162

52-
if(M5.BtnA.wasPressed()) {
53-
char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
54-
mb_send_frame((uint8_t *)data_str, 6);
55-
}
56-
57-
if(M5.BtnB.wasPressed()) {
58-
char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
59-
mb_send_frame((uint8_t *)data_str, 6);
60-
}
63+
if (M5.BtnC.wasPressed()) {
64+
ucTestFlag = 1 - ucTestFlag;
65+
}
6166

62-
63-
if(M5.BtnC.wasPressed()) {
64-
ucTestFlag = 1 - ucTestFlag;
65-
}
67+
vTaskDelay(pdMS_TO_TICKS(10));
6668

67-
vTaskDelay(pdMS_TO_TICKS(10));
68-
69-
// while(Serial1.available()) {
70-
// protocol_rec_put(Serial1.read());
71-
// micros();
72-
// }
73-
// put your main code here, to run repeatedly:
69+
// while(Serial1.available()) {
70+
// protocol_rec_put(Serial1.read());
71+
// micros();
72+
// }
73+
// put your main code here, to run repeatedly:
7474
}
7575

76-
void mb_send_one_byte(uint8_t data) {
77-
Serial1.write(data);
78-
}
76+
void mb_send_one_byte(uint8_t data) { Serial1.write(data); }
7977

80-
void protocol_callback(CmdData cmd) {
81-
Serial.printf("got ... \r\n");
82-
}
78+
void protocol_callback(CmdData cmd) { Serial.printf("got ... \r\n"); }

0 commit comments

Comments
 (0)