microbit-dal/source/MicroBitMultiButton.cpp

212 lines
6.1 KiB
C++
Raw Normal View History

microbit: Memory Optimisation Mega Update This release contains a widespread set of updates and optimisations to the micro:bit runtime, with a view to reducing the SRAM footprint of the whole system. This is to provide as much usable HEAP storage for application programs as possible. Specific updates and optimisations include: - Additional compilation flags to allow the core micro:bit runtime to be configured. These are defined in MicroBitConfig.h - A custom heap allocator. This is now included for two reasons: 1) To provide a simple mechanism to to utilise both the mbed heap space and other memory regions (such as unused memory in the SoftDevice region) as a single virtual heap. 2) To address some issues that have been noted that are attributable to heap fragmentation. The micro:bit heap allocator has a simple algorithm, but one that is chosen to respond well to the relativelt high 'heap churn' found in the micro:bit environment. All micro:bit components and user programs now use this heap allocator trasparently. - Updates to BLE services to remove persistent references to their GATT services. This consumes vast amounts SRAM, rather unecessarily. Instead only handles to the relevant GATT characteristics are now stored. This specifically includes: + MicroBitDFUService + MicroBitEventService + DeviceInformationService - Updates to the Fiber scheduler to save SRAM. More specifically: + Removed the need to hold an empty processor context to intialise fibers. + The IDLE fiber now runs without a stack + fiber stacks are now only created when a fiber is descheduled for the first time, thereby reducing heap churn. + the 'main' fiber is now recycled into the fiber_pool if it leaves app_main() + fibers created through invoke() now only maintains the necessary part of teh parent stack that is needed, thereby reducing the stack size of spawned fibers. - Updates to the Message Bus to reduce the overall memory footprint of processing events. More specifically: + Event handlers are now always called using invoke(), such that non-blocking event handlers no longer need a dedicated fiber to execute - thereby saving SRAM and processor time. + Processing of events from the event queue is now rate paced. Events only continue to be processed as long as there are no fibers on the run queue. i.e. event processing is no longer greedy, thereby reducing the number of fibers created on the runqueue. - Updates to BLUEZOENE code to bring up core BLE services even if they are not enabled by default. This allows programs that do not require BLE to operate to benefit from the full range of SRAM, whilst still allowing the device to be programmed over BLE. - Updates to the Soft Device initialisation configuration, reducing the size of the GATT table held in the top 1.8K of its 8K memory region to around 800 bytes. This is sufficient to run the default set of BLE services on the micro:bit so the additional memory is configured as HEAP storage by MicroBitHeapAllocator. - Minor changes to a range of components to integrate with the above changes. + rename of free() to release() in DynamicPWM to avoid namespace collision with MicroBitHeap free() + rename of fork_on_block to invoke() to enhance readbility. - Many code cleanups and updates to out of date comments.
2015-08-31 22:25:10 +00:00
#include "MicroBit.h"
/**
* Constructor.
microbit: Memory Optimisation Mega Update This release contains a widespread set of updates and optimisations to the micro:bit runtime, with a view to reducing the SRAM footprint of the whole system. This is to provide as much usable HEAP storage for application programs as possible. Specific updates and optimisations include: - Additional compilation flags to allow the core micro:bit runtime to be configured. These are defined in MicroBitConfig.h - A custom heap allocator. This is now included for two reasons: 1) To provide a simple mechanism to to utilise both the mbed heap space and other memory regions (such as unused memory in the SoftDevice region) as a single virtual heap. 2) To address some issues that have been noted that are attributable to heap fragmentation. The micro:bit heap allocator has a simple algorithm, but one that is chosen to respond well to the relativelt high 'heap churn' found in the micro:bit environment. All micro:bit components and user programs now use this heap allocator trasparently. - Updates to BLE services to remove persistent references to their GATT services. This consumes vast amounts SRAM, rather unecessarily. Instead only handles to the relevant GATT characteristics are now stored. This specifically includes: + MicroBitDFUService + MicroBitEventService + DeviceInformationService - Updates to the Fiber scheduler to save SRAM. More specifically: + Removed the need to hold an empty processor context to intialise fibers. + The IDLE fiber now runs without a stack + fiber stacks are now only created when a fiber is descheduled for the first time, thereby reducing heap churn. + the 'main' fiber is now recycled into the fiber_pool if it leaves app_main() + fibers created through invoke() now only maintains the necessary part of teh parent stack that is needed, thereby reducing the stack size of spawned fibers. - Updates to the Message Bus to reduce the overall memory footprint of processing events. More specifically: + Event handlers are now always called using invoke(), such that non-blocking event handlers no longer need a dedicated fiber to execute - thereby saving SRAM and processor time. + Processing of events from the event queue is now rate paced. Events only continue to be processed as long as there are no fibers on the run queue. i.e. event processing is no longer greedy, thereby reducing the number of fibers created on the runqueue. - Updates to BLUEZOENE code to bring up core BLE services even if they are not enabled by default. This allows programs that do not require BLE to operate to benefit from the full range of SRAM, whilst still allowing the device to be programmed over BLE. - Updates to the Soft Device initialisation configuration, reducing the size of the GATT table held in the top 1.8K of its 8K memory region to around 800 bytes. This is sufficient to run the default set of BLE services on the micro:bit so the additional memory is configured as HEAP storage by MicroBitHeapAllocator. - Minor changes to a range of components to integrate with the above changes. + rename of free() to release() in DynamicPWM to avoid namespace collision with MicroBitHeap free() + rename of fork_on_block to invoke() to enhance readbility. - Many code cleanups and updates to out of date comments.
2015-08-31 22:25:10 +00:00
* Create a representation of a virtual button, that generates events based upon the combination
* of two given buttons.
* @param id the ID of the new MultiButton object.
* @param button1 the ID of the first button to integrate.
* @param button2 the ID of the second button to integrate.
* @param name the physical pin on the processor that this butotn is connected to.
*
* Example:
* @code
* multiButton(MICROBIT_ID_BUTTON_AB, MICROBIT_ID_BUTTON_A, MICROBIT_ID_BUTTON_B);
* @endcode
*
* Possible Events:
* @code
* MICROBIT_BUTTON_EVT_DOWN
* MICROBIT_BUTTON_EVT_UP
* MICROBIT_BUTTON_EVT_CLICK
* MICROBIT_BUTTON_EVT_LONG_CLICK
* MICROBIT_BUTTON_EVT_HOLD
* @endcode
*/
MicroBitMultiButton::MicroBitMultiButton(uint16_t id, uint16_t button1, uint16_t button2, MicroBitMessageBus &messageBus)
{
this->id = id;
this->button1 = button1;
this->button2 = button2;
this->eventConfiguration = MICROBIT_BUTTON_SIMPLE_EVENTS;
messageBus.listen(button1, MICROBIT_EVT_ANY, this, &MicroBitMultiButton::onButtonEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
messageBus.listen(button2, MICROBIT_EVT_ANY, this, &MicroBitMultiButton::onButtonEvent, MESSAGE_BUS_LISTENER_IMMEDIATE);
}
uint16_t MicroBitMultiButton::otherSubButton(uint16_t b)
{
return (b == button1 ? button2 : button1);
}
int MicroBitMultiButton::isSubButtonPressed(uint16_t button)
{
if (button == button1)
return status & MICROBIT_MULTI_BUTTON_STATE_1;
if (button == button2)
return status & MICROBIT_MULTI_BUTTON_STATE_2;
return 0;
}
int MicroBitMultiButton::isSubButtonHeld(uint16_t button)
{
if (button == button1)
return status & MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1;
if (button == button2)
return status & MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2;
return 0;
}
int MicroBitMultiButton::isSubButtonSupressed(uint16_t button)
{
if (button == button1)
return status & MICROBIT_MULTI_BUTTON_SUPRESSED_1;
if (button == button2)
return status & MICROBIT_MULTI_BUTTON_SUPRESSED_2;
return 0;
}
void MicroBitMultiButton::setButtonState(uint16_t button, int value)
{
if (button == button1)
{
if (value)
status |= MICROBIT_MULTI_BUTTON_STATE_1;
else
status &= ~MICROBIT_MULTI_BUTTON_STATE_1;
}
if (button == button2)
{
if (value)
status |= MICROBIT_MULTI_BUTTON_STATE_2;
else
status &= ~MICROBIT_MULTI_BUTTON_STATE_2;
}
}
void MicroBitMultiButton::setHoldState(uint16_t button, int value)
{
if (button == button1)
{
if (value)
status |= MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1;
else
status &= ~MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1;
}
if (button == button2)
{
if (value)
status |= MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2;
else
status &= ~MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2;
}
}
void MicroBitMultiButton::setSupressedState(uint16_t button, int value)
{
if (button == button1)
{
if (value)
status |= MICROBIT_MULTI_BUTTON_SUPRESSED_1;
else
status &= ~MICROBIT_MULTI_BUTTON_SUPRESSED_1;
}
if (button == button2)
{
if (value)
status |= MICROBIT_MULTI_BUTTON_SUPRESSED_2;
else
status &= ~MICROBIT_MULTI_BUTTON_SUPRESSED_2;
}
}
/**
* Changes the event configuraiton of this button to the given value.
* All subsequent events generated by this button will then be informed by this configuration.
*
* @param config the new configuration for this button. legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS.
*
* example:
* @code
*
* // configure a button to generate all possible events from attached buttons.
* uBit.buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
*
* // configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events.
* uBit.buttonAB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
*
* @endcode
*/
void MicroBitMultiButton::setEventConfiguration(MicroBitButtonEventConfiguration config)
{
this->eventConfiguration = config;
}
void MicroBitMultiButton::onButtonEvent(MicroBitEvent evt)
{
int button = evt.source;
int otherButton = otherSubButton(button);
switch(evt.value)
{
case MICROBIT_BUTTON_EVT_DOWN:
setButtonState(button, 1);
if(isSubButtonPressed(otherButton))
MicroBitEvent e(id, MICROBIT_BUTTON_EVT_DOWN);
break;
case MICROBIT_BUTTON_EVT_HOLD:
setHoldState(button, 1);
if(isSubButtonHeld(otherButton))
MicroBitEvent e(id, MICROBIT_BUTTON_EVT_HOLD);
break;
case MICROBIT_BUTTON_EVT_UP:
if(isSubButtonPressed(otherButton))
{
MicroBitEvent e(id, MICROBIT_BUTTON_EVT_UP);
if (isSubButtonHeld(button) && isSubButtonHeld(otherButton))
MicroBitEvent e(id, MICROBIT_BUTTON_EVT_LONG_CLICK);
else
MicroBitEvent e(id, MICROBIT_BUTTON_EVT_CLICK);
setSupressedState(otherButton, 1);
}
else if (!isSubButtonSupressed(button) && eventConfiguration == MICROBIT_BUTTON_ALL_EVENTS)
{
if (isSubButtonHeld(button))
MicroBitEvent e(button, MICROBIT_BUTTON_EVT_LONG_CLICK);
else
MicroBitEvent e(button, MICROBIT_BUTTON_EVT_CLICK);
}
setButtonState(button, 0);
setHoldState(button, 0);
setSupressedState(button, 0);
break;
}
}
/**
* Tests if this MultiButton is currently pressed.
* @return 1 if both physical buttons are pressed simultaneously.
*/
int MicroBitMultiButton::isPressed()
{
return ((status & MICROBIT_MULTI_BUTTON_STATE_1) && (status & MICROBIT_MULTI_BUTTON_STATE_2));
}