Skip to content

Commit d736bfe

Browse files
Cleanup some cppcheck warnings.
Add const and initial values.
1 parent 20040e0 commit d736bfe

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/JC_Button.h

+19-19
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,42 @@ class Button
3333

3434
// Returns true if the button state was pressed at the last call to read().
3535
// Does not cause the button to be read.
36-
bool isPressed() {return m_state;}
36+
bool isPressed() const {return m_state;}
3737

3838
// Returns true if the button state was released at the last call to read().
3939
// Does not cause the button to be read.
40-
bool isReleased() {return !m_state;}
40+
bool isReleased() const {return !m_state;}
4141

4242
// These functions check the button state to see if it changed
4343
// between the last two reads and return true or false accordingly.
4444
// These functions do not cause the button to be read.
45-
bool wasPressed() {return m_state && m_changed;}
46-
bool wasReleased() {return !m_state && m_changed;}
45+
bool wasPressed() const {return m_state && m_changed;}
46+
bool wasReleased() const {return !m_state && m_changed;}
4747

4848
// Returns true if the button state at the last call to read() was pressed,
4949
// and has been in that state for at least the given number of milliseconds.
5050
// This function does not cause the button to be read.
51-
bool pressedFor(uint32_t ms) {return m_state && m_time - m_lastChange >= ms;}
51+
bool pressedFor(uint32_t ms) const {return m_state && m_time - m_lastChange >= ms;}
5252

5353
// Returns true if the button state at the last call to read() was released,
5454
// and has been in that state for at least the given number of milliseconds.
5555
// This function does not cause the button to be read.
56-
bool releasedFor(uint32_t ms) {return !m_state && m_time - m_lastChange >= ms;}
56+
bool releasedFor(uint32_t ms) const {return !m_state && m_time - m_lastChange >= ms;}
5757

5858
// Returns the time in milliseconds (from millis) that the button last
5959
// changed state.
60-
uint32_t lastChange() {return m_lastChange;}
60+
uint32_t lastChange() const {return m_lastChange;}
6161

6262
private:
63-
uint8_t m_pin; // arduino pin number connected to button
64-
uint32_t m_dbTime; // debounce time (ms)
65-
bool m_puEnable; // internal pullup resistor enabled
66-
bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
67-
bool m_state; // current button state, true=pressed
68-
bool m_lastState; // previous button state
69-
bool m_changed; // state changed since last read
70-
uint32_t m_time; // time of current state (ms from millis)
71-
uint32_t m_lastChange; // time of last state change (ms)
63+
uint8_t m_pin; // arduino pin number connected to button
64+
uint32_t m_dbTime; // debounce time (ms)
65+
bool m_puEnable; // internal pullup resistor enabled
66+
bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
67+
bool m_state = false; // current button state, true=pressed
68+
bool m_lastState = false; // previous button state
69+
bool m_changed = false; // state changed since last read
70+
uint32_t m_time = 0; // time of current state (ms from millis)
71+
uint32_t m_lastChange = 0; // time of last state change (ms)
7272
};
7373

7474
// a derived class for a "push-on, push-off" (toggle) type button.
@@ -97,13 +97,13 @@ class ToggleButton : public Button
9797
}
9898

9999
// has the state changed?
100-
bool changed() {return m_changed;}
100+
bool changed() const {return m_changed;}
101101

102102
// return the current state
103-
bool toggleState() {return m_toggleState;}
103+
bool toggleState() const {return m_toggleState;}
104104

105105
private:
106106
bool m_toggleState;
107-
bool m_changed;
107+
bool m_changed = false;
108108
};
109109
#endif

0 commit comments

Comments
 (0)