microbit: Restructure of repo

- Introduction of core, drivers, types and bluetooth folders in source and inc
- Added all inc subfolfers to the search path
- Removed MicroBit.cpp and MicroBit.h (yay!)
master
Joe Finney 7 years ago
parent 2b684c6204
commit 06c3c95609
  1. 471
      inc/MicroBit.h
  2. 0
      inc/bluetooth/ExternalEvents.h
  3. 0
      inc/bluetooth/MESEvents.h
  4. 0
      inc/bluetooth/MicroBitAccelerometerService.h
  5. 0
      inc/bluetooth/MicroBitBLEManager.h
  6. 0
      inc/bluetooth/MicroBitButtonService.h
  7. 0
      inc/bluetooth/MicroBitDFUService.h
  8. 0
      inc/bluetooth/MicroBitEventService.h
  9. 0
      inc/bluetooth/MicroBitIOPinService.h
  10. 0
      inc/bluetooth/MicroBitLEDService.h
  11. 0
      inc/bluetooth/MicroBitMagnetometerService.h
  12. 0
      inc/bluetooth/MicroBitTemperatureService.h
  13. 0
      inc/core/ErrorNo.h
  14. 0
      inc/core/EventModel.h
  15. 0
      inc/core/MemberFunctionCallback.h
  16. 0
      inc/core/MicroBitCompat.h
  17. 0
      inc/core/MicroBitComponent.h
  18. 0
      inc/core/MicroBitConfig.h
  19. 0
      inc/core/MicroBitDevice.h
  20. 0
      inc/core/MicroBitFiber.h
  21. 0
      inc/core/MicroBitFont.h
  22. 0
      inc/core/MicroBitHeapAllocator.h
  23. 0
      inc/core/MicroBitListener.h
  24. 0
      inc/core/MicroBitSystemTimer.h
  25. 0
      inc/drivers/DynamicPwm.h
  26. 0
      inc/drivers/MicroBitAccelerometer.h
  27. 0
      inc/drivers/MicroBitButton.h
  28. 0
      inc/drivers/MicroBitCompass.h
  29. 0
      inc/drivers/MicroBitCompassCalibrator.h
  30. 0
      inc/drivers/MicroBitDisplay.h
  31. 0
      inc/drivers/MicroBitI2C.h
  32. 0
      inc/drivers/MicroBitIO.h
  33. 0
      inc/drivers/MicroBitLightSensor.h
  34. 0
      inc/drivers/MicroBitMatrixMaps.h
  35. 0
      inc/drivers/MicroBitMessageBus.h
  36. 0
      inc/drivers/MicroBitMultiButton.h
  37. 0
      inc/drivers/MicroBitPin.h
  38. 0
      inc/drivers/MicroBitRadio.h
  39. 0
      inc/drivers/MicroBitRadioDatagram.h
  40. 0
      inc/drivers/MicroBitRadioEvent.h
  41. 0
      inc/drivers/MicroBitSerial.h
  42. 0
      inc/drivers/MicroBitStorage.h
  43. 0
      inc/drivers/MicroBitThermometer.h
  44. 0
      inc/types/ManagedString.h
  45. 0
      inc/types/ManagedType.h
  46. 0
      inc/types/Matrix4.h
  47. 0
      inc/types/MicroBitCoordinateSystem.h
  48. 0
      inc/types/MicroBitEvent.h
  49. 0
      inc/types/MicroBitImage.h
  50. 0
      inc/types/PacketBuffer.h
  51. 0
      inc/types/RefCounted.h
  52. 5
      module.json
  53. 87
      source/CMakeLists.txt
  54. 203
      source/MicroBit.cpp
  55. 29
      source/MicroBitSuperMain.cpp
  56. 0
      source/bluetooth/MicroBitAccelerometerService.cpp
  57. 0
      source/bluetooth/MicroBitBLEManager.cpp
  58. 0
      source/bluetooth/MicroBitButtonService.cpp
  59. 0
      source/bluetooth/MicroBitDFUService.cpp
  60. 0
      source/bluetooth/MicroBitEventService.cpp
  61. 0
      source/bluetooth/MicroBitIOPinService.cpp
  62. 0
      source/bluetooth/MicroBitLEDService.cpp
  63. 0
      source/bluetooth/MicroBitMagnetometerService.cpp
  64. 0
      source/bluetooth/MicroBitTemperatureService.cpp
  65. 0
      source/core/MemberFunctionCallback.cpp
  66. 0
      source/core/MicroBitCompat.cpp
  67. 0
      source/core/MicroBitDevice.cpp
  68. 0
      source/core/MicroBitFiber.cpp
  69. 0
      source/core/MicroBitFont.cpp
  70. 0
      source/core/MicroBitHeapAllocator.cpp
  71. 0
      source/core/MicroBitListener.cpp
  72. 0
      source/core/MicroBitSystemTimer.cpp
  73. 0
      source/drivers/DynamicPwm.cpp
  74. 0
      source/drivers/MicroBitAccelerometer.cpp
  75. 0
      source/drivers/MicroBitButton.cpp
  76. 0
      source/drivers/MicroBitCompass.cpp
  77. 0
      source/drivers/MicroBitCompassCalibrator.cpp
  78. 0
      source/drivers/MicroBitDisplay.cpp
  79. 0
      source/drivers/MicroBitI2C.cpp
  80. 0
      source/drivers/MicroBitIO.cpp
  81. 0
      source/drivers/MicroBitLightSensor.cpp
  82. 0
      source/drivers/MicroBitMessageBus.cpp
  83. 0
      source/drivers/MicroBitMultiButton.cpp
  84. 0
      source/drivers/MicroBitPin.cpp
  85. 0
      source/drivers/MicroBitRadio.cpp
  86. 0
      source/drivers/MicroBitRadioDatagram.cpp
  87. 0
      source/drivers/MicroBitRadioEvent.cpp
  88. 0
      source/drivers/MicroBitSerial.cpp
  89. 0
      source/drivers/MicroBitStorage.cpp
  90. 0
      source/drivers/MicroBitThermometer.cpp
  91. 0
      source/types/ManagedString.cpp
  92. 0
      source/types/Matrix4.cpp
  93. 0
      source/types/MicroBitEvent.cpp
  94. 0
      source/types/MicroBitImage.cpp
  95. 0
      source/types/PacketBuffer.cpp
  96. 0
      source/types/RefCounted.cpp

@ -1,471 +0,0 @@
#ifndef MICROBIT_H
#define MICROBIT_H
#include "mbed.h"
#include "MicroBitConfig.h"
#include "MicroBitHeapAllocator.h"
#include "MicroBitDevice.h"
#include "ErrorNo.h"
#include "MicroBitSystemTimer.h"
#include "Matrix4.h"
#include "MicroBitCompat.h"
#include "MicroBitComponent.h"
#include "ManagedType.h"
#include "ManagedString.h"
#include "MicroBitImage.h"
#include "MicroBitFont.h"
#include "MicroBitEvent.h"
#include "DynamicPwm.h"
#include "MicroBitI2C.h"
#include "MESEvents.h"
#include "MicroBitButton.h"
#include "MicroBitPin.h"
#include "MicroBitCompass.h"
#include "MicroBitCompassCalibrator.h"
#include "MicroBitAccelerometer.h"
#include "MicroBitThermometer.h"
#include "MicroBitLightSensor.h"
#include "MicroBitMultiButton.h"
#include "MicroBitSerial.h"
#include "MicroBitIO.h"
#include "MicroBitMatrixMaps.h"
#include "MicroBitDisplay.h"
#include "MicroBitFiber.h"
#include "MicroBitMessageBus.h"
#include "MicroBitBLEManager.h"
#include "MicroBitRadio.h"
#include "MicroBitStorage.h"
// MicroBit::flags values
#define MICROBIT_INITIALIZED 0x01
// Random number generator
#define NRF51822_RNG_ADDRESS 0x4000D000
// mbed pin assignments of core components.
#define MICROBIT_PIN_SDA P0_30
#define MICROBIT_PIN_SCL P0_0
/**
* Class definition for a MicroBit device.
*
* Represents the device as a whole, and includes member variables to that reflect the components of the system.
*/
class MicroBit
{
private:
void onListenerRegisteredEvent(MicroBitEvent evt);
uint8_t status;
public:
// Reset Button
InterruptIn resetButton;
// Persistent key value store
MicroBitStorage storage;
// I2C Interface
MicroBitI2C i2c;
// Serial Interface
MicroBitSerial serial;
// Device level Message Bus abstraction
MicroBitMessageBus messageBus;
// Member variables to represent each of the core components on the device.
MicroBitDisplay display;
MicroBitButton buttonA;
MicroBitButton buttonB;
MicroBitMultiButton buttonAB;
MicroBitAccelerometer accelerometer;
MicroBitCompass compass;
MicroBitCompassCalibrator compassCalibrator;
MicroBitThermometer thermometer;
//An object of available IO pins on the device
MicroBitIO io;
// Bluetooth related member variables.
MicroBitBLEManager bleManager;
MicroBitRadio radio;
BLEDevice *ble;
/**
* Constructor.
* Create a representation of a MicroBit device as a global singleton.
* @param messageBus callback function to receive MicroBitMessageBus events.
*
* Exposed objects:
* @code
* uBit.messageBus; //The message bus where events are fired.
* uBit.display; //The display object for the LED matrix.
* uBit.buttonA; //The buttonA object for button a.
* uBit.buttonB; //The buttonB object for button b.
* uBit.resetButton; //The resetButton used for soft resets.
* uBit.accelerometer; //The object that represents the inbuilt accelerometer
* uBit.compass; //The object that represents the inbuilt compass(magnetometer)
* uBit.io.P*; //Where P* is P0 to P16, P19 & P20 on the edge connector
* @endcode
*/
MicroBit();
/**
* Post constructor initialisation method.
* 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
* uBit.init();
* @endcode
*/
void init();
/**
* Return the friendly name for this device.
*
* @return A string representing the friendly name of this device.
*/
static ManagedString getName();
/**
* Return the serial number of this device.
*
* @return A string representing the serial number of this device.
*/
static ManagedString getSerial();
/**
* Will reset the micro:bit when called.
*
* Example:
* @code
* uBit.reset();
* @endcode
*/
void reset();
/**
* Delay for the given amount of time.
* If the scheduler is running, this will deschedule the current fiber and perform
* a power efficent, concurrent sleep operation.
* If the scheduler is disabled or we're running in an interrupt context, this
* will revert to a busy wait.
*
* @note Values of 6 and below tend to lose resolution - do you really need to sleep for this short amount of time?
*
* @param milliseconds the amount of time, in ms, to wait for. This number cannot be negative.
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER milliseconds is less than zero.
*
* Example:
* @code
* MicroBit::sleep(20); //sleep for 20ms
* @endcode
*/
void sleep(uint32_t milliseconds);
/**
* Seed the pseudo random number generator using the hardware generator.
*
* Example:
* @code
* uBit.seedRandom();
* @endcode
*/
void seedRandom();
/**
* Seed the pseudo random number generator using the given value.
*
* @param seed The 32-bit value to seed the generator with.
*
* Example:
* @code
* uBit.seedRandom(0x12345678);
* @endcode
*/
void seedRandom(uint32_t seed);
/**
* Generate a random number in the given range.
* We use the NRF51822 in built random number generator here
* TODO: Determine if we want to, given its relatively high power consumption!
*
* @param max the upper range to generate a number for. This number cannot be negative
* @return A random, natural number between 0 and the max-1. Or MICROBIT_INVALID_PARAMETER if max is <= 0.
*
* Example:
* @code
* uBit.random(200); //a number between 0 and 199
* @endcode
*/
int random(int max);
/**
* Determine the time since this MicroBit was last reset.
*
* @return The time since the last reset, in milliseconds. This will result in overflow after 1.6 months.
* TODO: handle overflow case.
*/
unsigned long systemTime();
/**
* Determine the version of the micro:bit runtime currently in use.
*
* @return A textual description of the currentlt executing micro:bit runtime.
* TODO: handle overflow case.
*/
const char *systemVersion();
/**
* Triggers a microbit panic where an infinite loop will occur swapping between the panicFace and statusCode if provided.
*
* @param statusCode the status code of the associated error. Status codes must be in the range 0-255.
*/
void panic(int statusCode = 0);
/**
* add a component to the array of components which invocate the systemTick member function during a systemTick
* @param component The component to add.
* @return MICROBIT_OK on success. MICROBIT_NO_RESOURCES is returned if further components cannot be supported.
* @note This interface is now deprecated. See fiber_add_system_component().
*/
int addSystemComponent(MicroBitComponent *component);
/**
* remove a component from the array of components
* @param component The component to remove.
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMTER is returned if the given component has not been previous added.
* @note This interface is now deprecated. See fiber_remove_system_component().
*/
int removeSystemComponent(MicroBitComponent *component);
/**
* add a component to the array of components which invocate the systemTick member function during a systemTick
* @param component The component to add.
* @return MICROBIT_OK on success. MICROBIT_NO_RESOURCES is returned if further components cannot be supported.
* @note This interface is now deprecated. See fiber_add_idle_component().
*/
int addIdleComponent(MicroBitComponent *component);
/**
* remove a component from the array of components
* @param component The component to remove.
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMTER is returned if the given component has not been previous added.
* @note This interface is now deprecated. See fiber_remove_idle_component().
*/
int removeIdleComponent(MicroBitComponent *component);
};
/**
* Return the friendly name for this device.
*
* @return A string representing the friendly name of this device.
*/
inline ManagedString MicroBit::getName()
{
return ManagedString(microbit_friendly_name());
}
/**
* Return the serial number of this device.
*
* @return A string representing the serial number of this device.
*/
inline ManagedString MicroBit::getSerial()
{
// We take to 16 bit numbers here, as we want the full range of ID bits, but don't want negative numbers...
int n1 = microbit_serial_number() & 0xffff;
int n2 = (microbit_serial_number() >> 16) & 0xffff;
// Simply concat the two numbers.
ManagedString s1(n1);
ManagedString s2(n2);
return s1 + s2;
}
/**
* Will reset the micro:bit when called.
*
* Example:
* @code
* uBit.reset();
* @endcode
*/
inline void MicroBit::reset()
{
if(ble && ble->getGapState().connected) {
// We have a connected BLE peer. Disconnect the BLE session.
ble->gap().disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
// Wait a little while for the connection to drop.
wait_ms(100);
}
microbit_reset();
}
/**
* Delay for the given amount of time.
* If the scheduler is running, this will deschedule the current fiber and perform
* a power efficent, concurrent sleep operation.
* If the scheduler is disabled or we're running in an interrupt context, this
* will revert to a busy wait.
*
* @note Values of below below the scheduling period (typical 6ms) tend to lose resolution.
*
* @param milliseconds the amount of time, in ms, to wait for. This number cannot be negative.
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER milliseconds is less than zero.
*
* Example:
* @code
* uBit.sleep(20); //sleep for 20ms
* @endcode
*/
inline void MicroBit::sleep(uint32_t milliseconds)
{
fiber_sleep(milliseconds);
}
/**
* Generate a random number in the given range.
* We use a simple Galois LFSR random number generator here,
* as a Galois LFSR is sufficient for our applications, and much more lightweight
* than the hardware random number generator built int the processor, which takes
* a long time and uses a lot of energy.
*
* KIDS: You shouldn't use this is the real world to generte cryptographic keys though...
* have a think why not. :-)
*
* @param max the upper range to generate a number for. This number cannot be negative
* @return A random, natural number between 0 and the max-1. Or MICROBIT_INVALID_VALUE (defined in ErrorNo.h) if max is <= 0.
*
* Example:
* @code
* uBit.random(200); //a number between 0 and 199
* @endcode
*/
inline int MicroBit::random(int max)
{
return microbit_random(max);
}
/**
* Seed our a random number generator (RNG).
* We use the NRF51822 in built cryptographic random number generator to seed a Galois LFSR.
* We do this as the hardware RNG is relatively high power, and use the the BLE stack internally,
* with a less than optimal application interface. A Galois LFSR is sufficient for our
* applications, and much more lightweight.
*/
inline void MicroBit::seedRandom()
{
microbit_seed_random();
}
/**
* Seed our pseudo random number generator (PRNG) using the given 32-bit value.
*/
inline void MicroBit::seedRandom(uint32_t seed)
{
microbit_seed_random(seed);
}
/**
* add a component to the array of components which invocate the systemTick member function during a systemTick
* @param component The component to add.
* @return MICROBIT_OK on success. MICROBIT_NO_RESOURCES is returned if further components cannot be supported.
* @note this will be converted into a dynamic list of components
*/
inline int MicroBit::addSystemComponent(MicroBitComponent *component)
{
return system_timer_add_component(component);
}
/**
* remove a component from the array of components
* @param component The component to remove.
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMTER is returned if the given component has not been previous added.
* @note this will be converted into a dynamic list of components
*/
inline int MicroBit::removeSystemComponent(MicroBitComponent *component)
{
return system_timer_remove_component(component);
}
/**
* add a component to the array of components which invocate the systemTick member function during a systemTick
* @param component The component to add.
* @return MICROBIT_OK on success. MICROBIT_NO_RESOURCES is returned if further components cannot be supported.
* @note this will be converted into a dynamic list of components
*/
inline int MicroBit::addIdleComponent(MicroBitComponent *component)
{
return fiber_add_idle_component(component);
}
/**
* remove a component from the array of components
* @param component The component to remove.
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMTER is returned if the given component has not been previous added.
* @note this will be converted into a dynamic list of components
*/
inline int MicroBit::removeIdleComponent(MicroBitComponent *component)
{
return fiber_remove_idle_component(component);
}
/**
* Determine the time since this MicroBit was last reset.
*
* @return The time since the last reset, in milliseconds. This will result in overflow after 1.6 months.
* TODO: handle overflow case.
*/
inline unsigned long MicroBit::systemTime()
{
return system_timer_current_time();
}
/**
* Determine the version of the micro:bit runtime currently in use.
*
* @return A textual description of the currentlt executing micro:bit runtime.
* TODO: handle overflow case.
*/
inline const char *MicroBit::systemVersion()
{
return microbit_dal_version();
}
/**
* Triggers a microbit panic. All functionality will cease, and a sad face displayed along with an error code.
* @param statusCode the status code of the associated error. Status codes must be in the range 0-255.
*/
inline void MicroBit::panic(int statusCode)
{
//show error and enter infinite while
microbit_panic(statusCode);
}
// Entry point for application programs. Called after the super-main function
// has initialized the device and runtime environment.
extern "C" void app_main();
#endif

@ -20,6 +20,9 @@
"nrf51-sdk": "lancaster-university/nrf51-sdk#v2.2.0+mb3"
},
"extraIncludes": [
"inc"
"inc/core",
"inc/types",
"inc/drivers",
"inc/bluetooth"
]
}

@ -6,49 +6,50 @@ cmake_minimum_required(VERSION 2.8.12)
enable_language(ASM)
set(YOTTA_AUTO_MICROBIT-DAL_CPP_FILES
"MicroBitSuperMain.cpp"
"MicroBitI2C.cpp"
"MicroBitMultiButton.cpp"
"MicroBitFont.cpp"
"MicroBit.cpp"
"MicroBitButton.cpp"
"MicroBitMessageBus.cpp"
"MicroBitCompass.cpp"
"MicroBitCompassCalibrator.cpp"
"MicroBitEvent.cpp"
"MicroBitFiber.cpp"
"MicroBitSystemTimer.cpp"
"ManagedString.cpp"
"Matrix4.cpp"
"MicroBitAccelerometer.cpp"
"MicroBitThermometer.cpp"
"MicroBitIO.cpp"
"MicroBitCompat.cpp"
"MicroBitDevice.cpp"
"MicroBitImage.cpp"
"MicroBitDisplay.cpp"
"DynamicPwm.cpp"
"MicroBitPin.cpp"
"MicroBitSerial.cpp"
"MicroBitHeapAllocator.cpp"
"MicroBitListener.cpp"
"MicroBitLightSensor.cpp"
"RefCounted.cpp"
"MemberFunctionCallback.cpp"
"MicroBitStorage.cpp"
"PacketBuffer.cpp"
"ble-services/MicroBitBLEManager.cpp"
"ble-services/MicroBitDFUService.cpp"
"ble-services/MicroBitEventService.cpp"
"ble-services/MicroBitLEDService.cpp"
"ble-services/MicroBitAccelerometerService.cpp"
"ble-services/MicroBitMagnetometerService.cpp"
"ble-services/MicroBitButtonService.cpp"
"ble-services/MicroBitIOPinService.cpp"
"ble-services/MicroBitTemperatureService.cpp"
"ble-services/MicroBitRadio.cpp"
"ble-services/MicroBitRadioDatagram.cpp"
"ble-services/MicroBitRadioEvent.cpp"
"core/MemberFunctionCallback.cpp"
"core/MicroBitCompat.cpp"
"core/MicroBitDevice.cpp"
"core/MicroBitFiber.cpp"
"core/MicroBitFont.cpp"
"core/MicroBitHeapAllocator.cpp"
"core/MicroBitListener.cpp"
"core/MicroBitSystemTimer.cpp"
"types/ManagedString.cpp"
"types/Matrix4.cpp"
"types/MicroBitEvent.cpp"
"types/MicroBitImage.cpp"
"types/PacketBuffer.cpp"
"types/RefCounted.cpp"
"drivers/DynamicPwm.cpp"
"drivers/MicroBitAccelerometer.cpp"
"drivers/MicroBitButton.cpp"
"drivers/MicroBitCompass.cpp"
"drivers/MicroBitCompassCalibrator.cpp"
"drivers/MicroBitDisplay.cpp"
"drivers/MicroBitI2C.cpp"
"drivers/MicroBitIO.cpp"
"drivers/MicroBitLightSensor.cpp"
"drivers/MicroBitMessageBus.cpp"
"drivers/MicroBitMultiButton.cpp"
"drivers/MicroBitPin.cpp"
"drivers/MicroBitRadio.cpp"
"drivers/MicroBitRadioDatagram.cpp"
"drivers/MicroBitRadioEvent.cpp"
"drivers/MicroBitSerial.cpp"
"drivers/MicroBitStorage.cpp"
"drivers/MicroBitThermometer.cpp"
"bluetooth/MicroBitAccelerometerService.cpp"
"bluetooth/MicroBitBLEManager.cpp"
"bluetooth/MicroBitButtonService.cpp"
"bluetooth/MicroBitDFUService.cpp"
"bluetooth/MicroBitEventService.cpp"
"bluetooth/MicroBitIOPinService.cpp"
"bluetooth/MicroBitLEDService.cpp"
"bluetooth/MicroBitMagnetometerService.cpp"
"bluetooth/MicroBitTemperatureService.cpp"
)
execute_process(WORKING_DIRECTORY "../../yotta_modules/${PROJECT_NAME}" COMMAND "git" "log" "--pretty=format:%h" "-n" "1" OUTPUT_VARIABLE git_hash)

@ -1,203 +0,0 @@
#include "MicroBitConfig.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
* as a compatability option, but does not support the options used...
*/
#if !defined(__arm)
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
#include "MicroBit.h"
#include "nrf_soc.h"
/*
* Return to our predefined compiler settings.
*/
#if !defined(__arm)
#pragma GCC diagnostic pop
#endif
/**
* Constructor.
* Create a representation of a MicroBit device as a global singleton.
* @param messageBus callback function to receive MicroBitMessageBus events.
*
* Exposed objects:
* @code
* uBit.systemTicker; //the Ticker callback that performs routines like updating the display.
* uBit.messageBus; //The message bus where events are fired.
* uBit.display; //The display object for the LED matrix.
* uBit.buttonA; //The buttonA object for button a.
* uBit.buttonB; //The buttonB object for button b.
* uBit.buttonAB; //The buttonAB object for button a+b multi press.
* uBit.resetButton; //The resetButton used for soft resets.
* uBit.accelerometer; //The object that represents the inbuilt accelerometer
* uBit.compass; //The object that represents the inbuilt compass(magnetometer)
* uBit.io.P*; //Where P* is P0 to P16, P19 & P20 on the edge connector
* @endcode
*/
MicroBit::MicroBit() :
resetButton(MICROBIT_PIN_BUTTON_RESET),
storage(),
i2c(MICROBIT_PIN_SDA, MICROBIT_PIN_SCL),
serial(USBTX, USBRX),
messageBus(),
display(),
buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A),
buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B),
buttonAB(MICROBIT_ID_BUTTON_A,MICROBIT_ID_BUTTON_B, MICROBIT_ID_BUTTON_AB),
accelerometer(i2c),
compass(i2c, accelerometer, storage),
compassCalibrator(compass, accelerometer, display),
thermometer(storage),
io(MICROBIT_ID_IO_P0,MICROBIT_ID_IO_P1,MICROBIT_ID_IO_P2,
MICROBIT_ID_IO_P3,MICROBIT_ID_IO_P4,MICROBIT_ID_IO_P5,
MICROBIT_ID_IO_P6,MICROBIT_ID_IO_P7,MICROBIT_ID_IO_P8,
MICROBIT_ID_IO_P9,MICROBIT_ID_IO_P10,MICROBIT_ID_IO_P11,
MICROBIT_ID_IO_P12,MICROBIT_ID_IO_P13,MICROBIT_ID_IO_P14,
MICROBIT_ID_IO_P15,MICROBIT_ID_IO_P16,MICROBIT_ID_IO_P19,
MICROBIT_ID_IO_P20),
bleManager(storage),
radio(),
ble(NULL)
{
// Clear our status
status = 0;
// Bring up soft reset functionality as soon as possible.
resetButton.mode(PullUp);
resetButton.fall(this, &MicroBit::reset);
}
/**
* Post constructor initialisation method.
* 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
* uBit.init();
* @endcode
*/
void MicroBit::init()
{
if (status & MICROBIT_INITIALIZED)
return;
#if CONFIG_ENABLED(MICROBIT_HEAP_ALLOCATOR)
// Bring up a nested heap allocator.
microbit_create_nested_heap(MICROBIT_NESTED_HEAP_SIZE);
#endif
// Bring up fiber scheduler.
scheduler_init(&messageBus);
// Seed our random number generator
seedRandom();
// Create an event handler to trap any handlers being created for I2C services.
// We do this to enable initialisation of those services only when they're used,
// which saves processor time, memeory and battery life.
messageBus.listen(MICROBIT_ID_MESSAGE_BUS_LISTENER, MICROBIT_EVT_ANY, this, &MicroBit::onListenerRegisteredEvent);
status |= MICROBIT_INITIALIZED;
#if CONFIG_ENABLED(MICROBIT_BLE_PAIRING_MODE)
// Test if we need to enter BLE pairing mode...
int i=0;
sleep(100);
while (buttonA.isPressed() && buttonB.isPressed() && i<10)
{
sleep(100);
i++;
if (i == 10)
{
#if CONFIG_ENABLED(MICROBIT_HEAP_ALLOCATOR) && CONFIG_ENABLED(MICROBIT_HEAP_REUSE_SD)
microbit_create_heap(MICROBIT_SD_GATT_TABLE_START + MICROBIT_SD_GATT_TABLE_SIZE, MICROBIT_SD_LIMIT);
#endif
// Start the BLE stack, if it isn't already running.
if (!ble)
{
bleManager.init(getName(), getSerial(), messageBus, true);
ble = bleManager.ble;
}
// Enter pairing mode, using the LED matrix for any necessary pairing operations
bleManager.pairingMode(display, buttonA);
}
}
#endif
// Attempt to bring up a second heap region, using unused memory normally reserved for Soft Device.
#if CONFIG_ENABLED(MICROBIT_HEAP_ALLOCATOR) && CONFIG_ENABLED(MICROBIT_HEAP_REUSE_SD)
#if CONFIG_ENABLED(MICROBIT_BLE_ENABLED)
microbit_create_heap(MICROBIT_SD_GATT_TABLE_START + MICROBIT_SD_GATT_TABLE_SIZE, MICROBIT_SD_LIMIT);
#else
microbit_create_heap(MICROBIT_SRAM_BASE, MICROBIT_SD_LIMIT);
#endif
#endif
#if CONFIG_ENABLED(MICROBIT_BLE_ENABLED)
// Start the BLE stack, if it isn't already running.
if (!ble)
{
bleManager.init(getName(), getSerial(), messageBus, false);
ble = bleManager.ble;
}
#endif
}
/**
* A listener to perform actions as a result of Message Bus reflection.
*
* In some cases we want to perform lazy instantiation of components, such as
* the compass and the accelerometer, where we only want to add them to the idle
* fiber when someone has the intention of using these components.
*/
void MicroBit::onListenerRegisteredEvent(MicroBitEvent evt)
{
switch(evt.value)
{
case MICROBIT_ID_BUTTON_AB:
// A user has registered to receive events from the buttonAB multibutton.
// Disable click events from being generated by ButtonA and ButtonB, and defer the
// control of this to the multibutton handler.
//
// This way, buttons look independent unless a buttonAB is requested, at which
// point button A+B clicks can be correclty handled without breaking
// causal ordering.
buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
buttonB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
break;
case MICROBIT_ID_COMPASS:
// A listener has been registered for the compass.
// The compass uses lazy instantiation, we just need to read the data once to start it running.
// Touch the compass through the heading() function to ensure it is calibrated. if it isn't this will launch any associated calibration algorithms.
compass.heading();
break;
case MICROBIT_ID_ACCELEROMETER:
// A listener has been registered for the accelerometer.
// The accelerometer uses lazy instantiation, we just need to read the data once to start it running.
accelerometer.updateSample();
break;
case MICROBIT_ID_THERMOMETER:
// A listener has been registered for the thermometer.
// The thermometer uses lazy instantiation, we just need to read the data once to start it running.
thermometer.updateSample();
break;
}
}

@ -1,29 +0,0 @@
#ifdef UBIT_SUPERMAIN
#include "MicroBitConfig.h"
#include "MicroBit.h"
MicroBit uBit;
int main()
{
#if CONFIG_ENABLED(MICROBIT_DBG)
uBit.serial.printf("micro:bit runtime version %s\n", MICROBIT_DAL_VERSION);
#endif
// Bring up random number generator, BLE, display and system timers.
uBit.init();
// Start the user application
app_main();
// If app_main exits, there may still be other fibers running, registered event handlers etc.
// Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
// sit in the idle task forever, in a power efficient sleep.
release_fiber();
// We should never get here, but just in case.
while(1);
}
#endif
Loading…
Cancel
Save