diff --git a/libs/core/led.ts b/libs/core/led.ts index 10fde262..10f308bf 100644 --- a/libs/core/led.ts +++ b/libs/core/led.ts @@ -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; } } } diff --git a/libs/core/math.ts b/libs/core/math.ts new file mode 100644 index 00000000..ad36784f --- /dev/null +++ b/libs/core/math.ts @@ -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; +} \ No newline at end of file diff --git a/libs/core/pxt.json b/libs/core/pxt.json index e9970043..bb091a19 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -8,6 +8,7 @@ "pxt.h", "pxtbase.h", "pxtcore.h", + "math.ts", "dal.d.ts", "enums.d.ts", "shims.d.ts",