emulation of temperature

This commit is contained in:
Peli de Halleux 2016-03-14 14:03:31 -07:00
parent 7f57eda606
commit 8277063e71
5 changed files with 66 additions and 7 deletions

View File

@ -108,7 +108,7 @@ namespace input {
* Gets the temperature in Celsius degrees (°C). * Gets the temperature in Celsius degrees (°C).
*/ */
//% weight=55 icon="\uf06d" //% weight=55 icon="\uf06d"
//% help=functions/temperature shim=uBit.thermometer.getTemperature //% help=functions/temperature shim=micro_bit::temperature
//% blockId=device_temperature block="temperature (°C)" blockGap=8 //% blockId=device_temperature block="temperature (°C)" blockGap=8
export function temperature(): number { export function temperature(): number {
return 0; return 0;

View File

@ -259,16 +259,25 @@ namespace ks.rt.micro_bit {
var b = board(); var b = board();
if (!b.usesHeading) { if (!b.usesHeading) {
b.usesHeading = true; b.usesHeading = true;
runtime.queueDisplayUpdate(); b.updateView();
} }
return b.heading; return b.heading;
} }
export function temperature(): number {
var b = board();
if (!b.usesTemperature) {
b.usesTemperature = true;
b.updateView();
}
return b.temperature;
}
export function getAcceleration(dimension: number): number { export function getAcceleration(dimension: number): number {
let b = board(); let b = board();
if (!b.usesAcceleration) { if (!b.usesAcceleration) {
b.usesAcceleration = true; b.usesAcceleration = true;
runtime.queueDisplayUpdate(); b.updateView();
} }
let acc = b.acceleration; let acc = b.acceleration;
switch (dimension) { switch (dimension) {
@ -283,7 +292,7 @@ namespace ks.rt.micro_bit {
let b = board(); let b = board();
if (!b.usesLightLevel) { if (!b.usesLightLevel) {
b.usesLightLevel = true; b.usesLightLevel = true;
runtime.queueDisplayUpdate(); b.updateView();
} }
return b.lightLevel; return b.lightLevel;
} }

View File

@ -58,6 +58,11 @@ svg.sim {
pointer-events: none; pointer-events: none;
} }
.sim-thermometer {
stroke:#aaa;
stroke-width: 3px;
}
/* animations */ /* animations */
.sim-flash { .sim-flash {
animation-name: sim-flash-animation; animation-name: sim-flash-animation;

View File

@ -165,6 +165,9 @@ namespace ks.rt.micro_bit {
private lightLevelButton: SVGCircleElement; private lightLevelButton: SVGCircleElement;
private lightLevelGradient : SVGLinearGradientElement; private lightLevelGradient : SVGLinearGradientElement;
private lightLevelText: SVGTextElement; private lightLevelText: SVGTextElement;
private thermometerGradient : SVGLinearGradientElement;
private thermometer: SVGRectElement;
private thermometerText: SVGTextElement;
public board: rt.micro_bit.Board; public board: rt.micro_bit.Board;
constructor(public props: IBoardProps) { constructor(public props: IBoardProps) {
@ -188,6 +191,8 @@ namespace ks.rt.micro_bit {
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);
Svg.setGradientColors(this.thermometerGradient, theme.ledOff, theme.ledOn);
} }
public updateState() { public updateState() {
@ -208,7 +213,9 @@ namespace ks.rt.micro_bit {
this.updatePins(); this.updatePins();
this.updateTilt(); this.updateTilt();
this.updateHeading(); this.updateHeading();
this.updateLightLevel(); this.updateLightLevel();
this.updateTemperature();
(<any>this.buttonsOuter[2]).style.visibility = state.usesButtonAB ? 'visible' : 'hidden'; (<any>this.buttonsOuter[2]).style.visibility = state.usesButtonAB ? 'visible' : 'hidden';
(<any>this.buttons[2]).style.visibility = state.usesButtonAB ? 'visible' : 'hidden'; (<any>this.buttons[2]).style.visibility = state.usesButtonAB ? 'visible' : 'hidden';
} }
@ -235,6 +242,43 @@ namespace ks.rt.micro_bit {
if (v) Svg.setGradientValue(this.pinGradients[index], v); if (v) Svg.setGradientValue(this.pinGradients[index], v);
} }
private updateTemperature() {
let state = this.board;
if (!state || !state.usesTemperature) return;
let tmin = -5;
let tmax = 50;
if (!this.thermometer) {
let gid = "gradient-thermometer";
this.thermometerGradient = Svg.linearGradient(this.defs, gid);
this.thermometer = <SVGRectElement> Svg.child(this.g, "rect", {
class: "sim-thermometer",
x:120,
y:110,
width:20,
height:160,
rx:5, ry:5,
fill:`url(#${gid})`
});
this.thermometerText = Svg.child(this.g, "text", { class:'sim-text', x:75, y:130}) as SVGTextElement;
this.updateTheme();
let pt = this.element.createSVGPoint();
Svg.buttonEvents(this.thermometer,
(ev) => {
let cur = Svg.cursorPoint(pt, this.element, ev);
let t = Math.max(0, Math.min(1, (260 - cur.y) / 140))
state.temperature = Math.floor(tmin + t * (tmax-tmin));
this.updateTemperature();
}, ev => {}, ev => {})
}
let t = Math.max(tmin, Math.min(tmax, state.temperature))
let per = Math.floor((state.temperature - tmin) / (tmax-tmin)*100)
Svg.setGradientValue(this.thermometerGradient, 100 - per + '%');
this.thermometerText.textContent = t + '°';
}
private updateHeading() { private updateHeading() {
let xc = 258; let xc = 258;
let yc = 75; let yc = 75;
@ -303,7 +347,7 @@ namespace ks.rt.micro_bit {
if (!this.lightLevelButton) { if (!this.lightLevelButton) {
let gid= "gradient-light-level"; let gid= "gradient-light-level";
this.lightLevelGradient = Svg.linearGradient(this.defs, "gradient-light-level") this.lightLevelGradient = Svg.linearGradient(this.defs, gid)
let cy = 50; let cy = 50;
let r = 35; let r = 35;
this.lightLevelButton = Svg.child(this.g, "circle", { this.lightLevelButton = Svg.child(this.g, "circle", {

View File

@ -164,6 +164,7 @@ namespace ks.rt.micro_bit {
usesHeading = false; usesHeading = false;
heading = 90; heading = 90;
usesTemperature = false;
temperature = 21; temperature = 21;
usesLightLevel = false; usesLightLevel = false;