瀏覽代碼

Moved JC_Button to libraries

master
Robin Thoni 4 年之前
父節點
當前提交
5f71eaea4f
簽署人: Robin THONI <robin@rthoni.com> GPG 金鑰 ID: 4E09DEF46B99E61E
共有 3 個檔案被更改,包括 1 行新增212 行删除
  1. 1
    1
      CMakeLists.txt
  2. 0
    100
      JC_Button.cpp
  3. 0
    111
      JC_Button.h

+ 1
- 1
CMakeLists.txt 查看文件

@@ -19,7 +19,7 @@ set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyUSB3)
19 19
 
20 20
 
21 21
 enable_language(ASM)
22
-set(${CMAKE_PROJECT_NAME}_ALL_SRCS main.ino JC_Button.cpp Storage.cpp AppCore.cpp)
22
+set(${CMAKE_PROJECT_NAME}_ALL_SRCS main.ino Storage.cpp AppCore.cpp)
23 23
 set(${CMAKE_PROJECT_NAME}_SKETCH main.ino)
24 24
 generate_arduino_firmware(${CMAKE_PROJECT_NAME})
25 25
 

+ 0
- 100
JC_Button.cpp 查看文件

@@ -1,100 +0,0 @@
1
-// Arduino Button Library
2
-// https://github.com/JChristensen/JC_Button
3
-// Copyright (C) 2018 by Jack Christensen and licensed under
4
-// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
5
-
6
-#include "JC_Button.h"
7
-
8
-/*----------------------------------------------------------------------*
9
-/ initialize a Button object and the pin it's connected to.             *
10
-/-----------------------------------------------------------------------*/
11
-void Button::begin()
12
-{
13
-    pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT);
14
-    m_state = digitalRead(m_pin);
15
-    if (m_invert) m_state = !m_state;
16
-    m_time = millis();
17
-    m_lastState = m_state;
18
-    m_changed = false;
19
-    m_lastChange = m_time;
20
-}
21
-
22
-/*----------------------------------------------------------------------*
23
-/ returns the state of the button, true if pressed, false if released.  *
24
-/ does debouncing, captures and maintains times, previous state, etc.   *
25
-/-----------------------------------------------------------------------*/
26
-bool Button::read()
27
-{
28
-    uint32_t ms = millis();
29
-    bool pinVal = digitalRead(m_pin);
30
-    if (m_invert) pinVal = !pinVal;
31
-    if (ms - m_lastChange < m_dbTime)
32
-    {
33
-        m_changed = false;
34
-    }
35
-    else
36
-    {
37
-        m_lastState = m_state;
38
-        m_state = pinVal;
39
-        m_changed = (m_state != m_lastState);
40
-        if (m_changed) m_lastChange = ms;
41
-    }
42
-    m_time = ms;
43
-    return m_state;
44
-}
45
-
46
-/*----------------------------------------------------------------------*
47
- * isPressed() and isReleased() check the button state when it was last *
48
- * read, and return false (0) or true (!=0) accordingly.                *
49
- * These functions do not cause the button to be read.                  *
50
- *----------------------------------------------------------------------*/
51
-bool Button::isPressed()
52
-{
53
-    return m_state;
54
-}
55
-
56
-bool Button::isReleased()
57
-{
58
-    return !m_state;
59
-}
60
-
61
-/*----------------------------------------------------------------------*
62
- * wasPressed() and wasReleased() check the button state to see if it   *
63
- * changed between the last two reads and return false (0) or           *
64
- * true (!=0) accordingly.                                              *
65
- * These functions do not cause the button to be read.                  *
66
- *----------------------------------------------------------------------*/
67
-bool Button::wasPressed()
68
-{
69
-    return m_state && m_changed;
70
-}
71
-
72
-bool Button::wasReleased()
73
-{
74
-    return !m_state && m_changed;
75
-}
76
-
77
-/*----------------------------------------------------------------------*
78
- * pressedFor(ms) and releasedFor(ms) check to see if the button is     *
79
- * pressed (or released), and has been in that state for the specified  *
80
- * time in milliseconds. Returns false (0) or true (!=0) accordingly.   *
81
- * These functions do not cause the button to be read.                  *
82
- *----------------------------------------------------------------------*/
83
-bool Button::pressedFor(uint32_t ms)
84
-{
85
-    return m_state && m_time - m_lastChange >= ms;
86
-}
87
-
88
-bool Button::releasedFor(uint32_t ms)
89
-{
90
-    return !m_state && m_time - m_lastChange >= ms;
91
-}
92
-
93
-/*----------------------------------------------------------------------*
94
- * lastChange() returns the time the button last changed state,         *
95
- * in milliseconds.                                                     *
96
- *----------------------------------------------------------------------*/
97
-uint32_t Button::lastChange()
98
-{
99
-    return m_lastChange;
100
-}

+ 0
- 111
JC_Button.h 查看文件

@@ -1,111 +0,0 @@
1
-// Arduino Button Library
2
-// https://github.com/JChristensen/JC_Button
3
-// Copyright (C) 2018 by Jack Christensen and licensed under
4
-// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
5
-
6
-#ifndef JC_BUTTON_H_INCLUDED
7
-#define JC_BUTTON_H_INCLUDED
8
-
9
-#include <Arduino.h>
10
-
11
-class Button
12
-{
13
-public:
14
-    // Button(pin, dbTime, puEnable, invert) instantiates a button object.
15
-    //
16
-    // Required parameter:
17
-    // pin      The Arduino pin the button is connected to
18
-    //
19
-    // Optional parameters:
20
-    // dbTime   Debounce time in milliseconds (default 25ms)
21
-    // puEnable true to enable the AVR internal pullup resistor (default true)
22
-    // invert   true to interpret a low logic level as pressed (default true)
23
-    Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
24
-            : m_pin(pin), m_dbTime(dbTime), m_puEnable(puEnable), m_invert(invert) {}
25
-
26
-    // Initialize a Button object and the pin it's connected to
27
-    void begin();
28
-
29
-    // Returns the current debounced button state, true for pressed,
30
-    // false for released. Call this function frequently to ensure
31
-    // the sketch is responsive to user input.
32
-    bool read();
33
-
34
-    // Returns true if the button state was pressed at the last call to read().
35
-    // Does not cause the button to be read.
36
-    bool isPressed();
37
-
38
-    // Returns true if the button state was released at the last call to read().
39
-    // Does not cause the button to be read.
40
-    bool isReleased();
41
-
42
-    // Returns true if the button state at the last call to read() was pressed,
43
-    // and this was a change since the previous read.
44
-    bool wasPressed();
45
-
46
-    // Returns true if the button state at the last call to read() was released,
47
-    // and this was a change since the previous read.
48
-    bool wasReleased();
49
-
50
-    // Returns true if the button state at the last call to read() was pressed,
51
-    // and has been in that state for at least the given number of milliseconds.
52
-    bool pressedFor(uint32_t ms);
53
-
54
-    // Returns true if the button state at the last call to read() was released,
55
-    // and has been in that state for at least the given number of milliseconds.
56
-    bool releasedFor(uint32_t ms);
57
-
58
-    // Returns the time in milliseconds (from millis) that the button last
59
-    // changed state.
60
-    uint32_t lastChange();
61
-
62
-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)
72
-};
73
-
74
-// a derived class for a "push-on, push-off" (toggle) type button.
75
-// initial state can be given, default is off (false).
76
-class ToggleButton : public Button
77
-{
78
-public:
79
-
80
-    // constructor is similar to Button, but includes the initial state for the toggle.
81
-    ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
82
-            : Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {}
83
-
84
-    // read the button and return its state.
85
-    // should be called frequently.
86
-    bool read()
87
-    {
88
-        Button::read();
89
-        if (wasPressed())
90
-        {
91
-            m_toggleState = !m_toggleState;
92
-            m_changed = true;
93
-        }
94
-        else
95
-        {
96
-            m_changed = false;
97
-        }
98
-        return m_toggleState;
99
-    }
100
-
101
-    // has the state changed?
102
-    bool changed() {return m_changed;}
103
-
104
-    // return the current state
105
-    bool toggleState() {return m_toggleState;}
106
-
107
-private:
108
-    bool m_toggleState;
109
-    bool m_changed;
110
-};
111
-#endif

Loading…
取消
儲存