Skip to content

Commit

Permalink
Merge pull request #158 from lancaster-university/156-readDigitalValu…
Browse files Browse the repository at this point in the history
…ePullOverload

microbit: Added getDigitalValue overload for PullMode #156
  • Loading branch information
finneyj committed Jun 3, 2016
2 parents 179b32f + 4cf4803 commit f214bc9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
13 changes: 13 additions & 0 deletions inc/core/MicroBitConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ DEALINGS IN THE SOFTWARE.
#endif


//
// I/O Options
//


//
// Define the default mode in which the digital input pins are configured.
// valid options are PullDown, PullUp and PullNone.
//
#ifndef MICROBIT_DEFAULT_PULLMODE
#define MICROBIT_DEFAULT_PULLMODE PullDown
#endif

//
// Panic options
//
Expand Down
17 changes: 16 additions & 1 deletion inc/drivers/MicroBitPin.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class MicroBitPin : public MicroBitComponent
{
// The mbed object looking after this pin at any point in time (untyped due to dynamic behaviour).
void *pin;

PinCapability capability;
uint8_t pullMode;

/**
* Disconnect any attached mBed IO from this pin.
Expand Down Expand Up @@ -199,6 +199,21 @@ class MicroBitPin : public MicroBitComponent
*/
int getDigitalValue();

/**
* Configures this IO pin as a digital input with the specified internal pull-up/pull-down configuraiton (if necessary) and tests its current value.
*
* @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone
*
* @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED
* if the given pin does not have digital capability.
*
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
* P0.getDigitalValue(PullUp); // P0 is either 0 or 1;
* @endcode
*/
int getDigitalValue(PinMode pull);

/**
* Configures this IO pin as an analog/pwm output, and change the output value to the given level.
*
Expand Down
4 changes: 4 additions & 0 deletions inc/platform/yotta_cfg_mappings.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
#define MICROBIT_DEFAULT_PRINT_SPEED YOTTA_CFG_MICROBIT_DAL_DISPLAY_PRINT_SPEED
#endif

#ifdef YOTTA_CFG_MICROBIT_DAL_DEFAULT_PULLMODE
#define MICROBIT_DEFAULT_PULLMODE YOTTA_CFG_MICROBIT_DAL_DEFAULT_PULLMODE
#endif

#ifdef YOTTA_CFG_MICROBIT_DAL_PANIC_ON_HEAP_FULL
#define MICROBIT_PANIC_HEAP_FULL YOTTA_CFG_MICROBIT_DAL_PANIC_ON_HEAP_FULL
#endif
Expand Down
26 changes: 24 additions & 2 deletions source/drivers/MicroBitPin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ MicroBitPin::MicroBitPin(int id, PinName name, PinCapability capability)
this->id = id;
this->name = name;
this->capability = capability;
this->pullMode = MICROBIT_DEFAULT_PULLMODE;

// Power up in a disconnected, low power state.
// If we're unused, this is how it will stay...
Expand Down Expand Up @@ -159,7 +160,7 @@ int MicroBitPin::getDigitalValue()
if (!(status & (IO_STATUS_DIGITAL_IN | IO_STATUS_EVENT_ON_EDGE | IO_STATUS_EVENT_PULSE_ON_EDGE)))
{
disconnect();
pin = new DigitalIn(name,PullDown);
pin = new DigitalIn(name, (PinMode)pullMode);
status |= IO_STATUS_DIGITAL_IN;
}

Expand All @@ -169,6 +170,25 @@ int MicroBitPin::getDigitalValue()
return ((DigitalIn *)pin)->read();
}

/**
* Configures this IO pin as a digital input with the specified internal pull-up/pull-down configuraiton (if necessary) and tests its current value.
*
* @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone
*
* @return 1 if this input is high, 0 if input is LO, or MICROBIT_NOT_SUPPORTED
* if the given pin does not have digital capability.
*
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
* P0.getDigitalValue(PullUp); // P0 is either 0 or 1;
* @endcode
*/
int MicroBitPin::getDigitalValue(PinMode pull)
{
setPull(pull);
return getDigitalValue();
}

int MicroBitPin::obtainAnalogChannel()
{
// Move into an analogue input state if necessary, if we are no longer the focus of a DynamicPWM instance, allocate ourselves again!
Expand Down Expand Up @@ -451,6 +471,8 @@ int MicroBitPin::getAnalogPeriod()
*/
int MicroBitPin::setPull(PinMode pull)
{
pullMode = pull;

if ((status & IO_STATUS_DIGITAL_IN))
{
((DigitalIn *)pin)->mode(pull);
Expand Down Expand Up @@ -528,7 +550,7 @@ int MicroBitPin::enableRiseFallEvents(int eventType)
disconnect();
pin = new TimedInterruptIn(name);

((TimedInterruptIn *)pin)->mode(PullDown);
((TimedInterruptIn *)pin)->mode((PinMode)pullMode);
((TimedInterruptIn *)pin)->rise(this, &MicroBitPin::onRise);
((TimedInterruptIn *)pin)->fall(this, &MicroBitPin::onFall);
}
Expand Down

0 comments on commit f214bc9

Please sign in to comment.