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:14 2016 +0100
Revision:
35:8ce23bc1af38
Parent:
1:8aa5cdb4ab67
Child:
36:6837feb07da4
Synchronized with git rev 732971e7
Author: James Devine
microbit-dal: Added events to MicroBitPin

Added rise, fall, pulse HI and LO events.

The pulse Hi and LO event timestamp given in the MicroBitEvent is the
duration for which the input was HI or LO for.

eventOn(int eventType) is used to configure the events generated
from the pin instance.

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 #ifndef MICROBIT_PIN_H
Jonathan Austin 1:8aa5cdb4ab67 27 #define MICROBIT_PIN_H
Jonathan Austin 1:8aa5cdb4ab67 28
Jonathan Austin 1:8aa5cdb4ab67 29 #include "mbed.h"
Jonathan Austin 1:8aa5cdb4ab67 30 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 31 #include "MicroBitComponent.h"
Jonathan Austin 1:8aa5cdb4ab67 32 // Status Field flags...
Jonathan Austin 1:8aa5cdb4ab67 33 #define IO_STATUS_DIGITAL_IN 0x01 // Pin is configured as a digital input, with no pull up.
Jonathan Austin 1:8aa5cdb4ab67 34 #define IO_STATUS_DIGITAL_OUT 0x02 // Pin is configured as a digital output
Jonathan Austin 1:8aa5cdb4ab67 35 #define IO_STATUS_ANALOG_IN 0x04 // Pin is Analog in
Jonathan Austin 1:8aa5cdb4ab67 36 #define IO_STATUS_ANALOG_OUT 0x08 // Pin is Analog out
Jonathan Austin 1:8aa5cdb4ab67 37 #define IO_STATUS_TOUCH_IN 0x10 // Pin is a makey-makey style touch sensor
LancasterUniversity 35:8ce23bc1af38 38 #define IO_STATUS_EVENT_ON_EDGE 0x20 // Pin will generate events on pin change
LancasterUniversity 35:8ce23bc1af38 39 #define IO_STATUS_EVENT_PULSE_ON_EDGE 0x40 // Pin will generate events on pin change
Jonathan Austin 1:8aa5cdb4ab67 40
Jonathan Austin 1:8aa5cdb4ab67 41 //#defines for each edge connector pin
Jonathan Austin 1:8aa5cdb4ab67 42 #define MICROBIT_PIN_P0 P0_3 //P0 is the left most pad (ANALOG/DIGITAL) used to be P0_3 on green board
Jonathan Austin 1:8aa5cdb4ab67 43 #define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 44 #define MICROBIT_PIN_P2 P0_1 //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board
Jonathan Austin 1:8aa5cdb4ab67 45 #define MICROBIT_PIN_P3 P0_4 //COL1 (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 46 #define MICROBIT_PIN_P4 P0_5 //COL2 (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 47 #define MICROBIT_PIN_P5 P0_17 //BTN_A
Jonathan Austin 1:8aa5cdb4ab67 48 #define MICROBIT_PIN_P6 P0_12 //COL9
Jonathan Austin 1:8aa5cdb4ab67 49 #define MICROBIT_PIN_P7 P0_11 //COL8
Jonathan Austin 1:8aa5cdb4ab67 50 #define MICROBIT_PIN_P8 P0_18 //PIN 18
Jonathan Austin 1:8aa5cdb4ab67 51 #define MICROBIT_PIN_P9 P0_10 //COL7
Jonathan Austin 1:8aa5cdb4ab67 52 #define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 53 #define MICROBIT_PIN_P11 P0_26 //BTN_B
Jonathan Austin 1:8aa5cdb4ab67 54 #define MICROBIT_PIN_P12 P0_20 //PIN 20
Jonathan Austin 1:8aa5cdb4ab67 55 #define MICROBIT_PIN_P13 P0_23 //SCK
Jonathan Austin 1:8aa5cdb4ab67 56 #define MICROBIT_PIN_P14 P0_22 //MISO
Jonathan Austin 1:8aa5cdb4ab67 57 #define MICROBIT_PIN_P15 P0_21 //MOSI
Jonathan Austin 1:8aa5cdb4ab67 58 #define MICROBIT_PIN_P16 P0_16 //PIN 16
Jonathan Austin 1:8aa5cdb4ab67 59 #define MICROBIT_PIN_P19 P0_0 //SCL
Jonathan Austin 1:8aa5cdb4ab67 60 #define MICROBIT_PIN_P20 P0_30 //SDA
Jonathan Austin 1:8aa5cdb4ab67 61
Jonathan Austin 1:8aa5cdb4ab67 62 #define MICROBIT_PIN_MAX_OUTPUT 1023
Jonathan Austin 1:8aa5cdb4ab67 63
Jonathan Austin 1:8aa5cdb4ab67 64 #define MICROBIT_PIN_MAX_SERVO_RANGE 180
Jonathan Austin 1:8aa5cdb4ab67 65 #define MICROBIT_PIN_DEFAULT_SERVO_RANGE 2000
Jonathan Austin 1:8aa5cdb4ab67 66 #define MICROBIT_PIN_DEFAULT_SERVO_CENTER 1500
Jonathan Austin 1:8aa5cdb4ab67 67
LancasterUniversity 35:8ce23bc1af38 68 #define MICROBIT_PIN_EVENT_NONE 0
LancasterUniversity 35:8ce23bc1af38 69 #define MICROBIT_PIN_EVENT_ON_EDGE 1
LancasterUniversity 35:8ce23bc1af38 70 #define MICROBIT_PIN_EVENT_ON_PULSE 2
LancasterUniversity 35:8ce23bc1af38 71 #define MICROBIT_PIN_EVENT_ON_TOUCH 3
LancasterUniversity 35:8ce23bc1af38 72
LancasterUniversity 35:8ce23bc1af38 73 #define MICROBIT_PIN_EVT_RISE 2
LancasterUniversity 35:8ce23bc1af38 74 #define MICROBIT_PIN_EVT_FALL 3
LancasterUniversity 35:8ce23bc1af38 75 #define MICROBIT_PIN_EVT_PULSE_HI 4
LancasterUniversity 35:8ce23bc1af38 76 #define MICROBIT_PIN_EVT_PULSE_LO 5
Jonathan Austin 1:8aa5cdb4ab67 77
Jonathan Austin 1:8aa5cdb4ab67 78 /**
Jonathan Austin 1:8aa5cdb4ab67 79 * Pin capabilities enum.
Jonathan Austin 1:8aa5cdb4ab67 80 * Used to determine the capabilities of each Pin as some can only be digital, or can be both digital and analogue.
Jonathan Austin 1:8aa5cdb4ab67 81 */
Jonathan Austin 1:8aa5cdb4ab67 82 enum PinCapability{
Jonathan Austin 1:8aa5cdb4ab67 83 PIN_CAPABILITY_DIGITAL = 0x01,
Jonathan Austin 1:8aa5cdb4ab67 84 PIN_CAPABILITY_ANALOG = 0x02,
Jonathan Austin 1:8aa5cdb4ab67 85 PIN_CAPABILITY_TOUCH = 0x04,
Jonathan Austin 1:8aa5cdb4ab67 86 PIN_CAPABILITY_AD = PIN_CAPABILITY_DIGITAL | PIN_CAPABILITY_ANALOG,
Jonathan Austin 1:8aa5cdb4ab67 87 PIN_CAPABILITY_ALL = PIN_CAPABILITY_DIGITAL | PIN_CAPABILITY_ANALOG | PIN_CAPABILITY_TOUCH
Jonathan Austin 1:8aa5cdb4ab67 88
Jonathan Austin 1:8aa5cdb4ab67 89 };
Jonathan Austin 1:8aa5cdb4ab67 90
Jonathan Austin 1:8aa5cdb4ab67 91 /**
Jonathan Austin 1:8aa5cdb4ab67 92 * Class definition for MicroBitPin.
Jonathan Austin 1:8aa5cdb4ab67 93 *
Jonathan Austin 1:8aa5cdb4ab67 94 * Commonly represents an I/O pin on the edge connector.
Jonathan Austin 1:8aa5cdb4ab67 95 */
Jonathan Austin 1:8aa5cdb4ab67 96 class MicroBitPin : public MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 97 {
Jonathan Austin 1:8aa5cdb4ab67 98 // The mbed object looking after this pin at any point in time (untyped due to dynamic behaviour).
Jonathan Austin 1:8aa5cdb4ab67 99 void *pin;
Jonathan Austin 1:8aa5cdb4ab67 100
Jonathan Austin 1:8aa5cdb4ab67 101 PinCapability capability;
Jonathan Austin 1:8aa5cdb4ab67 102
Jonathan Austin 1:8aa5cdb4ab67 103 /**
Jonathan Austin 1:8aa5cdb4ab67 104 * Disconnect any attached mBed IO from this pin.
Jonathan Austin 1:8aa5cdb4ab67 105 *
Jonathan Austin 1:8aa5cdb4ab67 106 * Used only when pin changes mode (i.e. Input/Output/Analog/Digital)
Jonathan Austin 1:8aa5cdb4ab67 107 */
Jonathan Austin 1:8aa5cdb4ab67 108 void disconnect();
Jonathan Austin 1:8aa5cdb4ab67 109
Jonathan Austin 1:8aa5cdb4ab67 110 /**
Jonathan Austin 1:8aa5cdb4ab67 111 * Performs a check to ensure that the current Pin is in control of a
Jonathan Austin 1:8aa5cdb4ab67 112 * DynamicPwm instance, and if it's not, allocates a new DynamicPwm instance.
Jonathan Austin 1:8aa5cdb4ab67 113 */
Jonathan Austin 1:8aa5cdb4ab67 114 int obtainAnalogChannel();
Jonathan Austin 1:8aa5cdb4ab67 115
LancasterUniversity 35:8ce23bc1af38 116 /**
LancasterUniversity 35:8ce23bc1af38 117 * Interrupt handler for when an rise interrupt is triggered.
LancasterUniversity 35:8ce23bc1af38 118 */
LancasterUniversity 35:8ce23bc1af38 119 void onRise();
LancasterUniversity 35:8ce23bc1af38 120
LancasterUniversity 35:8ce23bc1af38 121 /**
LancasterUniversity 35:8ce23bc1af38 122 * Interrupt handler for when an fall interrupt is triggered.
LancasterUniversity 35:8ce23bc1af38 123 */
LancasterUniversity 35:8ce23bc1af38 124 void onFall();
LancasterUniversity 35:8ce23bc1af38 125
LancasterUniversity 35:8ce23bc1af38 126 /**
LancasterUniversity 35:8ce23bc1af38 127 * This member function manages the calculation of the timestamp of a pulse detected
LancasterUniversity 35:8ce23bc1af38 128 * on a pin whilst in IO_STATUS_EVENT_PULSE_ON_EDGE or IO_STATUS_EVENT_ON_EDGE modes.
LancasterUniversity 35:8ce23bc1af38 129 *
LancasterUniversity 35:8ce23bc1af38 130 * @param eventValue the event value to distribute onto the message bus.
LancasterUniversity 35:8ce23bc1af38 131 */
LancasterUniversity 35:8ce23bc1af38 132 void pulseWidthEvent(int eventValue);
LancasterUniversity 35:8ce23bc1af38 133
LancasterUniversity 35:8ce23bc1af38 134 /**
LancasterUniversity 35:8ce23bc1af38 135 * This member function will construct an TimedInterruptIn instance, and configure
LancasterUniversity 35:8ce23bc1af38 136 * interrupts for rise and fall.
LancasterUniversity 35:8ce23bc1af38 137 *
LancasterUniversity 35:8ce23bc1af38 138 * @param eventType the specific mode used in interrupt context to determine how an
LancasterUniversity 35:8ce23bc1af38 139 * edge/rise is processed.
LancasterUniversity 35:8ce23bc1af38 140 *
LancasterUniversity 35:8ce23bc1af38 141 * @return MICROBIT_OK on success
LancasterUniversity 35:8ce23bc1af38 142 */
LancasterUniversity 35:8ce23bc1af38 143 int enableRiseFallEvents(int eventType);
LancasterUniversity 35:8ce23bc1af38 144
LancasterUniversity 35:8ce23bc1af38 145 /**
LancasterUniversity 35:8ce23bc1af38 146 * If this pin is in a mode where the pin is generating events, it will destruct
LancasterUniversity 35:8ce23bc1af38 147 * the current instance attached to this MicroBitPin instance.
LancasterUniversity 35:8ce23bc1af38 148 *
LancasterUniversity 35:8ce23bc1af38 149 * @return MICROBIT_OK on success.
LancasterUniversity 35:8ce23bc1af38 150 */
LancasterUniversity 35:8ce23bc1af38 151 int disableEvents();
LancasterUniversity 35:8ce23bc1af38 152
Jonathan Austin 1:8aa5cdb4ab67 153 public:
Jonathan Austin 1:8aa5cdb4ab67 154
Jonathan Austin 1:8aa5cdb4ab67 155 // mbed PinName of this pin.
Jonathan Austin 1:8aa5cdb4ab67 156 PinName name;
Jonathan Austin 1:8aa5cdb4ab67 157
Jonathan Austin 1:8aa5cdb4ab67 158 /**
Jonathan Austin 1:8aa5cdb4ab67 159 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 160 * Create a MicroBitPin instance, generally used to represent a pin on the edge connector.
Jonathan Austin 1:8aa5cdb4ab67 161 *
Jonathan Austin 1:8aa5cdb4ab67 162 * @param id the unique EventModel id of this component.
Jonathan Austin 1:8aa5cdb4ab67 163 *
Jonathan Austin 1:8aa5cdb4ab67 164 * @param name the mbed PinName for this MicroBitPin instance.
Jonathan Austin 1:8aa5cdb4ab67 165 *
Jonathan Austin 1:8aa5cdb4ab67 166 * @param capability the capabilities this MicroBitPin instance should have.
Jonathan Austin 1:8aa5cdb4ab67 167 * (PIN_CAPABILITY_DIGITAL, PIN_CAPABILITY_ANALOG, PIN_CAPABILITY_TOUCH, PIN_CAPABILITY_AD, PIN_CAPABILITY_ALL)
Jonathan Austin 1:8aa5cdb4ab67 168 *
Jonathan Austin 1:8aa5cdb4ab67 169 * @code
Jonathan Austin 1:8aa5cdb4ab67 170 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL);
Jonathan Austin 1:8aa5cdb4ab67 171 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 172 */
Jonathan Austin 1:8aa5cdb4ab67 173 MicroBitPin(int id, PinName name, PinCapability capability);
Jonathan Austin 1:8aa5cdb4ab67 174
Jonathan Austin 1:8aa5cdb4ab67 175 /**
Jonathan Austin 1:8aa5cdb4ab67 176 * Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'.
Jonathan Austin 1:8aa5cdb4ab67 177 *
Jonathan Austin 1:8aa5cdb4ab67 178 * @param value 0 (LO) or 1 (HI)
Jonathan Austin 1:8aa5cdb4ab67 179 *
Jonathan Austin 1:8aa5cdb4ab67 180 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED
Jonathan Austin 1:8aa5cdb4ab67 181 * if the given pin does not have digital capability.
Jonathan Austin 1:8aa5cdb4ab67 182 *
Jonathan Austin 1:8aa5cdb4ab67 183 * @code
Jonathan Austin 1:8aa5cdb4ab67 184 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
Jonathan Austin 1:8aa5cdb4ab67 185 * P0.setDigitalValue(1); // P0 is now HI
Jonathan Austin 1:8aa5cdb4ab67 186 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 187 */
Jonathan Austin 1:8aa5cdb4ab67 188 int setDigitalValue(int value);
Jonathan Austin 1:8aa5cdb4ab67 189
Jonathan Austin 1:8aa5cdb4ab67 190 /**
Jonathan Austin 1:8aa5cdb4ab67 191 * Configures this IO pin as a digital input (if necessary) and tests its current value.
Jonathan Austin 1:8aa5cdb4ab67 192 *
LancasterUniversity 35:8ce23bc1af38 193 *
Jonathan Austin 1:8aa5cdb4ab67 194 * @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED
LancasterUniversity 35:8ce23bc1af38 195 * if the given pin does not have digital capability.
Jonathan Austin 1:8aa5cdb4ab67 196 *
Jonathan Austin 1:8aa5cdb4ab67 197 * @code
Jonathan Austin 1:8aa5cdb4ab67 198 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
Jonathan Austin 1:8aa5cdb4ab67 199 * P0.getDigitalValue(); // P0 is either 0 or 1;
Jonathan Austin 1:8aa5cdb4ab67 200 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 201 */
Jonathan Austin 1:8aa5cdb4ab67 202 int getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 203
Jonathan Austin 1:8aa5cdb4ab67 204 /**
Jonathan Austin 1:8aa5cdb4ab67 205 * Configures this IO pin as an analog/pwm output, and change the output value to the given level.
Jonathan Austin 1:8aa5cdb4ab67 206 *
Jonathan Austin 1:8aa5cdb4ab67 207 * @param value the level to set on the output pin, in the range 0 - 1024
Jonathan Austin 1:8aa5cdb4ab67 208 *
Jonathan Austin 1:8aa5cdb4ab67 209 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED
Jonathan Austin 1:8aa5cdb4ab67 210 * if the given pin does not have analog capability.
Jonathan Austin 1:8aa5cdb4ab67 211 */
Jonathan Austin 1:8aa5cdb4ab67 212 int setAnalogValue(int value);
Jonathan Austin 1:8aa5cdb4ab67 213
Jonathan Austin 1:8aa5cdb4ab67 214 /**
Jonathan Austin 1:8aa5cdb4ab67 215 * Configures this IO pin as an analog/pwm output (if necessary) and configures the period to be 20ms,
Jonathan Austin 1:8aa5cdb4ab67 216 * with a duty cycle between 500 us and 2500 us.
Jonathan Austin 1:8aa5cdb4ab67 217 *
Jonathan Austin 1:8aa5cdb4ab67 218 * 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 219 *
Jonathan Austin 1:8aa5cdb4ab67 220 * This range can be modified to fine tune, and also tolerate different servos.
Jonathan Austin 1:8aa5cdb4ab67 221 *
Jonathan Austin 1:8aa5cdb4ab67 222 * @param value the level to set on the output pin, in the range 0 - 180.
Jonathan Austin 1:8aa5cdb4ab67 223 *
Jonathan Austin 1:8aa5cdb4ab67 224 * @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 225 *
Jonathan Austin 1:8aa5cdb4ab67 226 * @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 227 *
Jonathan Austin 1:8aa5cdb4ab67 228 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED
Jonathan Austin 1:8aa5cdb4ab67 229 * if the given pin does not have analog capability.
Jonathan Austin 1:8aa5cdb4ab67 230 */
Jonathan Austin 1:8aa5cdb4ab67 231 int setServoValue(int value, int range = MICROBIT_PIN_DEFAULT_SERVO_RANGE, int center = MICROBIT_PIN_DEFAULT_SERVO_CENTER);
Jonathan Austin 1:8aa5cdb4ab67 232
Jonathan Austin 1:8aa5cdb4ab67 233 /**
Jonathan Austin 1:8aa5cdb4ab67 234 * Configures this IO pin as an analogue input (if necessary), and samples the Pin for its analog value.
Jonathan Austin 1:8aa5cdb4ab67 235 *
Jonathan Austin 1:8aa5cdb4ab67 236 * @return the current analogue level on the pin, in the range 0 - 1024, or
Jonathan Austin 1:8aa5cdb4ab67 237 * MICROBIT_NOT_SUPPORTED if the given pin does not have analog capability.
Jonathan Austin 1:8aa5cdb4ab67 238 *
Jonathan Austin 1:8aa5cdb4ab67 239 * @code
Jonathan Austin 1:8aa5cdb4ab67 240 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
Jonathan Austin 1:8aa5cdb4ab67 241 * P0.getAnalogValue(); // P0 is a value in the range of 0 - 1024
Jonathan Austin 1:8aa5cdb4ab67 242 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 243 */
Jonathan Austin 1:8aa5cdb4ab67 244 int getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 245
Jonathan Austin 1:8aa5cdb4ab67 246 /**
Jonathan Austin 1:8aa5cdb4ab67 247 * Determines if this IO pin is currently configured as an input.
Jonathan Austin 1:8aa5cdb4ab67 248 *
Jonathan Austin 1:8aa5cdb4ab67 249 * @return 1 if pin is an analog or digital input, 0 otherwise.
Jonathan Austin 1:8aa5cdb4ab67 250 */
Jonathan Austin 1:8aa5cdb4ab67 251 int isInput();
Jonathan Austin 1:8aa5cdb4ab67 252
Jonathan Austin 1:8aa5cdb4ab67 253 /**
Jonathan Austin 1:8aa5cdb4ab67 254 * Determines if this IO pin is currently configured as an output.
Jonathan Austin 1:8aa5cdb4ab67 255 *
Jonathan Austin 1:8aa5cdb4ab67 256 * @return 1 if pin is an analog or digital output, 0 otherwise.
Jonathan Austin 1:8aa5cdb4ab67 257 */
Jonathan Austin 1:8aa5cdb4ab67 258 int isOutput();
Jonathan Austin 1:8aa5cdb4ab67 259
Jonathan Austin 1:8aa5cdb4ab67 260 /**
Jonathan Austin 1:8aa5cdb4ab67 261 * Determines if this IO pin is currently configured for digital use.
Jonathan Austin 1:8aa5cdb4ab67 262 *
Jonathan Austin 1:8aa5cdb4ab67 263 * @return 1 if pin is digital, 0 otherwise.
Jonathan Austin 1:8aa5cdb4ab67 264 */
Jonathan Austin 1:8aa5cdb4ab67 265 int isDigital();
Jonathan Austin 1:8aa5cdb4ab67 266
Jonathan Austin 1:8aa5cdb4ab67 267 /**
Jonathan Austin 1:8aa5cdb4ab67 268 * Determines if this IO pin is currently configured for analog use.
Jonathan Austin 1:8aa5cdb4ab67 269 *
Jonathan Austin 1:8aa5cdb4ab67 270 * @return 1 if pin is analog, 0 otherwise.
Jonathan Austin 1:8aa5cdb4ab67 271 */
Jonathan Austin 1:8aa5cdb4ab67 272 int isAnalog();
Jonathan Austin 1:8aa5cdb4ab67 273
Jonathan Austin 1:8aa5cdb4ab67 274 /**
Jonathan Austin 1:8aa5cdb4ab67 275 * Configures this IO pin as a "makey makey" style touch sensor (if necessary)
Jonathan Austin 1:8aa5cdb4ab67 276 * and tests its current debounced state.
Jonathan Austin 1:8aa5cdb4ab67 277 *
Jonathan Austin 1:8aa5cdb4ab67 278 * Users can also subscribe to MicroBitButton events generated from this pin.
Jonathan Austin 1:8aa5cdb4ab67 279 *
Jonathan Austin 1:8aa5cdb4ab67 280 * @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 281 *
Jonathan Austin 1:8aa5cdb4ab67 282 * @code
Jonathan Austin 1:8aa5cdb4ab67 283 * MicroBitMessageBus bus;
Jonathan Austin 1:8aa5cdb4ab67 284 *
Jonathan Austin 1:8aa5cdb4ab67 285 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL);
Jonathan Austin 1:8aa5cdb4ab67 286 * if(P0.isTouched())
Jonathan Austin 1:8aa5cdb4ab67 287 * {
Jonathan Austin 1:8aa5cdb4ab67 288 * //do something!
Jonathan Austin 1:8aa5cdb4ab67 289 * }
Jonathan Austin 1:8aa5cdb4ab67 290 *
Jonathan Austin 1:8aa5cdb4ab67 291 * // subscribe to events generated by this pin!
Jonathan Austin 1:8aa5cdb4ab67 292 * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_BUTTON_EVT_CLICK, someFunction);
Jonathan Austin 1:8aa5cdb4ab67 293 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 294 */
Jonathan Austin 1:8aa5cdb4ab67 295 int isTouched();
Jonathan Austin 1:8aa5cdb4ab67 296
Jonathan Austin 1:8aa5cdb4ab67 297 /**
Jonathan Austin 1:8aa5cdb4ab67 298 * Configures this IO pin as an analog/pwm output if it isn't already, configures the period to be 20ms,
Jonathan Austin 1:8aa5cdb4ab67 299 * and sets the pulse width, based on the value it is given.
Jonathan Austin 1:8aa5cdb4ab67 300 *
Jonathan Austin 1:8aa5cdb4ab67 301 * @param pulseWidth the desired pulse width in microseconds.
Jonathan Austin 1:8aa5cdb4ab67 302 *
Jonathan Austin 1:8aa5cdb4ab67 303 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED
Jonathan Austin 1:8aa5cdb4ab67 304 * if the given pin does not have analog capability.
Jonathan Austin 1:8aa5cdb4ab67 305 */
Jonathan Austin 1:8aa5cdb4ab67 306 int setServoPulseUs(int pulseWidth);
Jonathan Austin 1:8aa5cdb4ab67 307
Jonathan Austin 1:8aa5cdb4ab67 308 /**
Jonathan Austin 1:8aa5cdb4ab67 309 * Configures the PWM period of the analog output to the given value.
Jonathan Austin 1:8aa5cdb4ab67 310 *
Jonathan Austin 1:8aa5cdb4ab67 311 * @param period The new period for the analog output in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 312 *
Jonathan Austin 1:8aa5cdb4ab67 313 * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the
Jonathan Austin 1:8aa5cdb4ab67 314 * given pin is not configured as an analog output.
Jonathan Austin 1:8aa5cdb4ab67 315 */
Jonathan Austin 1:8aa5cdb4ab67 316 int setAnalogPeriod(int period);
Jonathan Austin 1:8aa5cdb4ab67 317
Jonathan Austin 1:8aa5cdb4ab67 318 /**
Jonathan Austin 1:8aa5cdb4ab67 319 * Configures the PWM period of the analog output to the given value.
Jonathan Austin 1:8aa5cdb4ab67 320 *
Jonathan Austin 1:8aa5cdb4ab67 321 * @param period The new period for the analog output in microseconds.
Jonathan Austin 1:8aa5cdb4ab67 322 *
Jonathan Austin 1:8aa5cdb4ab67 323 * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the
Jonathan Austin 1:8aa5cdb4ab67 324 * given pin is not configured as an analog output.
Jonathan Austin 1:8aa5cdb4ab67 325 */
Jonathan Austin 1:8aa5cdb4ab67 326 int setAnalogPeriodUs(int period);
Jonathan Austin 1:8aa5cdb4ab67 327
Jonathan Austin 1:8aa5cdb4ab67 328 /**
Jonathan Austin 1:8aa5cdb4ab67 329 * Obtains the PWM period of the analog output in microseconds.
Jonathan Austin 1:8aa5cdb4ab67 330 *
Jonathan Austin 1:8aa5cdb4ab67 331 * @return the period on success, or MICROBIT_NOT_SUPPORTED if the
Jonathan Austin 1:8aa5cdb4ab67 332 * given pin is not configured as an analog output.
Jonathan Austin 1:8aa5cdb4ab67 333 */
Jonathan Austin 1:8aa5cdb4ab67 334 int getAnalogPeriodUs();
Jonathan Austin 1:8aa5cdb4ab67 335
Jonathan Austin 1:8aa5cdb4ab67 336 /**
Jonathan Austin 1:8aa5cdb4ab67 337 * Obtains the PWM period of the analog output in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 338 *
Jonathan Austin 1:8aa5cdb4ab67 339 * @return the period on success, or MICROBIT_NOT_SUPPORTED if the
Jonathan Austin 1:8aa5cdb4ab67 340 * given pin is not configured as an analog output.
Jonathan Austin 1:8aa5cdb4ab67 341 */
Jonathan Austin 1:8aa5cdb4ab67 342 int getAnalogPeriod();
LancasterUniversity 35:8ce23bc1af38 343
LancasterUniversity 35:8ce23bc1af38 344 /**
LancasterUniversity 35:8ce23bc1af38 345 * Configures the events generated by this MicroBitPin instance.
LancasterUniversity 35:8ce23bc1af38 346 *
LancasterUniversity 35:8ce23bc1af38 347 * 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 348 * 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 349 * 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 350 * MICROBIT_PIN_EVENT_NONE - Disables events for this pin.
LancasterUniversity 35:8ce23bc1af38 351 *
LancasterUniversity 35:8ce23bc1af38 352 * @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 353 *
LancasterUniversity 35:8ce23bc1af38 354 * @code
LancasterUniversity 35:8ce23bc1af38 355 * MicroBitMessageBus bus;
LancasterUniversity 35:8ce23bc1af38 356 *
LancasterUniversity 35:8ce23bc1af38 357 * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
LancasterUniversity 35:8ce23bc1af38 358 * P0.eventOn(MICROBIT_PIN_EVENT_ON_PULSE);
LancasterUniversity 35:8ce23bc1af38 359 *
LancasterUniversity 35:8ce23bc1af38 360 * void onPulse(MicroBitEvent evt)
LancasterUniversity 35:8ce23bc1af38 361 * {
LancasterUniversity 35:8ce23bc1af38 362 * int duration = evt.timestamp;
LancasterUniversity 35:8ce23bc1af38 363 * }
LancasterUniversity 35:8ce23bc1af38 364 *
LancasterUniversity 35:8ce23bc1af38 365 * bus.listen(MICROBIT_ID_IO_P0, MICROBIT_PIN_EVT_PULSE_HI, onPulse, MESSAGE_BUS_LISTENER_IMMEDIATE)
LancasterUniversity 35:8ce23bc1af38 366 * @endcode
LancasterUniversity 35:8ce23bc1af38 367 *
LancasterUniversity 35:8ce23bc1af38 368 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the given eventype does not match
LancasterUniversity 35:8ce23bc1af38 369 *
LancasterUniversity 35:8ce23bc1af38 370 * @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 371 * please use the InterruptIn class supplied by ARM mbed.
LancasterUniversity 35:8ce23bc1af38 372 */
LancasterUniversity 35:8ce23bc1af38 373 int eventOn(int eventType);
Jonathan Austin 1:8aa5cdb4ab67 374 };
Jonathan Austin 1:8aa5cdb4ab67 375
LancasterUniversity 35:8ce23bc1af38 376 #endif