Compare commits

...

17 Commits

Author SHA1 Message Date
5ec4a3118a 0.8.1 2017-01-22 17:16:05 -08:00
352e59bfad updated version number 2017-01-22 17:15:54 -08:00
143c703bf2 0.7.18 2017-01-22 17:14:45 -08:00
37eb855df6 0.7.17 2017-01-22 17:13:27 -08:00
47dac2a175 Bump pxt-core to 0.10.15 2017-01-22 17:13:22 -08:00
211ec7a65d added audio flag 2017-01-22 17:13:12 -08:00
d4220593b0 Serial events (#337)
* added "serial->on data received"

* updated info

* updated docs

* added readline to read entire buffer

* lazy initialize of serial buffers

* init async on event

* updated docs
2017-01-22 17:08:59 -08:00
5d861e1e6d negative scroll fix
fix for Microsoft/pxt#813
2017-01-22 17:07:45 -08:00
64aa0fd6f5 One letter showString shouldn't disappear (#338)
* Fix for "One letter showString shouldn't disappear", fixes #798

* miss semicolon
2017-01-22 17:06:35 -08:00
5be188d6e7 0.7.16 2017-01-19 21:08:24 -08:00
502cca6cc0 Bump pxt-core to 0.10.11 2017-01-19 21:08:21 -08:00
703a876a84 updates style 2017-01-19 21:08:14 -08:00
ca1e3e7231 applying icons to namespaces 2017-01-19 20:55:31 -08:00
a0e971d49f 0.7.15 2017-01-19 11:31:22 -08:00
750faf42e4 Bump pxt-core to 0.10.10 2017-01-19 11:31:14 -08:00
b359411058 Merging array changes from pxt-microbit 2017-01-19 11:30:06 -08:00
9c140a5e6f 0.7.14 2017-01-17 14:12:28 -08:00
28 changed files with 579 additions and 187 deletions

View File

@ -7,8 +7,11 @@ serial.writeLine("");
serial.writeNumber(0);
serial.writeValue("x", 0);
serial.writeString("");
serial.readUntil(",");
serial.readLine();
serial.readString();
serial.redirect(SerialPin.P0, SerialPin.P0, BaudRate.BaudRate115200);
serial.onDataReceived(",", () => {})
```
### See Also

View File

@ -0,0 +1,29 @@
# Serial On Data Received
Registers an event to be fired when one of the delimiter is matched.
```sig
serial.onDataReceived(",", () => {})
```
### Parameters
* `delimiters` is a [string](/reference/types/string) containing any of the character to match
### Example
Read values separated by `,`:
```blocks
serial.onDataReceived(serial.delimiters(Delimiters.Comma), () => {
basic.showString(serial.readUntil(serial.delimiters(Delimiters.Comma)))
})
```
### See also
[serial](/device/serial),
[serial write line](/reference/serial/write-line),
[serial write value](/reference/serial/write-value)

View File

@ -0,0 +1,27 @@
# Serial Read String
Read the buffered serial data as a string
```sig
serial.readString();
```
### Returns
* a [string](/reference/types/string) containing input from the serial port. Empty if no data available.
### Example
The following program scrolls text on the screen as it arrives from serial.
```blocks
basic.forever(() => {
basic.showString(serial.readString());
});
```
### See also
[serial](/device/serial),
[serial write line](/reference/serial/write-line),
[serial write value](/reference/serial/write-value)

View File

@ -1,7 +1,7 @@
/**
* Support for additional Bluetooth services.
*/
//% color=#0082FB weight=20
//% color=#0082FB weight=20 icon="\uf294"
namespace bluetooth {
/**
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.

View File

@ -238,8 +238,10 @@
"pins.spiWrite|param|value": "Data to be sent to the SPI slave",
"serial": "Reading and writing data over a serial connection.",
"serial.delimiters": "Returns the delimiter corresponding string",
"serial.onLineReceived": "Registers an event to be fired when a line has been received",
"serial.onDataReceived": "Registers an event to be fired when one of the delimiter is matched.",
"serial.onDataReceived|param|delimiters": "the characters to match received characters against.",
"serial.readLine": "Reads a line of text from the serial port.",
"serial.readString": "Reads the buffered received data as a string",
"serial.readUntil": "Reads a line of text from the serial port and returns the buffer when the delimiter is met.",
"serial.readUntil|param|delimiter": "text delimiter that separates each text chunk",
"serial.redirect": "Dynamically configuring the serial instance to use pins other than USBTX and USBRX.",

View File

@ -181,7 +181,9 @@
"pins.spiWrite|block": "spi write %value",
"pins|block": "pins",
"serial.delimiters|block": "%del",
"serial.onDataReceived|block": "serial|on data received %delimiters=serial_delimiter_conv",
"serial.readLine|block": "serial|read line",
"serial.readString|block": "serial|read string",
"serial.readUntil|block": "serial|read until %delimiter=serial_delimiter_conv",
"serial.redirect|block": "serial|redirect to|TX %tx|RX %rx|at baud rate %rate",
"serial.writeLine|block": "serial|write line %text",

View File

@ -4,12 +4,12 @@
/**
* Provides access to basic micro:bit functionality.
*/
//% color=#54C9C9 weight=100
//% color=#54C9C9 weight=100 icon="\uf00a"
namespace basic {
/**
* Sets the color on the build-in LED. Set to 0 to turn off.
*/
//% blockId=device_set_led_color block="set led to %color=color_id" icon="\uf00a"
//% blockId=device_set_led_color block="set led to %color=color_id"
//% weight=50
void setLedColor(int color) {
if (!color) {
@ -31,7 +31,7 @@ namespace basic {
*/
//% help=basic/show-number
//% weight=96
//% blockId=device_show_number block="show|number %number" blockGap=8 icon="\uf1ec"
//% blockId=device_show_number block="show|number %number" blockGap=8
//% async
//% parts="ledmatrix"
void showNumber(int value, int interval = 150) {
@ -55,7 +55,7 @@ namespace basic {
//% weight=95 blockGap=8
//% imageLiteral=1 async
//% blockId=device_show_leds
//% block="show leds" icon="\uf00a"
//% block="show leds"
//% parts="ledmatrix"
void showLeds(ImageLiteral leds, int interval = 400) {
uBit.display.print(MicroBitImage(imageBytes(leds)), 0, 0, 0, interval);
@ -68,7 +68,7 @@ namespace basic {
*/
//% help=basic/show-string
//% weight=87 blockGap=8
//% block="show|string %text" icon="\uf031"
//% block="show|string %text"
//% async
//% blockId=device_print_message
//% parts="ledmatrix"
@ -91,7 +91,7 @@ namespace basic {
* Turn off all LEDs
*/
//% help=basic/clear-screen weight=79
//% blockId=device_clear_display block="clear screen" icon="\uf12d"
//% blockId=device_clear_display block="clear screen"
//% parts="ledmatrix"
void clearScreen() {
uBit.display.image.clear();
@ -131,7 +131,7 @@ namespace basic {
* @param body code to execute
*/
//% help=basic/forever weight=55 blockGap=8
//% blockId=device_forever block="forever" icon="\uf01e"
//% blockId=device_forever block="forever"
void forever(Action a) {
if (a != 0) {
incr(a);
@ -145,7 +145,7 @@ namespace basic {
*/
//% help=basic/pause weight=54
//% async block="pause (ms) %pause"
//% blockId=device_pause icon="\uf110"
//% blockId=device_pause
void pause(int ms) {
fiber_sleep(ms);
}

View File

@ -1,7 +1,7 @@
/**
* Runtime and event utilities.
*/
//% weight=1 color="#42495F"
//% weight=1 color="#42495F" icon="\uf233"
//% advanced=true
namespace control {

View File

@ -81,7 +81,7 @@ namespace Boolean_ {
}
//%
bool bang(bool v) { return !v; }
bool bang(int v) { return v == 0; }
}
namespace Number_ {
@ -158,13 +158,19 @@ namespace Array_ {
//%
int length(RefCollection *c) { return c->length(); }
//%
void setLength(RefCollection *c, int newLength) { c->setLength(newLength); }
//%
void push(RefCollection *c, uint32_t x) { c->push(x); }
//%
uint32_t pop(RefCollection *c) { return c->pop(); }
//%
uint32_t getAt(RefCollection *c, int x) { return c->getAt(x); }
//%
void removeAt(RefCollection *c, int x) { c->removeAt(x); }
void setAt(RefCollection *c, int x, uint32_t y) { c->setAt(x, y); }
//%
void setAt(RefCollection *c, int x, uint32_t y) { c->setAt(x, y); }
uint32_t removeAt(RefCollection *c, int x) { return c->removeAt(x); }
//%
void insertAt(RefCollection *c, int x, uint32_t value) { c->insertAt(x, value); }
//%
int indexOf(RefCollection *c, uint32_t x, int start) { return c->indexOf(x, start); }
//%

View File

@ -21,7 +21,7 @@ enum LedSpriteProperty {
/**
* A single-LED sprite game engine
*/
//% color=#008272 weight=32
//% color=#008272 weight=32 icon="\uf11b"
//% advanced=true
namespace game {
let _score: number = 0;

View File

@ -3,7 +3,7 @@
/**
* Creation, manipulation and display of LED images.
*/
//% color=#5C2D91 weight=31
//% color=#5C2D91 weight=31 icon="\uf03e"
//% advanced=true
namespace images {
/**

View File

@ -108,7 +108,7 @@ enum class Gesture {
SixG = MICROBIT_ACCELEROMETER_EVT_6G
};
//% color=#C90072 weight=99
//% color=#C90072 weight=99 icon="\uf192"
namespace input {
/**
* Do something when a button (``A``, ``B`` or both ``A+B``) is pressed
@ -116,7 +116,7 @@ namespace input {
* @param body TODO
*/
//% help=input/on-button-pressed weight=85 blockGap=8
//% blockId=device_button_event block="on button|%NAME|pressed" icon="\uf192"
//% blockId=device_button_event block="on button|%NAME|pressed"
//% parts="buttonpair"
void onButtonPressed(Button button, Action body) {
registerWithDal((int)button, MICROBIT_BUTTON_EVT_CLICK, body);
@ -127,7 +127,7 @@ namespace input {
* @param body TODO
*/
//% help=input/on-gesture weight=84 blockGap=8
//% blockId=device_gesture_event block="on |%NAME" icon="\uf135"
//% blockId=device_gesture_event block="on |%NAME"
//% parts="accelerometer"
void onGesture(Gesture gesture, Action body) {
if ((int)gesture == MICROBIT_ACCELEROMETER_EVT_3G && uBit.accelerometer.getRange() < 3)
@ -143,7 +143,7 @@ namespace input {
* @param body the code to run when the pin is pressed
*/
//% help=input/on-pin-pressed weight=83
//% blockId=device_pin_event block="on pin %NAME|pressed" icon="\uf094"
//% blockId=device_pin_event block="on pin %NAME|pressed"
void onPinPressed(TouchPin name, Action body) {
auto pin = getPin((int)name);
if (!pin) return;
@ -159,7 +159,7 @@ namespace input {
* @param body the code to run when the pin is released
*/
//% help=input/on-pin-released weight=6 blockGap=8
//% blockId=device_pin_released block="on pin %NAME|released" icon="\uf094"
//% blockId=device_pin_released block="on pin %NAME|released"
//% advanced=true
void onPinReleased(TouchPin name, Action body) {
auto pin = getPin((int)name);
@ -176,7 +176,7 @@ namespace input {
//% help=input/button-is-pressed weight=60
//% block="button|%NAME|is pressed"
//% blockId=device_get_button2
//% icon="\uf192" blockGap=8
//% blockGap=8
//% parts="buttonpair"
bool buttonIsPressed(Button button) {
if (button == Button::A)
@ -193,7 +193,7 @@ namespace input {
* @param name pin used to detect the touch
*/
//% help=input/pin-is-pressed weight=58
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed" icon="\uf094"
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed"
//% blockGap=8
bool pinIsPressed(TouchPin name) {
auto pin = getPin((int)name);
@ -211,7 +211,7 @@ namespace input {
* Get the acceleration value in milli-gravitys (when the board is laying flat with the screen up, x=0, y=0 and z=-1024)
* @param dimension TODO
*/
//% help=input/acceleration weight=58 icon="\uf135"
//% help=input/acceleration weight=58
//% blockId=device_acceleration block="acceleration (mg)|%NAME" blockGap=8
//% parts="accelerometer"
int acceleration(Dimension dimension) {
@ -228,7 +228,7 @@ namespace input {
* Reads the light level applied to the LED screen in a range from ``0`` (dark) to ``255`` bright.
*/
//% help=input/light-level weight=57
//% blockId=device_get_light_level block="light level" blockGap=8 icon="\uf185"
//% blockId=device_get_light_level block="light level" blockGap=8
//% parts="ledmatrix"
int lightLevel() {
return uBit.display.readLightLevel();
@ -238,7 +238,7 @@ namespace input {
* Get the current compass heading in degrees.
*/
//% help=input/compass-heading
//% weight=56 icon="\uf14e"
//% weight=56
//% blockId=device_heading block="compass heading (°)" blockGap=8
//% parts="compass"
int compassHeading() {
@ -249,7 +249,7 @@ namespace input {
/**
* Gets the temperature in Celsius degrees (°C).
*/
//% weight=55 icon="\uf06d"
//% weight=55
//% help=input/temperature
//% blockId=device_temperature block="temperature (°C)" blockGap=8
//% parts="thermometer"
@ -262,7 +262,7 @@ namespace input {
* @param kind TODO
*/
//% help=input/rotation weight=52
//% blockId=device_get_rotation block="rotation (°)|%NAME" blockGap=8 icon="\uf197"
//% blockId=device_get_rotation block="rotation (°)|%NAME" blockGap=8
//% parts="accelerometer" advanced=true
int rotation(Rotation kind) {
switch (kind) {
@ -277,7 +277,7 @@ namespace input {
* @param dimension TODO
*/
//% help=input/magnetic-force weight=51
//% blockId=device_get_magnetic_force block="magnetic force (µT)|%NAME" blockGap=8 icon="\uf076"
//% blockId=device_get_magnetic_force block="magnetic force (µT)|%NAME" blockGap=8
//% parts="compass"
//% advanced=true
int magneticForce(Dimension dimension) {
@ -297,7 +297,7 @@ namespace input {
* Gets the number of milliseconds elapsed since power on.
*/
//% help=input/running-time weight=50
//% blockId=device_get_running_time block="running time (ms)" icon="\uf017"
//% blockId=device_get_running_time block="running time (ms)"
//% advanced=true
int runningTime() {
return system_timer_current_time();
@ -314,7 +314,7 @@ namespace input {
* @param range a value describe the maximum strengh of acceleration measured
*/
//% help=input/set-accelerometer-range
//% blockId=device_set_accelerometer_range block="set accelerometer|range %range" icon="\uf135"
//% blockId=device_set_accelerometer_range block="set accelerometer|range %range"
//% weight=5
//% parts="accelerometer"
//% advanced=true

View File

@ -8,7 +8,7 @@ enum class DisplayMode_ {
// TODO DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE
};
//% color=#8169E6 weight=35
//% color=#8169E6 weight=35 icon="\uf205"
namespace led {
/**
@ -17,7 +17,7 @@ namespace led {
* @param y TODO
*/
//% help=led/plot weight=78
//% blockId=device_plot block="plot|x %x|y %y" icon="\uf205" blockGap=8
//% blockId=device_plot block="plot|x %x|y %y" blockGap=8
//% parts="ledmatrix"
void plot(int x, int y) {
uBit.display.image.setPixelValue(x, y, 1);
@ -29,7 +29,7 @@ namespace led {
* @param y TODO
*/
//% help=led/unplot weight=77
//% blockId=device_unplot block="unplot|x %x|y %y" icon="\uf204" blockGap=8
//% blockId=device_unplot block="unplot|x %x|y %y" blockGap=8
//% parts="ledmatrix"
void unplot(int x, int y) {
uBit.display.image.setPixelValue(x, y, 0);
@ -41,7 +41,7 @@ namespace led {
* @param y TODO
*/
//% help=led/point weight=76
//% blockId=device_point block="point|x %x|y %y" icon="\uf10c"
//% blockId=device_point block="point|x %x|y %y"
//% parts="ledmatrix"
bool point(int x, int y) {
int pix = uBit.display.image.getPixelValue(x, y);
@ -52,7 +52,7 @@ namespace led {
* Get the screen brightness from 0 (off) to 255 (full bright).
*/
//% help=led/brightness weight=60
//% blockId=device_get_brightness block="brightness" icon="\uf042" blockGap=8
//% blockId=device_get_brightness block="brightness" blockGap=8
//% parts="ledmatrix"
//% advanced=true
int brightness() {
@ -64,7 +64,7 @@ namespace led {
* @param value the brightness value, eg:255, 127, 0
*/
//% help=led/set-brightness weight=59
//% blockId=device_set_brightness block="set brightness %value" icon="\uf042"
//% blockId=device_set_brightness block="set brightness %value"
//% parts="ledmatrix"
//% advanced=true
void setBrightness(int value) {
@ -75,7 +75,7 @@ namespace led {
* Cancels the current animation and clears other pending animations.
*/
//% weight=50 help=led/stop-animation
//% blockId=device_stop_animation block="stop animation" icon="\uf04d"
//% blockId=device_stop_animation block="stop animation"
//% parts="ledmatrix"
//% advanced=true
void stopAnimation() {
@ -95,7 +95,7 @@ namespace led {
/**
* Turns on or off the display
*/
//% help=led/enable blockId=device_led_enable icon="\uf04d"
//% help=led/enable blockId=device_led_enable
//% advanced=true parts="ledmatrix"
void enable(bool on) {
if (on) uBit.display.enable();

View File

@ -19,7 +19,7 @@ enum Motor {
/**
* Blocks to control the onboard motors
*/
//% color=#008272 weight=30
//% color=#008272 weight=30 icon="\uf1b9"
namespace motors {
/**
* Turns on the motor at a certain percent of power. Switches to single motor mode!

View File

@ -125,7 +125,7 @@ enum BeatFraction {
/**
* Generation of music tones through pin ``P0``.
*/
//% color=#DF4600 weight=98
//% color=#DF4600 weight=98 icon="\uf025"
namespace music {
let beatsPerMinute: number = 120;
@ -145,7 +145,7 @@ namespace music {
* @param frequency pitch of the tone to play in Hertz (Hz)
*/
//% help=music/ring-tone weight=80
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" blockGap=8
//% parts="speaker" async
export function ringTone(frequency: number) {
playTone(frequency, 0);

View File

@ -1,7 +1,7 @@
/**
* Control currents in Pins for analog/digital signals, servos, i2c, ...
*/
//% color=#A80000 weight=30
//% color=#A80000 weight=30 icon="\uf140"
//% advanced=true
namespace pins {
/**

View File

@ -141,63 +141,327 @@ namespace pxt {
printf("RefRecord %p r=%d size=%d bytes\n", r, r->refcnt, r->getVTable()->numbytes);
}
void RefCollection::push(uint32_t x) {
if (isRef()) incr(x);
data.push_back(x);
}
uint32_t RefCollection::getAt(int x) {
if (in_range(x)) {
uint32_t tmp = data.at(x);
if (isRef()) incr(tmp);
return tmp;
}
else {
error(ERR_OUT_OF_BOUNDS);
return 0;
uint32_t Segment::get(uint32_t i)
{
#ifdef DEBUG_BUILD
printf("In Segment::get index:%u\n", i);
this->print();
#endif
if (i < length)
{
return data[i];
}
return Segment::DefaultValue;
}
void RefCollection::removeAt(int x) {
if (!in_range(x))
return;
if (isRef()) decr(data.at(x));
data.erase(data.begin()+x);
}
void RefCollection::setAt(int x, uint32_t y) {
if (!in_range(x))
return;
if (isRef()) {
decr(data.at(x));
incr(y);
}
data.at(x) = y;
}
int RefCollection::indexOf(uint32_t x, int start) {
if (!in_range(start))
return -1;
if (isString()) {
StringData *xx = (StringData*)x;
for (uint32_t i = start; i < data.size(); ++i) {
StringData *ee = (StringData*)data.at(i);
if (xx->len == ee->len && memcmp(xx->data, ee->data, xx->len) == 0)
return (int)i;
void Segment::set(uint32_t i, uint32_t value)
{
if (i < size)
{
data[i] = value;
}
} else {
for (uint32_t i = start; i < data.size(); ++i)
if (data.at(i) == x)
else if (i < Segment::MaxSize)
{
growByMin(i + 1);
data[i] = value;
}
if (length <= i)
{
length = i + 1;
}
#ifdef DEBUG_BUILD
printf("In Segment::set\n");
this->print();
#endif
return;
}
uint16_t Segment::growthFactor(uint16_t size)
{
if (size == 0)
{
return 4;
}
if (size < 64)
{
return size * 2; // Double
}
if (size < 512)
{
return size * 5/3; //Grow by 1.66 rate
}
return size + 256; //Grow by constant rate
}
void Segment::growByMin(uint16_t minSize)
{
growBy(max(minSize, growthFactor(size)));
}
void Segment::growBy(uint16_t newSize)
{
#ifdef DEBUG_BUILD
printf("growBy: %d\n", newSize);
this->print();
#endif
if (size < newSize)
{
//this will throw if unable to allocate
uint32_t *tmp = (uint32_t*)(::operator new(newSize * sizeof(uint32_t)));
//Copy existing data
if (size)
{
memcpy(tmp, data, size * sizeof(uint32_t));
}
//fill the rest with default value
memset(tmp + size, Segment::DefaultValue, (newSize - size) * sizeof(uint32_t));
//free older segment;
::operator delete(data);
data = tmp;
size = newSize;
#ifdef DEBUG_BUILD
printf("growBy - after reallocation\n");
this->print();
#endif
}
//else { no shrinking yet; }
return;
}
void Segment::ensure(uint16_t newSize)
{
if (newSize < size)
{
return;
}
growByMin(newSize);
}
void Segment::setLength(uint32_t newLength)
{
if (newLength > size)
{
ensure(length);
}
length = newLength;
return;
}
void Segment::push(uint32_t value)
{
this->set(length, value);
}
uint32_t Segment::pop()
{
#ifdef DEBUG_BUILD
printf("In Segment::pop\n");
this->print();
#endif
if (length > 0)
{
uint32_t value = data[length];
data[length] = Segment::DefaultValue;
--length;
return value;
}
return Segment::DefaultValue;
}
//this function removes an element at index i and shifts the rest of the elements to
//left to fill the gap
uint32_t Segment::remove(uint32_t i)
{
#ifdef DEBUG_BUILD
printf("In Segment::remove index:%u\n", i);
this->print();
#endif
if (i < length)
{
//value to return
uint32_t ret = data[i];
if (i + 1 < length)
{
//Move the rest of the elements to fill in the gap.
memmove(data + i, data + i + 1, (length - i - 1) * sizeof(uint32_t));
}
length--;
data[length] = Segment::DefaultValue;
#ifdef DEBUG_BUILD
printf("After Segment::remove index:%u\n", i);
this->print();
#endif
return ret;
}
return Segment::DefaultValue;
}
//this function inserts element value at index i by shifting the rest of the elements right.
void Segment::insert(uint32_t i, uint32_t value)
{
#ifdef DEBUG_BUILD
printf("In Segment::insert index:%u value:%u\n", i, value);
this->print();
#endif
if (i < length)
{
ensure(length + 1);
if (i + 1 < length)
{
//Move the rest of the elements to fill in the gap.
memmove(data + i + 1, data + i, (length - i) * sizeof(uint32_t));
}
data[i] = value;
length++;
}
else
{
//This is insert beyond the length, just call set which will adjust the length
set(i, value);
}
#ifdef DEBUG_BUILD
printf("After Segment::insert index:%u\n", i);
this->print();
#endif
}
void Segment::print()
{
printf("Segment: %x, length: %u, size: %u\n", data, (uint32_t)length, (uint32_t)size);
for(uint32_t i = 0; i < size; i++)
{
printf("%d ",(uint32_t)data[i]);
}
printf("\n");
}
bool Segment::isValidIndex(uint32_t i)
{
if (i > length)
{
return false;
}
return true;
}
void Segment::destroy()
{
#ifdef DEBUG_BUILD
printf("In Segment::destroy\n");
this->print();
#endif
length = size = 0;
::operator delete(data);
data = nullptr;
}
void RefCollection::push(uint32_t x)
{
if (isRef()) incr(x);
head.push(x);
}
uint32_t RefCollection::pop()
{
uint32_t ret = head.pop();
if (isRef())
{
incr(ret);
}
return ret;
}
uint32_t RefCollection::getAt(int i)
{
uint32_t tmp = head.get(i);
if (isRef())
{
incr(tmp);
}
return tmp;
}
uint32_t RefCollection::removeAt(int i)
{
if (isRef())
{
decr(head.get(i));
}
return head.remove(i);
}
void RefCollection::insertAt(int i, uint32_t value)
{
head.insert(i, value);
if (isRef())
{
incr(value);
}
}
void RefCollection::setAt(int i, uint32_t value)
{
if (isRef())
{
if (head.isValidIndex((uint32_t)i))
{
decr(head.get(i));
}
incr(value);
}
head.set(i, value);
}
int RefCollection::indexOf(uint32_t x, int start)
{
if (isString())
{
StringData *xx = (StringData*)x;
uint32_t i = start;
while(head.isValidIndex(i))
{
StringData *ee = (StringData*)head.get(i);
if (ee == xx)
{
//handles ee being null
return (int) i;
}
if (ee && xx->len == ee->len && memcmp(xx->data, ee->data, xx->len) == 0)
{
return (int)i;
}
i++;
}
}
else
{
uint32_t i = start;
while(head.isValidIndex(i))
{
if (head.get(i) == x)
{
return (int)i;
}
i++;
}
}
return -1;
}
int RefCollection::removeElement(uint32_t x) {
int RefCollection::removeElement(uint32_t x)
{
int idx = indexOf(x, 0);
if (idx >= 0) {
removeAt(idx);
@ -239,17 +503,20 @@ namespace pxt {
void RefCollection::destroy()
{
if (this->isRef())
for (uint32_t i = 0; i < this->data.size(); ++i) {
decr(this->data[i]);
this->data[i] = 0;
{
for(uint32_t i = 0; i < this->head.getLength(); i++)
{
decr(this->head.get(i));
}
this->data.resize(0);
}
this->head.destroy();
delete this;
}
void RefCollection::print()
{
printf("RefCollection %p r=%d flags=%d size=%d [%p, ...]\n", this, refcnt, getFlags(), data.size(), data.size() > 0 ? data[0] : 0);
printf("RefCollection %p r=%d flags=%d size=%d\n", this, refcnt, getFlags(), head.getLength());
head.print();
}
PXT_VTABLE_CTOR(RefAction) {}

View File

@ -1,7 +1,7 @@
#ifndef __PXT_H
#define __PXT_H
// #define DEBUG_MEMLEAKS 1
//#define DEBUG_MEMLEAKS 1
#pragma GCC diagnostic ignored "-Wunused-parameter"
@ -63,7 +63,7 @@ namespace pxt {
int templateHash();
int programHash();
uint32_t programSize();
uint32_t afterProgramPage();
uint32_t afterProgramPage();
int getNumGlobals();
RefRecord* mkClassInstance(int vtableOffset);
@ -167,11 +167,49 @@ namespace pxt {
}
};
class Segment {
private:
uint32_t* data;
uint16_t length;
uint16_t size;
static const uint16_t MaxSize = 0xFFFF;
static const uint32_t DefaultValue = 0x0;
static uint16_t growthFactor(uint16_t size);
void growByMin(uint16_t minSize);
void growBy(uint16_t newSize);
void ensure(uint16_t newSize);
public:
Segment() : data (nullptr), length(0), size(0) {};
uint32_t get(uint32_t i);
void set(uint32_t i, uint32_t value);
uint32_t getLength() { return length;};
void setLength(uint32_t newLength);
void push(uint32_t value);
uint32_t pop();
uint32_t remove(uint32_t i);
void insert(uint32_t i, uint32_t value);
bool isValidIndex(uint32_t i);
void destroy();
void print();
};
// A ref-counted collection of either primitive or ref-counted objects (String, Image,
// user-defined record, another collection)
class RefCollection
: public RefObject
{
private:
Segment head;
public:
// 1 - collection of refs (need decr)
// 2 - collection of strings (in fact we always have 3, never 2 alone)
@ -179,23 +217,23 @@ namespace pxt {
inline bool isRef() { return getFlags() & 1; }
inline bool isString() { return getFlags() & 2; }
std::vector<uint32_t> data;
RefCollection(uint16_t f);
inline bool in_range(int x) {
return (0 <= x && x < (int)data.size());
}
inline int length() { return data.size(); }
void destroy();
void print();
uint32_t length() { return head.getLength();}
void setLength(uint32_t newLength) { head.setLength(newLength); }
void push(uint32_t x);
uint32_t getAt(int x);
void removeAt(int x);
void setAt(int x, uint32_t y);
uint32_t pop();
uint32_t getAt(int i);
void setAt(int i, uint32_t x);
//removes the element at index i and shifts the other elements left
uint32_t removeAt(int i);
//inserts the element at index i and moves the other elements right.
void insertAt(int i, uint32_t x);
int indexOf(uint32_t x, int start);
int removeElement(uint32_t x);
};

View File

@ -51,24 +51,27 @@ namespace serial {
}
/**
* Reads a line of text from the serial port.
*/
//% help=serial/read-line
//% blockId=serial_read_line block="serial|read line"
//% weight=20 blockGap=8
StringData* readLine() {
return readUntil(ManagedString("\n").leakData());
* Reads the buffered received data as a string
*/
//% blockId=serial_read_buffer block="serial|read string"
//% weight=18
StringData* readString() {
int n = uBit.serial.getRxBufferSize();
if (n == 0) return ManagedString("").leakData();
return ManagedString(uBit.serial.read(n, MicroBitSerialMode::ASYNC)).leakData();
}
/**
* Registers an event to be fired when one of the delimiter is matched
* @param delimiters the characters to match received characters against. eg:"\n"
* Registers an event to be fired when one of the delimiter is matched.
* @param delimiters the characters to match received characters against.
*/
// help=serial/on-data-received
// weight=18
//% help=serial/on-data-received
//% weight=18 blockId=serial_on_data_received block="serial|on data received %delimiters=serial_delimiter_conv"
void onDataReceived(StringData* delimiters, Action body) {
uBit.serial.eventOn(ManagedString(delimiters));
registerWithDal(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_DELIM_MATCH, body);
// lazy initialization of serial buffers
uBit.serial.read(MicroBitSerialMode::ASYNC);
}
/**

View File

@ -1,7 +1,7 @@
/**
* Reading and writing data over a serial connection.
*/
//% weight=2 color=#002050
//% weight=2 color=#002050 icon="\uf287"
//% advanced=true
namespace serial {
/**
@ -38,20 +38,20 @@ namespace serial {
}
/**
* Registers an event to be fired when a line has been received
*/
// help=serial/on-line-received
// blockId=serial_on_line_received block="serial on line received"
// weight=21 blockGap=8
export function onLineReceived(body: Action): void {
// serial.onDataReceived("\n", body);
* Reads a line of text from the serial port.
*/
//% help=serial/read-line
//% blockId=serial_read_line block="serial|read line"
//% weight=20 blockGap=8
export function readLine(): string {
return serial.readUntil(delimiters(Delimiters.NewLine));
}
/**
* Returns the delimiter corresponding string
*/
//% blockId="serial_delimiter_conv" block="%del"
//% weight=1
//% weight=1 blockHidden=true
export function delimiters(del: Delimiters): string {
// even though it might not look like, this is more
// (memory) efficient than the C++ implementation, because the

83
libs/core/shims.d.ts vendored
View File

@ -4,7 +4,7 @@
/**
* Creation, manipulation and display of LED images.
*/
//% color=#5C2D91 weight=31
//% color=#5C2D91 weight=31 icon="\uf03e"
//% advanced=true
declare namespace images {
@ -126,13 +126,13 @@ declare interface Image {
/**
* Provides access to basic micro:bit functionality.
*/
//% color=#54C9C9 weight=100
//% color=#54C9C9 weight=100 icon="\uf00a"
declare namespace basic {
/**
* Sets the color on the build-in LED. Set to 0 to turn off.
*/
//% blockId=device_set_led_color block="set led to %color=color_id" icon="\uf00a"
//% blockId=device_set_led_color block="set led to %color=color_id"
//% weight=50 shim=basic::setLedColor
function setLedColor(color: number): void;
@ -142,7 +142,7 @@ declare namespace basic {
*/
//% help=basic/show-number
//% weight=96
//% blockId=device_show_number block="show|number %number" blockGap=8 icon="\uf1ec"
//% blockId=device_show_number block="show|number %number" blockGap=8
//% async
//% parts="ledmatrix" interval.defl=150 shim=basic::showNumber
function showNumber(value: number, interval?: number): void;
@ -156,7 +156,7 @@ declare namespace basic {
//% weight=95 blockGap=8
//% imageLiteral=1 async
//% blockId=device_show_leds
//% block="show leds" icon="\uf00a"
//% block="show leds"
//% parts="ledmatrix" interval.defl=400 shim=basic::showLeds
function showLeds(leds: string, interval?: number): void;
@ -167,7 +167,7 @@ declare namespace basic {
*/
//% help=basic/show-string
//% weight=87 blockGap=8
//% block="show|string %text" icon="\uf031"
//% block="show|string %text"
//% async
//% blockId=device_print_message
//% parts="ledmatrix" interval.defl=150 shim=basic::showString
@ -177,7 +177,7 @@ declare namespace basic {
* Turn off all LEDs
*/
//% help=basic/clear-screen weight=79
//% blockId=device_clear_display block="clear screen" icon="\uf12d"
//% blockId=device_clear_display block="clear screen"
//% parts="ledmatrix" shim=basic::clearScreen
function clearScreen(): void;
@ -203,7 +203,7 @@ declare namespace basic {
* @param body code to execute
*/
//% help=basic/forever weight=55 blockGap=8
//% blockId=device_forever block="forever" icon="\uf01e" shim=basic::forever
//% blockId=device_forever block="forever" shim=basic::forever
function forever(a: () => void): void;
/**
@ -212,13 +212,13 @@ declare namespace basic {
*/
//% help=basic/pause weight=54
//% async block="pause (ms) %pause"
//% blockId=device_pause icon="\uf110" shim=basic::pause
//% blockId=device_pause shim=basic::pause
function pause(ms: number): void;
}
//% color=#C90072 weight=99
//% color=#C90072 weight=99 icon="\uf192"
declare namespace input {
/**
@ -227,7 +227,7 @@ declare namespace input {
* @param body TODO
*/
//% help=input/on-button-pressed weight=85 blockGap=8
//% blockId=device_button_event block="on button|%NAME|pressed" icon="\uf192"
//% blockId=device_button_event block="on button|%NAME|pressed"
//% parts="buttonpair" shim=input::onButtonPressed
function onButtonPressed(button: Button, body: () => void): void;
@ -236,7 +236,7 @@ declare namespace input {
* @param body TODO
*/
//% help=input/on-gesture weight=84 blockGap=8
//% blockId=device_gesture_event block="on |%NAME" icon="\uf135"
//% blockId=device_gesture_event block="on |%NAME"
//% parts="accelerometer" shim=input::onGesture
function onGesture(gesture: Gesture, body: () => void): void;
@ -246,7 +246,7 @@ declare namespace input {
* @param body the code to run when the pin is pressed
*/
//% help=input/on-pin-pressed weight=83
//% blockId=device_pin_event block="on pin %NAME|pressed" icon="\uf094" shim=input::onPinPressed
//% blockId=device_pin_event block="on pin %NAME|pressed" shim=input::onPinPressed
function onPinPressed(name: TouchPin, body: () => void): void;
/**
@ -255,7 +255,7 @@ declare namespace input {
* @param body the code to run when the pin is released
*/
//% help=input/on-pin-released weight=6 blockGap=8
//% blockId=device_pin_released block="on pin %NAME|released" icon="\uf094"
//% blockId=device_pin_released block="on pin %NAME|released"
//% advanced=true shim=input::onPinReleased
function onPinReleased(name: TouchPin, body: () => void): void;
@ -265,7 +265,7 @@ declare namespace input {
//% help=input/button-is-pressed weight=60
//% block="button|%NAME|is pressed"
//% blockId=device_get_button2
//% icon="\uf192" blockGap=8
//% blockGap=8
//% parts="buttonpair" shim=input::buttonIsPressed
function buttonIsPressed(button: Button): boolean;
@ -274,7 +274,7 @@ declare namespace input {
* @param name pin used to detect the touch
*/
//% help=input/pin-is-pressed weight=58
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed" icon="\uf094"
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed"
//% blockGap=8 shim=input::pinIsPressed
function pinIsPressed(name: TouchPin): boolean;
@ -282,7 +282,7 @@ declare namespace input {
* Get the acceleration value in milli-gravitys (when the board is laying flat with the screen up, x=0, y=0 and z=-1024)
* @param dimension TODO
*/
//% help=input/acceleration weight=58 icon="\uf135"
//% help=input/acceleration weight=58
//% blockId=device_acceleration block="acceleration (mg)|%NAME" blockGap=8
//% parts="accelerometer" shim=input::acceleration
function acceleration(dimension: Dimension): number;
@ -291,7 +291,7 @@ declare namespace input {
* Reads the light level applied to the LED screen in a range from ``0`` (dark) to ``255`` bright.
*/
//% help=input/light-level weight=57
//% blockId=device_get_light_level block="light level" blockGap=8 icon="\uf185"
//% blockId=device_get_light_level block="light level" blockGap=8
//% parts="ledmatrix" shim=input::lightLevel
function lightLevel(): number;
@ -299,7 +299,7 @@ declare namespace input {
* Get the current compass heading in degrees.
*/
//% help=input/compass-heading
//% weight=56 icon="\uf14e"
//% weight=56
//% blockId=device_heading block="compass heading (°)" blockGap=8
//% parts="compass" shim=input::compassHeading
function compassHeading(): number;
@ -307,7 +307,7 @@ declare namespace input {
/**
* Gets the temperature in Celsius degrees (°C).
*/
//% weight=55 icon="\uf06d"
//% weight=55
//% help=input/temperature
//% blockId=device_temperature block="temperature (°C)" blockGap=8
//% parts="thermometer" shim=input::temperature
@ -318,7 +318,7 @@ declare namespace input {
* @param kind TODO
*/
//% help=input/rotation weight=52
//% blockId=device_get_rotation block="rotation (°)|%NAME" blockGap=8 icon="\uf197"
//% blockId=device_get_rotation block="rotation (°)|%NAME" blockGap=8
//% parts="accelerometer" advanced=true shim=input::rotation
function rotation(kind: Rotation): number;
@ -327,7 +327,7 @@ declare namespace input {
* @param dimension TODO
*/
//% help=input/magnetic-force weight=51
//% blockId=device_get_magnetic_force block="magnetic force (µT)|%NAME" blockGap=8 icon="\uf076"
//% blockId=device_get_magnetic_force block="magnetic force (µT)|%NAME" blockGap=8
//% parts="compass"
//% advanced=true shim=input::magneticForce
function magneticForce(dimension: Dimension): number;
@ -336,7 +336,7 @@ declare namespace input {
* Gets the number of milliseconds elapsed since power on.
*/
//% help=input/running-time weight=50
//% blockId=device_get_running_time block="running time (ms)" icon="\uf017"
//% blockId=device_get_running_time block="running time (ms)"
//% advanced=true shim=input::runningTime
function runningTime(): number;
@ -351,7 +351,7 @@ declare namespace input {
* @param range a value describe the maximum strengh of acceleration measured
*/
//% help=input/set-accelerometer-range
//% blockId=device_set_accelerometer_range block="set accelerometer|range %range" icon="\uf135"
//% blockId=device_set_accelerometer_range block="set accelerometer|range %range"
//% weight=5
//% parts="accelerometer"
//% advanced=true shim=input::setAccelerometerRange
@ -432,7 +432,7 @@ declare namespace control {
//% color=#8169E6 weight=35
//% color=#8169E6 weight=35 icon="\uf205"
declare namespace led {
/**
@ -441,7 +441,7 @@ declare namespace led {
* @param y TODO
*/
//% help=led/plot weight=78
//% blockId=device_plot block="plot|x %x|y %y" icon="\uf205" blockGap=8
//% blockId=device_plot block="plot|x %x|y %y" blockGap=8
//% parts="ledmatrix" shim=led::plot
function plot(x: number, y: number): void;
@ -451,7 +451,7 @@ declare namespace led {
* @param y TODO
*/
//% help=led/unplot weight=77
//% blockId=device_unplot block="unplot|x %x|y %y" icon="\uf204" blockGap=8
//% blockId=device_unplot block="unplot|x %x|y %y" blockGap=8
//% parts="ledmatrix" shim=led::unplot
function unplot(x: number, y: number): void;
@ -461,7 +461,7 @@ declare namespace led {
* @param y TODO
*/
//% help=led/point weight=76
//% blockId=device_point block="point|x %x|y %y" icon="\uf10c"
//% blockId=device_point block="point|x %x|y %y"
//% parts="ledmatrix" shim=led::point
function point(x: number, y: number): boolean;
@ -469,7 +469,7 @@ declare namespace led {
* Get the screen brightness from 0 (off) to 255 (full bright).
*/
//% help=led/brightness weight=60
//% blockId=device_get_brightness block="brightness" icon="\uf042" blockGap=8
//% blockId=device_get_brightness block="brightness" blockGap=8
//% parts="ledmatrix"
//% advanced=true shim=led::brightness
function brightness(): number;
@ -479,7 +479,7 @@ declare namespace led {
* @param value the brightness value, eg:255, 127, 0
*/
//% help=led/set-brightness weight=59
//% blockId=device_set_brightness block="set brightness %value" icon="\uf042"
//% blockId=device_set_brightness block="set brightness %value"
//% parts="ledmatrix"
//% advanced=true shim=led::setBrightness
function setBrightness(value: number): void;
@ -488,7 +488,7 @@ declare namespace led {
* Cancels the current animation and clears other pending animations.
*/
//% weight=50 help=led/stop-animation
//% blockId=device_stop_animation block="stop animation" icon="\uf04d"
//% blockId=device_stop_animation block="stop animation"
//% parts="ledmatrix"
//% advanced=true shim=led::stopAnimation
function stopAnimation(): void;
@ -504,7 +504,7 @@ declare namespace led {
/**
* Turns on or off the display
*/
//% help=led/enable blockId=device_led_enable icon="\uf04d"
//% help=led/enable blockId=device_led_enable
//% advanced=true parts="ledmatrix" shim=led::enable
function enable(on: boolean): void;
@ -520,7 +520,7 @@ declare namespace led {
/**
* Blocks to control the onboard motors
*/
//% color=#008272 weight=30
//% color=#008272 weight=30 icon="\uf1b9"
declare namespace motors {
/**
@ -715,12 +715,19 @@ declare namespace serial {
function readUntil(delimiter: string): string;
/**
* Reads a line of text from the serial port.
* Reads the buffered received data as a string
*/
//% help=serial/read-line
//% blockId=serial_read_line block="serial|read line"
//% weight=20 blockGap=8 shim=serial::readLine
function readLine(): string;
//% blockId=serial_read_buffer block="serial|read string"
//% weight=18 shim=serial::readString
function readString(): string;
/**
* Registers an event to be fired when one of the delimiter is matched.
* @param delimiters the characters to match received characters against.
*/
//% help=serial/on-data-received
//% weight=18 blockId=serial_on_data_received block="serial|on data received %delimiters=serial_delimiter_conv" shim=serial::onDataReceived
function onDataReceived(delimiters: string, body: () => void): void;
/**
* Sends a piece of text through Serial connection.

View File

@ -1,7 +1,7 @@
/**
* Communicate data using radio packets
*/
//% color=#E3008C weight=34
//% color=#E3008C weight=34 icon="\uf012"
namespace radio {
export class Packet {
/**

View File

@ -1,7 +1,7 @@
{
"name": "pxt-calliope",
"version": "0.7.13",
"description": "calliope target for PXT",
"version": "0.8.1",
"description": "Calliope Mini editor for PXT",
"keywords": [
"JavaScript",
"education",
@ -34,6 +34,6 @@
"semantic-ui-less": "^2.2.4"
},
"dependencies": {
"pxt-core": "0.10.8"
"pxt-core": "0.10.15"
}
}

View File

@ -292,6 +292,7 @@
"invertedMenu": true,
"invertedToolbox": true,
"monacoToolbox": false,
"hasAudio": true,
"simAnimationEnter": "rotate in",
"simAnimationExit": "rotate out",
"blocklyOptions": {

View File

@ -53,9 +53,9 @@ namespace pxsim {
}
public shiftRight(cols: number) {
for (let x = this.width - 1; x <= 0; --x)
for (let x = this.width - 1; x >= 0; --x)
for (let y = 0; y < 5; ++y)
this.set(x, y, x > cols ? this.get(x - cols, y) : 0);
this.set(x, y, x >= cols ? this.get(x - cols, y) : 0);
}
public clear(): void {
@ -195,11 +195,16 @@ namespace pxsim.ImageMethods {
board().ledMatrixState.animationQ.enqueue({
interval: interval,
frame: () => {
//TODO: support right to left.
if (off >= leds.width || off < 0) return false;
stride > 0 ? display.shiftLeft(stride) : display.shiftRight(-stride);
let c = Math.min(stride, leds.width - off);
leds.copyTo(off, c, display, 5 - stride)
if (stride > 0) {
display.shiftLeft(stride);
const c = Math.min(stride, leds.width - off);
leds.copyTo(off, c, display, 5 - stride)
} else {
display.shiftRight(-stride);
const c = Math.min(-stride, leds.width - off);
leds.copyTo(off, c, display, 0)
}
off += stride;
return true;
},
@ -223,7 +228,7 @@ namespace pxsim.basic {
clearScreen();
pause(interval * 5);
} else {
if (s.length == 1) showLeds(createImageFromString(s + " "), interval * 5)
if (s.length == 1) showLeds(createImageFromString(s), 0);
else ImageMethods.scrollImage(createImageFromString(s + " "), 1, interval);
}
}

View File

@ -35,18 +35,14 @@ namespace pxsim.serial {
board().writeSerial(s);
}
export function readUntil(del: string): string {
return readString();
}
export function readString(): string {
return board().serialState.readSerial();
}
export function readLine(): string {
return board().serialState.readSerial();
}
export function readUntil(del: string): string {
return readLine();
}
export function onDataReceived(delimiters: string, handler: RefAction) {
let b = board();
b.bus.listen(DAL.MICROBIT_ID_SERIAL, DAL.MICROBIT_SERIAL_EVT_DELIM_MATCH, handler);

View File

@ -12,17 +12,23 @@
/*******************************
Add your custom CSS here
*******************************/
/* not relevant in new UI
.openproject {
background: #4ECC60 !important;
}
} */
.blocks-menuitem.active, .javascript-menuitem.active {
background: #738791 !important;
}
/* not relevant in new UI
.help-dropdown-menuitem, .more-dropdown-menuitem {
background: #424955 !important;
margin-right:0px !important;
} */
.huge.download-button i {
display:none !important; // otherwise spans 2 lines
}
.play-button {

View File

@ -32,7 +32,7 @@
@input : 'pxt';
@label : 'pxt';
@list : 'pxt';
@loader : 'pulsar';
@loader : 'pxt';
@rail : 'pxt';
@reveal : 'pxt';
@segment : 'pxt';