Mbed OS 5

Fork of PinDetect by Arturo Alvarado

Rewritten to Mbed OS 5 and their callback mechanism

Committer:
pilotak
Date:
Wed Nov 16 23:20:29 2016 +0000
Revision:
4:bacc386fe2ad
Parent:
3:8e3abbd21291
Child:
5:2b0afd293d1d
rewritten to Mbed OS 5.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:4f4ccb203a70 1 /*
AjK 0:4f4ccb203a70 2 Copyright (c) 2010 Andy Kirkham
AjK 0:4f4ccb203a70 3
AjK 0:4f4ccb203a70 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:4f4ccb203a70 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:4f4ccb203a70 6 in the Software without restriction, including without limitation the rights
AjK 0:4f4ccb203a70 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:4f4ccb203a70 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:4f4ccb203a70 9 furnished to do so, subject to the following conditions:
AjK 0:4f4ccb203a70 10
AjK 0:4f4ccb203a70 11 The above copyright notice and this permission notice shall be included in
AjK 0:4f4ccb203a70 12 all copies or substantial portions of the Software.
AjK 0:4f4ccb203a70 13
AjK 0:4f4ccb203a70 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:4f4ccb203a70 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:4f4ccb203a70 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:4f4ccb203a70 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:4f4ccb203a70 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:4f4ccb203a70 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:4f4ccb203a70 20 THE SOFTWARE.
AjK 0:4f4ccb203a70 21 */
AjK 0:4f4ccb203a70 22
AjK 0:4f4ccb203a70 23 #ifndef AJK_PIN_DETECT_H
AjK 0:4f4ccb203a70 24 #define AJK_PIN_DETECT_H
AjK 0:4f4ccb203a70 25
AjK 0:4f4ccb203a70 26 #ifndef MBED_H
AjK 0:4f4ccb203a70 27 #include "mbed.h"
AjK 0:4f4ccb203a70 28 #endif
AjK 0:4f4ccb203a70 29
AjK 0:4f4ccb203a70 30 #ifndef PINDETECT_PIN_ASSTERED
AjK 0:4f4ccb203a70 31 #define PINDETECT_PIN_ASSTERED 1
AjK 0:4f4ccb203a70 32 #endif
AjK 0:4f4ccb203a70 33
AjK 0:4f4ccb203a70 34 #ifndef PINDETECT_SAMPLE_PERIOD
AjK 0:4f4ccb203a70 35 #define PINDETECT_SAMPLE_PERIOD 20000
AjK 0:4f4ccb203a70 36 #endif
AjK 0:4f4ccb203a70 37
AjK 0:4f4ccb203a70 38 #ifndef PINDETECT_ASSERT_COUNT
AjK 0:4f4ccb203a70 39 #define PINDETECT_ASSERT_COUNT 1
AjK 0:4f4ccb203a70 40 #endif
AjK 0:4f4ccb203a70 41
AjK 0:4f4ccb203a70 42 #ifndef PINDETECT_HOLD_COUNT
AjK 0:4f4ccb203a70 43 #define PINDETECT_HOLD_COUNT 50
AjK 0:4f4ccb203a70 44 #endif
AjK 0:4f4ccb203a70 45
AjK 0:4f4ccb203a70 46 namespace AjK {
AjK 0:4f4ccb203a70 47
AjK 0:4f4ccb203a70 48 class PinDetect {
AjK 1:611a8f5ac65c 49
pilotak 4:bacc386fe2ad 50 public:
pilotak 4:bacc386fe2ad 51 friend class Ticker;
pilotak 4:bacc386fe2ad 52
pilotak 4:bacc386fe2ad 53
pilotak 4:bacc386fe2ad 54 /** PinDetect constructor
pilotak 4:bacc386fe2ad 55 *
pilotak 4:bacc386fe2ad 56 * By default the PinMode is set to PullDown.
pilotak 4:bacc386fe2ad 57 *
pilotak 4:bacc386fe2ad 58 * @see http://mbed.org/handbook/DigitalIn
pilotak 4:bacc386fe2ad 59 * @param p PinName is a valid pin that supports DigitalIn
pilotak 4:bacc386fe2ad 60 */
pilotak 4:bacc386fe2ad 61 PinDetect(PinName p);
pilotak 4:bacc386fe2ad 62
pilotak 4:bacc386fe2ad 63 /** PinDetect constructor
pilotak 4:bacc386fe2ad 64 *
pilotak 4:bacc386fe2ad 65 * @see http://mbed.org/handbook/DigitalIn
pilotak 4:bacc386fe2ad 66 * @param PinName p is a valid pin that supports DigitalIn
pilotak 4:bacc386fe2ad 67 * @param PinMode m The mode the DigitalIn should use.
pilotak 4:bacc386fe2ad 68 */
pilotak 4:bacc386fe2ad 69 PinDetect(PinName p, PinMode m);
pilotak 4:bacc386fe2ad 70
pilotak 4:bacc386fe2ad 71 /** PinDetect destructor
pilotak 4:bacc386fe2ad 72 */
pilotak 4:bacc386fe2ad 73 virtual ~PinDetect(void);
pilotak 4:bacc386fe2ad 74
pilotak 4:bacc386fe2ad 75 /** Set the sampling time in microseconds.
pilotak 4:bacc386fe2ad 76 *
pilotak 4:bacc386fe2ad 77 * @param int The time between pin samples in microseconds.
pilotak 4:bacc386fe2ad 78 */
pilotak 4:bacc386fe2ad 79 void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD);
pilotak 4:bacc386fe2ad 80
pilotak 4:bacc386fe2ad 81 /** Set the value used as assert.
pilotak 4:bacc386fe2ad 82 *
pilotak 4:bacc386fe2ad 83 * Defaults to 1 (ie if pin == 1 then pin asserted).
pilotak 4:bacc386fe2ad 84 *
pilotak 4:bacc386fe2ad 85 * @param int New assert value (1 or 0)
pilotak 4:bacc386fe2ad 86 */
pilotak 4:bacc386fe2ad 87 void setAssertValue (int i = PINDETECT_PIN_ASSTERED);
pilotak 4:bacc386fe2ad 88
pilotak 4:bacc386fe2ad 89 /** Set the number of continuous samples until assert assumed.
pilotak 4:bacc386fe2ad 90 *
pilotak 4:bacc386fe2ad 91 * Defaults to 1 (1 * sample frequency).
pilotak 4:bacc386fe2ad 92 *
pilotak 4:bacc386fe2ad 93 * @param int The number of continuous samples until assert assumed.
pilotak 4:bacc386fe2ad 94 */
pilotak 4:bacc386fe2ad 95 void setSamplesTillAssert(int i);
pilotak 4:bacc386fe2ad 96
pilotak 4:bacc386fe2ad 97 /** Set the number of continuous samples until held assumed.
pilotak 4:bacc386fe2ad 98 *
pilotak 4:bacc386fe2ad 99 * Defaults to 50 * sample frequency.
pilotak 4:bacc386fe2ad 100 *
pilotak 4:bacc386fe2ad 101 * @param int The number of continuous samples until held assumed.
pilotak 4:bacc386fe2ad 102 */
pilotak 4:bacc386fe2ad 103 void setSamplesTillHeld(int i);
pilotak 4:bacc386fe2ad 104
pilotak 4:bacc386fe2ad 105 /** Set the pin mode.
pilotak 4:bacc386fe2ad 106 *
pilotak 4:bacc386fe2ad 107 * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
pilotak 4:bacc386fe2ad 108 * @param PinMode m The mode to pass on to the DigitalIn
pilotak 4:bacc386fe2ad 109 */
pilotak 4:bacc386fe2ad 110 void mode(PinMode m);
pilotak 4:bacc386fe2ad 111
pilotak 4:bacc386fe2ad 112 void attach_asserted(Callback<void()> function);
pilotak 4:bacc386fe2ad 113
pilotak 4:bacc386fe2ad 114 template<typename T, typename M>
pilotak 4:bacc386fe2ad 115 void attach_asserted(T *obj, M method){
pilotak 4:bacc386fe2ad 116 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 117 attach_asserted(callback(obj, method));
pilotak 4:bacc386fe2ad 118 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 119 }
pilotak 4:bacc386fe2ad 120
pilotak 4:bacc386fe2ad 121 void attach_deasserted(Callback<void()> function);
pilotak 4:bacc386fe2ad 122
pilotak 4:bacc386fe2ad 123 template<typename T, typename M>
pilotak 4:bacc386fe2ad 124 void attach_deasserted(T *obj, M method){
pilotak 4:bacc386fe2ad 125 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 126 attach_deasserted(callback(obj, method));
pilotak 4:bacc386fe2ad 127 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 128 }
pilotak 4:bacc386fe2ad 129
pilotak 4:bacc386fe2ad 130 void attach_asserted_held(Callback<void()> function);
pilotak 4:bacc386fe2ad 131
pilotak 4:bacc386fe2ad 132 template<typename T, typename M>
pilotak 4:bacc386fe2ad 133 void attach_asserted_held(T *obj, M method){
pilotak 4:bacc386fe2ad 134 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 135 attach_asserted_held(callback(obj, method));
pilotak 4:bacc386fe2ad 136 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 137 }
pilotak 4:bacc386fe2ad 138
pilotak 4:bacc386fe2ad 139 void attach_deasserted_held(Callback<void()> function);
pilotak 4:bacc386fe2ad 140
pilotak 4:bacc386fe2ad 141 template<typename T, typename M>
pilotak 4:bacc386fe2ad 142 void attach_deasserted_held(T *obj, M method){
pilotak 4:bacc386fe2ad 143 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 144 attach_deasserted_held(callback(obj, method));
pilotak 4:bacc386fe2ad 145 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 146 }
pilotak 4:bacc386fe2ad 147
pilotak 4:bacc386fe2ad 148 /** operator int()
pilotak 4:bacc386fe2ad 149 *
pilotak 4:bacc386fe2ad 150 * Read the value of the pin being sampled.
pilotak 4:bacc386fe2ad 151 */
pilotak 4:bacc386fe2ad 152 operator int() {
pilotak 4:bacc386fe2ad 153 return _in->read();
pilotak 4:bacc386fe2ad 154 }
pilotak 4:bacc386fe2ad 155
pilotak 4:bacc386fe2ad 156 protected:
AjK 1:611a8f5ac65c 157 DigitalIn *_in;
AjK 1:611a8f5ac65c 158 Ticker *_ticker;
AjK 1:611a8f5ac65c 159 int _prevState;
AjK 1:611a8f5ac65c 160 int _currentStateCounter;
AjK 1:611a8f5ac65c 161 int _sampleTime;
AjK 1:611a8f5ac65c 162 int _assertValue;
AjK 1:611a8f5ac65c 163 int _samplesTillAssertReload;
AjK 1:611a8f5ac65c 164 int _samplesTillAssert;
AjK 1:611a8f5ac65c 165 int _samplesTillHeldReload;
AjK 1:611a8f5ac65c 166 int _samplesTillHeld;
pilotak 4:bacc386fe2ad 167 Callback<void()> _callbackAsserted;
pilotak 4:bacc386fe2ad 168 Callback<void()> _callbackDeasserted;
pilotak 4:bacc386fe2ad 169 Callback<void()> _callbackAssertedHeld;
pilotak 4:bacc386fe2ad 170 Callback<void()> _callbackDeassertedHeld;
pilotak 4:bacc386fe2ad 171
pilotak 4:bacc386fe2ad 172 /** The Ticker periodic callback function
pilotak 4:bacc386fe2ad 173 */
pilotak 4:bacc386fe2ad 174 void isr(void);
AjK 1:611a8f5ac65c 175
AjK 1:611a8f5ac65c 176 /** initialise class
AjK 1:611a8f5ac65c 177 *
AjK 1:611a8f5ac65c 178 * @param PinName p is a valid pin that supports DigitalIn
AjK 1:611a8f5ac65c 179 * @param PinMode m The mode the DigitalIn should use.
AjK 1:611a8f5ac65c 180 */
pilotak 4:bacc386fe2ad 181 void init(PinName p, PinMode m);
AjK 0:4f4ccb203a70 182 };
AjK 0:4f4ccb203a70 183
AjK 0:4f4ccb203a70 184 }; // namespace AjK ends.
AjK 0:4f4ccb203a70 185
AjK 0:4f4ccb203a70 186 using namespace AjK;
AjK 0:4f4ccb203a70 187
AjK 0:4f4ccb203a70 188 #endif