diff --git a/inc/MicroBitPin.h b/inc/MicroBitPin.h index 65bece4..ee8c557 100644 --- a/inc/MicroBitPin.h +++ b/inc/MicroBitPin.h @@ -6,23 +6,23 @@ // 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 -#define IO_STATUS_ANALOG_IN 0x04 // Pin is Analog in -#define IO_STATUS_ANALOG_OUT 0x08 // Pin is Analog out (not currently possible) +#define IO_STATUS_ANALOG_IN 0x04 // Pin is Analog in +#define IO_STATUS_ANALOG_OUT 0x08 // Pin is Analog out #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 -#define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL) +#define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL) #define MICROBIT_PIN_P2 P0_1 //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board -#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) +#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) #define MICROBIT_PIN_P6 P0_12 //COL9 #define MICROBIT_PIN_P7 P0_11 //COL8 #define MICROBIT_PIN_P8 P0_18 //PIN 18 #define MICROBIT_PIN_P9 P0_10 //COL7 -#define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL) +#define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL) #define MICROBIT_PIN_P11 P0_26 //BTN_B #define MICROBIT_PIN_P12 P0_20 //PIN 20 #define MICROBIT_PIN_P13 P0_23 //SCK @@ -36,7 +36,7 @@ /** - * Pin capabilities enum. + * Pin capabilities enum. * Used to determine the capabilities of each Pin as some can only be digital, or can be both digital and analogue. */ enum PinCapability{ @@ -45,7 +45,7 @@ enum PinCapability{ 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 - + }; /** @@ -56,56 +56,56 @@ enum PinCapability{ class MicroBitPin : public MicroBitComponent { /** - * Unique, enumerated ID for this component. + * Unique, enumerated ID for this component. * Used to track asynchronous events in the event bus. */ - + void *pin; // The mBed object looking after this pin at any point in time (may change!). PinCapability capability; - + /** * Disconnect any attached mBed IO from this pin. * Used only when pin changes mode (i.e. Input/Output/Analog/Digital) */ void disconnect(); - + public: PinName name; // mBed pin name of this pin. - + /** - * Constructor. + * Constructor. * 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 * @param capability the capability of this pin. - * + * * Example: - * @code + * @code * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); * @endcode */ MicroBitPin(int id, PinName name, PinCapability capability); - + /** * Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'. * @param value 0 (LO) or 1 (HI) * @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. - * + * * Example: - * @code + * @code * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); * P0.setDigitalValue(1); // P0 is now HI * @endcode */ int setDigitalValue(int value); - + /** * Configures this IO pin as a digital input (if necessary) and tests its current value. * @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. - * + * * Example: - * @code + * @code * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); * P0.getDigitalValue(); // P0 is either 0 or 1; * @endcode @@ -124,9 +124,9 @@ class MicroBitPin : public MicroBitComponent /** * Configures this IO pin as an analogue input (if necessary and possible). * @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. - * + * * Example: - * @code + * @code * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH); * P0.getAnalogValue(); // P0 is a value in the range of 0 - 1024 * @endcode @@ -160,14 +160,14 @@ class MicroBitPin : public MicroBitComponent /** * Configures this IO pin as a makey makey style touch sensor (if necessary) and tests its current debounced state. * @return 1 if pin is touched, 0 if not, or MICROBIT_NOT_SUPPORTED if this pin does not support touch capability. - * + * * Example: - * @code + * @code * MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL); * if(P0.isTouched()) * { * uBit.display.clear(); - * } + * } * @endcode */ int isTouched(); @@ -176,19 +176,35 @@ class MicroBitPin : public MicroBitComponent * Configures the PWM period of the analog output to the given value. * * @param period The new period for the analog output in milliseconds. - * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the + * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the * given pin is not configured as an analog output. - */ + */ int setAnalogPeriod(int period); /** * Configures the PWM period of the analog output to the given value. * * @param period The new period for the analog output in microseconds. - * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the + * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the * given pin is not configured as an analog output. - */ + */ int setAnalogPeriodUs(int period); + + /** + * 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(); }; #endif diff --git a/source/MicroBitPin.cpp b/source/MicroBitPin.cpp index 9785845..de928e2 100644 --- a/source/MicroBitPin.cpp +++ b/source/MicroBitPin.cpp @@ -147,7 +147,7 @@ int MicroBitPin::setAnalogValue(int value) //perform a write with an extra check! :) if(((DynamicPwm *)pin)->getPinName() == name) - ((DynamicPwm *)pin)->write(level); + return ((DynamicPwm *)pin)->write(level); return MICROBIT_OK; } @@ -257,8 +257,7 @@ int MicroBitPin::setAnalogPeriodUs(int period) if (!(status & IO_STATUS_ANALOG_OUT)) return MICROBIT_NOT_SUPPORTED; - ((DynamicPwm *)pin)->setPeriodUs(period); - return MICROBIT_OK; + return ((DynamicPwm *)pin)->setPeriodUs(period); } /** @@ -272,3 +271,28 @@ int MicroBitPin::setAnalogPeriod(int period) { return setAnalogPeriodUs(period*1000); } + +/** + * 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 MicroBitPin::getAnalogPeriodUs() +{ + if (!(status & IO_STATUS_ANALOG_OUT)) + return MICROBIT_NOT_SUPPORTED; + + return ((DynamicPwm *)pin)->getPeriodUs(); +} + +/** + * 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 MicroBitPin::getAnalogPeriod() +{ + return getAnalogPeriodUs()/1000; +}