131 Week 8 - micro:bit display and images Flashcards
MicroBit class
Consists of variables and methods that operate as drivers to control commonly used features of the micro:bit.
Set up micro:bit in C++
include “MicroBit.h” Includes library of key microbit functions
MicroBit uBit; - create object uBit to control micro:bit
uBit.innit(); - called in main(), initialises micro:bit
Display text on microbit
uBit.display.scroll(); or uBit.display.print();
arguments:
text to scroll - can be text or num
delay - lower value = shorter delay (optional argument)
Difference between scroll and print
display.scroll scrolls text accross the screen pixel by pixel
display.print displays each character in turn
Changing display modes
uBit.display.setDisplayMode(displayMode);
DISPLAY_MODE_BLACK_AND_WHITE
Pixels are either on or off
Brightness of all pixels is changed by uBit.display.setBrightness(int between 1-255);
DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE
Each pixel can be on or off, and the display driver will also sense the ambient brightness from the LEDs.
DISPLAY_MODE_GREYSCALE
Each pixel can independently have 256 levels of brightness (0-255)
setting and getting pixel values
uBit.display.Image.setPixelValue(int 0-4, int 0-4, int 0-255)
Arguments: x coord, y coord and brightness of a pixel. black and white display mode will ignore brightness argument.
int i = uBit.display.iamge.getPixelValue(int 0-4, int 0-4);
Arguments: x coord and y coord of pixel
Creating images
MicroBitImage imageName(“0,255,0,255,0\n 0,255,0,255,0\n 0,0,0,0,0\n 255,0,0,0,255\n 0,255,255,255,0\n”);
MicroBitImage represents a bitmap picture
CSV of 0-255 for pixel brightness
\n moves to new line of pixels
displaying images
uBit.display.print(imageName);
pasting an image
Create a blank image (5x5 in this example):
MicroBitImage myimage(5,5)
paste an image at specified coords:
myImage.paste(imageName, xcoord, ycoord)
creating read only images
constant array of unsigned integers can be used to store a read-only picture:
const uint8_t imageName[] = {0,255,0,255,0,0,255,0,255,0,0,0,0,0,0,255,0,0,0,255,0,255,255,255,0};
MicroBitImage class offers a constructor that creates a bitmap representation of a given size (e.g., 5×5 in this example), based on a given buffer (i.e., the array imageName in this example):
MicroBitImage myImage(5,5,imageName);
displaying read only images
uBit.display.print(myImage);