Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/source/drivers/MicroBitPin.cpp@0:b9164b348919, 2016-10-17 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 17 12:41:20 2016 +0000
- Revision:
- 0:b9164b348919
Official Sheffield ARMBand Micro:bit program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 0:b9164b348919 | 1 | /* |
MrBedfordVan | 0:b9164b348919 | 2 | The MIT License (MIT) |
MrBedfordVan | 0:b9164b348919 | 3 | |
MrBedfordVan | 0:b9164b348919 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
MrBedfordVan | 0:b9164b348919 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
MrBedfordVan | 0:b9164b348919 | 6 | |
MrBedfordVan | 0:b9164b348919 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
MrBedfordVan | 0:b9164b348919 | 8 | copy of this software and associated documentation files (the "Software"), |
MrBedfordVan | 0:b9164b348919 | 9 | to deal in the Software without restriction, including without limitation |
MrBedfordVan | 0:b9164b348919 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MrBedfordVan | 0:b9164b348919 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
MrBedfordVan | 0:b9164b348919 | 12 | Software is furnished to do so, subject to the following conditions: |
MrBedfordVan | 0:b9164b348919 | 13 | |
MrBedfordVan | 0:b9164b348919 | 14 | The above copyright notice and this permission notice shall be included in |
MrBedfordVan | 0:b9164b348919 | 15 | all copies or substantial portions of the Software. |
MrBedfordVan | 0:b9164b348919 | 16 | |
MrBedfordVan | 0:b9164b348919 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MrBedfordVan | 0:b9164b348919 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MrBedfordVan | 0:b9164b348919 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
MrBedfordVan | 0:b9164b348919 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MrBedfordVan | 0:b9164b348919 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
MrBedfordVan | 0:b9164b348919 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
MrBedfordVan | 0:b9164b348919 | 23 | DEALINGS IN THE SOFTWARE. |
MrBedfordVan | 0:b9164b348919 | 24 | */ |
MrBedfordVan | 0:b9164b348919 | 25 | |
MrBedfordVan | 0:b9164b348919 | 26 | /** |
MrBedfordVan | 0:b9164b348919 | 27 | * Class definition for MicroBitPin. |
MrBedfordVan | 0:b9164b348919 | 28 | * |
MrBedfordVan | 0:b9164b348919 | 29 | * Commonly represents an I/O pin on the edge connector. |
MrBedfordVan | 0:b9164b348919 | 30 | */ |
MrBedfordVan | 0:b9164b348919 | 31 | #include "MicroBitConfig.h" |
MrBedfordVan | 0:b9164b348919 | 32 | #include "MicroBitPin.h" |
MrBedfordVan | 0:b9164b348919 | 33 | #include "MicroBitButton.h" |
MrBedfordVan | 0:b9164b348919 | 34 | #include "MicroBitSystemTimer.h" |
MrBedfordVan | 0:b9164b348919 | 35 | #include "TimedInterruptIn.h" |
MrBedfordVan | 0:b9164b348919 | 36 | #include "DynamicPwm.h" |
MrBedfordVan | 0:b9164b348919 | 37 | #include "ErrorNo.h" |
MrBedfordVan | 0:b9164b348919 | 38 | |
MrBedfordVan | 0:b9164b348919 | 39 | /** |
MrBedfordVan | 0:b9164b348919 | 40 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 41 | * Create a MicroBitPin instance, generally used to represent a pin on the edge connector. |
MrBedfordVan | 0:b9164b348919 | 42 | * |
MrBedfordVan | 0:b9164b348919 | 43 | * @param id the unique EventModel id of this component. |
MrBedfordVan | 0:b9164b348919 | 44 | * |
MrBedfordVan | 0:b9164b348919 | 45 | * @param name the mbed PinName for this MicroBitPin instance. |
MrBedfordVan | 0:b9164b348919 | 46 | * |
MrBedfordVan | 0:b9164b348919 | 47 | * @param capability the capabilities this MicroBitPin instance should have. |
MrBedfordVan | 0:b9164b348919 | 48 | * (PIN_CAPABILITY_DIGITAL, PIN_CAPABILITY_ANALOG, PIN_CAPABILITY_AD, PIN_CAPABILITY_ALL) |
MrBedfordVan | 0:b9164b348919 | 49 | * |
MrBedfordVan | 0:b9164b348919 | 50 | * @code |
MrBedfordVan | 0:b9164b348919 | 51 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL); |
MrBedfordVan | 0:b9164b348919 | 52 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 53 | */ |
MrBedfordVan | 0:b9164b348919 | 54 | MicroBitPin::MicroBitPin(int id, PinName name, PinCapability capability) |
MrBedfordVan | 0:b9164b348919 | 55 | { |
MrBedfordVan | 0:b9164b348919 | 56 | //set mandatory attributes |
MrBedfordVan | 0:b9164b348919 | 57 | this->id = id; |
MrBedfordVan | 0:b9164b348919 | 58 | this->name = name; |
MrBedfordVan | 0:b9164b348919 | 59 | this->capability = capability; |
MrBedfordVan | 0:b9164b348919 | 60 | this->pullMode = MICROBIT_DEFAULT_PULLMODE; |
MrBedfordVan | 0:b9164b348919 | 61 | |
MrBedfordVan | 0:b9164b348919 | 62 | // Power up in a disconnected, low power state. |
MrBedfordVan | 0:b9164b348919 | 63 | // If we're unused, this is how it will stay... |
MrBedfordVan | 0:b9164b348919 | 64 | this->status = 0x00; |
MrBedfordVan | 0:b9164b348919 | 65 | this->pin = NULL; |
MrBedfordVan | 0:b9164b348919 | 66 | |
MrBedfordVan | 0:b9164b348919 | 67 | } |
MrBedfordVan | 0:b9164b348919 | 68 | |
MrBedfordVan | 0:b9164b348919 | 69 | /** |
MrBedfordVan | 0:b9164b348919 | 70 | * Disconnect any attached mBed IO from this pin. |
MrBedfordVan | 0:b9164b348919 | 71 | * |
MrBedfordVan | 0:b9164b348919 | 72 | * Used only when pin changes mode (i.e. Input/Output/Analog/Digital) |
MrBedfordVan | 0:b9164b348919 | 73 | */ |
MrBedfordVan | 0:b9164b348919 | 74 | void MicroBitPin::disconnect() |
MrBedfordVan | 0:b9164b348919 | 75 | { |
MrBedfordVan | 0:b9164b348919 | 76 | // This is a bit ugly, but rarely used code. |
MrBedfordVan | 0:b9164b348919 | 77 | // It would be much better to use some polymorphism here, but the mBed I/O classes aren't arranged in an inheritance hierarchy... yet. :-) |
MrBedfordVan | 0:b9164b348919 | 78 | if (status & IO_STATUS_DIGITAL_IN) |
MrBedfordVan | 0:b9164b348919 | 79 | delete ((DigitalIn *)pin); |
MrBedfordVan | 0:b9164b348919 | 80 | |
MrBedfordVan | 0:b9164b348919 | 81 | if (status & IO_STATUS_DIGITAL_OUT) |
MrBedfordVan | 0:b9164b348919 | 82 | delete ((DigitalOut *)pin); |
MrBedfordVan | 0:b9164b348919 | 83 | |
MrBedfordVan | 0:b9164b348919 | 84 | if (status & IO_STATUS_ANALOG_IN){ |
MrBedfordVan | 0:b9164b348919 | 85 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; // forcibly disable the ADC - BUG in mbed.... |
MrBedfordVan | 0:b9164b348919 | 86 | delete ((AnalogIn *)pin); |
MrBedfordVan | 0:b9164b348919 | 87 | } |
MrBedfordVan | 0:b9164b348919 | 88 | |
MrBedfordVan | 0:b9164b348919 | 89 | if (status & IO_STATUS_ANALOG_OUT) |
MrBedfordVan | 0:b9164b348919 | 90 | { |
MrBedfordVan | 0:b9164b348919 | 91 | if(((DynamicPwm *)pin)->getPinName() == name) |
MrBedfordVan | 0:b9164b348919 | 92 | ((DynamicPwm *)pin)->release(); |
MrBedfordVan | 0:b9164b348919 | 93 | } |
MrBedfordVan | 0:b9164b348919 | 94 | |
MrBedfordVan | 0:b9164b348919 | 95 | if (status & IO_STATUS_TOUCH_IN) |
MrBedfordVan | 0:b9164b348919 | 96 | delete ((MicroBitButton *)pin); |
MrBedfordVan | 0:b9164b348919 | 97 | |
MrBedfordVan | 0:b9164b348919 | 98 | if ((status & IO_STATUS_EVENT_ON_EDGE) || (status & IO_STATUS_EVENT_PULSE_ON_EDGE)) |
MrBedfordVan | 0:b9164b348919 | 99 | delete ((TimedInterruptIn *)pin); |
MrBedfordVan | 0:b9164b348919 | 100 | |
MrBedfordVan | 0:b9164b348919 | 101 | this->pin = NULL; |
MrBedfordVan | 0:b9164b348919 | 102 | this->status = 0; |
MrBedfordVan | 0:b9164b348919 | 103 | } |
MrBedfordVan | 0:b9164b348919 | 104 | |
MrBedfordVan | 0:b9164b348919 | 105 | /** |
MrBedfordVan | 0:b9164b348919 | 106 | * Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'. |
MrBedfordVan | 0:b9164b348919 | 107 | * |
MrBedfordVan | 0:b9164b348919 | 108 | * @param value 0 (LO) or 1 (HI) |
MrBedfordVan | 0:b9164b348919 | 109 | * |
MrBedfordVan | 0:b9164b348919 | 110 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
MrBedfordVan | 0:b9164b348919 | 111 | * if the given pin does not have digital capability. |
MrBedfordVan | 0:b9164b348919 | 112 | * |
MrBedfordVan | 0:b9164b348919 | 113 | * @code |
MrBedfordVan | 0:b9164b348919 | 114 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
MrBedfordVan | 0:b9164b348919 | 115 | * P0.setDigitalValue(1); // P0 is now HI |
MrBedfordVan | 0:b9164b348919 | 116 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 117 | */ |
MrBedfordVan | 0:b9164b348919 | 118 | int MicroBitPin::setDigitalValue(int value) |
MrBedfordVan | 0:b9164b348919 | 119 | { |
MrBedfordVan | 0:b9164b348919 | 120 | // Check if this pin has a digital mode... |
MrBedfordVan | 0:b9164b348919 | 121 | if(!(PIN_CAPABILITY_DIGITAL & capability)) |
MrBedfordVan | 0:b9164b348919 | 122 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 123 | |
MrBedfordVan | 0:b9164b348919 | 124 | // Ensure we have a valid value. |
MrBedfordVan | 0:b9164b348919 | 125 | if (value < 0 || value > 1) |
MrBedfordVan | 0:b9164b348919 | 126 | return MICROBIT_INVALID_PARAMETER; |
MrBedfordVan | 0:b9164b348919 | 127 | |
MrBedfordVan | 0:b9164b348919 | 128 | // Move into a Digital input state if necessary. |
MrBedfordVan | 0:b9164b348919 | 129 | if (!(status & IO_STATUS_DIGITAL_OUT)){ |
MrBedfordVan | 0:b9164b348919 | 130 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 131 | pin = new DigitalOut(name); |
MrBedfordVan | 0:b9164b348919 | 132 | status |= IO_STATUS_DIGITAL_OUT; |
MrBedfordVan | 0:b9164b348919 | 133 | } |
MrBedfordVan | 0:b9164b348919 | 134 | |
MrBedfordVan | 0:b9164b348919 | 135 | // Write the value. |
MrBedfordVan | 0:b9164b348919 | 136 | ((DigitalOut *)pin)->write(value); |
MrBedfordVan | 0:b9164b348919 | 137 | |
MrBedfordVan | 0:b9164b348919 | 138 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 139 | } |
MrBedfordVan | 0:b9164b348919 | 140 | |
MrBedfordVan | 0:b9164b348919 | 141 | /** |
MrBedfordVan | 0:b9164b348919 | 142 | * Configures this IO pin as a digital input (if necessary) and tests its current value. |
MrBedfordVan | 0:b9164b348919 | 143 | * |
MrBedfordVan | 0:b9164b348919 | 144 | * |
MrBedfordVan | 0:b9164b348919 | 145 | * @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED |
MrBedfordVan | 0:b9164b348919 | 146 | * if the given pin does not have digital capability. |
MrBedfordVan | 0:b9164b348919 | 147 | * |
MrBedfordVan | 0:b9164b348919 | 148 | * @code |
MrBedfordVan | 0:b9164b348919 | 149 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
MrBedfordVan | 0:b9164b348919 | 150 | * P0.getDigitalValue(); // P0 is either 0 or 1; |
MrBedfordVan | 0:b9164b348919 | 151 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 152 | */ |
MrBedfordVan | 0:b9164b348919 | 153 | int MicroBitPin::getDigitalValue() |
MrBedfordVan | 0:b9164b348919 | 154 | { |
MrBedfordVan | 0:b9164b348919 | 155 | //check if this pin has a digital mode... |
MrBedfordVan | 0:b9164b348919 | 156 | if(!(PIN_CAPABILITY_DIGITAL & capability)) |
MrBedfordVan | 0:b9164b348919 | 157 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 158 | |
MrBedfordVan | 0:b9164b348919 | 159 | // Move into a Digital input state if necessary. |
MrBedfordVan | 0:b9164b348919 | 160 | if (!(status & (IO_STATUS_DIGITAL_IN | IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE))) |
MrBedfordVan | 0:b9164b348919 | 161 | { |
MrBedfordVan | 0:b9164b348919 | 162 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 163 | pin = new DigitalIn(name, (PinMode)pullMode); |
MrBedfordVan | 0:b9164b348919 | 164 | status |= IO_STATUS_DIGITAL_IN; |
MrBedfordVan | 0:b9164b348919 | 165 | } |
MrBedfordVan | 0:b9164b348919 | 166 | |
MrBedfordVan | 0:b9164b348919 | 167 | if(status & (IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE)) |
MrBedfordVan | 0:b9164b348919 | 168 | return ((TimedInterruptIn *)pin)->read(); |
MrBedfordVan | 0:b9164b348919 | 169 | |
MrBedfordVan | 0:b9164b348919 | 170 | return ((DigitalIn *)pin)->read(); |
MrBedfordVan | 0:b9164b348919 | 171 | } |
MrBedfordVan | 0:b9164b348919 | 172 | |
MrBedfordVan | 0:b9164b348919 | 173 | /** |
MrBedfordVan | 0:b9164b348919 | 174 | * Configures this IO pin as a digital input with the specified internal pull-up/pull-down configuraiton (if necessary) and tests its current value. |
MrBedfordVan | 0:b9164b348919 | 175 | * |
MrBedfordVan | 0:b9164b348919 | 176 | * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone |
MrBedfordVan | 0:b9164b348919 | 177 | * |
MrBedfordVan | 0:b9164b348919 | 178 | * @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED |
MrBedfordVan | 0:b9164b348919 | 179 | * if the given pin does not have digital capability. |
MrBedfordVan | 0:b9164b348919 | 180 | * |
MrBedfordVan | 0:b9164b348919 | 181 | * @code |
MrBedfordVan | 0:b9164b348919 | 182 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
MrBedfordVan | 0:b9164b348919 | 183 | * P0.getDigitalValue(PullUp); // P0 is either 0 or 1; |
MrBedfordVan | 0:b9164b348919 | 184 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 185 | */ |
MrBedfordVan | 0:b9164b348919 | 186 | int MicroBitPin::getDigitalValue(PinMode pull) |
MrBedfordVan | 0:b9164b348919 | 187 | { |
MrBedfordVan | 0:b9164b348919 | 188 | setPull(pull); |
MrBedfordVan | 0:b9164b348919 | 189 | return getDigitalValue(); |
MrBedfordVan | 0:b9164b348919 | 190 | } |
MrBedfordVan | 0:b9164b348919 | 191 | |
MrBedfordVan | 0:b9164b348919 | 192 | int MicroBitPin::obtainAnalogChannel() |
MrBedfordVan | 0:b9164b348919 | 193 | { |
MrBedfordVan | 0:b9164b348919 | 194 | // Move into an analogue input state if necessary, if we are no longer the focus of a DynamicPWM instance, allocate ourselves again! |
MrBedfordVan | 0:b9164b348919 | 195 | if (!(status & IO_STATUS_ANALOG_OUT) || !(((DynamicPwm *)pin)->getPinName() == name)){ |
MrBedfordVan | 0:b9164b348919 | 196 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 197 | pin = (void *)DynamicPwm::allocate(name); |
MrBedfordVan | 0:b9164b348919 | 198 | status |= IO_STATUS_ANALOG_OUT; |
MrBedfordVan | 0:b9164b348919 | 199 | } |
MrBedfordVan | 0:b9164b348919 | 200 | |
MrBedfordVan | 0:b9164b348919 | 201 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 202 | } |
MrBedfordVan | 0:b9164b348919 | 203 | |
MrBedfordVan | 0:b9164b348919 | 204 | /** |
MrBedfordVan | 0:b9164b348919 | 205 | * Configures this IO pin as an analog/pwm output, and change the output value to the given level. |
MrBedfordVan | 0:b9164b348919 | 206 | * |
MrBedfordVan | 0:b9164b348919 | 207 | * @param value the level to set on the output pin, in the range 0 - 1024 |
MrBedfordVan | 0:b9164b348919 | 208 | * |
MrBedfordVan | 0:b9164b348919 | 209 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
MrBedfordVan | 0:b9164b348919 | 210 | * if the given pin does not have analog capability. |
MrBedfordVan | 0:b9164b348919 | 211 | */ |
MrBedfordVan | 0:b9164b348919 | 212 | int MicroBitPin::setAnalogValue(int value) |
MrBedfordVan | 0:b9164b348919 | 213 | { |
MrBedfordVan | 0:b9164b348919 | 214 | //check if this pin has an analogue mode... |
MrBedfordVan | 0:b9164b348919 | 215 | if(!(PIN_CAPABILITY_ANALOG & capability)) |
MrBedfordVan | 0:b9164b348919 | 216 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 217 | |
MrBedfordVan | 0:b9164b348919 | 218 | //sanitise the level value |
MrBedfordVan | 0:b9164b348919 | 219 | if(value < 0 || value > MICROBIT_PIN_MAX_OUTPUT) |
MrBedfordVan | 0:b9164b348919 | 220 | return MICROBIT_INVALID_PARAMETER; |
MrBedfordVan | 0:b9164b348919 | 221 | |
MrBedfordVan | 0:b9164b348919 | 222 | float level = (float)value / float(MICROBIT_PIN_MAX_OUTPUT); |
MrBedfordVan | 0:b9164b348919 | 223 | |
MrBedfordVan | 0:b9164b348919 | 224 | //obtain use of the DynamicPwm instance, if it has changed / configure if we do not have one |
MrBedfordVan | 0:b9164b348919 | 225 | if(obtainAnalogChannel() == MICROBIT_OK) |
MrBedfordVan | 0:b9164b348919 | 226 | return ((DynamicPwm *)pin)->write(level); |
MrBedfordVan | 0:b9164b348919 | 227 | |
MrBedfordVan | 0:b9164b348919 | 228 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 229 | } |
MrBedfordVan | 0:b9164b348919 | 230 | |
MrBedfordVan | 0:b9164b348919 | 231 | /** |
MrBedfordVan | 0:b9164b348919 | 232 | * Configures this IO pin as an analog/pwm output (if necessary) and configures the period to be 20ms, |
MrBedfordVan | 0:b9164b348919 | 233 | * with a duty cycle between 500 us and 2500 us. |
MrBedfordVan | 0:b9164b348919 | 234 | * |
MrBedfordVan | 0:b9164b348919 | 235 | * 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. |
MrBedfordVan | 0:b9164b348919 | 236 | * |
MrBedfordVan | 0:b9164b348919 | 237 | * This range can be modified to fine tune, and also tolerate different servos. |
MrBedfordVan | 0:b9164b348919 | 238 | * |
MrBedfordVan | 0:b9164b348919 | 239 | * @param value the level to set on the output pin, in the range 0 - 180. |
MrBedfordVan | 0:b9164b348919 | 240 | * |
MrBedfordVan | 0:b9164b348919 | 241 | * @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. |
MrBedfordVan | 0:b9164b348919 | 242 | * |
MrBedfordVan | 0:b9164b348919 | 243 | * @param center the center point from which to calculate the lower and upper bounds. Defaults to MICROBIT_PIN_DEFAULT_SERVO_CENTER |
MrBedfordVan | 0:b9164b348919 | 244 | * |
MrBedfordVan | 0:b9164b348919 | 245 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
MrBedfordVan | 0:b9164b348919 | 246 | * if the given pin does not have analog capability. |
MrBedfordVan | 0:b9164b348919 | 247 | */ |
MrBedfordVan | 0:b9164b348919 | 248 | int MicroBitPin::setServoValue(int value, int range, int center) |
MrBedfordVan | 0:b9164b348919 | 249 | { |
MrBedfordVan | 0:b9164b348919 | 250 | //check if this pin has an analogue mode... |
MrBedfordVan | 0:b9164b348919 | 251 | if(!(PIN_CAPABILITY_ANALOG & capability)) |
MrBedfordVan | 0:b9164b348919 | 252 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 253 | |
MrBedfordVan | 0:b9164b348919 | 254 | //sanitise the servo level |
MrBedfordVan | 0:b9164b348919 | 255 | if(value < 0 || range < 1 || center < 1) |
MrBedfordVan | 0:b9164b348919 | 256 | return MICROBIT_INVALID_PARAMETER; |
MrBedfordVan | 0:b9164b348919 | 257 | |
MrBedfordVan | 0:b9164b348919 | 258 | //clip - just in case |
MrBedfordVan | 0:b9164b348919 | 259 | if(value > MICROBIT_PIN_MAX_SERVO_RANGE) |
MrBedfordVan | 0:b9164b348919 | 260 | value = MICROBIT_PIN_MAX_SERVO_RANGE; |
MrBedfordVan | 0:b9164b348919 | 261 | |
MrBedfordVan | 0:b9164b348919 | 262 | //calculate the lower bound based on the midpoint |
MrBedfordVan | 0:b9164b348919 | 263 | int lower = (center - (range / 2)) * 1000; |
MrBedfordVan | 0:b9164b348919 | 264 | |
MrBedfordVan | 0:b9164b348919 | 265 | value = value * 1000; |
MrBedfordVan | 0:b9164b348919 | 266 | |
MrBedfordVan | 0:b9164b348919 | 267 | //add the percentage of the range based on the value between 0 and 180 |
MrBedfordVan | 0:b9164b348919 | 268 | int scaled = lower + (range * (value / MICROBIT_PIN_MAX_SERVO_RANGE)); |
MrBedfordVan | 0:b9164b348919 | 269 | |
MrBedfordVan | 0:b9164b348919 | 270 | return setServoPulseUs(scaled / 1000); |
MrBedfordVan | 0:b9164b348919 | 271 | } |
MrBedfordVan | 0:b9164b348919 | 272 | |
MrBedfordVan | 0:b9164b348919 | 273 | /** |
MrBedfordVan | 0:b9164b348919 | 274 | * Configures this IO pin as an analogue input (if necessary), and samples the Pin for its analog value. |
MrBedfordVan | 0:b9164b348919 | 275 | * |
MrBedfordVan | 0:b9164b348919 | 276 | * @return the current analogue level on the pin, in the range 0 - 1024, or |
MrBedfordVan | 0:b9164b348919 | 277 | * MICROBIT_NOT_SUPPORTED if the given pin does not have analog capability. |
MrBedfordVan | 0:b9164b348919 | 278 | * |
MrBedfordVan | 0:b9164b348919 | 279 | * @code |
MrBedfordVan | 0:b9164b348919 | 280 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
MrBedfordVan | 0:b9164b348919 | 281 | * P0.getAnalogValue(); // P0 is a value in the range of 0 - 1024 |
MrBedfordVan | 0:b9164b348919 | 282 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 283 | */ |
MrBedfordVan | 0:b9164b348919 | 284 | int MicroBitPin::getAnalogValue() |
MrBedfordVan | 0:b9164b348919 | 285 | { |
MrBedfordVan | 0:b9164b348919 | 286 | //check if this pin has an analogue mode... |
MrBedfordVan | 0:b9164b348919 | 287 | if(!(PIN_CAPABILITY_ANALOG & capability)) |
MrBedfordVan | 0:b9164b348919 | 288 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 289 | |
MrBedfordVan | 0:b9164b348919 | 290 | // Move into an analogue input state if necessary. |
MrBedfordVan | 0:b9164b348919 | 291 | if (!(status & IO_STATUS_ANALOG_IN)){ |
MrBedfordVan | 0:b9164b348919 | 292 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 293 | pin = new AnalogIn(name); |
MrBedfordVan | 0:b9164b348919 | 294 | status |= IO_STATUS_ANALOG_IN; |
MrBedfordVan | 0:b9164b348919 | 295 | } |
MrBedfordVan | 0:b9164b348919 | 296 | |
MrBedfordVan | 0:b9164b348919 | 297 | //perform a read! |
MrBedfordVan | 0:b9164b348919 | 298 | return ((AnalogIn *)pin)->read_u16(); |
MrBedfordVan | 0:b9164b348919 | 299 | } |
MrBedfordVan | 0:b9164b348919 | 300 | |
MrBedfordVan | 0:b9164b348919 | 301 | /** |
MrBedfordVan | 0:b9164b348919 | 302 | * Determines if this IO pin is currently configured as an input. |
MrBedfordVan | 0:b9164b348919 | 303 | * |
MrBedfordVan | 0:b9164b348919 | 304 | * @return 1 if pin is an analog or digital input, 0 otherwise. |
MrBedfordVan | 0:b9164b348919 | 305 | */ |
MrBedfordVan | 0:b9164b348919 | 306 | int MicroBitPin::isInput() |
MrBedfordVan | 0:b9164b348919 | 307 | { |
MrBedfordVan | 0:b9164b348919 | 308 | return (status & (IO_STATUS_DIGITAL_IN | IO_STATUS_ANALOG_IN)) == 0 ? 0 : 1; |
MrBedfordVan | 0:b9164b348919 | 309 | } |
MrBedfordVan | 0:b9164b348919 | 310 | |
MrBedfordVan | 0:b9164b348919 | 311 | /** |
MrBedfordVan | 0:b9164b348919 | 312 | * Determines if this IO pin is currently configured as an output. |
MrBedfordVan | 0:b9164b348919 | 313 | * |
MrBedfordVan | 0:b9164b348919 | 314 | * @return 1 if pin is an analog or digital output, 0 otherwise. |
MrBedfordVan | 0:b9164b348919 | 315 | */ |
MrBedfordVan | 0:b9164b348919 | 316 | int MicroBitPin::isOutput() |
MrBedfordVan | 0:b9164b348919 | 317 | { |
MrBedfordVan | 0:b9164b348919 | 318 | return (status & (IO_STATUS_DIGITAL_OUT | IO_STATUS_ANALOG_OUT)) == 0 ? 0 : 1; |
MrBedfordVan | 0:b9164b348919 | 319 | } |
MrBedfordVan | 0:b9164b348919 | 320 | |
MrBedfordVan | 0:b9164b348919 | 321 | /** |
MrBedfordVan | 0:b9164b348919 | 322 | * Determines if this IO pin is currently configured for digital use. |
MrBedfordVan | 0:b9164b348919 | 323 | * |
MrBedfordVan | 0:b9164b348919 | 324 | * @return 1 if pin is digital, 0 otherwise. |
MrBedfordVan | 0:b9164b348919 | 325 | */ |
MrBedfordVan | 0:b9164b348919 | 326 | int MicroBitPin::isDigital() |
MrBedfordVan | 0:b9164b348919 | 327 | { |
MrBedfordVan | 0:b9164b348919 | 328 | return (status & (IO_STATUS_DIGITAL_IN | IO_STATUS_DIGITAL_OUT)) == 0 ? 0 : 1; |
MrBedfordVan | 0:b9164b348919 | 329 | } |
MrBedfordVan | 0:b9164b348919 | 330 | |
MrBedfordVan | 0:b9164b348919 | 331 | /** |
MrBedfordVan | 0:b9164b348919 | 332 | * Determines if this IO pin is currently configured for analog use. |
MrBedfordVan | 0:b9164b348919 | 333 | * |
MrBedfordVan | 0:b9164b348919 | 334 | * @return 1 if pin is analog, 0 otherwise. |
MrBedfordVan | 0:b9164b348919 | 335 | */ |
MrBedfordVan | 0:b9164b348919 | 336 | int MicroBitPin::isAnalog() |
MrBedfordVan | 0:b9164b348919 | 337 | { |
MrBedfordVan | 0:b9164b348919 | 338 | return (status & (IO_STATUS_ANALOG_IN | IO_STATUS_ANALOG_OUT)) == 0 ? 0 : 1; |
MrBedfordVan | 0:b9164b348919 | 339 | } |
MrBedfordVan | 0:b9164b348919 | 340 | |
MrBedfordVan | 0:b9164b348919 | 341 | /** |
MrBedfordVan | 0:b9164b348919 | 342 | * Configures this IO pin as a "makey makey" style touch sensor (if necessary) |
MrBedfordVan | 0:b9164b348919 | 343 | * and tests its current debounced state. |
MrBedfordVan | 0:b9164b348919 | 344 | * |
MrBedfordVan | 0:b9164b348919 | 345 | * Users can also subscribe to MicroBitButton events generated from this pin. |
MrBedfordVan | 0:b9164b348919 | 346 | * |
MrBedfordVan | 0:b9164b348919 | 347 | * @return 1 if pin is touched, 0 if not, or MICROBIT_NOT_SUPPORTED if this pin does not support touch capability. |
MrBedfordVan | 0:b9164b348919 | 348 | * |
MrBedfordVan | 0:b9164b348919 | 349 | * @code |
MrBedfordVan | 0:b9164b348919 | 350 | * MicroBitMessageBus bus; |
MrBedfordVan | 0:b9164b348919 | 351 | * |
MrBedfordVan | 0:b9164b348919 | 352 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL); |
MrBedfordVan | 0:b9164b348919 | 353 | * if(P0.isTouched()) |
MrBedfordVan | 0:b9164b348919 | 354 | * { |
MrBedfordVan | 0:b9164b348919 | 355 | * //do something! |
MrBedfordVan | 0:b9164b348919 | 356 | * } |
MrBedfordVan | 0:b9164b348919 | 357 | * |
MrBedfordVan | 0:b9164b348919 | 358 | * // subscribe to events generated by this pin! |
MrBedfordVan | 0:b9164b348919 | 359 | * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_BUTTON_EVT_CLICK, someFunction); |
MrBedfordVan | 0:b9164b348919 | 360 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 361 | */ |
MrBedfordVan | 0:b9164b348919 | 362 | int MicroBitPin::isTouched() |
MrBedfordVan | 0:b9164b348919 | 363 | { |
MrBedfordVan | 0:b9164b348919 | 364 | //check if this pin has a touch mode... |
MrBedfordVan | 0:b9164b348919 | 365 | if(!(PIN_CAPABILITY_DIGITAL & capability)) |
MrBedfordVan | 0:b9164b348919 | 366 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 367 | |
MrBedfordVan | 0:b9164b348919 | 368 | // Move into a touch input state if necessary. |
MrBedfordVan | 0:b9164b348919 | 369 | if (!(status & IO_STATUS_TOUCH_IN)){ |
MrBedfordVan | 0:b9164b348919 | 370 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 371 | pin = new MicroBitButton(name, id); |
MrBedfordVan | 0:b9164b348919 | 372 | status |= IO_STATUS_TOUCH_IN; |
MrBedfordVan | 0:b9164b348919 | 373 | } |
MrBedfordVan | 0:b9164b348919 | 374 | |
MrBedfordVan | 0:b9164b348919 | 375 | return ((MicroBitButton *)pin)->isPressed(); |
MrBedfordVan | 0:b9164b348919 | 376 | } |
MrBedfordVan | 0:b9164b348919 | 377 | |
MrBedfordVan | 0:b9164b348919 | 378 | /** |
MrBedfordVan | 0:b9164b348919 | 379 | * Configures this IO pin as an analog/pwm output if it isn't already, configures the period to be 20ms, |
MrBedfordVan | 0:b9164b348919 | 380 | * and sets the pulse width, based on the value it is given. |
MrBedfordVan | 0:b9164b348919 | 381 | * |
MrBedfordVan | 0:b9164b348919 | 382 | * @param pulseWidth the desired pulse width in microseconds. |
MrBedfordVan | 0:b9164b348919 | 383 | * |
MrBedfordVan | 0:b9164b348919 | 384 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED |
MrBedfordVan | 0:b9164b348919 | 385 | * if the given pin does not have analog capability. |
MrBedfordVan | 0:b9164b348919 | 386 | */ |
MrBedfordVan | 0:b9164b348919 | 387 | int MicroBitPin::setServoPulseUs(int pulseWidth) |
MrBedfordVan | 0:b9164b348919 | 388 | { |
MrBedfordVan | 0:b9164b348919 | 389 | //check if this pin has an analogue mode... |
MrBedfordVan | 0:b9164b348919 | 390 | if(!(PIN_CAPABILITY_ANALOG & capability)) |
MrBedfordVan | 0:b9164b348919 | 391 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 392 | |
MrBedfordVan | 0:b9164b348919 | 393 | //sanitise the pulse width |
MrBedfordVan | 0:b9164b348919 | 394 | if(pulseWidth < 0) |
MrBedfordVan | 0:b9164b348919 | 395 | return MICROBIT_INVALID_PARAMETER; |
MrBedfordVan | 0:b9164b348919 | 396 | |
MrBedfordVan | 0:b9164b348919 | 397 | //Check we still have the control over the DynamicPwm instance |
MrBedfordVan | 0:b9164b348919 | 398 | if(obtainAnalogChannel() == MICROBIT_OK) |
MrBedfordVan | 0:b9164b348919 | 399 | { |
MrBedfordVan | 0:b9164b348919 | 400 | //check if the period is set to 20ms |
MrBedfordVan | 0:b9164b348919 | 401 | if(((DynamicPwm *)pin)->getPeriodUs() != MICROBIT_DEFAULT_PWM_PERIOD) |
MrBedfordVan | 0:b9164b348919 | 402 | ((DynamicPwm *)pin)->setPeriodUs(MICROBIT_DEFAULT_PWM_PERIOD); |
MrBedfordVan | 0:b9164b348919 | 403 | |
MrBedfordVan | 0:b9164b348919 | 404 | ((DynamicPwm *)pin)->pulsewidth_us(pulseWidth); |
MrBedfordVan | 0:b9164b348919 | 405 | } |
MrBedfordVan | 0:b9164b348919 | 406 | |
MrBedfordVan | 0:b9164b348919 | 407 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 408 | } |
MrBedfordVan | 0:b9164b348919 | 409 | |
MrBedfordVan | 0:b9164b348919 | 410 | /** |
MrBedfordVan | 0:b9164b348919 | 411 | * Configures the PWM period of the analog output to the given value. |
MrBedfordVan | 0:b9164b348919 | 412 | * |
MrBedfordVan | 0:b9164b348919 | 413 | * @param period The new period for the analog output in microseconds. |
MrBedfordVan | 0:b9164b348919 | 414 | * |
MrBedfordVan | 0:b9164b348919 | 415 | * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the |
MrBedfordVan | 0:b9164b348919 | 416 | * given pin is not configured as an analog output. |
MrBedfordVan | 0:b9164b348919 | 417 | */ |
MrBedfordVan | 0:b9164b348919 | 418 | int MicroBitPin::setAnalogPeriodUs(int period) |
MrBedfordVan | 0:b9164b348919 | 419 | { |
MrBedfordVan | 0:b9164b348919 | 420 | if (!(status & IO_STATUS_ANALOG_OUT)) |
MrBedfordVan | 0:b9164b348919 | 421 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 422 | |
MrBedfordVan | 0:b9164b348919 | 423 | return ((DynamicPwm *)pin)->setPeriodUs(period); |
MrBedfordVan | 0:b9164b348919 | 424 | } |
MrBedfordVan | 0:b9164b348919 | 425 | |
MrBedfordVan | 0:b9164b348919 | 426 | /** |
MrBedfordVan | 0:b9164b348919 | 427 | * Configures the PWM period of the analog output to the given value. |
MrBedfordVan | 0:b9164b348919 | 428 | * |
MrBedfordVan | 0:b9164b348919 | 429 | * @param period The new period for the analog output in milliseconds. |
MrBedfordVan | 0:b9164b348919 | 430 | * |
MrBedfordVan | 0:b9164b348919 | 431 | * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the |
MrBedfordVan | 0:b9164b348919 | 432 | * given pin is not configured as an analog output. |
MrBedfordVan | 0:b9164b348919 | 433 | */ |
MrBedfordVan | 0:b9164b348919 | 434 | int MicroBitPin::setAnalogPeriod(int period) |
MrBedfordVan | 0:b9164b348919 | 435 | { |
MrBedfordVan | 0:b9164b348919 | 436 | return setAnalogPeriodUs(period*1000); |
MrBedfordVan | 0:b9164b348919 | 437 | } |
MrBedfordVan | 0:b9164b348919 | 438 | |
MrBedfordVan | 0:b9164b348919 | 439 | /** |
MrBedfordVan | 0:b9164b348919 | 440 | * Obtains the PWM period of the analog output in microseconds. |
MrBedfordVan | 0:b9164b348919 | 441 | * |
MrBedfordVan | 0:b9164b348919 | 442 | * @return the period on success, or MICROBIT_NOT_SUPPORTED if the |
MrBedfordVan | 0:b9164b348919 | 443 | * given pin is not configured as an analog output. |
MrBedfordVan | 0:b9164b348919 | 444 | */ |
MrBedfordVan | 0:b9164b348919 | 445 | int MicroBitPin::getAnalogPeriodUs() |
MrBedfordVan | 0:b9164b348919 | 446 | { |
MrBedfordVan | 0:b9164b348919 | 447 | if (!(status & IO_STATUS_ANALOG_OUT)) |
MrBedfordVan | 0:b9164b348919 | 448 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 449 | |
MrBedfordVan | 0:b9164b348919 | 450 | return ((DynamicPwm *)pin)->getPeriodUs(); |
MrBedfordVan | 0:b9164b348919 | 451 | } |
MrBedfordVan | 0:b9164b348919 | 452 | |
MrBedfordVan | 0:b9164b348919 | 453 | /** |
MrBedfordVan | 0:b9164b348919 | 454 | * Obtains the PWM period of the analog output in milliseconds. |
MrBedfordVan | 0:b9164b348919 | 455 | * |
MrBedfordVan | 0:b9164b348919 | 456 | * @return the period on success, or MICROBIT_NOT_SUPPORTED if the |
MrBedfordVan | 0:b9164b348919 | 457 | * given pin is not configured as an analog output. |
MrBedfordVan | 0:b9164b348919 | 458 | */ |
MrBedfordVan | 0:b9164b348919 | 459 | int MicroBitPin::getAnalogPeriod() |
MrBedfordVan | 0:b9164b348919 | 460 | { |
MrBedfordVan | 0:b9164b348919 | 461 | return getAnalogPeriodUs()/1000; |
MrBedfordVan | 0:b9164b348919 | 462 | } |
MrBedfordVan | 0:b9164b348919 | 463 | |
MrBedfordVan | 0:b9164b348919 | 464 | /** |
MrBedfordVan | 0:b9164b348919 | 465 | * Configures the pull of this pin. |
MrBedfordVan | 0:b9164b348919 | 466 | * |
MrBedfordVan | 0:b9164b348919 | 467 | * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone |
MrBedfordVan | 0:b9164b348919 | 468 | * |
MrBedfordVan | 0:b9164b348919 | 469 | * @return MICROBIT_NOT_SUPPORTED if the current pin configuration is anything other |
MrBedfordVan | 0:b9164b348919 | 470 | * than a digital input, otherwise MICROBIT_OK. |
MrBedfordVan | 0:b9164b348919 | 471 | */ |
MrBedfordVan | 0:b9164b348919 | 472 | int MicroBitPin::setPull(PinMode pull) |
MrBedfordVan | 0:b9164b348919 | 473 | { |
MrBedfordVan | 0:b9164b348919 | 474 | pullMode = pull; |
MrBedfordVan | 0:b9164b348919 | 475 | |
MrBedfordVan | 0:b9164b348919 | 476 | if ((status & IO_STATUS_DIGITAL_IN)) |
MrBedfordVan | 0:b9164b348919 | 477 | { |
MrBedfordVan | 0:b9164b348919 | 478 | ((DigitalIn *)pin)->mode(pull); |
MrBedfordVan | 0:b9164b348919 | 479 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 480 | } |
MrBedfordVan | 0:b9164b348919 | 481 | |
MrBedfordVan | 0:b9164b348919 | 482 | if((status & IO_STATUS_EVENT_ON_EDGE) || (status & IO_STATUS_EVENT_PULSE_ON_EDGE)) |
MrBedfordVan | 0:b9164b348919 | 483 | { |
MrBedfordVan | 0:b9164b348919 | 484 | ((TimedInterruptIn *)pin)->mode(pull); |
MrBedfordVan | 0:b9164b348919 | 485 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 486 | } |
MrBedfordVan | 0:b9164b348919 | 487 | |
MrBedfordVan | 0:b9164b348919 | 488 | return MICROBIT_NOT_SUPPORTED; |
MrBedfordVan | 0:b9164b348919 | 489 | } |
MrBedfordVan | 0:b9164b348919 | 490 | |
MrBedfordVan | 0:b9164b348919 | 491 | /** |
MrBedfordVan | 0:b9164b348919 | 492 | * This member function manages the calculation of the timestamp of a pulse detected |
MrBedfordVan | 0:b9164b348919 | 493 | * on a pin whilst in IO_STATUS_EVENT_PULSE_ON_EDGE or IO_STATUS_EVENT_ON_EDGE modes. |
MrBedfordVan | 0:b9164b348919 | 494 | * |
MrBedfordVan | 0:b9164b348919 | 495 | * @param eventValue the event value to distribute onto the message bus. |
MrBedfordVan | 0:b9164b348919 | 496 | */ |
MrBedfordVan | 0:b9164b348919 | 497 | void MicroBitPin::pulseWidthEvent(int eventValue) |
MrBedfordVan | 0:b9164b348919 | 498 | { |
MrBedfordVan | 0:b9164b348919 | 499 | MicroBitEvent evt(id, eventValue, CREATE_ONLY); |
MrBedfordVan | 0:b9164b348919 | 500 | uint64_t now = evt.timestamp; |
MrBedfordVan | 0:b9164b348919 | 501 | uint64_t previous = ((TimedInterruptIn *)pin)->getTimestamp(); |
MrBedfordVan | 0:b9164b348919 | 502 | |
MrBedfordVan | 0:b9164b348919 | 503 | if (previous != 0) |
MrBedfordVan | 0:b9164b348919 | 504 | { |
MrBedfordVan | 0:b9164b348919 | 505 | evt.timestamp -= previous; |
MrBedfordVan | 0:b9164b348919 | 506 | evt.fire(); |
MrBedfordVan | 0:b9164b348919 | 507 | } |
MrBedfordVan | 0:b9164b348919 | 508 | |
MrBedfordVan | 0:b9164b348919 | 509 | ((TimedInterruptIn *)pin)->setTimestamp(now); |
MrBedfordVan | 0:b9164b348919 | 510 | } |
MrBedfordVan | 0:b9164b348919 | 511 | |
MrBedfordVan | 0:b9164b348919 | 512 | /** |
MrBedfordVan | 0:b9164b348919 | 513 | * Interrupt handler for when an rise interrupt is triggered. |
MrBedfordVan | 0:b9164b348919 | 514 | */ |
MrBedfordVan | 0:b9164b348919 | 515 | void MicroBitPin::onRise() |
MrBedfordVan | 0:b9164b348919 | 516 | { |
MrBedfordVan | 0:b9164b348919 | 517 | if(status & IO_STATUS_EVENT_PULSE_ON_EDGE) |
MrBedfordVan | 0:b9164b348919 | 518 | pulseWidthEvent(MICROBIT_PIN_EVT_PULSE_LO); |
MrBedfordVan | 0:b9164b348919 | 519 | |
MrBedfordVan | 0:b9164b348919 | 520 | if(status & IO_STATUS_EVENT_ON_EDGE) |
MrBedfordVan | 0:b9164b348919 | 521 | MicroBitEvent(id, MICROBIT_PIN_EVT_RISE); |
MrBedfordVan | 0:b9164b348919 | 522 | } |
MrBedfordVan | 0:b9164b348919 | 523 | |
MrBedfordVan | 0:b9164b348919 | 524 | /** |
MrBedfordVan | 0:b9164b348919 | 525 | * Interrupt handler for when an fall interrupt is triggered. |
MrBedfordVan | 0:b9164b348919 | 526 | */ |
MrBedfordVan | 0:b9164b348919 | 527 | void MicroBitPin::onFall() |
MrBedfordVan | 0:b9164b348919 | 528 | { |
MrBedfordVan | 0:b9164b348919 | 529 | if(status & IO_STATUS_EVENT_PULSE_ON_EDGE) |
MrBedfordVan | 0:b9164b348919 | 530 | pulseWidthEvent(MICROBIT_PIN_EVT_PULSE_HI); |
MrBedfordVan | 0:b9164b348919 | 531 | |
MrBedfordVan | 0:b9164b348919 | 532 | if(status & IO_STATUS_EVENT_ON_EDGE) |
MrBedfordVan | 0:b9164b348919 | 533 | MicroBitEvent(id, MICROBIT_PIN_EVT_FALL); |
MrBedfordVan | 0:b9164b348919 | 534 | } |
MrBedfordVan | 0:b9164b348919 | 535 | |
MrBedfordVan | 0:b9164b348919 | 536 | /** |
MrBedfordVan | 0:b9164b348919 | 537 | * This member function will construct an TimedInterruptIn instance, and configure |
MrBedfordVan | 0:b9164b348919 | 538 | * interrupts for rise and fall. |
MrBedfordVan | 0:b9164b348919 | 539 | * |
MrBedfordVan | 0:b9164b348919 | 540 | * @param eventType the specific mode used in interrupt context to determine how an |
MrBedfordVan | 0:b9164b348919 | 541 | * edge/rise is processed. |
MrBedfordVan | 0:b9164b348919 | 542 | * |
MrBedfordVan | 0:b9164b348919 | 543 | * @return MICROBIT_OK on success |
MrBedfordVan | 0:b9164b348919 | 544 | */ |
MrBedfordVan | 0:b9164b348919 | 545 | int MicroBitPin::enableRiseFallEvents(int eventType) |
MrBedfordVan | 0:b9164b348919 | 546 | { |
MrBedfordVan | 0:b9164b348919 | 547 | // if we are in neither of the two modes, configure pin as a TimedInterruptIn. |
MrBedfordVan | 0:b9164b348919 | 548 | if (!(status & (IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE))) |
MrBedfordVan | 0:b9164b348919 | 549 | { |
MrBedfordVan | 0:b9164b348919 | 550 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 551 | pin = new TimedInterruptIn(name); |
MrBedfordVan | 0:b9164b348919 | 552 | |
MrBedfordVan | 0:b9164b348919 | 553 | ((TimedInterruptIn *)pin)->mode((PinMode)pullMode); |
MrBedfordVan | 0:b9164b348919 | 554 | ((TimedInterruptIn *)pin)->rise(this, &MicroBitPin::onRise); |
MrBedfordVan | 0:b9164b348919 | 555 | ((TimedInterruptIn *)pin)->fall(this, &MicroBitPin::onFall); |
MrBedfordVan | 0:b9164b348919 | 556 | } |
MrBedfordVan | 0:b9164b348919 | 557 | |
MrBedfordVan | 0:b9164b348919 | 558 | status &= ~(IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE); |
MrBedfordVan | 0:b9164b348919 | 559 | |
MrBedfordVan | 0:b9164b348919 | 560 | // set our status bits accordingly. |
MrBedfordVan | 0:b9164b348919 | 561 | if(eventType == MICROBIT_PIN_EVENT_ON_EDGE) |
MrBedfordVan | 0:b9164b348919 | 562 | status |= IO_STATUS_EVENT_ON_EDGE; |
MrBedfordVan | 0:b9164b348919 | 563 | else if(eventType == MICROBIT_PIN_EVENT_ON_PULSE) |
MrBedfordVan | 0:b9164b348919 | 564 | status |= IO_STATUS_EVENT_PULSE_ON_EDGE; |
MrBedfordVan | 0:b9164b348919 | 565 | |
MrBedfordVan | 0:b9164b348919 | 566 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 567 | } |
MrBedfordVan | 0:b9164b348919 | 568 | |
MrBedfordVan | 0:b9164b348919 | 569 | /** |
MrBedfordVan | 0:b9164b348919 | 570 | * If this pin is in a mode where the pin is generating events, it will destruct |
MrBedfordVan | 0:b9164b348919 | 571 | * the current instance attached to this MicroBitPin instance. |
MrBedfordVan | 0:b9164b348919 | 572 | * |
MrBedfordVan | 0:b9164b348919 | 573 | * @return MICROBIT_OK on success. |
MrBedfordVan | 0:b9164b348919 | 574 | */ |
MrBedfordVan | 0:b9164b348919 | 575 | int MicroBitPin::disableEvents() |
MrBedfordVan | 0:b9164b348919 | 576 | { |
MrBedfordVan | 0:b9164b348919 | 577 | if (status & (IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE | IO_STATUS_TOUCH_IN)) |
MrBedfordVan | 0:b9164b348919 | 578 | disconnect(); |
MrBedfordVan | 0:b9164b348919 | 579 | |
MrBedfordVan | 0:b9164b348919 | 580 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 581 | } |
MrBedfordVan | 0:b9164b348919 | 582 | |
MrBedfordVan | 0:b9164b348919 | 583 | /** |
MrBedfordVan | 0:b9164b348919 | 584 | * Configures the events generated by this MicroBitPin instance. |
MrBedfordVan | 0:b9164b348919 | 585 | * |
MrBedfordVan | 0:b9164b348919 | 586 | * 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) |
MrBedfordVan | 0:b9164b348919 | 587 | * 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) |
MrBedfordVan | 0:b9164b348919 | 588 | * 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. |
MrBedfordVan | 0:b9164b348919 | 589 | * MICROBIT_PIN_EVENT_NONE - Disables events for this pin. |
MrBedfordVan | 0:b9164b348919 | 590 | * |
MrBedfordVan | 0:b9164b348919 | 591 | * @param eventType One of: MICROBIT_PIN_EVENT_ON_EDGE, MICROBIT_PIN_EVENT_ON_PULSE, MICROBIT_PIN_EVENT_ON_TOUCH, MICROBIT_PIN_EVENT_NONE |
MrBedfordVan | 0:b9164b348919 | 592 | * |
MrBedfordVan | 0:b9164b348919 | 593 | * @code |
MrBedfordVan | 0:b9164b348919 | 594 | * MicroBitMessageBus bus; |
MrBedfordVan | 0:b9164b348919 | 595 | * |
MrBedfordVan | 0:b9164b348919 | 596 | * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); |
MrBedfordVan | 0:b9164b348919 | 597 | * P0.eventOn(MICROBIT_PIN_EVENT_ON_PULSE); |
MrBedfordVan | 0:b9164b348919 | 598 | * |
MrBedfordVan | 0:b9164b348919 | 599 | * void onPulse(MicroBitEvent evt) |
MrBedfordVan | 0:b9164b348919 | 600 | * { |
MrBedfordVan | 0:b9164b348919 | 601 | * int duration = evt.timestamp; |
MrBedfordVan | 0:b9164b348919 | 602 | * } |
MrBedfordVan | 0:b9164b348919 | 603 | * |
MrBedfordVan | 0:b9164b348919 | 604 | * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_PIN_EVT_PULSE_HI, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE) |
MrBedfordVan | 0:b9164b348919 | 605 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 606 | * |
MrBedfordVan | 0:b9164b348919 | 607 | * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the given eventype does not match |
MrBedfordVan | 0:b9164b348919 | 608 | * |
MrBedfordVan | 0:b9164b348919 | 609 | * @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, |
MrBedfordVan | 0:b9164b348919 | 610 | * please use the InterruptIn class supplied by ARM mbed. |
MrBedfordVan | 0:b9164b348919 | 611 | */ |
MrBedfordVan | 0:b9164b348919 | 612 | int MicroBitPin::eventOn(int eventType) |
MrBedfordVan | 0:b9164b348919 | 613 | { |
MrBedfordVan | 0:b9164b348919 | 614 | switch(eventType) |
MrBedfordVan | 0:b9164b348919 | 615 | { |
MrBedfordVan | 0:b9164b348919 | 616 | case MICROBIT_PIN_EVENT_ON_EDGE: |
MrBedfordVan | 0:b9164b348919 | 617 | case MICROBIT_PIN_EVENT_ON_PULSE: |
MrBedfordVan | 0:b9164b348919 | 618 | enableRiseFallEvents(eventType); |
MrBedfordVan | 0:b9164b348919 | 619 | break; |
MrBedfordVan | 0:b9164b348919 | 620 | |
MrBedfordVan | 0:b9164b348919 | 621 | case MICROBIT_PIN_EVENT_ON_TOUCH: |
MrBedfordVan | 0:b9164b348919 | 622 | isTouched(); |
MrBedfordVan | 0:b9164b348919 | 623 | break; |
MrBedfordVan | 0:b9164b348919 | 624 | |
MrBedfordVan | 0:b9164b348919 | 625 | case MICROBIT_PIN_EVENT_NONE: |
MrBedfordVan | 0:b9164b348919 | 626 | disableEvents(); |
MrBedfordVan | 0:b9164b348919 | 627 | break; |
MrBedfordVan | 0:b9164b348919 | 628 | |
MrBedfordVan | 0:b9164b348919 | 629 | default: |
MrBedfordVan | 0:b9164b348919 | 630 | return MICROBIT_INVALID_PARAMETER; |
MrBedfordVan | 0:b9164b348919 | 631 | } |
MrBedfordVan | 0:b9164b348919 | 632 | |
MrBedfordVan | 0:b9164b348919 | 633 | return MICROBIT_OK; |
MrBedfordVan | 0:b9164b348919 | 634 | } |