diff --git a/inc/MicroBitCompass.h b/inc/MicroBitCompass.h index a294c65..6bda624 100644 --- a/inc/MicroBitCompass.h +++ b/inc/MicroBitCompass.h @@ -48,7 +48,7 @@ struct MAG3110SampleRateConfig extern const MAG3110SampleRateConfig MAG3110SampleRate[]; -#define MAG3110_SAMPLE_RATES 11 +#define MAG3110_SAMPLE_RATES 11 /* * Compass events @@ -66,7 +66,7 @@ extern const MAG3110SampleRateConfig MAG3110SampleRate[]; */ #define MICROBIT_COMPASS_STATUS_CALIBRATED 1 #define MICROBIT_COMPASS_STATUS_CALIBRATING 2 - + /* * Term to convert sample data into SI units */ @@ -83,19 +83,19 @@ struct CompassSample int x; int y; int z; - + CompassSample() { this->x = 0; this->y = 0; - this->z = 0; + this->z = 0; } CompassSample(int x, int y, int z) { this->x = x; this->y = y; - this->z = z; + this->z = z; } }; @@ -108,11 +108,11 @@ struct CompassSample class MicroBitCompass : public MicroBitComponent { /** - * Unique, enumerated ID for this component. + * Unique, enumerated ID for this component. * Used to track asynchronous events in the event bus. */ - - uint16_t address; // I2C address of the magnetmometer. + + uint16_t address; // I2C address of the magnetmometer. uint16_t samplePeriod; // The time between samples, in millseconds. CompassSample average; // Centre point of sample data. @@ -120,9 +120,9 @@ class MicroBitCompass : public MicroBitComponent DigitalIn int1; // Data ready interrupt. public: - + /** - * Constructor. + * Constructor. * Create a compass representation with the given ID. * @param id the event ID of the compass object. * @param address the default address for the compass register @@ -140,7 +140,7 @@ class MicroBitCompass : public MicroBitComponent * @endcode */ MicroBitCompass(uint16_t id, uint16_t address); - + /** * Configures the compass for the sample rate defined * in this object. The nearest values are chosen to those defined @@ -160,7 +160,7 @@ class MicroBitCompass : public MicroBitComponent int setPeriod(int period); /** - * Reads the currently configured sample rate of the compass. + * Reads the currently configured sample rate of the compass. * @return The time between samples, in milliseconds. */ int getPeriod(); @@ -169,10 +169,10 @@ class MicroBitCompass : public MicroBitComponent * Gets the current heading of the device, relative to magnetic north. * If the compass is not calibrated, it will raise the MICROBIT_COMPASS_EVT_CALIBRATE event. * Users wishing to implement their own calibration algorithms should listen for this event, - * using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before - * the user program continues. - * - * @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating. + * using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before + * the user program continues. + * + * @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating. * * Example: * @code @@ -182,7 +182,7 @@ class MicroBitCompass : public MicroBitComponent int heading(); /** - * Attempts to determine the 8 bit ID from the magnetometer. + * Attempts to determine the 8 bit ID from the magnetometer. * @return the id of the compass (magnetometer), or MICROBIT_I2C_ERROR if the magnetometer could not be updated. * * Example: @@ -202,7 +202,7 @@ class MicroBitCompass : public MicroBitComponent * @endcode */ int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); - + /** * Reads the Y axis value of the latest update from the compass. * @return The magnetic force measured in the Y axis, in nano teslas. @@ -211,9 +211,9 @@ class MicroBitCompass : public MicroBitComponent * @code * uBit.compass.getY(); * @endcode - */ + */ int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); - + /** * Reads the Z axis value of the latest update from the compass. * @return The magnetic force measured in the Z axis, in nano teslas. @@ -222,8 +222,8 @@ class MicroBitCompass : public MicroBitComponent * @code * uBit.compass.getZ(); * @endcode - */ - int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); + */ + int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); /** * Determines the overall magnetic field strength based on the latest update from the compass. @@ -233,21 +233,21 @@ class MicroBitCompass : public MicroBitComponent * @code * uBit.compass.getFieldStrength(); * @endcode - */ + */ int getFieldStrength(); /** - * Reads the current die temperature of the compass. + * Reads the current die temperature of the compass. * @return the temperature in degrees celsius, or MICROBIT_I2C_ERROR if the magnetometer could not be updated. */ int readTemperature(); /** * Perform a calibration of the compass. - * + * * This method will be called automatically if a user attempts to read a compass value when * the compass is uncalibrated. It can also be called at any time by the user. - * + * * Any old calibration data is deleted. * The method will only return once the compass has been calibrated. * @@ -265,7 +265,7 @@ class MicroBitCompass : public MicroBitComponent * @note *** THIS FUNCITON IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE *** * @note *** PLEASE USE THE calibrate() FUNCTION INSTEAD *** */ - void calibrateAsync(); + void calibrateAsync(); /** * Perform a calibration of the compass. @@ -274,62 +274,62 @@ class MicroBitCompass : public MicroBitComponent * @note *** THIS FUNCITON IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE *** * @note *** PLEASE USE THE calibrate() FUNCTION INSTEAD *** */ - int calibrateStart(); + int calibrateStart(); /** * Complete the calibration of the compass. * This will fire MICROBIT_COMPASS_EVT_CAL_END. * * @note *** THIS FUNCITON IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE *** - */ - void calibrateEnd(); + */ + void calibrateEnd(); /** * Configure the compass to use the given calibration data. * Calibration data is comprised of the perceived zero offset of each axis of the compass. - * After calibration this should now take into account trimming errors in the magnetometer, + * After calibration this should now take into account trimming errors in the magnetometer, * and any "hard iron" offsets on the device. * * @param The x, y and z zero offsets to use as calibration data. - */ + */ void setCalibration(CompassSample calibration); /** * Provides the calibration data currently in use by the compass. * More specifically, the x, y and z zero offsets of the compass. * - * @return The x, y and z xero offsets of the compass. - */ + * @return The x, y and z xero offsets of the compass. + */ CompassSample getCalibration(); /** * Periodic callback from MicroBit idle thread. * Check if any data is ready for reading by checking the interrupt. - */ + */ virtual void idleTick(); - + /** * Returns 0 or 1. 1 indicates that the compass is calibrated, zero means the compass requires calibration. */ int isCalibrated(); - + /** * Returns 0 or 1. 1 indicates that the compass is calibrating, zero means the compass is not currently calibrating. */ int isCalibrating(); - + /** * Clears the calibration held in persistent storage, and sets the calibrated flag to zero. */ void clearCalibration(); - + /** * Returns 0 or 1. 1 indicates data is waiting to be read, zero means data is not ready to be read. */ virtual int isIdleCallbackNeeded(); - + private: - + /** * Issues a standard, 2 byte I2C command write to the magnetometer. * Blocks the calling thread until complete. @@ -339,7 +339,7 @@ class MicroBitCompass : public MicroBitComponent * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the magnetometer could not be accessed. */ int writeCommand(uint8_t reg, uint8_t value); - + /** * Issues a read command into the specified buffer. * Blocks the calling thread until complete. @@ -350,7 +350,7 @@ class MicroBitCompass : public MicroBitComponent * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the magnetometer could not be accessed. */ int readCommand(uint8_t reg, uint8_t* buffer, int length); - + /** * Issues a read of a given address, and returns the value. * Blocks the calling thread until complete. @@ -359,8 +359,8 @@ class MicroBitCompass : public MicroBitComponent * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed. */ int read16(uint8_t reg); - - + + /** * Issues a read of a given address, and returns the value. * Blocks the calling thread until complete. diff --git a/inc/MicroBitDisplay.h b/inc/MicroBitDisplay.h index 86e6dd3..6919b60 100644 --- a/inc/MicroBitDisplay.h +++ b/inc/MicroBitDisplay.h @@ -73,7 +73,7 @@ enum AnimationMode { enum DisplayMode { DISPLAY_MODE_BLACK_AND_WHITE, - DISPLAY_MODE_GREYSCALE + DISPLAY_MODE_GREYSCALE }; enum DisplayRotation { @@ -172,24 +172,24 @@ class MicroBitDisplay : public MicroBitComponent * Periodic callback, that we use to perform any animations we have running. */ void animationUpdate(); - + /** * Called by the display in an interval determined by the brightness of the display, to give an impression * of brightness. */ void renderFinish(); - + /** * Translates a bit mask to a bit mask suitable for the nrf PORT0 and PORT1. * Brightness has two levels on, or off. */ void render(); - + /** * Translates a bit mask into a timer interrupt that gives the appearence of greyscale. */ void renderGreyscale(); - + /** * Internal scrollText update method. * Shift the screen image by one pixel to the left. If necessary, paste in the next char. @@ -224,7 +224,7 @@ class MicroBitDisplay : public MicroBitComponent /** * Blocks the current fiber until the display is available (i.e. not effect is being displayed). * Animations are queued until their time to display. - */ + */ void waitForFreeDisplay(); public: @@ -255,16 +255,16 @@ public: * Frame update method, invoked periodically to strobe the display. */ virtual void systemTick(); - + /** * Prints the given character to the display, if it is not in use. * * @param c The character to display. * @param delay Optional parameter - the time for which to show the character. Zero displays the character forever. * @return MICROBIT_OK, MICROBIT_BUSY is the screen is in use, or MICROBIT_INVALID_PARAMETER. - * + * * Example: - * @code + * @code * uBit.display.printAsync('p'); * uBit.display.printAsync('p',100); * @endcode @@ -294,7 +294,7 @@ public: * @param i The image to display. * @param x The horizontal position on the screen to display the image (default 0) * @param y The vertical position on the screen to display the image (default 0) - * @param alpha Treats the brightness level '0' as transparent (default 0) + * @param alpha Treats the brightness level '0' as transparent (default 0) * @param delay The time to delay between characters, in milliseconds. set to 0 to display forever. (default 0). * * Example: @@ -334,7 +334,7 @@ public: * @endcode */ int print(ManagedString s, int delay = MICROBIT_DEFAULT_PRINT_SPEED); - + /** * Prints the given image to the display. * Blocks the calling thread until all the text has been displayed. @@ -350,7 +350,7 @@ public: * @endcode */ int print(MicroBitImage i, int x, int y, int alpha, int delay = 0); - + /** * Scrolls the given string to the display, from right to left. * Uses the given delay between characters. @@ -474,14 +474,14 @@ 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) - * + * * Example: - * @code + * @code * uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); //per pixel brightness * @endcode - */ + */ void setDisplayMode(DisplayMode mode); - + /** * Fetches the current brightness of this display. * @return the brightness of this display, in the range 0..255. @@ -557,7 +557,7 @@ public: * Retreives the font object used for rendering characters on the display. */ MicroBitFont getFont(); - + /** * Captures the bitmap currently being rendered on the display. */ @@ -565,4 +565,3 @@ public: }; #endif - diff --git a/inc/MicroBitMessageBus.h b/inc/MicroBitMessageBus.h index abd38f7..5213266 100644 --- a/inc/MicroBitMessageBus.h +++ b/inc/MicroBitMessageBus.h @@ -18,7 +18,7 @@ * MicroBit platform. It serves a number of purposes: * * 1) It provides an eventing abstraction that is independent of the underlying substrate. - * 2) It provides a mechanism to decouple user code from trusted system code + * 2) It provides a mechanism to decouple user code from trusted system code * i.e. the basis of a message passing nano kernel. * 3) It allows a common high level eventing abstraction across a range of hardware types.e.g. buttons, BLE... * 4) It provides a mechanims for extensibility - new devices added via I/O pins can have OO based @@ -34,23 +34,23 @@ class MicroBitMessageBus : public MicroBitComponent public: /** - * Default constructor. + * Default constructor. * Anticipating only one MessageBus per device, as filtering is handled within the class. */ - MicroBitMessageBus(); + MicroBitMessageBus(); /** * Queues the given event to be sent to all registered recipients. * - * @param The event to send. + * @param The event to send. * * n.b. THIS IS NOW WRAPPED BY THE MicroBitEvent CLASS FOR CONVENIENCE... * * Example: - * @code + * @code * MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN,ticks,false); * evt.fire(); - * //OR YOU CAN DO THIS... + * //OR YOU CAN DO THIS... * MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN); * @endcode */ @@ -63,7 +63,7 @@ class MicroBitMessageBus : public MicroBitComponent * IT IS RECOMMENDED THAT ALL EXTERNAL CODE USE THE send() FUNCTIONS INSTEAD OF THIS FUNCTION, * or the constructors provided by MicroBitEvent. * - * @param evt The event to send. + * @param evt The event to send. * @param urgent The type of listeners to process (optional). If set to true, only listeners defined as urgent and non-blocking will be processed * otherwise, all other (standard) listeners will be processed. * @return 1 if all matching listeners were processed, 0 if further processing is required. @@ -72,19 +72,19 @@ class MicroBitMessageBus : public MicroBitComponent /** * Register a listener function. - * - * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. + * + * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. * Use MICROBIT_ID_ANY to receive events from all components. * - * @param value The value of messages to listen for. Events with any other values will be filtered. + * @param value The value of messages to listen for. Events with any other values will be filtered. * Use MICROBIT_EVT_ANY to receive events of any value. * * @param handler The function to call when an event is received. * * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER - * + * * Example: - * @code + * @code * void onButtonBClick() * { * //do something @@ -93,22 +93,22 @@ class MicroBitMessageBus : public MicroBitComponent * @endcode */ int listen(int id, int value, void (*handler)(MicroBitEvent), uint16_t flags = MESSAGE_BUS_LISTENER_DEFAULT_FLAGS); - + /** * Register a listener function. - * - * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. + * + * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. * Use MICROBIT_ID_ANY to receive events from all components. * - * @param value The value of messages to listen for. Events with any other values will be filtered. + * @param value The value of messages to listen for. Events with any other values will be filtered. * Use MICROBIT_EVT_ANY to receive events of any value. * * @param hander The function to call when an event is received. * * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER - * + * * Example: - * @code + * @code * void onButtonBClick(void *arg) * { * //do something @@ -120,26 +120,26 @@ class MicroBitMessageBus : public MicroBitComponent /** * Register a listener function. - * - * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. + * + * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. * Use MICROBIT_ID_ANY to receive events from all components. * - * @param value The value of messages to listen for. Events with any other values will be filtered. + * @param value The value of messages to listen for. Events with any other values will be filtered. * Use MICROBIT_EVT_ANY to receive events of any value. * * @param hander The function to call when an event is received. * * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER - * + * * Example: - * @code + * @code * void SomeClass::onButtonBClick() * { * //do something * } * * SomeClass s = new SomeClass(); - * uBit.MessageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick); + * uBit.MessageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick); * @endcode */ template @@ -149,7 +149,7 @@ class MicroBitMessageBus : public MicroBitComponent /** * Unregister a listener function. * Listners are identified by the Event ID, Event VALUE and handler registered using listen(). - * + * * @param id The Event ID used to register the listener. * @param value The Event VALUE used to register the listener. * @param handler The function used to register the listener. @@ -158,21 +158,21 @@ class MicroBitMessageBus : public MicroBitComponent * * * Example: - * @code + * @code * void onButtonBClick() * { * //do something * } * - * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); + * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); * @endcode */ int ignore(int id, int value, void (*handler)(MicroBitEvent)); - + /** * Unregister a listener function. * Listners are identified by the Event ID, Event VALUE and handler registered using listen(). - * + * * @param id The Event ID used to register the listener. * @param value The Event VALUE used to register the listener. * @param handler The function used to register the listener. @@ -180,13 +180,13 @@ class MicroBitMessageBus : public MicroBitComponent * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER * * Example: - * @code + * @code * void onButtonBClick(void *arg) * { * //do something * } * - * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); + * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); * @endcode */ int ignore(int id, int value, void (*handler)(MicroBitEvent, void*)); @@ -194,7 +194,7 @@ class MicroBitMessageBus : public MicroBitComponent /** * Unregister a listener function. * Listners are identified by the Event ID, Event VALUE and handler registered using listen(). - * + * * @param id The Event ID used to register the listener. * @param value The Event VALUE used to register the listener. * @param handler The function used to register the listener. @@ -202,15 +202,15 @@ class MicroBitMessageBus : public MicroBitComponent * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER * * Example: - * @code - * + * @code + * * void SomeClass::onButtonBClick() * { * //do something * } * * SomeClass s = new SomeClass(); - * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick); + * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, s, &SomeClass::onButtonBClick); * @endcode */ template @@ -250,7 +250,7 @@ class MicroBitMessageBus : public MicroBitComponent MicroBitEventQueueItem *evt_queue_tail; // Tail of queued events to be processed. uint16_t nonce_val; // The last nonce issued. uint16_t queueLength; // The number of events currently waiting to be processed. - + void queueEvent(MicroBitEvent &evt); MicroBitEventQueueItem* dequeueEvent(); @@ -262,10 +262,10 @@ class MicroBitMessageBus : public MicroBitComponent * A registration function to allow C++ member funcitons (methods) to be registered as an event * listener. * - * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. + * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. * Use MICROBIT_ID_ANY to receive events from all components. * - * @param value The value of messages to listen for. Events with any other values will be filtered. + * @param value The value of messages to listen for. Events with any other values will be filtered. * Use MICROBIT_EVT_ANY to receive events of any value. * * @param object The object on which the method should be invoked. @@ -291,7 +291,7 @@ int MicroBitMessageBus::listen(uint16_t id, uint16_t value, T* object, void (T:: /** * Unregister a listener function. * Listners are identified by the Event ID, Event VALUE and handler registered using listen(). - * + * * @param id The Event ID used to register the listener. * @param value The Event VALUE used to register the listener. * @param handler The function used to register the listener. @@ -299,13 +299,13 @@ int MicroBitMessageBus::listen(uint16_t id, uint16_t value, T* object, void (T:: * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER * * Example: - * @code + * @code * void onButtonBClick(void *arg) * { * //do something * } * - * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); + * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); * @endcode */ template @@ -322,5 +322,3 @@ int MicroBitMessageBus::ignore(uint16_t id, uint16_t value, T* object, void (T:: #endif - - diff --git a/source/MicroBitCompass.cpp b/source/MicroBitCompass.cpp index e3610da..26edc9e 100644 --- a/source/MicroBitCompass.cpp +++ b/source/MicroBitCompass.cpp @@ -1,7 +1,7 @@ #include "MicroBit.h" /** - * Constructor. + * Constructor. * Create a compass representation with the given ID. * @param id the event ID of the compass object. * @param address the default address for the compass register @@ -22,17 +22,17 @@ MicroBitCompass::MicroBitCompass(uint16_t id, uint16_t address) : average(), sam { this->id = id; this->address = address; - + // We presume the device calibrated until the average values are read. this->status = 0x01; // Select 10Hz update rate, with oversampling, and enable the device. this->samplePeriod = 100; this->configure(); - - // Assume that we have no calibraiton information. + + // Assume that we have no calibraiton information. status &= ~MICROBIT_COMPASS_STATUS_CALIBRATED; - + // Indicate that we're up and running. uBit.flags |= MICROBIT_FLAG_COMPASS_RUNNING; } @@ -50,7 +50,7 @@ int MicroBitCompass::writeCommand(uint8_t reg, uint8_t value) uint8_t command[2]; command[0] = reg; command[1] = value; - + return uBit.i2c.write(address, (const char *)command, 2); } @@ -100,7 +100,7 @@ int MicroBitCompass::read16(uint8_t reg) cmd[0] = 0x00; cmd[1] = 0x00; - + result = uBit.i2c.read(address, (char *)cmd, 2); if (result !=0) return MICROBIT_I2C_ERROR; @@ -132,10 +132,10 @@ int MicroBitCompass::read8(uint8_t reg) * Gets the current heading of the device, relative to magnetic north. * If the compass is not calibrated, it will raise the MICROBIT_COMPASS_EVT_CALIBRATE event. * Users wishing to implement their own calibration algorithms should listen for this event, - * using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before - * the user program continues. - * - * @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating. + * using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before + * the user program continues. + * + * @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating. * * Example: * @code @@ -147,7 +147,7 @@ int MicroBitCompass::heading() float bearing; if(status & MICROBIT_COMPASS_STATUS_CALIBRATING) - return MICROBIT_CALIBRATION_IN_PROGRESS; + return MICROBIT_CALIBRATION_IN_PROGRESS; if(!(status & MICROBIT_COMPASS_STATUS_CALIBRATED)) calibrate(); @@ -165,11 +165,11 @@ int MicroBitCompass::heading() float sinTheta = sin(theta); float cosTheta = cos(theta); - bearing = (360*atan2(z*sinPhi - y*cosPhi, x*cosTheta + y*sinTheta*sinPhi + z*sinTheta*cosPhi)) / (2*PI); + bearing = (360*atan2(z*sinPhi - y*cosPhi, x*cosTheta + y*sinTheta*sinPhi + z*sinTheta*cosPhi)) / (2*PI); if (bearing < 0) bearing += 360.0; - + return (int) bearing; } @@ -179,7 +179,7 @@ int MicroBitCompass::heading() */ void MicroBitCompass::idleTick() { - // Poll interrupt line from accelerometer (Active HI). + // Poll interrupt line from accelerometer (Active HI). // Interrupt is cleared on data read of MAG_OUT_X_MSB. if(int1) { @@ -225,7 +225,7 @@ int MicroBitCompass::getX(MicroBitCoordinateSystem system) * @code * uBit.compass.getY(); * @endcode - */ + */ int MicroBitCompass::getY(MicroBitCoordinateSystem system) { switch (system) @@ -235,7 +235,7 @@ int MicroBitCompass::getY(MicroBitCoordinateSystem system) case NORTH_EAST_DOWN: return (sample.x - average.x); - + case RAW: default: return sample.y; @@ -250,7 +250,7 @@ int MicroBitCompass::getY(MicroBitCoordinateSystem system) * @code * uBit.compass.getZ(); * @endcode - */ + */ int MicroBitCompass::getZ(MicroBitCoordinateSystem system) { switch (system) @@ -273,7 +273,7 @@ int MicroBitCompass::getZ(MicroBitCoordinateSystem system) * @code * uBit.compass.getFieldStrength(); * @endcode - */ + */ int MicroBitCompass::getFieldStrength() { double x = getX(); @@ -300,7 +300,7 @@ int MicroBitCompass::configure() if (result != MICROBIT_OK) return MICROBIT_I2C_ERROR; - // Wait for the part to enter standby mode... + // Wait for the part to enter standby mode... while(1) { // Read the status of the part... @@ -335,7 +335,7 @@ int MicroBitCompass::configure() if (result != MICROBIT_OK) return MICROBIT_I2C_ERROR; - + // Bring the device online, with the requested sample frequency. result = writeCommand(MAG_CTRL_REG1, actualSampleRate->ctrl_reg1 | 0x01); if (result != MICROBIT_OK) @@ -358,7 +358,7 @@ int MicroBitCompass::setPeriod(int period) } /** - * Reads the currently configured sample rate of the compass. + * Reads the currently configured sample rate of the compass. * @return The time between samples, in milliseconds. */ int MicroBitCompass::getPeriod() @@ -368,7 +368,7 @@ int MicroBitCompass::getPeriod() /** - * Attempts to determine the 8 bit ID from the magnetometer. + * Attempts to determine the 8 bit ID from the magnetometer. * @return the id of the compass (magnetometer), or MICROBIT_I2C_ERROR if the magnetometer could not be updated. * * Example: @@ -381,7 +381,7 @@ int MicroBitCompass::whoAmI() uint8_t data; int result; - result = readCommand(MAG_WHOAMI, &data, 1); + result = readCommand(MAG_WHOAMI, &data, 1); if (result != MICROBIT_OK) return MICROBIT_I2C_ERROR; @@ -389,7 +389,7 @@ int MicroBitCompass::whoAmI() } /** - * Reads the current die temperature of the compass. + * Reads the current die temperature of the compass. * @return the temperature in degrees celsius, or MICROBIT_I2C_ERROR if the magnetometer could not be updated. */ int MicroBitCompass::readTemperature() @@ -406,10 +406,10 @@ int MicroBitCompass::readTemperature() /** * Perform a calibration of the compass. - * + * * This method will be called automatically if a user attempts to read a compass value when * the compass is uncalibrated. It can also be called at any time by the user. - * + * * Any old calibration data is deleted. * The method will only return once the compass has been calibrated. * @@ -453,7 +453,7 @@ int MicroBitCompass::calibrate() int MicroBitCompass::calibrateStart() { return calibrate(); -} +} /** * Perform the asynchronous calibration of the compass. @@ -463,7 +463,7 @@ int MicroBitCompass::calibrateStart() * @note *** PLEASE USE THE calibrate() FUNCTION INSTEAD *** */ void MicroBitCompass::calibrateAsync() -{ +{ calibrate(); } @@ -472,20 +472,20 @@ void MicroBitCompass::calibrateAsync() * This will fire MICROBIT_COMPASS_EVT_CAL_END. * * @note *** THIS FUNCTION IS NOW DEPRECATED AND WILL BE REMOVED IN THE NEXT MAJOR RELEASE *** - */ + */ void MicroBitCompass::calibrateEnd() { -} +} /** * Configure the compass to use the given calibration data. * Calibration data is comprised of the perceived zero offset of each axis of the compass. - * After calibration this should now take into account trimming errors in the magnetometer, + * After calibration this should now take into account trimming errors in the magnetometer, * and any "hard iron" offsets on the device. * * @param The x, y and z zero offsets to use as calibration data. - */ + */ void MicroBitCompass::setCalibration(CompassSample calibration) { average = calibration; @@ -496,8 +496,8 @@ void MicroBitCompass::setCalibration(CompassSample calibration) * Provides the calibration data currently in use by the compass. * More specifically, the x, y and z zero offsets of the compass. * - * @return The x, y and z xero offsets of the compass. - */ + * @return The x, y and z xero offsets of the compass. + */ CompassSample MicroBitCompass::getCalibration() { return average; @@ -508,7 +508,7 @@ CompassSample MicroBitCompass::getCalibration() */ int MicroBitCompass::isCalibrated() { - return status & MICROBIT_COMPASS_STATUS_CALIBRATED; + return status & MICROBIT_COMPASS_STATUS_CALIBRATED; } /** @@ -516,7 +516,7 @@ int MicroBitCompass::isCalibrated() */ int MicroBitCompass::isCalibrating() { - return status & MICROBIT_COMPASS_STATUS_CALIBRATING; + return status & MICROBIT_COMPASS_STATUS_CALIBRATING; } /** @@ -524,7 +524,7 @@ int MicroBitCompass::isCalibrating() */ void MicroBitCompass::clearCalibration() { - status &= ~MICROBIT_COMPASS_STATUS_CALIBRATED; + status &= ~MICROBIT_COMPASS_STATUS_CALIBRATED; } /** @@ -540,13 +540,13 @@ int MicroBitCompass::isIdleCallbackNeeded() const MAG3110SampleRateConfig MAG3110SampleRate[MAG3110_SAMPLE_RATES] = { {12500, 0x00}, // 80 Hz {25000, 0x20}, // 40 Hz - {50000, 0x40}, // 20 Hz - {100000, 0x60}, // 10 hz - {200000, 0x80}, // 5 hz - {400000, 0x88}, // 2.5 hz - {800000, 0x90}, // 1.25 hz - {1600000, 0xb0}, // 0.63 hz - {3200000, 0xd0}, // 0.31 hz - {6400000, 0xf0}, // 0.16 hz - {12800000, 0xf8} // 0.08 hz + {50000, 0x40}, // 20 Hz + {100000, 0x60}, // 10 hz + {200000, 0x80}, // 5 hz + {400000, 0x88}, // 2.5 hz + {800000, 0x90}, // 1.25 hz + {1600000, 0xb0}, // 0.63 hz + {3200000, 0xd0}, // 0.31 hz + {6400000, 0xf0}, // 0.16 hz + {12800000, 0xf8} // 0.08 hz }; diff --git a/source/MicroBitDisplay.cpp b/source/MicroBitDisplay.cpp index 1513c10..7822f0f 100644 --- a/source/MicroBitDisplay.cpp +++ b/source/MicroBitDisplay.cpp @@ -17,19 +17,19 @@ const float timings[MICROBIT_DISPLAY_GREYSCALE_BIT_DEPTH] = {0.000010, 0.000047, * * @param x the width of the display in pixels. * @param y the height of the display in pixels. - * + * * Example: - * @code + * @code * MicroBitDisplay display(MICROBIT_ID_DISPLAY, 5, 5), * @endcode */ -MicroBitDisplay::MicroBitDisplay(uint16_t id, uint8_t x, uint8_t y) : +MicroBitDisplay::MicroBitDisplay(uint16_t id, uint8_t x, uint8_t y) : font(), image(x*2,y) { //set pins as output nrf_gpio_range_cfg_output(MICROBIT_DISPLAY_COLUMN_START,MICROBIT_DISPLAY_COLUMN_START + MICROBIT_DISPLAY_COLUMN_COUNT + MICROBIT_DISPLAY_ROW_COUNT); - + this->id = id; this->width = x; this->height = y; @@ -38,46 +38,46 @@ MicroBitDisplay::MicroBitDisplay(uint16_t id, uint8_t x, uint8_t y) : this->rotation = MICROBIT_DISPLAY_ROTATION_0; this->greyscaleBitMsk = 0x01; this->timingCount = 0; - + this->setBrightness(MICROBIT_DISPLAY_DEFAULT_BRIGHTNESS); this->mode = DISPLAY_MODE_BLACK_AND_WHITE; this->animationMode = ANIMATION_MODE_NONE; - + uBit.flags |= MICROBIT_FLAG_DISPLAY_RUNNING; } /** * Internal frame update method, used to strobe the display. * - * TODO: Write a more efficient, complementary variation of this method for the case where + * TODO: Write a more efficient, complementary variation of this method for the case where * MICROBIT_DISPLAY_ROW_COUNT > MICROBIT_DISPLAY_COLUMN_COUNT. - */ + */ void MicroBitDisplay::systemTick() -{ +{ if(!(uBit.flags & MICROBIT_FLAG_DISPLAY_RUNNING)) return; - - // Move on to the next row. + + // Move on to the next row. strobeBitMsk <<= 1; strobeRow++; - + //reset the row counts and bit mask when we have hit the max. if(strobeRow == MICROBIT_DISPLAY_ROW_COUNT){ strobeRow = 0; - strobeBitMsk = 0x20; + strobeBitMsk = 0x20; } - + if(mode == DISPLAY_MODE_BLACK_AND_WHITE) render(); - + if(mode == DISPLAY_MODE_GREYSCALE) { greyscaleBitMsk = 0x01; timingCount = 0; renderGreyscale(); } - + // Update text and image animations if we need to. this->animationUpdate(); } @@ -87,60 +87,60 @@ void MicroBitDisplay::renderFinish() //kept inline to reduce overhead //clear the old bit pattern for this row. //clear port 0 4-7 and retain lower 4 bits - nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT0, 0xF0 | (nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0) & 0x0F)); - + nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT0, 0xF0 | (nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0) & 0x0F)); + // clear port 1 8-12 for the current row - nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobeBitMsk | 0x1F); + nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobeBitMsk | 0x1F); } void MicroBitDisplay::render() -{ +{ // Simple optimisation. If display is at zero brightness, there's nothign to do. if(brightness == 0) return; int coldata = 0; - + // Calculate the bitpattern to write. for (int i = 0; i>4 & 0x1F)); + nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobeBitMsk | (~coldata>>4 & 0x1F)); //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); - + //this will take around 23us to execute if(brightness <= MICROBIT_DISPLAY_MINIMUM_BRIGHTNESS) renderFinish(); @@ -149,47 +149,47 @@ void MicroBitDisplay::render() void MicroBitDisplay::renderGreyscale() { int coldata = 0; - + // Calculate the bitpattern to write. for (int i = 0; i>4 & 0x1F)); + nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobeBitMsk | (~coldata>>4 & 0x1F)); if(timingCount > MICROBIT_DISPLAY_GREYSCALE_BIT_DEPTH-1) return; greyscaleBitMsk <<= 1; - + renderTimer.attach(this,&MicroBitDisplay::renderGreyscale, timings[timingCount++]); } @@ -198,29 +198,29 @@ void MicroBitDisplay::renderGreyscale() */ void MicroBitDisplay::animationUpdate() -{ +{ // If there's no ongoing animation, then nothing to do. if (animationMode == ANIMATION_MODE_NONE) return; - - animationTick += FIBER_TICK_PERIOD_MS; - + + animationTick += FIBER_TICK_PERIOD_MS; + if(animationTick >= animationDelay) { animationTick = 0; - + if (animationMode == ANIMATION_MODE_SCROLL_TEXT) this->updateScrollText(); - + if (animationMode == ANIMATION_MODE_PRINT_TEXT) this->updatePrintText(); if (animationMode == ANIMATION_MODE_SCROLL_IMAGE) this->updateScrollImage(); - + if (animationMode == ANIMATION_MODE_ANIMATE_IMAGE) this->updateAnimateImage(); - + if(animationMode == ANIMATION_MODE_PRINT_CHARACTER) { animationMode = ANIMATION_MODE_NONE; @@ -243,18 +243,18 @@ void MicroBitDisplay::sendAnimationCompleteEvent() } /** - * Internal scrollText update method. + * Internal scrollText update method. * Shift the screen image by one pixel to the left. If necessary, paste in the next char. - */ + */ void MicroBitDisplay::updateScrollText() -{ +{ image.shiftLeft(1); scrollingPosition++; - + if (scrollingPosition == width + MICROBIT_DISPLAY_SPACING) - { + { scrollingPosition = 0; - + image.print(scrollingChar < scrollingText.length() ? scrollingText.charAt(scrollingChar) : ' ',width,0); if (scrollingChar > scrollingText.length()) @@ -268,36 +268,36 @@ void MicroBitDisplay::updateScrollText() } /** - * Internal printText update method. + * Internal printText update method. * Paste in the next char in the string. - */ + */ void MicroBitDisplay::updatePrintText() -{ +{ image.print(printingChar < printingText.length() ? printingText.charAt(printingChar) : ' ',0,0); if (printingChar > printingText.length()) { - animationMode = ANIMATION_MODE_NONE; + animationMode = ANIMATION_MODE_NONE; this->sendAnimationCompleteEvent(); return; } - + printingChar++; } /** - * Internal scrollImage update method. + * Internal scrollImage update method. * Paste the stored bitmap at the appropriate point. - */ + */ void MicroBitDisplay::updateScrollImage() -{ - image.clear(); +{ + image.clear(); if ((image.paste(scrollingImage, scrollingImagePosition, 0, 0) == 0) && scrollingImageRendered) { - animationMode = ANIMATION_MODE_NONE; - this->sendAnimationCompleteEvent(); + animationMode = ANIMATION_MODE_NONE; + this->sendAnimationCompleteEvent(); return; } @@ -307,27 +307,27 @@ void MicroBitDisplay::updateScrollImage() } /** - * Internal animateImage update method. + * Internal animateImage update method. * Paste the stored bitmap at the appropriate point and stop on the last frame. - */ + */ void MicroBitDisplay::updateAnimateImage() -{ +{ //wait until we have rendered the last position to give a continuous animation. if (scrollingImagePosition <= -scrollingImage.getWidth() + (MICROBIT_DISPLAY_WIDTH + scrollingImageStride) && scrollingImageRendered) { - animationMode = ANIMATION_MODE_NONE; + animationMode = ANIMATION_MODE_NONE; this->clear(); - this->sendAnimationCompleteEvent(); + this->sendAnimationCompleteEvent(); return; } - + if(scrollingImagePosition > 0) image.shiftLeft(-scrollingImageStride); - + image.paste(scrollingImage, scrollingImagePosition, 0, 0); - + scrollingImageRendered = true; - + scrollingImagePosition += scrollingImageStride; } @@ -347,7 +347,7 @@ void MicroBitDisplay::stopAnimation() // Wake up aall fibers that may blocked on the animation (if any). MicroBitEvent(MICROBIT_ID_NOTIFY, MICROBIT_DISPLAY_EVT_FREE); } - + // Clear the display and setup the animation timers. this->image.clear(); } @@ -356,7 +356,7 @@ void MicroBitDisplay::stopAnimation() * Blocks the current fiber until the display is available (i.e. not effect is being displayed). * Animations are queued until their time to display. * - */ + */ void MicroBitDisplay::waitForFreeDisplay() { // If there's an ongoing animation, wait for our turn to display. @@ -371,9 +371,9 @@ void MicroBitDisplay::waitForFreeDisplay() * @param c The character to display. * @param delay Optional parameter - the time for which to show the character. Zero displays the character forever. * @return MICROBIT_OK, MICROBIT_BUSY is the screen is in use, or MICROBIT_INVALID_PARAMETER. - * + * * Example: - * @code + * @code * uBit.display.printAsync('p'); * uBit.display.printAsync('p',100); * @endcode @@ -422,7 +422,7 @@ int MicroBitDisplay::printAsync(ManagedString s, int delay) //sanitise this value if(delay <= 0 ) return MICROBIT_INVALID_PARAMETER; - + if (animationMode == ANIMATION_MODE_NONE || animationMode == ANIMATION_MODE_STOPPED) { printingChar = 0; @@ -431,7 +431,7 @@ int MicroBitDisplay::printAsync(ManagedString s, int delay) animationTick = 0; animationMode = ANIMATION_MODE_PRINT_TEXT; - } + } else { return MICROBIT_BUSY; @@ -448,7 +448,7 @@ int MicroBitDisplay::printAsync(ManagedString s, int delay) * @param i The image to display. * @param x The horizontal position on the screen to display the image (default 0) * @param y The vertical position on the screen to display the image (default 0) - * @param alpha Treats the brightness level '0' as transparent (default 0) + * @param alpha Treats the brightness level '0' as transparent (default 0) * @param delay The time to delay between characters, in milliseconds. set to 0 to display forever. (default 0). * * Example: @@ -487,9 +487,9 @@ int MicroBitDisplay::printAsync(MicroBitImage i, int x, int y, int alpha, int de * @param c The character to display. * @param delay The time to delay between characters, in milliseconds. Must be > 0. * @return MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER. - * + * * Example: - * @code + * @code * uBit.display.print('p'); * uBit.display.print('p',100); * @endcode @@ -537,7 +537,7 @@ int MicroBitDisplay::print(ManagedString s, int delay) //sanitise this value if(delay <= 0 ) return MICROBIT_INVALID_PARAMETER; - + // If there's an ongoing animation, wait for our turn to display. this->waitForFreeDisplay(); @@ -613,7 +613,7 @@ int MicroBitDisplay::scrollAsync(ManagedString s, int delay) //sanitise this value if(delay <= 0) return MICROBIT_INVALID_PARAMETER; - + // If the display is free, it's our turn to display. if (animationMode == ANIMATION_MODE_NONE || animationMode == ANIMATION_MODE_STOPPED) { @@ -649,7 +649,7 @@ int MicroBitDisplay::scrollAsync(ManagedString s, int delay) * @endcode */ int MicroBitDisplay::scrollAsync(MicroBitImage image, int delay, int stride) -{ +{ //sanitise the delay value if(delay <= 0) return MICROBIT_INVALID_PARAMETER; @@ -682,9 +682,9 @@ int MicroBitDisplay::scrollAsync(MicroBitImage image, int delay, int stride) * @param s The string to display. * @param delay The time to delay between each update to the display, in milliseconds. Must be > 0. * @return MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER. - * + * * Example: - * @code + * @code * uBit.display.scrollString("abc123",100); * @endcode */ @@ -706,12 +706,12 @@ int MicroBitDisplay::scroll(ManagedString s, int delay) // Wait for completion. fiber_wait_for_event(MICROBIT_ID_DISPLAY, MICROBIT_DISPLAY_EVT_ANIMATION_COMPLETE); - } + } else { return MICROBIT_CANCELLED; } - + return MICROBIT_OK; } @@ -731,11 +731,11 @@ int MicroBitDisplay::scroll(ManagedString s, int delay) * @endcode */ int MicroBitDisplay::scroll(MicroBitImage image, int delay, int stride) -{ +{ //sanitise the delay value if(delay <= 0) return MICROBIT_INVALID_PARAMETER; - + // If there's an ongoing animation, wait for our turn to display. this->waitForFreeDisplay(); @@ -789,7 +789,7 @@ int MicroBitDisplay::animateAsync(MicroBitImage image, int delay, int stride, in stride = -stride; //calculate starting position which is offset by the stride - scrollingImagePosition = (startingPosition == MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS) ? MICROBIT_DISPLAY_WIDTH + stride : startingPosition; + scrollingImagePosition = (startingPosition == MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS) ? MICROBIT_DISPLAY_WIDTH + stride : startingPosition; scrollingImageStride = stride; scrollingImage = image; scrollingImageRendered = false; @@ -858,14 +858,14 @@ int MicroBitDisplay::animate(MicroBitImage image, int delay, int stride, int sta * Sets the display brightness to the specified level. * @param b The brightness to set the brightness to, in the range 0..255. * @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER - * + * * Example: - * @code + * @code * uBit.display.setBrightness(255); //max brightness * @endcode - */ + */ int MicroBitDisplay::setBrightness(int b) -{ +{ //sanitise the brightness level if(b < 0 || b > 255) return MICROBIT_INVALID_PARAMETER; @@ -878,26 +878,26 @@ 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) - * + * * Example: - * @code + * @code * uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); //per pixel brightness * @endcode - */ + */ void MicroBitDisplay::setDisplayMode(DisplayMode mode) -{ +{ this->mode = mode; } /** * Fetches the current brightness of this display. * @return the brightness of this display, in the range 0..255. - * + * * Example: - * @code + * @code * uBit.display.getBrightness(); //the current brightness * @endcode - */ + */ int MicroBitDisplay::getBrightness() { return this->brightness; @@ -908,10 +908,10 @@ int MicroBitDisplay::getBrightness() * Axis aligned values only. * * Example: - * @code + * @code * uBit.display.rotateTo(MICROBIT_DISPLAY_ROTATION_180); //rotates 180 degrees from original orientation * @endcode - */ + */ void MicroBitDisplay::rotateTo(DisplayRotation rotation) { this->rotation = rotation; @@ -921,7 +921,7 @@ void MicroBitDisplay::rotateTo(DisplayRotation rotation) * Enables the display, should only be called if the display is disabled. * * Example: - * @code + * @code * uBit.display.enable(); //reenables the display mechanics * @endcode */ @@ -933,13 +933,13 @@ void MicroBitDisplay::enable() uBit.flags |= MICROBIT_FLAG_DISPLAY_RUNNING; //set the display running flag } } - + /** * Disables the display, should only be called if the display is enabled. * Display must be disabled to avoid MUXing of edge connector pins. * * Example: - * @code + * @code * uBit.display.disable(); //disables the display * @endcode */ @@ -948,7 +948,7 @@ void MicroBitDisplay::disable() if(uBit.flags & MICROBIT_FLAG_DISPLAY_RUNNING) { uBit.flags &= ~MICROBIT_FLAG_DISPLAY_RUNNING; //unset the display running flag - } + } } /** @@ -956,13 +956,13 @@ void MicroBitDisplay::disable() * Simplifies the process, you can also use uBit.display.image.clear * * Example: - * @code + * @code * uBit.display.clear(); //clears the display * @endcode - */ + */ void MicroBitDisplay::clear() { - image.clear(); + image.clear(); } /** @@ -970,12 +970,12 @@ void MicroBitDisplay::clear() * @param statusCode the appropriate status code - 0 means no code will be displayed. Status codes must be in the range 0-255. * * Example: - * @code + * @code * uBit.display.error(20); * @endcode */ void MicroBitDisplay::error(int statusCode) -{ +{ extern InterruptIn resetButton; __disable_irq(); //stop ALL interrupts @@ -984,16 +984,16 @@ void MicroBitDisplay::error(int statusCode) statusCode = 0; disable(); //relinquish PWMOut's control - + uint8_t strobeRow = 0; uint8_t strobeBitMsk = 0x20; - + //point to the font stored in Flash const unsigned char * fontLocation = MicroBitFont::defaultFont; - + //get individual digits of status code, and place it into a single array/ const uint8_t* chars[MICROBIT_DISPLAY_ERROR_CHARS] = { panicFace, fontLocation+((((statusCode/100 % 10)+48)-MICROBIT_FONT_ASCII_START) * 5), fontLocation+((((statusCode/10 % 10)+48)-MICROBIT_FONT_ASCII_START) * 5), fontLocation+((((statusCode % 10)+48)-MICROBIT_FONT_ASCII_START) * 5)}; - + //enter infinite loop. while(1) { @@ -1001,42 +1001,42 @@ void MicroBitDisplay::error(int statusCode) for(int characterCount = 0; characterCount < MICROBIT_DISPLAY_ERROR_CHARS; characterCount++) { int outerCount = 0; - + //display the current character while(outerCount < 500) { int coldata = 0; - + int i = 0; - + //if we have hit the row limit - reset both the bit mask and the row variable if(strobeRow == 3) { - strobeRow = 0; + strobeRow = 0; strobeBitMsk = 0x20; - } - + } + // Calculate the bitpattern to write. for (i = 0; i> matrixMap[i][strobeRow].x; //chars are right aligned but read left to right int y = matrixMap[i][strobeRow].y; - + if(chars[characterCount][y] & bitMsk) coldata |= (1 << i); } - + nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT0, 0xF0); //clear port 0 4-7 nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobeBitMsk | 0x1F); // clear port 1 8-12 - + //write the new bit pattern nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT0, ~coldata<<4 & 0xF0); //set port 0 4-7 nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, strobeBitMsk | (~coldata>>4 & 0x1F)); //set port 1 8-12 - + //set i to an obscene number. i = 1000; - + //burn cycles while(i>0) { @@ -1046,9 +1046,9 @@ void MicroBitDisplay::error(int statusCode) i--; } - + //update the bit mask and row count - strobeBitMsk <<= 1; + strobeBitMsk <<= 1; strobeRow++; outerCount++; } diff --git a/source/MicroBitMessageBus.cpp b/source/MicroBitMessageBus.cpp index bf85e52..51d6ec3 100644 --- a/source/MicroBitMessageBus.cpp +++ b/source/MicroBitMessageBus.cpp @@ -7,7 +7,7 @@ #include "MicroBit.h" /** - * Constructor. + * Constructor. * Create a new Message Bus. */ MicroBitMessageBus::MicroBitMessageBus() @@ -30,7 +30,7 @@ void async_callback(void *param) // OK, now we need to decide how to behave depending on our configuration. // If this a fiber f already active within this listener then check our - // configuration to determine the correct course of action. + // configuration to determine the correct course of action. // if (listener->flags & MESSAGE_BUS_LISTENER_BUSY) @@ -47,7 +47,7 @@ void async_callback(void *param) } } - // Determine the calling convention for the callback, and invoke... + // Determine the calling convention for the callback, and invoke... // C++ is really bad at this! Especially as the ARM compiler is yet to support C++ 11 :-/ // Record that we have a fiber going into this listener... @@ -91,7 +91,7 @@ void async_callback(void *param) * Queue the given event for processing at a later time. * Add the given event at the tail of our queue. * - * @param The event to queue. + * @param The event to queue. */ void MicroBitMessageBus::queueEvent(MicroBitEvent &evt) { @@ -99,7 +99,7 @@ void MicroBitMessageBus::queueEvent(MicroBitEvent &evt) MicroBitEventQueueItem *prev = evt_queue_tail; - // Now process all handler regsitered as URGENT. + // Now process all handler regsitered as URGENT. // These pre-empt the queue, and are useful for fast, high priority services. processingComplete = this->process(evt, true); @@ -125,7 +125,7 @@ void MicroBitMessageBus::queueEvent(MicroBitEvent &evt) { item->next = evt_queue_head; evt_queue_head = item; - } + } else { item->next = prev->next; @@ -142,18 +142,18 @@ void MicroBitMessageBus::queueEvent(MicroBitEvent &evt) /** * Extract the next event from the front of the event queue (if present). - * @return + * @return * - * @param The event to queue. + * @param The event to queue. */ MicroBitEventQueueItem* MicroBitMessageBus::dequeueEvent() { MicroBitEventQueueItem *item = NULL; __disable_irq(); - + if (evt_queue_head != NULL) - { + { item = evt_queue_head; evt_queue_head = item->next; @@ -162,7 +162,7 @@ MicroBitEventQueueItem* MicroBitMessageBus::dequeueEvent() queueLength--; } - + __enable_irq(); @@ -188,7 +188,7 @@ int MicroBitMessageBus::deleteMarkedListeners() { if (p == NULL) listeners = l->next; - else + else p->next = l->next; // delete the listener. @@ -212,7 +212,7 @@ int MicroBitMessageBus::deleteMarkedListeners() * Periodic callback from MicroBit. * Process at least one event from the event queue, if it is not empty. * We then continue processing events until something appears on the runqueue. - */ + */ void MicroBitMessageBus::idleTick() { // Clear out any listeners marked for deletion @@ -231,7 +231,7 @@ void MicroBitMessageBus::idleTick() // If we have created some useful work to do, we stop processing. // This helps to minimise the number of blocked fibers we create at any point in time, therefore - // also reducing the RAM footprint. + // also reducing the RAM footprint. if(!scheduler_runqueue_empty()) break; @@ -242,26 +242,26 @@ void MicroBitMessageBus::idleTick() /** * Indicates whether or not we have any background work to do. - * @ return 1 if there are any events waitingto be processed, 0 otherwise. + * @ return 1 if there are any events waitingto be processed, 0 otherwise. */ int MicroBitMessageBus::isIdleCallbackNeeded() { - return !(evt_queue_head == NULL); + return !(evt_queue_head == NULL); } /** * Queues the given event to be sent to all registered recipients. * - * @param The event to send. + * @param The event to send. * * n.b. THIS IS NOW WRAPPED BY THE MicroBitEvent CLASS FOR CONVENIENCE... * * Example: - * @code + * @code * MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN,ticks,CREATE_ONLY); * evt.fire(); * - * //OR YOU CAN DO THIS... + * //OR YOU CAN DO THIS... * MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN); * @endcode */ @@ -299,7 +299,7 @@ int MicroBitMessageBus::process(MicroBitEvent &evt, bool urgent) { l->evt = evt; - // OK, if this handler has regisitered itself as non-blocking, we just execute it directly... + // OK, if this handler has regisitered itself as non-blocking, we just execute it directly... // This is normally only done for trusted system components. // Otherwise, we invoke it in a 'fork on block' context, that will automatically create a fiber // should the event handler attempt a blocking operation, but doesn't have the overhead @@ -323,11 +323,11 @@ int MicroBitMessageBus::process(MicroBitEvent &evt, bool urgent) /** * Register a listener function. - * - * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. + * + * @param id The source of messages to listen for. Events sent from any other IDs will be filtered. * Use MICROBIT_ID_ANY to receive events from all components. * - * @param value The value of messages to listen for. Events with any other values will be filtered. + * @param value The value of messages to listen for. Events with any other values will be filtered. * Use MICROBIT_VALUE_ANY to receive events of any value. * * @param handler The function to call when an event is received. @@ -335,7 +335,7 @@ int MicroBitMessageBus::process(MicroBitEvent &evt, bool urgent) * @return MICROBIT_OK on success MICROBIT_INVALID_PARAMETER * * Example: - * @code + * @code * void onButtonBClick(MicroBitEvent evt) * { * //do something @@ -344,7 +344,7 @@ int MicroBitMessageBus::process(MicroBitEvent &evt, bool urgent) * @endcode */ -int MicroBitMessageBus::listen(int id, int value, void (*handler)(MicroBitEvent), uint16_t flags) +int MicroBitMessageBus::listen(int id, int value, void (*handler)(MicroBitEvent), uint16_t flags) { if (handler == NULL) return MICROBIT_INVALID_PARAMETER; @@ -360,7 +360,7 @@ int MicroBitMessageBus::listen(int id, int value, void (*handler)(MicroBitEvent) } -int MicroBitMessageBus::listen(int id, int value, void (*handler)(MicroBitEvent, void*), void* arg, uint16_t flags) +int MicroBitMessageBus::listen(int id, int value, void (*handler)(MicroBitEvent, void*), void* arg, uint16_t flags) { if (handler == NULL) return MICROBIT_INVALID_PARAMETER; @@ -378,20 +378,20 @@ int MicroBitMessageBus::listen(int id, int value, void (*handler)(MicroBitEvent, /** * Unregister a listener function. * Listners are identified by the Event ID, Event VALUE and handler registered using listen(). - * + * * @param id The Event ID used to register the listener. * @param value The Event VALUE used to register the listener. * @param handler The function used to register the listener. * * * Example: - * @code + * @code * void onButtonBClick() * { * //do something * } * - * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); + * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); * @endcode */ int MicroBitMessageBus::ignore(int id, int value, void (*handler)(MicroBitEvent)) @@ -408,20 +408,20 @@ int MicroBitMessageBus::ignore(int id, int value, void (*handler)(MicroBitEvent) /** * Unregister a listener function. * Listners are identified by the Event ID, Event VALUE and handler registered using listen(). - * + * * @param id The Event ID used to register the listener. * @param value The Event VALUE used to register the listener. * @param handler The function used to register the listener. * * * Example: - * @code + * @code * void onButtonBClick(void *arg) * { * //do something * } * - * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); + * uBit.MessageBus.ignore(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonBClick); * @endcode */ int MicroBitMessageBus::ignore(int id, int value, void (*handler)(MicroBitEvent, void*)) @@ -464,7 +464,7 @@ int MicroBitMessageBus::add(MicroBitListener *newListener) if (l->id == newListener->id && l->value == newListener->value && (methodCallback ? *l->cb_method == *newListener->cb_method : l->cb == newListener->cb)) { - // We have a perfect match for this event listener already registered. + // We have a perfect match for this event listener already registered. // If it's marked for deletion, we simply resurrect the listener, and we're done. // Either way, we return an error code, as the *new* listener should be released... if(l->flags & MESSAGE_BUS_LISTENER_DELETING) @@ -484,7 +484,7 @@ int MicroBitMessageBus::add(MicroBitListener *newListener) return MICROBIT_OK; } - // We maintain an ordered list of listeners. + // We maintain an ordered list of listeners. // The chain is held stictly in increasing order of ID (first level), then value code (second level). // Find the correct point in the chain for this event. // Adding a listener is a rare occurance, so we just walk the list... @@ -544,7 +544,7 @@ int MicroBitMessageBus::remove(MicroBitListener *listener) { if ((listener->flags & MESSAGE_BUS_LISTENER_METHOD) == (l->flags & MESSAGE_BUS_LISTENER_METHOD)) { - if(((listener->flags & MESSAGE_BUS_LISTENER_METHOD) && (*l->cb_method == *listener->cb_method)) || + if(((listener->flags & MESSAGE_BUS_LISTENER_METHOD) && (*l->cb_method == *listener->cb_method)) || ((!(listener->flags & MESSAGE_BUS_LISTENER_METHOD) && l->cb == listener->cb))) { if ((listener->id == MICROBIT_ID_ANY || listener->id == l->id) && (listener->value == MICROBIT_EVT_ANY || listener->value == l->value)) @@ -585,4 +585,3 @@ MicroBitListener* MicroBitMessageBus::elementAt(int n) return l; } -