microbit: removed whitespace from all files
Commits were polluted by the amount of trailing whitespace there was in files. All whitespace errors should now be fixed.
This commit is contained in:
parent
0fd04e11a4
commit
d58690a8a7
|
@ -11,13 +11,13 @@ enum ErrorCode{
|
|||
MICROBIT_OK = 0,
|
||||
|
||||
// Invalid parameter given.
|
||||
MICROBIT_INVALID_PARAMETER = -1001,
|
||||
MICROBIT_INVALID_PARAMETER = -1001,
|
||||
|
||||
// Requested operation is unspupported.
|
||||
MICROBIT_NOT_SUPPORTED = -1002,
|
||||
MICROBIT_NOT_SUPPORTED = -1002,
|
||||
|
||||
// Device calibration errors
|
||||
MICROBIT_CALIBRATION_IN_PROGRESS = -1003,
|
||||
MICROBIT_CALIBRATION_IN_PROGRESS = -1003,
|
||||
MICROBIT_CALIBRATION_REQUIRED = -1004,
|
||||
|
||||
// The requested operation could not be performed as the device has run out of some essential resource (e.g. allocated memory)
|
||||
|
@ -29,8 +29,8 @@ enum ErrorCode{
|
|||
// The requested operation was cancelled before it completed.
|
||||
MICROBIT_CANCELLED = -1007,
|
||||
|
||||
// I2C Communication error occured (typically I2C module on processor has locked up.)
|
||||
MICROBIT_I2C_ERROR = -1010
|
||||
// I2C Communication error occured (typically I2C module on processor has locked up.)
|
||||
MICROBIT_I2C_ERROR = -1010
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -42,7 +42,7 @@ enum PanicCode{
|
|||
// entered where the panic code is diplayed.
|
||||
|
||||
// Out out memory error. Heap storage was requested, but is not available.
|
||||
MICROBIT_OOM = 20,
|
||||
MICROBIT_OOM = 20,
|
||||
|
||||
// Corruption detected in the micro:bit heap space
|
||||
MICROBIT_HEAP_ERROR = 30,
|
||||
|
|
|
@ -12,14 +12,14 @@
|
|||
* It is common to need to send events from one part of a program (or system) to another.
|
||||
* The way that these events are stored and delivered is known as an Event Model...
|
||||
*
|
||||
* The micro:bit can be programmed in a number of languages, and it not be good to
|
||||
* The micro:bit can be programmed in a number of languages, and it not be good to
|
||||
* constrain those languages to any particular event model (e.g. they may have their own already).
|
||||
*
|
||||
* This class defines the functionality an event model needs to have to be able to interact
|
||||
* This class defines the functionality an event model needs to have to be able to interact
|
||||
* with events generated and/or used by the micro:bit runtime. Programmer may choose to implement
|
||||
* such funcitonality to integrate their own event models.
|
||||
*
|
||||
* This is an example of a key principle in computing - ABSTRACTION. This is now part of the
|
||||
* This is an example of a key principle in computing - ABSTRACTION. This is now part of the
|
||||
* UK's Computing curriculum in schools... so ask your teacher about it. :-)
|
||||
*
|
||||
* An EventModel implementation is provided in the MicroBitMessageBus class.
|
||||
|
@ -31,7 +31,7 @@ class EventModel
|
|||
static EventModel *defaultEventBus;
|
||||
/**
|
||||
* Queues the given event to be sent to all registered recipients.
|
||||
* The method of delivery will vary depending on the
|
||||
* The method of delivery will vary depending on the
|
||||
*
|
||||
* @param The event to send.
|
||||
* @return MICROBIT_OK on success, or any valid error code defined in "ErrNo.h". The default implementation
|
||||
|
@ -100,7 +100,7 @@ class EventModel
|
|||
* Register a listener function.
|
||||
*
|
||||
* An EventModel implementing this interface may optionally choose to override this method,
|
||||
* if that EventModel supports asynchronous callbacks to user code, but there is no
|
||||
* if that EventModel supports asynchronous callbacks to user code, but there is no
|
||||
* requirement to do so.
|
||||
*
|
||||
* @param id The source of messages to listen for. Events sent from any other IDs will be filtered.
|
||||
|
|
|
@ -76,7 +76,7 @@
|
|||
#define MES_DPAD_BUTTON_D_DOWN 7
|
||||
#define MES_DPAD_BUTTON_D_UP 8
|
||||
#define MES_DPAD_BUTTON_1_DOWN 9
|
||||
#define MES_DPAD_BUTTON_1_UP 10
|
||||
#define MES_DPAD_BUTTON_1_UP 10
|
||||
#define MES_DPAD_BUTTON_2_DOWN 11
|
||||
#define MES_DPAD_BUTTON_2_UP 12
|
||||
#define MES_DPAD_BUTTON_3_DOWN 13
|
||||
|
|
|
@ -35,17 +35,17 @@ class ManagedString
|
|||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a specially prepared string literal. It will ptr->incr().
|
||||
*
|
||||
* @param ptr The literal - first two bytes should be 0xff, then the length in little endian, then the literal. The literal has to be 4-byte aligned.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* static const char hello[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "Hello";
|
||||
* ManagedString s((StringData*)(void*)hello);
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString(StringData *ptr);
|
||||
|
||||
/**
|
||||
|
@ -55,49 +55,49 @@ class ManagedString
|
|||
StringData *leakData();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a pointer to an 8-bit character buffer.
|
||||
* The buffer is copied to ensure safe memory management (the supplied
|
||||
* character buffer may be decalred on the stack for instance).
|
||||
*
|
||||
* @param str The character array on which to base the new ManagedString.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcdefg");
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString(const char *str);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a given integer.
|
||||
*
|
||||
* @param value The integer from which to create the ManagedString
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s(20);
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString(const int value);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a given char.
|
||||
*
|
||||
* @param value The char from which to create the ManagedString
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s('a');
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString(const char value);
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a pointer to an 8-bit character buffer of a given length.
|
||||
* The buffer is copied to ensure sane memory management (the supplied
|
||||
* character buffer may be declared on the stack for instance).
|
||||
|
@ -106,50 +106,50 @@ class ManagedString
|
|||
* @param length The length of the character array
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcdefg",7); // this is generally used for substring... why not use a normal char * constructor?
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString(const char *str, const int16_t length);
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
* Makes a new ManagedString identical to the one supplied.
|
||||
* Copy constructor.
|
||||
* Makes a new ManagedString identical to the one supplied.
|
||||
* Shares the character buffer and reference count with the supplied ManagedString.
|
||||
*
|
||||
* @param s The ManagedString to copy.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcdefg");
|
||||
* ManagedString p(s);
|
||||
* @endcode
|
||||
*/
|
||||
ManagedString(const ManagedString &s);
|
||||
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
* Default constructor.
|
||||
*
|
||||
* Create an empty ManagedString.
|
||||
* Create an empty ManagedString.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s();
|
||||
* @endcode
|
||||
*/
|
||||
ManagedString();
|
||||
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destructor.
|
||||
*
|
||||
* Free this ManagedString, and decrement the reference count to the
|
||||
* internal character buffer. If we're holding the last reference,
|
||||
* also free the character buffer.
|
||||
*/
|
||||
~ManagedString();
|
||||
|
||||
|
||||
/**
|
||||
* Copy assign operation.
|
||||
* Copy assign operation.
|
||||
*
|
||||
* Called when one ManagedString is assigned the value of another.
|
||||
* If the ManagedString being assigned is already refering to a character buffer,
|
||||
|
@ -160,14 +160,14 @@ class ManagedString
|
|||
* @param s The ManagedString to copy.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
* ManagedString p("efgh");
|
||||
* p = s // p now points to s, s' ref is incremented
|
||||
* p = s // p now points to s, s' ref is incremented
|
||||
* @endcode
|
||||
*/
|
||||
ManagedString& operator = (const ManagedString& s);
|
||||
|
||||
|
||||
/**
|
||||
* Equality operation.
|
||||
*
|
||||
|
@ -177,10 +177,10 @@ class ManagedString
|
|||
* @return true if this ManagedString is identical to the one supplied, false otherwise.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
* ManagedString p("efgh");
|
||||
*
|
||||
*
|
||||
* if(p==s)
|
||||
* print("We are the same!");
|
||||
* else
|
||||
|
@ -198,14 +198,14 @@ class ManagedString
|
|||
* @return true if this ManagedString is alphabetically less than to the one supplied, false otherwise.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("a");
|
||||
* ManagedString p("b");
|
||||
*
|
||||
*
|
||||
* if(s<p)
|
||||
* print("a is before b!"); //a is before b
|
||||
* else
|
||||
* print("b is before a!");
|
||||
* print("b is before a!");
|
||||
* @endcode
|
||||
*/
|
||||
bool operator< (const ManagedString& s);
|
||||
|
@ -219,14 +219,14 @@ class ManagedString
|
|||
* @return true if this ManagedString is alphabetically greater than to the one supplied, false otherwise.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("a");
|
||||
* ManagedString p("b");
|
||||
*
|
||||
*
|
||||
* if(p>a)
|
||||
* print("b is after a!"); //b is after a
|
||||
* else
|
||||
* print("a is after b!");
|
||||
* print("a is after b!");
|
||||
* @endcode
|
||||
*/
|
||||
bool operator> (const ManagedString& s);
|
||||
|
@ -239,14 +239,14 @@ class ManagedString
|
|||
* @return a ManagedString representing the requested substring.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcdefg");
|
||||
*
|
||||
* print(s.substring(0,2)) // prints "ab"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString substring(int16_t start, int16_t length);
|
||||
|
||||
|
||||
/**
|
||||
* Concatenates this string with the one provided.
|
||||
*
|
||||
|
@ -254,13 +254,13 @@ class ManagedString
|
|||
* @return a new ManagedString representing the joined strings.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
* ManagedString p("efgh")
|
||||
*
|
||||
* print(s + p) // prints "abcdefgh"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString operator+ (ManagedString& s);
|
||||
|
||||
/**
|
||||
|
@ -270,12 +270,12 @@ class ManagedString
|
|||
* @return the character at posisiton index, zero if index is invalid.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
*
|
||||
* print(s.charAt(1)) // prints "b"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
char charAt(int16_t index);
|
||||
|
||||
|
||||
|
@ -283,24 +283,24 @@ class ManagedString
|
|||
* Provides an immutable 8 bit wide character buffer representing this string.
|
||||
*
|
||||
* @return a pointer to the character buffer.
|
||||
*/
|
||||
*/
|
||||
const char *toCharArray() const
|
||||
{
|
||||
return ptr->data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines the length of this ManagedString in characters.
|
||||
*
|
||||
* @return the length of the string in characters.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
*
|
||||
* print(s.length()) // prints "4"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
int16_t length() const
|
||||
{
|
||||
return ptr->len;
|
||||
|
@ -308,7 +308,7 @@ class ManagedString
|
|||
|
||||
/**
|
||||
* Empty String constant
|
||||
*/
|
||||
*/
|
||||
static ManagedString EmptyString;
|
||||
|
||||
private:
|
||||
|
@ -326,14 +326,14 @@ class ManagedString
|
|||
void initString(const char *str);
|
||||
|
||||
/**
|
||||
* Private Constructor.
|
||||
* Private Constructor.
|
||||
* Create a managed string based on a concat of two strings.
|
||||
* The buffer is copied to ensure sane memory management (the supplied
|
||||
* character buffer may be decalred on the stack for instance).
|
||||
*
|
||||
* @param str1 The first string on which to base the new ManagedString
|
||||
* @param str2 The second string on which to base the new ManagedString
|
||||
*/
|
||||
*/
|
||||
ManagedString(const ManagedString &s1, const ManagedString &s2);
|
||||
|
||||
};
|
||||
|
|
|
@ -24,13 +24,13 @@ public:
|
|||
* @param object the object that you would like to be ref counted - of class T
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* T object = new T();
|
||||
* ManagedType<T> mt(t);
|
||||
* @endcode
|
||||
*/
|
||||
ManagedType(T* object);
|
||||
|
||||
|
||||
/**
|
||||
* Default constructor for the managed type, given a class space T.
|
||||
*/
|
||||
|
@ -41,7 +41,7 @@ public:
|
|||
* @param t another managed type instance of class type T.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* T* object = new T();
|
||||
* ManagedType<T> mt(t);
|
||||
* ManagedType<T> mt1(mt);
|
||||
|
@ -58,19 +58,19 @@ public:
|
|||
* Copy-assign member function for the managed type, given a class space.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* T* object = new T();
|
||||
* ManagedType<T> mt(t);
|
||||
* ManagedType<T> mt1 = mt;
|
||||
* @endcode
|
||||
*/
|
||||
ManagedType<T>& operator = (const ManagedType<T>&i);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the references to this ManagedType
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* T* object = new T();
|
||||
* ManagedType<T> mt(t);
|
||||
* ManagedType<T> mt1(mt);
|
||||
|
@ -170,7 +170,7 @@ ManagedType<T>::~ManagedType()
|
|||
free(ref);
|
||||
}
|
||||
|
||||
// Normal case - we have a valid piece of data.
|
||||
// Normal case - we have a valid piece of data.
|
||||
// Decrement our reference counter and free all allocated memory if we're deleting the last reference.
|
||||
else if (--(*ref) == 0)
|
||||
{
|
||||
|
@ -215,6 +215,6 @@ ManagedType<T>& ManagedType<T>::operator = (const ManagedType<T>&t)
|
|||
template<typename T>
|
||||
int ManagedType<T>::getReferences()
|
||||
{
|
||||
return (*ref);
|
||||
return (*ref);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -45,7 +45,7 @@ class MemberFunctionCallback
|
|||
};
|
||||
|
||||
/**
|
||||
* Constructor. Creates a representation of a pointer to a C++ member function (method).
|
||||
* Constructor. Creates a representation of a pointer to a C++ member function (method).
|
||||
* @param object The object the callback method should be invooked on.
|
||||
* @param method The method to invoke.
|
||||
*/
|
||||
|
|
|
@ -14,16 +14,16 @@ extern const uint8_t MicroBitAccelerometerServicePeriodUUID[];
|
|||
* Provides access to live accelerometer data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitAccelerometerService
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the AccelerometerService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitAccelerometerService(BLEDevice &_ble, MicroBitAccelerometer &_acclerometer, MicroBitMessageBus &messageBus);
|
||||
|
||||
MicroBitAccelerometerService(BLEDevice &_ble, MicroBitAccelerometer &_acclerometer, MicroBitMessageBus &messageBus);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include "mbed.h"
|
||||
|
||||
/*
|
||||
/*
|
||||
* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
|
||||
* If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
|
||||
* The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
|
||||
|
@ -15,7 +15,7 @@
|
|||
#endif
|
||||
#include "ble/BLE.h"
|
||||
|
||||
/*
|
||||
/*
|
||||
* Return to our predefined compiler settings.
|
||||
*/
|
||||
#if !defined (__arm)
|
||||
|
@ -54,25 +54,25 @@ class MicroBitBLEManager : MicroBitComponent
|
|||
|
||||
// The mbed abstraction of the BlueTooth Low Energy (BLE) hardware
|
||||
BLEDevice *ble;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
*
|
||||
* Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
|
||||
* Note that the BLE stack *cannot* be brought up in a static context.
|
||||
* (the software simply hangs or corrupts itself).
|
||||
* Hence, we bring it up in an explicit init() method, rather than in the constructor.
|
||||
*/
|
||||
MicroBitBLEManager();
|
||||
MicroBitBLEManager();
|
||||
|
||||
/**
|
||||
* Post constructor initialisation method.
|
||||
* After *MUCH* pain, it's noted that the BLE stack can't be brought up in a
|
||||
* After *MUCH* pain, it's noted that the BLE stack can't be brought up in a
|
||||
* static context, so we bring it up here rather than in the constructor.
|
||||
* n.b. This method *must* be called in main() or later, not before.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.init();
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -81,12 +81,12 @@ class MicroBitBLEManager : MicroBitComponent
|
|||
/**
|
||||
* Change the output power level of the transmitter to the given value.
|
||||
*
|
||||
* @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
|
||||
* @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
|
||||
*
|
||||
*/
|
||||
int setTransmitPower(int power);
|
||||
|
||||
|
||||
/**
|
||||
* Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming
|
||||
* of the micro:bit in cases where BLE is disabled during normal operation.
|
||||
|
@ -98,7 +98,7 @@ class MicroBitBLEManager : MicroBitComponent
|
|||
|
||||
/**
|
||||
* Makes the micro:bit discoverable via BLE, such that bonded devices can connect
|
||||
* When called, the micro:bit will begin advertising for a predefined period,
|
||||
* When called, the micro:bit will begin advertising for a predefined period,
|
||||
* (MICROBIT_BLE_ADVERTISING_TIMEOUT seconds) thereby allowing bonded devices to connect.
|
||||
*/
|
||||
void advertise();
|
||||
|
|
|
@ -100,10 +100,10 @@ class MicroBitButton : public MicroBitComponent
|
|||
* @code
|
||||
*
|
||||
* // Configure a button to generate all possible events.
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
|
||||
*
|
||||
* // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events.
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
|
||||
*
|
||||
* @endcode
|
||||
*/
|
||||
|
|
|
@ -14,16 +14,16 @@ extern const uint8_t MicroBitButtonBServiceDataUUID[];
|
|||
* Provides access to live button data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitButtonService
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the ButtonService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitButtonService(BLEDevice &_ble, MicroBitMessageBus &messageBus);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* Compatibility / portability funcitons and constants for the MicroBit DAL.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef MICROBIT_COMPAT_H
|
||||
#define MICROBIT_COMPAT_H
|
||||
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
/**
|
||||
* Co-ordinate systems that can be used.
|
||||
* RAW: Unaltered data. Data will be returned directly from the accelerometer.
|
||||
*
|
||||
*
|
||||
* SIMPLE_CARTESIAN: Data will be returned based on an easy to understand alignment, consistent with the cartesian system taught in schools.
|
||||
* When held upright, facing the user:
|
||||
*
|
||||
*
|
||||
* /
|
||||
* +--------------------+ z
|
||||
* | |
|
||||
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* NORTH_EAST_DOWN: Data will be returned based on the industry convention of the North East Down (NED) system.
|
||||
* When held upright, facing the user:
|
||||
*
|
||||
*
|
||||
* z
|
||||
* +--------------------+ /
|
||||
* | |
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
// MicroBit ControlPoint OpCodes
|
||||
|
||||
// Requests transfer to the Nordic DFU bootloader.
|
||||
// This will only occur if
|
||||
#define MICROBIT_DFU_OPCODE_START_DFU 1
|
||||
// This will only occur if
|
||||
#define MICROBIT_DFU_OPCODE_START_DFU 1
|
||||
#define MICROBIT_DFU_OPCODE_START_PAIR 2
|
||||
|
||||
#define MICROBIT_DFU_HISTOGRAM_WIDTH 5
|
||||
|
@ -16,7 +16,7 @@
|
|||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitDFUServiceUUID[];
|
||||
extern const uint8_t MicroBitDFUServiceControlCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitDFUServiceFlashCodeCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitDFUServiceFlashCodeCharacteristicUUID[];
|
||||
|
||||
// Handle on the memory resident Nordic bootloader.
|
||||
extern "C" void bootloader_start(void);
|
||||
|
@ -26,33 +26,33 @@ extern "C" void bootloader_start(void);
|
|||
*
|
||||
* This is actually just a frontend to a memory resident nordic DFU loader.
|
||||
* Here we deal with the MicroBit 'pairing' functionality with BLE devices, and
|
||||
* very basic authentication and authorization.
|
||||
* very basic authentication and authorization.
|
||||
*
|
||||
* This implementation is not intended to be fully secure, but rather intends to:
|
||||
*
|
||||
* 1. Provide a simple mechanism to identify an individual MicroBit amongst a classroom of others
|
||||
* 2. Allow BLE devices to discover and cache a passcode that can be used to flash the device over BLE.
|
||||
* 3. Provide an escape route for programs that 'brick' the MicroBit.
|
||||
* 3. Provide an escape route for programs that 'brick' the MicroBit.
|
||||
*
|
||||
* Represents the device as a whole, and includes member variables to that reflect the components of the system.
|
||||
*/
|
||||
class MicroBitDFUService
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of a MicroBit device.
|
||||
* @param messageBus callback function to receive MicroBitMessageBus events.
|
||||
*/
|
||||
MicroBitDFUService(BLEDevice &BLE);
|
||||
|
||||
MicroBitDFUService(BLEDevice &BLE);
|
||||
|
||||
/**
|
||||
* Begin the pairing process. Typically called when device is powered up with buttons held down.
|
||||
* Scroll a description on the display, then displays the device ID code as a histogram on the matrix display.
|
||||
*/
|
||||
void pair();
|
||||
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
|
@ -63,7 +63,7 @@ class MicroBitDFUService
|
|||
// State of paiting process.
|
||||
bool authenticated;
|
||||
bool flashCodeRequested;
|
||||
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
BLEDevice &ble;
|
||||
|
||||
|
@ -78,7 +78,7 @@ class MicroBitDFUService
|
|||
|
||||
// Displays the device's ID code as a histogram on the LED matrix display.
|
||||
void showNameHistogram();
|
||||
|
||||
|
||||
// Displays an acknowledgement on the LED matrix display.
|
||||
void showTick();
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
#ifndef MICROBIT_DEVICE_H
|
||||
#define MICROBIT_DEVICE_H
|
||||
/*
|
||||
/*
|
||||
* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
|
||||
* If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
|
||||
* The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
enum MicroBitEventLaunchMode
|
||||
{
|
||||
CREATE_ONLY,
|
||||
CREATE_ONLY,
|
||||
CREATE_AND_FIRE
|
||||
};
|
||||
|
||||
|
@ -21,13 +21,13 @@ class MicroBitEvent
|
|||
|
||||
//These are public at the moment for backwards compatability with old code
|
||||
//will be refactored in the future!
|
||||
|
||||
uint16_t source; // ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
|
||||
|
||||
uint16_t source; // ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
|
||||
uint16_t value; // Component specific code indicating the cause of the event.
|
||||
uint32_t timestamp; // Time at which the event was generated. ms since power on.
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
|
||||
* @param value Component specific code indicating the cause of the event.
|
||||
* @param mode optional definition of how the event should be processed after construction (if at all):
|
||||
|
@ -35,24 +35,24 @@ class MicroBitEvent
|
|||
* CREATE_ONLY: MicroBitEvent is initialised, and no further processing takes place.
|
||||
* CREATE_AND_QUEUE: MicroBitEvent is initialised, and queued on the MicroBitMessageBus.
|
||||
* CREATE_AND_FIRE: MicroBitEvent is initialised, and its event handlers are immediately fired (not suitable for use in interrupts!).
|
||||
*
|
||||
*
|
||||
* Example: Create and launch an event using the default configuration
|
||||
* @code
|
||||
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK);
|
||||
* @code
|
||||
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK);
|
||||
* @endcode
|
||||
*
|
||||
* Example: Create and launch an event and process all registered event handlers immediately.
|
||||
* @code
|
||||
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK,CREATE_AND_FIRE);
|
||||
* @code
|
||||
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK,CREATE_AND_FIRE);
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
MicroBitEvent(uint16_t source, uint16_t value, MicroBitEventLaunchMode mode = MICROBIT_EVENT_DEFAULT_LAUNCH_MODE);
|
||||
|
||||
|
||||
MicroBitEvent(uint16_t source, uint16_t value, MicroBitEventLaunchMode mode = MICROBIT_EVENT_DEFAULT_LAUNCH_MODE);
|
||||
|
||||
/**
|
||||
* Default constructor - initialises all values, and sets timestamp to the current time.
|
||||
*/
|
||||
MicroBitEvent();
|
||||
*/
|
||||
MicroBitEvent();
|
||||
|
||||
/**
|
||||
* Fires the represented event onto the message bus using the default configuration.
|
||||
|
@ -69,7 +69,7 @@ struct MicroBitEventQueueItem
|
|||
MicroBitEventQueueItem *next;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Creates a new MicroBitEventQueueItem.
|
||||
* @param evt The event that is to be queued.
|
||||
*/
|
||||
|
|
|
@ -5,15 +5,15 @@
|
|||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitEventServiceUUID[];
|
||||
extern const uint8_t MicroBitEventServiceMicroBitEventCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitEventServiceMicroBitEventCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitEventServiceClientEventCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitEventServiceMicroBitRequirementsCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitEventServiceMicroBitRequirementsCharacteristicUUID[];
|
||||
extern const uint8_t MicroBitEventServiceClientRequirementsCharacteristicUUID[];
|
||||
|
||||
struct EventServiceEvent
|
||||
{
|
||||
uint16_t type;
|
||||
uint16_t reason;
|
||||
uint16_t reason;
|
||||
};
|
||||
|
||||
|
||||
|
@ -22,27 +22,27 @@ struct EventServiceEvent
|
|||
* Provides a _ble gateway onto the MicroBit Message Bus.
|
||||
*/
|
||||
class MicroBitEventService : public MicroBitComponent
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the EventService
|
||||
* @param BLE The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitEventService(BLEDevice &_ble, MicroBitMessageBus &_messageBus);
|
||||
|
||||
MicroBitEventService(BLEDevice &_ble, MicroBitMessageBus &_messageBus);
|
||||
|
||||
/**
|
||||
* Periodic callback from MicroBit scheduler.
|
||||
* If we're no longer connected, remove any registered Message Bus listeners.
|
||||
*/
|
||||
virtual void idleTick();
|
||||
*/
|
||||
virtual void idleTick();
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void onDataWritten(const GattWriteCallbackParams *params);
|
||||
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any events are sent on the microBit message bus.
|
||||
*/
|
||||
|
@ -51,7 +51,7 @@ class MicroBitEventService : public MicroBitComponent
|
|||
/**
|
||||
* read callback on microBitRequirements characteristic.
|
||||
* Used to iterate through the events that the code on this micro:bit is interested in.
|
||||
*/
|
||||
*/
|
||||
void onRequirementsRead(GattReadAuthCallbackParams *params);
|
||||
|
||||
private:
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* 1) To provide a clean abstraction for application languages to use when building async behaviour (callbacks).
|
||||
* 2) To provide ISR decoupling for Messagebus events generted in an ISR context.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef MICROBIT_FIBER_H
|
||||
#define MICROBIT_FIBER_H
|
||||
|
||||
|
@ -21,17 +21,17 @@
|
|||
#define MICROBIT_SCHEDULER_RUNNING 0x01
|
||||
|
||||
// Fiber Flags
|
||||
#define MICROBIT_FIBER_FLAG_FOB 0x01
|
||||
#define MICROBIT_FIBER_FLAG_PARENT 0x02
|
||||
#define MICROBIT_FIBER_FLAG_CHILD 0x04
|
||||
#define MICROBIT_FIBER_FLAG_FOB 0x01
|
||||
#define MICROBIT_FIBER_FLAG_PARENT 0x02
|
||||
#define MICROBIT_FIBER_FLAG_CHILD 0x04
|
||||
#define MICROBIT_FIBER_FLAG_DO_NOT_PAGE 0x08
|
||||
|
||||
/**
|
||||
* Thread Context for an ARM Cortex M0 core.
|
||||
*
|
||||
*
|
||||
* This is probably overkill, but the ARMCC compiler uses a lot register optimisation
|
||||
* in its calling conventions, so better safe than sorry. :-)
|
||||
*
|
||||
*
|
||||
*/
|
||||
struct Cortex_M0_TCB
|
||||
{
|
||||
|
@ -50,7 +50,7 @@ struct Cortex_M0_TCB
|
|||
uint32_t R12;
|
||||
uint32_t SP;
|
||||
uint32_t LR;
|
||||
uint32_t stack_base;
|
||||
uint32_t stack_base;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -62,7 +62,7 @@ struct Fiber
|
|||
Cortex_M0_TCB tcb; // Thread context when last scheduled out.
|
||||
uint32_t stack_bottom; // The start sddress of this Fiber's stack. Stack is heap allocated, and full descending.
|
||||
uint32_t stack_top; // The end address of this Fiber's stack.
|
||||
uint32_t context; // Context specific information.
|
||||
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 queues.
|
||||
|
@ -72,7 +72,7 @@ extern Fiber *currentFiber;
|
|||
|
||||
|
||||
/**
|
||||
* Initialises the Fiber scheduler.
|
||||
* Initialises the Fiber scheduler.
|
||||
* Creates a Fiber context around the calling thread, and adds it to the run queue as the current thread.
|
||||
*
|
||||
* This function must be called once only from the main thread, and before any other Fiber operation.
|
||||
|
@ -111,7 +111,7 @@ void launch_new_fiber_param(void (*ep)(void *), void (*cp)(void *), void *pm)
|
|||
* Creates a new Fiber, and launches it.
|
||||
*
|
||||
* @param entry_fn The function the new Fiber will begin execution in.
|
||||
* @param completion_fn The function called when the thread completes execution of entry_fn.
|
||||
* @param completion_fn The function called when the thread completes execution of entry_fn.
|
||||
* @return The new Fiber.
|
||||
*/
|
||||
Fiber *create_fiber(void (*entry_fn)(void), void (*completion_fn)(void) = release_fiber);
|
||||
|
@ -122,7 +122,7 @@ Fiber *create_fiber(void (*entry_fn)(void), void (*completion_fn)(void) = releas
|
|||
*
|
||||
* @param entry_fn The function the new Fiber will begin execution in.
|
||||
* @param param an untyped parameter passed into the entry_fn anf completion_fn.
|
||||
* @param completion_fn The function called when the thread completes execution of entry_fn.
|
||||
* @param completion_fn The function called when the thread completes execution of entry_fn.
|
||||
* @return The new Fiber.
|
||||
*/
|
||||
Fiber *create_fiber(void (*entry_fn)(void *), void *param, void (*completion_fn)(void *) = release_fiber);
|
||||
|
@ -137,9 +137,9 @@ void schedule();
|
|||
|
||||
/**
|
||||
* Blocks the calling thread for the given period of time.
|
||||
* The calling thread will be immediatley descheduled, and placed onto a
|
||||
* wait queue until the requested amount of time has elapsed.
|
||||
*
|
||||
* The calling thread will be immediatley descheduled, and placed onto a
|
||||
* wait queue until the requested amount of time has elapsed.
|
||||
*
|
||||
* n.b. the fiber will not be be made runnable until after the elasped time, but there
|
||||
* are no guarantees precisely when the fiber will next be scheduled.
|
||||
*
|
||||
|
@ -149,16 +149,16 @@ void fiber_sleep(unsigned long t);
|
|||
|
||||
/**
|
||||
* Timer callback. Called from interrupt context, once every SYSTEM_TICK_PERIOD_MS milliseconds by default.
|
||||
* Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
|
||||
* Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
|
||||
* and made runnable.
|
||||
*/
|
||||
void scheduler_tick();
|
||||
|
||||
/**
|
||||
* Blocks the calling thread until the specified event is raised.
|
||||
* The calling thread will be immediatley descheduled, and placed onto a
|
||||
* The calling thread will be immediatley descheduled, and placed onto a
|
||||
* wait queue until the requested event is received.
|
||||
*
|
||||
*
|
||||
* n.b. the fiber will not be be made runnable until after the event is raised, but there
|
||||
* are no guarantees precisely when the fiber will next be scheduled.
|
||||
*
|
||||
|
@ -170,30 +170,30 @@ int fiber_wait_for_event(uint16_t id, uint16_t value);
|
|||
|
||||
/**
|
||||
* Executes the given function asynchronously if necessary.
|
||||
*
|
||||
*
|
||||
* Fibers are often used to run event handlers, however many of these event handlers are very simple functions
|
||||
* that complete very quickly, bringing unecessary RAM overhead.
|
||||
*
|
||||
* This function takes a snapshot of the current processor context, then attempts to optimistically call the given function directly.
|
||||
* We only create an additional fiber if that function performs a block operation.
|
||||
* This function takes a snapshot of the current processor context, then attempts to optimistically call the given function directly.
|
||||
* We only create an additional fiber if that function performs a block operation.
|
||||
*
|
||||
* @param entry_fn The function to execute.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
*/
|
||||
int invoke(void (*entry_fn)(void));
|
||||
|
||||
/**
|
||||
* Executes the given function asynchronously if necessary.
|
||||
*
|
||||
* Fibers are often used to run event handlers, however many of these event handlers are very simple functions
|
||||
* that complete very quickly, bringing unecessary RAM. overhead
|
||||
*
|
||||
* This function takes a snapshot of the current fiber context, then attempt to optimistically call the given function directly.
|
||||
* We only create an additional fiber if that function performs a block operation.
|
||||
* Fibers are often used to run event handlers, however many of these event handlers are very simple functions
|
||||
* that complete very quickly, bringing unecessary RAM. overhead
|
||||
*
|
||||
* This function takes a snapshot of the current fiber context, then attempt to optimistically call the given function directly.
|
||||
* We only create an additional fiber if that function performs a block operation.
|
||||
*
|
||||
* @param entry_fn The function to execute.
|
||||
* @param param an untyped parameter passed into the entry_fn and completion_fn.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
*/
|
||||
int invoke(void (*entry_fn)(void *), void *param);
|
||||
|
||||
|
@ -209,8 +209,8 @@ int invoke(void (*entry_fn)(void *), void *param);
|
|||
inline void verify_stack_size(Fiber *f);
|
||||
|
||||
/**
|
||||
* Event callback. Called from the message bus whenever an event is raised.
|
||||
* Checks to determine if any fibers blocked on the wait queue need to be woken up
|
||||
* Event callback. Called from the message bus whenever an event is raised.
|
||||
* Checks to determine if any fibers blocked on the wait queue need to be woken up
|
||||
* and made runnable due to the event.
|
||||
*/
|
||||
void scheduler_event(MicroBitEvent evt);
|
||||
|
@ -222,7 +222,7 @@ void scheduler_event(MicroBitEvent evt);
|
|||
int scheduler_runqueue_empty();
|
||||
|
||||
/**
|
||||
* Utility function to add the currenty running fiber to the given queue.
|
||||
* Utility function to add the currenty running fiber to the given queue.
|
||||
* Perform a simple add at the head, to avoid complexity,
|
||||
* Queues are normally very short, so maintaining a doubly linked, sorted list typically outweighs the cost of
|
||||
* brute force searching.
|
||||
|
@ -233,7 +233,7 @@ int scheduler_runqueue_empty();
|
|||
void queue_fiber(Fiber *f, Fiber **queue);
|
||||
|
||||
/**
|
||||
* Utility function to the given fiber from whichever queue it is currently stored on.
|
||||
* Utility function to the given fiber from whichever queue it is currently stored on.
|
||||
* @param f the fiber to remove.
|
||||
*/
|
||||
void dequeue_fiber(Fiber *f);
|
||||
|
|
|
@ -15,14 +15,14 @@
|
|||
class MicroBitFont
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
static const unsigned char* defaultFont;
|
||||
static MicroBitFont systemFont;
|
||||
|
||||
|
||||
const unsigned char* characters;
|
||||
|
||||
|
||||
int asciiEnd;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Sets the font represented by this font object.
|
||||
|
@ -31,8 +31,8 @@ class MicroBitFont
|
|||
*
|
||||
* @note see main_font_test.cpp in the test folder for an example.
|
||||
*/
|
||||
MicroBitFont(const unsigned char* font, int asciiEnd = MICROBIT_FONT_ASCII_END);
|
||||
|
||||
MicroBitFont(const unsigned char* font, int asciiEnd = MICROBIT_FONT_ASCII_END);
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
* Sets the characters to defaultFont characters and asciiEnd to MICROBIT_FONT_ASCII_END.
|
||||
|
|
|
@ -3,11 +3,11 @@
|
|||
* be designated as heap storage, and is designed to run in a static memory area or inside the standard C
|
||||
* heap for use by the micro:bit runtime. This is required for several reasons:
|
||||
*
|
||||
* 1) It reduces memory fragmentation due to the high churn sometime placed on the heap
|
||||
* 1) It reduces memory fragmentation due to the high churn sometime placed on the heap
|
||||
* by ManagedTypes, fibers and user code. Underlying heap implentations are often have very simplistic
|
||||
* allocation pilicies and suffer from fragmentation in prolonged use - which can cause programs to
|
||||
* stop working after a period of time. The algorithm implemented here is simple, but highly tolerant to
|
||||
* large amounts of churn.
|
||||
* stop working after a period of time. The algorithm implemented here is simple, but highly tolerant to
|
||||
* large amounts of churn.
|
||||
*
|
||||
* 2) It allows us to reuse the 8K of SRAM set aside for SoftDevice as additional heap storage
|
||||
* when BLE is not in use.
|
||||
|
@ -17,7 +17,7 @@
|
|||
* N.B. The need for this should be reviewed in the future, should a different memory allocator be
|
||||
* made availiable in the mbed platform.
|
||||
*
|
||||
* P.S. This is a very simple allocator, therefore not without its weaknesses. Why don't you consider
|
||||
* P.S. This is a very simple allocator, therefore not without its weaknesses. Why don't you consider
|
||||
* what these are, and consider the tradeoffs against simplicity...
|
||||
*
|
||||
* TODO: Consider caching recently freed blocks to improve allocation time.
|
||||
|
@ -27,7 +27,7 @@
|
|||
#define MICROBIT_HEAP_ALLOCTOR_H
|
||||
|
||||
#include "mbed.h"
|
||||
#include <new>
|
||||
#include <new>
|
||||
|
||||
// The number of heap segments created.
|
||||
#define MICROBIT_HEAP_COUNT 2
|
||||
|
@ -55,13 +55,13 @@ void *microbit_malloc(size_t size);
|
|||
|
||||
|
||||
/**
|
||||
* Release a given area of memory from the heap.
|
||||
* Release a given area of memory from the heap.
|
||||
* @param mem The memory area to release.
|
||||
*/
|
||||
void microbit_free(void *mem);
|
||||
|
||||
/*
|
||||
* Wrapper function to ensure we have an explicit handle on the heap allocator provided
|
||||
* Wrapper function to ensure we have an explicit handle on the heap allocator provided
|
||||
* by our underlying platform.
|
||||
*
|
||||
* @param size The amount of memory to allocate.
|
||||
|
@ -73,7 +73,7 @@ inline void *native_malloc(size_t size)
|
|||
}
|
||||
|
||||
/*
|
||||
* Wrapper function to ensure we have an explicit handle on the heap allocator provided
|
||||
* Wrapper function to ensure we have an explicit handle on the heap allocator provided
|
||||
* by our underlying platform.
|
||||
*
|
||||
* @param p Pointer to the memory to be freed.
|
||||
|
@ -87,7 +87,7 @@ inline void native_free(void *p)
|
|||
* Overrides the 'new' operator globally, and redirects calls to the micro:bit theap allocator.
|
||||
*/
|
||||
inline void* operator new(size_t size) throw(std::bad_alloc)
|
||||
{
|
||||
{
|
||||
return microbit_malloc(size);
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ inline void* operator new(size_t size) throw(std::bad_alloc)
|
|||
* Overrides the 'new' operator globally, and redirects calls to the micro:bit theap allocator.
|
||||
*/
|
||||
inline void* operator new[](size_t size) throw(std::bad_alloc)
|
||||
{
|
||||
{
|
||||
return microbit_malloc(size);
|
||||
}
|
||||
|
||||
|
@ -103,15 +103,15 @@ inline void* operator new[](size_t size) throw(std::bad_alloc)
|
|||
* Overrides the 'delete' operator globally, and redirects calls to the micro:bit theap allocator.
|
||||
*/
|
||||
inline void operator delete(void *ptr) throw()
|
||||
{
|
||||
{
|
||||
microbit_free(ptr);
|
||||
}
|
||||
|
||||
|
||||
// Macros to override overrides the 'malloc' and 'delete' functions globally, and redirects calls
|
||||
// to the micro:bit theap allocator.
|
||||
// Macros to override overrides the 'malloc' and 'delete' functions globally, and redirects calls
|
||||
// to the micro:bit theap allocator.
|
||||
|
||||
#define malloc(X) microbit_malloc( X )
|
||||
#define free(X) microbit_free( X )
|
||||
#define malloc(X) microbit_malloc( X )
|
||||
#define free(X) microbit_free( X )
|
||||
|
||||
#endif
|
||||
|
|
|
@ -10,32 +10,32 @@
|
|||
*
|
||||
* Presents a wrapped mbed call to capture failed I2C operations caused by a known silicon bug in the nrf51822.
|
||||
* Attempts to automatically reset and restart the I2C hardware if this case is detected.
|
||||
*
|
||||
* For reference see PAN56 in:
|
||||
*
|
||||
*
|
||||
* For reference see PAN56 in:
|
||||
*
|
||||
* https://www.nordicsemi.com/eng/nordic/Products/nRF51822/PAN-nRF51822/24634
|
||||
*
|
||||
*
|
||||
* v2.0 through to v2.4
|
||||
*/
|
||||
class MicroBitI2C : public I2C
|
||||
{
|
||||
uint8_t retries;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create an instance of i2c
|
||||
* @param sda the Pin to be used for SDA
|
||||
* @param scl the Pin to be used for SCL
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicroBitI2C i2c(MICROBIT_PIN_SDA, MICROBIT_PIN_SCL);
|
||||
* @endcode
|
||||
* @note this should prevent i2c lockups as well.
|
||||
*/
|
||||
MicroBitI2C(PinName sda, PinName scl);
|
||||
|
||||
|
||||
/**
|
||||
* Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.
|
||||
*
|
||||
|
@ -47,12 +47,12 @@ class MicroBitI2C : public I2C
|
|||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved read failure is detected.
|
||||
*/
|
||||
int read(int address, char *data, int length, bool repeated = false);
|
||||
|
||||
|
||||
/**
|
||||
* Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.
|
||||
*
|
||||
* @address 8-bit I2C slave address [ addr | 0 ]
|
||||
* @data Pointer to the byte-arraycontaining the data to write
|
||||
* @data Pointer to the byte-arraycontaining the data to write
|
||||
* @length Number of bytes to write
|
||||
* @repeated Repeated start, true - don't send stop at end.
|
||||
*
|
||||
|
|
|
@ -11,10 +11,10 @@
|
|||
* This is an object that contains the pins on the edge connector as properties.
|
||||
*/
|
||||
class MicroBitIO
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
MicroBitPin pin[0];
|
||||
|
||||
MicroBitPin pin[0];
|
||||
MicroBitPin P0;
|
||||
MicroBitPin P1;
|
||||
MicroBitPin P2;
|
||||
|
@ -34,9 +34,9 @@ class MicroBitIO
|
|||
MicroBitPin P16;
|
||||
MicroBitPin P19;
|
||||
MicroBitPin P20;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of all given I/O pins on the edge connector
|
||||
*/
|
||||
MicroBitIO(int ID_P0, int ID_P1, int ID_P2,
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
#include "MicroBit.h"
|
||||
|
||||
#define MICROBIT_IO_PIN_SERVICE_PINCOUNT 20
|
||||
#define MICROBIT_IO_PIN_SERVICE_DATA_SIZE 10
|
||||
#define MICROBIT_IO_PIN_SERVICE_PINCOUNT 20
|
||||
#define MICROBIT_IO_PIN_SERVICE_DATA_SIZE 10
|
||||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitIOPinServiceUUID[];
|
||||
|
@ -27,21 +27,21 @@ struct IOData
|
|||
* Provides access to live ioPin data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitIOPinService : public MicroBitComponent
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the IOPinService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io);
|
||||
MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io);
|
||||
|
||||
/**
|
||||
* periodic callback from MicroBit scheduler.
|
||||
* Check if any of the pins we're watching need updating. Apply a BLE NOTIFY if so...
|
||||
*/
|
||||
virtual void idleTick();
|
||||
* Check if any of the pins we're watching need updating. Apply a BLE NOTIFY if so...
|
||||
*/
|
||||
virtual void idleTick();
|
||||
|
||||
private:
|
||||
|
||||
|
@ -53,7 +53,7 @@ class MicroBitIOPinService : public MicroBitComponent
|
|||
/**
|
||||
* Callback. invoked when the BLE data characteristic is read.
|
||||
* reads all the pins marked as inputs, and updates the data stored in the BLE stack.
|
||||
*/
|
||||
*/
|
||||
void onDataRead(GattReadAuthCallbackParams *params);
|
||||
|
||||
/**
|
||||
|
@ -62,7 +62,7 @@ class MicroBitIOPinService : public MicroBitComponent
|
|||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as a digital value, 0 otherwise
|
||||
*/
|
||||
int isDigital(int i);
|
||||
int isDigital(int i);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as an analog pin by the BLE ADPinConfigurationCharacterisitic.
|
||||
|
@ -70,7 +70,7 @@ class MicroBitIOPinService : public MicroBitComponent
|
|||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as a analog value, 0 otherwise
|
||||
*/
|
||||
int isAnalog(int i);
|
||||
int isAnalog(int i);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as an input by the BLE IOPinConfigurationCharacterisitic.
|
||||
|
@ -78,7 +78,7 @@ class MicroBitIOPinService : public MicroBitComponent
|
|||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as an input, 0 otherwise
|
||||
*/
|
||||
int isInput(int i);
|
||||
int isInput(int i);
|
||||
|
||||
/**
|
||||
* Determines if the given pin was configured as output by the BLE IOPinConfigurationCharacterisitic.
|
||||
|
@ -86,7 +86,7 @@ class MicroBitIOPinService : public MicroBitComponent
|
|||
* @param pin the enumeration of the pin to test
|
||||
* @return 1 if this pin is configured as an output, 0 otherwise
|
||||
*/
|
||||
int isOutput(int i);
|
||||
int isOutput(int i);
|
||||
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
|
|
|
@ -20,8 +20,8 @@ struct ImageData : RefCounted
|
|||
class MicroBitImage
|
||||
{
|
||||
ImageData *ptr; // Pointer to payload data
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal constructor which provides sanity checking and initialises class properties.
|
||||
*
|
||||
|
@ -30,12 +30,12 @@ class MicroBitImage
|
|||
* @param bitmap an array of integers that make up an image.
|
||||
*/
|
||||
void init(const int16_t x, const int16_t y, const uint8_t *bitmap);
|
||||
|
||||
|
||||
/**
|
||||
* Internal constructor which defaults to the Empty Image instance variable
|
||||
*/
|
||||
void init_empty();
|
||||
|
||||
|
||||
public:
|
||||
static MicroBitImage EmptyImage; // Shared representation of a null image.
|
||||
|
||||
|
@ -52,24 +52,24 @@ class MicroBitImage
|
|||
{
|
||||
return ptr->data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create an image from a specially prepared constant array, with no copying. Will call ptr->incr().
|
||||
*
|
||||
* @param ptr The literal - first two bytes should be 0xff, then width, 0, height, 0, and the bitmap. Width and height are 16 bit. The literal has to be 4-byte aligned.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* static const uint8_t heart[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 10, 0, 5, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i((ImageData*)(void*)heart);
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitImage(ImageData *ptr);
|
||||
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
* Creates a new reference to the empty MicroBitImage bitmap
|
||||
* Default Constructor.
|
||||
* Creates a new reference to the empty MicroBitImage bitmap
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
|
@ -77,12 +77,12 @@ class MicroBitImage
|
|||
* @endcode
|
||||
*/
|
||||
MicroBitImage();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Copy Constructor.
|
||||
* Copy Constructor.
|
||||
* Add ourselves as a reference to an existing MicroBitImage.
|
||||
*
|
||||
*
|
||||
* @param image The MicroBitImage to reference.
|
||||
*
|
||||
* Example:
|
||||
|
@ -92,11 +92,11 @@ class MicroBitImage
|
|||
* @endcode
|
||||
*/
|
||||
MicroBitImage(const MicroBitImage &image);
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a blank bitmap representation of a given size.
|
||||
*
|
||||
*
|
||||
* @param s A text based representation of the image given whitespace delimited numeric values.
|
||||
*
|
||||
* Example:
|
||||
|
@ -107,13 +107,13 @@ class MicroBitImage
|
|||
explicit MicroBitImage(const char *s);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a blank bitmap representation of a given size.
|
||||
*
|
||||
* @param x the width of the image.
|
||||
* @param y the height of the image.
|
||||
*
|
||||
* Bitmap buffer is linear, with 8 bits per pixel, row by row,
|
||||
* @param x the width of the image.
|
||||
* @param y the height of the image.
|
||||
*
|
||||
* Bitmap buffer is linear, with 8 bits per pixel, row by row,
|
||||
* top to bottom with no word alignment. Stride is therefore the image width in pixels.
|
||||
* in where w and h are width and height respectively, the layout is therefore:
|
||||
*
|
||||
|
@ -123,35 +123,35 @@ class MicroBitImage
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* MicroBitImage i(5,5); // a blank 5x5 image
|
||||
* @endcode
|
||||
* MicroBitImage i(5,5); // a blank 5x5 image
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitImage(const int16_t x, const int16_t y);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a bitmap representation of a given size, based on a given buffer.
|
||||
*
|
||||
*
|
||||
* @param x the width of the image.
|
||||
* @param y the height of the image.
|
||||
* @param bitmap a 2D array representing the image.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* @endcode
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitImage(const int16_t x, const int16_t y, const uint8_t *bitmap);
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destructor.
|
||||
* Removes buffer resources held by the instance.
|
||||
*/
|
||||
~MicroBitImage();
|
||||
|
||||
/**
|
||||
* Copy assign operation.
|
||||
* Copy assign operation.
|
||||
*
|
||||
* Called when one MicroBitImage is assigned the value of another using the '=' operator.
|
||||
* Decrement our reference count and free up the buffer as necessary.
|
||||
|
@ -159,13 +159,13 @@ class MicroBitImage
|
|||
* and increase its reference count.
|
||||
*
|
||||
* @param s The MicroBitImage to reference.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* MicroBitImage i1();
|
||||
* i1 = 1; // i1 now references i
|
||||
* i1 = 1; // i1 now references i
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitImage& operator = (const MicroBitImage& i);
|
||||
|
@ -178,18 +178,18 @@ class MicroBitImage
|
|||
*
|
||||
* @param i The MicroBitImage to test ourselves against.
|
||||
* @return true if this MicroBitImage is identical to the one supplied, false otherwise.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* MicroBitImage i();
|
||||
* MicroBitImage i();
|
||||
* MicroBitImage i1();
|
||||
*
|
||||
* if(i == i1) //will be true
|
||||
* print("true");
|
||||
* print("true");
|
||||
* @endcode
|
||||
*/
|
||||
bool operator== (const MicroBitImage& i);
|
||||
|
||||
|
||||
/**
|
||||
* Clears all pixels in this image
|
||||
*
|
||||
|
@ -217,7 +217,7 @@ class MicroBitImage
|
|||
int setPixelValue(int16_t x , int16_t y, uint8_t value);
|
||||
|
||||
/**
|
||||
* Determines the value of a given pixel.
|
||||
* Determines the value of a given pixel.
|
||||
*
|
||||
* @param x The x co-ordinate of the pixel to read. Must be within the dimensions of the image.
|
||||
* @param y The y co-ordinate of the pixel to read. Must be within the dimensions of the image.
|
||||
|
@ -232,7 +232,7 @@ class MicroBitImage
|
|||
int getPixelValue(int16_t x , int16_t y);
|
||||
|
||||
/**
|
||||
* Replaces the content of this image with that of a given
|
||||
* Replaces the content of this image with that of a given
|
||||
* 2D array representing the image.
|
||||
* Origin is in the top left corner of the image.
|
||||
*
|
||||
|
@ -240,35 +240,35 @@ class MicroBitImage
|
|||
* @param y the width of the image. Must be within the dimensions of the image.
|
||||
* @param bitmap a 2D array representing the image.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i();
|
||||
* i.printImage(0,0,heart);
|
||||
* MicroBitImage i();
|
||||
* i.printImage(0,0,heart);
|
||||
* @endcode
|
||||
*/
|
||||
int printImage(int16_t x, int16_t y, const uint8_t *bitmap);
|
||||
|
||||
|
||||
/**
|
||||
* Pastes a given bitmap at the given co-ordinates.
|
||||
* Any pixels in the relvant area of this image are replaced.
|
||||
*
|
||||
*
|
||||
* @param image The MicroBitImage to paste.
|
||||
* @param x The leftmost X co-ordinate in this image where the given image should be pasted.
|
||||
* @param y The uppermost Y co-ordinate in this image where the given image should be pasted.
|
||||
* @param alpha set to 1 if transparency clear pixels in given image should be treated as transparent. Set to 0 otherwise.
|
||||
* @return The number of pixels written, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart); //if you show this image - you will see a big heart
|
||||
* i.paste(-5,0,i); //displays a small heart :)
|
||||
* i.paste(-5,0,i); //displays a small heart :)
|
||||
* @endcode
|
||||
*/
|
||||
int paste(const MicroBitImage &image, int16_t x, int16_t y, uint8_t alpha);
|
||||
|
||||
|
||||
/**
|
||||
* Prints a character to the display at the given location
|
||||
*
|
||||
|
@ -276,26 +276,26 @@ class MicroBitImage
|
|||
* @param x The x co-ordinate of on the image to place the top left of the character
|
||||
* @param y The y co-ordinate of on the image to place the top left of the character
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* MicroBitImage i(5,5);
|
||||
* MicroBitImage i(5,5);
|
||||
* i.print('a',0,0);
|
||||
* @endcode
|
||||
*/
|
||||
int print(char c, int16_t x, int16_t y);
|
||||
|
||||
|
||||
/**
|
||||
* Shifts the pixels in this Image a given number of pixels to the Left.
|
||||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart); //if you show this image - you will see a big heart
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* @endcode
|
||||
*/
|
||||
int shiftLeft(int16_t n);
|
||||
|
@ -305,23 +305,23 @@ class MicroBitImage
|
|||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* i.shiftRight(5); //displays a big heart :)
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* i.shiftRight(5); //displays a big heart :)
|
||||
* @endcode
|
||||
*/
|
||||
int shiftRight(int16_t n);
|
||||
|
||||
|
||||
/**
|
||||
* Shifts the pixels in this Image a given number of pixels to Upward.
|
||||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
@ -330,13 +330,13 @@ class MicroBitImage
|
|||
* @endcode
|
||||
*/
|
||||
int shiftUp(int16_t n);
|
||||
|
||||
|
||||
/**
|
||||
* Shifts the pixels in this Image a given number of pixels to Downward.
|
||||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
@ -350,7 +350,7 @@ class MicroBitImage
|
|||
* Gets the width of this image.
|
||||
*
|
||||
* @return The width of this image.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
@ -367,7 +367,7 @@ class MicroBitImage
|
|||
* Gets the height of this image.
|
||||
*
|
||||
* @return The height of this image.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
@ -379,12 +379,12 @@ class MicroBitImage
|
|||
{
|
||||
return ptr->height;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets number of bytes in the bitmap, ie., width * height.
|
||||
*
|
||||
* @return The size of the bitmap.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
|
||||
// UUIDs for our service and characteristics
|
||||
extern const uint8_t MicroBitLEDServiceUUID[];
|
||||
extern const uint8_t MicroBitLEDServiceMatrixUUID[];
|
||||
extern const uint8_t MicroBitLEDServiceTextUUID[];
|
||||
extern const uint8_t MicroBitLEDServiceScrollingSpeedUUID[];
|
||||
extern const uint8_t MicroBitLEDServiceMatrixUUID[];
|
||||
extern const uint8_t MicroBitLEDServiceTextUUID[];
|
||||
extern const uint8_t MicroBitLEDServiceScrollingSpeedUUID[];
|
||||
|
||||
|
||||
/**
|
||||
|
@ -15,17 +15,17 @@ extern const uint8_t MicroBitLEDServiceScrollingSpeedUUID[];
|
|||
* Provides a _ble gateway onto the MicroBit Message Bus.
|
||||
*/
|
||||
class MicroBitLEDService
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the LEDService
|
||||
* @param BLE The instance of a BLE device that we're running on.
|
||||
* @param display The instance of a MicroBitDisplay to interface with.
|
||||
*/
|
||||
MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_display);
|
||||
|
||||
MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_display);
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
|
@ -35,7 +35,7 @@ class MicroBitLEDService
|
|||
* Callback. Invoked when any of our attributes are read via BLE.
|
||||
*/
|
||||
void onDataRead(GattReadAuthCallbackParams *params);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// Bluetooth stack we're running on.
|
||||
|
|
|
@ -21,48 +21,48 @@
|
|||
|
||||
struct MicroBitListener
|
||||
{
|
||||
uint16_t id; // The ID of the component that this listener is interested in.
|
||||
uint16_t value; // Value this listener is interested in receiving.
|
||||
uint16_t id; // The ID of the component that this listener is interested in.
|
||||
uint16_t value; // Value this listener is interested in receiving.
|
||||
uint16_t flags; // Status and configuration options codes for this listener.
|
||||
|
||||
union
|
||||
union
|
||||
{
|
||||
void (*cb)(MicroBitEvent);
|
||||
void (*cb_param)(MicroBitEvent, void *);
|
||||
MemberFunctionCallback *cb_method;
|
||||
};
|
||||
|
||||
void* cb_arg; // Optional argument to be passed to the caller.
|
||||
void* cb_arg; // Optional argument to be passed to the caller.
|
||||
|
||||
MicroBitEvent evt;
|
||||
MicroBitEventQueueItem *evt_queue;
|
||||
|
||||
|
||||
MicroBitListener *next;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a new Message Bus Listener.
|
||||
* @param id The ID of the component you want to listen to.
|
||||
* @param value The event ID you would like to listen to from that component
|
||||
* @param handler A function pointer to call when the event is detected.
|
||||
*/
|
||||
MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent), uint16_t flags = EVENT_LISTENER_DEFAULT_FLAGS);
|
||||
|
||||
|
||||
/**
|
||||
* Alternative constructor where we register a value to be passed to the
|
||||
* callback.
|
||||
* callback.
|
||||
*/
|
||||
MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent, void *), void* arg, uint16_t flags = EVENT_LISTENER_DEFAULT_FLAGS);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a new Message Bus Listener, with a callback to a c++ member function.
|
||||
* @param id The ID of the component you want to listen to.
|
||||
* @param value The event ID you would like to listen to from that component.
|
||||
* @param object The C++ object on which to call the event handler.
|
||||
* @param object The method within the C++ object to call.
|
||||
*/
|
||||
template <typename T>
|
||||
template <typename T>
|
||||
MicroBitListener(uint16_t id, uint16_t value, T* object, void (T::*method)(MicroBitEvent), uint16_t flags = EVENT_LISTENER_DEFAULT_FLAGS);
|
||||
|
||||
/**
|
||||
|
@ -78,7 +78,7 @@ struct MicroBitListener
|
|||
};
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a new Message Bus Listener, with a callback to a c++ member function.
|
||||
* @param id The ID of the component you want to listen to.
|
||||
* @param value The event ID you would like to listen to from that component.
|
||||
|
|
|
@ -15,16 +15,16 @@ extern const uint8_t MicroBitMagnetometerServicePeriodUUID[];
|
|||
* Provides access to live magnetometer data via BLE, and provides basic configuration options.
|
||||
*/
|
||||
class MicroBitMagnetometerService
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the MagnetometerService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitMagnetometerService(BLEDevice &_ble, MicroBitCompass &_compass, MicroBitMessageBus &messageBus);
|
||||
|
||||
MicroBitMagnetometerService(BLEDevice &_ble, MicroBitCompass &_compass, MicroBitMessageBus &messageBus);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
@ -41,7 +41,7 @@ class MicroBitMagnetometerService
|
|||
/**
|
||||
* Sample Period Change Needed callback.
|
||||
* Reconfiguring the magnetometer can to a REALLY long time (sometimes even seconds to complete)
|
||||
* So we do this in the background when necessary, through this event handler.
|
||||
* So we do this in the background when necessary, through this event handler.
|
||||
*/
|
||||
void samplePeriodUpdateNeeded(MicroBitEvent e);
|
||||
|
||||
|
|
|
@ -9,14 +9,14 @@
|
|||
#define MICROBIT_MULTI_BUTTON_STATE_2 0x02
|
||||
#define MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1 0x04
|
||||
#define MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2 0x08
|
||||
#define MICROBIT_MULTI_BUTTON_SUPRESSED_1 0X10
|
||||
#define MICROBIT_MULTI_BUTTON_SUPRESSED_1 0X10
|
||||
#define MICROBIT_MULTI_BUTTON_SUPRESSED_2 0x20
|
||||
#define MICROBIT_MULTI_BUTTON_ATTACHED 0x40
|
||||
|
||||
/**
|
||||
* Class definition for MicroBitMultiButton.
|
||||
*
|
||||
* Represents a virtual button, capable of reacting to simultaneous presses of multiple
|
||||
* Represents a virtual button, capable of reacting to simultaneous presses of multiple
|
||||
* other buttons.
|
||||
*/
|
||||
class MicroBitMultiButton : public MicroBitComponent
|
||||
|
@ -36,7 +36,7 @@ class MicroBitMultiButton : public MicroBitComponent
|
|||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of a vurtual button, that generates events based upon the combination
|
||||
* of two given buttons.
|
||||
* @param id the ID of the new MultiButton object.
|
||||
|
@ -45,12 +45,12 @@ class MicroBitMultiButton : public MicroBitComponent
|
|||
* @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);
|
||||
* @code
|
||||
* multiButton(MICROBIT_ID_BUTTON_AB, MICROBIT_ID_BUTTON_A, MICROBIT_ID_BUTTON_B);
|
||||
* @endcode
|
||||
*
|
||||
* Possible Events:
|
||||
* @code
|
||||
* @code
|
||||
* MICROBIT_BUTTON_EVT_DOWN
|
||||
* MICROBIT_BUTTON_EVT_UP
|
||||
* MICROBIT_BUTTON_EVT_CLICK
|
||||
|
@ -58,15 +58,15 @@ class MicroBitMultiButton : public MicroBitComponent
|
|||
* MICROBIT_BUTTON_EVT_DOUBLE_CLICK
|
||||
* MICROBIT_BUTTON_EVT_HOLD
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitMultiButton(uint16_t id, uint16_t button1, uint16_t button2, MicroBitMessageBus &messageBus);
|
||||
*/
|
||||
MicroBitMultiButton(uint16_t id, uint16_t button1, uint16_t button2, MicroBitMessageBus &messageBus);
|
||||
|
||||
/**
|
||||
* Tests if this MultiButton is currently pressed.
|
||||
* @return 1 if both physical buttons are pressed simultaneously.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* if(uBit.buttonAB.isPressed())
|
||||
* print("Pressed!");
|
||||
* @endcode
|
||||
|
@ -83,10 +83,10 @@ class MicroBitMultiButton : public MicroBitComponent
|
|||
* @code
|
||||
*
|
||||
* // Configure a button to generate all possible events.
|
||||
* uBit.buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
|
||||
* 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);
|
||||
* uBit.buttonAB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
|
||||
*
|
||||
* @endcode
|
||||
*
|
||||
|
@ -101,7 +101,7 @@ class MicroBitMultiButton : public MicroBitComponent
|
|||
*/
|
||||
void setEventConfiguration(MicroBitButtonEventConfiguration config);
|
||||
|
||||
private:
|
||||
private:
|
||||
void onButtonEvent(MicroBitEvent evt);
|
||||
};
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
* For serious applications, BLE should be considered a substantially more secure alternative.
|
||||
*/
|
||||
|
||||
class MicroBitRadioDatagram
|
||||
class MicroBitRadioDatagram
|
||||
{
|
||||
MicroBitRadio &radio; // The underlying radio module used to send and receive data.
|
||||
FrameBuffer *rxQueue; // A linear list of incoming packets, queued awaiting processing.
|
||||
|
@ -32,7 +32,7 @@ class MicroBitRadioDatagram
|
|||
|
||||
/**
|
||||
* Retreives packet payload data into the given buffer.
|
||||
* If a data packet is already available, then it will be returned immediately to the caller.
|
||||
* If a data packet is already available, then it will be returned immediately to the caller.
|
||||
* If no data is available the EmptyString is returned, then MICROBIT_INVALID_PARAMETER is returned.
|
||||
*
|
||||
* @param buf A pointer to a valid memory location where the received data is to be stored.
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
* For serious applications, BLE should be considered a substantially more secure alternative.
|
||||
*/
|
||||
|
||||
class MicroBitRadioEvent
|
||||
class MicroBitRadioEvent
|
||||
{
|
||||
bool suppressForwarding; // A private flag used to prevent event forwarding loops.
|
||||
MicroBitRadio &radio; // A reference to the underlying radio module to use.
|
||||
|
@ -31,7 +31,7 @@ class MicroBitRadioEvent
|
|||
|
||||
/**
|
||||
* Associates the given MessageBus events with the radio channel.
|
||||
* Once registered, all events matching the given registration sent to this micro:bit's
|
||||
* Once registered, all events matching the given registration sent to this micro:bit's
|
||||
* default MessageBus will be automaticlaly retrasmitted on the radio.
|
||||
*
|
||||
* @param id The ID of the events to register.
|
||||
|
@ -43,7 +43,7 @@ class MicroBitRadioEvent
|
|||
|
||||
/**
|
||||
* Associates the given MessageBus events with the radio channel.
|
||||
* Once registered, all events matching the given registration sent to the given
|
||||
* Once registered, all events matching the given registration sent to the given
|
||||
* MessageBus will be automaticlaly retrasmitted on the radio.
|
||||
*
|
||||
* @param id The ID of the events to register.
|
||||
|
@ -77,7 +77,7 @@ class MicroBitRadioEvent
|
|||
|
||||
/**
|
||||
* Protocol handler callback. This is called when the radio receives a packet marked as using the event protocol.
|
||||
* This function process this packet, and fires the event contained inside onto the local MessageBus.
|
||||
* This function process this packet, and fires the event contained inside onto the local MessageBus.
|
||||
*/
|
||||
void packetReceived();
|
||||
|
||||
|
|
|
@ -18,54 +18,54 @@
|
|||
class MicroBitSerial : public Serial
|
||||
{
|
||||
ssize_t readChars(void* buffer, size_t length, char eof = MICROBIT_SERIAL_DEFAULT_EOF);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create an instance of MicroBitSerial
|
||||
* @param sda the Pin to be used for SDA
|
||||
* @param scl the Pin to be used for SCL
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicroBitSerial serial(USBTX, USBRX);
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitSerial(PinName tx, PinName rx);
|
||||
|
||||
|
||||
/**
|
||||
* Sends a managed string over serial.
|
||||
*
|
||||
* @param s the ManagedString to send
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.printString("abc123");
|
||||
* @endcode
|
||||
*/
|
||||
void sendString(ManagedString s);
|
||||
|
||||
|
||||
/**
|
||||
* Reads a ManagedString from serial
|
||||
*
|
||||
* @param len the buffer size for the string, default is defined by MICROBIT_SERIAL_BUFFER_SIZE
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.readString();
|
||||
* @endcode
|
||||
*
|
||||
* @note this member function will wait until either the buffer is full, or a \n is received
|
||||
*/
|
||||
ManagedString readString(int len = MICROBIT_SERIAL_BUFFER_SIZE);
|
||||
|
||||
|
||||
/**
|
||||
* Sends a MicroBitImage over serial in csv format.
|
||||
*
|
||||
* @param i the instance of MicroBitImage you would like to send.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* uBit.serial.sendImage(i);
|
||||
|
@ -80,38 +80,38 @@ class MicroBitSerial : public Serial
|
|||
* @return a MicroBitImage with the format described over serial
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicroBitImage i = uBit.serial.readImage(2,2);
|
||||
* @endcode
|
||||
*
|
||||
* Example Serial Format:
|
||||
* @code
|
||||
* @code
|
||||
* 0,10x0a0,10x0a // 0x0a is a LF terminal which is used as a delimeter
|
||||
* @endcode
|
||||
* @note this will finish once the dimensions are met.
|
||||
*/
|
||||
MicroBitImage readImage(int width, int height);
|
||||
|
||||
|
||||
/**
|
||||
* Sends the current pixel values, byte-per-pixel, over serial
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.sendDisplayState();
|
||||
* @endcode
|
||||
*/
|
||||
void sendDisplayState();
|
||||
|
||||
|
||||
/**
|
||||
* Reads pixel values, byte-per-pixel, from serial, and sets the display.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.readDisplayState();
|
||||
* @endcode
|
||||
*/
|
||||
void readDisplayState();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -32,27 +32,27 @@ class MicroBitStorage
|
|||
|
||||
/*
|
||||
* Default constructor.
|
||||
*/
|
||||
MicroBitStorage();
|
||||
|
||||
*/
|
||||
MicroBitStorage();
|
||||
|
||||
/*
|
||||
* Writes the given number of bytes to the address specified.
|
||||
* @param buffer the data to write.
|
||||
* @param address the location in memory to write to.
|
||||
* @param length the number of bytes to write.
|
||||
* TODO: Write this one!
|
||||
*/
|
||||
*/
|
||||
int writeBytes(uint8_t *buffer, uint32_t address, int length);
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method for erasing a page in flash.
|
||||
* @param page_address Address of the first word in the page to be erased.
|
||||
*/
|
||||
void flashPageErase(uint32_t * page_address);
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method for writing a word of data in flash with a value.
|
||||
|
||||
|
||||
* @param address Address of the word to change.
|
||||
* @param value Value to be written to flash.
|
||||
*/
|
||||
|
@ -60,14 +60,14 @@ class MicroBitStorage
|
|||
|
||||
/*
|
||||
* Reads the micro:bit's configuration data block from FLASH into a RAM buffer.
|
||||
* @return a structure containing the stored data.
|
||||
*/
|
||||
* @return a structure containing the stored data.
|
||||
*/
|
||||
MicroBitConfigurationBlock *getConfigurationBlock();
|
||||
|
||||
/*
|
||||
* Writes the micro:bit's configuration data block from FLASH into a RAM buffer.
|
||||
* @return a structure containing the stored data.
|
||||
*/
|
||||
* @return a structure containing the stored data.
|
||||
*/
|
||||
int setConfigurationBlock(MicroBitConfigurationBlock *block);
|
||||
};
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
* The latter is useful to avoid costs associated with multiple mbed Ticker instances
|
||||
* in microbit-dal components, as each incurs a significant additional RAM overhead (circa 80 bytes).
|
||||
*/
|
||||
|
||||
|
||||
#ifndef MICROBIT_SYSTEM_TIMER_H
|
||||
#define MICROBIT_SYSTEM_TIMER_H
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
|||
#include "MicroBitComponent.h"
|
||||
|
||||
/**
|
||||
* Initialises the system wide timer.
|
||||
* Initialises the system wide timer.
|
||||
* This must be called before any components register to receive periodic periodic callbacks.
|
||||
*
|
||||
* @param timer_period The initial period between interrupts, in millseconds.
|
||||
|
@ -48,7 +48,7 @@ unsigned long system_timer_current_time();
|
|||
|
||||
/**
|
||||
* Timer callback. Called from interrupt context, once per period.
|
||||
* Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
|
||||
* Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
|
||||
* and made runnable.
|
||||
*/
|
||||
void system_timer_tick();
|
||||
|
|
|
@ -14,16 +14,16 @@ extern const uint8_t MicroBitTemperatureServicePeriodUUID[];
|
|||
* Provides access to live temperature data via BLE.
|
||||
*/
|
||||
class MicroBitTemperatureService
|
||||
{
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the TempertureService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitTemperatureService(BLEDevice &_ble, MicroBitThermometer &_thermometer, MicroBitMessageBus &messageBus);
|
||||
|
||||
|
||||
/**
|
||||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#define MICROBIT_THERMOMETER_PERIOD 1000
|
||||
|
||||
|
||||
#define MAG3110_SAMPLE_RATES 11
|
||||
#define MAG3110_SAMPLE_RATES 11
|
||||
|
||||
/*
|
||||
* Temperature events
|
||||
|
@ -25,21 +25,21 @@ class MicroBitThermometer : public MicroBitComponent
|
|||
{
|
||||
unsigned long sampleTime;
|
||||
uint32_t samplePeriod;
|
||||
int16_t temperature;
|
||||
int16_t temperature;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create new object that can sense temperature.
|
||||
* @param id the ID of the new MicroBitThermometer object.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* thermometer(MICROBIT_ID_THERMOMETER);
|
||||
* @code
|
||||
* thermometer(MICROBIT_ID_THERMOMETER);
|
||||
* @endcode
|
||||
*
|
||||
* Possible Events:
|
||||
* @code
|
||||
* @code
|
||||
* MICROBIT_THERMOMETER_EVT_CHANGED
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -48,14 +48,14 @@ class MicroBitThermometer : public MicroBitComponent
|
|||
/**
|
||||
* Set the sample rate at which the temperatureis read (in ms).
|
||||
* n.b. the temperature is alwasy read in the background, so wis only updated
|
||||
* when the processor is idle, or when the temperature is explicitly read.
|
||||
* when the processor is idle, or when the temperature is explicitly read.
|
||||
* The default sample period is 1 second.
|
||||
* @param period the requested time between samples, in milliseconds.
|
||||
*/
|
||||
void setPeriod(int period);
|
||||
|
||||
/**
|
||||
* Reads the currently configured sample rate of the thermometer.
|
||||
* Reads the currently configured sample rate of the thermometer.
|
||||
* @return The time between samples, in milliseconds.
|
||||
*/
|
||||
int getPeriod();
|
||||
|
@ -70,13 +70,13 @@ class MicroBitThermometer : public MicroBitComponent
|
|||
* @endcode
|
||||
*/
|
||||
int getTemperature();
|
||||
|
||||
|
||||
/**
|
||||
* Periodic callback from MicroBit idle thread.
|
||||
* Check if any data is ready for reading by checking the interrupt.
|
||||
*/
|
||||
*/
|
||||
virtual void idleTick();
|
||||
|
||||
|
||||
/**
|
||||
* Indicates if we'd like some processor time to sense the temperature. 0 means we're not due to read the tmeperature yet.
|
||||
* @returns 1 if we'd like some processor time, 0 otherwise.
|
||||
|
@ -96,7 +96,7 @@ class MicroBitThermometer : public MicroBitComponent
|
|||
*/
|
||||
void updateTemperature();
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -19,7 +19,7 @@ struct PacketData : RefCounted
|
|||
class PacketBuffer
|
||||
{
|
||||
PacketData *ptr; // Pointer to payload data
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
|
@ -29,19 +29,19 @@ class PacketBuffer
|
|||
uint8_t *getBytes();
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
* Creates an empty Packet Buffer.
|
||||
* Default Constructor.
|
||||
* Creates an empty Packet Buffer.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p();
|
||||
* PacketBuffer p();
|
||||
* @endcode
|
||||
*/
|
||||
PacketBuffer();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Creates a new PacketBuffer of the given size.
|
||||
* Constructor.
|
||||
* Creates a new PacketBuffer of the given size.
|
||||
*
|
||||
* @param length The length of the buffer to create.
|
||||
*
|
||||
|
@ -53,14 +53,14 @@ class PacketBuffer
|
|||
PacketBuffer(int length);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Creates an empty Packet Buffer of the given size,
|
||||
* and fills it with the data provided.
|
||||
*
|
||||
* @param data The data with which to fill the buffer.
|
||||
* @param length The length of the buffer to create.
|
||||
* @param rssi The radio signal strength at the time this pacer was recieved.
|
||||
*
|
||||
* @param rssi The radio signal strength at the time this pacer was recieved.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* uint8_t buf = {13,5,2};
|
||||
|
@ -70,15 +70,15 @@ class PacketBuffer
|
|||
PacketBuffer(uint8_t *data, int length, int rssi = 0);
|
||||
|
||||
/**
|
||||
* Copy Constructor.
|
||||
* Copy Constructor.
|
||||
* Add ourselves as a reference to an existing PacketBuffer.
|
||||
*
|
||||
*
|
||||
* @param buffer The PacketBuffer to reference.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p();
|
||||
* PacketBuffer p2(i); // Refers to the same packet as p.
|
||||
* PacketBuffer p2(i); // Refers to the same packet as p.
|
||||
* @endcode
|
||||
*/
|
||||
PacketBuffer(const PacketBuffer &buffer);
|
||||
|
@ -88,19 +88,19 @@ class PacketBuffer
|
|||
*
|
||||
* @param data The data with which to fill the buffer.
|
||||
* @param length The length of the buffer to create.
|
||||
* @param rssi The radio signal strength at the time this packet was recieved.
|
||||
*
|
||||
* @param rssi The radio signal strength at the time this packet was recieved.
|
||||
*
|
||||
*/
|
||||
void init(uint8_t *data, int length, int rssi);
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destructor.
|
||||
* Removes buffer resources held by the instance.
|
||||
*/
|
||||
~PacketBuffer();
|
||||
|
||||
/**
|
||||
* Copy assign operation.
|
||||
* Copy assign operation.
|
||||
*
|
||||
* Called when one PacketBuffer is assigned the value of another using the '=' operator.
|
||||
* Decrements our reference count and free up the buffer as necessary.
|
||||
|
@ -108,41 +108,41 @@ class PacketBuffer
|
|||
* and increase its reference count.
|
||||
*
|
||||
* @param p The PacketBuffer to reference.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* uint8_t buf = {13,5,2};
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
*
|
||||
* p1 = p2;
|
||||
* p1 = p2;
|
||||
* @endcode
|
||||
*/
|
||||
PacketBuffer& operator = (const PacketBuffer& p);
|
||||
|
||||
/**
|
||||
* Array access operation (read).
|
||||
* Array access operation (read).
|
||||
*
|
||||
* Called when a PacketBuffer is dereferenced with a [] operation.
|
||||
* Transparently map this through to the underlying payload for elegance of programming.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* uint8_t data = p1[0];
|
||||
* @endcode
|
||||
*/
|
||||
uint8_t operator [] (int i) const;
|
||||
|
||||
/**
|
||||
* Array access operation (modify).
|
||||
* Array access operation (modify).
|
||||
*
|
||||
* Called when a PacketBuffer is dereferenced with a [] operation.
|
||||
* Transparently map this through to the underlying payload for elegance of programming.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1[0] = 42;
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -155,20 +155,20 @@ class PacketBuffer
|
|||
*
|
||||
* @param p The PacketBuffer to test ourselves against.
|
||||
* @return true if this PacketBuffer is identical to the one supplied, false otherwise.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
*
|
||||
* uint8_t buf = {13,5,2};
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
*
|
||||
* if(p1 == p2) // will be true
|
||||
* uBit.display.scroll("same!");
|
||||
* @endcode
|
||||
*/
|
||||
bool operator== (const PacketBuffer& p);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the byte at the given index to value provided.
|
||||
* @param position The index of the byte to change.
|
||||
|
@ -177,7 +177,7 @@ class PacketBuffer
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setByte(0,255); // Sets the first byte in the buffer to the value 255.
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -191,7 +191,7 @@ class PacketBuffer
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setByte(0,255); // Sets the first byte in the buffer to the value 255.
|
||||
* p1.getByte(0); // Returns 255.
|
||||
* @endcode
|
||||
|
@ -199,12 +199,12 @@ class PacketBuffer
|
|||
int getByte(int position);
|
||||
|
||||
/**
|
||||
* Gets number of bytes in this buffer
|
||||
* Gets number of bytes in this buffer
|
||||
* @return The size of the buffer in bytes.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.length(); // Returns 16.
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -214,10 +214,10 @@ class PacketBuffer
|
|||
* Gets the received signal strength of this packet.
|
||||
*
|
||||
* @return The signal strength of the radio when this packet was received, in -dbM.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.getRSSI(); // Returns the received signal strength.
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -228,8 +228,8 @@ class PacketBuffer
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setRSSI(37);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setRSSI(37);
|
||||
* @endcode
|
||||
*/
|
||||
void setRSSI(uint8_t rssi);
|
||||
|
|
|
@ -19,7 +19,7 @@ void ManagedString::initEmpty()
|
|||
* creates this ManagedString based on a given null terminated char array.
|
||||
*/
|
||||
void ManagedString::initString(const char *str)
|
||||
{
|
||||
{
|
||||
// Initialise this ManagedString as a new string, using the data provided.
|
||||
// We assume the string is sane, and null terminated.
|
||||
int len = strlen(str);
|
||||
|
@ -30,17 +30,17 @@ void ManagedString::initString(const char *str)
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a specially prepared string literal. It will ptr->incr().
|
||||
*
|
||||
* @param ptr The literal - first two bytes should be 0xff, then the length in little endian, then the literal. The literal has to be 4-byte aligned.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* static const char hello[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "Hello";
|
||||
* ManagedString s((StringData*)(void*)hello);
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString::ManagedString(StringData *p)
|
||||
{
|
||||
ptr = p;
|
||||
|
@ -59,50 +59,50 @@ StringData* ManagedString::leakData()
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a given integer.
|
||||
*
|
||||
* @param value The integer from which to create the ManagedString
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s(20);
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString::ManagedString(const int value)
|
||||
{
|
||||
char str[12];
|
||||
|
||||
|
||||
itoa(value, str);
|
||||
initString(str);
|
||||
initString(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a given char.
|
||||
*
|
||||
* @param value The char from which to create the ManagedString
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s('a');
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString::ManagedString(const char value)
|
||||
{
|
||||
char str[2] = {value, 0};
|
||||
initString(str);
|
||||
initString(str);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a pointer to an 8-bit character buffer.
|
||||
* The buffer is copied to ensure sane memory management (the supplied
|
||||
* character buffer may be decalred on the stack for instance).
|
||||
*
|
||||
* @param str The character array on which to base the new ManagedString.
|
||||
*/
|
||||
*/
|
||||
ManagedString::ManagedString(const char *str)
|
||||
{
|
||||
// Sanity check. Return EmptyString for anything distasteful
|
||||
|
@ -111,7 +111,7 @@ ManagedString::ManagedString(const char *str)
|
|||
initEmpty();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
initString(str);
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ ManagedString::ManagedString(const ManagedString &s1, const ManagedString &s2)
|
|||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a managed string from a pointer to an 8-bit character buffer of a given length.
|
||||
* The buffer is copied to ensure sane memory management (the supplied
|
||||
* character buffer may be declared on the stack for instance).
|
||||
|
@ -142,10 +142,10 @@ ManagedString::ManagedString(const ManagedString &s1, const ManagedString &s2)
|
|||
* @param length The number of characters to use.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* ManagedString s("abcdefg",7);
|
||||
* @code
|
||||
* ManagedString s("abcdefg",7);
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString::ManagedString(const char *str, const int16_t length)
|
||||
{
|
||||
// Sanity check. Return EmptyString for anything distasteful
|
||||
|
@ -155,7 +155,7 @@ ManagedString::ManagedString(const char *str, const int16_t length)
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Allocate a new buffer, and create a NULL terminated string.
|
||||
ptr = (StringData*) malloc(4+length+1);
|
||||
ptr->init();
|
||||
|
@ -166,14 +166,14 @@ ManagedString::ManagedString(const char *str, const int16_t length)
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
* Makes a new ManagedString identical to the one supplied.
|
||||
* Copy constructor.
|
||||
* Makes a new ManagedString identical to the one supplied.
|
||||
* Shares the character buffer and reference count with the supplied ManagedString.
|
||||
*
|
||||
* @param s The ManagedString to copy.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcdefg");
|
||||
* ManagedString p(s);
|
||||
* @endcode
|
||||
|
@ -186,12 +186,12 @@ ManagedString::ManagedString(const ManagedString &s)
|
|||
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
* Default constructor.
|
||||
*
|
||||
* Create an empty ManagedString.
|
||||
* Create an empty ManagedString.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s();
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -201,7 +201,7 @@ ManagedString::ManagedString()
|
|||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destructor.
|
||||
*
|
||||
* Free this ManagedString, and decrement the reference count to the
|
||||
* internal character buffer. If we're holding the last reference,
|
||||
|
@ -213,7 +213,7 @@ ManagedString::~ManagedString()
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy assign operation.
|
||||
* Copy assign operation.
|
||||
*
|
||||
* Called when one ManagedString is assigned the value of another.
|
||||
* If the ManagedString being assigned is already refering to a character buffer,
|
||||
|
@ -224,16 +224,16 @@ ManagedString::~ManagedString()
|
|||
* @param s The ManagedString to copy.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
* ManagedString p("efgh");
|
||||
* p = s // p now points to s, s' ref is incremented
|
||||
* p = s // p now points to s, s' ref is incremented
|
||||
* @endcode
|
||||
*/
|
||||
ManagedString& ManagedString::operator = (const ManagedString& s)
|
||||
{
|
||||
if (this->ptr == s.ptr)
|
||||
return *this;
|
||||
return *this;
|
||||
|
||||
ptr->decr();
|
||||
ptr = s.ptr;
|
||||
|
@ -251,10 +251,10 @@ ManagedString& ManagedString::operator = (const ManagedString& s)
|
|||
* @return true if this ManagedString is identical to the one supplied, false otherwise.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
* ManagedString p("efgh");
|
||||
*
|
||||
*
|
||||
* if(p==s)
|
||||
* print("We are the same!");
|
||||
* else
|
||||
|
@ -275,14 +275,14 @@ bool ManagedString::operator== (const ManagedString& s)
|
|||
* @return true if this ManagedString is alphabetically less than to the one supplied, false otherwise.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("a");
|
||||
* ManagedString p("b");
|
||||
*
|
||||
*
|
||||
* if(s<p)
|
||||
* print("a is before b!"); //a is before b
|
||||
* else
|
||||
* print("b is before a!");
|
||||
* print("b is before a!");
|
||||
* @endcode
|
||||
*/
|
||||
bool ManagedString::operator< (const ManagedString& s)
|
||||
|
@ -299,14 +299,14 @@ bool ManagedString::operator< (const ManagedString& s)
|
|||
* @return true if this ManagedString is alphabetically greater than to the one supplied, false otherwise.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("a");
|
||||
* ManagedString p("b");
|
||||
*
|
||||
*
|
||||
* if(p>a)
|
||||
* print("b is after a!"); //b is after a
|
||||
* else
|
||||
* print("a is after b!");
|
||||
* print("a is after b!");
|
||||
* @endcode
|
||||
*/
|
||||
bool ManagedString::operator> (const ManagedString& s)
|
||||
|
@ -322,12 +322,12 @@ bool ManagedString::operator> (const ManagedString& s)
|
|||
* @return a ManagedString representing the requested substring.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcdefg");
|
||||
*
|
||||
* print(s.substring(0,2)) // prints "ab"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString ManagedString::substring(int16_t start, int16_t length)
|
||||
{
|
||||
// If the parameters are illegal, just return a reference to the empty string.
|
||||
|
@ -348,13 +348,13 @@ ManagedString ManagedString::substring(int16_t start, int16_t length)
|
|||
* @return a new ManagedString representing the joined strings.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
* ManagedString p("efgh")
|
||||
*
|
||||
* print(s + p) // prints "abcdefgh"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
ManagedString ManagedString::operator+ (ManagedString& s)
|
||||
{
|
||||
// If the other string is empty, nothing to do!
|
||||
|
@ -375,12 +375,12 @@ ManagedString ManagedString::operator+ (ManagedString& s)
|
|||
* @return the character at posisiton index, zero if index is invalid.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* ManagedString s("abcd");
|
||||
*
|
||||
* print(s.charAt(1)) // prints "b"
|
||||
* @endcode
|
||||
*/
|
||||
*/
|
||||
char ManagedString::charAt(int16_t index)
|
||||
{
|
||||
return (index >=0 && index < length()) ? ptr->data[index] : 0;
|
||||
|
|
|
@ -169,7 +169,7 @@ Matrix4 Matrix4::multiply(Matrix4 &matrix, bool transpose)
|
|||
{
|
||||
int w = transpose ? height() : width();
|
||||
int h = transpose ? width() : height();
|
||||
|
||||
|
||||
if (w != matrix.height())
|
||||
return Matrix4(0, 0);
|
||||
|
||||
|
|
|
@ -26,6 +26,6 @@ void MemberFunctionCallback::fire(MicroBitEvent e)
|
|||
*/
|
||||
bool MemberFunctionCallback::operator==(const MemberFunctionCallback &mfc)
|
||||
{
|
||||
return (object == mfc.object && (memcmp(method,mfc.method,sizeof(method))==0));
|
||||
return (object == mfc.object && (memcmp(method,mfc.method,sizeof(method))==0));
|
||||
}
|
||||
|
||||
|
|
|
@ -41,10 +41,10 @@ MicroBitButton::MicroBitButton(uint16_t id, PinName name, MicroBitButtonEventCon
|
|||
* @code
|
||||
*
|
||||
* // configure a button to generate all possible events.
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
|
||||
*
|
||||
* // configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events.
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
|
||||
* uBit.buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
|
||||
*
|
||||
* @endcode
|
||||
*/
|
||||
|
|
|
@ -10,18 +10,18 @@
|
|||
* @param s the char* to reverse.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
*/
|
||||
int string_reverse(char *s)
|
||||
int string_reverse(char *s)
|
||||
{
|
||||
//sanity check...
|
||||
if(s == NULL)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
||||
char *j;
|
||||
int c;
|
||||
|
||||
|
||||
j = s + strlen(s) - 1;
|
||||
|
||||
while(s < j)
|
||||
|
||||
while(s < j)
|
||||
{
|
||||
c = *s;
|
||||
*s++ = *j;
|
||||
|
@ -49,20 +49,20 @@ int itoa(int n, char *s)
|
|||
// Record the sign of the number,
|
||||
// Ensure our working value is positive.
|
||||
if (positive)
|
||||
n = -n;
|
||||
n = -n;
|
||||
|
||||
// Calculate each character, starting with the LSB.
|
||||
do {
|
||||
// Calculate each character, starting with the LSB.
|
||||
do {
|
||||
s[i++] = abs(n % 10) + '0';
|
||||
} while (abs(n /= 10) > 0);
|
||||
|
||||
|
||||
// Add a negative sign as needed
|
||||
if (!positive)
|
||||
s[i++] = '-';
|
||||
|
||||
|
||||
// Terminate the string.
|
||||
s[i] = '\0';
|
||||
|
||||
|
||||
// Flip the order.
|
||||
string_reverse(s);
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
*/
|
||||
|
||||
|
||||
/*
|
||||
/*
|
||||
* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
|
||||
* If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
|
||||
* The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
|
||||
|
@ -33,7 +33,7 @@
|
|||
#include "nrf_soc.h"
|
||||
#include "nrf_sdm.h"
|
||||
|
||||
/*
|
||||
/*
|
||||
* Return to our predefined compiler settings.
|
||||
*/
|
||||
#if !defined(__arm)
|
||||
|
|
|
@ -9,13 +9,13 @@
|
|||
EventModel* EventModel::defaultEventBus = NULL;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
|
||||
* @param value Component specific code indicating the cause of the event.
|
||||
* @param fire whether the event should be fire immediately upon construction
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK,true); // auto fire
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -24,10 +24,10 @@ MicroBitEvent::MicroBitEvent(uint16_t source, uint16_t value, MicroBitEventLaunc
|
|||
this->source = source;
|
||||
this->value = value;
|
||||
this->timestamp = system_timer_current_time();
|
||||
|
||||
|
||||
if(mode != CREATE_ONLY)
|
||||
this->fire();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor - initialises all values, and sets timestamp to the current time.
|
||||
|
@ -50,7 +50,7 @@ void MicroBitEvent::fire()
|
|||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a new MicroBitEventQueueItem.
|
||||
* @param evt The event to be queued.
|
||||
*/
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
* The original MicroBit font.
|
||||
* The font is 5x5.
|
||||
* Each Row is represented by a byte in the array.
|
||||
*
|
||||
*
|
||||
* Row Format:| N/A | N/A | N/A | Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
|
||||
* | 0x80 | 0x40 | 0x20 | 0x10 | 0x08 | 0x04 | 0x02 | 0x01 |
|
||||
*
|
||||
|
@ -41,8 +41,8 @@ MicroBitFont MicroBitFont::systemFont = MicroBitFont(defaultFont, MICROBIT_FONT_
|
|||
*/
|
||||
MicroBitFont::MicroBitFont(const unsigned char* characters, int asciiEnd)
|
||||
{
|
||||
this->characters = characters;
|
||||
this->asciiEnd = asciiEnd;
|
||||
this->characters = characters;
|
||||
this->asciiEnd = asciiEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,9 +51,9 @@ MicroBitFont::MicroBitFont(const unsigned char* characters, int asciiEnd)
|
|||
*/
|
||||
MicroBitFont::MicroBitFont()
|
||||
{
|
||||
this->characters = defaultFont;
|
||||
this->asciiEnd = MICROBIT_FONT_ASCII_END;
|
||||
}
|
||||
this->characters = defaultFont;
|
||||
this->asciiEnd = MICROBIT_FONT_ASCII_END;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the current system font to the one specified.
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
* be designated as heap storage, and is designed to run in a static memory area or inside the standard C
|
||||
* heap for use by the micro:bit runtime. This is required for several reasons:
|
||||
*
|
||||
* 1) It reduces memory fragmentation due to the high churn sometime placed on the heap
|
||||
* 1) It reduces memory fragmentation due to the high churn sometime placed on the heap
|
||||
* by ManagedTypes, fibers and user code. Underlying heap implentations are often have very simplistic
|
||||
* allocation pilicies and suffer from fragmentation in prolonged use - which can cause programs to
|
||||
* stop working after a period of time. The algorithm implemented here is simple, but highly tolerant to
|
||||
* large amounts of churn.
|
||||
* stop working after a period of time. The algorithm implemented here is simple, but highly tolerant to
|
||||
* large amounts of churn.
|
||||
*
|
||||
* 2) It allows us to reuse the 8K of SRAM set aside for SoftDevice as additional heap storage
|
||||
* when BLE is not in use.
|
||||
|
@ -19,7 +19,7 @@
|
|||
* N.B. The need for this should be reviewed in the future, should a different memory allocator be
|
||||
* made availiable in the mbed platform.
|
||||
*
|
||||
* P.S. This is a very simple allocator, therefore not without its weaknesses. Why don't you consider
|
||||
* P.S. This is a very simple allocator, therefore not without its weaknesses. Why don't you consider
|
||||
* what these are, and consider the tradeoffs against simplicity...
|
||||
*
|
||||
* TODO: Consider caching recently freed blocks to improve allocation time.
|
||||
|
@ -32,7 +32,7 @@ struct HeapDefinition
|
|||
|
||||
// Create the necessary heap definitions.
|
||||
// We use two heaps by default: one for SoftDevice reuse, and one to run inside the mbed heap.
|
||||
HeapDefinition heap[MICROBIT_HEAP_COUNT] = { };
|
||||
HeapDefinition heap[MICROBIT_HEAP_COUNT] = { };
|
||||
|
||||
// Scans the status of the heap definition table, and returns the number of INITIALISED heaps.
|
||||
int microbit_active_heaps()
|
||||
|
@ -134,7 +134,7 @@ microbit_create_sd_heap(HeapDefinition &heap)
|
|||
// Reclaim RAM from unusused areas on the BLE stack GATT table.
|
||||
heap.heap_start = (uint32_t *)MICROBIT_HEAP_BASE_BLE_ENABLED;
|
||||
heap.heap_end = (uint32_t *)MICROBIT_HEAP_SD_LIMIT;
|
||||
#else
|
||||
#else
|
||||
// Reclaim all the RAM normally reserved for the Nordic SoftDevice.
|
||||
heap.heap_start = (uint32_t *)MICROBIT_HEAP_BASE_BLE_DISABLED;
|
||||
heap.heap_end = (uint32_t *)MICROBIT_HEAP_SD_LIMIT;
|
||||
|
@ -152,7 +152,7 @@ microbit_create_nested_heap(HeapDefinition &heap)
|
|||
{
|
||||
uint32_t mb_heap_max;
|
||||
void *p;
|
||||
|
||||
|
||||
// Ensure we're configured to use this heap at all. If not, we can safely return.
|
||||
if (MICROBIT_HEAP_SIZE <= 0)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
@ -221,7 +221,7 @@ microbit_heap_init()
|
|||
|
||||
#if CONFIG_ENABLED(MICROBIT_DBG) && CONFIG_ENABLED(MICROBIT_HEAP_DBG)
|
||||
microbit_heap_print();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -244,7 +244,7 @@ void *microbit_malloc(size_t size, HeapDefinition &heap)
|
|||
|
||||
// Account for the index block;
|
||||
blocksNeeded++;
|
||||
|
||||
|
||||
// Disable IRQ temporarily to ensure no race conditions!
|
||||
__disable_irq();
|
||||
|
||||
|
@ -262,7 +262,7 @@ void *microbit_malloc(size_t size, HeapDefinition &heap)
|
|||
|
||||
blockSize = *block & ~MICROBIT_HEAP_BLOCK_FREE;
|
||||
|
||||
// We have a free block. Let's see if the subsequent ones are too. If so, we can merge...
|
||||
// We have a free block. Let's see if the subsequent ones are too. If so, we can merge...
|
||||
next = block + blockSize;
|
||||
|
||||
while (*next & MICROBIT_HEAP_BLOCK_FREE)
|
||||
|
@ -273,11 +273,11 @@ void *microbit_malloc(size_t size, HeapDefinition &heap)
|
|||
// We can merge!
|
||||
blockSize += (*next & ~MICROBIT_HEAP_BLOCK_FREE);
|
||||
*block = blockSize | MICROBIT_HEAP_BLOCK_FREE;
|
||||
|
||||
|
||||
next = block + blockSize;
|
||||
}
|
||||
|
||||
// We have a free block. Let's see if it's big enough.
|
||||
// We have a free block. Let's see if it's big enough.
|
||||
// If so, we have a winner.
|
||||
if (blockSize >= blocksNeeded)
|
||||
break;
|
||||
|
@ -334,7 +334,7 @@ void *microbit_malloc(size_t size)
|
|||
{
|
||||
#if CONFIG_ENABLED(MICROBIT_DBG) && CONFIG_ENABLED(MICROBIT_HEAP_DBG)
|
||||
uBit.serial.printf("microbit_malloc: ALLOCATED: %d [%p]\n", size, p);
|
||||
#endif
|
||||
#endif
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
@ -350,7 +350,7 @@ void *microbit_malloc(size_t size)
|
|||
// Keep everything trasparent if we've not been initialised yet
|
||||
if (microbit_active_heaps())
|
||||
uBit.serial.printf("microbit_malloc: NATIVE ALLOCATED: %d [%p]\n", size, p);
|
||||
#endif
|
||||
#endif
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -359,17 +359,17 @@ void *microbit_malloc(size_t size)
|
|||
// Keep everything trasparent if we've not been initialised yet
|
||||
if (microbit_active_heaps())
|
||||
uBit.serial.printf("microbit_malloc: OUT OF MEMORY\n");
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if CONFIG_ENABLED(MICROBIT_PANIC_HEAP_FULL)
|
||||
MicroBitDisplay::panic(MICROBIT_OOM);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a given area of memory from the heap.
|
||||
* Release a given area of memory from the heap.
|
||||
* @param mem The memory area to release.
|
||||
*/
|
||||
void microbit_free(void *mem)
|
||||
|
@ -380,11 +380,11 @@ void microbit_free(void *mem)
|
|||
#if CONFIG_ENABLED(MICROBIT_DBG) && CONFIG_ENABLED(MICROBIT_HEAP_DBG)
|
||||
if (microbit_active_heaps())
|
||||
uBit.serial.printf("microbit_free: %p\n", mem);
|
||||
#endif
|
||||
#endif
|
||||
// Sanity check.
|
||||
if (memory == NULL)
|
||||
return;
|
||||
|
||||
|
||||
// If this memory was created from a heap registered with us, free it.
|
||||
for (int i=0; i < MICROBIT_HEAP_COUNT; i++)
|
||||
{
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
#include "nrf_delay.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create an instance of i2c
|
||||
* @param sda the Pin to be used for SDA
|
||||
* @param scl the Pin to be used for SCL
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicroBitI2C i2c(MICROBIT_PIN_SDA, MICROBIT_PIN_SCL);
|
||||
* @endcode
|
||||
* @note this should prevent i2c lockups as well.
|
||||
|
@ -32,12 +32,12 @@ MicroBitI2C::MicroBitI2C(PinName sda, PinName scl) : I2C(sda,scl)
|
|||
int MicroBitI2C::read(int address, char *data, int length, bool repeated)
|
||||
{
|
||||
int result = I2C::read(address,data,length,repeated);
|
||||
|
||||
|
||||
//0 indicates a success, presume failure
|
||||
while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES)
|
||||
{
|
||||
_i2c.i2c->EVENTS_ERROR = 0;
|
||||
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
|
||||
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
|
||||
_i2c.i2c->POWER = 0;
|
||||
nrf_delay_us(5);
|
||||
_i2c.i2c->POWER = 1;
|
||||
|
@ -46,11 +46,11 @@ int MicroBitI2C::read(int address, char *data, int length, bool repeated)
|
|||
result = I2C::read(address,data,length,repeated);
|
||||
retries++;
|
||||
}
|
||||
|
||||
|
||||
if(result != 0)
|
||||
return MICROBIT_I2C_ERROR;
|
||||
|
||||
retries = 0;
|
||||
|
||||
retries = 0;
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
@ -58,34 +58,34 @@ int MicroBitI2C::read(int address, char *data, int length, bool repeated)
|
|||
* Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.
|
||||
*
|
||||
* @address 8-bit I2C slave address [ addr | 0 ]
|
||||
* @data Pointer to the byte-arraycontaining the data to write
|
||||
* @data Pointer to the byte-arraycontaining the data to write
|
||||
* @length Number of bytes to write
|
||||
* @repeated Repeated start, true - don't send stop at end.
|
||||
*
|
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved write failure is detected.
|
||||
*/
|
||||
int MicroBitI2C::write(int address, const char *data, int length, bool repeated)
|
||||
{
|
||||
{
|
||||
int result = I2C::write(address,data,length,repeated);
|
||||
|
||||
|
||||
//0 indicates a success, presume failure
|
||||
while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES)
|
||||
{
|
||||
_i2c.i2c->EVENTS_ERROR = 0;
|
||||
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
|
||||
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
|
||||
_i2c.i2c->POWER = 0;
|
||||
nrf_delay_us(5);
|
||||
_i2c.i2c->POWER = 1;
|
||||
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
|
||||
|
||||
|
||||
twi_master_init_and_clear();
|
||||
result = I2C::write(address,data,length,repeated);
|
||||
retries++;
|
||||
}
|
||||
|
||||
|
||||
if(result != 0)
|
||||
return MICROBIT_I2C_ERROR;
|
||||
|
||||
retries = 0;
|
||||
|
||||
retries = 0;
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@ static const uint16_t empty[] __attribute__ ((aligned (4))) = { 0xffff, 1, 1, 0,
|
|||
MicroBitImage MicroBitImage::EmptyImage((ImageData*)(void*)empty);
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
* Creates a new reference to the empty MicroBitImage bitmap
|
||||
* Default Constructor.
|
||||
* Creates a new reference to the empty MicroBitImage bitmap
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
|
@ -31,13 +31,13 @@ MicroBitImage::MicroBitImage()
|
|||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a blank bitmap representation of a given size.
|
||||
*
|
||||
* @param x the width of the image.
|
||||
* @param y the height of the image.
|
||||
*
|
||||
* Bitmap buffer is linear, with 8 bits per pixel, row by row,
|
||||
* @param x the width of the image.
|
||||
* @param y the height of the image.
|
||||
*
|
||||
* Bitmap buffer is linear, with 8 bits per pixel, row by row,
|
||||
* top to bottom with no word alignment. Stride is therefore the image width in pixels.
|
||||
* in where w and h are width and height respectively, the layout is therefore:
|
||||
*
|
||||
|
@ -54,9 +54,9 @@ MicroBitImage::MicroBitImage(const int16_t x, const int16_t y)
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy Constructor.
|
||||
* Copy Constructor.
|
||||
* Add ourselves as a reference to an existing MicroBitImage.
|
||||
*
|
||||
*
|
||||
* @param image The MicroBitImage to reference.
|
||||
*
|
||||
* Example:
|
||||
|
@ -72,9 +72,9 @@ MicroBitImage::MicroBitImage(const MicroBitImage &image)
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a blank bitmap representation of a given size.
|
||||
*
|
||||
*
|
||||
* @param s A text based representation of the image given whitespace delimited numeric values.
|
||||
*
|
||||
* Example:
|
||||
|
@ -110,7 +110,7 @@ MicroBitImage::MicroBitImage(const char *s)
|
|||
{
|
||||
// Ignore numbers.
|
||||
digit = 1;
|
||||
}
|
||||
}
|
||||
else if (*parseReadPtr =='\n')
|
||||
{
|
||||
if (digit)
|
||||
|
@ -123,7 +123,7 @@ MicroBitImage::MicroBitImage(const char *s)
|
|||
|
||||
width = count > width ? count : width;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digit)
|
||||
|
@ -147,9 +147,9 @@ MicroBitImage::MicroBitImage(const char *s)
|
|||
{
|
||||
if (isdigit(*parseReadPtr))
|
||||
{
|
||||
*parseWritePtr = *parseReadPtr;
|
||||
*parseWritePtr = *parseReadPtr;
|
||||
parseWritePtr++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*parseWritePtr = 0;
|
||||
|
@ -166,13 +166,13 @@ MicroBitImage::MicroBitImage(const char *s)
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create an image from a specially prepared constant array, with no copying. Will call ptr->incr().
|
||||
*
|
||||
* @param ptr The literal - first two bytes should be 0xff, then width, 0, height, 0, and the bitmap. Width and height are 16 bit. The literal has to be 4-byte aligned.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* static const uint8_t heart[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 10, 0, 5, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i((ImageData*)(void*)heart);
|
||||
* @endcode
|
||||
|
@ -182,7 +182,7 @@ MicroBitImage::MicroBitImage(ImageData *p)
|
|||
ptr = p;
|
||||
ptr->incr();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get current ptr, do not decr() it, and set the current instance to empty image.
|
||||
* This is to be used by specialized runtimes which pass ImageData around.
|
||||
|
@ -196,18 +196,18 @@ ImageData *MicroBitImage::leakData()
|
|||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a bitmap representation of a given size, based on a given buffer.
|
||||
*
|
||||
*
|
||||
* @param x the width of the image.
|
||||
* @param y the height of the image.
|
||||
* @param bitmap a 2D array representing the image.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* @endcode
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitImage::MicroBitImage(const int16_t x, const int16_t y, const uint8_t *bitmap)
|
||||
{
|
||||
|
@ -215,7 +215,7 @@ MicroBitImage::MicroBitImage(const int16_t x, const int16_t y, const uint8_t *bi
|
|||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destructor.
|
||||
* Removes buffer resources held by the instance.
|
||||
*/
|
||||
MicroBitImage::~MicroBitImage()
|
||||
|
@ -227,7 +227,7 @@ MicroBitImage::~MicroBitImage()
|
|||
* Internal constructor which defaults to the EmptyImage instance variable
|
||||
*/
|
||||
void MicroBitImage::init_empty()
|
||||
{
|
||||
{
|
||||
ptr = (ImageData*)(void*)empty;
|
||||
}
|
||||
|
||||
|
@ -244,19 +244,19 @@ void MicroBitImage::init(const int16_t x, const int16_t y, const uint8_t *bitmap
|
|||
if(x < 0 || y < 0)
|
||||
{
|
||||
init_empty();
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Create a copy of the array
|
||||
ptr = (ImageData*)malloc(sizeof(ImageData) + x * y);
|
||||
ptr->init();
|
||||
ptr->width = x;
|
||||
ptr->height = y;
|
||||
|
||||
|
||||
// create a linear buffer to represent the image. We could use a jagged/2D array here, but experimentation
|
||||
// showed this had a negative effect on memory management (heap fragmentation etc).
|
||||
|
||||
|
||||
if (bitmap)
|
||||
this->printImage(x,y,bitmap);
|
||||
else
|
||||
|
@ -264,7 +264,7 @@ void MicroBitImage::init(const int16_t x, const int16_t y, const uint8_t *bitmap
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy assign operation.
|
||||
* Copy assign operation.
|
||||
*
|
||||
* Called when one MicroBitImage is assigned the value of another using the '=' operator.
|
||||
* Decrement our reference count and free up the buffer as necessary.
|
||||
|
@ -272,13 +272,13 @@ void MicroBitImage::init(const int16_t x, const int16_t y, const uint8_t *bitmap
|
|||
* and increase its reference count.
|
||||
*
|
||||
* @param s The MicroBitImage to reference.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* MicroBitImage i1();
|
||||
* i1 = 1; // i1 now references i
|
||||
* i1 = 1; // i1 now references i
|
||||
* @endcode
|
||||
*/
|
||||
MicroBitImage& MicroBitImage::operator = (const MicroBitImage& i)
|
||||
|
@ -300,14 +300,14 @@ MicroBitImage& MicroBitImage::operator = (const MicroBitImage& i)
|
|||
*
|
||||
* @param i The MicroBitImage to test ourselves against.
|
||||
* @return true if this MicroBitImage is identical to the one supplied, false otherwise.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* MicroBitImage i();
|
||||
* MicroBitImage i();
|
||||
* MicroBitImage i1();
|
||||
*
|
||||
* if(i == i1) //will be true
|
||||
* print("true");
|
||||
* print("true");
|
||||
* @endcode
|
||||
*/
|
||||
bool MicroBitImage::operator== (const MicroBitImage& i)
|
||||
|
@ -315,7 +315,7 @@ bool MicroBitImage::operator== (const MicroBitImage& i)
|
|||
if (ptr == i.ptr)
|
||||
return true;
|
||||
else
|
||||
return (ptr->width == i.ptr->width && ptr->height == i.ptr->height && (memcmp(getBitmap(), i.ptr->data, getSize())==0));
|
||||
return (ptr->width == i.ptr->width && ptr->height == i.ptr->height && (memcmp(getBitmap(), i.ptr->data, getSize())==0));
|
||||
}
|
||||
|
||||
|
||||
|
@ -332,7 +332,7 @@ void MicroBitImage::clear()
|
|||
{
|
||||
memclr(getBitmap(), getSize());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the pixel at the given co-ordinates to a given value.
|
||||
* @param x The co-ordinate of the pixel to change w.r.t. top left origin.
|
||||
|
@ -351,7 +351,7 @@ int MicroBitImage::setPixelValue(int16_t x , int16_t y, uint8_t value)
|
|||
//sanity check
|
||||
if(x >= getWidth() || y >= getHeight() || x < 0 || y < 0)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
||||
this->getBitmap()[y*getWidth()+x] = value;
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -374,12 +374,12 @@ int MicroBitImage::getPixelValue(int16_t x , int16_t y)
|
|||
//sanity check
|
||||
if(x >= getWidth() || y >= getHeight() || x < 0 || y < 0)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
||||
return this->getBitmap()[y*getWidth()+x];
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the content of this image with that of a given
|
||||
* Replaces the content of this image with that of a given
|
||||
* 2D array representing the image.
|
||||
* Origin is in the top left corner of the image.
|
||||
*
|
||||
|
@ -387,16 +387,16 @@ int MicroBitImage::getPixelValue(int16_t x , int16_t y)
|
|||
* @param y the width of the image. Must be within the dimensions of the image.
|
||||
* @param bitmap a 2D array representing the image.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i();
|
||||
* i.printImage(0,0,heart);
|
||||
* MicroBitImage i();
|
||||
* i.printImage(0,0,heart);
|
||||
* @endcode
|
||||
*/
|
||||
int MicroBitImage::printImage(int16_t width, int16_t height, const uint8_t *bitmap)
|
||||
{
|
||||
{
|
||||
const uint8_t *pIn;
|
||||
uint8_t *pOut;
|
||||
int pixelsToCopyX, pixelsToCopyY;
|
||||
|
@ -411,7 +411,7 @@ int MicroBitImage::printImage(int16_t width, int16_t height, const uint8_t *bitm
|
|||
|
||||
pIn = bitmap;
|
||||
pOut = this->getBitmap();
|
||||
|
||||
|
||||
// Copy the image, stride by stride.
|
||||
for (int i=0; i<pixelsToCopyY; i++)
|
||||
{
|
||||
|
@ -422,22 +422,22 @@ int MicroBitImage::printImage(int16_t width, int16_t height, const uint8_t *bitm
|
|||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pastes a given bitmap at the given co-ordinates.
|
||||
* Any pixels in the relvant area of this image are replaced.
|
||||
*
|
||||
*
|
||||
* @param image The MicroBitImage to paste.
|
||||
* @param x The leftmost X co-ordinate in this image where the given image should be pasted.
|
||||
* @param y The uppermost Y co-ordinate in this image where the given image should be pasted.
|
||||
* @param alpha set to 1 if transparency clear pixels in given image should be treated as transparent. Set to 0 otherwise.
|
||||
* @return The number of pixels written.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart); //if you show this image - you will see a big heart
|
||||
* i.paste(-5,0,i); //displays a small heart :)
|
||||
* i.paste(-5,0,i); //displays a small heart :)
|
||||
* @endcode
|
||||
*/
|
||||
int MicroBitImage::paste(const MicroBitImage &image, int16_t x, int16_t y, uint8_t alpha)
|
||||
|
@ -459,7 +459,7 @@ int MicroBitImage::paste(const MicroBitImage &image, int16_t x, int16_t y, uint8
|
|||
pIn = image.ptr->data;
|
||||
pIn += (x < 0) ? -x : 0;
|
||||
pIn += (y < 0) ? -image.getWidth()*y : 0;
|
||||
|
||||
|
||||
pOut = getBitmap();
|
||||
pOut += (x > 0) ? x : 0;
|
||||
pOut += (y > 0) ? getWidth()*y : 0;
|
||||
|
@ -480,7 +480,7 @@ int MicroBitImage::paste(const MicroBitImage &image, int16_t x, int16_t y, uint8
|
|||
pxWritten++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pIn += image.getWidth();
|
||||
pOut += getWidth();
|
||||
}
|
||||
|
@ -496,7 +496,7 @@ int MicroBitImage::paste(const MicroBitImage &image, int16_t x, int16_t y, uint8
|
|||
pOut += getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return pxWritten;
|
||||
}
|
||||
|
||||
|
@ -507,10 +507,10 @@ int MicroBitImage::paste(const MicroBitImage &image, int16_t x, int16_t y, uint8
|
|||
* @param x The x co-ordinate of on the image to place the top left of the character
|
||||
* @param y The y co-ordinate of on the image to place the top left of the character
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* MicroBitImage i(5,5);
|
||||
* MicroBitImage i(5,5);
|
||||
* i.print('a',0,0);
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -518,34 +518,34 @@ int MicroBitImage::print(char c, int16_t x, int16_t y)
|
|||
{
|
||||
unsigned char v;
|
||||
int x1, y1;
|
||||
|
||||
|
||||
MicroBitFont font = MicroBitFont::getSystemFont();
|
||||
|
||||
|
||||
// Sanity check. Silently ignore anything out of bounds.
|
||||
if (x >= getWidth() || y >= getHeight() || c < MICROBIT_FONT_ASCII_START || c > font.asciiEnd)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
||||
// Paste.
|
||||
int offset = (c-MICROBIT_FONT_ASCII_START) * 5;
|
||||
|
||||
|
||||
for (int row=0; row<MICROBIT_FONT_HEIGHT; row++)
|
||||
{
|
||||
v = (char)*(font.characters + offset);
|
||||
|
||||
|
||||
offset++;
|
||||
|
||||
|
||||
// Update our Y co-ord write position
|
||||
y1 = y+row;
|
||||
|
||||
|
||||
for (int col = 0; col < MICROBIT_FONT_WIDTH; col++)
|
||||
{
|
||||
// Update our X co-ord write position
|
||||
x1 = x+col;
|
||||
|
||||
|
||||
if (x1 < getWidth() && y1 < getHeight())
|
||||
this->getBitmap()[y1*getWidth()+x1] = (v & (0x10 >> col)) ? 255 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -556,19 +556,19 @@ int MicroBitImage::print(char c, int16_t x, int16_t y)
|
|||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart); //if you show this image - you will see a big heart
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* @endcode
|
||||
*/
|
||||
int MicroBitImage::shiftLeft(int16_t n)
|
||||
{
|
||||
uint8_t *p = getBitmap();
|
||||
int pixels = getWidth()-n;
|
||||
|
||||
|
||||
if (n <= 0 )
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
@ -577,14 +577,14 @@ int MicroBitImage::shiftLeft(int16_t n)
|
|||
clear();
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
||||
for (int y = 0; y < getHeight(); y++)
|
||||
{
|
||||
// Copy, and blank fill the rightmost column.
|
||||
memcpy(p, p+n, pixels);
|
||||
memclr(p+pixels, n);
|
||||
p += getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -594,20 +594,20 @@ int MicroBitImage::shiftLeft(int16_t n)
|
|||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* i.shiftRight(5); //displays a big heart :)
|
||||
* i.shiftLeft(5); //displays a small heart :)
|
||||
* i.shiftRight(5); //displays a big heart :)
|
||||
* @endcode
|
||||
*/
|
||||
int MicroBitImage::shiftRight(int16_t n)
|
||||
{
|
||||
uint8_t *p = getBitmap();
|
||||
int pixels = getWidth()-n;
|
||||
|
||||
|
||||
if (n <= 0)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
@ -623,7 +623,7 @@ int MicroBitImage::shiftRight(int16_t n)
|
|||
memmove(p+n, p, pixels);
|
||||
memclr(p, n);
|
||||
p += getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -634,7 +634,7 @@ int MicroBitImage::shiftRight(int16_t n)
|
|||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
@ -645,7 +645,7 @@ int MicroBitImage::shiftRight(int16_t n)
|
|||
int MicroBitImage::shiftUp(int16_t n)
|
||||
{
|
||||
uint8_t *pOut, *pIn;
|
||||
|
||||
|
||||
if (n <= 0 )
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
@ -654,10 +654,10 @@ int MicroBitImage::shiftUp(int16_t n)
|
|||
clear();
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
||||
pOut = getBitmap();
|
||||
pIn = getBitmap()+getWidth()*n;
|
||||
|
||||
|
||||
for (int y = 0; y < getHeight(); y++)
|
||||
{
|
||||
// Copy, and blank fill the leftmost column.
|
||||
|
@ -665,10 +665,10 @@ int MicroBitImage::shiftUp(int16_t n)
|
|||
memcpy(pOut, pIn, getWidth());
|
||||
else
|
||||
memclr(pOut, getWidth());
|
||||
|
||||
|
||||
pIn += getWidth();
|
||||
pOut += getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -679,7 +679,7 @@ int MicroBitImage::shiftUp(int16_t n)
|
|||
*
|
||||
* @param n The number of pixels to shift.
|
||||
* @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
|
@ -690,7 +690,7 @@ int MicroBitImage::shiftUp(int16_t n)
|
|||
int MicroBitImage::shiftDown(int16_t n)
|
||||
{
|
||||
uint8_t *pOut, *pIn;
|
||||
|
||||
|
||||
if (n <= 0 )
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
@ -699,10 +699,10 @@ int MicroBitImage::shiftDown(int16_t n)
|
|||
clear();
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
||||
pOut = getBitmap() + getWidth()*(getHeight()-1);
|
||||
pIn = pOut - getWidth()*n;
|
||||
|
||||
|
||||
for (int y = 0; y < getHeight(); y++)
|
||||
{
|
||||
// Copy, and blank fill the leftmost column.
|
||||
|
@ -710,10 +710,10 @@ int MicroBitImage::shiftDown(int16_t n)
|
|||
memcpy(pOut, pIn, getWidth());
|
||||
else
|
||||
memclr(pOut, getWidth());
|
||||
|
||||
|
||||
pIn -= getWidth();
|
||||
pOut -= getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -730,17 +730,17 @@ int MicroBitImage::shiftDown(int16_t n)
|
|||
* @endcode
|
||||
*/
|
||||
ManagedString MicroBitImage::toString()
|
||||
{
|
||||
{
|
||||
//width including commans and \n * height
|
||||
int stringSize = getSize() * 2;
|
||||
|
||||
|
||||
//plus one for string terminator
|
||||
char parseBuffer[stringSize + 1];
|
||||
|
||||
|
||||
parseBuffer[stringSize] = '\0';
|
||||
|
||||
|
||||
uint8_t *bitmapPtr = getBitmap();
|
||||
|
||||
|
||||
int parseIndex = 0;
|
||||
int widthCount = 0;
|
||||
|
||||
|
@ -748,11 +748,11 @@ ManagedString MicroBitImage::toString()
|
|||
{
|
||||
if(*bitmapPtr)
|
||||
parseBuffer[parseIndex] = '1';
|
||||
else
|
||||
else
|
||||
parseBuffer[parseIndex] = '0';
|
||||
|
||||
|
||||
parseIndex++;
|
||||
|
||||
|
||||
if(widthCount == getWidth()-1)
|
||||
{
|
||||
parseBuffer[parseIndex] = '\n';
|
||||
|
@ -763,11 +763,11 @@ ManagedString MicroBitImage::toString()
|
|||
parseBuffer[parseIndex] = ',';
|
||||
widthCount++;
|
||||
}
|
||||
|
||||
|
||||
parseIndex++;
|
||||
bitmapPtr++;
|
||||
}
|
||||
|
||||
|
||||
return ManagedString(parseBuffer);
|
||||
}
|
||||
|
||||
|
@ -793,29 +793,29 @@ MicroBitImage MicroBitImage::crop(int startx, int starty, int cropWidth, int cro
|
|||
|
||||
if (newWidth >= getWidth() || newWidth <=0)
|
||||
newWidth = getWidth();
|
||||
|
||||
|
||||
if (newHeight >= getHeight() || newHeight <= 0)
|
||||
newHeight = getHeight();
|
||||
|
||||
newHeight = getHeight();
|
||||
|
||||
//allocate our storage.
|
||||
uint8_t cropped[newWidth * newHeight];
|
||||
|
||||
|
||||
//calculate the pointer to where we want to begin cropping
|
||||
uint8_t *copyPointer = getBitmap() + (getWidth() * starty) + startx;
|
||||
|
||||
uint8_t *copyPointer = getBitmap() + (getWidth() * starty) + startx;
|
||||
|
||||
//get a reference to our storage
|
||||
uint8_t *pastePointer = cropped;
|
||||
|
||||
|
||||
//go through row by row and select our image.
|
||||
for (int i = starty; i < newHeight; i++)
|
||||
{
|
||||
memcpy(pastePointer, copyPointer, newWidth);
|
||||
|
||||
|
||||
copyPointer += getWidth();
|
||||
pastePointer += newHeight;
|
||||
}
|
||||
|
||||
return MicroBitImage(newWidth, newHeight, cropped);
|
||||
|
||||
return MicroBitImage(newWidth, newHeight, cropped);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include "MicroBit.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a new Message Bus Listener.
|
||||
* @param id The ID of the component you want to listen to.
|
||||
* @param value The event ID you would like to listen to from that component
|
||||
|
@ -27,7 +27,7 @@ MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a new parameterised Message Bus Listener.
|
||||
* @param id The ID of the component you want to listen to.
|
||||
* @param value The event ID you would like to listen to from that component.
|
||||
|
@ -76,7 +76,7 @@ void MicroBitListener::queue(MicroBitEvent e)
|
|||
queueDepth++;
|
||||
}
|
||||
|
||||
if (queueDepth < MESSAGE_BUS_LISTENER_MAX_QUEUE_DEPTH)
|
||||
if (queueDepth < MESSAGE_BUS_LISTENER_MAX_QUEUE_DEPTH)
|
||||
p->next = new MicroBitEventQueueItem(e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "MicroBit.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* 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.
|
||||
|
@ -10,26 +10,26 @@
|
|||
* @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);
|
||||
* @code
|
||||
* multiButton(MICROBIT_ID_BUTTON_AB, MICROBIT_ID_BUTTON_A, MICROBIT_ID_BUTTON_B);
|
||||
* @endcode
|
||||
*
|
||||
* Possible Events:
|
||||
* @code
|
||||
* @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);
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ 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;
|
||||
}
|
||||
|
||||
|
@ -54,10 +54,10 @@ 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;
|
||||
}
|
||||
|
||||
|
@ -65,10 +65,10 @@ 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;
|
||||
}
|
||||
|
||||
|
@ -81,7 +81,7 @@ void MicroBitMultiButton::setButtonState(uint16_t button, int value)
|
|||
else
|
||||
status &= ~MICROBIT_MULTI_BUTTON_STATE_1;
|
||||
}
|
||||
|
||||
|
||||
if (button == button2)
|
||||
{
|
||||
if (value)
|
||||
|
@ -100,7 +100,7 @@ void MicroBitMultiButton::setHoldState(uint16_t button, int value)
|
|||
else
|
||||
status &= ~MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1;
|
||||
}
|
||||
|
||||
|
||||
if (button == button2)
|
||||
{
|
||||
if (value)
|
||||
|
@ -119,7 +119,7 @@ void MicroBitMultiButton::setSupressedState(uint16_t button, int value)
|
|||
else
|
||||
status &= ~MICROBIT_MULTI_BUTTON_SUPRESSED_1;
|
||||
}
|
||||
|
||||
|
||||
if (button == button2)
|
||||
{
|
||||
if (value)
|
||||
|
@ -139,10 +139,10 @@ void MicroBitMultiButton::setSupressedState(uint16_t button, int value)
|
|||
* @code
|
||||
*
|
||||
* // configure a button to generate all possible events from attached buttons.
|
||||
* uBit.buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
|
||||
* 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);
|
||||
* uBit.buttonAB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
|
||||
*
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -154,28 +154,28 @@ 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
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
#include "MicroBit.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create an instance of MicroBitSerial
|
||||
*
|
||||
* @param tx the Pin to be used for transmission
|
||||
* @param rx the Pin to be used for receiving data
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicroBitSerial serial(USBTX, USBRX);
|
||||
* @endcode
|
||||
* @note the default baud rate is 115200
|
||||
|
@ -25,16 +25,16 @@ MicroBitSerial::MicroBitSerial(PinName tx, PinName rx) : Serial(tx,rx)
|
|||
* @param s the ManagedString to send
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.printString("abc123");
|
||||
* @endcode
|
||||
*/
|
||||
void MicroBitSerial::sendString(ManagedString s)
|
||||
{
|
||||
{
|
||||
const int len = s.length();
|
||||
const char *data = s.toCharArray();
|
||||
|
||||
Serial::write(data,len);
|
||||
|
||||
Serial::write(data,len);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,30 +43,30 @@ void MicroBitSerial::sendString(ManagedString s)
|
|||
* @param len the buffer size for the string, default is defined by MICROBIT_SERIAL_BUFFER_SIZE
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.readString();
|
||||
* @endcode
|
||||
*
|
||||
* @note this member function will wait until either the buffer is full, or a \n is received
|
||||
*/
|
||||
ManagedString MicroBitSerial::readString(int len)
|
||||
{
|
||||
{
|
||||
if(len < 3)
|
||||
len = 3;
|
||||
|
||||
char buffer[len];
|
||||
|
||||
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
int length = readChars(buffer,len);
|
||||
|
||||
|
||||
int length = readChars(buffer,len);
|
||||
|
||||
if(length == 0)
|
||||
return ManagedString();
|
||||
|
||||
|
||||
//add in a null terminator so bad things don't happen with ManagedString
|
||||
buffer[length] = '\0';
|
||||
|
||||
return ManagedString(buffer);
|
||||
|
||||
return ManagedString(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,7 +75,7 @@ ManagedString MicroBitSerial::readString(int len)
|
|||
* @param i the instance of MicroBitImage you would like to send.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
|
||||
* MicroBitImage i(10,5,heart);
|
||||
* uBit.serial.sendImage(i);
|
||||
|
@ -94,12 +94,12 @@ void MicroBitSerial::sendImage(MicroBitImage i)
|
|||
* @return a MicroBitImage with the format described over serial
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* MicroBitImage i = uBit.serial.readImage(5,5);
|
||||
* @endcode
|
||||
*
|
||||
* Example Serial Format:
|
||||
* @code
|
||||
* @code
|
||||
* 0,10x0a0,10x0a // 0x0a is a LF terminal which is used as a delimeter
|
||||
* @endcode
|
||||
* @note this will finish once the dimensions are met.
|
||||
|
@ -108,23 +108,23 @@ MicroBitImage MicroBitSerial::readImage(int width, int height)
|
|||
{
|
||||
int rowLength = width * 2;
|
||||
int len = rowLength * height;
|
||||
|
||||
|
||||
char buffer[len + 1];
|
||||
|
||||
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
|
||||
//add in a null terminator so bad things don't happen with MicroBitImage
|
||||
buffer[len] = '\0';
|
||||
|
||||
|
||||
for(int i = 0; i < height; i++)
|
||||
{
|
||||
int offset = i * rowLength;
|
||||
|
||||
|
||||
readChars(&buffer[offset],len - offset);
|
||||
|
||||
buffer[(offset + rowLength) - 1] = '\n';
|
||||
|
||||
buffer[(offset + rowLength) - 1] = '\n';
|
||||
}
|
||||
|
||||
|
||||
return MicroBitImage(buffer);
|
||||
}
|
||||
|
||||
|
@ -132,7 +132,7 @@ MicroBitImage MicroBitSerial::readImage(int width, int height)
|
|||
* Sends the current pixel values, byte-per-pixel, over serial
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.sendDisplayState();
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -140,7 +140,7 @@ void MicroBitSerial::sendDisplayState()
|
|||
{
|
||||
return;
|
||||
|
||||
/*
|
||||
/*
|
||||
for(int i = 0; i < MICROBIT_DISPLAY_HEIGHT; i++)
|
||||
for(int j = 0; j < MICROBIT_DISPLAY_WIDTH; j++)
|
||||
_putc(uBit.display.image.getPixelValue(j,i));
|
||||
|
@ -151,7 +151,7 @@ void MicroBitSerial::sendDisplayState()
|
|||
* Reads pixel values, byte-per-pixel, from serial, and sets the display.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* @code
|
||||
* uBit.serial.readDisplayState();
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -168,24 +168,24 @@ void MicroBitSerial::readDisplayState()
|
|||
}
|
||||
|
||||
ssize_t MicroBitSerial::readChars(void* buffer, size_t length, char eof) {
|
||||
|
||||
|
||||
char* ptr = (char*)buffer;
|
||||
char* end = ptr + length;
|
||||
|
||||
|
||||
int eofAscii = (int)eof;
|
||||
|
||||
|
||||
while (ptr != end) {
|
||||
|
||||
|
||||
int c = _getc();
|
||||
|
||||
|
||||
//check EOF
|
||||
if (c == eofAscii)
|
||||
if (c == eofAscii)
|
||||
break;
|
||||
|
||||
//store the character
|
||||
*ptr++ = c;
|
||||
}
|
||||
|
||||
|
||||
//store the character
|
||||
*ptr++ = c;
|
||||
}
|
||||
|
||||
return ptr - (const char*)buffer;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,17 +19,17 @@ MicroBitStorage::MicroBitStorage()
|
|||
* @param buffer the data to write.
|
||||
* @param address the location in memory to write to.
|
||||
* @param length the number of bytes to write.
|
||||
*/
|
||||
*/
|
||||
int MicroBitStorage::writeBytes(uint8_t *buffer, uint32_t address, int length)
|
||||
{
|
||||
(void) buffer;
|
||||
(void) address;
|
||||
(void) length;
|
||||
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Method for erasing a page in flash.
|
||||
*
|
||||
* @param page_address Address of the first word in the page to be erased.
|
||||
|
@ -55,19 +55,19 @@ void MicroBitStorage::flashPageErase(uint32_t * page_address)
|
|||
|
||||
/*
|
||||
* Reads the micro:bit's configuration data block from FLASH into a RAM buffer.
|
||||
* @return a pointer to the structure containing the stored data.
|
||||
* @return a pointer to the structure containing the stored data.
|
||||
* NOTE: it is the callers responsibility to free the buffer.
|
||||
*/
|
||||
*/
|
||||
|
||||
MicroBitConfigurationBlock *MicroBitStorage::getConfigurationBlock()
|
||||
{
|
||||
uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
|
||||
uint32_t pg_num = NRF_FICR->CODESIZE - 19; // Use the page just below the BLE Bond Data
|
||||
uint32_t pg_num = NRF_FICR->CODESIZE - 19; // Use the page just below the BLE Bond Data
|
||||
|
||||
MicroBitConfigurationBlock *block = new MicroBitConfigurationBlock();
|
||||
memcpy(block, (uint32_t *)(pg_size * pg_num), sizeof(MicroBitConfigurationBlock));
|
||||
|
||||
if (block->magic != MICROBIT_STORAGE_CONFIG_MAGIC)
|
||||
if (block->magic != MICROBIT_STORAGE_CONFIG_MAGIC)
|
||||
memclr(block, sizeof(MicroBitConfigurationBlock));
|
||||
|
||||
#if CONFIG_ENABLED(MICROBIT_DBG)
|
||||
|
@ -102,7 +102,7 @@ MicroBitConfigurationBlock *MicroBitStorage::getConfigurationBlock()
|
|||
return block;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Function for filling a page in flash with a value.
|
||||
*
|
||||
* @param address Address of the first word in the page to be filled.
|
||||
|
@ -128,8 +128,8 @@ void MicroBitStorage::flashWordWrite(uint32_t * address, uint32_t value)
|
|||
|
||||
/*
|
||||
* Writes the micro:bit's configuration data block from FLASH into a RAM buffer.
|
||||
* @return a structure containing the stored data.
|
||||
*/
|
||||
* @return a structure containing the stored data.
|
||||
*/
|
||||
int MicroBitStorage::setConfigurationBlock(MicroBitConfigurationBlock *block)
|
||||
{
|
||||
|
||||
|
@ -167,9 +167,9 @@ int MicroBitStorage::setConfigurationBlock(MicroBitConfigurationBlock *block)
|
|||
uint32_t pg_size;
|
||||
uint32_t pg_num;
|
||||
int wordsToWrite = sizeof(MicroBitConfigurationBlock) / 4 + 1;
|
||||
|
||||
|
||||
pg_size = NRF_FICR->CODEPAGESIZE;
|
||||
pg_num = NRF_FICR->CODESIZE - 19; // Use the page just below the BLE Bond Data
|
||||
pg_num = NRF_FICR->CODESIZE - 19; // Use the page just below the BLE Bond Data
|
||||
|
||||
addr = (uint32_t *)(pg_size * pg_num);
|
||||
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
MicroBit uBit;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
#if CONFIG_ENABLED(MICROBIT_DBG)
|
||||
uBit.serial.printf("micro:bit runtime version %s\n", MICROBIT_DAL_VERSION);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Bring up random number generator, BLE, display and system timers.
|
||||
// Bring up random number generator, BLE, display and system timers.
|
||||
uBit.init();
|
||||
|
||||
// Start the user application
|
||||
|
|
|
@ -27,7 +27,7 @@ static Ticker timer;
|
|||
|
||||
|
||||
/**
|
||||
* Initialises the system wide timer.
|
||||
* Initialises the system wide timer.
|
||||
* This must be called before any components register to receive periodic periodic callbacks.
|
||||
*
|
||||
* @param timer_period The initial period between interrupts, in millseconds.
|
||||
|
@ -55,7 +55,7 @@ int system_timer_set_period(int period)
|
|||
|
||||
// register a period callback to drive the scheduler and any other registered components.
|
||||
tick_period = period;
|
||||
timer.attach_us(system_timer_tick, period * 1000);
|
||||
timer.attach_us(system_timer_tick, period * 1000);
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ unsigned long system_timer_current_time()
|
|||
|
||||
/**
|
||||
* Timer callback. Called from interrupt context, once per period.
|
||||
* Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
|
||||
* Simply checks to determine if any fibers blocked on the sleep queue need to be woken up
|
||||
* and made runnable.
|
||||
*/
|
||||
void system_timer_tick()
|
||||
|
@ -104,14 +104,14 @@ void system_timer_tick()
|
|||
int system_timer_add_component(MicroBitComponent *component)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS)
|
||||
|
||||
while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS)
|
||||
i++;
|
||||
|
||||
|
||||
if(i == MICROBIT_SYSTEM_COMPONENTS)
|
||||
return MICROBIT_NO_RESOURCES;
|
||||
|
||||
systemTickComponents[i] = component;
|
||||
|
||||
systemTickComponents[i] = component;
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
@ -124,10 +124,10 @@ int system_timer_add_component(MicroBitComponent *component)
|
|||
int system_timer_remove_component(MicroBitComponent *component)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while(systemTickComponents[i] != component && i < MICROBIT_SYSTEM_COMPONENTS)
|
||||
|
||||
while(systemTickComponents[i] != component && i < MICROBIT_SYSTEM_COMPONENTS)
|
||||
i++;
|
||||
|
||||
|
||||
if(i == MICROBIT_SYSTEM_COMPONENTS)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
*/
|
||||
|
||||
|
||||
/*
|
||||
/*
|
||||
* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
|
||||
* If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
|
||||
* The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
|
||||
|
@ -29,7 +29,7 @@
|
|||
#include "nrf_soc.h"
|
||||
#include "nrf_sdm.h"
|
||||
|
||||
/*
|
||||
/*
|
||||
* Return to our predefined compiler settings.
|
||||
*/
|
||||
#if !defined(__arm)
|
||||
|
@ -37,17 +37,17 @@
|
|||
#endif
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create new object that can sense temperature.
|
||||
* @param id the ID of the new MicroBitThermometer object.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* thermometer(MICROBIT_ID_THERMOMETER);
|
||||
* @code
|
||||
* thermometer(MICROBIT_ID_THERMOMETER);
|
||||
* @endcode
|
||||
*
|
||||
* Possible Events:
|
||||
* @code
|
||||
* @code
|
||||
* MICROBIT_THERMOMETER_EVT_CHANGED
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -85,14 +85,14 @@ int MicroBitThermometer::getTemperature()
|
|||
*/
|
||||
int MicroBitThermometer::isIdleCallbackNeeded()
|
||||
{
|
||||
return isSampleNeeded();
|
||||
return isSampleNeeded();
|
||||
}
|
||||
/**
|
||||
* periodic callback.
|
||||
* Check once every second or so for a new temperature reading.
|
||||
*/
|
||||
*/
|
||||
void MicroBitThermometer::idleTick()
|
||||
{
|
||||
{
|
||||
if (isSampleNeeded())
|
||||
updateTemperature();
|
||||
}
|
||||
|
@ -102,24 +102,24 @@ void MicroBitThermometer::idleTick()
|
|||
* @return 1 if we're due to take a temperature reading, 0 otherwise.
|
||||
*/
|
||||
int MicroBitThermometer::isSampleNeeded()
|
||||
{
|
||||
{
|
||||
return system_timer_current_time() >= sampleTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sample rate at which the temperatureis read (in ms).
|
||||
* n.b. the temperature is alwasy read in the background, so wis only updated
|
||||
* when the processor is idle, or when the temperature is explicitly read.
|
||||
* when the processor is idle, or when the temperature is explicitly read.
|
||||
* The default sample period is 1 second.
|
||||
* @param period the requested time between samples, in milliseconds.
|
||||
*/
|
||||
void MicroBitThermometer::setPeriod(int period)
|
||||
{
|
||||
samplePeriod = period;
|
||||
samplePeriod = period;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the currently configured sample rate of the thermometer.
|
||||
* Reads the currently configured sample rate of the thermometer.
|
||||
* @return The time between samples, in milliseconds.
|
||||
*/
|
||||
int MicroBitThermometer::getPeriod()
|
||||
|
@ -155,7 +155,7 @@ void MicroBitThermometer::updateTemperature()
|
|||
|
||||
while (NRF_TEMP->EVENTS_DATARDY == 0);
|
||||
|
||||
NRF_TEMP->EVENTS_DATARDY = 0;
|
||||
NRF_TEMP->EVENTS_DATARDY = 0;
|
||||
|
||||
processorTemperature = *TEMP;
|
||||
|
||||
|
@ -168,7 +168,7 @@ void MicroBitThermometer::updateTemperature()
|
|||
|
||||
// Schedule our next sample.
|
||||
sampleTime = system_timer_current_time() + samplePeriod;
|
||||
|
||||
|
||||
// Send an event to indicate that we'e updated our temperature.
|
||||
MicroBitEvent e(id, MICROBIT_THERMOMETER_EVT_UPDATE);
|
||||
}
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
PacketBuffer PacketBuffer::EmptyPacket = PacketBuffer(1);
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
* Creates an empty Packet Buffer.
|
||||
* Default Constructor.
|
||||
* Creates an empty Packet Buffer.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p();
|
||||
* PacketBuffer p();
|
||||
* @endcode
|
||||
*/
|
||||
PacketBuffer::PacketBuffer()
|
||||
|
@ -18,8 +18,8 @@ PacketBuffer::PacketBuffer()
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Creates an empty Packet Buffer of the given size.
|
||||
* Constructor.
|
||||
* Creates an empty Packet Buffer of the given size.
|
||||
*
|
||||
* @param length The length of the buffer to create.
|
||||
*
|
||||
|
@ -34,14 +34,14 @@ PacketBuffer::PacketBuffer(int length)
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Creates a new PacketBuffer of the given size,
|
||||
* and fills it with the data provided.
|
||||
*
|
||||
* @param data The data with which to fill the buffer.
|
||||
* @param length The length of the buffer to create.
|
||||
* @param rssi The radio signal strength at the time this packet was recieved.
|
||||
*
|
||||
* @param rssi The radio signal strength at the time this packet was recieved.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* uint8_t buf = {13,5,2};
|
||||
|
@ -54,15 +54,15 @@ PacketBuffer::PacketBuffer(uint8_t *data, int length, int rssi)
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy Constructor.
|
||||
* Copy Constructor.
|
||||
* Add ourselves as a reference to an existing PacketBuffer.
|
||||
*
|
||||
*
|
||||
* @param buffer The PacketBuffer to reference.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p();
|
||||
* PacketBuffer p2(i); // Refers to the same packet as p.
|
||||
* PacketBuffer p2(i); // Refers to the same packet as p.
|
||||
* @endcode
|
||||
*/
|
||||
PacketBuffer::PacketBuffer(const PacketBuffer &buffer)
|
||||
|
@ -76,8 +76,8 @@ PacketBuffer::PacketBuffer(const PacketBuffer &buffer)
|
|||
*
|
||||
* @param data The data with which to fill the buffer.
|
||||
* @param length The length of the buffer to create.
|
||||
* @param rssi The radio signal strength at the time this pacer was recieved.
|
||||
*
|
||||
* @param rssi The radio signal strength at the time this pacer was recieved.
|
||||
*
|
||||
*/
|
||||
void PacketBuffer::init(uint8_t *data, int length, int rssi)
|
||||
{
|
||||
|
@ -96,7 +96,7 @@ void PacketBuffer::init(uint8_t *data, int length, int rssi)
|
|||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
* Destructor.
|
||||
* Removes buffer resources held by the instance.
|
||||
*/
|
||||
PacketBuffer::~PacketBuffer()
|
||||
|
@ -105,7 +105,7 @@ PacketBuffer::~PacketBuffer()
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy assign operation.
|
||||
* Copy assign operation.
|
||||
*
|
||||
* Called when one PacketBuffer is assigned the value of another using the '=' operator.
|
||||
* Decrements our reference count and free up the buffer as necessary.
|
||||
|
@ -113,14 +113,14 @@ PacketBuffer::~PacketBuffer()
|
|||
* and increase its reference count.
|
||||
*
|
||||
* @param p The PacketBuffer to reference.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* uint8_t buf = {13,5,2};
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
*
|
||||
* p1 = p2;
|
||||
* p1 = p2;
|
||||
* @endcode
|
||||
*/
|
||||
PacketBuffer& PacketBuffer::operator = (const PacketBuffer &p)
|
||||
|
@ -136,14 +136,14 @@ PacketBuffer& PacketBuffer::operator = (const PacketBuffer &p)
|
|||
}
|
||||
|
||||
/**
|
||||
* Array access operation (read).
|
||||
* Array access operation (read).
|
||||
*
|
||||
* Called when a PacketBuffer is dereferenced with a [] operation.
|
||||
* Transparently map this through to the underlying payload for elegance of programming.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* uint8_t data = p1[0];
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -153,14 +153,14 @@ uint8_t PacketBuffer::operator [] (int i) const
|
|||
}
|
||||
|
||||
/**
|
||||
* Array access operation (modify).
|
||||
* Array access operation (modify).
|
||||
*
|
||||
* Called when a PacketBuffer is dereferenced with a [] operation.
|
||||
* Transparently map this through to the underlying payload for elegance of programming.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1[0] = 42;
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -176,13 +176,13 @@ uint8_t& PacketBuffer::operator [] (int i)
|
|||
*
|
||||
* @param p The PacketBuffer to test ourselves against.
|
||||
* @return true if this PacketBuffer is identical to the one supplied, false otherwise.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
*
|
||||
* uint8_t buf = {13,5,2};
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p2(buf, 3);
|
||||
*
|
||||
* if(p1 == p2) // will be true
|
||||
* uBit.display.scroll("same!");
|
||||
|
@ -193,7 +193,7 @@ bool PacketBuffer::operator== (const PacketBuffer& p)
|
|||
if (ptr == p.ptr)
|
||||
return true;
|
||||
else
|
||||
return (ptr->length == p.ptr->length && (memcmp(ptr->payload, p.ptr->payload, ptr->length)==0));
|
||||
return (ptr->length == p.ptr->length && (memcmp(ptr->payload, p.ptr->payload, ptr->length)==0));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -204,7 +204,7 @@ bool PacketBuffer::operator== (const PacketBuffer& p)
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setByte(0,255); // Sets the firts byte in the buffer to the value 255.
|
||||
* @endcode
|
||||
*/
|
||||
|
@ -229,7 +229,7 @@ int PacketBuffer::setByte(int position, uint8_t value)
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setByte(0,255); // Sets the firts byte in the buffer to the value 255.
|
||||
* p1.getByte(0); // Returns 255.
|
||||
* @endcode
|
||||
|
@ -240,7 +240,7 @@ int PacketBuffer::getByte(int position)
|
|||
return ptr->payload[position];
|
||||
else
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide an array containing the packet data.
|
||||
|
@ -252,16 +252,16 @@ uint8_t*PacketBuffer::getBytes()
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets number of bytes in this buffer
|
||||
* Gets number of bytes in this buffer
|
||||
* @return The size of the buffer in bytes.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.length(); // Returns 16.
|
||||
* @endcode
|
||||
*/
|
||||
int PacketBuffer::length()
|
||||
int PacketBuffer::length()
|
||||
{
|
||||
return ptr->length;
|
||||
}
|
||||
|
@ -269,15 +269,15 @@ int PacketBuffer::length()
|
|||
/**
|
||||
* Gets the received signal strength of this packet.
|
||||
*
|
||||
* @return The signal strength of the radio when this packet was received.
|
||||
*
|
||||
* @return The signal strength of the radio when this packet was received.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* p1.getRSSI();
|
||||
* PacketBuffer p1(16);
|
||||
* p1.getRSSI();
|
||||
* @endcode
|
||||
*/
|
||||
int PacketBuffer::getRSSI()
|
||||
int PacketBuffer::getRSSI()
|
||||
{
|
||||
return ptr->rssi;
|
||||
}
|
||||
|
@ -287,11 +287,11 @@ int PacketBuffer::getRSSI()
|
|||
*
|
||||
* Example:
|
||||
* @code
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setRSSI(37);
|
||||
* PacketBuffer p1(16);
|
||||
* p1.setRSSI(37);
|
||||
* @endcode
|
||||
*/
|
||||
void PacketBuffer::setRSSI(uint8_t rssi)
|
||||
void PacketBuffer::setRSSI(uint8_t rssi)
|
||||
{
|
||||
ptr->rssi = rssi;
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
AREA asm_func, CODE, READONLY
|
||||
|
||||
|
||||
; Export our context switching subroutine as a C function for use in mbed
|
||||
EXPORT swap_context
|
||||
EXPORT save_context
|
||||
EXPORT save_register_context
|
||||
EXPORT restore_register_context
|
||||
|
||||
|
||||
ALIGN
|
||||
|
||||
|
||||
; R0 Contains a pointer to the TCB of the fibre being scheduled out.
|
||||
; R1 Contains a pointer to the TCB of the fibre being scheduled in.
|
||||
; R2 Contains a pointer to the base of the stack of the fibre being scheduled out.
|
||||
|
@ -15,12 +15,12 @@
|
|||
|
||||
swap_context
|
||||
|
||||
; Write our core registers into the TCB
|
||||
; Write our core registers into the TCB
|
||||
; First, store the general registers
|
||||
|
||||
; Skip this is we're given a NULL parameter for the TCB
|
||||
CMP R0, #0
|
||||
BEQ store_context_complete
|
||||
BEQ store_context_complete
|
||||
|
||||
STR R0, [R0,#0]
|
||||
STR R1, [R0,#4]
|
||||
|
@ -30,8 +30,8 @@ swap_context
|
|||
STR R5, [R0,#20]
|
||||
STR R6, [R0,#24]
|
||||
STR R7, [R0,#28]
|
||||
|
||||
; Now the high general purpose registers
|
||||
|
||||
; Now the high general purpose registers
|
||||
MOV R4, R8
|
||||
STR R4, [R0,#32]
|
||||
MOV R4, R9
|
||||
|
@ -42,35 +42,35 @@ swap_context
|
|||
STR R4, [R0,#44]
|
||||
MOV R4, R12
|
||||
STR R4, [R0,#48]
|
||||
|
||||
|
||||
; Now the Stack and Link Register.
|
||||
; As this context is only intended for use with a fiber scheduler,
|
||||
; we don't need the PC.
|
||||
MOV R6, SP
|
||||
STR R6, [R0,#52]
|
||||
MOV R4, LR
|
||||
STR R4, [R0,#56]
|
||||
|
||||
store_context_complete
|
||||
STR R4, [R0,#56]
|
||||
|
||||
store_context_complete
|
||||
; Finally, Copy the stack. We do this to reduce RAM footprint, as stack is usually very small at the point
|
||||
; of scheduling, but we need a lot of capacity for interrupt handling and other functions.
|
||||
|
||||
; Skip this is we're given a NULL parameter for the stack.
|
||||
CMP R2, #0
|
||||
BEQ store_stack_complete
|
||||
BEQ store_stack_complete
|
||||
|
||||
LDR R4, [R0,#60] ; Load R4 with the fiber's defined stack_base.
|
||||
LDR R4, [R0,#60] ; Load R4 with the fiber's defined stack_base.
|
||||
store_stack
|
||||
SUBS R4, #4
|
||||
SUBS R2, #4
|
||||
|
||||
|
||||
LDR R5, [R4]
|
||||
STR R5, [R2]
|
||||
|
||||
|
||||
CMP R4, R6
|
||||
BNE store_stack
|
||||
|
||||
store_stack_complete
|
||||
store_stack_complete
|
||||
|
||||
;
|
||||
; Now page in the new context.
|
||||
|
@ -86,20 +86,20 @@ store_stack_complete
|
|||
|
||||
; Skip this is we're given a NULL parameter for the stack.
|
||||
CMP R3, #0
|
||||
BEQ restore_stack_complete
|
||||
BEQ restore_stack_complete
|
||||
|
||||
LDR R4, [R1,#60] ; Load R4 with the fiber's defined stack_base.
|
||||
|
||||
LDR R4, [R1,#60] ; Load R4 with the fiber's defined stack_base.
|
||||
|
||||
restore_stack
|
||||
SUBS R4, #4
|
||||
SUBS R3, #4
|
||||
|
||||
|
||||
LDR R5, [R3]
|
||||
STR R5, [R4]
|
||||
|
||||
|
||||
CMP R4, R6
|
||||
BNE restore_stack
|
||||
|
||||
|
||||
restore_stack_complete
|
||||
LDR R4, [R1, #48]
|
||||
MOV R12, R4
|
||||
|
@ -111,7 +111,7 @@ restore_stack_complete
|
|||
MOV R9, R4
|
||||
LDR R4, [R1, #32]
|
||||
MOV R8, R4
|
||||
|
||||
|
||||
LDR R7, [R1, #28]
|
||||
LDR R6, [R1, #24]
|
||||
LDR R5, [R1, #20]
|
||||
|
@ -120,8 +120,8 @@ restore_stack_complete
|
|||
LDR R2, [R1, #8]
|
||||
LDR R0, [R1, #0]
|
||||
LDR R1, [R1, #4]
|
||||
|
||||
; Return to caller (scheduler).
|
||||
|
||||
; Return to caller (scheduler).
|
||||
BX LR
|
||||
|
||||
|
||||
|
@ -130,7 +130,7 @@ restore_stack_complete
|
|||
|
||||
save_context
|
||||
|
||||
; Write our core registers into the TCB
|
||||
; Write our core registers into the TCB
|
||||
; First, store the general registers
|
||||
|
||||
STR R0, [R0,#0]
|
||||
|
@ -141,8 +141,8 @@ save_context
|
|||
STR R5, [R0,#20]
|
||||
STR R6, [R0,#24]
|
||||
STR R7, [R0,#28]
|
||||
|
||||
; Now the high general purpose registers
|
||||
|
||||
; Now the high general purpose registers
|
||||
MOV R4, R8
|
||||
STR R4, [R0,#32]
|
||||
MOV R4, R9
|
||||
|
@ -153,45 +153,45 @@ save_context
|
|||
STR R4, [R0,#44]
|
||||
MOV R4, R12
|
||||
STR R4, [R0,#48]
|
||||
|
||||
|
||||
; Now the Stack and Link Register.
|
||||
; As this context is only intended for use with a fiber scheduler,
|
||||
; we don't need the PC.
|
||||
MOV R6, SP
|
||||
STR R6, [R0,#52]
|
||||
MOV R4, LR
|
||||
STR R4, [R0,#56]
|
||||
|
||||
STR R4, [R0,#56]
|
||||
|
||||
; Finally, Copy the stack. We do this to reduce RAM footprint, as stackis usually very small at the point
|
||||
; of sceduling, but we need a lot of capacity for interrupt handling and other functions.
|
||||
|
||||
LDR R4, [R0,#60] ; Load R4 with the fiber's defined stack_base.
|
||||
LDR R4, [R0,#60] ; Load R4 with the fiber's defined stack_base.
|
||||
|
||||
store_stack1
|
||||
SUBS R4, #4
|
||||
SUBS R1, #4
|
||||
|
||||
|
||||
LDR R5, [R4]
|
||||
STR R5, [R1]
|
||||
|
||||
|
||||
CMP R4, R6
|
||||
BNE store_stack1
|
||||
|
||||
|
||||
; Restore scratch registers.
|
||||
|
||||
|
||||
LDR R7, [R0, #28]
|
||||
LDR R6, [R0, #24]
|
||||
LDR R5, [R0, #20]
|
||||
LDR R4, [R0, #16]
|
||||
|
||||
; Return to caller (scheduler).
|
||||
|
||||
; Return to caller (scheduler).
|
||||
BX LR
|
||||
|
||||
|
||||
|
||||
; R0 Contains a pointer to the TCB of the fiber to snapshot
|
||||
save_register_context
|
||||
|
||||
; Write our core registers into the TCB
|
||||
; Write our core registers into the TCB
|
||||
; First, store the general registers
|
||||
|
||||
STR R0, [R0,#0]
|
||||
|
@ -202,8 +202,8 @@ save_register_context
|
|||
STR R5, [R0,#20]
|
||||
STR R6, [R0,#24]
|
||||
STR R7, [R0,#28]
|
||||
|
||||
; Now the high general purpose registers
|
||||
|
||||
; Now the high general purpose registers
|
||||
MOV R4, R8
|
||||
STR R4, [R0,#32]
|
||||
MOV R4, R9
|
||||
|
@ -214,19 +214,19 @@ save_register_context
|
|||
STR R4, [R0,#44]
|
||||
MOV R4, R12
|
||||
STR R4, [R0,#48]
|
||||
|
||||
|
||||
; Now the Stack Pointer and Link Register.
|
||||
; As this context is only intended for use with a fiber scheduler,
|
||||
; we don't need the PC.
|
||||
MOV R4, SP
|
||||
STR R4, [R0,#52]
|
||||
MOV R4, LR
|
||||
STR R4, [R0,#56]
|
||||
|
||||
STR R4, [R0,#56]
|
||||
|
||||
; Restore scratch registers.
|
||||
LDR R4, [R0, #16]
|
||||
|
||||
; Return to caller (scheduler).
|
||||
|
||||
; Return to caller (scheduler).
|
||||
BX LR
|
||||
|
||||
|
||||
|
@ -241,7 +241,7 @@ restore_register_context
|
|||
LDR R4, [R0, #52]
|
||||
MOV SP, R4
|
||||
|
||||
; High registers...
|
||||
; High registers...
|
||||
LDR R4, [R0, #48]
|
||||
MOV R12, R4
|
||||
LDR R4, [R0, #44]
|
||||
|
@ -252,8 +252,8 @@ restore_register_context
|
|||
MOV R9, R4
|
||||
LDR R4, [R0, #32]
|
||||
MOV R8, R4
|
||||
|
||||
; Low registers...
|
||||
|
||||
; Low registers...
|
||||
LDR R7, [R0, #28]
|
||||
LDR R6, [R0, #24]
|
||||
LDR R5, [R0, #20]
|
||||
|
@ -262,8 +262,8 @@ restore_register_context
|
|||
LDR R2, [R0, #8]
|
||||
LDR R0, [R0, #0]
|
||||
LDR R1, [R0, #4]
|
||||
|
||||
; Return to caller (normally the scheduler).
|
||||
|
||||
; Return to caller (normally the scheduler).
|
||||
BX LR
|
||||
|
||||
ALIGN
|
||||
|
|
|
@ -3,13 +3,13 @@
|
|||
.thumb
|
||||
.text
|
||||
.align 2
|
||||
|
||||
|
||||
@ Export our context switching subroutine as a C function for use in mbed
|
||||
.global swap_context
|
||||
.global save_context
|
||||
.global save_register_context
|
||||
.global restore_register_context
|
||||
|
||||
|
||||
@ R0 Contains a pointer to the TCB of the fibre being scheduled out.
|
||||
@ R1 Contains a pointer to the TCB of the fibre being scheduled in.
|
||||
@ R2 Contains a pointer to the base of the stack of the fibre being scheduled out.
|
||||
|
@ -17,12 +17,12 @@
|
|||
|
||||
swap_context:
|
||||
|
||||
@ Write our core registers into the TCB
|
||||
@ Write our core registers into the TCB
|
||||
@ First, store the general registers
|
||||
|
||||
@ Skip this is we're given a NULL parameter for the TCB
|
||||
CMP R0, #0
|
||||
BEQ store_context_complete
|
||||
BEQ store_context_complete
|
||||
|
||||
STR R0, [R0,#0]
|
||||
STR R1, [R0,#4]
|
||||
|
@ -32,8 +32,8 @@ swap_context:
|
|||
STR R5, [R0,#20]
|
||||
STR R6, [R0,#24]
|
||||
STR R7, [R0,#28]
|
||||
|
||||
@ Now the high general purpose registers
|
||||
|
||||
@ Now the high general purpose registers
|
||||
MOV R4, R8
|
||||
STR R4, [R0,#32]
|
||||
MOV R4, R9
|
||||
|
@ -44,31 +44,31 @@ swap_context:
|
|||
STR R4, [R0,#44]
|
||||
MOV R4, R12
|
||||
STR R4, [R0,#48]
|
||||
|
||||
|
||||
@ Now the Stack and Link Register.
|
||||
@ As this context is only intended for use with a fiber scheduler,
|
||||
@ we don't need the PC.
|
||||
MOV R6, SP
|
||||
STR R6, [R0,#52]
|
||||
MOV R4, LR
|
||||
STR R4, [R0,#56]
|
||||
|
||||
STR R4, [R0,#56]
|
||||
|
||||
store_context_complete:
|
||||
@ Finally, Copy the stack. We do this to reduce RAM footprint, as stack is usually very small at the point
|
||||
@ of scheduling, but we need a lot of capacity for interrupt handling and other functions.
|
||||
|
||||
@ Skip this is we're given a NULL parameter for the stack.
|
||||
CMP R2, #0
|
||||
BEQ store_stack_complete
|
||||
BEQ store_stack_complete
|
||||
|
||||
LDR R4, [R0,#60] @ Load R4 with the fiber's defined stack_base.
|
||||
LDR R4, [R0,#60] @ Load R4 with the fiber's defined stack_base.
|
||||
store_stack:
|
||||
SUBS R4, #4
|
||||
SUBS R2, #4
|
||||
|
||||
|
||||
LDR R5, [R4]
|
||||
STR R5, [R2]
|
||||
|
||||
|
||||
CMP R4, R6
|
||||
BNE store_stack
|
||||
|
||||
|
@ -88,20 +88,20 @@ store_stack_complete:
|
|||
|
||||
@ Skip this is we're given a NULL parameter for the stack.
|
||||
CMP R3, #0
|
||||
BEQ restore_stack_complete
|
||||
BEQ restore_stack_complete
|
||||
|
||||
LDR R4, [R1,#60] @ Load R4 with the fiber's defined stack_base.
|
||||
|
||||
LDR R4, [R1,#60] @ Load R4 with the fiber's defined stack_base.
|
||||
|
||||
restore_stack:
|
||||
SUBS R4, #4
|
||||
SUBS R3, #4
|
||||
|
||||
|
||||
LDR R5, [R3]
|
||||
STR R5, [R4]
|
||||
|
||||
|
||||
CMP R4, R6
|
||||
BNE restore_stack
|
||||
|
||||
|
||||
restore_stack_complete:
|
||||
LDR R4, [R1, #48]
|
||||
MOV R12, R4
|
||||
|
@ -113,7 +113,7 @@ restore_stack_complete:
|
|||
MOV R9, R4
|
||||
LDR R4, [R1, #32]
|
||||
MOV R8, R4
|
||||
|
||||
|
||||
LDR R7, [R1, #28]
|
||||
LDR R6, [R1, #24]
|
||||
LDR R5, [R1, #20]
|
||||
|
@ -122,8 +122,8 @@ restore_stack_complete:
|
|||
LDR R2, [R1, #8]
|
||||
LDR R0, [R1, #0]
|
||||
LDR R1, [R1, #4]
|
||||
|
||||
@ Return to caller (scheduler).
|
||||
|
||||
@ Return to caller (scheduler).
|
||||
BX LR
|
||||
|
||||
|
||||
|
@ -132,7 +132,7 @@ restore_stack_complete:
|
|||
|
||||
save_context:
|
||||
|
||||
@ Write our core registers into the TCB
|
||||
@ Write our core registers into the TCB
|
||||
@ First, store the general registers
|
||||
|
||||
STR R0, [R0,#0]
|
||||
|
@ -143,8 +143,8 @@ save_context:
|
|||
STR R5, [R0,#20]
|
||||
STR R6, [R0,#24]
|
||||
STR R7, [R0,#28]
|
||||
|
||||
@ Now the high general purpose registers
|
||||
|
||||
@ Now the high general purpose registers
|
||||
MOV R4, R8
|
||||
STR R4, [R0,#32]
|
||||
MOV R4, R9
|
||||
|
@ -155,45 +155,45 @@ save_context:
|
|||
STR R4, [R0,#44]
|
||||
MOV R4, R12
|
||||
STR R4, [R0,#48]
|
||||
|
||||
|
||||
@ Now the Stack and Link Register.
|
||||
@ As this context is only intended for use with a fiber scheduler,
|
||||
@ we don't need the PC.
|
||||
MOV R6, SP
|
||||
STR R6, [R0,#52]
|
||||
MOV R4, LR
|
||||
STR R4, [R0,#56]
|
||||
|
||||
STR R4, [R0,#56]
|
||||
|
||||
@ Finally, Copy the stack. We do this to reduce RAM footprint, as stackis usually very small at the point
|
||||
@ of sceduling, but we need a lot of capacity for interrupt handling and other functions.
|
||||
|
||||
LDR R4, [R0,#60] @ Load R4 with the fiber's defined stack_base.
|
||||
LDR R4, [R0,#60] @ Load R4 with the fiber's defined stack_base.
|
||||
|
||||
store_stack1:
|
||||
SUBS R4, #4
|
||||
SUBS R1, #4
|
||||
|
||||
|
||||
LDR R5, [R4]
|
||||
STR R5, [R1]
|
||||
|
||||
|
||||
CMP R4, R6
|
||||
BNE store_stack1
|
||||
|
||||
|
||||
@ Restore scratch registers.
|
||||
|
||||
|
||||
LDR R7, [R0, #28]
|
||||
LDR R6, [R0, #24]
|
||||
LDR R5, [R0, #20]
|
||||
LDR R4, [R0, #16]
|
||||
|
||||
@ Return to caller (scheduler).
|
||||
|
||||
@ Return to caller (scheduler).
|
||||
BX LR
|
||||
|
||||
|
||||
|
||||
@ R0 Contains a pointer to the TCB of the fiber to snapshot
|
||||
save_register_context:
|
||||
|
||||
@ Write our core registers into the TCB
|
||||
@ Write our core registers into the TCB
|
||||
@ First, store the general registers
|
||||
|
||||
STR R0, [R0,#0]
|
||||
|
@ -204,8 +204,8 @@ save_register_context:
|
|||
STR R5, [R0,#20]
|
||||
STR R6, [R0,#24]
|
||||
STR R7, [R0,#28]
|
||||
|
||||
@ Now the high general purpose registers
|
||||
|
||||
@ Now the high general purpose registers
|
||||
MOV R4, R8
|
||||
STR R4, [R0,#32]
|
||||
MOV R4, R9
|
||||
|
@ -216,19 +216,19 @@ save_register_context:
|
|||
STR R4, [R0,#44]
|
||||
MOV R4, R12
|
||||
STR R4, [R0,#48]
|
||||
|
||||
|
||||
@ Now the Stack Pointer and Link Register.
|
||||
@ As this context is only intended for use with a fiber scheduler,
|
||||
@ we don't need the PC.
|
||||
MOV R4, SP
|
||||
STR R4, [R0,#52]
|
||||
MOV R4, LR
|
||||
STR R4, [R0,#56]
|
||||
|
||||
STR R4, [R0,#56]
|
||||
|
||||
@ Restore scratch registers.
|
||||
LDR R4, [R0, #16]
|
||||
|
||||
@ Return to caller (scheduler).
|
||||
|
||||
@ Return to caller (scheduler).
|
||||
BX LR
|
||||
|
||||
|
||||
|
@ -243,7 +243,7 @@ restore_register_context:
|
|||
LDR R4, [R0, #52]
|
||||
MOV SP, R4
|
||||
|
||||
@ High registers...
|
||||
@ High registers...
|
||||
LDR R4, [R0, #48]
|
||||
MOV R12, R4
|
||||
LDR R4, [R0, #44]
|
||||
|
@ -254,8 +254,8 @@ restore_register_context:
|
|||
MOV R9, R4
|
||||
LDR R4, [R0, #32]
|
||||
MOV R8, R4
|
||||
|
||||
@ Low registers...
|
||||
|
||||
@ Low registers...
|
||||
LDR R7, [R0, #28]
|
||||
LDR R6, [R0, #24]
|
||||
LDR R5, [R0, #20]
|
||||
|
@ -264,7 +264,7 @@ restore_register_context:
|
|||
LDR R2, [R0, #8]
|
||||
LDR R0, [R0, #0]
|
||||
LDR R1, [R0, #4]
|
||||
|
||||
@ Return to caller (normally the scheduler).
|
||||
|
||||
@ Return to caller (normally the scheduler).
|
||||
BX LR
|
||||
|
||||
|
|
|
@ -2,26 +2,26 @@
|
|||
* Class definition for the custom MicroBit Accelerometer Service.
|
||||
* Provides a BLE service to remotely read the state of the accelerometer, and configure its behaviour.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitAccelerometerService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the AccelerometerService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitAccelerometerService::MicroBitAccelerometerService(BLEDevice &_ble, MicroBitAccelerometer &_accelerometer, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble), accelerometer(_accelerometer)
|
||||
MicroBitAccelerometerService::MicroBitAccelerometerService(BLEDevice &_ble, MicroBitAccelerometer &_accelerometer, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble), accelerometer(_accelerometer)
|
||||
{
|
||||
// Create the data structures that represent each of our characteristics in Soft Device.
|
||||
GattCharacteristic accelerometerDataCharacteristic(MicroBitAccelerometerServiceDataUUID, (uint8_t *)accelerometerDataCharacteristicBuffer, 0,
|
||||
GattCharacteristic accelerometerDataCharacteristic(MicroBitAccelerometerServiceDataUUID, (uint8_t *)accelerometerDataCharacteristicBuffer, 0,
|
||||
sizeof(accelerometerDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
GattCharacteristic accelerometerPeriodCharacteristic(MicroBitAccelerometerServicePeriodUUID, (uint8_t *)&accelerometerPeriodCharacteristicBuffer, 0,
|
||||
sizeof(accelerometerPeriodCharacteristicBuffer),
|
||||
GattCharacteristic accelerometerPeriodCharacteristic(MicroBitAccelerometerServicePeriodUUID, (uint8_t *)&accelerometerPeriodCharacteristicBuffer, 0,
|
||||
sizeof(accelerometerPeriodCharacteristicBuffer),
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
|
||||
// Initialise our characteristic values.
|
||||
|
@ -53,7 +53,7 @@ MicroBitAccelerometerService::MicroBitAccelerometerService(BLEDevice &_ble, Micr
|
|||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void MicroBitAccelerometerService::onDataWritten(const GattWriteCallbackParams *params)
|
||||
{
|
||||
{
|
||||
if (params->handle == accelerometerPeriodCharacteristicHandle && params->len >= sizeof(accelerometerPeriodCharacteristicBuffer))
|
||||
{
|
||||
accelerometerPeriodCharacteristicBuffer = *((uint16_t *)params->data);
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
* Class definition for the custom MicroBit Button Service.
|
||||
* Provides a BLE service to remotely read the state of each button, and configure its behaviour.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitButtonService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the ButtonService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitButtonService::MicroBitButtonService(BLEDevice &_ble, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble)
|
||||
MicroBitButtonService::MicroBitButtonService(BLEDevice &_ble, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble)
|
||||
{
|
||||
// Create the data structures that represent each of our characteristics in Soft Device.
|
||||
GattCharacteristic buttonADataCharacteristic(MicroBitButtonAServiceDataUUID, (uint8_t *)&buttonADataCharacteristicBuffer, 0,
|
||||
GattCharacteristic buttonADataCharacteristic(MicroBitButtonAServiceDataUUID, (uint8_t *)&buttonADataCharacteristicBuffer, 0,
|
||||
sizeof(buttonADataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
GattCharacteristic buttonBDataCharacteristic(MicroBitButtonBServiceDataUUID, (uint8_t *)&buttonADataCharacteristicBuffer, 0,
|
||||
GattCharacteristic buttonBDataCharacteristic(MicroBitButtonBServiceDataUUID, (uint8_t *)&buttonADataCharacteristicBuffer, 0,
|
||||
sizeof(buttonADataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
|
||||
|
@ -31,7 +31,7 @@ MicroBitButtonService::MicroBitButtonService(BLEDevice &_ble, MicroBitMessageBus
|
|||
// Set default security requirements
|
||||
buttonADataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
buttonBDataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
|
||||
|
||||
GattCharacteristic *characteristics[] = {&buttonADataCharacteristic, &buttonBDataCharacteristic};
|
||||
GattService service(MicroBitButtonServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
|
||||
|
||||
|
|
|
@ -2,20 +2,20 @@
|
|||
* Class definition for a MicroBit BLE Event Service.
|
||||
* Provides a BLE gateway onto the MicroBit Message Bus.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
#include "ExternalEvents.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the EventService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitEventService::MicroBitEventService(BLEDevice &_ble, MicroBitMessageBus &_messageBus) :
|
||||
ble(_ble),messageBus(_messageBus)
|
||||
MicroBitEventService::MicroBitEventService(BLEDevice &_ble, MicroBitMessageBus &_messageBus) :
|
||||
ble(_ble),messageBus(_messageBus)
|
||||
{
|
||||
GattCharacteristic microBitEventCharacteristic(MicroBitEventServiceMicroBitEventCharacteristicUUID, (uint8_t *)µBitEventBuffer, 0, sizeof(EventServiceEvent),
|
||||
GattCharacteristic microBitEventCharacteristic(MicroBitEventServiceMicroBitEventCharacteristicUUID, (uint8_t *)µBitEventBuffer, 0, sizeof(EventServiceEvent),
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
GattCharacteristic clientEventCharacteristic(MicroBitEventServiceClientEventCharacteristicUUID, (uint8_t *)&clientEventBuffer, 0, sizeof(EventServiceEvent),
|
||||
|
@ -29,11 +29,11 @@ MicroBitEventService::MicroBitEventService(BLEDevice &_ble, MicroBitMessageBus &
|
|||
|
||||
clientEventBuffer.type = 0x00;
|
||||
clientEventBuffer.reason = 0x00;
|
||||
|
||||
|
||||
microBitEventBuffer = microBitRequirementsBuffer = clientRequirementsBuffer = clientEventBuffer;
|
||||
|
||||
messageBusListenerOffset = 0;
|
||||
|
||||
|
||||
// Set default security requirements
|
||||
microBitEventCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
clientEventCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
|
@ -59,12 +59,12 @@ MicroBitEventService::MicroBitEventService(BLEDevice &_ble, MicroBitMessageBus &
|
|||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void MicroBitEventService::onDataWritten(const GattWriteCallbackParams *params)
|
||||
{
|
||||
int len = params->len;
|
||||
{
|
||||
int len = params->len;
|
||||
EventServiceEvent *e = (EventServiceEvent *)params->data;
|
||||
|
||||
|
||||
if (params->handle == clientEventCharacteristicHandle) {
|
||||
|
||||
|
||||
// Read and fire all events...
|
||||
while (len >= 4)
|
||||
{
|
||||
|
@ -94,23 +94,23 @@ void MicroBitEventService::onDataWritten(const GattWriteCallbackParams *params)
|
|||
void MicroBitEventService::onMicroBitEvent(MicroBitEvent evt)
|
||||
{
|
||||
EventServiceEvent *e = µBitEventBuffer;
|
||||
|
||||
|
||||
if (ble.getGapState().connected) {
|
||||
e->type = evt.source;
|
||||
e->reason = evt.value;
|
||||
|
||||
|
||||
ble.gattServer().notify(microBitEventCharacteristicHandle, (const uint8_t *)e, sizeof(EventServiceEvent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Periodic callback from MicroBit scheduler.
|
||||
* If we're no longer connected, remove any registered Message Bus listeners.
|
||||
*/
|
||||
*/
|
||||
void MicroBitEventService::idleTick()
|
||||
{
|
||||
if (!ble.getGapState().connected && messageBusListenerOffset >0) {
|
||||
messageBusListenerOffset = 0;
|
||||
messageBusListenerOffset = 0;
|
||||
messageBus.ignore(MICROBIT_ID_ANY, MICROBIT_EVT_ANY, this, &MicroBitEventService::onMicroBitEvent);
|
||||
}
|
||||
}
|
||||
|
@ -118,14 +118,14 @@ void MicroBitEventService::idleTick()
|
|||
/**
|
||||
* read callback on data characteristic.
|
||||
* reads all the pins marked as inputs, and updates the data stored in the BLE stack.
|
||||
*/
|
||||
*/
|
||||
void MicroBitEventService::onRequirementsRead(GattReadAuthCallbackParams *params)
|
||||
{
|
||||
if (params->handle == microBitRequirementsCharacteristic->getValueHandle())
|
||||
{
|
||||
// Walk through the lsit of message bus listeners.
|
||||
// We send one at a time, and our client can keep reading from this characterisitic until we return an emtpy value.
|
||||
MicroBitListener *l = messageBus.elementAt(messageBusListenerOffset++);
|
||||
MicroBitListener *l = messageBus.elementAt(messageBusListenerOffset++);
|
||||
|
||||
if (l != NULL)
|
||||
{
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
* Class definition for the custom MicroBit IOPin Service.
|
||||
* Provides a BLE service to remotely read the state of the ioPin, and configure its behaviour.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitIOPinService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the IOPinService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitIOPinService::MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io) :
|
||||
MicroBitIOPinService::MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io) :
|
||||
ble(_ble), io(_io)
|
||||
{
|
||||
// Create the AD characteristic, that defines whether each pin is treated as analogue or digital
|
||||
GattCharacteristic ioPinServiceADCharacteristic(MicroBitIOPinServiceADConfigurationUUID, (uint8_t *)&ioPinServiceADCharacteristicBuffer, 0, sizeof(ioPinServiceADCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
GattCharacteristic ioPinServiceADCharacteristic(MicroBitIOPinServiceADConfigurationUUID, (uint8_t *)&ioPinServiceADCharacteristicBuffer, 0, sizeof(ioPinServiceADCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
|
||||
// Create the IO characteristic, that defines whether each pin is treated as input or output
|
||||
GattCharacteristic ioPinServiceIOCharacteristic(MicroBitIOPinServiceIOConfigurationUUID, (uint8_t *)&ioPinServiceIOCharacteristicBuffer, 0, sizeof(ioPinServiceIOCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
GattCharacteristic ioPinServiceIOCharacteristic(MicroBitIOPinServiceIOConfigurationUUID, (uint8_t *)&ioPinServiceIOCharacteristicBuffer, 0, sizeof(ioPinServiceIOCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
|
||||
// Create the Data characteristic, that allows the actual read and write operations.
|
||||
ioPinServiceDataCharacteristic = new GattCharacteristic(MicroBitIOPinServiceDataUUID, (uint8_t *)ioPinServiceDataCharacteristicBuffer, 0, sizeof(ioPinServiceDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
@ -35,7 +35,7 @@ MicroBitIOPinService::MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io) :
|
|||
ioPinServiceADCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
ioPinServiceIOCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
ioPinServiceDataCharacteristic->requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
|
||||
|
||||
GattCharacteristic *characteristics[] = {&ioPinServiceADCharacteristic, &ioPinServiceIOCharacteristic, ioPinServiceDataCharacteristic};
|
||||
GattService service(MicroBitIOPinServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
|
||||
|
||||
|
@ -99,8 +99,8 @@ int MicroBitIOPinService::isOutput(int i)
|
|||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void MicroBitIOPinService::onDataWritten(const GattWriteCallbackParams *params)
|
||||
{
|
||||
// Check for writes to the IO configuration characteristic
|
||||
{
|
||||
// Check for writes to the IO configuration characteristic
|
||||
if (params->handle == ioPinServiceIOCharacteristicHandle && params->len >= sizeof(ioPinServiceIOCharacteristicBuffer))
|
||||
{
|
||||
uint32_t *value = (uint32_t *)params->data;
|
||||
|
@ -122,7 +122,7 @@ void MicroBitIOPinService::onDataWritten(const GattWriteCallbackParams *params)
|
|||
}
|
||||
}
|
||||
|
||||
// Check for writes to the IO configuration characteristic
|
||||
// Check for writes to the IO configuration characteristic
|
||||
if (params->handle == ioPinServiceADCharacteristicHandle && params->len >= sizeof(ioPinServiceADCharacteristicBuffer))
|
||||
{
|
||||
uint32_t *value = (uint32_t *)params->data;
|
||||
|
@ -172,7 +172,7 @@ void MicroBitIOPinService::onDataWritten(const GattWriteCallbackParams *params)
|
|||
/**
|
||||
* read callback on data characteristic.
|
||||
* reads all the pins marked as inputs, and updates the data stored in the BLE stack.
|
||||
*/
|
||||
*/
|
||||
void MicroBitIOPinService::onDataRead(GattReadAuthCallbackParams *params)
|
||||
{
|
||||
if (params->handle == ioPinServiceDataCharacteristic->getValueHandle())
|
||||
|
@ -195,8 +195,8 @@ void MicroBitIOPinService::onDataRead(GattReadAuthCallbackParams *params)
|
|||
//value = MicroBitIOPins[i]->getAnalogValue();
|
||||
|
||||
ioPinServiceIOData[i] = value;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].pin = i;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].value = value;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].pin = i;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].value = value;
|
||||
|
||||
pairs++;
|
||||
|
||||
|
@ -214,8 +214,8 @@ void MicroBitIOPinService::onDataRead(GattReadAuthCallbackParams *params)
|
|||
|
||||
/**
|
||||
* Periodic callback from MicroBit scheduler.
|
||||
* Check if any of the pins we're watching need updating. Apply a BLE NOTIFY if so...
|
||||
*/
|
||||
* Check if any of the pins we're watching need updating. Apply a BLE NOTIFY if so...
|
||||
*/
|
||||
void MicroBitIOPinService::idleTick()
|
||||
{
|
||||
// If we're not we're connected, then there's nothing to do...
|
||||
|
@ -243,8 +243,8 @@ void MicroBitIOPinService::idleTick()
|
|||
{
|
||||
ioPinServiceIOData[i] = value;
|
||||
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].pin = i;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].value = value;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].pin = i;
|
||||
ioPinServiceDataCharacteristicBuffer[pairs].value = value;
|
||||
|
||||
pairs++;
|
||||
|
||||
|
@ -276,25 +276,25 @@ const uint8_t MicroBitIOPinServiceDataUUID[] = {
|
|||
};
|
||||
|
||||
/*
|
||||
MicroBitPin * const MicroBitIOPins[] = {
|
||||
&uBit.io.P0,
|
||||
&uBit.io.P1,
|
||||
MicroBitPin * const MicroBitIOPins[] = {
|
||||
&uBit.io.P0,
|
||||
&uBit.io.P1,
|
||||
&uBit.io.P2,
|
||||
&uBit.io.P3,
|
||||
&uBit.io.P4,
|
||||
&uBit.io.P3,
|
||||
&uBit.io.P4,
|
||||
&uBit.io.P5,
|
||||
&uBit.io.P6,
|
||||
&uBit.io.P7,
|
||||
&uBit.io.P6,
|
||||
&uBit.io.P7,
|
||||
&uBit.io.P8,
|
||||
&uBit.io.P9,
|
||||
&uBit.io.P10,
|
||||
&uBit.io.P9,
|
||||
&uBit.io.P10,
|
||||
&uBit.io.P11,
|
||||
&uBit.io.P12,
|
||||
&uBit.io.P13,
|
||||
&uBit.io.P12,
|
||||
&uBit.io.P13,
|
||||
&uBit.io.P14,
|
||||
&uBit.io.P15,
|
||||
&uBit.io.P16,
|
||||
&uBit.io.P19,
|
||||
&uBit.io.P15,
|
||||
&uBit.io.P16,
|
||||
&uBit.io.P19,
|
||||
&uBit.io.P20
|
||||
};
|
||||
*/
|
||||
|
|
|
@ -2,36 +2,36 @@
|
|||
* Class definition for the custom MicroBit LED Service.
|
||||
* Provides a BLE service to remotely read and write the state of the LED display.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitLEDService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the LEDService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitLEDService::MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_display) :
|
||||
MicroBitLEDService::MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_display) :
|
||||
ble(_ble), display(_display),
|
||||
matrixCharacteristic(MicroBitLEDServiceMatrixUUID, (uint8_t *)&matrixCharacteristicBuffer, 0, sizeof(matrixCharacteristicBuffer),
|
||||
matrixCharacteristic(MicroBitLEDServiceMatrixUUID, (uint8_t *)&matrixCharacteristicBuffer, 0, sizeof(matrixCharacteristicBuffer),
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
|
||||
{
|
||||
// Create the data structures that represent each of our characteristics in Soft Device.
|
||||
GattCharacteristic textCharacteristic(MicroBitLEDServiceTextUUID, (uint8_t *)textCharacteristicBuffer, 0, MICROBIT_BLE_MAXIMUM_SCROLLTEXT,
|
||||
GattCharacteristic textCharacteristic(MicroBitLEDServiceTextUUID, (uint8_t *)textCharacteristicBuffer, 0, MICROBIT_BLE_MAXIMUM_SCROLLTEXT,
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
|
||||
GattCharacteristic scrollingSpeedCharacteristic(MicroBitLEDServiceScrollingSpeedUUID, (uint8_t *)&scrollingSpeedCharacteristicBuffer, 0,
|
||||
GattCharacteristic scrollingSpeedCharacteristic(MicroBitLEDServiceScrollingSpeedUUID, (uint8_t *)&scrollingSpeedCharacteristicBuffer, 0,
|
||||
sizeof(scrollingSpeedCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
|
||||
|
||||
// Initialise our characteristic values.
|
||||
memclr(matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer));
|
||||
textCharacteristicBuffer[0] = 0;
|
||||
scrollingSpeedCharacteristicBuffer = MICROBIT_DEFAULT_SCROLL_SPEED;
|
||||
|
||||
|
||||
matrixCharacteristic.setReadAuthorizationCallback(this, &MicroBitLEDService::onDataRead);
|
||||
|
||||
|
||||
// Set default security requirements
|
||||
matrixCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
textCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
|
||||
|
@ -57,17 +57,17 @@ MicroBitLEDService::MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_displa
|
|||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void MicroBitLEDService::onDataWritten(const GattWriteCallbackParams *params)
|
||||
{
|
||||
{
|
||||
uint8_t *data = (uint8_t *)params->data;
|
||||
|
||||
if (params->handle == matrixCharacteristicHandle && params->len > 0 && params->len < 6)
|
||||
{
|
||||
for (int y=0; y<params->len; y++)
|
||||
for (int x=0; x<5; x++)
|
||||
for (int y=0; y<params->len; y++)
|
||||
for (int x=0; x<5; x++)
|
||||
display.image.setPixelValue(x, y, (data[y] & (0x01 << (4-x))) ? 255 : 0);
|
||||
}
|
||||
|
||||
else if (params->handle == textCharacteristicHandle)
|
||||
else if (params->handle == textCharacteristicHandle)
|
||||
{
|
||||
// Create a ManagedString representation from the UTF8 data.
|
||||
// We do this explicitly to control the length (in case the string is not NULL terminated!)
|
||||
|
@ -77,7 +77,7 @@ void MicroBitLEDService::onDataWritten(const GattWriteCallbackParams *params)
|
|||
display.scrollAsync(s, (int) scrollingSpeedCharacteristicBuffer);
|
||||
}
|
||||
|
||||
else if (params->handle == scrollingSpeedCharacteristicHandle && params->len >= sizeof(scrollingSpeedCharacteristicBuffer))
|
||||
else if (params->handle == scrollingSpeedCharacteristicHandle && params->len >= sizeof(scrollingSpeedCharacteristicBuffer))
|
||||
{
|
||||
// Read the speed requested, and store it locally.
|
||||
// We use this as the speed for all scroll operations subsquently initiated from BLE.
|
||||
|
@ -92,11 +92,11 @@ void MicroBitLEDService::onDataRead(GattReadAuthCallbackParams *params)
|
|||
{
|
||||
if (params->handle == matrixCharacteristicHandle)
|
||||
{
|
||||
for (int y=0; y<5; y++)
|
||||
for (int y=0; y<5; y++)
|
||||
{
|
||||
matrixCharacteristicBuffer[y] = 0;
|
||||
|
||||
for (int x=0; x<5; x++)
|
||||
for (int x=0; x<5; x++)
|
||||
{
|
||||
if (display.image.getPixelValue(x, y))
|
||||
matrixCharacteristicBuffer[y] |= 0x01 << (4-x);
|
||||
|
|
|
@ -2,29 +2,29 @@
|
|||
* Class definition for the custom MicroBit Magnetometer Service.
|
||||
* Provides a BLE service to remotely read the state of the magnetometer, and configure its behaviour.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitMagnetometerService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the MagnetometerService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroBitCompass &_compass, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble), compass(_compass)
|
||||
MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroBitCompass &_compass, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble), compass(_compass)
|
||||
{
|
||||
// Create the data structures that represent each of our characteristics in Soft Device.
|
||||
GattCharacteristic magnetometerDataCharacteristic(MicroBitMagnetometerServiceDataUUID, (uint8_t *)magnetometerDataCharacteristicBuffer, 0,
|
||||
GattCharacteristic magnetometerDataCharacteristic(MicroBitMagnetometerServiceDataUUID, (uint8_t *)magnetometerDataCharacteristicBuffer, 0,
|
||||
sizeof(magnetometerDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
GattCharacteristic magnetometerBearingCharacteristic(MicroBitMagnetometerServiceBearingUUID, (uint8_t *)&magnetometerBearingCharacteristicBuffer, 0,
|
||||
GattCharacteristic magnetometerBearingCharacteristic(MicroBitMagnetometerServiceBearingUUID, (uint8_t *)&magnetometerBearingCharacteristicBuffer, 0,
|
||||
sizeof(magnetometerBearingCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
GattCharacteristic magnetometerPeriodCharacteristic(MicroBitMagnetometerServicePeriodUUID, (uint8_t *)&magnetometerPeriodCharacteristicBuffer, 0,
|
||||
sizeof(magnetometerPeriodCharacteristicBuffer),
|
||||
GattCharacteristic magnetometerPeriodCharacteristic(MicroBitMagnetometerServicePeriodUUID, (uint8_t *)&magnetometerPeriodCharacteristicBuffer, 0,
|
||||
sizeof(magnetometerPeriodCharacteristicBuffer),
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
|
||||
// Initialise our characteristic values.
|
||||
|
@ -61,7 +61,7 @@ MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroB
|
|||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void MicroBitMagnetometerService::onDataWritten(const GattWriteCallbackParams *params)
|
||||
{
|
||||
{
|
||||
if (params->handle == magnetometerPeriodCharacteristicHandle && params->len >= sizeof(magnetometerPeriodCharacteristicBuffer))
|
||||
{
|
||||
magnetometerPeriodCharacteristicBuffer = *((uint16_t *)params->data);
|
||||
|
@ -96,7 +96,7 @@ void MicroBitMagnetometerService::magnetometerUpdate(MicroBitEvent)
|
|||
/**
|
||||
* Sample Period Change Needed callback.
|
||||
* Reconfiguring the magnetometer can to a REALLY long time (sometimes even seconds to complete)
|
||||
* So we do this in the background when necessary, through this event handler.
|
||||
* So we do this in the background when necessary, through this event handler.
|
||||
*/
|
||||
void MicroBitMagnetometerService::samplePeriodUpdateNeeded(MicroBitEvent)
|
||||
{
|
||||
|
|
|
@ -24,7 +24,7 @@ MicroBitRadioDatagram::MicroBitRadioDatagram(MicroBitRadio &r) : radio(r)
|
|||
|
||||
/**
|
||||
* Retreives packet payload data into the given buffer.
|
||||
* If a data packet is already available, then it will be returned immediately to the caller.
|
||||
* If a data packet is already available, then it will be returned immediately to the caller.
|
||||
* If no data is available the EmptyString is returned, then MICROBIT_INVALID_PARAMETER is returned.
|
||||
*
|
||||
* @param buf A pointer to a valid memory location where the received data is to be stored.
|
||||
|
@ -83,7 +83,7 @@ int MicroBitRadioDatagram::send(uint8_t *buffer, int len)
|
|||
{
|
||||
if (buffer == NULL || len < 0 || len > MICROBIT_RADIO_MAX_PACKET_SIZE + MICROBIT_RADIO_HEADER_SIZE - 1)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
|
||||
FrameBuffer buf;
|
||||
|
||||
buf.length = len + MICROBIT_RADIO_HEADER_SIZE - 1;
|
||||
|
@ -136,7 +136,7 @@ void MicroBitRadioDatagram::packetReceived()
|
|||
{
|
||||
delete packet;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
p->next = packet;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ MicroBitRadioEvent::MicroBitRadioEvent(MicroBitRadio &r) : radio(r)
|
|||
|
||||
/**
|
||||
* Associates the given MicroBitEvent with the radio channel.
|
||||
* Once registered, all events matching the given registration sent to this micro:bit's
|
||||
* Once registered, all events matching the given registration sent to this micro:bit's
|
||||
* default EventBus will be automatically retrasmitted on the radio.
|
||||
*
|
||||
* @param id The ID of the events to register.
|
||||
|
@ -42,7 +42,7 @@ int MicroBitRadioEvent::listen(uint16_t id, uint16_t value)
|
|||
|
||||
/**
|
||||
* Associates the given MicroBitEvent events with the radio channel.
|
||||
* Once registered, all events matching the given registration sent to the given
|
||||
* Once registered, all events matching the given registration sent to the given
|
||||
* MessageBus will be automaticlaly retrasmitted on the radio.
|
||||
*
|
||||
* @param id The ID of the events to register.
|
||||
|
@ -88,7 +88,7 @@ int MicroBitRadioEvent::ignore(uint16_t id, uint16_t value, EventModel &eventBus
|
|||
|
||||
/**
|
||||
* Protocol handler callback. This is called when the radio receives a packet marked as an event
|
||||
* This function process this packet, and fires the event contained inside onto the local MessageBus.
|
||||
* This function process this packet, and fires the event contained inside onto the local MessageBus.
|
||||
*/
|
||||
void MicroBitRadioEvent::packetReceived()
|
||||
{
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
* Class definition for the custom MicroBit Temperature Service.
|
||||
* Provides a BLE service to remotely read the state of the temperature, and configure its behaviour.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
#include "ble/UUID.h"
|
||||
|
||||
#include "MicroBitTemperatureService.h"
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor.
|
||||
* Create a representation of the TemperatureService
|
||||
* @param _ble The instance of a BLE device that we're running on.
|
||||
*/
|
||||
MicroBitTemperatureService::MicroBitTemperatureService(BLEDevice &_ble, MicroBitThermometer &_thermometer, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble), thermometer(_thermometer)
|
||||
MicroBitTemperatureService::MicroBitTemperatureService(BLEDevice &_ble, MicroBitThermometer &_thermometer, MicroBitMessageBus &messageBus) :
|
||||
ble(_ble), thermometer(_thermometer)
|
||||
{
|
||||
// Create the data structures that represent each of our characteristics in Soft Device.
|
||||
GattCharacteristic temperatureDataCharacteristic(MicroBitTemperatureServiceDataUUID, (uint8_t *)&temperatureDataCharacteristicBuffer, 0,
|
||||
GattCharacteristic temperatureDataCharacteristic(MicroBitTemperatureServiceDataUUID, (uint8_t *)&temperatureDataCharacteristicBuffer, 0,
|
||||
sizeof(temperatureDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
|
||||
|
||||
GattCharacteristic temperaturePeriodCharacteristic(MicroBitTemperatureServicePeriodUUID, (uint8_t *)&temperaturePeriodCharacteristicBuffer, 0,
|
||||
GattCharacteristic temperaturePeriodCharacteristic(MicroBitTemperatureServicePeriodUUID, (uint8_t *)&temperaturePeriodCharacteristicBuffer, 0,
|
||||
sizeof(temperaturePeriodCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
|
||||
|
||||
// Initialise our characteristic values.
|
||||
|
@ -62,7 +62,7 @@ void MicroBitTemperatureService::temperatureUpdate(MicroBitEvent)
|
|||
* Callback. Invoked when any of our attributes are written via BLE.
|
||||
*/
|
||||
void MicroBitTemperatureService::onDataWritten(const GattWriteCallbackParams *params)
|
||||
{
|
||||
{
|
||||
if (params->handle == temperaturePeriodCharacteristicHandle && params->len >= sizeof(temperaturePeriodCharacteristicBuffer))
|
||||
{
|
||||
temperaturePeriodCharacteristicBuffer = *((uint16_t *)params->data);
|
||||
|
|
Loading…
Reference in New Issue