plot bar graph handling floating point values (#855)
* handling floating point values * removelogging
This commit is contained in:
parent
2492716f54
commit
4619dbdd89
@ -19,19 +19,25 @@
|
||||
//% blockId=device_plot_bar_graph block="plot bar graph of %value up to %high" icon="\uf080" blockExternalInputs=true
|
||||
//% parts="ledmatrix"
|
||||
export function plotBarGraph(value: number, high: number): void {
|
||||
let now = input.runningTime();
|
||||
const now = input.runningTime();
|
||||
serial.writeLine(value.toString());
|
||||
value = Math.abs(value);
|
||||
|
||||
if (high != 0) barGraphHigh = high;
|
||||
else if (value > barGraphHigh || now - barGraphHighLast > 10000) {
|
||||
// auto-scale "high" is not provided
|
||||
if (high <= 0) {
|
||||
barGraphHigh = high;
|
||||
} else if (value > barGraphHigh || now - barGraphHighLast > 10000) {
|
||||
barGraphHigh = value;
|
||||
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;
|
||||
for (let y = 4; y >= 0; --y) {
|
||||
for (let x = 0; x < 3; ++x) {
|
||||
@ -42,7 +48,7 @@
|
||||
plot(2 - x, y);
|
||||
plot(2 + x, y);
|
||||
}
|
||||
++k;
|
||||
k += dv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
libs/core/math.ts
Normal file
16
libs/core/math.ts
Normal 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;
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
"pxt.h",
|
||||
"pxtbase.h",
|
||||
"pxtcore.h",
|
||||
"math.ts",
|
||||
"dal.d.ts",
|
||||
"enums.d.ts",
|
||||
"shims.d.ts",
|
||||
|
Loading…
Reference in New Issue
Block a user