plot bar graph handling floating point values (#855)

* handling floating point values

* removelogging
This commit is contained in:
Peli de Halleux 2018-06-04 16:47:26 -07:00 committed by GitHub
parent 2492716f54
commit 4619dbdd89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View File

@ -19,19 +19,25 @@
//% blockId=device_plot_bar_graph block="plot bar graph of %value up to %high" icon="\uf080" blockExternalInputs=true //% blockId=device_plot_bar_graph block="plot bar graph of %value up to %high" icon="\uf080" blockExternalInputs=true
//% parts="ledmatrix" //% parts="ledmatrix"
export function plotBarGraph(value: number, high: number): void { export function plotBarGraph(value: number, high: number): void {
let now = input.runningTime(); const now = input.runningTime();
serial.writeLine(value.toString()); serial.writeLine(value.toString());
value = Math.abs(value); value = Math.abs(value);
if (high != 0) barGraphHigh = high; // auto-scale "high" is not provided
else if (value > barGraphHigh || now - barGraphHighLast > 10000) { if (high <= 0) {
barGraphHigh = high;
} else if (value > barGraphHigh || now - barGraphHighLast > 10000) {
barGraphHigh = value; barGraphHigh = value;
barGraphHighLast = now; barGraphHighLast = now;
} }
barGraphHigh = Math.max(barGraphHigh, 16); // normalize lack of data to 0..1
if (barGraphHigh < 16 * Number.EPSILON)
barGraphHigh = 1;
let v = (value * 15) / barGraphHigh; // normalize value to 0..1
const v = value / barGraphHigh;
const dv = 1 / 16;
let k = 0; let k = 0;
for (let y = 4; y >= 0; --y) { for (let y = 4; y >= 0; --y) {
for (let x = 0; x < 3; ++x) { for (let x = 0; x < 3; ++x) {
@ -42,7 +48,7 @@
plot(2 - x, y); plot(2 - x, y);
plot(2 + x, y); plot(2 + x, y);
} }
++k; k += dv;
} }
} }
} }

16
libs/core/math.ts Normal file
View File

@ -0,0 +1,16 @@
namespace Math {
export const E = 2.718281828459045;
export const LN2 = 0.6931471805599453;
export const LN10 = 2.302585092994046;
export const LOG2E = 1.4426950408889634;
export const LOG10E = 0.4342944819032518;
export const PI = 3.141592653589793;
export const SQRT1_2 = 0.7071067811865476;
export const SQRT2 = 1.4142135623730951;
}
namespace Number {
export const EPSILON = 2.220446049250313e-16;
}

View File

@ -8,6 +8,7 @@
"pxt.h", "pxt.h",
"pxtbase.h", "pxtbase.h",
"pxtcore.h", "pxtcore.h",
"math.ts",
"dal.d.ts", "dal.d.ts",
"enums.d.ts", "enums.d.ts",
"shims.d.ts", "shims.d.ts",