-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinDetect.h
159 lines (128 loc) · 4.39 KB
/
PinDetect.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
159
/*
Copyright (c) 2010 Andy Kirkham
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 AJK_PIN_DETECT_H
#define AJK_PIN_DETECT_H
#ifndef MBED_H
#include "mbed.h"
#endif
#ifndef PINDETECT_PIN_ASSERTED
#define PINDETECT_PIN_ASSERTED 1
#endif
#ifndef PINDETECT_SAMPLE_PERIOD
#define PINDETECT_SAMPLE_PERIOD 20000
#endif
#ifndef PINDETECT_ASSERT_COUNT
#define PINDETECT_ASSERT_COUNT 1
#endif
#ifndef PINDETECT_HOLD_COUNT
#define PINDETECT_HOLD_COUNT 50
#endif
namespace AjK {
class PinDetect {
public:
friend class Ticker;
/** PinDetect constructor
*
* By default the PinMode is set to PullDown.
*
* @see http://mbed.org/handbook/DigitalIn
* @param p PinName is a valid pin that supports DigitalIn
*/
PinDetect(PinName p);
/** PinDetect constructor
*
* @see http://mbed.org/handbook/DigitalIn
* @param PinName p is a valid pin that supports DigitalIn
* @param PinMode m The mode the DigitalIn should use.
*/
PinDetect(PinName p, PinMode m);
/** PinDetect destructor
*/
virtual ~PinDetect(void);
/** Set the sampling time in microseconds.
*
* @param int The time between pin samples in microseconds.
*/
void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD);
/** Set the value used as assert.
*
* Defaults to 1 (ie if pin == 1 then pin asserted).
*
* @param int New assert value (1 or 0)
*/
void setAssertValue(int i = PINDETECT_PIN_ASSERTED);
/** Set the number of continuous samples until assert assumed.
*
* Defaults to 1 (1 * sample frequency).
*
* @param int The number of continuous samples until assert assumed.
*/
void setSamplesTillAssert(int i);
/** Set the number of continuous samples until held assumed.
*
* Defaults to 50 * sample frequency.
*
* @param int The number of continuous samples until held assumed.
*/
void setSamplesTillHeld(int i);
/** Set the pin mode.
*
* @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
* @param PinMode m The mode to pass on to the DigitalIn
*/
void mode(PinMode m);
void attach_asserted(Callback<void()> function);
void attach_deasserted(Callback<void()> function);
void attach_asserted_held(Callback<void()> function);
void attach_deasserted_held(Callback<void()> function);
/** operator int()
*
* Read the value of the pin being sampled.
*/
operator int() {
return _in->read();
}
protected:
DigitalIn *_in;
Ticker *_ticker;
int _prevState;
int _currentStateCounter;
int _sampleTime;
int _assertValue;
int _samplesTillAssertReload;
int _samplesTillAssert;
int _samplesTillHeldReload;
int _samplesTillHeld;
Callback<void()> _callbackAsserted;
Callback<void()> _callbackDeasserted;
Callback<void()> _callbackAssertedHeld;
Callback<void()> _callbackDeassertedHeld;
/** The Ticker periodic callback function
*/
void isr(void);
/** initialise class
*
* @param PinName p is a valid pin that supports DigitalIn
* @param PinMode m The mode the DigitalIn should use.
*/
void init(PinName p, PinMode m);
};
}; // namespace AjK ends.
using namespace AjK;
#endif