Skip to content

Commit

Permalink
Merge remote-tracking branch 'MICROBIT/js-event-semantics' into js-ev…
Browse files Browse the repository at this point in the history
…ent-semantics
  • Loading branch information
Juri committed Apr 30, 2020
2 parents b366bb2 + 673228f commit 14fd4e0
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 99 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
build
.yotta.json
.vscode
yotta_modules
yotta_targets
*.swp
*~
Makefile
.idea
*.iml
.vscode
30 changes: 13 additions & 17 deletions inc/core/MicroBitComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ und Björn Eberhardt GbR by arrangement with Calliope GbR.
// Enumeration of core components.
#define MICROBIT_ID_BUTTON_A 1
#define MICROBIT_ID_BUTTON_B 2
#define MICROBIT_ID_BUTTON_RESET 3
#define MICROBIT_ID_ACCELEROMETER 4
#define MICROBIT_ID_COMPASS 5
#define MICROBIT_ID_DISPLAY 6
#define MICROBIT_ID_BUTTON_AB 3 // Button A+B multibutton
#define MICROBIT_ID_BUTTON_RESET 4
#define MICROBIT_ID_ACCELEROMETER 5
#define MICROBIT_ID_COMPASS 6
#define MICROBIT_ID_DISPLAY 7
#define MICROBIT_ID_THERMOMETER 8
#define MICROBIT_ID_RADIO 9
#define MICROBIT_ID_RADIO_DATA_READY 10
#define MICROBIT_ID_MULTIBUTTON_ATTACH 11
#define MICROBIT_ID_SERIAL 12
#define MICROBIT_ID_GESTURE 13 // Gesture events

//EDGE connector events
#ifdef TARGET_NRF51_CALLIOPE
Expand Down Expand Up @@ -71,19 +78,8 @@ und Björn Eberhardt GbR by arrangement with Calliope GbR.
#define MICROBIT_ID_IO_P21 50 // CM: analog microphone
#endif

#define MICROBIT_ID_BUTTON_AB 26 // Button A+B multibutton
#define MICROBIT_ID_GESTURE 27 // Gesture events

#define MICROBIT_ID_THERMOMETER 28
#define MICROBIT_ID_RADIO 29
#define MICROBIT_ID_RADIO_DATA_READY 30
#define MICROBIT_ID_MULTIBUTTON_ATTACH 31
#define MICROBIT_ID_SERIAL 32

#define MICROBIT_ID_IO_INT1 33 //INT1
#define MICROBIT_ID_IO_INT2 34 //INT2
#define MICROBIT_ID_IO_INT3 35 //INT3
#define MICROBIT_ID_PARTIAL_FLASHING 36
// System Softwarre components
#define MICROBIT_ID_PARTIAL_FLASHING 200

#define MICROBIT_ID_MESSAGE_BUS_LISTENER 1021 // Message bus indication that a handler for a given ID has been registered.
#define MICROBIT_ID_NOTIFY_ONE 1022 // Notfication channel, for general purpose synchronisation
Expand Down
27 changes: 27 additions & 0 deletions inc/core/MicroBitConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ extern uint32_t __etext;
#define MICROBIT_FIBER_USER_DATA 0
#endif

// Indicate get_fiber_list() API is supported
#ifndef MICROBIT_GET_FIBER_LIST_SUPPORTED
#define MICROBIT_GET_FIBER_LIST_SUPPORTED 1
#endif

// Maximum size of the FiberPool
// Defines the size that the pool of unused Fiber contexts is permitted to grow to. After this point, memory
// from unused Fiber contexts will be restored to the Heap Allocator.
#ifndef MICROBIT_FIBER_MAXIMUM_FIBER_POOL_SIZE
#define MICROBIT_FIBER_MAXIMUM_FIBER_POOL_SIZE 3
#endif

//
// Message Bus:
// Default behaviour for event handlers, if not specified in the listen() call
Expand All @@ -176,6 +188,21 @@ extern uint32_t __etext;
#define MESSAGE_BUS_LISTENER_MAX_QUEUE_DEPTH 10
#endif

//
// Define MESSAGE_BUS concurrency behaviour.
// Set to MESSAGE_BUS_CONCURRENT_LISTENERS to fire event handler
// concurrently when a given event is raised, and process events sequentially as they arrive (default micro:bit semantics).
// Set to MESSAGE_BUS_CONCURRENT_EVENTS to to fire event handlers sequentially for any given event, while still allowing
// concurrent processing of events.
//
//
// Permissable values are:
// 0: MESSAGE_BUS_CONCURRENT_LISTENERS
// 1: MESSAGE_BUS_CONCURRENT_EVENTS
//
#ifndef MESSAGE_BUS_CONCURRENCY_MODE
#define MESSAGE_BUS_CONCURRENCY_MODE MESSAGE_BUS_CONCURRENT_LISTENERS
#endif
//
// Core micro:bit services
//
Expand Down
19 changes: 17 additions & 2 deletions inc/core/MicroBitFiber.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ DEALINGS IN THE SOFTWARE.
#define MICROBIT_FIBER_FLAG_CHILD 0x04
#define MICROBIT_FIBER_FLAG_DO_NOT_PAGE 0x08

#if CONFIG_ENABLED(MICROBIT_FIBER_USER_DATA)
#define HAS_THREAD_USER_DATA (currentFiber->user_data != NULL)
#else
#define HAS_THREAD_USER_DATA false
#endif

/**
* Thread Context for an ARM Cortex M0 core.
*
Expand Down Expand Up @@ -88,9 +94,11 @@ struct Fiber
uint32_t context; // Context specific information.
uint32_t flags; // Information about this fiber.
Fiber **queue; // The queue this fiber is stored on.
Fiber *next, *prev; // Position of this Fiber on the run queue.
Fiber *qnext; // Position of this Fiber on its queue.
Fiber *next; // Position of this Fiber in the global list of fibers.

#if CONFIG_ENABLED(MICROBIT_FIBER_USER_DATA)
void *user_data;
void *user_data; // Optional pointer to user defined data block.
#endif
};

Expand All @@ -114,6 +122,13 @@ void scheduler_init(EventModel &_messageBus);
*/
int fiber_scheduler_running();

/**
* Provides a list of all active fibers.
*
* @return A pointer to the head of the list of all active fibers.
*/
Fiber* get_fiber_list();

/**
* Exit point for all fibers.
*
Expand Down
4 changes: 2 additions & 2 deletions inc/core/MicroBitListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ DEALINGS IN THE SOFTWARE.

#include "mbed.h"
#include "MicroBitConfig.h"
#include "MicroBitLock.h"
#include "MicroBitEvent.h"
#include "MemberFunctionCallback.h"
#include "MicroBitConfig.h"

// MicroBitListener flags...
#define MESSAGE_BUS_LISTENER_PARAMETERISED 0x0001
Expand Down Expand Up @@ -67,7 +67,7 @@ struct MicroBitListener

MicroBitEvent evt;
MicroBitEventQueueItem *evt_queue;

MicroBitLock lock;
MicroBitListener *next;

/**
Expand Down
66 changes: 66 additions & 0 deletions inc/core/MicroBitLock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/**
* A simple lock, mostly used for mutual exclusion.
*/

#ifndef MICROBIT_LOCK_H
#define MICROBIT_LOCK_H

#include "MicroBitConfig.h"

class Fiber;

class MicroBitLock
{
private:
bool locked;
Fiber *queue;

public:

/**
* Create a new lock that can be used for mutual exclusion and condition synchronisation.
*/
MicroBitLock();

/**
* Block the calling fiber until the lock is available
**/
void wait();

/**
* Release the lock, and signal to one waiting fiber to continue
*/
void notify();

/**
* Release the lock, and signal to all waiting fibers to continue
*/
void notifyAll();
};

#endif
44 changes: 41 additions & 3 deletions inc/drivers/MicroBitDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,33 @@ class MicroBitDisplay : public MicroBitComponent
* @param s The string to display.
*
* @param delay The time to delay between characters, in milliseconds. Must be > 0.
* Defaults to: MICROBIT_DEFAULT_PRINT_SPEED.
*
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.printAsync("abc123",400);
* @endcode
*/
int printAsync(ManagedString s, int delay = MICROBIT_DEFAULT_PRINT_SPEED);
int printAsync(ManagedString s, int delay);

/**
* Prints the given ManagedString to the display, one character at a time.
* Returns immediately, and executes the animation asynchronously.
*
* If the string is greater than one charcter in length, the screen
* will be cleared after MICROBIT_DEFAULT_PRINT_SPEED milliseconds.
* Otherwise, that character will be left on the screen indefinitely.
*
* @param s The string to display.
*
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.printAsync("abc123");
* @endcode
*/
int printAsync(ManagedString s);


/**
* Prints the given image to the display, if the display is not in use.
Expand Down Expand Up @@ -352,7 +370,27 @@ class MicroBitDisplay : public MicroBitComponent
* display.print("abc123",400);
* @endcode
*/
int print(ManagedString s, int delay = MICROBIT_DEFAULT_PRINT_SPEED);
int print(ManagedString s, int delay);

/**
* Prints the given string to the display, one character at a time.
*
* Blocks the calling thread until all the text has been displayed.
*
* If the string is greater than one charcter in length, the screen
* will be cleared after MICROBIT_DEFAULT_PRINT_SPEED milliseconds.
* Otherwise, that character will be left on the screen indefinitely.
*
* @param s The string to display.
*
* @return MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.print("abc123");
* @endcode
*/
int print(ManagedString s);


/**
* Prints the given image to the display.
Expand Down
6 changes: 4 additions & 2 deletions inc/drivers/MicroBitMessageBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ DEALINGS IN THE SOFTWARE.
#include "MicroBitListener.h"
#include "EventModel.h"

#define MESSAGE_BUS_CONCURRENT_LISTENERS 0
#define MESSAGE_BUS_CONCURRENT_EVENTS 1

/**
* Class definition for the MicroBitMessageBus.
*
Expand Down Expand Up @@ -105,7 +108,7 @@ class MicroBitMessageBus : public EventModel, public MicroBitComponent
* @note It is recommended that all external code uses the send() function instead of this function,
* or the constructors provided by MicrobitEvent.
*/
int process(MicroBitEvent &evt, bool urgent = false);
int process(MicroBitEvent evt, bool urgent = false);

/**
* Returns the microBitListener with the given position in our list.
Expand Down Expand Up @@ -144,7 +147,6 @@ class MicroBitMessageBus : public EventModel, public MicroBitComponent
MicroBitListener *listeners; // Chain of active listeners.
MicroBitEventQueueItem *evt_queue_head; // Head of queued events to be processed.
MicroBitEventQueueItem *evt_queue_tail; // Tail of queued events to be processed.
uint16_t nonce_val; // The last nonce issued.
uint16_t queueLength; // The number of events currently waiting to be processed.

/**
Expand Down
13 changes: 13 additions & 0 deletions inc/platform/yotta_cfg_mappings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
*/

//DAL mappings

#ifdef YOTTA_CFG_MICROBIT_DAL_FIBER_USER_DATA
#define MICROBIT_FIBER_USER_DATA YOTTA_CFG_MICROBIT_DAL_FIBER_USER_DATA
#endif

#ifdef YOTTA_CFG_MICROBIT_DAL_MAXIMUM_FIBER_POOL_SIZE
#define MICROBIT_FIBER_MAXIMUM_FIBER_POOL_SIZE YOTTA_CFG_MICROBIT_DAL_MAXIMUM_FIBER_POOL_SIZE
#endif

#ifdef YOTTA_CFG_MICROBIT_DAL_HEAP_ALLOCATOR
#define MICROBIT_HEAP_ALLOCATOR YOTTA_CFG_MICROBIT_DAL_HEAP_ALLOCATOR
#endif
Expand Down Expand Up @@ -191,6 +200,10 @@
#define MICROBIT_BLE_DEVICE_INFORMATION_SERVICE YOTTA_CFG_MICROBIT_DAL_BLUETOOTH_DEVICE_INFO_SERVICE
#endif

#ifdef YOTTA_CFG_MICROBIT_DAL_MESSAGE_BUS_CONCURRENCY_MODE
#define MESSAGE_BUS_CONCURRENCY_MODE YOTTA_CFG_MICROBIT_DAL_MESSAGE_BUS_CONCURRENCY_MODE
#endif

#endif

#ifdef YOTTA_CFG_MICROBIT_DAL_ACCELEROMETER
Expand Down
Loading

0 comments on commit 14fd4e0

Please sign in to comment.