pxt-calliope/sim/state.ts

721 lines
27 KiB
TypeScript
Raw Normal View History

2016-03-11 01:24:11 +01:00
namespace ks.rt.micro_bit {
export interface RuntimeOptions {
theme: string;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export enum DisplayMode {
bw,
greyscale
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export enum PinMode {
2016-03-14 05:24:11 +01:00
Unused = 0,
2016-03-11 01:24:11 +01:00
Digital = 0x0001,
2016-03-14 05:24:11 +01:00
Analog = 0x0002,
Input = 0x0004,
Output = 0x0008,
Touch = 0x0010
2016-03-11 01:24:11 +01:00
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export class Pin {
2016-03-14 05:24:11 +01:00
constructor(public id: number) { }
2016-03-11 01:24:11 +01:00
touched = false;
value = 0;
2016-03-14 16:32:02 +01:00
period = 0;
2016-03-12 01:51:35 +01:00
mode = PinMode.Unused;
pitch = false;
2016-03-14 05:24:11 +01:00
isTouched(): boolean {
2016-03-11 01:24:11 +01:00
this.mode = PinMode.Touch;
return this.touched;
}
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export class Button {
2016-03-14 05:24:11 +01:00
constructor(public id: number) { }
2016-03-11 01:24:11 +01:00
pressed: boolean;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export class EventBus {
2016-03-14 05:24:11 +01:00
private queues: Map<EventQueue<number>> = {};
constructor(private runtime: Runtime) { }
listen(id: number, evid: number, handler: RefAction) {
let k = id + ':' + evid;
2016-03-11 01:24:11 +01:00
let queue = this.queues[k];
if (!queue) queue = this.queues[k] = new EventQueue<number>(this.runtime);
queue.handler = handler;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
queue(id: number, evid: number, value: number = 0) {
2016-03-14 05:24:11 +01:00
let k = id + ':' + evid;
2016-03-11 01:24:11 +01:00
let queue = this.queues[k];
if (queue) queue.push(value);
}
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export interface PacketBuffer {
2016-03-14 05:24:11 +01:00
data: number[];
rssi?: number;
2016-03-11 01:24:11 +01:00
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export class RadioDatagram {
datagram: PacketBuffer[] = [];
2016-03-14 05:24:11 +01:00
lastReceived: PacketBuffer = {
data: [0, 0, 0, 0],
rssi: -1
2016-03-11 01:24:11 +01:00
};
2016-03-14 05:24:11 +01:00
constructor(private runtime: Runtime) {
2016-03-11 01:24:11 +01:00
}
2016-03-14 05:24:11 +01:00
queue(packet: PacketBuffer) {
2016-03-11 01:24:11 +01:00
if (this.datagram.length < 5) {
this.datagram.push(packet);
let ens = enums();
(<Board>runtime.board).bus.queue(ens.MICROBIT_ID_RADIO, ens.MICROBIT_RADIO_EVT_DATAGRAM);
}
}
2016-03-14 05:24:11 +01:00
send(buffer: number[]) {
2016-03-11 01:24:11 +01:00
Runtime.postMessage(<SimulatorRadioPacketMessage>{
2016-03-14 05:24:11 +01:00
type: 'radiopacket',
2016-03-11 01:24:11 +01:00
data: buffer.slice(0, 8)
})
}
2016-03-14 05:24:11 +01:00
recv(): PacketBuffer {
2016-03-11 01:24:11 +01:00
var r = this.datagram.shift();
2016-03-14 05:24:11 +01:00
if (!r) r = {
data: [0, 0, 0, 0],
rssi: -1
2016-03-11 01:24:11 +01:00
};
return this.lastReceived = r;
}
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export class RadioBus {
// uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP;
groupId = 0; // todo
2016-03-15 21:05:11 +01:00
power = 0;
2016-03-14 05:24:11 +01:00
datagram: RadioDatagram;
constructor(private runtime: Runtime) {
2016-03-11 01:24:11 +01:00
this.datagram = new RadioDatagram(runtime);
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
setGroup(id: number) {
this.groupId = id & 0xff; // byte only
}
2016-03-20 03:15:20 +01:00
2016-03-15 21:05:11 +01:00
setTransmitPower(power: number) {
this.power = Math.max(0, Math.min(7, power));
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
broadcast(msg: number) {
let ens = enums();
Runtime.postMessage(<SimulatorEventBusMessage>{
2016-03-14 05:24:11 +01:00
type: 'eventbus',
2016-03-11 01:24:11 +01:00
id: ens.MES_BROADCAST_GENERAL_ID,
2016-03-15 21:05:11 +01:00
eventid: msg,
power: this.power,
group: this.groupId
2016-03-11 01:24:11 +01:00
})
}
}
2016-03-14 05:24:11 +01:00
2016-03-20 03:15:20 +01:00
export enum BasicGesture {
GESTURE_NONE,
GESTURE_UP,
GESTURE_DOWN,
GESTURE_LEFT,
GESTURE_RIGHT,
GESTURE_FACE_UP,
GESTURE_FACE_DOWN,
GESTURE_FREEFALL,
GESTURE_3G,
GESTURE_6G,
GESTURE_8G,
GESTURE_SHAKE
};
interface AccelerometerSample {
x: number;
y: number;
z: number;
}
interface ShakeHistory {
x: boolean;
y: boolean;
z: boolean;
count: number;
shaken: number;
timer: number;
}
/**
* Co-ordinate systems that can be used.
* RAW: Unaltered data. Data will be returned directly from the accelerometer.
*
* SIMPLE_CARTESIAN: Data will be returned based on an easy to understand alignment, consistent with the cartesian system taught in schools.
* When held upright, facing the user:
*
* /
* +--------------------+ z
* | |
* | ..... |
* | * ..... * |
* ^ | ..... |
* | | |
* y +--------------------+ x-->
*
*
* NORTH_EAST_DOWN: Data will be returned based on the industry convention of the North East Down (NED) system.
* When held upright, facing the user:
*
* z
* +--------------------+ /
* | |
* | ..... |
* | * ..... * |
* ^ | ..... |
* | | |
* x +--------------------+ y-->
*
*/
export enum MicroBitCoordinateSystem {
RAW,
SIMPLE_CARTESIAN,
NORTH_EAST_DOWN
}
export class Accelerometer {
private sigma: number = 0; // the number of ticks that the instantaneous gesture has been stable.
private lastGesture: BasicGesture = BasicGesture.GESTURE_NONE; // the last, stable gesture recorded.
private currentGesture: BasicGesture = BasicGesture.GESTURE_NONE; // the instantaneous, unfiltered gesture detected.
private sample: AccelerometerSample = { x: 0, y: 0, z: -1023 }
private shake: ShakeHistory = { x: false, y: false, z: false, count: 0, shaken: 0, timer: 0 }; // State information needed to detect shake events.
private pitch:number;
private roll:number;
private id: number;
public isActive = false;
public sampleRange = 2;
constructor(public runtime: Runtime) {
this.id = (<Enums><any>runtime.enums).MICROBIT_ID_ACCELEROMETER;
}
public setSampleRange(range : number) {
this.activate();
this.sampleRange = Math.max(1, Math.min(8, range));
}
public activate() {
if (!this.isActive) {
this.isActive = true;
this.runtime.queueDisplayUpdate();
}
}
/**
* Reads the acceleration data from the accelerometer, and stores it in our buffer.
* This is called by the tick() member function, if the interrupt is set!
*/
public update(x : number, y : number, z : number) {
// read MSB values...
this.sample.x = Math.floor(x);
this.sample.y = Math.floor(y);
this.sample.z = Math.floor(z);
// Update gesture tracking
this.updateGesture();
// Indicate that a new sample is available
board().bus.queue(this.id, enums().MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE)
}
public instantaneousAccelerationSquared() {
// Use pythagoras theorem to determine the combined force acting on the device.
return this.sample.x * this.sample.x + this.sample.y * this.sample.y + this.sample.z * this.sample.z;
}
/**
* Service function. Determines the best guess posture of the device based on instantaneous data.
* This makes no use of historic data (except for shake), and forms this input to the filter implemented in updateGesture().
*
* @return A best guess of the current posture of the device, based on instantaneous data.
*/
private instantaneousPosture(): BasicGesture {
let ens = enums()
let force = this.instantaneousAccelerationSquared();
let shakeDetected = false;
// Test for shake events.
// We detect a shake by measuring zero crossings in each axis. In other words, if we see a strong acceleration to the left followed by
// a string acceleration to the right, then we can infer a shake. Similarly, we can do this for each acxis (left/right, up/down, in/out).
//
// If we see enough zero crossings in succession (MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD), then we decide that the device
// has been shaken.
if ((this.getX() < -ens.MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && this.shake.x) || (this.getX() > ens.MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !this.shake.x)) {
shakeDetected = true;
this.shake.x = !this.shake.x;
}
if ((this.getY() < -ens.MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && this.shake.y) || (this.getY() > ens.MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !this.shake.y)) {
shakeDetected = true;
this.shake.y = !this.shake.y;
}
if ((this.getZ() < -ens.MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && this.shake.z) || (this.getZ() > ens.MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !this.shake.z)) {
shakeDetected = true;
this.shake.z = !this.shake.z;
}
if (shakeDetected && this.shake.count < ens.MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD && ++this.shake.count == ens.MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD)
this.shake.shaken = 1;
if (++this.shake.timer >= ens.MICROBIT_ACCELEROMETER_SHAKE_DAMPING) {
this.shake.timer = 0;
if (this.shake.count > 0) {
if (--this.shake.count == 0)
this.shake.shaken = 0;
}
}
if (this.shake.shaken)
return BasicGesture.GESTURE_SHAKE;
if (force < ens.MICROBIT_ACCELEROMETER_FREEFALL_THRESHOLD)
return BasicGesture.GESTURE_FREEFALL;
if (force > ens.MICROBIT_ACCELEROMETER_3G_THRESHOLD)
return BasicGesture.GESTURE_3G;
if (force > ens.MICROBIT_ACCELEROMETER_6G_THRESHOLD)
return BasicGesture.GESTURE_6G;
if (force > ens.MICROBIT_ACCELEROMETER_8G_THRESHOLD)
return BasicGesture.GESTURE_8G;
// Determine our posture.
if (this.getX() < (-1000 + ens.MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return BasicGesture.GESTURE_LEFT;
if (this.getX() > (1000 - ens.MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return BasicGesture.GESTURE_RIGHT;
if (this.getY() < (-1000 + ens.MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return BasicGesture.GESTURE_DOWN;
if (this.getY() > (1000 - ens.MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return BasicGesture.GESTURE_UP;
if (this.getZ() < (-1000 + ens.MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return BasicGesture.GESTURE_FACE_UP;
if (this.getZ() > (1000 - ens.MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return BasicGesture.GESTURE_FACE_DOWN;
return BasicGesture.GESTURE_NONE;
}
updateGesture() {
let ens = enums()
// Determine what it looks like we're doing based on the latest sample...
let g = this.instantaneousPosture();
// Perform some low pass filtering to reduce jitter from any detected effects
if (g == this.currentGesture) {
if (this.sigma < ens.MICROBIT_ACCELEROMETER_GESTURE_DAMPING)
this.sigma++;
}
else {
this.currentGesture = g;
this.sigma = 0;
}
// If we've reached threshold, update our record and raise the relevant event...
if (this.currentGesture != this.lastGesture && this.sigma >= ens.MICROBIT_ACCELEROMETER_GESTURE_DAMPING) {
this.lastGesture = this.currentGesture;
board().bus.queue(ens.MICROBIT_ID_GESTURE, this.lastGesture);
}
}
/**
* Reads the X axis value of the latest update from the accelerometer.
* @param system The coordinate system to use. By default, a simple cartesian system is provided.
* @return The force measured in the X axis, in milli-g.
*
* Example:
* @code
* uBit.accelerometer.getX();
* uBit.accelerometer.getX(RAW);
* @endcode
*/
public getX(system : MicroBitCoordinateSystem = MicroBitCoordinateSystem.SIMPLE_CARTESIAN): number {
this.activate();
switch (system) {
case MicroBitCoordinateSystem.SIMPLE_CARTESIAN:
return -this.sample.x;
case MicroBitCoordinateSystem.NORTH_EAST_DOWN:
return this.sample.y;
//case MicroBitCoordinateSystem.SIMPLE_CARTESIAN.RAW:
default:
return this.sample.x;
}
}
/**
* Reads the Y axis value of the latest update from the accelerometer.
* @param system The coordinate system to use. By default, a simple cartesian system is provided.
* @return The force measured in the Y axis, in milli-g.
*
* Example:
* @code
* uBit.accelerometer.getY();
* uBit.accelerometer.getY(RAW);
* @endcode
*/
public getY(system : MicroBitCoordinateSystem = MicroBitCoordinateSystem.SIMPLE_CARTESIAN): number {
this.activate();
switch (system) {
case MicroBitCoordinateSystem.SIMPLE_CARTESIAN:
return -this.sample.y;
case MicroBitCoordinateSystem.NORTH_EAST_DOWN:
return -this.sample.x;
//case RAW:
default:
return this.sample.y;
}
}
/**
* Reads the Z axis value of the latest update from the accelerometer.
* @param system The coordinate system to use. By default, a simple cartesian system is provided.
* @return The force measured in the Z axis, in milli-g.
*
* Example:
* @code
* uBit.accelerometer.getZ();
* uBit.accelerometer.getZ(RAW);
* @endcode
*/
public getZ(system : MicroBitCoordinateSystem = MicroBitCoordinateSystem.SIMPLE_CARTESIAN): number {
this.activate();
switch (system) {
case MicroBitCoordinateSystem.NORTH_EAST_DOWN:
return -this.sample.z;
//case MicroBitCoordinateSystem.SIMPLE_CARTESIAN:
//case MicroBitCoordinateSystem.RAW:
default:
return this.sample.z;
}
}
/**
* Provides a rotation compensated pitch of the device, based on the latest update from the accelerometer.
* @return The pitch of the device, in degrees.
*
* Example:
* @code
* uBit.accelerometer.getPitch();
* @endcode
*/
public getPitch(): number {
this.activate();
return Math.floor((360 * this.getPitchRadians()) / (2 * Math.PI));
}
getPitchRadians() : number {
this.recalculatePitchRoll();
return this.pitch;
}
/**
* Provides a rotation compensated roll of the device, based on the latest update from the accelerometer.
* @return The roll of the device, in degrees.
*
* Example:
* @code
* uBit.accelerometer.getRoll();
* @endcode
*/
public getRoll(): number {
this.activate();
return Math.floor((360 * this.getRollRadians()) / (2 * Math.PI));
}
getRollRadians(): number {
this.recalculatePitchRoll();
return this.roll;
}
/**
* Recalculate roll and pitch values for the current sample.
* We only do this at most once per sample, as the necessary trigonemteric functions are rather
* heavyweight for a CPU without a floating point unit...
*/
recalculatePitchRoll() {
let x = this.getX(MicroBitCoordinateSystem.NORTH_EAST_DOWN);
let y = this.getY(MicroBitCoordinateSystem.NORTH_EAST_DOWN);
let z = this.getZ(MicroBitCoordinateSystem.NORTH_EAST_DOWN);
this.roll = Math.atan2(y, z);
this.pitch = Math.atan(-x / (y * Math.sin(this.roll) + z * Math.cos(this.roll)));
}
}
2016-03-11 01:24:11 +01:00
export interface SimulatorEventBusMessage extends SimulatorMessage {
id: number;
eventid: number;
value?: number;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export interface SimulatorSerialMessage extends SimulatorMessage {
2016-03-14 05:24:11 +01:00
id: string;
2016-03-11 01:24:11 +01:00
data: string;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export interface SimulatorRadioPacketMessage extends SimulatorMessage {
data: number[];
rssi?: number;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
export class Board extends BaseBoard {
id: string;
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
// the bus
2016-03-14 05:24:11 +01:00
bus: EventBus;
2016-03-11 01:24:11 +01:00
radio: RadioBus;
// display
image = createImage(5);
brigthness = 255;
displayMode = DisplayMode.bw;
font: Image = createFont();
// buttons
usesButtonAB: boolean = false;
2016-03-14 05:24:11 +01:00
buttons: Button[];
2016-03-11 01:24:11 +01:00
// pins
2016-03-14 05:24:11 +01:00
pins: Pin[];
2016-03-11 01:24:11 +01:00
// serial
serialIn: string[] = [];
2016-03-20 03:15:20 +01:00
// sensors
accelerometer : Accelerometer;
2016-03-18 22:54:27 +01:00
// gestures
useShake = false;
2016-03-14 05:24:11 +01:00
usesHeading = false;
2016-03-11 01:24:11 +01:00
heading = 90;
2016-03-14 05:24:11 +01:00
2016-03-14 22:03:31 +01:00
usesTemperature = false;
2016-03-11 01:24:11 +01:00
temperature = 21;
2016-03-14 05:24:11 +01:00
2016-03-11 22:17:49 +01:00
usesLightLevel = false;
2016-03-11 01:24:11 +01:00
lightLevel = 128;
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
animationQ: AnimationQueue;
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
constructor() {
super()
this.id = "b" + Math.random();
this.animationQ = new AnimationQueue(runtime);
this.bus = new EventBus(runtime);
this.radio = new RadioBus(runtime);
2016-03-20 03:15:20 +01:00
this.accelerometer = new Accelerometer(runtime);
2016-03-11 01:24:11 +01:00
let ens = enums();
this.buttons = [
new Button(ens.MICROBIT_ID_BUTTON_A),
new Button(ens.MICROBIT_ID_BUTTON_B),
new Button(ens.MICROBIT_ID_BUTTON_AB)
];
this.pins = [
new Pin(ens.MICROBIT_ID_IO_P0),
new Pin(ens.MICROBIT_ID_IO_P1),
new Pin(ens.MICROBIT_ID_IO_P2),
new Pin(ens.MICROBIT_ID_IO_P3),
new Pin(ens.MICROBIT_ID_IO_P4),
new Pin(ens.MICROBIT_ID_IO_P5),
new Pin(ens.MICROBIT_ID_IO_P6),
new Pin(ens.MICROBIT_ID_IO_P7),
new Pin(ens.MICROBIT_ID_IO_P8),
new Pin(ens.MICROBIT_ID_IO_P9),
new Pin(ens.MICROBIT_ID_IO_P10),
new Pin(ens.MICROBIT_ID_IO_P11),
new Pin(ens.MICROBIT_ID_IO_P12),
new Pin(ens.MICROBIT_ID_IO_P13),
new Pin(ens.MICROBIT_ID_IO_P14),
new Pin(ens.MICROBIT_ID_IO_P15),
new Pin(ens.MICROBIT_ID_IO_P16),
2016-03-14 05:24:11 +01:00
null,
null,
2016-03-11 01:24:11 +01:00
new Pin(ens.MICROBIT_ID_IO_P19),
new Pin(ens.MICROBIT_ID_IO_P20)
];
}
2016-03-14 05:24:11 +01:00
initAsync(msg: SimulatorRunMessage): Promise<void> {
2016-03-11 01:24:11 +01:00
let options = (msg.options || {}) as RuntimeOptions;
2016-03-14 05:24:11 +01:00
let theme: micro_bit.IBoardTheme;
switch (options.theme) {
2016-03-11 01:24:11 +01:00
case 'blue': theme = micro_bit.themes[0]; break;
case 'yellow': theme = micro_bit.themes[1]; break;
case 'green': theme = micro_bit.themes[2]; break;
case 'red': theme = micro_bit.themes[3]; break;
2016-03-14 05:24:11 +01:00
default: theme = ks.rt.micro_bit.randomTheme();
2016-03-11 01:24:11 +01:00
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
console.log('setting up microbit simulator')
let view = new ks.rt.micro_bit.MicrobitBoardSvg({
theme: theme,
runtime: runtime
})
document.body.innerHTML = ''; // clear children
document.body.appendChild(view.element);
2016-03-14 05:24:11 +01:00
return Promise.resolve();
2016-03-11 01:24:11 +01:00
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
receiveMessage(msg: SimulatorMessage) {
if (!runtime || runtime.dead) return;
2016-03-14 05:24:11 +01:00
switch (msg.type || "") {
case 'eventbus':
2016-03-11 01:24:11 +01:00
let ev = <SimulatorEventBusMessage>msg;
this.bus.queue(ev.id, ev.eventid, ev.value);
break;
2016-03-14 05:24:11 +01:00
case 'serial':
2016-03-11 01:24:11 +01:00
this.serialIn.push((<SimulatorSerialMessage>msg).data || '');
break;
case 'radiopacket':
let packet = <SimulatorRadioPacketMessage>msg;
2016-03-14 05:24:11 +01:00
this.radio.datagram.queue({ data: packet.data || [], rssi: packet.rssi || 0 })
2016-03-11 01:24:11 +01:00
break;
}
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
readSerial() {
let v = this.serialIn.shift() || '';
return v;
}
2016-03-14 05:24:11 +01:00
2016-03-11 01:24:11 +01:00
serialOutBuffer: string = '';
2016-03-14 05:24:11 +01:00
writeSerial(s: string) {
for (let i = 0; i < s.length; ++i) {
2016-03-11 01:24:11 +01:00
let c = s[i];
2016-03-14 05:24:11 +01:00
switch (c) {
case '\n':
2016-03-11 01:24:11 +01:00
Runtime.postMessage(<SimulatorSerialMessage>{
type: 'serial',
data: this.serialOutBuffer,
id: runtime.id
2016-03-14 05:24:11 +01:00
})
2016-03-11 01:24:11 +01:00
this.serialOutBuffer = ''
break;
case '\r': continue;
default: this.serialOutBuffer += c;
2016-03-14 05:24:11 +01:00
}
2016-03-11 01:24:11 +01:00
}
}
}
export class Image {
public width: number;
2016-03-14 05:24:11 +01:00
public data: number[];
2016-03-11 01:24:11 +01:00
constructor(width: number, data: number[]) {
this.width = width;
this.data = data;
}
public get(x: number, y: number): number {
// TODO range checking
return this.data[y * this.width + x];
}
public set(x: number, y: number, v: number) {
// TODO range checking
this.data[y * this.width + x] = v;
}
public copyTo(xSrcIndex: number, length: number, target: Image, xTargetIndex: number): void {
for (let x = 0; x < length; x++) {
for (let y = 0; y < 5; y++) {
let value = this.get(xSrcIndex + x, y);
target.set(xTargetIndex + x, y, value);
}
}
}
2016-03-15 05:37:03 +01:00
public shiftLeft(cols: number) {
2016-03-20 03:15:20 +01:00
for (let x = 0; x < this.width; ++x)
for (let y = 0; y < 5; ++y)
this.set(x, y, x < this.width - cols ? this.get(x + cols, y) : 0);
2016-03-15 05:37:03 +01:00
}
public shiftRight(cols: number) {
2016-03-20 03:15:20 +01:00
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);
2016-03-15 05:37:03 +01:00
}
2016-03-20 03:15:20 +01:00
2016-03-14 05:24:11 +01:00
public clear(): void {
for (var i = 0; i < this.data.length; ++i)
2016-03-11 01:24:11 +01:00
this.data[i] = 0;
}
}
export function createImage(width: number): Image {
return new Image(width, new Array(width * 5));
}
export function createImageFromBuffer(data: number[]): Image {
return new Image(data.length / 5, data);
}
export function createImageFromString(text: string): Image {
let font = board().font;
let w = font.width;
let sprite = createImage(6 * text.length - 1);
let k = 0;
for (let i = 0; i < text.length; i++) {
let charCode = text.charCodeAt(i);
let charStart = (charCode - 32) * 5;
if (charStart < 0 || charStart + 5 > w) {
charCode = " ".charCodeAt(0);
charStart = (charCode - 32) * 5;
}
font.copyTo(charStart, 5, sprite, k);
k = k + 5;
if (i < text.length - 1) {
k = k + 1;
}
}
return sprite;
}
export function createFont(): Image {
var data = [0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x0, 0x8, 0xa, 0x4a, 0x40, 0x0, 0x0, 0xa, 0x5f, 0xea, 0x5f, 0xea, 0xe, 0xd9, 0x2e, 0xd3, 0x6e, 0x19, 0x32, 0x44, 0x89, 0x33, 0x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x6, 0xc8, 0x4, 0x98, 0x8, 0x8, 0xe, 0xc8, 0x7, 0x0, 0x12, 0x52, 0x52, 0x4f, 0x0, 0x11, 0x31, 0x2a, 0x44, 0x0, 0x11, 0x31, 0x35, 0xbb, 0x0, 0x12, 0x4c, 0x8c, 0x92, 0x0, 0x11, 0x2a, 0x44, 0x98, 0x0, 0x1e, 0xc4, 0x88, 0x1e, 0x6, 0xc4, 0x8c, 0x84, 0x86, 0x8, 0x8, 0x8, 0x8, 0x8, 0x18, 0x8, 0xc, 0x88, 0x18, 0x0, 0x0, 0xc, 0x83, 0x60];
let nb = data.length;
let n = nb / 5;
var font = createImage(nb);
for (let c = 0; c < n; c++) {
for (let row = 0; row < 5; row++) {
let char = data[c * 5 + row];
for (let col = 0; col < 5; col++) {
if ((char & (1 << col)) != 0)
font.set((c * 5 + 4) - col, row, 255);
}
}
}
return font;
}
}