This commit is contained in:
Michal Moskal 2016-04-03 16:52:57 -07:00
Родитель b003af6eae
Коммит 02c41b59bd
10 изменённых файлов: 194 добавлений и 7 удалений

Просмотреть файл

@ -6,6 +6,7 @@
],
"public": true,
"dependencies": {
"microbit": "file:../microbit"
"microbit": "file:../microbit",
"microbit-radio": "file:../microbit-radio"
}
}

Просмотреть файл

@ -59,6 +59,7 @@ testMemoryFreeHOF();
postPreFix()
eqOp()
testEnums()
testBuffer()
// test some top-level code
let xsum = 0;
@ -722,3 +723,52 @@ function switchB(e: En) {
}
return r;
}
function bufferIs(b:Buffer, a:number[]) {
assert(b.length == a.length, "bis-len")
for (let i = 0; i < a.length; ++i) {
if (a[i] != b[i]) {
assert(false, `bufferIs: buf[${i}]:${b[i]} != ${a[i]}`)
}
}
}
function testBuffer() {
let b = pins.createBuffer(3);
assert(b[0] == 0, "buf0");
assert(b[1] == 0, "buf0");
assert(b[2] == 0, "buf0");
assert(b[-100000] == 0, "bufM");
assert(b[100000] == 0, "bufM");
b[0] = 42;
bufferIs(b, [42, 0, 0]);
b[2] = 41;
bufferIs(b, [42, 0, 41]);
b.rotate(1)
bufferIs(b, [0, 41, 42]);
b.rotate(-2)
bufferIs(b, [41, 42, 0]);
b.shift(1)
bufferIs(b, [42, 0, 0]);
b.rotate(9)
bufferIs(b, [42, 0, 0]);
b.rotate(-9)
bufferIs(b, [42, 0, 0]);
b.fill(4);
bufferIs(b, [4, 4, 4]);
b.fill(12, 1, 1);
bufferIs(b, [4, 12, 4]);
b.fill(13, 1, -1);
bufferIs(b, [4, 13, 13]);
b.fill(100, -1, -1);
bufferIs(b, [4, 13, 13]);
b.shift(-1)
bufferIs(b, [0, 4, 13]);
}

72
libs/microbit/buffer.cpp Normal file
Просмотреть файл

@ -0,0 +1,72 @@
#include "ksbit.h"
//% indexerGet=BufferMethods::getByte indexerSet=BufferMethods::setByte
namespace BufferMethods {
//%
int getByte(Buffer buf, int off) {
return max(ManagedBuffer(buf).getByte(off), 0);
}
//%
void setByte(Buffer buf, int off, int v) {
ManagedBuffer(buf).setByte(off, v);
}
/** Returns the length of a Buffer object. */
//% property
int length(Buffer s) {
return s->length;
}
/**
* Fill (a fragment) of the buffer with given value.
*/
//%
void fill(Buffer buf, int value, int offset = 0, int length = -1)
{
ManagedBuffer(buf).fill(value, offset, length);
}
/**
* Return a copy of a fragment of a buffer.
*/
//%
Buffer slice(Buffer buf, int offset = 0, int length = -1)
{
return ManagedBuffer(buf).slice(offset, length).leakData();
}
/**
* Shift buffer left in place, with zero padding.
* @param offset number of bytes to shift; use negative value to shift right
*/
//%
void shift(Buffer buf, int offset)
{
ManagedBuffer(buf).shift(offset);
}
/**
* Rotate buffer left in place.
* @param offset number of bytes to shift; use negative value to shift right
*/
//%
void rotate(Buffer buf, int offset)
{
ManagedBuffer(buf).rotate(offset);
}
// int readBytes(uint8_t *dst, int offset, int length, bool swapBytes = false) const;
// int writeBytes(int dstOffset, uint8_t *src, int length, bool swapBytes = false);
/**
* Write contents of `src` at `dstOffset` in current buffer.
*/
//%
void write(Buffer buf, int dstOffset, Buffer src)
{
//Not supported, we only do up to 4 args :/
//void write(Buffer buf, int dstOffset, Buffer src, int srcOffset = 0, int length = -1)
ManagedBuffer(buf).writeBuffer(dstOffset, ManagedBuffer(src), 0, -1);
}
}

Просмотреть файл

@ -324,7 +324,7 @@ namespace ksrt {
RefAction *stclo(RefAction *a, int idx, uint32_t v)
{
//DBG("STCLO "); a->print(); DBG("@%d = %p\n", idx, (void*)v);
a->st(idx, v);
a->stCore(idx, v);
return a;
}

5
libs/microbit/core.d.ts поставляемый
Просмотреть файл

@ -82,6 +82,10 @@ interface String {
}
interface Buffer {
[index: number]: number;
}
/**
* Converts A string to an integer.
* @param s A string to convert into a number.
@ -93,4 +97,3 @@ interface Object {}
interface Function {}
interface IArguments {}
interface RegExp {}

Просмотреть файл

@ -23,7 +23,8 @@
"music.ts",
"pins.cpp",
"serial.cpp",
"serial.ts"
"serial.ts",
"buffer.cpp"
],
"public": true,
"dependencies": {},
@ -88,7 +89,7 @@
"autoRun": true
},
"compileService": {
"gittag": "v0.1.2",
"gittag": "v0.1.5",
"serviceId": "ws"
},
"serial": {

Просмотреть файл

@ -1,6 +1,8 @@
#include "kindscript.h"
#include "ManagedBuffer.h"
using namespace kindscript;
MicroBitPin *getPin(int id);
typedef ImageData* Image;
typedef BufferData* Buffer;

Просмотреть файл

@ -179,6 +179,16 @@ namespace pins {
}
}
/**
* Create a new zero-initialized buffer.
* @param size number of bytes in the buffer
*/
//%
Buffer createBuffer(int size)
{
return ManagedBuffer(size).leakData();
}
// TODO:
void i2cReadBuffer(int address, RefBuffer *buf)
{

50
libs/microbit/shims.d.ts поставляемый
Просмотреть файл

@ -374,7 +374,7 @@ declare namespace control {
* @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
//% mode.defl=1 shim=control::raiseEvent
//% mode.defl=1 shim=control::raiseEvent
function raiseEvent(src: number, value: number, mode: EventCreationMode): void;
/**
@ -534,6 +534,13 @@ declare namespace pins {
*/
//% help=pins/analog-pitch weight=14 async shim=pins::analogPitch
function analogPitch(frequency: number, ms: number): void;
/**
* Create a new zero-initialized buffer.
* @param size number of bytes in the buffer
*/
//% shim=pins::createBuffer
function createBuffer(size: number): Buffer;
}
@ -566,4 +573,45 @@ declare namespace serial {
function readScreen(): void;
}
//% indexerGet=BufferMethods::getByte indexerSet=BufferMethods::setByte
declare interface Buffer {
/** Returns the length of a Buffer object. */
//% property shim=BufferMethods::length
length: number;
/**
* Fill (a fragment) of the buffer with given value.
*/
//% offset.defl=0 length.defl=-1 shim=BufferMethods::fill
fill(value: number, offset?: number, length?: number): void;
/**
* Return a copy of a fragment of a buffer.
*/
//% offset.defl=0 length.defl=-1 shim=BufferMethods::slice
slice(offset?: number, length?: number): Buffer;
/**
* Shift buffer left in place, with zero padding.
* @param offset number of bytes to shift; use negative value to shift right
*/
//% shim=BufferMethods::shift
shift(offset: number): void;
/**
* Rotate buffer left in place.
* @param offset number of bytes to shift; use negative value to shift right
*/
//% shim=BufferMethods::rotate
rotate(offset: number): void;
/**
* Write contents of `src` at `dstOffset` in current buffer.
*/
//% shim=BufferMethods::write
write(dstOffset: number, src: Buffer): void;
}
// Auto-generated. Do not edit. Really.

Просмотреть файл

@ -527,4 +527,4 @@ namespace ks.rt.ImageMethods {
}
// TODO ...
}
}