Merge pull request #86 from lancaster-university/light-sensor
Adding MicroBitLightSensor to the DAL
This commit is contained in:
commit
004b701419
8 changed files with 513 additions and 105 deletions
|
@ -24,6 +24,7 @@
|
|||
#include "MicroBitCompass.h"
|
||||
#include "MicroBitAccelerometer.h"
|
||||
#include "MicroBitThermometer.h"
|
||||
#include "MicroBitLightSensor.h"
|
||||
#include "MicroBitMultiButton.h"
|
||||
|
||||
#include "MicroBitSerial.h"
|
||||
|
@ -46,12 +47,14 @@
|
|||
#define MICROBIT_NAME_CODE_LETTERS 5
|
||||
|
||||
// Random number generator
|
||||
#define NRF51822_RNG_ADDRESS 0x4000D000
|
||||
#define NRF51822_RNG_ADDRESS 0x4000D000
|
||||
|
||||
|
||||
// mbed pin assignments of core components.
|
||||
#define MICROBIT_PIN_SDA P0_30
|
||||
#define MICROBIT_PIN_SCL P0_0
|
||||
#define MICROBIT_PIN_SDA P0_30
|
||||
#define MICROBIT_PIN_SCL P0_0
|
||||
|
||||
#define MICROBIT_DEFAULT_TICK_PERIOD FIBER_TICK_PERIOD_MS
|
||||
|
||||
/**
|
||||
* Class definition for a MicroBit device.
|
||||
|
@ -65,6 +68,8 @@ class MicroBit
|
|||
void compassCalibrator(MicroBitEvent e);
|
||||
uint32_t randomValue;
|
||||
|
||||
//the current tick period in MS
|
||||
int tickPeriod;
|
||||
|
||||
public:
|
||||
|
||||
|
@ -259,6 +264,21 @@ class MicroBit
|
|||
*/
|
||||
int removeIdleComponent(MicroBitComponent *component);
|
||||
|
||||
|
||||
/*
|
||||
* Reconfigures the ticker to the given speed in milliseconds.
|
||||
* @param speedMs the speed in milliseconds
|
||||
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if speedUs < 1
|
||||
*
|
||||
* @note this will also modify the value that is added to ticks in MiroBitFiber:scheduler_tick()
|
||||
*/
|
||||
int setTickPeriod(int speedMs);
|
||||
|
||||
/*
|
||||
* Returns the currently used tick speed in milliseconds
|
||||
*/
|
||||
int getTickPeriod();
|
||||
|
||||
/**
|
||||
* Determine the time since this MicroBit was last reset.
|
||||
*
|
||||
|
|
|
@ -1,17 +1,12 @@
|
|||
#ifndef MICROBIT_DISPLAY_H
|
||||
#define MICROBIT_DISPLAY_H
|
||||
|
||||
|
||||
/**
|
||||
* Core Configuration settings.
|
||||
*/
|
||||
#define MICROBIT_DISPLAY_REFRESH_PERIOD ((float)FIBER_TICK_PERIOD_MS / (float)1000)
|
||||
|
||||
/**
|
||||
* MessageBus Event Codes
|
||||
*/
|
||||
#define MICROBIT_DISPLAY_EVT_ANIMATION_COMPLETE 1
|
||||
#define MICROBIT_DISPLAY_EVT_FREE 2
|
||||
#define MICROBIT_DISPLAY_EVT_LIGHT_SENSE 4
|
||||
|
||||
/**
|
||||
* I/O configurations for common devices.
|
||||
|
@ -43,6 +38,7 @@
|
|||
#define MICROBIT_DISPLAY_COLUMN_COUNT 9
|
||||
#define MICROBIT_DISPLAY_COLUMN_PINS P0_4, P0_5, P0_6, P0_7, P0_8, P0_9, P0_10, P0_11, P0_12
|
||||
#define MICROBIT_DISPLAY_COLUMN_START P0_4
|
||||
#define MICROBIT_DISPLAY_ROW_START P0_13
|
||||
#endif
|
||||
|
||||
//
|
||||
|
@ -55,6 +51,8 @@
|
|||
#define MICROBIT_DISPLAY_GREYSCALE_BIT_DEPTH 8
|
||||
#define MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS -255
|
||||
|
||||
#define MICROBIT_DISPLAY_ROW_RESET 0x20
|
||||
|
||||
#include "mbed.h"
|
||||
#include "ManagedString.h"
|
||||
#include "MicroBitComponent.h"
|
||||
|
@ -73,7 +71,8 @@ enum AnimationMode {
|
|||
|
||||
enum DisplayMode {
|
||||
DISPLAY_MODE_BLACK_AND_WHITE,
|
||||
DISPLAY_MODE_GREYSCALE
|
||||
DISPLAY_MODE_GREYSCALE,
|
||||
DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE
|
||||
};
|
||||
|
||||
enum DisplayRotation {
|
||||
|
@ -161,6 +160,9 @@ class MicroBitDisplay : public MicroBitComponent
|
|||
// The number of pixels the image is shifted on the display in each quantum.
|
||||
int8_t scrollingImageStride;
|
||||
|
||||
// A pointer to an instance of light sensor, if in use
|
||||
MicroBitLightSensor* lightSensor;
|
||||
|
||||
// Flag to indicate if image has been rendered to screen yet (or not)
|
||||
bool scrollingImageRendered;
|
||||
|
||||
|
@ -185,6 +187,12 @@ class MicroBitDisplay : public MicroBitComponent
|
|||
*/
|
||||
void render();
|
||||
|
||||
/**
|
||||
* Renders the current image, and drops the fourth frame to allow for
|
||||
* sensors that require the display to operate.
|
||||
*/
|
||||
void renderWithLightSense();
|
||||
|
||||
/**
|
||||
* Translates a bit mask into a timer interrupt that gives the appearence of greyscale.
|
||||
*/
|
||||
|
@ -473,7 +481,7 @@ public:
|
|||
|
||||
/**
|
||||
* Sets the mode of the display.
|
||||
* @param mode The mode to swap the display into. (can be either DISPLAY_MODE_GREYSCALE, or DISPLAY_MODE_NORMAL)
|
||||
* @param mode The mode to swap the display into. (can be either DISPLAY_MODE_GREYSCALE, DISPLAY_MODE_BLACK_AND_WHITE, DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE)
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
|
@ -482,6 +490,12 @@ public:
|
|||
*/
|
||||
void setDisplayMode(DisplayMode mode);
|
||||
|
||||
/**
|
||||
* Gets the mode of the display.
|
||||
* @return the current mode of the display
|
||||
*/
|
||||
int getDisplayMode();
|
||||
|
||||
/**
|
||||
* Fetches the current brightness of this display.
|
||||
* @return the brightness of this display, in the range 0..255.
|
||||
|
@ -563,6 +577,19 @@ public:
|
|||
*/
|
||||
MicroBitImage screenShot();
|
||||
|
||||
/**
|
||||
* Constructs an instance of a MicroBitLightSensor if not already configured
|
||||
* and sets the display mode to DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE.
|
||||
*
|
||||
* This also changes the tickPeriod to MICROBIT_LIGHT_SENSOR_TICK_SPEED so
|
||||
* that the display does not suffer from artifacts.
|
||||
*
|
||||
* @note this will return 0 on the first call to this method, a light reading
|
||||
* will be available after the display has activated the light sensor for the
|
||||
* first time.
|
||||
*/
|
||||
int readLightLevel();
|
||||
|
||||
/**
|
||||
* Destructor for MicroBitDisplay, so that we deregister ourselves as a systemComponent
|
||||
*/
|
||||
|
|
104
inc/MicroBitLightSensor.h
Normal file
104
inc/MicroBitLightSensor.h
Normal file
|
@ -0,0 +1,104 @@
|
|||
#ifndef MICROBIT_LIGHT_SENSOR_H
|
||||
#define MICROBIT_LIGHT_SENSOR_H
|
||||
|
||||
#include "mbed.h"
|
||||
#include "MicroBitComponent.h"
|
||||
|
||||
#define MICROBIT_LIGHT_SENSOR_CHAN_NUM 3
|
||||
#define MICROBIT_LIGHT_SENSOR_AN_SET_TIME 4000
|
||||
#define MICROBIT_LIGHT_SENSOR_TICK_PERIOD 5
|
||||
|
||||
#define MICROBIT_LIGHT_SENSOR_MAX_VALUE 338
|
||||
#define MICROBIT_LIGHT_SENSOR_MIN_VALUE 75
|
||||
|
||||
/**
|
||||
* Class definition for MicroBitLightSensor.
|
||||
*
|
||||
* This is an object that interleaves light sensing with uBit.display.
|
||||
*/
|
||||
class MicroBitLightSensor
|
||||
{
|
||||
|
||||
//contains the results from each section of the display
|
||||
int results[MICROBIT_LIGHT_SENSOR_CHAN_NUM] = { 0 };
|
||||
|
||||
//holds the current channel (also used to index the results array)
|
||||
uint8_t chan;
|
||||
|
||||
//a Timeout which triggers our analogReady() call
|
||||
Timeout analogTrigger;
|
||||
|
||||
//a pointer the currently sensed pin, represented as an AnalogIn
|
||||
AnalogIn* sensePin;
|
||||
|
||||
/**
|
||||
* After the startSensing method has been called, this method will be called
|
||||
* MICROBIT_LIGHT_SENSOR_AN_SET_TIME after.
|
||||
*
|
||||
* It will then read from the currently selected channel using the AnalogIn
|
||||
* that was configured in the startSensing method.
|
||||
*/
|
||||
void analogReady();
|
||||
|
||||
/**
|
||||
* Forcibly disables the AnalogIn, otherwise it will remain in possession
|
||||
* of the GPIO channel it is using, meaning that the display will not be
|
||||
* able to use a channel (COL).
|
||||
*
|
||||
* This is required as per PAN 3, details of which can be found here:
|
||||
*
|
||||
* https://www.nordicsemi.com/eng/nordic/download_resource/24634/5/88440387
|
||||
*/
|
||||
void analogDisable();
|
||||
|
||||
/**
|
||||
* The method that is invoked by sending MICROBIT_DISPLAY_EVT_LIGHT_SENSE
|
||||
* using the id MICROBIT_ID_DISPLAY.
|
||||
*
|
||||
* If you want to manually trigger this method, you should use the event bus.
|
||||
*/
|
||||
void startSensing(MicroBitEvent);
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the light sensor
|
||||
*/
|
||||
MicroBitLightSensor();
|
||||
|
||||
/**
|
||||
* This method returns a summed average of the three sections of the display.
|
||||
*
|
||||
* A section is defined as:
|
||||
* ___________________
|
||||
* | 1 | | 2 | | 3 |
|
||||
* |___|___|___|___|___|
|
||||
* | | | | | |
|
||||
* |___|___|___|___|___|
|
||||
* | 2 | | 3 | | 1 |
|
||||
* |___|___|___|___|___|
|
||||
* | | | | | |
|
||||
* |___|___|___|___|___|
|
||||
* | 3 | | 1 | | 2 |
|
||||
* |___|___|___|___|___|
|
||||
*
|
||||
* Where each number represents a different section on the 5 x 5 matrix display.
|
||||
*
|
||||
* @return returns a value in the range 0 - 100 where 0 is dark, and 100
|
||||
* is very bright
|
||||
*
|
||||
* @note currently returns a value in the range 0 - 100 where 0 is dark, and 100
|
||||
* is very bright perhaps we should normalise the returned values into an SI unit!
|
||||
* TODO.
|
||||
*/
|
||||
int read();
|
||||
|
||||
/**
|
||||
* The destructor restores the default Display Mode and tick speed, and also
|
||||
* removes the listener from the MessageBus.
|
||||
*/
|
||||
~MicroBitLightSensor();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -29,6 +29,7 @@ set(YOTTA_AUTO_MICROBIT-DAL_CPP_FILES
|
|||
"MicroBitSerial.cpp"
|
||||
"MicroBitHeapAllocator.cpp"
|
||||
"MicroBitListener.cpp"
|
||||
"MicroBitLightSensor.cpp"
|
||||
"RefCounted.cpp"
|
||||
"MemberFunctionCallback.cpp"
|
||||
"ble-services/MicroBitBLEManager.cpp"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/*
|
||||
/*
|
||||
* 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
|
||||
|
@ -133,8 +133,10 @@ void MicroBit::init()
|
|||
// Seed our random number generator
|
||||
seedRandom();
|
||||
|
||||
tickPeriod = MICROBIT_DEFAULT_TICK_PERIOD;
|
||||
|
||||
// Start refreshing the Matrix Display
|
||||
systemTicker.attach(this, &MicroBit::systemTick, MICROBIT_DISPLAY_REFRESH_PERIOD);
|
||||
systemTicker.attach_us(this, &MicroBit::systemTick, tickPeriod * 1000);
|
||||
|
||||
// Register our compass calibration algorithm.
|
||||
MessageBus.listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATE, this, &MicroBit::compassCalibrator, MESSAGE_BUS_LISTENER_IMMEDIATE);
|
||||
|
@ -608,6 +610,35 @@ int MicroBit::removeIdleComponent(MicroBitComponent *component)
|
|||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reconfigures the ticker to the given speed in milliseconds.
|
||||
* @param speedMs the speed in milliseconds
|
||||
* @return MICROBIT_OK on success. MICROBIT_INVALID_PARAMETER is returned if speedUs < 1
|
||||
*
|
||||
* @note this will also modify the value that is added to ticks in MiroBitFiber:scheduler_tick()
|
||||
*/
|
||||
int MicroBit::setTickPeriod(int speedMs)
|
||||
{
|
||||
if(speedMs < 1)
|
||||
return MICROBIT_INVALID_PARAMETER;
|
||||
|
||||
uBit.systemTicker.detach();
|
||||
|
||||
uBit.systemTicker.attach_us(this, &MicroBit::systemTick, speedMs * 1000);
|
||||
|
||||
tickPeriod = speedMs;
|
||||
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the currently used tick speed in milliseconds
|
||||
*/
|
||||
int MicroBit::getTickPeriod()
|
||||
{
|
||||
return tickPeriod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the time since this MicroBit was last reset.
|
||||
*
|
||||
|
|
|
@ -34,7 +34,7 @@ MicroBitDisplay::MicroBitDisplay(uint16_t id, uint8_t x, uint8_t y) :
|
|||
this->width = x;
|
||||
this->height = y;
|
||||
this->strobeRow = 0;
|
||||
this->strobeBitMsk = 0x20;
|
||||
this->strobeBitMsk = MICROBIT_DISPLAY_ROW_RESET;
|
||||
this->rotation = MICROBIT_DISPLAY_ROTATION_0;
|
||||
this->greyscaleBitMsk = 0x01;
|
||||
this->timingCount = 0;
|
||||
|
@ -44,6 +44,8 @@ MicroBitDisplay::MicroBitDisplay(uint16_t id, uint8_t x, uint8_t y) :
|
|||
this->mode = DISPLAY_MODE_BLACK_AND_WHITE;
|
||||
this->animationMode = ANIMATION_MODE_NONE;
|
||||
|
||||
this->lightSensor = NULL;
|
||||
|
||||
uBit.flags |= MICROBIT_FLAG_DISPLAY_RUNNING;
|
||||
}
|
||||
|
||||
|
@ -58,6 +60,12 @@ void MicroBitDisplay::systemTick()
|
|||
if(!(uBit.flags & MICROBIT_FLAG_DISPLAY_RUNNING))
|
||||
return;
|
||||
|
||||
if(mode == DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE)
|
||||
{
|
||||
renderWithLightSense();
|
||||
return;
|
||||
}
|
||||
|
||||
// Move on to the next row.
|
||||
strobeBitMsk <<= 1;
|
||||
strobeRow++;
|
||||
|
@ -65,7 +73,7 @@ void MicroBitDisplay::systemTick()
|
|||
//reset the row counts and bit mask when we have hit the max.
|
||||
if(strobeRow == MICROBIT_DISPLAY_ROW_COUNT){
|
||||
strobeRow = 0;
|
||||
strobeBitMsk = 0x20;
|
||||
strobeBitMsk = MICROBIT_DISPLAY_ROW_RESET;
|
||||
}
|
||||
|
||||
if(mode == DISPLAY_MODE_BLACK_AND_WHITE)
|
||||
|
@ -139,13 +147,37 @@ void MicroBitDisplay::render()
|
|||
|
||||
//timer does not have enough resolution for brightness of 1. 23.53 us
|
||||
if(brightness != MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS && brightness > MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS)
|
||||
renderTimer.attach(this, &MicroBitDisplay::renderFinish, (((float)brightness) / ((float)MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS)) * (float)MICROBIT_DISPLAY_REFRESH_PERIOD);
|
||||
renderTimer.attach_us(this, &MicroBitDisplay::renderFinish, (((brightness * 100) / (MICROBIT_DISPLAY_MAXIMUM_BRIGHTNESS)) * uBit.getTickPeriod()));
|
||||
|
||||
//this will take around 23us to execute
|
||||
if(brightness <= MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS)
|
||||
renderFinish();
|
||||
}
|
||||
|
||||
void MicroBitDisplay::renderWithLightSense()
|
||||
{
|
||||
//reset the row counts and bit mask when we have hit the max.
|
||||
if(strobeRow == MICROBIT_DISPLAY_ROW_COUNT + 1)
|
||||
{
|
||||
|
||||
MicroBitEvent(id, MICROBIT_DISPLAY_EVT_LIGHT_SENSE);
|
||||
|
||||
strobeRow = 0;
|
||||
strobeBitMsk = MICROBIT_DISPLAY_ROW_RESET;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
render();
|
||||
this->animationUpdate();
|
||||
|
||||
// Move on to the next row.
|
||||
strobeBitMsk <<= 1;
|
||||
strobeRow++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MicroBitDisplay::renderGreyscale()
|
||||
{
|
||||
int coldata = 0;
|
||||
|
@ -883,7 +915,7 @@ int MicroBitDisplay::setBrightness(int b)
|
|||
|
||||
/**
|
||||
* Sets the mode of the display.
|
||||
* @param mode The mode to swap the display into. (can be either DISPLAY_MODE_GREYSCALE, or DISPLAY_MODE_NORMAL)
|
||||
* @param mode The mode to swap the display into. (can be either DISPLAY_MODE_GREYSCALE, DISPLAY_MODE_BLACK_AND_WHITE, DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE)
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
|
@ -892,9 +924,37 @@ int MicroBitDisplay::setBrightness(int b)
|
|||
*/
|
||||
void MicroBitDisplay::setDisplayMode(DisplayMode mode)
|
||||
{
|
||||
if(mode == DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE)
|
||||
{
|
||||
//to reduce the artifacts on the display - increase the tick
|
||||
if(uBit.getTickPeriod() != MICROBIT_LIGHT_SENSOR_TICK_PERIOD)
|
||||
uBit.setTickPeriod(MICROBIT_LIGHT_SENSOR_TICK_PERIOD);
|
||||
}
|
||||
|
||||
if(this->mode == DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE && mode != DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE)
|
||||
{
|
||||
|
||||
//if we previously were in light sense mode - return to our default.
|
||||
if(uBit.getTickPeriod() != MICROBIT_DEFAULT_TICK_PERIOD)
|
||||
uBit.setTickPeriod(MICROBIT_DEFAULT_TICK_PERIOD);
|
||||
|
||||
delete this->lightSensor;
|
||||
|
||||
this->lightSensor = NULL;
|
||||
}
|
||||
|
||||
this->mode = mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the mode of the display.
|
||||
* @return the current mode of the display
|
||||
*/
|
||||
int MicroBitDisplay::getDisplayMode()
|
||||
{
|
||||
return this->mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the current brightness of this display.
|
||||
* @return the brightness of this display, in the range 0..255.
|
||||
|
@ -992,7 +1052,7 @@ void MicroBitDisplay::error(int statusCode)
|
|||
disable(); //relinquish PWMOut's control
|
||||
|
||||
uint8_t strobeRow = 0;
|
||||
uint8_t strobeBitMsk = 0x20;
|
||||
uint8_t strobeBitMsk = MICROBIT_DISPLAY_ROW_RESET;
|
||||
|
||||
//point to the font stored in Flash
|
||||
const unsigned char * fontLocation = MicroBitFont::defaultFont;
|
||||
|
@ -1019,7 +1079,7 @@ void MicroBitDisplay::error(int statusCode)
|
|||
if(strobeRow == 3)
|
||||
{
|
||||
strobeRow = 0;
|
||||
strobeBitMsk = 0x20;
|
||||
strobeBitMsk = MICROBIT_DISPLAY_ROW_RESET;
|
||||
}
|
||||
|
||||
// Calculate the bitpattern to write.
|
||||
|
@ -1087,6 +1147,28 @@ MicroBitImage MicroBitDisplay::screenShot()
|
|||
return image.crop(0,0,MICROBIT_DISPLAY_WIDTH,MICROBIT_DISPLAY_HEIGHT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of a MicroBitLightSensor if not already configured
|
||||
* and sets the display mode to DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE.
|
||||
*
|
||||
* This also changes the tickPeriod to MICROBIT_LIGHT_SENSOR_TICK_SPEED so
|
||||
* that the display does not suffer from artifacts.
|
||||
*
|
||||
* @note this will return 0 on the first call to this method, a light reading
|
||||
* will be available after the display has activated the light sensor for the
|
||||
* first time.
|
||||
*/
|
||||
int MicroBitDisplay::readLightLevel()
|
||||
{
|
||||
if(mode != DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE)
|
||||
{
|
||||
setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE);
|
||||
this->lightSensor = new MicroBitLightSensor();
|
||||
}
|
||||
|
||||
return this->lightSensor->read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor for MicroBitDisplay, so that we deregister ourselves as a systemComponent
|
||||
*/
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
||||
|
||||
#include "MicroBit.h"
|
||||
|
||||
/*
|
||||
|
@ -37,7 +37,7 @@ unsigned long ticks = 0;
|
|||
uint8_t fiber_flags = 0;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -62,7 +62,7 @@ void queue_fiber(Fiber *f, Fiber **queue)
|
|||
}
|
||||
else
|
||||
{
|
||||
// Scan to the end of the queue.
|
||||
// Scan to the end of the queue.
|
||||
// We don't maintain a tail pointer to save RAM (queues are nrmally very short).
|
||||
Fiber *last = *queue;
|
||||
|
||||
|
@ -78,7 +78,7 @@ 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)
|
||||
|
@ -87,34 +87,34 @@ void dequeue_fiber(Fiber *f)
|
|||
if (f->queue == NULL)
|
||||
return;
|
||||
|
||||
// Remove this fiber fromm whichever queue it is on.
|
||||
// Remove this fiber fromm whichever queue it is on.
|
||||
__disable_irq();
|
||||
|
||||
|
||||
if (f->prev != NULL)
|
||||
f->prev->next = f->next;
|
||||
else
|
||||
*(f->queue) = f->next;
|
||||
|
||||
|
||||
if(f->next)
|
||||
f->next->prev = f->prev;
|
||||
|
||||
|
||||
f->next = NULL;
|
||||
f->prev = NULL;
|
||||
f->queue = NULL;
|
||||
|
||||
|
||||
__enable_irq();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocates a fiber from the fiber pool if availiable. Otherwise, allocates a new one from the heap.
|
||||
*/
|
||||
*/
|
||||
Fiber *getFiberContext()
|
||||
{
|
||||
Fiber *f;
|
||||
|
||||
|
||||
__disable_irq();
|
||||
|
||||
|
||||
if (fiberPool != NULL)
|
||||
{
|
||||
f = fiberPool;
|
||||
|
@ -124,17 +124,17 @@ Fiber *getFiberContext()
|
|||
else
|
||||
{
|
||||
__enable_irq();
|
||||
|
||||
|
||||
f = new Fiber();
|
||||
|
||||
|
||||
if (f == NULL)
|
||||
return NULL;
|
||||
|
||||
f->stack_bottom = 0;
|
||||
f->stack_top = 0;
|
||||
}
|
||||
|
||||
// Ensure this fiber is in suitable state for reuse.
|
||||
}
|
||||
|
||||
// Ensure this fiber is in suitable state for reuse.
|
||||
f->flags = 0;
|
||||
f->tcb.stack_base = CORTEX_M0_STACK_BASE;
|
||||
|
||||
|
@ -143,7 +143,7 @@ Fiber *getFiberContext()
|
|||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -152,14 +152,14 @@ void scheduler_init()
|
|||
{
|
||||
// Create a new fiber context
|
||||
currentFiber = getFiberContext();
|
||||
|
||||
|
||||
// Add ourselves to the run queue.
|
||||
queue_fiber(currentFiber, &runQueue);
|
||||
|
||||
// Create the IDLE fiber.
|
||||
// Configure the fiber to directly enter the idle task.
|
||||
idleFiber = getFiberContext();
|
||||
idleFiber->tcb.SP = CORTEX_M0_STACK_BASE - 0x04;
|
||||
idleFiber->tcb.SP = CORTEX_M0_STACK_BASE - 0x04;
|
||||
idleFiber->tcb.LR = (uint32_t) &idle_task;
|
||||
|
||||
// Register to receive events in the NOTIFY channel - this is used to implement wait-notify semantics
|
||||
|
@ -172,36 +172,36 @@ void scheduler_init()
|
|||
|
||||
/**
|
||||
* Timer callback. Called from interrupt context, once every FIBER_TICK_PERIOD_MS milliseconds.
|
||||
* 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()
|
||||
{
|
||||
Fiber *f = sleepQueue;
|
||||
Fiber *t;
|
||||
|
||||
|
||||
// increment our real-time counter.
|
||||
ticks += FIBER_TICK_PERIOD_MS;
|
||||
|
||||
ticks += uBit.getTickPeriod();
|
||||
|
||||
// Check the sleep queue, and wake up any fibers as necessary.
|
||||
while (f != NULL)
|
||||
{
|
||||
t = f->next;
|
||||
|
||||
t = f->next;
|
||||
|
||||
if (ticks >= f->context)
|
||||
{
|
||||
// Wakey wakey!
|
||||
dequeue_fiber(f);
|
||||
queue_fiber(f,&runQueue);
|
||||
}
|
||||
|
||||
|
||||
f = t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
|
@ -209,20 +209,20 @@ void scheduler_event(MicroBitEvent evt)
|
|||
Fiber *f = waitQueue;
|
||||
Fiber *t;
|
||||
int notifyOneComplete = 0;
|
||||
|
||||
|
||||
// Check the wait queue, and wake up any fibers as necessary.
|
||||
while (f != NULL)
|
||||
{
|
||||
t = f->next;
|
||||
|
||||
// extract the event data this fiber is blocked on.
|
||||
t = f->next;
|
||||
|
||||
// extract the event data this fiber is blocked on.
|
||||
uint16_t id = f->context & 0xFFFF;
|
||||
uint16_t value = (f->context & 0xFFFF0000) >> 16;
|
||||
|
||||
|
||||
// Special case for the NOTIFY_ONE channel...
|
||||
if ((evt.source == MICROBIT_ID_NOTIFY_ONE && id == MICROBIT_ID_NOTIFY) && (value == MICROBIT_EVT_ANY || value == evt.value))
|
||||
{
|
||||
if (!notifyOneComplete)
|
||||
if (!notifyOneComplete)
|
||||
{
|
||||
// Wakey wakey!
|
||||
dequeue_fiber(f);
|
||||
|
@ -238,7 +238,7 @@ void scheduler_event(MicroBitEvent evt)
|
|||
dequeue_fiber(f);
|
||||
queue_fiber(f,&runQueue);
|
||||
}
|
||||
|
||||
|
||||
f = t;
|
||||
}
|
||||
|
||||
|
@ -250,9 +250,9 @@ void scheduler_event(MicroBitEvent evt)
|
|||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
@ -269,8 +269,8 @@ void fiber_sleep(unsigned long t)
|
|||
// Allocate a new fiber. This will come from the fiber pool if availiable,
|
||||
// else a new one will be allocated on the heap.
|
||||
forkedFiber = getFiberContext();
|
||||
|
||||
// If we're out of memory, there's nothing we can do.
|
||||
|
||||
// If we're out of memory, there's nothing we can do.
|
||||
// keep running in the context of the current thread as a best effort.
|
||||
if (forkedFiber != NULL)
|
||||
f = forkedFiber;
|
||||
|
@ -278,22 +278,22 @@ void fiber_sleep(unsigned long t)
|
|||
|
||||
// Calculate and store the time we want to wake up.
|
||||
f->context = ticks + t;
|
||||
|
||||
|
||||
// Remove fiber from the run queue
|
||||
dequeue_fiber(f);
|
||||
|
||||
|
||||
// Add fiber to the sleep queue. We maintain strict ordering here to reduce lookup times.
|
||||
queue_fiber(f, &sleepQueue);
|
||||
|
||||
|
||||
// Finally, enter the scheduler.
|
||||
schedule();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
@ -311,8 +311,8 @@ void fiber_wait_for_event(uint16_t id, uint16_t value)
|
|||
// Allocate a TCB from the new fiber. This will come from the tread pool if availiable,
|
||||
// else a new one will be allocated on the heap.
|
||||
forkedFiber = getFiberContext();
|
||||
|
||||
// If we're out of memory, there's nothing we can do.
|
||||
|
||||
// If we're out of memory, there's nothing we can do.
|
||||
// keep running in the context of the current thread as a best effort.
|
||||
if (forkedFiber != NULL)
|
||||
f = forkedFiber;
|
||||
|
@ -320,13 +320,13 @@ void fiber_wait_for_event(uint16_t id, uint16_t value)
|
|||
|
||||
// Encode the event data in the context field. It's handy having a 32 bit core. :-)
|
||||
f->context = value << 16 | id;
|
||||
|
||||
|
||||
// Remove ourselve from the run queue
|
||||
dequeue_fiber(f);
|
||||
|
||||
|
||||
// Add ourselves to the sleep queue. We maintain strict ordering here to reduce lookup times.
|
||||
queue_fiber(f, &waitQueue);
|
||||
|
||||
|
||||
// Register to receive this event, so we can wake up the fiber when it happens.
|
||||
// Special case for teh notify channel, as we always stay registered for that.
|
||||
if (id != MICROBIT_ID_NOTIFY && id != MICROBIT_ID_NOTIFY_ONE)
|
||||
|
@ -337,16 +337,16 @@ void fiber_wait_for_event(uint16_t id, uint16_t value)
|
|||
}
|
||||
|
||||
/**
|
||||
* Executes the given function asynchronously.
|
||||
*
|
||||
* Executes the given function asynchronously.
|
||||
*
|
||||
* 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))
|
||||
{
|
||||
|
@ -361,14 +361,14 @@ int invoke(void (*entry_fn)(void))
|
|||
create_fiber(entry_fn);
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
||||
// Snapshot current context, but also update the Link Register to
|
||||
// refer to our calling function.
|
||||
save_register_context(¤tFiber->tcb);
|
||||
|
||||
// If we're here, there are two possibilities:
|
||||
// 1) We're about to attempt to execute the user code
|
||||
// 2) We've already tried to execute the code, it blocked, and we've backtracked.
|
||||
// 2) We've already tried to execute the code, it blocked, and we've backtracked.
|
||||
|
||||
// If we're returning from the user function and we forked another fiber then cleanup and exit.
|
||||
if (currentFiber->flags & MICROBIT_FIBER_FLAG_PARENT)
|
||||
|
@ -382,7 +382,7 @@ int invoke(void (*entry_fn)(void))
|
|||
// execute the function directly. If the code tries to block, we detect this and
|
||||
// spawn a thread to deal with it.
|
||||
currentFiber->flags |= MICROBIT_FIBER_FLAG_FOB;
|
||||
entry_fn();
|
||||
entry_fn();
|
||||
currentFiber->flags &= ~MICROBIT_FIBER_FLAG_FOB;
|
||||
|
||||
// If this is is an exiting fiber that for spawned to handle a blocking call, recycle it.
|
||||
|
@ -394,17 +394,17 @@ int invoke(void (*entry_fn)(void))
|
|||
}
|
||||
|
||||
/**
|
||||
* Executes the given parameterized function asynchronously.
|
||||
*
|
||||
* Executes the given parameterized function asynchronously.
|
||||
*
|
||||
* 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 attempt 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 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.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
||||
*/
|
||||
int invoke(void (*entry_fn)(void *), void *param)
|
||||
{
|
||||
|
@ -419,14 +419,14 @@ int invoke(void (*entry_fn)(void *), void *param)
|
|||
create_fiber(entry_fn, param);
|
||||
return MICROBIT_OK;
|
||||
}
|
||||
|
||||
|
||||
// Snapshot current context, but also update the Link Register to
|
||||
// refer to our calling function.
|
||||
save_register_context(¤tFiber->tcb);
|
||||
|
||||
// If we're here, there are two possibilities:
|
||||
// 1) We're about to attempt to execute the user code
|
||||
// 2) We've already tried to execute the code, it blocked, and we've backtracked.
|
||||
// 2) We've already tried to execute the code, it blocked, and we've backtracked.
|
||||
|
||||
// If we're returning from the user function and we forked another fiber then cleanup and exit.
|
||||
if (currentFiber->flags & MICROBIT_FIBER_FLAG_PARENT)
|
||||
|
@ -440,7 +440,7 @@ int invoke(void (*entry_fn)(void *), void *param)
|
|||
// execute the function directly. If the code tries to block, we detect this and
|
||||
// spawn a thread to deal with it.
|
||||
currentFiber->flags |= MICROBIT_FIBER_FLAG_FOB;
|
||||
entry_fn(param);
|
||||
entry_fn(param);
|
||||
currentFiber->flags &= ~MICROBIT_FIBER_FLAG_FOB;
|
||||
|
||||
// If this is is an exiting fiber that for spawned to handle a blocking call, recycle it.
|
||||
|
@ -480,26 +480,26 @@ Fiber *__create_fiber(uint32_t ep, uint32_t cp, uint32_t pm, int parameterised)
|
|||
// Validate our parameters.
|
||||
if (ep == 0 || cp == 0)
|
||||
return NULL;
|
||||
|
||||
|
||||
// Allocate a TCB from the new fiber. This will come from the fiber pool if availiable,
|
||||
// else a new one will be allocated on the heap.
|
||||
Fiber *newFiber = getFiberContext();
|
||||
|
||||
|
||||
// If we're out of memory, there's nothing we can do.
|
||||
if (newFiber == NULL)
|
||||
return NULL;
|
||||
|
||||
|
||||
newFiber->tcb.R0 = (uint32_t) ep;
|
||||
newFiber->tcb.R1 = (uint32_t) cp;
|
||||
newFiber->tcb.R2 = (uint32_t) pm;
|
||||
|
||||
// Set the stack and assign the link register to refer to the appropriate entry point wrapper.
|
||||
newFiber->tcb.SP = CORTEX_M0_STACK_BASE - 0x04;
|
||||
newFiber->tcb.SP = CORTEX_M0_STACK_BASE - 0x04;
|
||||
newFiber->tcb.LR = parameterised ? (uint32_t) &launch_new_fiber_param : (uint32_t) &launch_new_fiber;
|
||||
|
||||
|
||||
// Add new fiber to the run queue.
|
||||
queue_fiber(newFiber, &runQueue);
|
||||
|
||||
|
||||
return newFiber;
|
||||
}
|
||||
|
||||
|
@ -507,7 +507,7 @@ Fiber *__create_fiber(uint32_t ep, uint32_t cp, uint32_t pm, int parameterised)
|
|||
* 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))
|
||||
|
@ -521,7 +521,7 @@ Fiber *create_fiber(void (*entry_fn)(void), void (*completion_fn)(void))
|
|||
*
|
||||
* @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 *))
|
||||
|
@ -543,15 +543,15 @@ void release_fiber(void *)
|
|||
* Any fiber reaching the end of its entry function will return here for recycling.
|
||||
*/
|
||||
void release_fiber(void)
|
||||
{
|
||||
{
|
||||
// Remove ourselves form the runqueue.
|
||||
dequeue_fiber(currentFiber);
|
||||
|
||||
// Add ourselves to the list of free fibers
|
||||
queue_fiber(currentFiber, &fiberPool);
|
||||
|
||||
|
||||
// Find something else to do!
|
||||
schedule();
|
||||
schedule();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -571,7 +571,7 @@ void verify_stack_size(Fiber *f)
|
|||
// Calculate the stack depth.
|
||||
stackDepth = f->tcb.stack_base - ((uint32_t) __get_MSP());
|
||||
|
||||
// Calculate the size of our allocated stack buffer
|
||||
// Calculate the size of our allocated stack buffer
|
||||
bufferSize = f->stack_top - f->stack_bottom;
|
||||
|
||||
// If we're too small, increase our buffer size.
|
||||
|
@ -624,10 +624,10 @@ void schedule()
|
|||
// Define the stack base of the forked fiber to be align with the entry point of the parent fiber
|
||||
forkedFiber->tcb.stack_base = currentFiber->tcb.SP;
|
||||
|
||||
// Ensure the stack allocation of the new fiber is large enough
|
||||
// Ensure the stack allocation of the new fiber is large enough
|
||||
verify_stack_size(forkedFiber);
|
||||
|
||||
// Store the full context of this fiber.
|
||||
// Store the full context of this fiber.
|
||||
save_context(&forkedFiber->tcb, forkedFiber->stack_top);
|
||||
|
||||
// We may now be either the newly created thread, or the one that created it.
|
||||
|
@ -638,7 +638,7 @@ void schedule()
|
|||
|
||||
// If we're the new thread, we must have been unblocked by the scheduler, so simply return
|
||||
// and continue processing.
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
// We're in a normal scheduling context, so perform a round robin algorithm across runnable fibers.
|
||||
|
@ -653,7 +653,7 @@ void schedule()
|
|||
else
|
||||
// Otherwise, just pick the head of the run queue.
|
||||
currentFiber = runQueue;
|
||||
|
||||
|
||||
if (currentFiber == idleFiber && oldFiber->flags & MICROBIT_FIBER_FLAG_DO_NOT_PAGE)
|
||||
{
|
||||
// Run the idle task right here using the old fiber's stack.
|
||||
|
@ -676,12 +676,12 @@ void schedule()
|
|||
|
||||
// Swap to the context of the chosen fiber, and we're done.
|
||||
// Don't bother with the overhead of switching if there's only one fiber on the runqueue!
|
||||
if (currentFiber != oldFiber)
|
||||
if (currentFiber != oldFiber)
|
||||
{
|
||||
// Special case for the idle task, as we don't maintain a stack context (just to save memory).
|
||||
if (currentFiber == idleFiber)
|
||||
{
|
||||
idleFiber->tcb.SP = CORTEX_M0_STACK_BASE - 0x04;
|
||||
idleFiber->tcb.SP = CORTEX_M0_STACK_BASE - 0x04;
|
||||
idleFiber->tcb.LR = (uint32_t) &idle_task;
|
||||
}
|
||||
|
||||
|
@ -692,7 +692,7 @@ void schedule()
|
|||
}
|
||||
else
|
||||
{
|
||||
// Ensure the stack allocation of the fiber being scheduled out is large enough
|
||||
// Ensure the stack allocation of the fiber being scheduled out is large enough
|
||||
verify_stack_size(oldFiber);
|
||||
|
||||
// Schedule in the new fiber.
|
||||
|
|
143
source/MicroBitLightSensor.cpp
Normal file
143
source/MicroBitLightSensor.cpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
/**
|
||||
* Class definition for MicroBitLightSensor.
|
||||
*
|
||||
* This is an object that interleaves light sensing with uBit.display.
|
||||
*/
|
||||
|
||||
#include "MicroBit.h"
|
||||
|
||||
/**
|
||||
* After the startSensing method has been called, this method will be called
|
||||
* MICROBIT_LIGHT_SENSOR_AN_SET_TIME after.
|
||||
*
|
||||
* It will then read from the currently selected channel using the AnalogIn
|
||||
* that was configured in the startSensing method.
|
||||
*/
|
||||
void MicroBitLightSensor::analogReady()
|
||||
{
|
||||
this->results[chan] = this->sensePin->read_u16();
|
||||
|
||||
analogDisable();
|
||||
|
||||
DigitalOut((PinName)(MICROBIT_DISPLAY_COLUMN_START + chan)).write(1);
|
||||
|
||||
chan++;
|
||||
|
||||
chan = chan % MICROBIT_LIGHT_SENSOR_CHAN_NUM;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forcibly disables the AnalogIn, otherwise it will remain in possession
|
||||
* of the GPIO channel it is using, meaning that the display will not be
|
||||
* able to use a channel (COL).
|
||||
*
|
||||
* This is required as per PAN 3, details of which can be found here:
|
||||
*
|
||||
* https://www.nordicsemi.com/eng/nordic/download_resource/24634/5/88440387
|
||||
*/
|
||||
void MicroBitLightSensor::analogDisable()
|
||||
{
|
||||
NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled;
|
||||
|
||||
NRF_ADC->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos) |
|
||||
(ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling << ADC_CONFIG_INPSEL_Pos) |
|
||||
(ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
|
||||
(ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) |
|
||||
(ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* The method that is invoked by sending MICROBIT_DISPLAY_EVT_LIGHT_SENSE
|
||||
* using the id MICROBIT_ID_DISPLAY.
|
||||
*
|
||||
* If you want to manually trigger this method, you should use the event bus.
|
||||
*
|
||||
* @note This is currently too churny, and allocates a lot of stuff on the stack
|
||||
* however, this makes this chunk of code platform agnostic in mbed land.
|
||||
*/
|
||||
void MicroBitLightSensor::startSensing(MicroBitEvent)
|
||||
{
|
||||
for(int rowCount = 0; rowCount < MICROBIT_DISPLAY_ROW_COUNT; rowCount++)
|
||||
DigitalOut((PinName)(MICROBIT_DISPLAY_ROW_START + rowCount)).write(0);
|
||||
|
||||
PinName currentPin = (PinName)(MICROBIT_DISPLAY_COLUMN_START + chan);
|
||||
|
||||
DigitalOut(currentPin).write(1);
|
||||
|
||||
DigitalIn(currentPin, PullNone).~DigitalIn();
|
||||
|
||||
if(this->sensePin != NULL)
|
||||
delete this->sensePin;
|
||||
|
||||
this->sensePin = new AnalogIn(currentPin);
|
||||
|
||||
analogTrigger.attach_us(this, &MicroBitLightSensor::analogReady, MICROBIT_LIGHT_SENSOR_AN_SET_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Create a representation of the light sensor
|
||||
*/
|
||||
MicroBitLightSensor::MicroBitLightSensor() : analogTrigger()
|
||||
{
|
||||
this->chan = 0;
|
||||
|
||||
uBit.MessageBus.listen(MICROBIT_ID_DISPLAY, MICROBIT_DISPLAY_EVT_LIGHT_SENSE, this, &MicroBitLightSensor::startSensing, MESSAGE_BUS_LISTENER_IMMEDIATE);
|
||||
|
||||
this->sensePin = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns a summed average of the three sections of the display.
|
||||
*
|
||||
* A section is defined as:
|
||||
* ___________________
|
||||
* | 1 | | 2 | | 3 |
|
||||
* |___|___|___|___|___|
|
||||
* | | | | | |
|
||||
* |___|___|___|___|___|
|
||||
* | 2 | | 3 | | 1 |
|
||||
* |___|___|___|___|___|
|
||||
* | | | | | |
|
||||
* |___|___|___|___|___|
|
||||
* | 3 | | 1 | | 2 |
|
||||
* |___|___|___|___|___|
|
||||
*
|
||||
* Where each number represents a different section on the 5 x 5 matrix display.
|
||||
*
|
||||
* @return returns a value in the range 0 - 100 where 0 is dark, and 100
|
||||
* is very bright
|
||||
*
|
||||
* @note currently returns a value in the range 0 - 100 where 0 is dark, and 100
|
||||
* is very bright perhaps we should normalise the returned values into an SI unit!
|
||||
* TODO.
|
||||
*/
|
||||
int MicroBitLightSensor::read()
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for(int i = 0; i < MICROBIT_LIGHT_SENSOR_CHAN_NUM; i++)
|
||||
sum += results[i];
|
||||
|
||||
int average = sum / MICROBIT_LIGHT_SENSOR_CHAN_NUM;
|
||||
|
||||
average = min(average, MICROBIT_LIGHT_SENSOR_MAX_VALUE);
|
||||
|
||||
average = max(average, MICROBIT_LIGHT_SENSOR_MIN_VALUE);
|
||||
|
||||
int inverted = (MICROBIT_LIGHT_SENSOR_MAX_VALUE - average) + MICROBIT_LIGHT_SENSOR_MIN_VALUE;
|
||||
|
||||
int normalised = ((inverted - MICROBIT_LIGHT_SENSOR_MIN_VALUE) * 100) / (MICROBIT_LIGHT_SENSOR_MAX_VALUE - MICROBIT_LIGHT_SENSOR_MIN_VALUE);
|
||||
|
||||
return normalised;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The destructor restores the default Display Mode and tick speed, and also
|
||||
* removes the listener from the MessageBus.
|
||||
*/
|
||||
MicroBitLightSensor::~MicroBitLightSensor()
|
||||
{
|
||||
uBit.MessageBus.ignore(MICROBIT_ID_DISPLAY, MICROBIT_DISPLAY_EVT_LIGHT_SENSE, this, &MicroBitLightSensor::startSensing);
|
||||
}
|
Loading…
Reference in a new issue