Skip to content

Commit

Permalink
javascript inadequacies: JS can only represent 54 bits of integer!!
Browse files Browse the repository at this point in the history
  • Loading branch information
k12ish committed Jun 4, 2024
1 parent d2e9a75 commit 012da11
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
18 changes: 8 additions & 10 deletions firmware/firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ void setup() {

void loop() {
if(!idle){
// MsgPack Start byte: Begin 64 bit unsigned integer
Serial.write(0xcf);
for (int _index = 0; _index < 2; _index++) {
unsigned int read = analogRead(A0);
Serial.write((read >> 8) & 0xFF);
Serial.write((read & 0xFF));
unsigned int time = micros();
Serial.write((time >> 8) & 0xFF);
Serial.write((time & 0xFF));
}
// MsgPack Start byte: Begin 32 bit unsigned integer
Serial.write(0xce);
unsigned int read = analogRead(A0);
Serial.write((read >> 8) & 0xFF);
Serial.write((read & 0xFF));
unsigned int time = micros();
Serial.write((time >> 8) & 0xFF);
Serial.write((time & 0xFF));
}
if (Serial.available() != 0){ // Check for message from host computer
mode = Serial.readString(); // Read change in mode
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Joi from 'joi';
export type arduinoModes = 'IDLE' | 'LALL' | 'RALL' | 'LARA';

// check that packet is integer and less than 64 bits
const DATA_VALIDATOR = Joi.number().integer().positive().max(Math.pow(2, 64))
const DATA_VALIDATOR = Joi.number().integer().positive().max(Math.pow(2, 32))

// Taken from https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
const timeout = (prom: Promise<any>, time: number, exception: any): Promise<any> => {
Expand Down Expand Up @@ -74,10 +74,10 @@ export class ArduinoInterface {
if (message.done) { break; }
const parse = DATA_VALIDATOR.validate(message.value)
if (!parse.error) {
const first = [(parse.value >> 48) & 0xFFFF, (parse.value >> 32) & 0xFFFF];
const second = [(parse.value >> 16) & 0xFFFF, parse.value & 0xFFFF];
items.push(first, second)
items.push([(parse.value >> 16) & 0xFFFF, parse.value & 0xFFFF])
// items must be in value, timestamp format
} else {
console.log('validation error', parse.error, message.value)
}
}
return items
Expand Down
1 change: 0 additions & 1 deletion frontend/src/routes/(app)/measure/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
const interval = setInterval(() => {
if ('loadPyodide' in window) {
clearInterval(interval);
console.log('foo');
resolve();
}
}, 20);
Expand Down

0 comments on commit 012da11

Please sign in to comment.