microbit-dal/inc/MicroBitButton.h

102 lines
3.6 KiB
C
Raw Normal View History

#ifndef MICROBIT_BUTTON_H
#define MICROBIT_BUTTON_H
#include "mbed.h"
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
#include "MicroBitComponent.h"
#include "MicroBitEvent.h"
//TODO: When platform is built for MB2 - pins will be defined by default, these will change...
#define MICROBIT_PIN_BUTTON_A P0_17
#define MICROBIT_PIN_BUTTON_B P0_26
#define MICROBIT_PIN_BUTTON_RESET P0_19
#define MICROBIT_BUTTON_EVT_DOWN 1
#define MICROBIT_BUTTON_EVT_UP 2
#define MICROBIT_BUTTON_EVT_CLICK 3
#define MICROBIT_BUTTON_EVT_LONG_CLICK 4
#define MICROBIT_BUTTON_EVT_HOLD 5
#define MICROBIT_BUTTON_EVT_DOUBLE_CLICK 6
#define MICROBIT_BUTTON_LONG_CLICK_TIME 1000
#define MICROBIT_BUTTON_HOLD_TIME 1500
#define MICROBIT_BUTTON_STATE 1
#define MICROBIT_BUTTON_STATE_HOLD_TRIGGERED 2
#define MICROBIT_BUTTON_STATE_CLICK 4
#define MICROBIT_BUTTON_STATE_LONG_CLICK 8
#define MICROBIT_BUTTON_SIGMA_MIN 0
#define MICROBIT_BUTTON_SIGMA_MAX 12
#define MICROBIT_BUTTON_SIGMA_THRESH_HI 8
#define MICROBIT_BUTTON_SIGMA_THRESH_LO 2
#define MICROBIT_BUTTON_DOUBLE_CLICK_THRESH 50
enum MicroBitButtonEventConfiguration
{
MICROBIT_BUTTON_SIMPLE_EVENTS,
MICROBIT_BUTTON_ALL_EVENTS
};
/**
* Class definition for MicroBit Button.
*
* Represents a single, generic button on the device.
*/
class MicroBitButton : public MicroBitComponent
{
PinName name; // mbed pin name of this pin.
DigitalIn pin; // The mbed object looking after this pin at any point in time (may change!).
unsigned long downStartTime; // used to store the current system clock when a button down event occurs
uint8_t sigma; // integration of samples over time. We use this for debouncing, and noise tolerance for touch sensing
MicroBitButtonEventConfiguration eventConfiguration; // Do we want to generate high level event (clicks), or defer this to another service.
public:
/**
* Constructor.
* Create a pin representation with the given ID.
* @param id the ID of the new MicroBitButton object.
* @param name the physical pin on the processor that this butotn is connected to.
* @param mode the configuration of internal pullups/pulldowns, as define in the mbed PinMode class. PullNone by default.
*
* Example:
* @code
* buttonA(MICROBIT_ID_BUTTON_A,MICROBIT_PIN_BUTTON_A); //a number between 0 and 200 inclusive
* @endcode
*
* Possible Events:
* @code
* MICROBIT_BUTTON_EVT_DOWN
* MICROBIT_BUTTON_EVT_UP
* MICROBIT_BUTTON_EVT_CLICK
* MICROBIT_BUTTON_EVT_LONG_CLICK
* MICROBIT_BUTTON_EVT_DOUBLE_CLICK
* MICROBIT_BUTTON_EVT_HOLD
* @endcode
*/
MicroBitButton(uint16_t id, PinName name, MicroBitButtonEventConfiguration eventConfiguration = MICROBIT_BUTTON_ALL_EVENTS, PinMode mode = PullNone);
/**
* Tests if this Button is currently pressed.
* @return 1 if this button is pressed, 0 otherwise.
*
* Example:
* @code
* if(uBit.buttonA.isPressed())
* print("Pressed!");
* @endcode
*/
int isPressed();
/**
* periodic callback from MicroBit clock.
* Check for state change for this button, and fires a hold event if button is pressed.
*/
virtual void systemTick();
};
#endif