simulation of shake

This commit is contained in:
Peli de Halleux 2016-03-18 14:54:27 -07:00
parent 51e025507a
commit d7466797c4
4 changed files with 58 additions and 15 deletions

View File

@ -77,5 +77,6 @@ namespace ks.rt.micro_bit {
MES_BROADCAST_GENERAL_ID: number; MES_BROADCAST_GENERAL_ID: number;
MICROBIT_ID_RADIO: number; MICROBIT_ID_RADIO: number;
MICROBIT_RADIO_EVT_DATAGRAM: number; MICROBIT_RADIO_EVT_DATAGRAM: number;
MICROBIT_ID_GESTURE: number;
} }
} }

View File

@ -216,24 +216,35 @@ namespace ks.rt.micro_bit {
let b = board(); let b = board();
if (button == ens.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) { if (button == ens.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) {
b.usesButtonAB = true; b.usesButtonAB = true;
b.updateView(); runtime.queueDisplayUpdate();
} }
b.bus.listen(button, ens.MICROBIT_BUTTON_EVT_CLICK, handler); b.bus.listen(button, ens.MICROBIT_BUTTON_EVT_CLICK, handler);
} }
export function isButtonPressed(button: number): boolean { export function isButtonPressed(button: number): boolean {
var ens = enums(); let ens = enums();
let b = board(); let b = board();
if (button == ens.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) { if (button == ens.MICROBIT_ID_BUTTON_AB && !board().usesButtonAB) {
b.usesButtonAB = true; b.usesButtonAB = true;
b.updateView(); runtime.queueDisplayUpdate();
} }
var bts = b.buttons; let bts = b.buttons;
if (button == ens.MICROBIT_ID_BUTTON_A) return bts[0].pressed; if (button == ens.MICROBIT_ID_BUTTON_A) return bts[0].pressed;
if (button == ens.MICROBIT_ID_BUTTON_B) return bts[1].pressed; if (button == ens.MICROBIT_ID_BUTTON_B) return bts[1].pressed;
return bts[2].pressed || (bts[0].pressed && bts[1].pressed); return bts[2].pressed || (bts[0].pressed && bts[1].pressed);
} }
export function onGesture(gesture: number, handler: RefAction) {
let ens = enums();
let b = board();
if (gesture == 11 && !b.useShake) { // SAKE
b.useShake = true;
runtime.queueDisplayUpdate();
}
b.bus.listen(ens.MICROBIT_ID_GESTURE, gesture, handler);
}
export function onPinPressed(pin: Pin, handler: RefAction) { export function onPinPressed(pin: Pin, handler: RefAction) {
pin.isTouched(); pin.isTouched();
onButtonPressed(pin.id, handler); onButtonPressed(pin.id, handler);
@ -267,7 +278,7 @@ namespace ks.rt.micro_bit {
var b = board(); var b = board();
if (!b.usesHeading) { if (!b.usesHeading) {
b.usesHeading = true; b.usesHeading = true;
b.updateView(); runtime.queueDisplayUpdate();
} }
return b.heading; return b.heading;
} }
@ -276,7 +287,7 @@ namespace ks.rt.micro_bit {
var b = board(); var b = board();
if (!b.usesTemperature) { if (!b.usesTemperature) {
b.usesTemperature = true; b.usesTemperature = true;
b.updateView(); runtime.queueDisplayUpdate();
} }
return b.temperature; return b.temperature;
} }
@ -285,7 +296,7 @@ namespace ks.rt.micro_bit {
let b = board(); let b = board();
if (!b.usesAcceleration) { if (!b.usesAcceleration) {
b.usesAcceleration = true; b.usesAcceleration = true;
b.updateView(); runtime.queueDisplayUpdate();
} }
let acc = b.acceleration; let acc = b.acceleration;
switch (dimension) { switch (dimension) {
@ -299,14 +310,14 @@ namespace ks.rt.micro_bit {
export function setAccelerometerRange(range : number) { export function setAccelerometerRange(range : number) {
let b = board(); let b = board();
b.accelerometerRange = Math.max(1, Math.min(8, range)); b.accelerometerRange = Math.max(1, Math.min(8, range));
b.updateView(); runtime.queueDisplayUpdate();
} }
export function lightLevel(): number { export function lightLevel(): number {
let b = board(); let b = board();
if (!b.usesLightLevel) { if (!b.usesLightLevel) {
b.usesLightLevel = true; b.usesLightLevel = true;
b.updateView(); runtime.queueDisplayUpdate();
} }
return b.lightLevel; return b.lightLevel;
} }
@ -329,7 +340,7 @@ namespace ks.rt.micro_bit {
export function digitalWritePin(pin : Pin, value: number) { export function digitalWritePin(pin : Pin, value: number) {
pin.mode = PinMode.Digital | PinMode.Output; pin.mode = PinMode.Digital | PinMode.Output;
pin.value = value > 0 ? 1023 : 0; pin.value = value > 0 ? 1023 : 0;
board().updateView(); runtime.queueDisplayUpdate();
} }
export function analogReadPin(pin : Pin) : number { export function analogReadPin(pin : Pin) : number {
@ -340,13 +351,13 @@ namespace ks.rt.micro_bit {
export function analogWritePin(pin : Pin, value: number) { export function analogWritePin(pin : Pin, value: number) {
pin.mode = PinMode.Analog | PinMode.Output; pin.mode = PinMode.Analog | PinMode.Output;
pin.value = value ? 1 : 0; pin.value = value ? 1 : 0;
board().updateView(); runtime.queueDisplayUpdate();
} }
export function setAnalogPeriodUs(pin: Pin, micros:number) { export function setAnalogPeriodUs(pin: Pin, micros:number) {
pin.mode = PinMode.Analog | PinMode.Output; pin.mode = PinMode.Analog | PinMode.Output;
pin.period = micros; pin.period = micros;
board().updateView(); runtime.queueDisplayUpdate();
} }
export function servoWritePin(pin: Pin, value: number) { export function servoWritePin(pin: Pin, value: number) {
@ -425,7 +436,7 @@ namespace ks.rt.micro_bit {
pin.value = 512; pin.value = 512;
pin.period = 1000000/frequency; pin.period = 1000000/frequency;
} }
board().updateView(); runtime.queueDisplayUpdate();
let cb = getResume(); let cb = getResume();
AudioContextManager.tone(frequency, 1); AudioContextManager.tone(frequency, 1);
@ -436,7 +447,7 @@ namespace ks.rt.micro_bit {
pin.value = 0; pin.value = 0;
pin.period = 0; pin.period = 0;
pin.mode = PinMode.Unused; pin.mode = PinMode.Unused;
board().updateView(); runtime.queueDisplayUpdate();
cb() cb()
}, ms); }, ms);
} }

View File

@ -182,6 +182,8 @@ namespace ks.rt.micro_bit {
private thermometerGradient : SVGLinearGradientElement; private thermometerGradient : SVGLinearGradientElement;
private thermometer: SVGRectElement; private thermometer: SVGRectElement;
private thermometerText: SVGTextElement; private thermometerText: SVGTextElement;
private shakeButton: SVGCircleElement;
private shakeText: SVGTextElement;
public board: rt.micro_bit.Board; public board: rt.micro_bit.Board;
constructor(public props: IBoardProps) { constructor(public props: IBoardProps) {
@ -204,6 +206,7 @@ namespace ks.rt.micro_bit {
Svg.fill(this.buttonsOuter[2], theme.virtualButtonOuter); Svg.fill(this.buttonsOuter[2], theme.virtualButtonOuter);
Svg.fill(this.buttons[2], theme.virtualButtonUp); Svg.fill(this.buttons[2], theme.virtualButtonUp);
Svg.fills(this.logos, theme.accent); Svg.fills(this.logos, theme.accent);
if (this.shakeButton) Svg.fill(this.shakeButton, theme.virtualButtonUp);
this.pinGradients.forEach(lg => Svg.setGradientColors(lg, theme.pin, theme.pinActive)); this.pinGradients.forEach(lg => Svg.setGradientColors(lg, theme.pin, theme.pinActive));
Svg.setGradientColors(this.lightLevelGradient, theme.lightLevelOn, theme.lightLevelOff); Svg.setGradientColors(this.lightLevelGradient, theme.lightLevelOn, theme.lightLevelOff);
@ -231,7 +234,32 @@ namespace ks.rt.micro_bit {
this.updateHeading(); this.updateHeading();
this.updateLightLevel(); this.updateLightLevel();
this.updateTemperature(); this.updateTemperature();
this.updateButtonAB(); this.updateButtonAB();
this.updateGestures();
}
private updateGestures() {
let state = this.board;
if (state.useShake && !this.shakeButton) {
this.shakeButton = Svg.child(this.g, "circle", {cx:380, cy:100, r:16.5}) as SVGCircleElement;
Svg.fill(this.shakeButton, this.props.theme.virtualButtonUp)
this.shakeButton.addEventListener("mousedown", ev => {
let state = this.board;
Svg.fill(this.shakeButton, this.props.theme.buttonDown);
})
this.shakeButton.addEventListener("mouseleave", ev => {
let state = this.board;
Svg.fill(this.shakeButton, this.props.theme.virtualButtonUp);
})
this.shakeButton.addEventListener("mouseup", ev => {
let state = this.board;
Svg.fill(this.shakeButton, this.props.theme.virtualButtonUp);
let ens = enums();
this.board.bus.queue(ens.MICROBIT_ID_GESTURE, 11); // GESTURE_SHAKE
})
this.shakeText = Svg.child(this.g, "text", {x:400, y:110, class:'sim-text'}) as SVGTextElement;
this.shakeText.textContent = "SHAKE"
}
} }
private updateButtonAB() { private updateButtonAB() {

View File

@ -168,6 +168,9 @@ namespace ks.rt.micro_bit {
usesAcceleration = false; usesAcceleration = false;
acceleration = [0, 0, -1023]; acceleration = [0, 0, -1023];
accelerometerRange = 2; accelerometerRange = 2;
// gestures
useShake = false;
usesHeading = false; usesHeading = false;
heading = 90; heading = 90;