Created
March 30, 2012 19:40
-
-
Save Tasssadar/2254374 to your computer and use it in GitHub Desktop.
Lorris (JavaScript) implementation of our communication protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SMSG_POT_VALUES_8 = 0x00; // (int8_t x, int8_t y) pro kazdy joystick, min -128, max 127 | |
var SMSG_POT_VALUES_16 = 0x01; // (int16_t x, int16_t y) pro kazdy joystick, min -32768, max 32787 | |
var SMSG_BINARY_VALUES = 0x02; // (uint8_t count, uint8_t values...) hodnoty napr. z tlacitek, count je pocet, | |
// values muze byt vice bytu podle poctu tlacitek | |
var SMSG_SET_MOTOR_POWER_8 = 0x03; // (int8_t right, int8_t left) hodnoty pro nastaveni motoru, min -128, max 127 | |
var SMSG_SET_MOTOR_POWER_16 = 0x04; // (int16_t right, int16_t left) hodnoty pro nastaveni motoru, min -32768, max 32767 | |
var SMSG_GET_VALUES = 0x05; // (uint8_t valueType) typ hodnoty, viz Prikazy clienta. | |
// Dalsi data jsou potreba v pripade CMSG_RANGE_VALUE a CMSG_COLOR_VALUE, | |
// kdy struktura vypada takto: (uint16_t valueType, uint8_t sensorId) | |
var CMSG_BOARD_VOLTAGE = 0x00; // (uint16_t value) napeti na desce, hodnota z prevodniku | |
var CMSG_ENC_GLOBAL = 0x01; // (uint32_t right, uint32_t right) ujeta vzdalenost v dilcich encoderu, celkove | |
var CMSG_ENC_FROM_LAST = 0x02; // (uint32_t right, uint32_t right) ujeta vzdalenost v dilcich encoderu, od posledniho mereni | |
var CMSG_RANGE_VALUE = 0x03; // (uint8_t id, uint16_t distance) vzadelnost ze senzoru v cm, 0 az 65534, 65535 == chyba | |
var CMSG_COLOR_VALUE = 0x04; // (uint8_t id, uint8_t r, uint8_t g, uint8_t b, uint8_t ir, uint8_t uv) hodnota barevneho senzoru. | |
// ir a uv nemusi byt poslano pokud je senzor neumi. | |
var CMSG_OTHER_VALUES_16 = 0x05; // (uint16_t value, ...) hodnoty z dalsich senzoru | |
var CMSG_OTHER_VALUES_32 = 0x06; // (uint32_t value, ...) hodnoty z dalsich senzoruva | |
var CMSG_OTHER_VALUES_FLOAT = 0x07; // (float value, ...) hodnoty z dalsich senzoru | |
var client_names = new Array("CMSG_BOARD_VOLTAGE", "CMSG_ENC_GLOBAL", "CMSG_ENC_FROM_LAST", "CMSG_RANGE_VALUE", | |
"CMSG_COLOR_VALUE" ,"CMSG_OTHER_VALUES_16", "CMSG_OTHER_VALUES_32", "CMSG_OTHER_VALUES_FLOAT"); | |
var target_device = 0x01; | |
var lastX = (script.width + 20); | |
function onDataChanged(data, dev, cmd, index) | |
{ | |
var res = "\n"; | |
if(cmd < client_names.length) | |
res += client_names[cmd] + " "; | |
else | |
res += "Unknown "; | |
res += "0x" + cmd.toString(16).toUpperCase() + " "; | |
res += "\nDevice: " + dev + " (0x" + dev.toString(16).toUpperCase() + ")\n"; | |
appendTerm(res); | |
handlePacket(cmd, data); | |
} | |
function onKeyPress(key) | |
{ | |
switch(key) | |
{ | |
case "c": | |
clearTerm(); | |
break; | |
case "d": | |
dumpStoredTest(); | |
break; | |
} | |
} | |
function onScriptExit() | |
{ | |
storage.setBool("testBool", true); | |
storage.setInt32("testInt32", -5324); | |
storage.setUInt32("testUInt32", 435324); | |
storage.setFloat("testFloat", 3.143532); | |
storage.setString("testString", "šfasdfads"); | |
storage.setInt32Array("testInt32Array", new Array(-4, 532, -68, 3532)); | |
storage.setUInt32Array("testUInt32Array", new Array(4, 532, 68, 3532, 73432)); | |
storage.setFloatArray("testFloatArray", new Array(4.5, -532.43, -68, 3532.343)); | |
} | |
function dumpStoredTest() | |
{ | |
var res = "\n"; | |
res += "bool: " + storage.getBool("testBool") + "\n"; | |
res += "int32: " + storage.getInt32("testInt32") + "\n"; | |
res += "uint32: " + storage.getUInt32("testUInt32") + "\n"; | |
res += "float: " + storage.getFloat("testFloat") + "\n"; | |
res += "string: " + storage.getString("testString") + "\n"; | |
res += "int32 array: " + storage.getInt32Array("testInt32Array") + "\n"; | |
res += "uint32 array: " + storage.getUInt32Array("testUInt32Array") + "\n"; | |
res += "float array: " + storage.getFloatArray("testFloatArray") + "\n"; | |
appendTerm(res); | |
} | |
function handlePacket(cmd, data) | |
{ | |
switch(cmd) | |
{ | |
case CMSG_BOARD_VOLTAGE: | |
{ | |
var adc_val = (data[4] << 8) | data[5]; | |
appendTerm("ADC voltage value: " + adc_val + "\n"); | |
Voltage.updateValues(adc_val); | |
break; | |
} | |
case CMSG_ENC_GLOBAL: | |
case CMSG_ENC_FROM_LAST: | |
{ | |
var enc_r = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7]; | |
var enc_l = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]; | |
appendTerm("R: " + enc_r + " L: " + enc_l + "\n"); | |
if(cmd == CMSG_ENC_GLOBAL) | |
EncoderGlobal.updateValues(enc_r, enc_l); | |
else | |
EncoderLast.updateValues(enc_r, enc_l); | |
break; | |
} | |
case CMSG_RANGE_VALUE: | |
{ | |
var id = data[4]; | |
var value = (data[5] << 8) | data[6]; | |
if(Ranges[id] == null) | |
{ | |
Ranges[id] = new Range(id, LastRangeX, RangeY); | |
LastRangeX += Ranges[id].num.width + 20; | |
} | |
Ranges[id].updateValues(value); | |
break; | |
} | |
case CMSG_COLOR_VALUE: | |
{ | |
var id = data[4]; | |
var rgb = new Array(data[5], data[6], data[7]); | |
if(Colors[id] == null) | |
{ | |
Colors[id] = new Color(id, LastColorX, ColorY); | |
LastColorX += Colors[id].color.width + 20; | |
} | |
Colors[id].updateValues(rgb); | |
break; | |
} | |
} | |
} | |
var DataWithFormula = Class.extend( | |
{ | |
init: function(name, labelText, x, y, cmd) | |
{ | |
this.cmd = cmd; | |
this.name = name; | |
this.labelInput = newInputWidget(name, 200, 30, x + 50, y - 110); | |
this.label = this.labelInput.newWidget("QLabel", 1); | |
this.label.alignment = 0x0080 | 0x0004; | |
this.label.text = name; | |
this.requestBtn = this.labelInput.newWidget("QPushButton"); | |
this.requestBtn.text = "Request"; | |
this.requestBtn.clicked.connect(this, "requestClicked"); | |
this.input = newInputWidget(name + " formula", 300, 100, x, y); | |
this.formula_label = this.input.newWidget("QLabel", 1); | |
this.formula_label.text = labelText + "\n(v is received value):"; | |
this.formula = this.input.newWidget("QLineEdit"); | |
var stored = storage.getString(this.name); | |
this.formula.text = stored != "" ? stored : "v"; | |
this.formula.textEdited.connect(this, "formulaChanged"); | |
}, | |
parseFormula: function(adc) | |
{ | |
var form = this.formula.text; | |
form = form.replace("v", adc); | |
return parseInt(eval(form)); | |
}, | |
requestClicked: function() | |
{ | |
var packet = new Array(0xFF, target_device, 2, SMSG_GET_VALUES, this.cmd); | |
sendData(packet); | |
appendTerm("\nSMSG_GET_VALUES with cmd " + client_names[this.cmd] + " was sent\n"); | |
}, | |
formulaChanged: function(text) | |
{ | |
storage.setString(this.name, text); | |
} | |
}); | |
var _Voltage = DataWithFormula.extend( | |
{ | |
init: function(name, labelText, x, y, cmd) | |
{ | |
this._super(name, labelText, x, y, cmd); | |
this.board_voltage_adc = newNumberWidget("Voltage (ADC)", 0, 0, (script.width + 20), this.input.height + 20); | |
this.board_voltage_volts = newNumberWidget("Voltage (volts)", 0, 0, (script.width + 40 + this.board_voltage_adc.width), this.input.height + 20) | |
this.board_voltage_adc.setDataType(NUM_UINT16); | |
this.board_voltage_volts.setDataType(NUM_UINT16); | |
}, | |
updateValues: function(adc) | |
{ | |
this.board_voltage_adc.setValue(adc); | |
var voltage_volt = this.parseFormula(adc); | |
if(!isNaN(voltage_volt)) | |
this.board_voltage_volts.setValue(voltage_volt); | |
} | |
}); | |
var Voltage = new _Voltage("Board voltage", "Formula to calculate value in volts", lastX, 0, CMSG_BOARD_VOLTAGE); | |
lastX += Voltage.input.width + 20; | |
var _Encoder = DataWithFormula.extend( | |
{ | |
init: function(name, labelText, x, y, cmd) | |
{ | |
this._super(name, labelText, x, y, cmd); | |
this.left_tics = newNumberWidget("Left (tics)", 0, 0, x, this.input.height + 20); | |
this.left_form = newNumberWidget("Left (formula)", 0, 0, x, this.input.height + 40 + this.left_tics.height); | |
this.right_tics = newNumberWidget("Right (tics)", 0, 0, (x + 20 + this.left_tics.width), this.input.height + 20); | |
this.right_form = newNumberWidget("Right (formula)", 0, 0, (x + 20 + this.left_tics.width), this.input.height + 40 + this.right_tics.height); | |
this.left_tics.setDataType(NUM_INT32); | |
this.left_form.setDataType(NUM_INT32); | |
this.right_tics.setDataType(NUM_INT32); | |
this.right_form.setDataType(NUM_INT32); | |
}, | |
updateValues: function(right, left) | |
{ | |
this.left_tics.setValue(left); | |
this.right_tics.setValue(right); | |
var right_calc = this.parseFormula(right); | |
if(!isNaN(right_calc)) | |
this.right_form.setValue(right_calc); | |
var left_calc = this.parseFormula(left); | |
if(!isNaN(left_calc)) | |
this.left_form.setValue(left_calc); | |
} | |
}); | |
var EncoderGlobal = new _Encoder("Encoders (global)", "Formula to calculate value in metric system", lastX, 0, CMSG_ENC_GLOBAL); | |
lastX += EncoderGlobal.input.width + 20; | |
var EncoderLast = new _Encoder("Encoders (since last)", "Formula to calculate value in metric system", lastX, 0, CMSG_ENC_FROM_LAST); | |
lastX += EncoderLast.input.width + 20; | |
var IdRequest = Class.extend( | |
{ | |
init: function(name, text, cmd, x, y) | |
{ | |
this.cmd = cmd; | |
this.name = name; | |
this.labelInput = newInputWidget(name, 200, 140, x + 50, y); | |
this.label = this.labelInput.newWidget("QLabel", 1); | |
this.label.alignment = 0x0080 | 0x0004; | |
this.label.text = text; | |
this.idBox = this.labelInput.newWidget("QSpinBox"); | |
this.idBox.maximum = 255; | |
this.idBox.value = storage.getUInt32(name); | |
this.requestBtn = this.labelInput.newWidget("QPushButton"); | |
this.requestBtn.text = "Request"; | |
this.requestBtn.clicked.connect(this, "requestClicked"); | |
}, | |
requestClicked: function() | |
{ | |
storage.setUInt32(this.name, this.idBox.value); | |
var packet = new Array(0xFF, target_device, 3, SMSG_GET_VALUES, this.cmd, this.idBox.value); | |
sendData(packet); | |
appendTerm("\nSMSG_GET_VALUES with cmd " + client_names[this.cmd] + ", id " + this.idBox.value + " was sent\n"); | |
} | |
}); | |
var LastColorX = script.width + 20; | |
var ColorY = EncoderLast.input.height + EncoderLast.left_tics.height*2 + 20*3 + 40; | |
var RangeY = ColorY + 270; | |
new IdRequest("Color request", "Press \"Request\" to get\ncolor from sensor", CMSG_COLOR_VALUE, LastColorX, ColorY); | |
new IdRequest("Range request", "Press \"Request\" to get\nrange from sensor", CMSG_RANGE_VALUE, LastColorX, RangeY); | |
LastColorX += 320; | |
var LastRangeX = LastColorX; | |
var Color = Class.extend( | |
{ | |
init: function(id, x, y) | |
{ | |
this.id = id; | |
this.color = newColorWidget("Color " + id, 300, 250, x, y); | |
}, | |
updateValues: function(rgb) | |
{ | |
this.color.setValue(rgb[0], rgb[1], rgb[2]); | |
} | |
}); | |
var Colors = new Array(); | |
var Range = Class.extend( | |
{ | |
init: function(id, x, y) | |
{ | |
this.id = id; | |
this.num = newNumberWidget("Range " + id, 300, 100, x, y); | |
this.num.setDataType(NUM_INT32); | |
}, | |
updateValues: function(val) | |
{ | |
this.num.setValue(val); | |
if(val == 0xFFFF) | |
this.num.styleSheet = "background-color: red"; | |
else | |
this.num.styleSheet = ""; | |
} | |
}); | |
var Ranges = new Array(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment