diff --git a/libs/cpp-test/hello.ts b/libs/cpp-test/hello.ts index 6f4776d1..4b8b9240 100644 --- a/libs/cpp-test/hello.ts +++ b/libs/cpp-test/hello.ts @@ -1,7 +1,3 @@ -//% shim=foo::bar -function test() { -} - basic.plotLeds(` # # . # # . . # . . @@ -10,4 +6,4 @@ basic.plotLeds(` . # # # . `); basic.pause(300); -test(); +foo.bar(); diff --git a/libs/cpp-test/support.cpp b/libs/cpp-test/support.cpp index 1474159a..89d523f9 100644 --- a/libs/cpp-test/support.cpp +++ b/libs/cpp-test/support.cpp @@ -1,6 +1,10 @@ +#include "BitVM.h" +#include "MicroBitTouchDevelop.h" + namespace foo { - GLUE void bar() + //% + void bar() { - micro_bit::scrollNumber(108108, 50); + touch_develop::micro_bit::scrollNumber(108108, 50); } } diff --git a/libs/microbit/basic.ts b/libs/microbit/basic.cpp similarity index 54% rename from libs/microbit/basic.ts rename to libs/microbit/basic.cpp index 5c94e097..f2b999a4 100644 --- a/libs/microbit/basic.ts +++ b/libs/microbit/basic.cpp @@ -1,3 +1,8 @@ +#include "BitVM.h" + + +typedef uint32_t ImageLiteral; + /** * Provides access to basic micro:bit functionality. */ @@ -10,10 +15,19 @@ namespace basic { */ //% help=basic/show-number //% weight=96 - //% shim=micro_bit::scrollNumber //% blockId=device_show_number block="show|number %number" blockGap=8 icon="\uf1ec" //% async - export function showNumber(value: number, interval: number = 150): void { } + void showNumber(int value, int interval = 150) { + if (interval < 0) + return; + + ManagedString t(value); + if (value < 0 || value >= 10) { + uBit.display.scroll(t, interval); + } else { + uBit.display.print(t.charAt(0), interval * 5); + } + } /** * Draws an image on the LED screen. @@ -22,11 +36,12 @@ namespace basic { */ //% help=basic/show-leds //% weight=95 blockGap=8 - //% shim=micro_bit::showLeds //% imageLiteral=1 async //% blockId=device_show_leds //% block="show leds" icon="\uf00a" - export function showLeds(leds: string, interval: number = 400): void { } + void showLeds(ImageLiteral leds, int interval = 400) { + uBit.display.print(MicroBitImage(getbytes(leds)), 0, 0, 0, delay); + } /** * Display text on the display, one character at a time. If the string fits on the screen (i.e. is one letter), does not scroll. @@ -35,19 +50,32 @@ namespace basic { */ //% help=basic/show-string //% weight=87 blockGap=8 - //% shim=micro_bit::scrollString async //% block="show|string %text" icon="\uf031" //% async //% blockId=device_print_message - export function showString(text: string, interval: number = 150): void { } + void showString(StringData *text, int interval = 150) { + if (interval < 0) + return; + ManagedString s(text); + int l = s.length(); + if (l == 0) { + uBit.display.clear(); + uBit.sleep(interval * 5); + } else if (l > 1) { + uBit.display.scroll(s, interval); + } else { + uBit.display.print(s.charAt(0), interval * 5); + } + } /** * Turn off all LEDs */ //% help=basic/clear-screen weight=79 - //% shim=micro_bit::clearScreen //% blockId=device_clear_display block="clear screen" icon="\uf12d" - export function clearScreen(): void { } + void clearScreen() { + uBit.display.image.clear(); + } /** * Shows a sequence of LED screens as an animation. @@ -55,29 +83,48 @@ namespace basic { * @param interval TODO */ //% help=basic/show-animation shim=micro_bit::showAnimation imageLiteral=1 async - export function showAnimation(leds: string, interval: number = 400): void { } + void showAnimation(ImageLiteral leds, int interval = 400) { + uBit.display.animate(MicroBitImage(getbytes(leds)), interval, 5, 0); + } /** * Draws an image on the LED screen. * @param leds TODO */ - //% help=basic/plot-leds weight=80 shim=micro_bit::plotLeds imageLiteral=1 - export function plotLeds(leds: string): void { } + //% help=basic/plot-leds weight=80 shim=micro_bit::plotLeds + void plotLeds(ImageLiteral leds) { + MicroBitImage i(getbytes(leds)); + uBit.display.print(i, 0, 0, 0, 0); + } + + void forever_stub(void *a) { + while (true) { + action::run((Action)a); + micro_bit::pause(20); + } + } /** * Repeats the code forever in the background. On each iteration, allows other codes to run. * @param body TODO */ //% help=basic/forever weight=55 blockGap=8 - //% blockId=device_forever block="forever" icon="\uf01e" shim=micro_bit::forever - export function forever(body: () => void): void { } + //% blockId=device_forever block="forever" icon="\uf01e" + void forever(Action a) { + if (a != 0) { + incr(a); + create_fiber(forever_stub, (void*)a); + } + } /** * Pause for the specified time in milliseconds * @param ms how long to pause for, eg: 100, 200, 500, 1000, 2000 */ //% help=basic/pause weight=54 - //% shim=micro_bit::pause async block="pause (ms) %pause" + //% async block="pause (ms) %pause" //% blockId=device_pause icon="\uf110" - export function pause(ms: number): void { } + void pause(int ms) { + uBit.sleep(ms); + } } diff --git a/libs/microbit/control.cpp b/libs/microbit/control.cpp new file mode 100644 index 00000000..167f3820 --- /dev/null +++ b/libs/microbit/control.cpp @@ -0,0 +1,30 @@ +#include "BitVM.h" + +namespace control { + void fiberDone(void *a) + { + decr((Action)a); + release_fiber(); + } + + /** + * Schedules code that run in the background. + */ + //% help=control/in-background + //% blockId="control_in_background" block="run in background" blockGap=8 + void inBackground(Action a) { + if (a != 0) { + incr(a); + create_fiber((void(*)(void*))action::run, (void*)a, fiberDone); + } + } + + /** + * Resets the BBC micro:bit. + */ + //% weight=30 async help=control/reset + //% blockId="control_reset" block="reset" + void reset() { + uBit.reset(); + } +} diff --git a/libs/microbit/control.ts b/libs/microbit/control.ts index a1d358d1..f8112492 100644 --- a/libs/microbit/control.ts +++ b/libs/microbit/control.ts @@ -192,27 +192,29 @@ enum EventBusValue { //% weight=1 color="#333333" namespace control { - /** - * Schedules code that run in the background. - */ - //% help=control/in-background shim=micro_bit::runInBackground - //% blockId="control_in_background" block="run in background" blockGap=8 - export function inBackground(body: Action): void { } /** - * Resets the BBC micro:bit. + * Returns the value of a C++ runtime constant */ - //% weight=30 shim=uBit.reset async help=control/reset - //% blockId="control_reset" block="reset" - export function reset(): void { } + //% weight=19 weight=19 blockId="control_event_source" block="%id" + export function eventSource(id: EventBusSource) : number { + return id; + } + /** + * Returns the value of a C++ runtime constant + */ + //% weight=19 weight=19 blockId="control_event_value" block="%id" + export function eventValue(id: EventBusValue) : number { + return id; + } /** * Raises an event in the event bus. - @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A. - @param value Component specific code indicating the cause of the event. - @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_QUEUE). - */ - // shim=micro_bit::busRaiseEvent + * @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A. + * @param value Component specific code indicating the cause of the event. + * @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_QUEUE). + */ + //% shim=micro_bit::busRaiseEvent //% 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 export function raiseEvent(src: number, value: number, mode: EventCreationMode = EventCreationMode.CreateAndQueue): void { } @@ -223,19 +225,4 @@ namespace control { //% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source|with value %value=control_event_value" //% blockExternalInputs=1 blockStatement=1 export function onEvent(src: number, value: number, handler: Action): void { } - - /** - * Returns the value of a C++ runtime constant - */ - //% weight=19 shimw=TD_ID weight=19 blockId="control_event_source" block="%id" - export function eventSource(id: EventBusSource) : number { - return 0; - } - /** - * Returns the value of a C++ runtime constant - */ - //% weight=19 shimw=TD_ID weight=19 blockId="control_event_value" block="%id" - export function eventValue(id: EventBusValue) : number { - return 0; - } } diff --git a/libs/microbit/kind.json b/libs/microbit/kind.json index 6cd6bc30..6f827ac1 100644 --- a/libs/microbit/kind.json +++ b/libs/microbit/kind.json @@ -7,9 +7,10 @@ "core.d.ts", "mbit.ts", "images.ts", - "basic.ts", + "basic.cpp", "input.ts", "control.ts", + "control.cpp", "game.ts", "led.ts", "music.ts",