-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathButton.h
158 lines (138 loc) · 5.95 KB
/
Button.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef CODAL_BUTTON_H
#define CODAL_BUTTON_H
#include "AbstractButton.h"
#include "CodalConfig.h"
#include "CodalComponent.h"
#include "Event.h"
#include "Pin.h"
namespace codal
{
/**
* Class definition for Device Button.
*
* Represents a single, generic button on the device.
*/
class Button : public AbstractButton, public PinPeripheral
{
unsigned long downStartTime; // used to store the current system clock when a button down event occurs
uint8_t sigma; // integration of samples over time. We use this for debouncing, and noise tolerance for touch sensing
ButtonEventConfiguration eventConfiguration; // Do we want to generate high level event (clicks), or defer this to another service.
ButtonPolarity polarity; // Determines if the button is active HIGH or LOW.
public:
Pin &_pin; // The pin this button is connected to.
PullMode pullMode; // Determines if pull up or pull down resistors should be applied.
/**
* Constructor.
*
* Create a software representation of a button.
*
* @param pin the physical pin on the device connected to this button.
*
* @param id the ID of the new Button object.
*
* @param eventConfiguration Configures the events that will be generated by this Button instance.
* Defaults to DEVICE_BUTTON_ALL_EVENTS.
*
* @param mode the configuration of internal pullups/pulldowns, as defined in the mbed PullMode class. PullNone by default.
*
* @code
* buttonA(DEVICE_PIN_BUTTON_A, DEVICE_ID_BUTTON_A);
* @endcode
*/
Button(Pin &pin, uint16_t id, ButtonEventConfiguration eventConfiguration = DEVICE_BUTTON_ALL_EVENTS, ButtonPolarity polarity = ACTIVE_LOW, PullMode mode = PullMode::None);
/**
* Tests if this Button is currently pressed.
*
* @code
* if(buttonA.isPressed())
* display.scroll("Pressed!");
* @endcode
*
* @return 1 if this button is pressed, 0 otherwise.
*/
virtual int isPressed();
/**
* Changes the event configuration used by this button to the given ButtonEventConfiguration.
*
* All subsequent events generated by this button will then be informed by this configuraiton.
*
* @param config The new configuration for this button. Legal values are DEVICE_BUTTON_ALL_EVENTS or DEVICE_BUTTON_SIMPLE_EVENTS.
*
* Example:
* @code
* // Configure a button to generate all possible events.
* buttonA.setEventConfiguration(DEVICE_BUTTON_ALL_EVENTS);
*
* // Configure a button to suppress DEVICE_BUTTON_EVT_CLICK and DEVICE_BUTTON_EVT_LONG_CLICK events.
* buttonA.setEventConfiguration(DEVICE_BUTTON_SIMPLE_EVENTS);
* @endcode
*/
void setEventConfiguration(ButtonEventConfiguration config);
/**
* periodic callback from Device system timer.
*
* Check for state change for this button, and fires various events on a state change.
*/
void periodicCallback();
/**
* Sets whether the button should trigger power manager wake-up.
*
* @param wake The action of the button - either 1 to trigger wake-up or 0 for no wake-up
*/
void wakeOnActive(int wake)
{
_pin.wakeOnActive( wake);
}
/**
* Deternine if the button should trigger power manager wake-up.
*
* @param wake The action of the button - either 1 to trigger wake up or 0 for no
*/
int isWakeOnActive()
{
return _pin.isWakeOnActive();
}
/**
* Method to release the given pin from a peripheral, if already bound.
* Device drivers should override this method to disconnect themselves from the give pin
* to allow it to be used by a different peripheral.
*
* @param pin the Pin to be released
*/
virtual int releasePin(Pin &pin) override;
/**
* Destructor for Button, where we deregister this instance from the array of fiber components.
*/
~Button();
protected:
/**
* Determines if this button is instantenously active (i.e. pressed).
* Internal method, use before debouncing.
*/
virtual int buttonActive();
/**
* Puts the component in (or out of) sleep (low power) mode.
*/
virtual int setSleep(bool doSleep) override;
};
}
#endif