Skip to content

Commit e1aa90c

Browse files
committed
update button example
1 parent 16dcb61 commit e1aa90c

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

examples/Basic/button/button.ino

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
2+
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
33
*
44
* SPDX-License-Identifier: MIT
55
*/
66

77
/*
88
* @file button.ino
99
* @author tinyu (tinyu@m5stack.com)
10-
* @brief M5CoreS3 Touch Test
11-
* @version 0.1
12-
* @date 2024-06-07
10+
* @brief M5CoreS3 Touch Button / PWR Button Test
11+
* @version 0.2
12+
* @date 2025-03-07
1313
*
1414
*
1515
* @Hardwares: M5CoreS3
16-
* @Platform Version: Arduino M5Stack Board Manager v2.0.9
16+
* @Platform Version: Arduino M5Stack Board Manager v2.1.3
1717
* @Dependent Library:
1818
* M5GFX: https://github.com/m5stack/M5GFX
1919
* M5Unified: https://github.com/m5stack/M5Unified
@@ -24,7 +24,8 @@
2424

2525
static m5::touch_state_t prev_state;
2626

27-
void setup(void) {
27+
void setup(void)
28+
{
2829
auto cfg = M5.config();
2930
CoreS3.begin(cfg);
3031

@@ -33,42 +34,36 @@ void setup(void) {
3334
CoreS3.Display.setFont(&fonts::Orbitron_Light_24);
3435
CoreS3.Display.setTextSize(1);
3536

36-
CoreS3.Display.drawString("Touch Button Test", CoreS3.Display.width() / 2,
37-
15);
38-
CoreS3.Display.fillRect(0, CoreS3.Display.height() - 40,
39-
CoreS3.Display.width() / 3, 40, WHITE);
40-
CoreS3.Display.fillRect(CoreS3.Display.width() / 3,
41-
CoreS3.Display.height() - 40,
42-
CoreS3.Display.width() / 3, 40, GREEN);
43-
CoreS3.Display.fillRect((CoreS3.Display.width() / 3) * 2,
44-
CoreS3.Display.height() - 40,
45-
CoreS3.Display.width() / 3, 40, YELLOW);
46-
CoreS3.Display.drawString("Btn A Btn B Btn C",
47-
CoreS3.Display.width() / 2,
48-
CoreS3.Display.height() - 20);
37+
CoreS3.Display.drawString("Touch Button Test", CoreS3.Display.width() / 2, 15);
38+
CoreS3.Display.fillRect(0, CoreS3.Display.height() - 40, CoreS3.Display.width() / 3, 40, WHITE);
39+
CoreS3.Display.fillRect(CoreS3.Display.width() / 3, CoreS3.Display.height() - 40, CoreS3.Display.width() / 3, 40,
40+
GREEN);
41+
CoreS3.Display.fillRect((CoreS3.Display.width() / 3) * 2, CoreS3.Display.height() - 40, CoreS3.Display.width() / 3,
42+
40, YELLOW);
43+
CoreS3.Display.drawString("Btn A Btn B Btn C", CoreS3.Display.width() / 2, CoreS3.Display.height() - 20);
4944
}
5045

51-
void loop(void) {
46+
void loop(void)
47+
{
5248
CoreS3.update();
5349
auto touchPoint = CoreS3.Touch.getDetail();
5450
if (prev_state != touchPoint.state) {
5551
prev_state = touchPoint.state;
5652
}
57-
if ((CoreS3.Display.height() > touchPoint.y &&
58-
touchPoint.y > CoreS3.Display.height() - 40) &&
53+
if ((CoreS3.Display.height() > touchPoint.y && touchPoint.y > CoreS3.Display.height() - 40) &&
5954
touchPoint.state == m5::touch_state_t::touch_begin) {
6055
CoreS3.Display.fillRect(0, 40, CoreS3.Display.width(), 70, BLACK);
6156
if (CoreS3.Display.width() / 3 > touchPoint.x && touchPoint.x > 0)
62-
CoreS3.Display.drawString("Btn A", CoreS3.Display.width() / 2,
63-
CoreS3.Display.height() / 2 - 30);
64-
if ((CoreS3.Display.width() / 3) * 2 > touchPoint.x &&
65-
touchPoint.x > CoreS3.Display.width() / 3)
66-
CoreS3.Display.drawString("Btn B", CoreS3.Display.width() / 2,
67-
CoreS3.Display.height() / 2 - 30);
68-
if (CoreS3.Display.width() > touchPoint.x &&
69-
touchPoint.x > (CoreS3.Display.width() / 3) * 2)
70-
CoreS3.Display.drawString("Btn C", CoreS3.Display.width() / 2,
71-
CoreS3.Display.height() / 2 - 30);
57+
CoreS3.Display.drawString("Btn A", CoreS3.Display.width() / 2, CoreS3.Display.height() / 2 - 30);
58+
if ((CoreS3.Display.width() / 3) * 2 > touchPoint.x && touchPoint.x > CoreS3.Display.width() / 3)
59+
CoreS3.Display.drawString("Btn B", CoreS3.Display.width() / 2, CoreS3.Display.height() / 2 - 30);
60+
if (CoreS3.Display.width() > touchPoint.x && touchPoint.x > (CoreS3.Display.width() / 3) * 2)
61+
CoreS3.Display.drawString("Btn C", CoreS3.Display.width() / 2, CoreS3.Display.height() / 2 - 30);
7262
Serial.printf("x:%d ,y:%d", touchPoint.x, touchPoint.y);
7363
}
64+
int state = M5.BtnPWR.wasClicked();
65+
if (state) {
66+
CoreS3.Display.fillRect(0, 40, CoreS3.Display.width(), 70, BLACK);
67+
CoreS3.Display.drawString("Btn PWR", CoreS3.Display.width() / 2, CoreS3.Display.height() / 2 - 30);
68+
}
7469
}

0 commit comments

Comments
 (0)