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