mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

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?

UserRevisionLine numberNew 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 49:88f03f3feff1 34 #include "MicroBitSystemTimer.h"
LancasterUniversity 49:88f03f3feff1 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.
LancasterUniversity 45:23b71960fe6c 48 * (PIN_CAPABILITY_DIGITAL, PIN_CAPABILITY_ANALOG, 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 49:88f03f3feff1 97 if ((status & IO_STATUS_EVENT_ON_EDGE) || (status & IO_STATUS_EVENT_PULSE_ON_EDGE))
LancasterUniversity 49:88f03f3feff1 98 delete ((TimedInterruptIn *)pin);
LancasterUniversity 49:88f03f3feff1 99
Jonathan Austin 1:8aa5cdb4ab67 100 this->pin = NULL;
LancasterUniversity 49:88f03f3feff1 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 49:88f03f3feff1 143 *
Jonathan Austin 1:8aa5cdb4ab67 144 * @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED
LancasterUniversity 49:88f03f3feff1 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 49:88f03f3feff1 159 if (!(status & (IO_STATUS_DIGITAL_IN | IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE)))
LancasterUniversity 49:88f03f3feff1 160 {
Jonathan Austin 1:8aa5cdb4ab67 161 disconnect();
LancasterUniversity 65:f7ebabf23e15 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 49:88f03f3feff1 166 if(status & (IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE))
LancasterUniversity 49:88f03f3feff1 167 return ((TimedInterruptIn *)pin)->read();
LancasterUniversity 49:88f03f3feff1 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...
LancasterUniversity 45:23b71960fe6c 345 if(!(PIN_CAPABILITY_DIGITAL & 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;
LancasterUniversity 49:88f03f3feff1 442 }
LancasterUniversity 49:88f03f3feff1 443
LancasterUniversity 49:88f03f3feff1 444 /**
LancasterUniversity 49:88f03f3feff1 445 * Configures the pull of this pin.
LancasterUniversity 49:88f03f3feff1 446 *
LancasterUniversity 53:ee44932401cb 447 * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone
LancasterUniversity 49:88f03f3feff1 448 *
LancasterUniversity 49:88f03f3feff1 449 * @return MICROBIT_NOT_SUPPORTED if the current pin configuration is anything other
LancasterUniversity 49:88f03f3feff1 450 * than a digital input, otherwise MICROBIT_OK.
LancasterUniversity 49:88f03f3feff1 451 */
LancasterUniversity 49:88f03f3feff1 452 int MicroBitPin::setPull(PinMode pull)
LancasterUniversity 49:88f03f3feff1 453 {
LancasterUniversity 49:88f03f3feff1 454 if ((status & IO_STATUS_DIGITAL_IN))
LancasterUniversity 49:88f03f3feff1 455 {
LancasterUniversity 49:88f03f3feff1 456 ((DigitalIn *)pin)->mode(pull);
LancasterUniversity 49:88f03f3feff1 457 return MICROBIT_OK;
LancasterUniversity 49:88f03f3feff1 458 }
LancasterUniversity 49:88f03f3feff1 459
LancasterUniversity 49:88f03f3feff1 460 if((status & IO_STATUS_EVENT_ON_EDGE) || (status & IO_STATUS_EVENT_PULSE_ON_EDGE))
LancasterUniversity 49:88f03f3feff1 461 {
LancasterUniversity 49:88f03f3feff1 462 ((TimedInterruptIn *)pin)->mode(pull);
LancasterUniversity 49:88f03f3feff1 463 return MICROBIT_OK;
LancasterUniversity 49:88f03f3feff1 464 }
LancasterUniversity 49:88f03f3feff1 465
LancasterUniversity 49:88f03f3feff1 466 return MICROBIT_NOT_SUPPORTED;
LancasterUniversity 49:88f03f3feff1 467 }
LancasterUniversity 49:88f03f3feff1 468
LancasterUniversity 49:88f03f3feff1 469 /**
LancasterUniversity 49:88f03f3feff1 470 * This member function manages the calculation of the timestamp of a pulse detected
LancasterUniversity 49:88f03f3feff1 471 * on a pin whilst in IO_STATUS_EVENT_PULSE_ON_EDGE or IO_STATUS_EVENT_ON_EDGE modes.
LancasterUniversity 49:88f03f3feff1 472 *
LancasterUniversity 49:88f03f3feff1 473 * @param eventValue the event value to distribute onto the message bus.
LancasterUniversity 49:88f03f3feff1 474 */
LancasterUniversity 49:88f03f3feff1 475 void MicroBitPin::pulseWidthEvent(int eventValue)
LancasterUniversity 49:88f03f3feff1 476 {
LancasterUniversity 49:88f03f3feff1 477 MicroBitEvent evt(id, eventValue, CREATE_ONLY);
LancasterUniversity 49:88f03f3feff1 478 uint64_t now = evt.timestamp;
LancasterUniversity 49:88f03f3feff1 479 uint64_t previous = ((TimedInterruptIn *)pin)->getTimestamp();
LancasterUniversity 49:88f03f3feff1 480
LancasterUniversity 49:88f03f3feff1 481 if (previous != 0)
LancasterUniversity 49:88f03f3feff1 482 {
LancasterUniversity 49:88f03f3feff1 483 evt.timestamp -= previous;
LancasterUniversity 49:88f03f3feff1 484 evt.fire();
LancasterUniversity 49:88f03f3feff1 485 }
LancasterUniversity 49:88f03f3feff1 486
LancasterUniversity 49:88f03f3feff1 487 ((TimedInterruptIn *)pin)->setTimestamp(now);
LancasterUniversity 49:88f03f3feff1 488 }
LancasterUniversity 49:88f03f3feff1 489
LancasterUniversity 49:88f03f3feff1 490 /**
LancasterUniversity 49:88f03f3feff1 491 * Interrupt handler for when an rise interrupt is triggered.
LancasterUniversity 49:88f03f3feff1 492 */
LancasterUniversity 49:88f03f3feff1 493 void MicroBitPin::onRise()
LancasterUniversity 49:88f03f3feff1 494 {
LancasterUniversity 49:88f03f3feff1 495 if(status & IO_STATUS_EVENT_PULSE_ON_EDGE)
LancasterUniversity 49:88f03f3feff1 496 pulseWidthEvent(MICROBIT_PIN_EVT_PULSE_LO);
LancasterUniversity 49:88f03f3feff1 497
LancasterUniversity 49:88f03f3feff1 498 if(status & IO_STATUS_EVENT_ON_EDGE)
LancasterUniversity 49:88f03f3feff1 499 MicroBitEvent(id, MICROBIT_PIN_EVT_RISE);
LancasterUniversity 49:88f03f3feff1 500 }
LancasterUniversity 49:88f03f3feff1 501
LancasterUniversity 49:88f03f3feff1 502 /**
LancasterUniversity 49:88f03f3feff1 503 * Interrupt handler for when an fall interrupt is triggered.
LancasterUniversity 49:88f03f3feff1 504 */
LancasterUniversity 49:88f03f3feff1 505 void MicroBitPin::onFall()
LancasterUniversity 49:88f03f3feff1 506 {
LancasterUniversity 49:88f03f3feff1 507 if(status & IO_STATUS_EVENT_PULSE_ON_EDGE)
LancasterUniversity 49:88f03f3feff1 508 pulseWidthEvent(MICROBIT_PIN_EVT_PULSE_HI);
LancasterUniversity 49:88f03f3feff1 509
LancasterUniversity 49:88f03f3feff1 510 if(status & IO_STATUS_EVENT_ON_EDGE)
LancasterUniversity 49:88f03f3feff1 511 MicroBitEvent(id, MICROBIT_PIN_EVT_FALL);
LancasterUniversity 49:88f03f3feff1 512 }
LancasterUniversity 49:88f03f3feff1 513
LancasterUniversity 49:88f03f3feff1 514 /**
LancasterUniversity 49:88f03f3feff1 515 * This member function will construct an TimedInterruptIn instance, and configure
LancasterUniversity 49:88f03f3feff1 516 * interrupts for rise and fall.
LancasterUniversity 49:88f03f3feff1 517 *
LancasterUniversity 49:88f03f3feff1 518 * @param eventType the specific mode used in interrupt context to determine how an
LancasterUniversity 49:88f03f3feff1 519 * edge/rise is processed.
LancasterUniversity 49:88f03f3feff1 520 *
LancasterUniversity 49:88f03f3feff1 521 * @return MICROBIT_OK on success
LancasterUniversity 49:88f03f3feff1 522 */
LancasterUniversity 49:88f03f3feff1 523 int MicroBitPin::enableRiseFallEvents(int eventType)
LancasterUniversity 49:88f03f3feff1 524 {
LancasterUniversity 49:88f03f3feff1 525 // if we are in neither of the two modes, configure pin as a TimedInterruptIn.
LancasterUniversity 49:88f03f3feff1 526 if (!(status & (IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE)))
LancasterUniversity 49:88f03f3feff1 527 {
LancasterUniversity 49:88f03f3feff1 528 disconnect();
LancasterUniversity 49:88f03f3feff1 529 pin = new TimedInterruptIn(name);
LancasterUniversity 49:88f03f3feff1 530
LancasterUniversity 65:f7ebabf23e15 531 ((TimedInterruptIn *)pin)->mode(PullDown);
LancasterUniversity 49:88f03f3feff1 532 ((TimedInterruptIn *)pin)->rise(this, &MicroBitPin::onRise);
LancasterUniversity 49:88f03f3feff1 533 ((TimedInterruptIn *)pin)->fall(this, &MicroBitPin::onFall);
LancasterUniversity 49:88f03f3feff1 534 }
LancasterUniversity 49:88f03f3feff1 535
LancasterUniversity 49:88f03f3feff1 536 status &= ~(IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE);
LancasterUniversity 49:88f03f3feff1 537
LancasterUniversity 49:88f03f3feff1 538 // set our status bits accordingly.
LancasterUniversity 49:88f03f3feff1 539 if(eventType == MICROBIT_PIN_EVENT_ON_EDGE)
LancasterUniversity 49:88f03f3feff1 540 status |= IO_STATUS_EVENT_ON_EDGE;
LancasterUniversity 49:88f03f3feff1 541 else if(eventType == MICROBIT_PIN_EVENT_ON_PULSE)
LancasterUniversity 49:88f03f3feff1 542 status |= IO_STATUS_EVENT_PULSE_ON_EDGE;
LancasterUniversity 49:88f03f3feff1 543
LancasterUniversity 49:88f03f3feff1 544 return MICROBIT_OK;
LancasterUniversity 49:88f03f3feff1 545 }
LancasterUniversity 49:88f03f3feff1 546
LancasterUniversity 49:88f03f3feff1 547 /**
LancasterUniversity 49:88f03f3feff1 548 * If this pin is in a mode where the pin is generating events, it will destruct
LancasterUniversity 49:88f03f3feff1 549 * the current instance attached to this MicroBitPin instance.
LancasterUniversity 49:88f03f3feff1 550 *
LancasterUniversity 49:88f03f3feff1 551 * @return MICROBIT_OK on success.
LancasterUniversity 49:88f03f3feff1 552 */
LancasterUniversity 49:88f03f3feff1 553 int MicroBitPin::disableEvents()
LancasterUniversity 49:88f03f3feff1 554 {
LancasterUniversity 49:88f03f3feff1 555 if (status & (IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE | IO_STATUS_TOUCH_IN))
LancasterUniversity 49:88f03f3feff1 556 disconnect();
LancasterUniversity 49:88f03f3feff1 557
LancasterUniversity 49:88f03f3feff1 558 return MICROBIT_OK;
LancasterUniversity 49:88f03f3feff1 559 }
LancasterUniversity 49:88f03f3feff1 560
LancasterUniversity 49:88f03f3feff1 561 /**
LancasterUniversity 49:88f03f3feff1 562 * Configures the events generated by this MicroBitPin instance.
LancasterUniversity 49:88f03f3feff1 563 *
LancasterUniversity 49:88f03f3feff1 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 49:88f03f3feff1 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 49:88f03f3feff1 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 49:88f03f3feff1 567 * MICROBIT_PIN_EVENT_NONE - Disables events for this pin.
LancasterUniversity 49:88f03f3feff1 568 *
LancasterUniversity 49:88f03f3feff1 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 49:88f03f3feff1 570 *
LancasterUniversity 49:88f03f3feff1 571 * @code
LancasterUniversity 49:88f03f3feff1 572 * MicroBitMessageBus bus;
LancasterUniversity 49:88f03f3feff1 573 *
LancasterUniversity 49:88f03f3feff1 574 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
LancasterUniversity 49:88f03f3feff1 575 * P0.eventOn(MICROBIT_PIN_EVENT_ON_PULSE);
LancasterUniversity 49:88f03f3feff1 576 *
LancasterUniversity 49:88f03f3feff1 577 * void onPulse(MicroBitEvent evt)
LancasterUniversity 49:88f03f3feff1 578 * {
LancasterUniversity 49:88f03f3feff1 579 * int duration = evt.timestamp;
LancasterUniversity 49:88f03f3feff1 580 * }
LancasterUniversity 49:88f03f3feff1 581 *
LancasterUniversity 49:88f03f3feff1 582 * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_PIN_EVT_PULSE_HI, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE)
LancasterUniversity 49:88f03f3feff1 583 * @endcode
LancasterUniversity 49:88f03f3feff1 584 *
LancasterUniversity 49:88f03f3feff1 585 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the given eventype does not match
LancasterUniversity 49:88f03f3feff1 586 *
LancasterUniversity 49:88f03f3feff1 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 49:88f03f3feff1 588 * please use the InterruptIn class supplied by ARM mbed.
LancasterUniversity 49:88f03f3feff1 589 */
LancasterUniversity 49:88f03f3feff1 590 int MicroBitPin::eventOn(int eventType)
LancasterUniversity 49:88f03f3feff1 591 {
LancasterUniversity 49:88f03f3feff1 592 switch(eventType)
LancasterUniversity 49:88f03f3feff1 593 {
LancasterUniversity 49:88f03f3feff1 594 case MICROBIT_PIN_EVENT_ON_EDGE:
LancasterUniversity 49:88f03f3feff1 595 case MICROBIT_PIN_EVENT_ON_PULSE:
LancasterUniversity 49:88f03f3feff1 596 enableRiseFallEvents(eventType);
LancasterUniversity 49:88f03f3feff1 597 break;
LancasterUniversity 49:88f03f3feff1 598
LancasterUniversity 49:88f03f3feff1 599 case MICROBIT_PIN_EVENT_ON_TOUCH:
LancasterUniversity 49:88f03f3feff1 600 isTouched();
LancasterUniversity 49:88f03f3feff1 601 break;
LancasterUniversity 49:88f03f3feff1 602
LancasterUniversity 49:88f03f3feff1 603 case MICROBIT_PIN_EVENT_NONE:
LancasterUniversity 49:88f03f3feff1 604 disableEvents();
LancasterUniversity 49:88f03f3feff1 605 break;
LancasterUniversity 49:88f03f3feff1 606
LancasterUniversity 49:88f03f3feff1 607 default:
LancasterUniversity 49:88f03f3feff1 608 return MICROBIT_INVALID_PARAMETER;
LancasterUniversity 49:88f03f3feff1 609 }
LancasterUniversity 49:88f03f3feff1 610
LancasterUniversity 49:88f03f3feff1 611 return MICROBIT_OK;
LancasterUniversity 35:8ce23bc1af38 612 }