Skip to content

Commit

Permalink
updated sensorData() for m:b V2
Browse files Browse the repository at this point in the history
  • Loading branch information
Joy-IT committed Jan 26, 2021
1 parent 19fb138 commit d3f3371
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions joycar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,20 +540,22 @@ 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
function scale (num: number, in_min: number, in_max: number, out_min: number, out_max: number) {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

}
}

0 comments on commit d3f3371

Please sign in to comment.