Mbed OS 5

Fork of PinDetect by Arturo Alvarado

Rewritten to Mbed OS 5 and their callback mechanism

Committer:
pilotak
Date:
Thu Jun 28 16:19:47 2018 +0200
Revision:
6:09458c0b5cad
Parent:
5:2b0afd293d1d
typo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:4f4ccb203a70 1 /*
AjK 0:4f4ccb203a70 2 Copyright (c) 2010 Andy Kirkham
pilotak 5:2b0afd293d1d 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:
pilotak 5:2b0afd293d1d 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.
pilotak 5:2b0afd293d1d 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
pilotak 5:2b0afd293d1d 27 #include "mbed.h"
AjK 0:4f4ccb203a70 28 #endif
AjK 0:4f4ccb203a70 29
pilotak 5:2b0afd293d1d 30 #ifndef PINDETECT_PIN_ASSERTED
pilotak 5:2b0afd293d1d 31 #define PINDETECT_PIN_ASSERTED 1
AjK 0:4f4ccb203a70 32 #endif
AjK 0:4f4ccb203a70 33
AjK 0:4f4ccb203a70 34 #ifndef PINDETECT_SAMPLE_PERIOD
pilotak 5:2b0afd293d1d 35 #define PINDETECT_SAMPLE_PERIOD 20000
AjK 0:4f4ccb203a70 36 #endif
AjK 0:4f4ccb203a70 37
pilotak 5:2b0afd293d1d 38 #ifndef PINDETECT_ASSERT_COUNT
pilotak 5:2b0afd293d1d 39 #define PINDETECT_ASSERT_COUNT 1
AjK 0:4f4ccb203a70 40 #endif
AjK 0:4f4ccb203a70 41
AjK 0:4f4ccb203a70 42 #ifndef PINDETECT_HOLD_COUNT
pilotak 5:2b0afd293d1d 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 {
pilotak 5:2b0afd293d1d 49 public:
pilotak 4:bacc386fe2ad 50 friend class Ticker;
pilotak 5:2b0afd293d1d 51
pilotak 5:2b0afd293d1d 52
pilotak 4:bacc386fe2ad 53 /** PinDetect constructor
pilotak 4:bacc386fe2ad 54 *
pilotak 4:bacc386fe2ad 55 * By default the PinMode is set to PullDown.
pilotak 4:bacc386fe2ad 56 *
pilotak 4:bacc386fe2ad 57 * @see http://mbed.org/handbook/DigitalIn
pilotak 4:bacc386fe2ad 58 * @param p PinName is a valid pin that supports DigitalIn
pilotak 5:2b0afd293d1d 59 */
pilotak 4:bacc386fe2ad 60 PinDetect(PinName p);
pilotak 5:2b0afd293d1d 61
pilotak 4:bacc386fe2ad 62 /** PinDetect constructor
pilotak 4:bacc386fe2ad 63 *
pilotak 4:bacc386fe2ad 64 * @see http://mbed.org/handbook/DigitalIn
pilotak 4:bacc386fe2ad 65 * @param PinName p is a valid pin that supports DigitalIn
pilotak 4:bacc386fe2ad 66 * @param PinMode m The mode the DigitalIn should use.
pilotak 5:2b0afd293d1d 67 */
pilotak 4:bacc386fe2ad 68 PinDetect(PinName p, PinMode m);
pilotak 5:2b0afd293d1d 69
pilotak 4:bacc386fe2ad 70 /** PinDetect destructor
pilotak 5:2b0afd293d1d 71 */
pilotak 4:bacc386fe2ad 72 virtual ~PinDetect(void);
pilotak 5:2b0afd293d1d 73
pilotak 4:bacc386fe2ad 74 /** Set the sampling time in microseconds.
pilotak 4:bacc386fe2ad 75 *
pilotak 4:bacc386fe2ad 76 * @param int The time between pin samples in microseconds.
pilotak 4:bacc386fe2ad 77 */
pilotak 4:bacc386fe2ad 78 void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD);
pilotak 5:2b0afd293d1d 79
pilotak 4:bacc386fe2ad 80 /** Set the value used as assert.
pilotak 4:bacc386fe2ad 81 *
pilotak 4:bacc386fe2ad 82 * Defaults to 1 (ie if pin == 1 then pin asserted).
pilotak 4:bacc386fe2ad 83 *
pilotak 4:bacc386fe2ad 84 * @param int New assert value (1 or 0)
pilotak 4:bacc386fe2ad 85 */
pilotak 6:09458c0b5cad 86 void setAssertValue(int i = PINDETECT_PIN_ASSERTED);
pilotak 5:2b0afd293d1d 87
pilotak 4:bacc386fe2ad 88 /** Set the number of continuous samples until assert assumed.
pilotak 4:bacc386fe2ad 89 *
pilotak 4:bacc386fe2ad 90 * Defaults to 1 (1 * sample frequency).
pilotak 4:bacc386fe2ad 91 *
pilotak 4:bacc386fe2ad 92 * @param int The number of continuous samples until assert assumed.
pilotak 5:2b0afd293d1d 93 */
pilotak 4:bacc386fe2ad 94 void setSamplesTillAssert(int i);
pilotak 5:2b0afd293d1d 95
pilotak 4:bacc386fe2ad 96 /** Set the number of continuous samples until held assumed.
pilotak 4:bacc386fe2ad 97 *
pilotak 4:bacc386fe2ad 98 * Defaults to 50 * sample frequency.
pilotak 4:bacc386fe2ad 99 *
pilotak 4:bacc386fe2ad 100 * @param int The number of continuous samples until held assumed.
pilotak 5:2b0afd293d1d 101 */
pilotak 4:bacc386fe2ad 102 void setSamplesTillHeld(int i);
pilotak 5:2b0afd293d1d 103
pilotak 4:bacc386fe2ad 104 /** Set the pin mode.
pilotak 4:bacc386fe2ad 105 *
pilotak 4:bacc386fe2ad 106 * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
pilotak 4:bacc386fe2ad 107 * @param PinMode m The mode to pass on to the DigitalIn
pilotak 4:bacc386fe2ad 108 */
pilotak 4:bacc386fe2ad 109 void mode(PinMode m);
pilotak 5:2b0afd293d1d 110
pilotak 4:bacc386fe2ad 111 void attach_asserted(Callback<void()> function);
pilotak 5:2b0afd293d1d 112
pilotak 4:bacc386fe2ad 113 void attach_deasserted(Callback<void()> function);
pilotak 5:2b0afd293d1d 114
pilotak 4:bacc386fe2ad 115 void attach_asserted_held(Callback<void()> function);
pilotak 5:2b0afd293d1d 116
pilotak 4:bacc386fe2ad 117 void attach_deasserted_held(Callback<void()> function);
pilotak 5:2b0afd293d1d 118
pilotak 4:bacc386fe2ad 119 /** operator int()
pilotak 4:bacc386fe2ad 120 *
pilotak 4:bacc386fe2ad 121 * Read the value of the pin being sampled.
pilotak 4:bacc386fe2ad 122 */
pilotak 4:bacc386fe2ad 123 operator int() {
pilotak 4:bacc386fe2ad 124 return _in->read();
pilotak 4:bacc386fe2ad 125 }
pilotak 4:bacc386fe2ad 126
pilotak 5:2b0afd293d1d 127 protected:
AjK 1:611a8f5ac65c 128 DigitalIn *_in;
AjK 1:611a8f5ac65c 129 Ticker *_ticker;
AjK 1:611a8f5ac65c 130 int _prevState;
AjK 1:611a8f5ac65c 131 int _currentStateCounter;
AjK 1:611a8f5ac65c 132 int _sampleTime;
AjK 1:611a8f5ac65c 133 int _assertValue;
AjK 1:611a8f5ac65c 134 int _samplesTillAssertReload;
AjK 1:611a8f5ac65c 135 int _samplesTillAssert;
AjK 1:611a8f5ac65c 136 int _samplesTillHeldReload;
AjK 1:611a8f5ac65c 137 int _samplesTillHeld;
pilotak 4:bacc386fe2ad 138 Callback<void()> _callbackAsserted;
pilotak 4:bacc386fe2ad 139 Callback<void()> _callbackDeasserted;
pilotak 4:bacc386fe2ad 140 Callback<void()> _callbackAssertedHeld;
pilotak 4:bacc386fe2ad 141 Callback<void()> _callbackDeassertedHeld;
pilotak 5:2b0afd293d1d 142
pilotak 5:2b0afd293d1d 143 /** The Ticker periodic callback function
pilotak 5:2b0afd293d1d 144 */
pilotak 4:bacc386fe2ad 145 void isr(void);
pilotak 5:2b0afd293d1d 146
AjK 1:611a8f5ac65c 147 /** initialise class
AjK 1:611a8f5ac65c 148 *
AjK 1:611a8f5ac65c 149 * @param PinName p is a valid pin that supports DigitalIn
AjK 1:611a8f5ac65c 150 * @param PinMode m The mode the DigitalIn should use.
AjK 1:611a8f5ac65c 151 */
pilotak 4:bacc386fe2ad 152 void init(PinName p, PinMode m);
AjK 0:4f4ccb203a70 153 };
AjK 0:4f4ccb203a70 154
AjK 0:4f4ccb203a70 155 }; // namespace AjK ends.
AjK 0:4f4ccb203a70 156
AjK 0:4f4ccb203a70 157 using namespace AjK;
AjK 0:4f4ccb203a70 158
AjK 0:4f4ccb203a70 159 #endif