Skip to content

Commit e82ed02

Browse files
committed
Merge branch 'develop'
2 parents 5c1a751 + 355665d commit e82ed02

12 files changed

Lines changed: 307 additions & 14 deletions
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
name: Arduino Lint Check
22
on:
33
push:
4-
branches: [ master ]
4+
branches: [ master, main ]
55
pull_request:
6-
branches: [ master ]
6+
branches: [ master, main ]
7+
workflow_dispatch:
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
717
jobs:
818
lint:
919
name: Lint Check
1020
runs-on: ubuntu-latest
1121
steps:
12-
- uses: actions/checkout@v2
13-
- uses: arduino/arduino-lint-action@v1
22+
- uses: actions/checkout@v4
23+
- uses: arduino/arduino-lint-action@v2
1424
with:
1525
library-manager: update
1626
compliance: strict
17-
project-type: all
27+
project-type: all

.github/workflows/clang-format-check.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ on:
3131
- '**.clang-format'
3232
workflow_dispatch:
3333

34+
defaults:
35+
run:
36+
shell: bash
37+
38+
concurrency:
39+
group: ${{ github.workflow }}-${{ github.ref }}
40+
cancel-in-progress: true
41+
3442
jobs:
3543
formatting-check:
3644
name: Formatting Check

.github/workflows/doxygen-gh-pages.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ on: [release, workflow_dispatch]
33
# branches:
44
# - main
55
# - master
6+
defaults:
7+
run:
8+
shell: bash
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
614
jobs:
715
deploy:
816
runs-on: ubuntu-latest

.github/workflows/platformio-build-check.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ on:
1616
- 'test/**.hpp'
1717
- 'test/**.h'
1818
- 'test/**.c'
19-
- '**platformio-build-check.yml'
20-
- '**platformio.ini'
19+
- '**/platformio-build-check.yml'
20+
- '**/platformio.ini'
2121
pull_request:
2222
paths:
2323
- 'src/unit/**.cpp'
@@ -28,8 +28,8 @@ on:
2828
- 'test/**.hpp'
2929
- 'test/**.h'
3030
- 'test/**.c'
31-
- '**platformio-build-check.yml'
32-
- '**platformio.ini'
31+
- '**/platformio-build-check.yml'
32+
- '**/platformio.ini'
3333
workflow_dispatch:
3434

3535
jobs:

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "https://github.com/m5stack/M5Utility.git"
1212
},
1313
"dependencies": [],
14-
"version": "0.0.2",
14+
"version": "0.0.3",
1515
"frameworks": [
1616
"arduino"
1717
],

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5Utility
2-
version=0.0.2
2+
version=0.0.3
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=Library for other M5 libraries and products

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ extends = m5base
8181
board = m5stack-nanoc6
8282
platform = https://github.com/platformio/platform-espressif32.git
8383
platform_packages =
84-
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git
84+
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.7
8585
platformio/framework-arduinoespressif32-libs @ https://github.com/espressif/esp32-arduino-libs.git#idf-release/v5.1
8686
board_build.partitions = default.csv
8787
lib_deps = ${env.lib_deps}

src/M5Utility.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "m5_utility/string.hpp"
3131
#include "m5_utility/conversion.hpp"
3232
#include "m5_utility/math.hpp"
33+
#include "m5_utility/button_status.hpp"
3334
#include "m5_utility/misc.hpp"
3435

3536
/*!

src/m5_utility/button_status.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
/*!
7+
@file button_status.cpp
8+
@brief Button status management
9+
*/
10+
11+
#include "button_status.hpp"
12+
13+
namespace m5 {
14+
namespace utility {
15+
namespace button {
16+
17+
void Status::setState(const uint32_t msec, const button_state_t state)
18+
{
19+
if (_currentState == button_state_t::state_decide_click_count) {
20+
_clickCount = 0;
21+
}
22+
23+
_lastMsec = msec;
24+
bool flg_timeout = (msec - _lastClicked > _msecHold);
25+
auto new_state = state;
26+
switch (state) {
27+
case button_state_t::state_nochange:
28+
if (flg_timeout && !_press && _clickCount) {
29+
if (_oldPress == 0 && _currentState == button_state_t::state_nochange) {
30+
new_state = button_state_t::state_decide_click_count;
31+
} else {
32+
_clickCount = 0;
33+
}
34+
}
35+
break;
36+
37+
case button_state_t::state_clicked:
38+
++_clickCount;
39+
_lastClicked = msec;
40+
break;
41+
42+
default:
43+
break;
44+
}
45+
_currentState = new_state;
46+
}
47+
48+
void Status::setRawState(const uint32_t msec, const bool press)
49+
{
50+
button_state_t state = button_state_t::state_nochange;
51+
bool disable_db = (msec - _lastMsec) > _msecDebounce;
52+
auto oldPress = _press;
53+
_oldPress = oldPress;
54+
if (_raw_press != press) {
55+
_raw_press = press;
56+
_lastRawChange = msec;
57+
}
58+
if (disable_db || msec - _lastRawChange >= _msecDebounce) {
59+
if (press != (0 != oldPress)) {
60+
_lastChange = msec;
61+
}
62+
63+
if (press) {
64+
uint32_t holdPeriod = msec - _lastChange;
65+
_lastHoldPeriod = holdPeriod;
66+
if (!oldPress) {
67+
_press = 1;
68+
} else if (oldPress == 1 && (holdPeriod >= _msecHold)) {
69+
_press = 2;
70+
state = button_state_t::state_hold;
71+
}
72+
} else {
73+
_press = 0;
74+
if (oldPress == 1) {
75+
state = button_state_t::state_clicked;
76+
}
77+
}
78+
}
79+
setState(msec, state);
80+
}
81+
82+
} // namespace button
83+
} // namespace utility
84+
} // namespace m5

src/m5_utility/button_status.hpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
/*!
7+
@file button_status.hpp
8+
@brief Button status management
9+
*/
10+
#ifndef M5_UTILITY_BUTTON_STATUS_HPP
11+
#define M5_UTILITY_BUTTON_STATUS_HPP
12+
13+
#include <cstdint>
14+
15+
namespace m5 {
16+
namespace utility {
17+
namespace button {
18+
19+
/*!
20+
@class m5::utility::button::Status
21+
@brief Button status management
22+
@note Class compatible with Button_Class in M5Unified
23+
*/
24+
class Status {
25+
public:
26+
/*!
27+
@enum button_state_t
28+
@brief Button status
29+
*/
30+
enum class button_state_t : uint8_t { state_nochange, state_clicked, state_hold, state_decide_click_count };
31+
32+
/*!
33+
@brief Constructor
34+
@param hold_ms Time to be considered hold(ms)
35+
@param debounce_ms Debounce time(ms)
36+
*/
37+
Status(const uint16_t hold_ms = 500, const uint16_t debounce_ms = 10)
38+
: _msecHold{hold_ms}, _msecDebounce{debounce_ms}
39+
{
40+
}
41+
42+
///@name Settings
43+
///@{
44+
//! @brief Set debounce time(ms)
45+
inline void setDebounceThreshold(const uint32_t msec)
46+
{
47+
_msecDebounce = msec;
48+
}
49+
//! @brief Set time to be considered hold(ms)
50+
inline void setHoldThreshold(const uint32_t msec)
51+
{
52+
_msecHold = msec;
53+
}
54+
//! @brief Gets the debounce time(ms)
55+
inline uint32_t getDebounceThreshold(void) const
56+
{
57+
return _msecDebounce;
58+
}
59+
//! @brief Gets the time to be considered hold(ms)
60+
inline uint32_t getHoldThreshold(void) const
61+
{
62+
return _msecHold;
63+
}
64+
///@}
65+
66+
///@name Button status
67+
///@{
68+
//! @brief Is pressed?
69+
bool isPressed(void) const
70+
{
71+
return _press;
72+
}
73+
//! @brief Is released?
74+
bool isReleased(void) const
75+
{
76+
return !_press;
77+
}
78+
//! @brief Returns true if the button is currently held pressed
79+
bool isHolding(void) const
80+
{
81+
return _press == 2;
82+
}
83+
//! @brief Returns true if button was pressed
84+
bool wasPressed(void) const
85+
{
86+
return !_oldPress && _press;
87+
}
88+
//! @brief Returns true if button was released
89+
bool wasReleased(void) const
90+
{
91+
return _oldPress && !_press;
92+
}
93+
//! @brief Returns true when the button is pressed briefly and released
94+
bool wasClicked(void) const
95+
{
96+
return _currentState == button_state_t::state_clicked;
97+
}
98+
//! @brief Returns true when the button has been held pressed for a while
99+
bool wasHold(void) const
100+
{
101+
return _currentState == button_state_t::state_hold;
102+
}
103+
//! @brief Returns true when some time has passed since the button was single clicked
104+
bool wasSingleClicked(void) const
105+
{
106+
return _currentState == button_state_t::state_decide_click_count && _clickCount == 1;
107+
}
108+
//! @brief Returns true when some time has passed since the button was double clicked
109+
bool wasDoubleClicked(void) const
110+
{
111+
return _currentState == button_state_t::state_decide_click_count && _clickCount == 2;
112+
}
113+
//! @brief Returns true when some time has passed since the button was multiple clicked
114+
bool wasDecideClickCount(void) const
115+
{
116+
return _currentState == button_state_t::state_decide_click_count;
117+
}
118+
//! @brief Gets the number of consecutive button clicks
119+
uint8_t getClickCount(void) const
120+
{
121+
return _clickCount;
122+
}
123+
//! @brief Has the button press state changed?
124+
bool wasChangePressed(void) const
125+
{
126+
return ((bool)_press) != ((bool)_oldPress);
127+
}
128+
//! @brief Pressed and released a button for more than the set hold time?
129+
bool wasReleasedAfterHold(void) const
130+
{
131+
return !_press && _oldPress == 2;
132+
}
133+
//! @brief Was it pressed for more than the specified time?
134+
bool wasReleaseFor(const uint32_t ms) const
135+
{
136+
return _oldPress && !_press && _lastHoldPeriod >= ms;
137+
}
138+
//! @brief Is pressed for more than the specified time?
139+
bool pressedFor(const uint32_t ms) const
140+
{
141+
return (_press && _lastMsec - _lastChange >= ms);
142+
}
143+
//! @brief Is released for more than the specified time?
144+
bool releasedFor(const uint32_t ms) const
145+
{
146+
return (!_press && _lastMsec - _lastChange >= ms);
147+
}
148+
///@}
149+
150+
///@name Status
151+
///@{
152+
void setRawState(const uint32_t msec, const bool press);
153+
void setState(const uint32_t msec, const button_state_t state);
154+
inline button_state_t getState(void) const
155+
{
156+
return _currentState;
157+
}
158+
inline uint32_t lastChange(void) const
159+
{
160+
return _lastChange;
161+
}
162+
inline uint32_t getUpdateMsec(void) const
163+
{
164+
return _lastMsec;
165+
}
166+
///@}
167+
168+
private:
169+
uint16_t _msecHold{500}, _msecDebounce{10};
170+
uint32_t _lastMsec{}, _lastChange{}, _lastRawChange{}, _lastClicked{};
171+
uint16_t _lastHoldPeriod{};
172+
button_state_t _currentState{button_state_t::state_nochange};
173+
bool _raw_press{};
174+
// 0:release 1:click 2:holding
175+
uint8_t _press{}, _oldPress{}, _clickCount{};
176+
};
177+
178+
} // namespace button
179+
} // namespace utility
180+
} // namespace m5
181+
182+
#endif

0 commit comments

Comments
 (0)