mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

MicroBitDisplay Class Reference

MicroBitDisplay Class Reference

Class definition for MicroBitDisplay. More...

#include <MicroBitDisplay.h>

Inherits MicroBitComponent.

Public Member Functions

 MicroBitDisplay (uint16_t id=MICROBIT_ID_DISPLAY, const MatrixMap &map=microbitMatrixMap)
 Constructor.
void stopAnimation ()
 Stops any currently running animation, and any that are waiting to be displayed.
virtual void systemTick ()
 Frame update method, invoked periodically to strobe the display.
int printCharAsync (char c, int delay=0)
 Prints the given character to the display, if it is not in use.
int printAsync (ManagedString s, int delay=MICROBIT_DEFAULT_PRINT_SPEED)
 Prints the given ManagedString to the display, one character at a time.
int printAsync (MicroBitImage i, int x=0, int y=0, int alpha=0, int delay=0)
 Prints the given image to the display, if the display is not in use.
int printChar (char c, int delay=0)
 Prints the given character to the display.
int print (ManagedString s, int delay=MICROBIT_DEFAULT_PRINT_SPEED)
 Prints the given string to the display, one character at a time.
int print (MicroBitImage i, int x=0, int y=0, int alpha=0, int delay=0)
 Prints the given image to the display.
int scrollAsync (ManagedString s, int delay=MICROBIT_DEFAULT_SCROLL_SPEED)
 Scrolls the given string to the display, from right to left.
int scrollAsync (MicroBitImage image, int delay=MICROBIT_DEFAULT_SCROLL_SPEED, int stride=MICROBIT_DEFAULT_SCROLL_STRIDE)
 Scrolls the given image across the display, from right to left.
int scroll (ManagedString s, int delay=MICROBIT_DEFAULT_SCROLL_SPEED)
 Scrolls the given string across the display, from right to left.
int scroll (MicroBitImage image, int delay=MICROBIT_DEFAULT_SCROLL_SPEED, int stride=MICROBIT_DEFAULT_SCROLL_STRIDE)
 Scrolls the given image across the display, from right to left.
int animateAsync (MicroBitImage image, int delay, int stride, int startingPosition=MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS, int autoClear=MICROBIT_DISPLAY_DEFAULT_AUTOCLEAR)
 "Animates" the current image across the display with a given stride, finishing on the last frame of the animation.
int animate (MicroBitImage image, int delay, int stride, int startingPosition=MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS, int autoClear=MICROBIT_DISPLAY_DEFAULT_AUTOCLEAR)
 "Animates" the current image across the display with a given stride, finishing on the last frame of the animation.
int setBrightness (int b)
 Configures the brightness of the display.
void setDisplayMode (DisplayMode mode)
 Configures the mode of the display.
int getDisplayMode ()
 Retrieves the mode of the display.
int getBrightness ()
 Fetches the current brightness of this display.
void rotateTo (DisplayRotation position)
 Rotates the display to the given position.
void enable ()
 Enables the display, should only be called if the display is disabled.
void disable ()
 Disables the display, which releases control of the GPIO pins used by the display, which are exposed on the edge connector.
void clear ()
 Clears the display of any remaining pixels.
void setFont (MicroBitFont font)
 Updates the font that will be used for display operations.
MicroBitFont getFont ()
 Retrieves the font object used for rendering characters on the display.
MicroBitImage screenShot ()
 Captures the bitmap currently being rendered on the display.
int readLightLevel ()
 Gives a representative figure of the light level in the current environment where are micro:bit is situated.
 ~MicroBitDisplay ()
 Destructor for MicroBitDisplay, where we deregister this instance from the array of system components.
virtual void idleTick ()
 The idle thread will call this member function once the component has been added to the array of idle components using fiber_add_idle_component.

Detailed Description

Class definition for MicroBitDisplay.

A MicroBitDisplay represents the LED matrix array on the micro:bit.

Definition at line 81 of file MicroBitDisplay.h.


Constructor & Destructor Documentation

MicroBitDisplay ( uint16_t  id = MICROBIT_ID_DISPLAY,
const MatrixMap map = microbitMatrixMap 
)

Constructor.

Create a software representation the micro:bit's 5x5 LED matrix. The display is initially blank.

Parameters:
idThe id the display should use when sending events on the MessageBus. Defaults to MICROBIT_ID_DISPLAY.
mapThe mapping information that relates pin inputs/outputs to physical screen coordinates. Defaults to microbitMatrixMap, defined in MicroBitMatrixMaps.h.
 MicroBitDisplay display;

Definition at line 55 of file MicroBitDisplay.cpp.

Destructor for MicroBitDisplay, where we deregister this instance from the array of system components.

Definition at line 1222 of file MicroBitDisplay.cpp.


Member Function Documentation

int animate ( MicroBitImage  image,
int  delay,
int  stride,
int  startingPosition = MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS,
int  autoClear = MICROBIT_DISPLAY_DEFAULT_AUTOCLEAR 
)

"Animates" the current image across the display with a given stride, finishing on the last frame of the animation.

Blocks the calling thread until the animation is complete.

Parameters:
delayThe time to delay between each update of the display, in milliseconds.
strideThe number of pixels to shift by in each update.
startingPositionthe starting position on the display for the animation to begin at. Defaults to MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS.
autoCleardefines whether or not the display is automatically cleared once the animation is complete. By default, the display is cleared. Set this parameter to zero to disable the autoClear operation.
Returns:
MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
 const int heart_w = 10;
 const int heart_h = 5;
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, };

 MicroBitImage i(heart_w,heart_h,heart);
 display.animate(i,100,5);

Definition at line 965 of file MicroBitDisplay.cpp.

int animateAsync ( MicroBitImage  image,
int  delay,
int  stride,
int  startingPosition = MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS,
int  autoClear = MICROBIT_DISPLAY_DEFAULT_AUTOCLEAR 
)

"Animates" the current image across the display with a given stride, finishing on the last frame of the animation.

Returns immediately.

Parameters:
imageThe image to display.
delayThe time to delay between each update of the display, in milliseconds.
strideThe number of pixels to shift by in each update.
startingPositionthe starting position on the display for the animation to begin at. Defaults to MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS.
autoCleardefines whether or not the display is automatically cleared once the animation is complete. By default, the display is cleared. Set this parameter to zero to disable the autoClear operation.
Returns:
MICROBIT_OK, MICROBIT_BUSY if the screen is in use, or MICROBIT_INVALID_PARAMETER.
 const int heart_w = 10;
 const int heart_h = 5;
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, };

 MicroBitImage i(heart_w,heart_h,heart);
 display.animateAsync(i,100,5);

Definition at line 910 of file MicroBitDisplay.cpp.

void clear (  )

Clears the display of any remaining pixels.

`display.image.clear()` can also be used!

 display.clear(); //clears the display

Definition at line 1155 of file MicroBitDisplay.cpp.

void disable (  )

Disables the display, which releases control of the GPIO pins used by the display, which are exposed on the edge connector.

 display.disable(); //disables the display
Note:
Only disables the display if the display is currently enabled.

Definition at line 1141 of file MicroBitDisplay.cpp.

void enable (  )

Enables the display, should only be called if the display is disabled.

 display.enable(); //Enables the display mechanics
Note:
Only enables the display if the display is currently disabled.

Definition at line 1126 of file MicroBitDisplay.cpp.

int getBrightness (  )

Fetches the current brightness of this display.

Returns:
the brightness of this display, in the range 0..255.
 display.getBrightness(); //the current brightness

Definition at line 1065 of file MicroBitDisplay.cpp.

int getDisplayMode (  )

Retrieves the mode of the display.

Returns:
the current mode of the display

Definition at line 1051 of file MicroBitDisplay.cpp.

MicroBitFont getFont (  )

Retrieves the font object used for rendering characters on the display.

Note:
DEPRECATED! Please use MicroBitFont::getSystemFont() instead.

Definition at line 1177 of file MicroBitDisplay.cpp.

virtual void idleTick (  ) [virtual, inherited]

The idle thread will call this member function once the component has been added to the array of idle components using fiber_add_idle_component.

Reimplemented in MicroBitBLEManager, MicroBitEventService, MicroBitIOPinService, MicroBitAccelerometer, MicroBitCompass, and MicroBitThermometer.

Definition at line 133 of file MicroBitComponent.h.

int print ( MicroBitImage  i,
int  x = 0,
int  y = 0,
int  alpha = 0,
int  delay = 0 
)

Prints the given image to the display.

Blocks the calling thread until all the image has been displayed.

Parameters:
iThe image to display.
xThe horizontal position on the screen to display the image. Defaults to 0.
yThe vertical position on the screen to display the image. Defaults to 0.
alphaTreats the brightness level '0' as transparent. Defaults to 0.
delayThe time to display the image for, or zero to show the image forever. Defaults to 0.
Returns:
MICROBIT_OK, MICROBIT_BUSY if the display is already in use, or MICROBIT_INVALID_PARAMETER.
 MicrobitImage i("1,1,1,1,1\n1,1,1,1,1\n");
 display.print(i,400);

Definition at line 688 of file MicroBitDisplay.cpp.

int print ( ManagedString  s,
int  delay = MICROBIT_DEFAULT_PRINT_SPEED 
)

Prints the given string to the display, one character at a time.

Blocks the calling thread until all the text has been displayed.

Parameters:
sThe string to display.
delayThe time to delay between characters, in milliseconds. Defaults to: MICROBIT_DEFAULT_PRINT_SPEED.
Returns:
MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
 display.print("abc123",400);

Definition at line 636 of file MicroBitDisplay.cpp.

int printAsync ( MicroBitImage  i,
int  x = 0,
int  y = 0,
int  alpha = 0,
int  delay = 0 
)

Prints the given image to the display, if the display is not in use.

Returns immediately, and executes the animation asynchronously.

Parameters:
iThe image to display.
xThe horizontal position on the screen to display the image. Defaults to 0.
yThe vertical position on the screen to display the image. Defaults to 0.
alphaTreats the brightness level '0' as transparent. Defaults to 0.
delayThe time to delay between characters, in milliseconds. Defaults to 0.
 MicrobitImage i("1,1,1,1,1\n1,1,1,1,1\n");
 display.print(i,400);

Definition at line 556 of file MicroBitDisplay.cpp.

int printAsync ( ManagedString  s,
int  delay = MICROBIT_DEFAULT_PRINT_SPEED 
)

Prints the given ManagedString to the display, one character at a time.

Returns immediately, and executes the animation asynchronously.

Parameters:
sThe string to display.
delayThe time to delay between characters, in milliseconds. Must be > 0. Defaults to: MICROBIT_DEFAULT_PRINT_SPEED.
Returns:
MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
 display.printAsync("abc123",400);

Definition at line 511 of file MicroBitDisplay.cpp.

int printChar ( char  c,
int  delay = 0 
)

Prints the given character to the display.

Parameters:
cThe character to display.
delayOptional parameter - the time for which to show the character. Zero displays the character forever, or until the Displays next use.
Returns:
MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
 display.printAsync('p');
 display.printAsync('p',100);

Definition at line 595 of file MicroBitDisplay.cpp.

int printCharAsync ( char  c,
int  delay = 0 
)

Prints the given character to the display, if it is not in use.

Parameters:
cThe character to display.
delayOptional parameter - the time for which to show the character. Zero displays the character forever, or until the Displays next use.
Returns:
MICROBIT_OK, MICROBIT_BUSY is the screen is in use, or MICROBIT_INVALID_PARAMETER.
 display.printAsync('p');
 display.printAsync('p',100);

Definition at line 470 of file MicroBitDisplay.cpp.

int readLightLevel (  )

Gives a representative figure of the light level in the current environment where are micro:bit is situated.

Internally, it 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.

Returns:
an indicative light level in the range 0 - 255.
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.

Definition at line 1208 of file MicroBitDisplay.cpp.

void rotateTo ( DisplayRotation  rotation )

Rotates the display to the given position.

Axis aligned values only.

 display.rotateTo(MICROBIT_DISPLAY_ROTATION_180); //rotates 180 degrees from original orientation

Definition at line 1079 of file MicroBitDisplay.cpp.

MicroBitImage screenShot (  )

Captures the bitmap currently being rendered on the display.

Returns:
a MicroBitImage containing the captured data.

Definition at line 1187 of file MicroBitDisplay.cpp.

int scroll ( ManagedString  s,
int  delay = MICROBIT_DEFAULT_SCROLL_SPEED 
)

Scrolls the given string across the display, from right to left.

Blocks the calling thread until all text has been displayed.

Parameters:
sThe string to display.
delayThe time to delay between characters, in milliseconds. Defaults to: MICROBIT_DEFAULT_SCROLL_SPEED.
Returns:
MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
 display.scroll("abc123",100);

Definition at line 812 of file MicroBitDisplay.cpp.

int scroll ( MicroBitImage  image,
int  delay = MICROBIT_DEFAULT_SCROLL_SPEED,
int  stride = MICROBIT_DEFAULT_SCROLL_STRIDE 
)

Scrolls the given image across the display, from right to left.

Blocks the calling thread until all the text has been displayed.

Parameters:
imageThe image to display.
delayThe time between updates, in milliseconds. Defaults to: MICROBIT_DEFAULT_SCROLL_SPEED.
strideThe number of pixels to shift by in each update. Defaults to MICROBIT_DEFAULT_SCROLL_STRIDE.
Returns:
MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
 MicrobitImage i("1,1,1,1,1\n1,1,1,1,1\n");
 display.scroll(i,100,1);

Definition at line 857 of file MicroBitDisplay.cpp.

int scrollAsync ( ManagedString  s,
int  delay = MICROBIT_DEFAULT_SCROLL_SPEED 
)

Scrolls the given string to the display, from right to left.

Returns immediately, and executes the animation asynchronously.

Parameters:
sThe string to display.
delayThe time to delay between characters, in milliseconds. Defaults to: MICROBIT_DEFAULT_SCROLL_SPEED.
Returns:
MICROBIT_OK, MICROBIT_BUSY if the display is already in use, or MICROBIT_INVALID_PARAMETER.
 display.scrollAsync("abc123",100);

Definition at line 728 of file MicroBitDisplay.cpp.

int scrollAsync ( MicroBitImage  image,
int  delay = MICROBIT_DEFAULT_SCROLL_SPEED,
int  stride = MICROBIT_DEFAULT_SCROLL_STRIDE 
)

Scrolls the given image across the display, from right to left.

Returns immediately, and executes the animation asynchronously.

Parameters:
imageThe image to display.
delayThe time between updates, in milliseconds. Defaults to: MICROBIT_DEFAULT_SCROLL_SPEED.
strideThe number of pixels to shift by in each update. Defaults to MICROBIT_DEFAULT_SCROLL_STRIDE.
Returns:
MICROBIT_OK, MICROBIT_BUSY if the display is already in use, or MICROBIT_INVALID_PARAMETER.
 MicrobitImage i("1,1,1,1,1\n1,1,1,1,1\n");
 display.scrollAsync(i,100,1);

Definition at line 771 of file MicroBitDisplay.cpp.

int setBrightness ( int  b )

Configures the brightness of the display.

Parameters:
bThe brightness to set the brightness to, in the range 0 - 255.
Returns:
MICROBIT_OK, or MICROBIT_INVALID_PARAMETER
 display.setBrightness(255); //max brightness

Definition at line 1006 of file MicroBitDisplay.cpp.

void setDisplayMode ( DisplayMode  mode )

Configures the mode of the display.

Parameters:
modeThe mode to swap the display into. One of: DISPLAY_MODE_GREYSCALE, DISPLAY_MODE_BLACK_AND_WHITE, DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE
 display.setDisplayMode(DISPLAY_MODE_GREYSCALE); //per pixel brightness

Definition at line 1027 of file MicroBitDisplay.cpp.

void setFont ( MicroBitFont  font )

Updates the font that will be used for display operations.

Parameters:
fontthe new font that will be used to render characters.
Note:
DEPRECATED! Please use MicroBitFont::setSystemFont() instead.

Definition at line 1167 of file MicroBitDisplay.cpp.

void stopAnimation (  )

Stops any currently running animation, and any that are waiting to be displayed.

Resets the current given animation.

Definition at line 415 of file MicroBitDisplay.cpp.

void systemTick (  ) [virtual]

Frame update method, invoked periodically to strobe the display.

Internal frame update method, used to strobe the display.

TODO: Write a more efficient, complementary variation of this method for the case where MICROBIT_DISPLAY_ROW_COUNT > MICROBIT_DISPLAY_COLUMN_COUNT.

Reimplemented from MicroBitComponent.

Definition at line 97 of file MicroBitDisplay.cpp.