Implement tagged integers in C++ (#813)
* Start on the C++ conversion for tagged ints
This commit is contained in:
committed by
Sam El-Husseini
parent
01c7c0b588
commit
cad13785e2
@ -1,156 +1,178 @@
|
||||
#include "pxt.h"
|
||||
|
||||
/**
|
||||
* Creation, manipulation and display of LED images.
|
||||
*/
|
||||
//% color=#5C2D91 weight=31 icon="\uf03e"
|
||||
//% 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) {
|
||||
return MicroBitImage(imageBytes(leds)).clone().leakData();
|
||||
}
|
||||
PXT_VTABLE_BEGIN(RefMImage, 0, 0)
|
||||
PXT_VTABLE_END
|
||||
|
||||
/**
|
||||
* 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);
|
||||
RefMImage::RefMImage(ImageData *d) : PXT_VTABLE_INIT(RefMImage), img(d) {
|
||||
img->incr();
|
||||
}
|
||||
|
||||
void RefMImage::destroy(RefMImage *t) {
|
||||
t->img->decr();
|
||||
}
|
||||
|
||||
void RefMImage::print(RefMImage *t) {
|
||||
DMESG("RefMImage %p r=%d size=%d x %d", t, t->refcnt, img->width, img->height);
|
||||
}
|
||||
|
||||
void RefMImage::makeWritable() {
|
||||
if (img->isReadOnly()) {
|
||||
MicroBitImage i(img);
|
||||
img = i.clone().leakData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creation, manipulation and display of LED images.
|
||||
*/
|
||||
//% color=#5C2D91 weight=31 icon="\uf03e"
|
||||
//% 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) {
|
||||
return new RefMImage(imageBytes(leds));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
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), -xOffset, 0, 0, 0);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
//% blockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8
|
||||
//% parts="ledmatrix" async
|
||||
void showImage(Image sprite, int xOffset, int interval = 400) {
|
||||
uBit.display.print(MicroBitImage(sprite), -xOffset, 0, 0, interval);
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
//% blockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8
|
||||
//% parts="ledmatrix" async
|
||||
void showImage(Image sprite, int xOffset, int interval = 400) {
|
||||
uBit.display.print(MicroBitImage(sprite->img), -xOffset, 0, 0, interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 block="scroll image %sprite|with offset %frameoffset|and interval (ms) %delay" blockGap=8
|
||||
//% parts="ledmatrix"
|
||||
void scrollImage(Image id, int frameOffset, int interval) {
|
||||
MicroBitImage i(id);
|
||||
uBit.display.animate(i, interval, frameOffset, MICROBIT_DISPLAY_ANIMATE_DEFAULT_POS, 0);
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
//% block="scroll image %sprite|with offset %frameoffset|and interval (ms) %delay"
|
||||
//% 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all pixels off.
|
||||
*/
|
||||
//% help=images/clear
|
||||
//% parts="ledmatrix"
|
||||
void clear(Image i) {
|
||||
i->makeWritable();
|
||||
MicroBitImage(i->img).clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all pixels off.
|
||||
*/
|
||||
//% help=images/clear
|
||||
//% parts="ledmatrix"
|
||||
void clear(Image i) {
|
||||
MicroBitImage(i).clear();
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a specific pixel brightness at a given position
|
||||
*/
|
||||
//%
|
||||
//% parts="ledmatrix"
|
||||
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
|
||||
*/
|
||||
//%
|
||||
//% 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the width in columns
|
||||
*/
|
||||
//% help=functions/width
|
||||
int width(Image i) {
|
||||
return i->img->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the pixel brightness ([0..255]) at a given position
|
||||
*/
|
||||
//%
|
||||
//% parts="ledmatrix"
|
||||
int pixelBrightness(Image i, int x, int y) {
|
||||
int pix = MicroBitImage(i).getPixelValue(x, y);
|
||||
if (pix < 0) return 0;
|
||||
return pix;
|
||||
}
|
||||
/**
|
||||
* Gets the height in rows (always 5)
|
||||
*/
|
||||
//%
|
||||
int height(Image i) {
|
||||
return i->img->height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a pixel state at position ``(x,y)``
|
||||
* @param x TODO
|
||||
* @param y TODO
|
||||
* @param value TODO
|
||||
*/
|
||||
//% help=images/set-pixel
|
||||
//% parts="ledmatrix"
|
||||
void setPixel(Image i, int x, int y, bool value) {
|
||||
setPixelBrightness(i, x, y, value ? 255 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the width in columns
|
||||
*/
|
||||
//% help=functions/width
|
||||
int width(Image i) {
|
||||
return i->width;
|
||||
}
|
||||
/**
|
||||
* Get the pixel state at position ``(x,y)``
|
||||
* @param x TODO
|
||||
* @param y TODO
|
||||
*/
|
||||
//% help=images/pixel
|
||||
//% parts="ledmatrix"
|
||||
bool pixel(Image i, int x, int y) {
|
||||
return pixelBrightness(i, x, y) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the height in rows (always 5)
|
||||
*/
|
||||
//%
|
||||
int height(Image i) {
|
||||
return i->height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a pixel state at position ``(x,y)``
|
||||
* @param x TODO
|
||||
* @param y TODO
|
||||
* @param value TODO
|
||||
*/
|
||||
//% help=images/set-pixel
|
||||
//% parts="ledmatrix"
|
||||
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
|
||||
*/
|
||||
//% help=images/pixel
|
||||
//% parts="ledmatrix"
|
||||
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
|
||||
*/
|
||||
//% weight=70 help=images/show-frame
|
||||
//% parts="ledmatrix"
|
||||
void showFrame(Image i, int frame, int interval = 400) {
|
||||
showImage(i, frame * 5, interval);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Shows a particular frame of the image strip.
|
||||
* @param frame TODO
|
||||
*/
|
||||
//% weight=70 help=images/show-frame
|
||||
//% parts="ledmatrix"
|
||||
void showFrame(Image i, int frame, int interval = 400) {
|
||||
showImage(i, frame * 5, interval);
|
||||
}
|
||||
} // namespace ImageMethods
|
Reference in New Issue
Block a user