From d3f3371a958d101bc7e4623148d8ca01f36d3d70 Mon Sep 17 00:00:00 2001 From: Joy-IT Date: Tue, 26 Jan 2021 13:44:43 +0100 Subject: [PATCH] updated sensorData() for m:b V2 --- joycar.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/joycar.ts b/joycar.ts index 7421363..ee5392e 100644 --- a/joycar.ts +++ b/joycar.ts @@ -540,15 +540,17 @@ namespace JoyCar { } // Read from IO Expander - function sensorData(bit: number){ - let expander = pins.i2cReadBuffer(56, 1, true); - let bin = []; - while(expander[0] > 0) { - bin.push(expander[0] % 2); - expander[0] >>= 1; - } - - return !(!!bin[bit]); + function sensorData(channel: number){ + let expander = pins.i2cReadNumber(56, NumberFormat.UInt8LE, false); + let bin = "" + let bit = 1 + while (expander > 0) { + bin = bin + ((expander & bit) ? "1" : "0") + expander &= ~bit + bit *= 2 + } + while (bin.length < 8) bin = "0" + bin + return !parseInt(bin[channel]) } // Mapping function @@ -556,4 +558,4 @@ namespace JoyCar { return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } -} +} \ No newline at end of file