Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e545ae948a | ||
|
|
dc6386da52 | ||
|
|
c908794d23 | ||
|
|
8e27d596aa | ||
|
|
9b46145391 | ||
|
|
3182f7c546 | ||
|
|
8aed8548cc | ||
|
|
5e10bd7cc9 | ||
|
|
fddb9ff0d8 | ||
|
|
a0a0554633 | ||
|
|
df92a3daae | ||
|
|
26985f2813 | ||
|
|
e7fd68e7ee | ||
|
|
e63b764568 | ||
|
|
ef821e4b8b | ||
|
|
b7a547c2b4 |
@@ -42,6 +42,18 @@ namespace String_ {
|
|||||||
{
|
{
|
||||||
return ManagedString::EmptyString.leakData();
|
return ManagedString::EmptyString.leakData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//%
|
||||||
|
StringData *substr(StringData *s, int start, int length)
|
||||||
|
{
|
||||||
|
if (length <= 0)
|
||||||
|
return mkEmpty();
|
||||||
|
if (start < 0)
|
||||||
|
start = max(s->len + start, 0);
|
||||||
|
length = min(length, s->len - start);
|
||||||
|
ManagedString x(s);
|
||||||
|
return x.substring(start, length).leakData();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
181
libs/microbit/core.d.ts
vendored
181
libs/microbit/core.d.ts
vendored
@@ -1,181 +0,0 @@
|
|||||||
/// <reference no-default-lib="true"/>
|
|
||||||
|
|
||||||
interface Array<T> {
|
|
||||||
/**
|
|
||||||
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
|
|
||||||
*/
|
|
||||||
//% shim=Array_::length
|
|
||||||
length: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Appends new elements to an array.
|
|
||||||
* @param items New elements of the Array.
|
|
||||||
*/
|
|
||||||
//% shim=Array_::push
|
|
||||||
push(item: T): void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes the last element from an array and returns it.
|
|
||||||
*/
|
|
||||||
//% helper=arrayPop
|
|
||||||
pop(): T;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverses the elements in an Array.
|
|
||||||
*/
|
|
||||||
//% helper=arrayReverse
|
|
||||||
reverse(): void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes the first element from an array and returns it.
|
|
||||||
*/
|
|
||||||
//% helper=arrayShift
|
|
||||||
shift(): T;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a section of an array.
|
|
||||||
* @param start The beginning of the specified portion of the array.
|
|
||||||
* @param end The end of the specified portion of the array.
|
|
||||||
*/
|
|
||||||
//% helper=arraySlice
|
|
||||||
slice(start: number, end: number): T[];
|
|
||||||
|
|
||||||
/** Removes the first occurence of an object. Returns true if removed. */
|
|
||||||
//% shim=Array_::removeElement
|
|
||||||
removeElement(element:T) : boolean;
|
|
||||||
|
|
||||||
/** Removes the object at position index. */
|
|
||||||
//% shim=Array_::removeAt
|
|
||||||
removeAt(idx:number) : void;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes elements from an array.
|
|
||||||
* @param start The zero-based location in the array from which to start removing elements.
|
|
||||||
* @param deleteCount The number of elements to remove.
|
|
||||||
*/
|
|
||||||
//% helper=arraySplice
|
|
||||||
splice(start: number, deleteCount: number): void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inserts new elements at the start of an array.
|
|
||||||
* @param items Elements to insert at the start of the Array.
|
|
||||||
*/
|
|
||||||
//% helper=arrayUnshift
|
|
||||||
unshift(item:T): void;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the index of the first occurrence of a value in an array.
|
|
||||||
* @param searchElement The value to locate in the array.
|
|
||||||
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
|
|
||||||
*/
|
|
||||||
//% shim=Array_::indexOf
|
|
||||||
indexOf(searchElement: T, fromIndex?: number): number;
|
|
||||||
|
|
||||||
|
|
||||||
[n: number]: T;
|
|
||||||
}
|
|
||||||
|
|
||||||
declare interface String {
|
|
||||||
/**
|
|
||||||
* Returns the character at the specified index.
|
|
||||||
* @param pos The zero-based index of the desired character.
|
|
||||||
*/
|
|
||||||
//% shim=String_::charAt
|
|
||||||
charAt(pos: number): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Unicode value of the character at the specified location.
|
|
||||||
* @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned.
|
|
||||||
*/
|
|
||||||
//% shim=String_::charCodeAt
|
|
||||||
charCodeAt(index: number): number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a string that contains the concatenation of two or more strings.
|
|
||||||
* @param other The string to append to the end of the string.
|
|
||||||
*/
|
|
||||||
//% shim=String_::concat
|
|
||||||
concat(other: string): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines whether relative order of two strings (in ASCII encoding).
|
|
||||||
* @param that String to compare to target string
|
|
||||||
*/
|
|
||||||
//% shim=String_::compare
|
|
||||||
compare(that: string): number;
|
|
||||||
|
|
||||||
/** Returns the length of a String object. */
|
|
||||||
//% property shim=String_::length
|
|
||||||
length: number;
|
|
||||||
|
|
||||||
[index: number]: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts A string to an integer.
|
|
||||||
* @param s A string to convert into a number.
|
|
||||||
*/
|
|
||||||
//% shim=String_::toNumber
|
|
||||||
declare function parseInt(s: string): number;
|
|
||||||
|
|
||||||
interface Object {}
|
|
||||||
interface Function {}
|
|
||||||
interface IArguments {}
|
|
||||||
interface RegExp {}
|
|
||||||
|
|
||||||
|
|
||||||
declare interface Boolean {
|
|
||||||
/**
|
|
||||||
* Returns a string representation of an object.
|
|
||||||
*/
|
|
||||||
//% shim=Boolean_::toString
|
|
||||||
toString(): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
declare namespace String {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a string from the given ASCII character code.
|
|
||||||
*/
|
|
||||||
//% shim=String_::fromCharCode
|
|
||||||
function fromCharCode(code: number): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
declare interface Number {
|
|
||||||
/**
|
|
||||||
* Returns a string representation of a number.
|
|
||||||
*/
|
|
||||||
//% shim=Number_::toString
|
|
||||||
toString(): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Numbers and arithmetic operators
|
|
||||||
*/
|
|
||||||
declare namespace Math {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the value of a base expression taken to a specified power.
|
|
||||||
* @param x The base value of the expression.
|
|
||||||
* @param y The exponent value of the expression.
|
|
||||||
*/
|
|
||||||
//% shim=Math_::pow
|
|
||||||
function pow(x: number, y: number): number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a pseudorandom number between 0 and `max`.
|
|
||||||
*/
|
|
||||||
//% shim=Math_::random
|
|
||||||
function random(max: number): number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the square root of a number.
|
|
||||||
* @param x A numeric expression.
|
|
||||||
*/
|
|
||||||
//% shim=Math_::sqrt
|
|
||||||
function sqrt(x: number): number;
|
|
||||||
}
|
|
||||||
@@ -25,6 +25,14 @@ namespace images {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace ImageMethods {
|
namespace ImageMethods {
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows an frame from the image at offset ``x offset``.
|
* Shows an frame from the image at offset ``x offset``.
|
||||||
* @param xOffset TODO
|
* @param xOffset TODO
|
||||||
@@ -35,6 +43,16 @@ namespace ImageMethods {
|
|||||||
uBit.display.print(MicroBitImage(i), -xOffset, 0, 0);
|
uBit.display.print(MicroBitImage(i), -xOffset, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the ``index``-th frame of the image on the screen.
|
||||||
|
* @param xOffset TODO
|
||||||
|
*/
|
||||||
|
//% help=images/plot-frame weight=80
|
||||||
|
void plotFrame(Image i, int xOffset) {
|
||||||
|
// TODO showImage() used in original implementation
|
||||||
|
plotImage(i, xOffset * 5);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scrolls an image .
|
* Scrolls an image .
|
||||||
* @param frameOffset x offset moved on each animation step, eg: 5, 1, -1
|
* @param frameOffset x offset moved on each animation step, eg: 5, 1, -1
|
||||||
@@ -51,14 +69,6 @@ namespace ImageMethods {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
* Sets all pixels off.
|
||||||
*/
|
*/
|
||||||
@@ -85,4 +95,52 @@ namespace ImageMethods {
|
|||||||
if (pix < 0) return 0;
|
if (pix < 0) return 0;
|
||||||
return pix;
|
return pix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the width in columns
|
||||||
|
*/
|
||||||
|
//% help=functions/width
|
||||||
|
int width(Image i) {
|
||||||
|
return i->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the height in rows (always 5)
|
||||||
|
*/
|
||||||
|
//% shim=
|
||||||
|
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=functions/set-pixel
|
||||||
|
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=functions/pixel
|
||||||
|
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=functions/show-frame
|
||||||
|
void showFrame(Image i, int frame) {
|
||||||
|
showImage(i, frame * 5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,8 +134,9 @@ namespace music {
|
|||||||
*/
|
*/
|
||||||
//% help=music/beat weight=49
|
//% help=music/beat weight=49
|
||||||
//% blockId=device_beat block="%fraction|beat"
|
//% blockId=device_beat block="%fraction|beat"
|
||||||
export function beat(fraction : BeatFraction = BeatFraction.Whole): number {
|
export function beat(fraction?: BeatFraction): number {
|
||||||
init();
|
init();
|
||||||
|
if (fraction == null) fraction = BeatFraction.Whole;
|
||||||
let beat = 60000 / beatsPerMinute;
|
let beat = 60000 / beatsPerMinute;
|
||||||
if (fraction == BeatFraction.Whole) return beat;
|
if (fraction == BeatFraction.Whole) return beat;
|
||||||
else if (fraction == BeatFraction.Half) return beat / 2;
|
else if (fraction == BeatFraction.Half) return beat / 2;
|
||||||
|
|||||||
55
libs/microbit/shims.d.ts
vendored
55
libs/microbit/shims.d.ts
vendored
@@ -24,6 +24,12 @@ declare namespace images {
|
|||||||
|
|
||||||
|
|
||||||
declare interface Image {
|
declare interface Image {
|
||||||
|
/**
|
||||||
|
* Plots the image at a given column to the screen
|
||||||
|
*/
|
||||||
|
//% help=images/plot-image xOffset.defl=0 shim=ImageMethods::plotImage
|
||||||
|
plotImage(xOffset?: number): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows an frame from the image at offset ``x offset``.
|
* Shows an frame from the image at offset ``x offset``.
|
||||||
* @param xOffset TODO
|
* @param xOffset TODO
|
||||||
@@ -32,6 +38,13 @@ declare interface Image {
|
|||||||
//% BUGblockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8 xOffset.defl=0 shim=ImageMethods::showImage
|
//% BUGblockId=device_show_image_offset block="show image %sprite|at offset %offset" blockGap=8 xOffset.defl=0 shim=ImageMethods::showImage
|
||||||
showImage(xOffset?: number): void;
|
showImage(xOffset?: number): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the ``index``-th frame of the image on the screen.
|
||||||
|
* @param xOffset TODO
|
||||||
|
*/
|
||||||
|
//% help=images/plot-frame weight=80 shim=ImageMethods::plotFrame
|
||||||
|
plotFrame(xOffset: number): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scrolls an image .
|
* Scrolls an image .
|
||||||
* @param frameOffset x offset moved on each animation step, eg: 5, 1, -1
|
* @param frameOffset x offset moved on each animation step, eg: 5, 1, -1
|
||||||
@@ -41,12 +54,6 @@ declare interface Image {
|
|||||||
//% BUGblockId=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
|
//% BUGblockId=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;
|
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.
|
* Sets all pixels off.
|
||||||
*/
|
*/
|
||||||
@@ -64,6 +71,42 @@ declare interface Image {
|
|||||||
*/
|
*/
|
||||||
//% help= shim=ImageMethods::pixelBrightness
|
//% help= shim=ImageMethods::pixelBrightness
|
||||||
pixelBrightness(x: number, y: number): number;
|
pixelBrightness(x: number, y: number): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the width in columns
|
||||||
|
*/
|
||||||
|
//% help=functions/width shim=ImageMethods::width
|
||||||
|
width(): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the height in rows (always 5)
|
||||||
|
*/
|
||||||
|
//% shim= shim=ImageMethods::height
|
||||||
|
height(): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a pixel state at position ``(x,y)``
|
||||||
|
* @param x TODO
|
||||||
|
* @param y TODO
|
||||||
|
* @param value TODO
|
||||||
|
*/
|
||||||
|
//% help=functions/set-pixel shim=ImageMethods::setPixel
|
||||||
|
setPixel(x: number, y: number, value: boolean): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the pixel state at position ``(x,y)``
|
||||||
|
* @param x TODO
|
||||||
|
* @param y TODO
|
||||||
|
*/
|
||||||
|
//% help=functions/pixel shim=ImageMethods::pixel
|
||||||
|
pixel(x: number, y: number): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows a particular frame of the image strip.
|
||||||
|
* @param frame TODO
|
||||||
|
*/
|
||||||
|
//% weight=70 help=functions/show-frame shim=ImageMethods::showFrame
|
||||||
|
showFrame(frame: number): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,22 +8,20 @@ separately.
|
|||||||
|
|
||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
// Create a NeoPixel driver - specify the number of LEDs:
|
// Create a NeoPixel driver - specify the number of LEDs:
|
||||||
let strip = neopixel.create(24)
|
let strip = neopixel.create(DigitalPin.P0, 24)
|
||||||
|
|
||||||
// set pixel colors
|
// set pixel colors
|
||||||
strip.setPix(0, 255, 255, 255) // white
|
strip.setPixelColor(0, 255, 255, 255) // white
|
||||||
strip.setPix(1, 255, 0, 0) // red
|
strip.setPixelColor(1, 255, 0, 0) // red
|
||||||
strip.setPix(2, 0, 255, 0) // green
|
strip.setPixelColor(2, 0, 255, 0) // green
|
||||||
strip.setPix(3, 0, 0, 255) // blue
|
strip.setPixelColor(3, 0, 0, 255) // blue
|
||||||
|
|
||||||
// send the data to the strip
|
// send the data to the strip
|
||||||
strip.display()
|
strip.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
Use `strip.setPin()` if your strip is not at `P0`.
|
|
||||||
|
|
||||||
Use `strip.setBrigthness()` to lower the brightness (it's maxed out by default).
|
Use `strip.setBrigthness()` to lower the brightness (it's maxed out by default).
|
||||||
|
|
||||||
Use `strip.shift()` or `strip.rotate()` to shift the lights around.
|
Use `strip.shift()` or `strip.rotate()` to shift the lights around.
|
||||||
@@ -33,15 +31,15 @@ Use `strip.shift()` or `strip.rotate()` to shift the lights around.
|
|||||||
This little program will let the position of the microbit control the color of the first LED.
|
This little program will let the position of the microbit control the color of the first LED.
|
||||||
This first LED will then get shifted further away every 100ms.
|
This first LED will then get shifted further away every 100ms.
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let strip = neopixel.create(24)
|
let strip = neopixel.create(DigitalPin.P0, 24)
|
||||||
while (true) {
|
while (true) {
|
||||||
let x = input.acceleration(Dimension.X) / 2
|
let x = input.acceleration(Dimension.X) / 2
|
||||||
let y = input.acceleration(Dimension.Y) / 2
|
let y = input.acceleration(Dimension.Y) / 2
|
||||||
let z = input.acceleration(Dimension.Z) / 2
|
let z = input.acceleration(Dimension.Z) / 2
|
||||||
strip.setPix(0, x, y, -z);
|
strip.setPixelColor(0, x, y, -z);
|
||||||
strip.shift(1);
|
strip.shift(1);
|
||||||
strip.display();
|
strip.show();
|
||||||
basic.pause(100);
|
basic.pause(100);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,25 +1,97 @@
|
|||||||
|
/**
|
||||||
|
* Functions to operate NeoPixel strips.
|
||||||
|
*/
|
||||||
|
//% weight=5 color=#2699BF
|
||||||
namespace neopixel {
|
namespace neopixel {
|
||||||
|
|
||||||
//% shim=sendBufferAsm
|
//% shim=sendBufferAsm
|
||||||
function sendBuffer(buf: Buffer, pin: DigitalPin) {
|
function sendBuffer(buf: Buffer, pin: DigitalPin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Strip {
|
/**
|
||||||
|
* A NeoPixel strip
|
||||||
|
*/
|
||||||
|
export class Strip {
|
||||||
buf: Buffer;
|
buf: Buffer;
|
||||||
pin: DigitalPin;
|
pin: DigitalPin;
|
||||||
brightness: number;
|
brightness: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set give LED to a given color (range 0-255 for r, g, b)
|
||||||
|
*/
|
||||||
|
//% blockId="neopixel_set_pixel_color" block="%strip|set pixel color at %ledoff|red: %red|green: %green|blue: %blue" blockGap=8
|
||||||
|
//% weight=80
|
||||||
|
setPixelColor(ledoff: number, red: number, green: number, blue: number): void {
|
||||||
|
ledoff = ledoff * 3;
|
||||||
|
let br = this.brightness;
|
||||||
|
if (br < 255) {
|
||||||
|
red = (Math.clamp(0, 255, red) * br) >> 8;
|
||||||
|
green = (Math.clamp(0, 255, blue) * br) >> 8;
|
||||||
|
blue = (Math.clamp(0, 255, blue) * br) >> 8;
|
||||||
|
}
|
||||||
|
let buf = this.buf;
|
||||||
|
buf[ledoff + 0] = Math.clamp(0, 255, green);
|
||||||
|
buf[ledoff + 1] = Math.clamp(0, 255, red);
|
||||||
|
buf[ledoff + 2] = Math.clamp(0, 255, blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send all the changes to the strip.
|
||||||
|
*/
|
||||||
|
//% blockId="neopixel_show" block="%strip|show" blockGap=8
|
||||||
|
//% weight=79
|
||||||
|
show() {
|
||||||
|
basic.pause(1)
|
||||||
|
sendBuffer(this.buf, this.pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn off all LEDs.
|
||||||
|
*/
|
||||||
|
//% blockId="neopixel_clear" block="%strip|clear"
|
||||||
|
//% weight=76
|
||||||
|
clear(): void {
|
||||||
|
this.buf.fill(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number of pixels declared on the strip
|
||||||
|
*/
|
||||||
|
//% blockId="neopixel_length" block="%strip|length" blockGap=8
|
||||||
|
//% weight=60
|
||||||
length() {
|
length() {
|
||||||
return this.buf.length / 3
|
return this.buf.length / 3
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the brightness of the strip, 0-255.
|
* Set the brightness of the strip, 0-255. eg: 255
|
||||||
*/
|
*/
|
||||||
|
//% blockId="neopixel_set_brightness" block="%strip|set brightness %brightness" blockGap=8
|
||||||
|
//% weight=59
|
||||||
setBrigthness(brightness: number): void {
|
setBrigthness(brightness: number): void {
|
||||||
this.brightness = brightness;
|
this.brightness = brightness;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shift LEDs forward and clear with zeros.
|
||||||
|
* @params off number of pixels to shift forward, eg: 1
|
||||||
|
*/
|
||||||
|
//% blockId="neopixel_shift" block="%strip|shift pixels forward by %off" blockGap=8
|
||||||
|
//% weight=40
|
||||||
|
shift(off: number = 1): void {
|
||||||
|
this.buf.shift(-off * 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rotate LEDs forward.
|
||||||
|
* @params off number of pixels to rotate forward, eg: 1
|
||||||
|
*/
|
||||||
|
//% blockId="neopixel_rotate" block="%strip|rotate pixels forward by %off" blockGap=8
|
||||||
|
//% weight=39
|
||||||
|
rotate(off:number = 1): void {
|
||||||
|
this.buf.rotate(-off * 3)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the pin where the neopixel is connected, defaults to P0.
|
* Set the pin where the neopixel is connected, defaults to P0.
|
||||||
*/
|
*/
|
||||||
@@ -28,61 +100,20 @@ namespace neopixel {
|
|||||||
pins.digitalWritePin(this.pin, 0)
|
pins.digitalWritePin(this.pin, 0)
|
||||||
basic.pause(50)
|
basic.pause(50)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Turn off all LEDs.
|
|
||||||
*/
|
|
||||||
clear(): void {
|
|
||||||
this.buf.fill(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shift LEDs forward.
|
|
||||||
*/
|
|
||||||
shift(off: number = 1): void {
|
|
||||||
this.buf.shift(-off * 3)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shift LEDs forward.
|
|
||||||
*/
|
|
||||||
rotate(): void {
|
|
||||||
this.buf.rotate(-3)
|
|
||||||
}
|
|
||||||
|
|
||||||
display() {
|
|
||||||
basic.pause(1)
|
|
||||||
sendBuffer(this.buf, this.pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set give LED to a given color (range 0-255 for r, g, b)
|
|
||||||
*/
|
|
||||||
setPix(ledoff: number, r: number, g: number, b: number): void {
|
|
||||||
ledoff = ledoff * 3;
|
|
||||||
let br = this.brightness;
|
|
||||||
if (br < 255) {
|
|
||||||
r = (Math.clamp(0, 255, r) * br) >> 8;
|
|
||||||
g = (Math.clamp(0, 255, b) * br) >> 8;
|
|
||||||
b = (Math.clamp(0, 255, b) * br) >> 8;
|
|
||||||
}
|
|
||||||
let buf = this.buf;
|
|
||||||
buf[ledoff + 0] = Math.clamp(0, 255, g);
|
|
||||||
buf[ledoff + 1] = Math.clamp(0, 255, r);
|
|
||||||
buf[ledoff + 2] = Math.clamp(0, 255, b);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new NeoPixel driver for `numleds` LEDs.
|
* Create a new NeoPixel driver for `numleds` LEDs.
|
||||||
|
* @params pin the pin where the neopixel is connected.
|
||||||
* @params numleds number of leds in the strip, eg: 24,30,60,64
|
* @params numleds number of leds in the strip, eg: 24,30,60,64
|
||||||
*/
|
*/
|
||||||
export function create(numleds: number): Strip {
|
//% blockId="neopixel_create" block="neopixel create|at pin %pin|with %numleds leds"
|
||||||
|
//% weight=90
|
||||||
|
export function create(pin: DigitalPin, numleds: number): Strip {
|
||||||
let strip = new Strip();
|
let strip = new Strip();
|
||||||
strip.buf = pins.createBuffer(numleds * 3);
|
strip.buf = pins.createBuffer(numleds * 3);
|
||||||
strip.setBrigthness(255)
|
strip.setBrigthness(255)
|
||||||
strip.setPin(DigitalPin.P0)
|
strip.setPin(pin)
|
||||||
return strip;
|
return strip;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
let strip = neopixel.create(24);
|
let strip = neopixel.create(DigitalPin.P0, 24);
|
||||||
let br = 100;
|
let br = 100;
|
||||||
strip.setBrigthness(100);
|
strip.setBrigthness(100);
|
||||||
input.onButtonPressed(Button.B, () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
@@ -39,9 +39,9 @@ while (true) {
|
|||||||
if (rotationMode) {
|
if (rotationMode) {
|
||||||
strip.rotate();
|
strip.rotate();
|
||||||
} else {
|
} else {
|
||||||
strip.setPix(0, x, y, -z);
|
strip.setPixelColor(0, x, y, -z);
|
||||||
strip.shift(1);
|
strip.shift(1);
|
||||||
}
|
}
|
||||||
strip.display();
|
strip.show();
|
||||||
basic.pause(100);
|
basic.pause(100);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pxt-microbit",
|
"name": "pxt-microbit",
|
||||||
"version": "0.2.60",
|
"version": "0.2.63",
|
||||||
"description": "BBC micro:bit target for PXT",
|
"description": "BBC micro:bit target for PXT",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"JavaScript",
|
"JavaScript",
|
||||||
@@ -29,6 +29,6 @@
|
|||||||
"typescript": "^1.8.7"
|
"typescript": "^1.8.7"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pxt-core": "0.2.62"
|
"pxt-core": "0.2.67"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"id": "microbit",
|
"id": "microbit",
|
||||||
"name": "code micro:bit",
|
"name": "code micro:bit",
|
||||||
"title": "micro:bit",
|
"title": "code micro:bit",
|
||||||
"corepkg": "microbit",
|
"corepkg": "microbit",
|
||||||
"bundleddirs": [
|
"bundleddirs": [
|
||||||
"libs/microbit",
|
"libs/microbit",
|
||||||
|
|||||||
@@ -416,14 +416,14 @@ svg.sim.grayscale {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.sim-text {
|
.sim-text {
|
||||||
font-family:monospace;
|
font-family:"Lucida Console", Monaco, monospace;
|
||||||
font-size:25px;
|
font-size:25px;
|
||||||
fill:#fff;
|
fill:#fff;
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.sim-text-pin {
|
.sim-text-pin {
|
||||||
font-family:monospace;
|
font-family:"Lucida Console", Monaco, monospace;
|
||||||
font-size:20px;
|
font-size:20px;
|
||||||
fill:#fff;
|
fill:#fff;
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
|
|||||||
Reference in New Issue
Block a user