2016-11-30 06:35:26 +01:00
|
|
|
#include "pxt.h"
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2019-07-30 18:45:32 +02:00
|
|
|
PXT_VTABLE(RefMImage, ValType::Object)
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
RefMImage::RefMImage(ImageData *d) : PXT_VTABLE_INIT(RefMImage), img(d) {
|
|
|
|
img->incr();
|
2016-04-02 04:55:51 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefMImage::destroy(RefMImage *t) {
|
|
|
|
t->img->decr();
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefMImage::print(RefMImage *t) {
|
|
|
|
DMESG("RefMImage %p r=%d size=%d x %d", t, t->refcnt, img->width, img->height);
|
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefMImage::makeWritable() {
|
|
|
|
if (img->isReadOnly()) {
|
|
|
|
MicroBitImage i(img);
|
|
|
|
img = i.clone().leakData();
|
2016-04-13 02:10:37 +02:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2019-02-22 00:41:32 +01:00
|
|
|
void RefMImage::scan(RefMImage *t) {}
|
|
|
|
|
|
|
|
unsigned RefMImage::gcsize(RefMImage *t) {
|
|
|
|
return (sizeof(*t) + 3) >> 2;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Creation, manipulation and display of LED images.
|
|
|
|
*/
|
2018-10-01 23:39:51 +02:00
|
|
|
//% color=#7600A8 weight=31 icon="\uf03e"
|
2018-05-30 00:55:58 +02:00
|
|
|
//% advanced=true
|
|
|
|
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"
|
|
|
|
Image createImage(ImageLiteral_ leds) {
|
2019-03-27 23:10:10 +01:00
|
|
|
return NEW_GC(RefMImage, imageBytes(leds));
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2018-05-30 00:55:58 +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"
|
|
|
|
Image createBigImage(ImageLiteral_ leds) {
|
|
|
|
return createImage(leds);
|
|
|
|
}
|
|
|
|
} // namespace images
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
namespace ImageMethods {
|
|
|
|
/**
|
|
|
|
* Plots the image at a given column to the screen
|
|
|
|
*/
|
|
|
|
//% help=images/plot-image
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
void plotImage(Image i, int xOffset = 0) {
|
|
|
|
uBit.display.print(MicroBitImage(i->img), -xOffset, 0, 0, 0);
|
|
|
|
}
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Shows an frame from the image at offset ``x offset``.
|
|
|
|
* @param xOffset column index to start displaying the image
|
|
|
|
*/
|
|
|
|
//% help=images/show-image weight=80 blockNamespace=images
|
2019-02-22 00:41:32 +01:00
|
|
|
//% blockId=device_show_image_offset block="show image %sprite(myImage)|at offset %offset"
|
|
|
|
//% blockGap=8 parts="ledmatrix" async
|
2018-05-30 00:55:58 +02:00
|
|
|
void showImage(Image sprite, int xOffset, int interval = 400) {
|
|
|
|
uBit.display.print(MicroBitImage(sprite->img), -xOffset, 0, 0, interval);
|
|
|
|
}
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Draws the ``index``-th frame of the image on the screen.
|
|
|
|
* @param xOffset column index to start displaying the image
|
|
|
|
*/
|
|
|
|
//% help=images/plot-frame weight=80
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
void plotFrame(Image i, int xOffset) {
|
|
|
|
// TODO showImage() used in original implementation
|
|
|
|
plotImage(i, xOffset * 5);
|
|
|
|
}
|
2016-04-02 04:55:51 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Scrolls an image .
|
|
|
|
* @param frameOffset x offset moved on each animation step, eg: 1, 2, 5
|
|
|
|
* @param interval time between each animation step in milli seconds, eg: 200
|
|
|
|
*/
|
|
|
|
//% help=images/scroll-image weight=79 async blockNamespace=images
|
|
|
|
//% blockId=device_scroll_image
|
2018-08-14 19:18:23 +02:00
|
|
|
//% block="scroll image %sprite(myImage)|with offset %frameoffset|and interval (ms) %delay"
|
2018-05-30 00:55:58 +02:00
|
|
|
//% blockGap=8 parts="ledmatrix"
|
|
|
|
void scrollImage(Image id, int frameOffset, int interval) {
|
|
|
|
MicroBitImage i(id->img);
|
|
|
|
uBit.display.animate(i, interval, frameOffset, MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS, 0);
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Sets all pixels off.
|
|
|
|
*/
|
|
|
|
//% help=images/clear
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
void clear(Image i) {
|
|
|
|
i->makeWritable();
|
|
|
|
MicroBitImage(i->img).clear();
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Sets a specific pixel brightness at a given position
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
void setPixelBrightness(Image i, int x, int y, int value) {
|
|
|
|
i->makeWritable();
|
|
|
|
MicroBitImage(i->img).setPixelValue(x, y, value);
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the pixel brightness ([0..255]) at a given position
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
int pixelBrightness(Image i, int x, int y) {
|
|
|
|
int pix = MicroBitImage(i->img).getPixelValue(x, y);
|
|
|
|
if (pix < 0)
|
|
|
|
return 0;
|
|
|
|
return pix;
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the width in columns
|
|
|
|
*/
|
|
|
|
//% help=functions/width
|
|
|
|
int width(Image i) {
|
|
|
|
return i->img->width;
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the height in rows (always 5)
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
int height(Image i) {
|
|
|
|
return i->img->height;
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Set a pixel state at position ``(x,y)``
|
2019-04-05 05:32:48 +02:00
|
|
|
* @param x pixel column
|
|
|
|
* @param y pixel row
|
|
|
|
* @param value pixel state
|
2018-05-30 00:55:58 +02:00
|
|
|
*/
|
|
|
|
//% help=images/set-pixel
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
void setPixel(Image i, int x, int y, bool value) {
|
|
|
|
setPixelBrightness(i, x, y, value ? 255 : 0);
|
|
|
|
}
|
2016-04-13 02:10:37 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
/**
|
|
|
|
* Get the pixel state at position ``(x,y)``
|
2019-04-05 05:32:48 +02:00
|
|
|
* @param x pixel column
|
|
|
|
* @param y pixel row
|
2018-05-30 00:55:58 +02:00
|
|
|
*/
|
|
|
|
//% help=images/pixel
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
bool pixel(Image i, int x, int y) {
|
|
|
|
return pixelBrightness(i, x, y) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 05:32:48 +02:00
|
|
|
* Show a particular frame of the image strip.
|
|
|
|
* @param frame image frame to show
|
2018-05-30 00:55:58 +02:00
|
|
|
*/
|
|
|
|
//% weight=70 help=images/show-frame
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
void showFrame(Image i, int frame, int interval = 400) {
|
|
|
|
showImage(i, frame * 5, interval);
|
|
|
|
}
|
|
|
|
} // namespace ImageMethods
|