pxt-calliope/libs/core/images.cpp

160 lines
4.1 KiB
C++
Raw Normal View History

2016-04-02 06:26:06 +02:00
#include "ksbit.h"
2016-04-02 04:55:51 +02:00
2016-04-06 00:59:25 +02:00
/**
* Creation, manipulation and display of LED images.
*/
2016-05-19 20:59:57 +02:00
//% color=#5C2D91 weight=31
//% advanced=true
2016-04-02 04:55:51 +02:00
namespace images {
/**
* Creates an image that fits on the LED screen.
*/
//% weight=75 help=images/create-image
//% blockId=device_build_image block="create image"
//% parts="ledmatrix"
2016-04-02 04:55:51 +02:00
Image createImage(ImageLiteral leds) {
2016-04-02 22:44:29 +02:00
return MicroBitImage(imageBytes(leds)).clone().leakData();
2016-04-02 04:55:51 +02:00
}
/**
* Creates an image with 2 frames.
*/
//% weight=74 help=images/create-big-image
//% blockId=device_build_big_image block="create big image" imageLiteral=2
//% parts="ledmatrix"
2016-04-02 04:55:51 +02:00
Image createBigImage(ImageLiteral leds) {
return createImage(leds);
}
}
namespace ImageMethods {
2016-04-13 02:10:37 +02:00
/**
* Plots the image at a given column to the screen
*/
2016-04-13 02:10:37 +02:00
//% help=images/plot-image
//% parts="ledmatrix"
2016-04-13 02:10:37 +02:00
void plotImage(Image i, int xOffset = 0) {
uBit.display.print(MicroBitImage(i), -xOffset, 0, 0, 0);
}
2016-04-02 04:55:51 +02:00
/**
* Shows an frame from the image at offset ``x offset``.
2016-05-27 05:42:15 +02:00
* @param xOffset column index to start displaying the image
2016-04-02 04:55:51 +02:00
*/
2016-05-27 05:42:15 +02:00
//% help=images/show-image weight=80 blockNamespace=images
//% blockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8
//% parts="ledmatrix"
2016-05-27 05:42:15 +02:00
void showImage(Image sprite, int xOffset) {
uBit.display.print(MicroBitImage(sprite), -xOffset, 0, 0);
2016-04-02 04:55:51 +02:00
}
2016-04-13 02:10:37 +02:00
/**
* Draws the ``index``-th frame of the image on the screen.
2016-05-27 05:42:15 +02:00
* @param xOffset column index to start displaying the image
2016-04-13 02:10:37 +02:00
*/
//% help=images/plot-frame weight=80
//% parts="ledmatrix"
2016-04-13 02:10:37 +02:00
void plotFrame(Image i, int xOffset) {
// TODO showImage() used in original implementation
plotImage(i, xOffset * 5);
}
2016-04-02 04:55:51 +02:00
/**
* Scrolls an image .
* @param frameOffset x offset moved on each animation step, eg: 5, 1, -1
* @param interval time between each animation step in milli seconds, eg: 200
*/
2016-05-27 05:42:15 +02:00
//% help=images/show-image weight=79 async blockNamespace=images
//% blockId=device_scroll_image block="scroll image %sprite|with offset %frameoffset|and interval (ms) %delay" blockGap=8
//% parts="ledmatrix"
2016-05-27 05:42:15 +02:00
void scrollImage(Image id, int frameOffset, int interval) {
2016-04-02 04:55:51 +02:00
MicroBitImage i(id);
if (i.getWidth() <= 5)
showImage(id, 0);
else
uBit.display.animate(i, interval, frameOffset, 0);
}
/**
* Sets all pixels off.
*/
//% help=images/clear
//% parts="ledmatrix"
2016-04-02 04:55:51 +02:00
void clear(Image i) {
MicroBitImage(i).clear();
}
/**
* Sets a specific pixel brightness at a given position
*/
2016-05-27 05:42:15 +02:00
//%
//% parts="ledmatrix"
2016-04-02 04:55:51 +02:00
void setPixelBrightness(Image i, int x, int y, int value) {
MicroBitImage(i).setPixelValue(x, y, value);
}
/**
* Gets the pixel brightness ([0..255]) at a given position
*/
2016-05-27 05:42:15 +02:00
//%
//% parts="ledmatrix"
2016-04-02 04:55:51 +02:00
int pixelBrightness(Image i, int x, int y) {
int pix = MicroBitImage(i).getPixelValue(x, y);
if (pix < 0) return 0;
return pix;
}
2016-04-13 02:10:37 +02:00
/**
* Gets the width in columns
*/
//% help=functions/width
int width(Image i) {
return i->width;
}
/**
* Gets the height in rows (always 5)
*/
2016-05-27 05:42:15 +02:00
//%
2016-04-13 02:10:37 +02:00
int height(Image i) {
return i->height;
}
/**
* Set a pixel state at position ``(x,y)``
* @param x TODO
* @param y TODO
* @param value TODO
*/
2016-05-27 05:42:15 +02:00
//% help=images/set-pixel
//% parts="ledmatrix"
2016-04-13 02:10:37 +02:00
void setPixel(Image i, int x, int y, bool value) {
setPixelBrightness(i, x, y, value ? 255 : 0);
}
/**
* Get the pixel state at position ``(x,y)``
* @param x TODO
* @param y TODO
*/
2016-05-27 05:42:15 +02:00
//% help=images/pixel
//% parts="ledmatrix"
2016-04-13 02:10:37 +02:00
bool pixel(Image i, int x, int y) {
return pixelBrightness(i, x, y) > 0;
}
/**
* Shows a particular frame of the image strip.
* @param frame TODO
*/
2016-05-27 05:42:15 +02:00
//% weight=70 help=images/show-frame
//% parts="ledmatrix"
2016-04-13 02:10:37 +02:00
void showFrame(Image i, int frame) {
showImage(i, frame * 5);
}
2016-04-02 04:55:51 +02:00
}