Forked to avoid changing the publioc repo
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/drivers/MicroBitPin.h@65:f7ebabf23e15, 2016-07-13 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Wed Jul 13 12:18:45 2016 +0100
- Revision:
- 65:f7ebabf23e15
- Parent:
- 63:b4372a29994f
- Child:
- 66:2fc7d7c2fffc
Synchronized with git rev 00674e34
Author: Joe Finney
microbit: Tuning and furhter debounce of SHAKE gesture
- Added timeout code to prevent over-generation of SHAKE events.
- Tuning of SHAKE detection parameters to align sensitivity with existing
microbit.co.uk algorithm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | #ifndef MICROBIT_PIN_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_PIN_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "mbed.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "MicroBitComponent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | // Status Field flags... |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | #define IO_STATUS_DIGITAL_IN 0x01 // Pin is configured as a digital input, with no pull up. |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | #define IO_STATUS_DIGITAL_OUT 0x02 // Pin is configured as a digital output |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | #define IO_STATUS_ANALOG_IN 0x04 // Pin is Analog in |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | #define IO_STATUS_ANALOG_OUT 0x08 // Pin is Analog out |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | #define IO_STATUS_TOUCH_IN 0x10 // Pin is a makey-makey style touch sensor |
LancasterUniversity | 49:88f03f3feff1 | 38 | #define IO_STATUS_EVENT_ON_EDGE 0x20 // Pin will generate events on pin change |
LancasterUniversity | 49:88f03f3feff1 | 39 | #define IO_STATUS_EVENT_PULSE_ON_EDGE 0x40 // Pin will generate events on pin change |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | //#defines for each edge connector pin |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | #define MICROBIT_PIN_P0 P0_3 //P0 is the left most pad (ANALOG/DIGITAL) used to be P0_3 on green board |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | #define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | #define MICROBIT_PIN_P2 P0_1 //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | #define MICROBIT_PIN_P3 P0_4 //COL1 (ANALOG/DIGITAL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | #define MICROBIT_PIN_P4 P0_5 //COL2 (ANALOG/DIGITAL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | #define MICROBIT_PIN_P5 P0_17 //BTN_A |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | #define MICROBIT_PIN_P6 P0_12 //COL9 |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | #define MICROBIT_PIN_P7 P0_11 //COL8 |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | #define MICROBIT_PIN_P8 P0_18 //PIN 18 |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | #define MICROBIT_PIN_P9 P0_10 //COL7 |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | #define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | #define MICROBIT_PIN_P11 P0_26 //BTN_B |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | #define MICROBIT_PIN_P12 P0_20 //PIN 20 |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | #define MICROBIT_PIN_P13 P0_23 //SCK |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | #define MICROBIT_PIN_P14 P0_22 //MISO |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | #define MICROBIT_PIN_P15 P0_21 //MOSI |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | #define MICROBIT_PIN_P16 P0_16 //PIN 16 |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | #define MICROBIT_PIN_P19 P0_0 //SCL |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | #define MICROBIT_PIN_P20 P0_30 //SDA |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | #define MICROBIT_PIN_MAX_OUTPUT 1023 |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | #define MICROBIT_PIN_MAX_SERVO_RANGE 180 |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | #define MICROBIT_PIN_DEFAULT_SERVO_RANGE 2000 |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | #define MICROBIT_PIN_DEFAULT_SERVO_CENTER 1500 |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | |
LancasterUniversity | 49:88f03f3feff1 | 68 | #define MICROBIT_PIN_EVENT_NONE 0 |
LancasterUniversity | 49:88f03f3feff1 | 69 | #define MICROBIT_PIN_EVENT_ON_EDGE 1 |
LancasterUniversity | 49:88f03f3feff1 | 70 | #define MICROBIT_PIN_EVENT_ON_PULSE 2 |
LancasterUniversity | 49:88f03f3feff1 | 71 | #define MICROBIT_PIN_EVENT_ON_TOUCH 3 |
LancasterUniversity | 49:88f03f3feff1 | 72 | |
LancasterUniversity | 49:88f03f3feff1 | 73 | #define MICROBIT_PIN_EVT_RISE 2 |
LancasterUniversity | 49:88f03f3feff1 | 74 | #define MICROBIT_PIN_EVT_FALL 3 |
LancasterUniversity | 49:88f03f3feff1 | 75 | #define MICROBIT_PIN_EVT_PULSE_HI 4 |
LancasterUniversity | 49:88f03f3feff1 | 76 | #define MICROBIT_PIN_EVT_PULSE_LO 5 |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * Pin capabilities enum. |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | * Used to determine the capabilities of each Pin as some can only be digital, or can be both digital and analogue. |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | enum PinCapability{ |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | PIN_CAPABILITY_DIGITAL = 0x01, |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | PIN_CAPABILITY_ANALOG = 0x02, |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | PIN_CAPABILITY_AD = PIN_CAPABILITY_DIGITAL | PIN_CAPABILITY_ANALOG, |
LancasterUniversity | 45:23b71960fe6c | 86 | PIN_CAPABILITY_ALL = PIN_CAPABILITY_DIGITAL | PIN_CAPABILITY_ANALOG |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | * Class definition for MicroBitPin. |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | * Commonly represents an I/O pin on the edge connector. |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | class MicroBitPin : public MicroBitComponent |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | // The mbed object looking after this pin at any point in time (untyped due to dynamic behaviour). |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | void *pin; |
LancasterUniversity | 65:f7ebabf23e15 | 98 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | PinCapability capability; |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | * Disconnect any attached mBed IO from this pin. |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | * Used only when pin changes mode (i.e. Input/Output/Analog/Digital) |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | void disconnect(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | * Performs a check to ensure that the current Pin is in control of a |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | * DynamicPwm instance, and if it's not, allocates a new DynamicPwm instance. |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | int obtainAnalogChannel(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | |
LancasterUniversity | 49:88f03f3feff1 | 114 | /** |
LancasterUniversity | 49:88f03f3feff1 | 115 | * Interrupt handler for when an rise interrupt is triggered. |
LancasterUniversity | 49:88f03f3feff1 | 116 | */ |
LancasterUniversity | 49:88f03f3feff1 | 117 | void onRise(); |
LancasterUniversity | 49:88f03f3feff1 | 118 | |
LancasterUniversity | 49:88f03f3feff1 | 119 | /** |
LancasterUniversity | 49:88f03f3feff1 | 120 | * Interrupt handler for when an fall interrupt is triggered. |
LancasterUniversity | 49:88f03f3feff1 | 121 | */ |
LancasterUniversity | 49:88f03f3feff1 | 122 | void onFall(); |
LancasterUniversity | 49:88f03f3feff1 | 123 | |
LancasterUniversity | 49:88f03f3feff1 | 124 | /** |
LancasterUniversity | 49:88f03f3feff1 | 125 | * This member function manages the calculation of the timestamp of a pulse detected |
LancasterUniversity | 49:88f03f3feff1 | 126 | * on a pin whilst in IO_STATUS_EVENT_PULSE_ON_EDGE or IO_STATUS_EVENT_ON_EDGE modes. |
LancasterUniversity | 49:88f03f3feff1 | 127 | * |
LancasterUniversity | 49:88f03f3feff1 | 128 | * @param eventValue the event value to distribute onto the message bus. |
LancasterUniversity | 49:88f03f3feff1 | 129 | */ |
LancasterUniversity | 49:88f03f3feff1 | 130 | void pulseWidthEvent(int eventValue); |
LancasterUniversity | 49:88f03f3feff1 | 131 | |
LancasterUniversity | 49:88f03f3feff1 | 132 | /** |
LancasterUniversity | 49:88f03f3feff1 | 133 | * This member function will construct an TimedInterruptIn instance, and configure |
LancasterUniversity | 49:88f03f3feff1 | 134 | * interrupts for rise and fall. |
LancasterUniversity | 49:88f03f3feff1 | 135 | * |
LancasterUniversity | 49:88f03f3feff1 | 136 | * @param eventType the specific mode used in interrupt context to determine how an |
LancasterUniversity | 49:88f03f3feff1 | 137 | * edge/rise is processed. |
LancasterUniversity | 49:88f03f3feff1 | 138 | * |
LancasterUniversity | 49:88f03f3feff1 | 139 | * @return MICROBIT_OK on success |
LancasterUniversity | 49:88f03f3feff1 | 140 | */ |
LancasterUniversity | 49:88f03f3feff1 | 141 | int enableRiseFallEvents(int eventType); |
LancasterUniversity | 49:88f03f3feff1 | 142 | |
LancasterUniversity | 49:88f03f3feff1 | 143 | /** |
LancasterUniversity | 49:88f03f3feff1 | 144 | * If this pin is in a mode where the pin is generating events, it will destruct |
LancasterUniversity | 49:88f03f3feff1 | 145 | * the current instance attached to this MicroBitPin instance. |
LancasterUniversity | 49:88f03f3feff1 | 146 | * |
LancasterUniversity | 49:88f03f3feff1 | 147 | * @return MICROBIT_OK on success. |
LancasterUniversity | 49:88f03f3feff1 | 148 | */ |
LancasterUniversity | 49:88f03f3feff1 | 149 | int disableEvents(); |
LancasterUniversity | 49:88f03f3feff1 | 150 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 152 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 153 | // mbed PinName of this pin. |
Jonathan Austin |
1:8aa5cdb4ab67 | 154 | PinName name; |
Jonathan Austin |
1:8aa5cdb4ab67 | 155 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 156 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 157 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | * Create a MicroBitPin instance, generally used to represent a pin on the edge connector. |
Jonathan Austin |
1:8aa5cdb4ab67 | 159 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | * @param id the unique EventModel id of this component. |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | * @param name the mbed PinName for this MicroBitPin instance. |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 164 | * @param capability the capabilities this MicroBitPin instance should have. |
LancasterUniversity | 45:23b71960fe6c | 165 | * (PIN_CAPABILITY_DIGITAL, PIN_CAPABILITY_ANALOG, PIN_CAPABILITY_AD, PIN_CAPABILITY_ALL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 166 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 167 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 168 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL); |
Jonathan Austin |
1:8aa5cdb4ab67 | 169 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 170 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 171 | MicroBitPin(int id, PinName name, PinCapability capability); |
Jonathan Austin |
1:8aa5cdb4ab67 | 172 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 173 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 174 | * Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'. |
Jonathan Austin |
1:8aa5cdb4ab67 | 175 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 176 | * @param value 0 (LO) or 1 (HI) |
Jonathan Austin |
1:8aa5cdb4ab67 | 177 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 178 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
Jonathan Austin |
1:8aa5cdb4ab67 | 179 | * if the given pin does not have digital capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 180 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 181 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 182 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
Jonathan Austin |
1:8aa5cdb4ab67 | 183 | * P0.setDigitalValue(1); // P0 is now HI |
Jonathan Austin |
1:8aa5cdb4ab67 | 184 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 185 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 186 | int setDigitalValue(int value); |
Jonathan Austin |
1:8aa5cdb4ab67 | 187 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 188 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 189 | * Configures this IO pin as a digital input (if necessary) and tests its current value. |
Jonathan Austin |
1:8aa5cdb4ab67 | 190 | * |
LancasterUniversity | 49:88f03f3feff1 | 191 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 192 | * @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED |
LancasterUniversity | 49:88f03f3feff1 | 193 | * if the given pin does not have digital capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 194 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 195 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 196 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
Jonathan Austin |
1:8aa5cdb4ab67 | 197 | * P0.getDigitalValue(); // P0 is either 0 or 1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 198 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 199 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 200 | int getDigitalValue(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 201 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 202 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 203 | * Configures this IO pin as an analog/pwm output, and change the output value to the given level. |
Jonathan Austin |
1:8aa5cdb4ab67 | 204 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 205 | * @param value the level to set on the output pin, in the range 0 - 1024 |
Jonathan Austin |
1:8aa5cdb4ab67 | 206 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 207 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
Jonathan Austin |
1:8aa5cdb4ab67 | 208 | * if the given pin does not have analog capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 209 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 210 | int setAnalogValue(int value); |
Jonathan Austin |
1:8aa5cdb4ab67 | 211 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 212 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 213 | * Configures this IO pin as an analog/pwm output (if necessary) and configures the period to be 20ms, |
Jonathan Austin |
1:8aa5cdb4ab67 | 214 | * with a duty cycle between 500 us and 2500 us. |
Jonathan Austin |
1:8aa5cdb4ab67 | 215 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 216 | * A value of 180 sets the duty cycle to be 2500us, and a value of 0 sets the duty cycle to be 500us by default. |
Jonathan Austin |
1:8aa5cdb4ab67 | 217 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 218 | * This range can be modified to fine tune, and also tolerate different servos. |
Jonathan Austin |
1:8aa5cdb4ab67 | 219 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 220 | * @param value the level to set on the output pin, in the range 0 - 180. |
Jonathan Austin |
1:8aa5cdb4ab67 | 221 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 222 | * @param range which gives the span of possible values the i.e. the lower and upper bounds (center +/- range/2). Defaults to MICROBIT_PIN_DEFAULT_SERVO_RANGE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 223 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 224 | * @param center the center point from which to calculate the lower and upper bounds. Defaults to MICROBIT_PIN_DEFAULT_SERVO_CENTER |
Jonathan Austin |
1:8aa5cdb4ab67 | 225 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 226 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
Jonathan Austin |
1:8aa5cdb4ab67 | 227 | * if the given pin does not have analog capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 228 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 229 | int setServoValue(int value, int range = MICROBIT_PIN_DEFAULT_SERVO_RANGE, int center = MICROBIT_PIN_DEFAULT_SERVO_CENTER); |
Jonathan Austin |
1:8aa5cdb4ab67 | 230 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 231 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 232 | * Configures this IO pin as an analogue input (if necessary), and samples the Pin for its analog value. |
Jonathan Austin |
1:8aa5cdb4ab67 | 233 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 234 | * @return the current analogue level on the pin, in the range 0 - 1024, or |
Jonathan Austin |
1:8aa5cdb4ab67 | 235 | * MICROBIT_NOT_SUPPORTED if the given pin does not have analog capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 236 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 237 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 238 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
Jonathan Austin |
1:8aa5cdb4ab67 | 239 | * P0.getAnalogValue(); // P0 is a value in the range of 0 - 1024 |
Jonathan Austin |
1:8aa5cdb4ab67 | 240 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 241 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 242 | int getAnalogValue(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 243 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 244 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 245 | * Determines if this IO pin is currently configured as an input. |
Jonathan Austin |
1:8aa5cdb4ab67 | 246 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 247 | * @return 1 if pin is an analog or digital input, 0 otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 248 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 249 | int isInput(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 250 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 251 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 252 | * Determines if this IO pin is currently configured as an output. |
Jonathan Austin |
1:8aa5cdb4ab67 | 253 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 254 | * @return 1 if pin is an analog or digital output, 0 otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 255 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 256 | int isOutput(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 257 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 258 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 259 | * Determines if this IO pin is currently configured for digital use. |
Jonathan Austin |
1:8aa5cdb4ab67 | 260 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 261 | * @return 1 if pin is digital, 0 otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 262 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 263 | int isDigital(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 264 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 265 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 266 | * Determines if this IO pin is currently configured for analog use. |
Jonathan Austin |
1:8aa5cdb4ab67 | 267 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 268 | * @return 1 if pin is analog, 0 otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 269 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 270 | int isAnalog(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 271 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 272 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 273 | * Configures this IO pin as a "makey makey" style touch sensor (if necessary) |
Jonathan Austin |
1:8aa5cdb4ab67 | 274 | * and tests its current debounced state. |
Jonathan Austin |
1:8aa5cdb4ab67 | 275 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 276 | * Users can also subscribe to MicroBitButton events generated from this pin. |
Jonathan Austin |
1:8aa5cdb4ab67 | 277 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 278 | * @return 1 if pin is touched, 0 if not, or MICROBIT_NOT_SUPPORTED if this pin does not support touch capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 279 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 280 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 281 | * MicroBitMessageBus bus; |
Jonathan Austin |
1:8aa5cdb4ab67 | 282 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 283 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL); |
Jonathan Austin |
1:8aa5cdb4ab67 | 284 | * if(P0.isTouched()) |
Jonathan Austin |
1:8aa5cdb4ab67 | 285 | * { |
Jonathan Austin |
1:8aa5cdb4ab67 | 286 | * //do something! |
Jonathan Austin |
1:8aa5cdb4ab67 | 287 | * } |
Jonathan Austin |
1:8aa5cdb4ab67 | 288 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 289 | * // subscribe to events generated by this pin! |
Jonathan Austin |
1:8aa5cdb4ab67 | 290 | * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_BUTTON_EVT_CLICK, someFunction); |
Jonathan Austin |
1:8aa5cdb4ab67 | 291 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 292 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 293 | int isTouched(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 294 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 295 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 296 | * Configures this IO pin as an analog/pwm output if it isn't already, configures the period to be 20ms, |
Jonathan Austin |
1:8aa5cdb4ab67 | 297 | * and sets the pulse width, based on the value it is given. |
Jonathan Austin |
1:8aa5cdb4ab67 | 298 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 299 | * @param pulseWidth the desired pulse width in microseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 300 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 301 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
Jonathan Austin |
1:8aa5cdb4ab67 | 302 | * if the given pin does not have analog capability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 303 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 304 | int setServoPulseUs(int pulseWidth); |
Jonathan Austin |
1:8aa5cdb4ab67 | 305 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 306 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 307 | * Configures the PWM period of the analog output to the given value. |
Jonathan Austin |
1:8aa5cdb4ab67 | 308 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 309 | * @param period The new period for the analog output in milliseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 310 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 311 | * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the |
Jonathan Austin |
1:8aa5cdb4ab67 | 312 | * given pin is not configured as an analog output. |
Jonathan Austin |
1:8aa5cdb4ab67 | 313 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 314 | int setAnalogPeriod(int period); |
Jonathan Austin |
1:8aa5cdb4ab67 | 315 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 316 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 317 | * Configures the PWM period of the analog output to the given value. |
Jonathan Austin |
1:8aa5cdb4ab67 | 318 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 319 | * @param period The new period for the analog output in microseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 320 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 321 | * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the |
Jonathan Austin |
1:8aa5cdb4ab67 | 322 | * given pin is not configured as an analog output. |
Jonathan Austin |
1:8aa5cdb4ab67 | 323 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 324 | int setAnalogPeriodUs(int period); |
Jonathan Austin |
1:8aa5cdb4ab67 | 325 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 326 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 327 | * Obtains the PWM period of the analog output in microseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 328 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 329 | * @return the period on success, or MICROBIT_NOT_SUPPORTED if the |
Jonathan Austin |
1:8aa5cdb4ab67 | 330 | * given pin is not configured as an analog output. |
Jonathan Austin |
1:8aa5cdb4ab67 | 331 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 332 | int getAnalogPeriodUs(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 333 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 334 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 335 | * Obtains the PWM period of the analog output in milliseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 336 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 337 | * @return the period on success, or MICROBIT_NOT_SUPPORTED if the |
Jonathan Austin |
1:8aa5cdb4ab67 | 338 | * given pin is not configured as an analog output. |
Jonathan Austin |
1:8aa5cdb4ab67 | 339 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 340 | int getAnalogPeriod(); |
LancasterUniversity | 49:88f03f3feff1 | 341 | |
LancasterUniversity | 49:88f03f3feff1 | 342 | /** |
LancasterUniversity | 49:88f03f3feff1 | 343 | * Configures the pull of this pin. |
LancasterUniversity | 49:88f03f3feff1 | 344 | * |
LancasterUniversity | 53:ee44932401cb | 345 | * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone |
LancasterUniversity | 49:88f03f3feff1 | 346 | * |
LancasterUniversity | 49:88f03f3feff1 | 347 | * @return MICROBIT_NOT_SUPPORTED if the current pin configuration is anything other |
LancasterUniversity | 49:88f03f3feff1 | 348 | * than a digital input, otherwise MICROBIT_OK. |
LancasterUniversity | 49:88f03f3feff1 | 349 | */ |
LancasterUniversity | 49:88f03f3feff1 | 350 | int setPull(PinMode pull); |
LancasterUniversity | 49:88f03f3feff1 | 351 | |
LancasterUniversity | 49:88f03f3feff1 | 352 | /** |
LancasterUniversity | 49:88f03f3feff1 | 353 | * Configures the events generated by this MicroBitPin instance. |
LancasterUniversity | 49:88f03f3feff1 | 354 | * |
LancasterUniversity | 49:88f03f3feff1 | 355 | * MICROBIT_PIN_EVENT_ON_EDGE - Configures this pin to a digital input, and generates events whenever a rise/fall is detected on this pin. (MICROBIT_PIN_EVT_RISE, MICROBIT_PIN_EVT_FALL) |
LancasterUniversity | 49:88f03f3feff1 | 356 | * MICROBIT_PIN_EVENT_ON_PULSE - Configures this pin to a digital input, and generates events where the timestamp is the duration that this pin was either HI or LO. (MICROBIT_PIN_EVT_PULSE_HI, MICROBIT_PIN_EVT_PULSE_LO) |
LancasterUniversity | 49:88f03f3feff1 | 357 | * MICROBIT_PIN_EVENT_ON_TOUCH - Configures this pin as a makey makey style touch sensor, in the form of a MicroBitButton. Normal button events will be generated using the ID of this pin. |
LancasterUniversity | 49:88f03f3feff1 | 358 | * MICROBIT_PIN_EVENT_NONE - Disables events for this pin. |
LancasterUniversity | 49:88f03f3feff1 | 359 | * |
LancasterUniversity | 49:88f03f3feff1 | 360 | * @param eventType One of: MICROBIT_PIN_EVENT_ON_EDGE, MICROBIT_PIN_EVENT_ON_PULSE, MICROBIT_PIN_EVENT_ON_TOUCH, MICROBIT_PIN_EVENT_NONE |
LancasterUniversity | 49:88f03f3feff1 | 361 | * |
LancasterUniversity | 49:88f03f3feff1 | 362 | * @code |
LancasterUniversity | 49:88f03f3feff1 | 363 | * MicroBitMessageBus bus; |
LancasterUniversity | 49:88f03f3feff1 | 364 | * |
LancasterUniversity | 49:88f03f3feff1 | 365 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
LancasterUniversity | 49:88f03f3feff1 | 366 | * P0.eventOn(MICROBIT_PIN_EVENT_ON_PULSE); |
LancasterUniversity | 49:88f03f3feff1 | 367 | * |
LancasterUniversity | 49:88f03f3feff1 | 368 | * void onPulse(MicroBitEvent evt) |
LancasterUniversity | 49:88f03f3feff1 | 369 | * { |
LancasterUniversity | 49:88f03f3feff1 | 370 | * int duration = evt.timestamp; |
LancasterUniversity | 49:88f03f3feff1 | 371 | * } |
LancasterUniversity | 49:88f03f3feff1 | 372 | * |
LancasterUniversity | 49:88f03f3feff1 | 373 | * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_PIN_EVT_PULSE_HI, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE) |
LancasterUniversity | 49:88f03f3feff1 | 374 | * @endcode |
LancasterUniversity | 49:88f03f3feff1 | 375 | * |
LancasterUniversity | 49:88f03f3feff1 | 376 | * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the given eventype does not match |
LancasterUniversity | 49:88f03f3feff1 | 377 | * |
LancasterUniversity | 49:88f03f3feff1 | 378 | * @note In the MICROBIT_PIN_EVENT_ON_PULSE mode, the smallest pulse that was reliably detected was 85us, around 5khz. If more precision is required, |
LancasterUniversity | 49:88f03f3feff1 | 379 | * please use the InterruptIn class supplied by ARM mbed. |
LancasterUniversity | 49:88f03f3feff1 | 380 | */ |
LancasterUniversity | 49:88f03f3feff1 | 381 | int eventOn(int eventType); |
Jonathan Austin |
1:8aa5cdb4ab67 | 382 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 383 | |
LancasterUniversity | 35:8ce23bc1af38 | 384 | #endif |