Implement Images

This commit is contained in:
Michal Moskal 2016-04-01 19:55:51 -07:00
parent 6176963504
commit 4b92de7516
8 changed files with 163 additions and 76 deletions

View File

@ -150,7 +150,8 @@ namespace control {
*/ */
//% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source|with value %value=control_event_value" blockExternalInputs=1 //% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source|with value %value=control_event_value" blockExternalInputs=1
//% mode.defl=CREATE_AND_QUEUE //% mode.defl=CREATE_AND_QUEUE
void raiseEvent(int src, int value, EventCreationMode mode) { } void raiseEvent(int src, int value, EventCreationMode mode) {
}
/** /**
* Raises an event in the event bus. * Raises an event in the event bus.

View File

@ -1,4 +1,6 @@
// Auto-generated. Do not edit. // Auto-generated. Do not edit.
declare namespace images {
}
declare namespace basic { declare namespace basic {
} }

View File

@ -28,7 +28,7 @@ namespace game {
var _countdownPause: number = 0; var _countdownPause: number = 0;
var _level: number = 1; var _level: number = 1;
var _gameId: number = 0; var _gameId: number = 0;
var img: images.Image; var img: Image;
var sprites: LedSprite[]; var sprites: LedSprite[];
/** /**

87
libs/microbit/images.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "BitVM.h"
typedef ImageData* Image;
//% color=45 weight=31
namespace images {
/**
* Creates an image that fits on the LED screen.
*/
//% weight=75 help=images/create-image
//% blockId=device_build_image block="create image"
Image createImage(ImageLiteral leds) {
return MicroBitImage(getbytes(leds)).clone().leakData();
}
/**
* Creates an image with 2 frames.
*/
//% weight=74 help=images/create-big-image
//% blockId=device_build_big_image block="create big image" imageLiteral=2
Image createBigImage(ImageLiteral leds) {
return createImage(leds);
}
}
namespace ImageMethods {
/**
* Shows an frame from the image at offset ``x offset``.
* @param xOffset TODO
*/
//% help=images/show-image weight=80 async
//% blockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8
void showImage(Image i, int xOffset = 0) {
uBit.display.print(MicroBitImage(i), -xOffset, 0, 0);
}
/**
* 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
*/
//% help=images/show-image weight=79 async
//% blockId=device_scroll_image block="scroll image %sprite|with offset %frameoffset|and interval (ms) %delay" blockGap=8
void scrollImage(Image id, int frameOffset = 0, int interval = 200) {
MicroBitImage i(id);
if (i.getWidth() <= 5)
showImage(id, 0);
else
uBit.display.animate(i, interval, frameOffset, 0);
}
/**
* Plots the image at a given column to the screen
*/
//% help=images/plot-image
void plotImage(Image i, int xOffset = 0) {
uBit.display.print(MicroBitImage(i), -xOffset, 0, 0, 0);
}
/**
* Sets all pixels off.
*/
//% help=images/clear
void clear(Image i) {
MicroBitImage(i).clear();
}
/**
* Sets a specific pixel brightness at a given position
*/
//% help=
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
*/
//% help=
int pixelBrightness(Image i, int x, int y) {
int pix = MicroBitImage(i).getPixelValue(x, y);
if (pix < 0) return 0;
return pix;
}
}

View File

@ -1,63 +0,0 @@
//% color=45 weight=31
namespace images {
/**
* Creates an image that fits on the LED screen.
*/
//% weight=75 help=images/create-image
//% blockId=device_build_image block="create image" imageLiteral=1
export function createImage(leds: string): Image {
return null;
}
/**
* Creates an image with 2 frames.
*/
//% weight=74 help=images/create-big-image
//% blockId=device_build_big_image block="create big image" imageLiteral=2
export function createBigImage(leds: string): Image {
return null;
}
export class Image {
/**
* Shows an frame from the image at offset ``x offset``.
* @param xOffset TODO
*/
//% help=images/show-image weight=80 shim=micro_bit::showImage
//% blockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8
public showImage(xOffset: number = 0): void {}
/**
* 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
*/
//% help=images/show-image weight=79 shim=micro_bit::scrollImage async
//% blockId=device_scroll_image block="scroll image %sprite|with offset %frameoffset|and interval (ms) %delay" blockGap=8
public scrollImage(frameOffset: number = 0, interval : number = 200) {
}
/**
* Plots the image at a given column to the screen
*/
public plotImage(xOffset: number = 0): void {}
/**
* Sets all pixels off.
*/
public clear(): void {}
/**
* Sets a specific pixel brightness at a given position
*/
public setPixelBrightness(x: number, y: number, v: number): void {}
/**
* Gets the pixel brightness ([0..255]) at a given position
*/
public pixelBrightness(x: number, y: number): number {
return 0;
}
}
}

View File

@ -9,7 +9,7 @@
"shims.d.ts", "shims.d.ts",
"core.d.ts", "core.d.ts",
"mbit.ts", "mbit.ts",
"images.ts", "images.cpp",
"basic.cpp", "basic.cpp",
"input.cpp", "input.cpp",
"input.ts", "input.ts",

View File

@ -186,7 +186,7 @@ namespace led {
* Takes a screenshot of the LED screen and returns an image. * Takes a screenshot of the LED screen and returns an image.
*/ */
//% shim=uBit.display.screenShot help=led/screenshot //% shim=uBit.display.screenShot help=led/screenshot
export function screenshot(): images.Image { export function screenshot(): Image {
/* /*
let img: Image; let img: Image;
img = image.createImage(""); img = image.createImage("");

View File

@ -1,6 +1,70 @@
// Auto-generated. Do not edit. // Auto-generated. Do not edit.
//% color=45 weight=31
declare namespace images {
/**
* Creates an image that fits on the LED screen.
*/
//% weight=75 help=images/create-image
//% blockId=device_build_image block="create image" imageLiteral=1 shim=images::createImage
function createImage(leds: string): Image;
/**
* Creates an image with 2 frames.
*/
//% weight=74 help=images/create-big-image
//% blockId=device_build_big_image block="create big image" imageLiteral=2 shim=images::createBigImage
function createBigImage(leds: string): Image;
}
declare interface Image {
/**
* Shows an frame from the image at offset ``x offset``.
* @param xOffset TODO
*/
//% help=images/show-image weight=80 async
//% blockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8 xOffset.defl=0 shim=ImageMethods::showImage
showImage(xOffset?: number): void;
/**
* 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
*/
//% help=images/show-image weight=79 async
//% blockId=device_scroll_image block="scroll image %sprite|with offset %frameoffset|and interval (ms) %delay" blockGap=8 frameOffset.defl=0 interval.defl=200 shim=ImageMethods::scrollImage
scrollImage(frameOffset?: number, interval?: number): void;
/**
* Plots the image at a given column to the screen
*/
//% help=images/plot-image xOffset.defl=0 shim=ImageMethods::plotImage
plotImage(xOffset?: number): void;
/**
* Sets all pixels off.
*/
//% help=images/clear shim=ImageMethods::clear
clear(): void;
/**
* Sets a specific pixel brightness at a given position
*/
//% help= shim=ImageMethods::setPixelBrightness
setPixelBrightness(x: number, y: number, value: number): void;
/**
* Gets the pixel brightness ([0..255]) at a given position
*/
//% help= shim=ImageMethods::pixelBrightness
pixelBrightness(x: number, y: number): number;
}
/** /**
* Provides access to basic micro:bit functionality. * Provides access to basic micro:bit functionality.
*/ */
@ -14,8 +78,7 @@ declare namespace basic {
//% help=basic/show-number //% help=basic/show-number
//% weight=96 //% weight=96
//% blockId=device_show_number block="show|number %number" blockGap=8 icon="\uf1ec" //% blockId=device_show_number block="show|number %number" blockGap=8 icon="\uf1ec"
//% async //% async interval.defl=150 shim=basic::showNumber
//% interval.defl=150 shim=basic::showNumber
function showNumber(value: number, interval?: number): void; function showNumber(value: number, interval?: number): void;
/** /**
@ -27,8 +90,7 @@ declare namespace basic {
//% weight=95 blockGap=8 //% weight=95 blockGap=8
//% imageLiteral=1 async //% imageLiteral=1 async
//% blockId=device_show_leds //% blockId=device_show_leds
//% block="show leds" icon="\uf00a" //% block="show leds" icon="\uf00a" interval.defl=400 shim=basic::showLeds
//% interval.defl=400 imageLiteral=1 shim=basic::showLeds
function showLeds(leds: string, interval?: number): void; function showLeds(leds: string, interval?: number): void;
/** /**
@ -40,8 +102,7 @@ declare namespace basic {
//% weight=87 blockGap=8 //% weight=87 blockGap=8
//% block="show|string %text" icon="\uf031" //% block="show|string %text" icon="\uf031"
//% async //% async
//% blockId=device_print_message //% blockId=device_print_message interval.defl=150 shim=basic::showString
//% interval.defl=150 shim=basic::showString
function showString(text: string, interval?: number): void; function showString(text: string, interval?: number): void;
/** /**
@ -56,8 +117,7 @@ declare namespace basic {
* @param leds TODO * @param leds TODO
* @param interval TODO * @param interval TODO
*/ */
//% help=basic/show-animation shim=micro_bit::showAnimation imageLiteral=1 async //% help=basic/show-animation shim=micro_bit::showAnimation imageLiteral=1 async interval.defl=400 shim=basic::showAnimation
//% interval.defl=400 imageLiteral=1 shim=basic::showAnimation
function showAnimation(leds: string, interval?: number): void; function showAnimation(leds: string, interval?: number): void;
/** /**
@ -111,7 +171,7 @@ declare namespace control {
* @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_QUEUE). * @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_QUEUE).
*/ */
//% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source|with value %value=control_event_value" blockExternalInputs=1 //% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source|with value %value=control_event_value" blockExternalInputs=1
//% mode.defl=CREATE_AND_QUEUE shim=control::raiseEvent //% mode.defl=1 shim=control::raiseEvent
function raiseEvent(src: number, value: number, mode: EventCreationMode ): void; function raiseEvent(src: number, value: number, mode: EventCreationMode ): void;
/** /**