2015-08-12 10:53:41 +00:00
|
|
|
#ifndef MICROBIT_PIN_H
|
|
|
|
#define MICROBIT_PIN_H
|
|
|
|
|
|
|
|
#include "mbed.h"
|
2015-09-11 15:39:38 +00:00
|
|
|
#include "MicroBitComponent.h"
|
2015-08-12 10:53:41 +00:00
|
|
|
// Status Field flags...
|
|
|
|
#define IO_STATUS_DIGITAL_IN 0x01 // Pin is configured as a digital input, with no pull up.
|
|
|
|
#define IO_STATUS_DIGITAL_OUT 0x02 // Pin is configured as a digital output
|
2016-01-09 19:52:46 +00:00
|
|
|
#define IO_STATUS_ANALOG_IN 0x04 // Pin is Analog in
|
|
|
|
#define IO_STATUS_ANALOG_OUT 0x08 // Pin is Analog out
|
2015-08-12 10:53:41 +00:00
|
|
|
#define IO_STATUS_TOUCH_IN 0x10 // Pin is a makey-makey style touch sensor
|
|
|
|
#define IO_STATUS_EVENTBUS_ENABLED 0x80 // Pin is will generate events on change
|
|
|
|
|
|
|
|
//#defines for each edge connector pin
|
|
|
|
#define MICROBIT_PIN_P0 P0_3 //P0 is the left most pad (ANALOG/DIGITAL) used to be P0_3 on green board
|
2016-01-09 19:52:46 +00:00
|
|
|
#define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL)
|
2015-08-12 10:53:41 +00:00
|
|
|
#define MICROBIT_PIN_P2 P0_1 //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board
|
2016-01-09 19:52:46 +00:00
|
|
|
#define MICROBIT_PIN_P3 P0_4 //COL1 (ANALOG/DIGITAL)
|
|
|
|
#define MICROBIT_PIN_P4 P0_17 //BTN_A
|
|
|
|
#define MICROBIT_PIN_P5 P0_5 //COL2 (ANALOG/DIGITAL)
|
2015-11-13 23:06:54 +00:00
|
|
|
#define MICROBIT_PIN_P6 P0_12 //COL9
|
|
|
|
#define MICROBIT_PIN_P7 P0_11 //COL8
|
2015-08-12 10:53:41 +00:00
|
|
|
#define MICROBIT_PIN_P8 P0_18 //PIN 18
|
2015-11-13 23:06:54 +00:00
|
|
|
#define MICROBIT_PIN_P9 P0_10 //COL7
|
2016-01-09 19:52:46 +00:00
|
|
|
#define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL)
|
2015-08-12 10:53:41 +00:00
|
|
|
#define MICROBIT_PIN_P11 P0_26 //BTN_B
|
|
|
|
#define MICROBIT_PIN_P12 P0_20 //PIN 20
|
|
|
|
#define MICROBIT_PIN_P13 P0_23 //SCK
|
|
|
|
#define MICROBIT_PIN_P14 P0_22 //MISO
|
|
|
|
#define MICROBIT_PIN_P15 P0_21 //MOSI
|
|
|
|
#define MICROBIT_PIN_P16 P0_16 //PIN 16
|
|
|
|
#define MICROBIT_PIN_P19 P0_0 //SCL
|
|
|
|
#define MICROBIT_PIN_P20 P0_30 //SDA
|
|
|
|
|
|
|
|
#define MICROBIT_PIN_MAX_OUTPUT 1023
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-09 19:52:46 +00:00
|
|
|
* Pin capabilities enum.
|
2015-08-12 10:53:41 +00:00
|
|
|
* Used to determine the capabilities of each Pin as some can only be digital, or can be both digital and analogue.
|
|
|
|
*/
|
|
|
|
enum PinCapability{
|
|
|
|
PIN_CAPABILITY_DIGITAL = 0x01,
|
|
|
|
PIN_CAPABILITY_ANALOG = 0x02,
|
|
|
|
PIN_CAPABILITY_TOUCH = 0x04,
|
|
|
|
PIN_CAPABILITY_AD = PIN_CAPABILITY_DIGITAL | PIN_CAPABILITY_ANALOG,
|
|
|
|
PIN_CAPABILITY_ALL = PIN_CAPABILITY_DIGITAL | PIN_CAPABILITY_ANALOG | PIN_CAPABILITY_TOUCH
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class definition for MicroBitPin.
|
|
|
|
*
|
|
|
|
* Represents a I/O on the edge connector.
|
|
|
|
*/
|
|
|
|
class MicroBitPin : public MicroBitComponent
|
|
|
|
{
|
|
|
|
/**
|
2016-01-09 19:52:46 +00:00
|
|
|
* Unique, enumerated ID for this component.
|
2015-08-12 10:53:41 +00:00
|
|
|
* Used to track asynchronous events in the event bus.
|
|
|
|
*/
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
void *pin; // The mBed object looking after this pin at any point in time (may change!).
|
|
|
|
PinCapability capability;
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
/**
|
|
|
|
* Disconnect any attached mBed IO from this pin.
|
|
|
|
* Used only when pin changes mode (i.e. Input/Output/Analog/Digital)
|
|
|
|
*/
|
|
|
|
void disconnect();
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
public:
|
2015-08-14 22:51:16 +00:00
|
|
|
PinName name; // mBed pin name of this pin.
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
/**
|
2016-01-09 19:52:46 +00:00
|
|
|
* Constructor.
|
2015-08-12 10:53:41 +00:00
|
|
|
* Create a Button representation with the given ID.
|
|
|
|
* @param id the ID of the new Pin object.
|
|
|
|
* @param name the pin name for this MicroBitPin instance to represent
|
2015-10-25 21:51:33 +00:00
|
|
|
* @param capability the capability of this pin.
|
2016-01-09 19:52:46 +00:00
|
|
|
*
|
2015-08-12 10:53:41 +00:00
|
|
|
* Example:
|
2016-01-09 19:52:46 +00:00
|
|
|
* @code
|
2015-08-12 10:53:41 +00:00
|
|
|
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
MicroBitPin(int id, PinName name, PinCapability capability);
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
/**
|
|
|
|
* Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'.
|
|
|
|
* @param value 0 (LO) or 1 (HI)
|
2015-10-25 21:51:33 +00:00
|
|
|
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED
|
|
|
|
* if the given pin does not have digital capability.
|
2016-01-09 19:52:46 +00:00
|
|
|
*
|
2015-08-12 10:53:41 +00:00
|
|
|
* Example:
|
2016-01-09 19:52:46 +00:00
|
|
|
* @code
|
2015-08-12 10:53:41 +00:00
|
|
|
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
|
2015-10-25 21:51:33 +00:00
|
|
|
* P0.setDigitalValue(1); // P0 is now HI
|
2015-08-12 10:53:41 +00:00
|
|
|
* @endcode
|
|
|
|
*/
|
2015-10-25 21:51:33 +00:00
|
|
|
int setDigitalValue(int value);
|
2016-01-09 19:52:46 +00:00
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
/**
|
|
|
|
* Configures this IO pin as a digital input (if necessary) and tests its current value.
|
2015-10-25 21:51:33 +00:00
|
|
|
* @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED if the given pin does not have analog capability.
|
2016-01-09 19:52:46 +00:00
|
|
|
*
|
2015-08-12 10:53:41 +00:00
|
|
|
* Example:
|
2016-01-09 19:52:46 +00:00
|
|
|
* @code
|
2015-08-12 10:53:41 +00:00
|
|
|
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
|
|
|
|
* P0.getDigitalValue(); // P0 is either 0 or 1;
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
int getDigitalValue();
|
|
|
|
|
|
|
|
/**
|
2015-10-25 21:51:33 +00:00
|
|
|
* Configures this IO pin as an analog/pwm output, and change the output value to the given level.
|
|
|
|
* @param value the level to set on the output pin, in the range 0 - 1024
|
|
|
|
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range, or MICROBIT_NOT_SUPPORTED
|
|
|
|
* if the given pin does not have analog capability.
|
2015-08-12 10:53:41 +00:00
|
|
|
*/
|
2015-10-25 21:51:33 +00:00
|
|
|
int setAnalogValue(int value);
|
2015-08-12 10:53:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures this IO pin as an analogue input (if necessary and possible).
|
2015-10-25 21:51:33 +00:00
|
|
|
* @return the current analogue level on the pin, in the range 0 - 1024, or MICROBIT_NOT_SUPPORTED if the given pin does not have analog capability.
|
2016-01-09 19:52:46 +00:00
|
|
|
*
|
2015-08-12 10:53:41 +00:00
|
|
|
* Example:
|
2016-01-09 19:52:46 +00:00
|
|
|
* @code
|
2015-08-12 10:53:41 +00:00
|
|
|
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
|
2015-10-25 21:51:33 +00:00
|
|
|
* P0.getAnalogValue(); // P0 is a value in the range of 0 - 1024
|
2015-08-12 10:53:41 +00:00
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
int getAnalogValue();
|
|
|
|
|
2015-09-22 15:13:08 +00:00
|
|
|
/**
|
|
|
|
* Determines if this IO pin is currently configured as an input.
|
|
|
|
* @return 1 if pin is an analog or digital input, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int isInput();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this IO pin is currently configured as an output.
|
|
|
|
* @return 1 if pin is an analog or digital output, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int isOutput();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this IO pin is currently configured for digital use.
|
|
|
|
* @return 1 if pin is digital, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int isDigital();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this IO pin is currently configured for analog use.
|
|
|
|
* @return 1 if pin is analog, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int isAnalog();
|
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
/**
|
|
|
|
* Configures this IO pin as a makey makey style touch sensor (if necessary) and tests its current debounced state.
|
2015-10-25 21:51:33 +00:00
|
|
|
* @return 1 if pin is touched, 0 if not, or MICROBIT_NOT_SUPPORTED if this pin does not support touch capability.
|
2016-01-09 19:52:46 +00:00
|
|
|
*
|
2015-08-12 10:53:41 +00:00
|
|
|
* Example:
|
2016-01-09 19:52:46 +00:00
|
|
|
* @code
|
2015-08-12 10:53:41 +00:00
|
|
|
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL);
|
|
|
|
* if(P0.isTouched())
|
|
|
|
* {
|
|
|
|
* uBit.display.clear();
|
2016-01-09 19:52:46 +00:00
|
|
|
* }
|
2015-08-12 10:53:41 +00:00
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
int isTouched();
|
|
|
|
|
2015-10-25 21:51:33 +00:00
|
|
|
/**
|
|
|
|
* Configures the PWM period of the analog output to the given value.
|
|
|
|
*
|
|
|
|
* @param period The new period for the analog output in milliseconds.
|
2016-01-09 19:52:46 +00:00
|
|
|
* @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the
|
2015-10-25 21:51:33 +00:00
|
|
|
* given pin is not configured as an analog output.
|
2016-01-09 19:52:46 +00:00
|
|
|
*/
|
2015-10-25 21:51:33 +00:00
|
|
|
int setAnalogPeriod(int period);
|
2015-09-16 18:46:39 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-25 21:51:33 +00:00
|
|
|
* Configures the PWM period of the analog output to the given value.
|
|
|
|
*
|
|
|
|
* @param period The new period for the analog output in microseconds.
|
2016-01-09 19:52:46 +00:00
|
|
|
* @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the
|
2015-10-25 21:51:33 +00:00
|
|
|
* given pin is not configured as an analog output.
|
2016-01-09 19:52:46 +00:00
|
|
|
*/
|
2015-10-25 21:51:33 +00:00
|
|
|
int setAnalogPeriodUs(int period);
|
2016-01-09 19:52:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the PWM period of the analog output.
|
|
|
|
*
|
|
|
|
* @return the period on success, or MICROBIT_NOT_SUPPORTED if the
|
|
|
|
* given pin is not configured as an analog output.
|
|
|
|
*/
|
|
|
|
int getAnalogPeriodUs();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the PWM period of the analog output.
|
|
|
|
*
|
|
|
|
* @return the period on success, or MICROBIT_NOT_SUPPORTED if the
|
|
|
|
* given pin is not configured as an analog output.
|
|
|
|
*/
|
|
|
|
int getAnalogPeriod();
|
2015-08-12 10:53:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|