| 1 |
|
| 2 |
var allsensors = []; |
| 3 |
|
| 4 |
Blockly.Blocks['scanI2C'] = { |
| 5 |
|
| 6 |
init: function() { |
| 7 |
this.appendDummyInput() |
| 8 |
.appendField("Scan I2C port, and get a") |
| 9 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('scanI2C');},'SS')); |
| 10 |
this.appendDummyInput() |
| 11 |
.appendField("list of detected sensors") |
| 12 |
this.setColour(330); |
| 13 |
this.setOutput(true, null); |
| 14 |
this.setTooltip("Scan I2C"); |
| 15 |
this.setHelpUrl(""); |
| 16 |
}, |
| 17 |
|
| 18 |
}; |
| 19 |
|
| 20 |
|
| 21 |
Blockly.JavaScript['scanI2C'] = function(block) { |
| 22 |
var code = 'JSON.parse(scanI2C())'; |
| 23 |
return [code,Blockly.JavaScript.ORDER_NONE]; |
| 24 |
}; |
| 25 |
|
| 26 |
Blockly.Python['scanI2C'] = function(block) { |
| 27 |
var code = 'I2C.scan()'; |
| 28 |
return [code,Blockly.Python.ORDER_NONE]; |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
Blockly.Blocks['scanI2CString'] = { |
| 35 |
|
| 36 |
init: function() { |
| 37 |
this.appendDummyInput() |
| 38 |
.appendField("Scan I2C port, and get detected") |
| 39 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('scanI2CString');},'SS')); |
| 40 |
this.appendDummyInput() |
| 41 |
.appendField("sensors in a comma separated string") |
| 42 |
this.setColour(330); |
| 43 |
this.setOutput(true, null); |
| 44 |
this.setTooltip("Scan I2C"); |
| 45 |
this.setHelpUrl(""); |
| 46 |
}, |
| 47 |
|
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
Blockly.JavaScript['scanI2CString'] = function(block) { |
| 52 |
var code = 'scanI2CString()'; |
| 53 |
return [code,Blockly.JavaScript.ORDER_NONE]; |
| 54 |
}; |
| 55 |
|
| 56 |
Blockly.Python['scanI2CString'] = function(block) { |
| 57 |
var code = 'I2C.scan()'; |
| 58 |
return [code,Blockly.Python.ORDER_NONE]; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Blockly.Blocks['scanI2Cselector'] = { |
| 64 |
|
| 65 |
init: function() { |
| 66 |
this.appendDummyInput() |
| 67 |
.appendField("Scan and select sensor") |
| 68 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('scanI2Cselector');},'SS')); |
| 69 |
this.setColour(330); |
| 70 |
this.setOutput(true, null); |
| 71 |
this.setTooltip("Scan I2C"); |
| 72 |
this.setHelpUrl(""); |
| 73 |
}, |
| 74 |
|
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
Blockly.JavaScript['scanI2Cselector'] = function(block) { |
| 79 |
var code = 'scanI2Cselector()'; |
| 80 |
return [code,Blockly.JavaScript.ORDER_NONE]; |
| 81 |
}; |
| 82 |
|
| 83 |
Blockly.Python['scanI2Cselector'] = function(block) { |
| 84 |
var code = 'I2C.scan()[0]'; |
| 85 |
return [code,Blockly.Python.ORDER_NONE]; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
Blockly.Blocks['read_I2C_sensor'] = { |
| 93 |
init: function() { |
| 94 |
this.appendDummyInput() |
| 95 |
.appendField("Read I2C Sensor:") |
| 96 |
.appendField(new Blockly.FieldDropdown([["BMP280","BMP280"],["MS5611","MS5611"],["INA219","INA219"],["ADS1115","ADS1115"], ["HMC5883L","HMC5883L"], ["TCS34725","TCS34725"], ["TSL2561","TSL2561"], ["TSL2591","TSL2591"], ["MAX44009","MAX44009"], ["AHT10","AHT10"], ["QMC5883L","QMC5883L"], ["MPU6050","MPU6050"], ["AK8963","AK8963"],["MAX30100","MAX30100"],["VL53L0X","VL53L0X"]]), "NAME") |
| 97 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_I2C_sensor');},'SS')); |
| 98 |
this.appendDummyInput() |
| 99 |
.appendField("Address:") |
| 100 |
.appendField(new Blockly.FieldNumber(13, 1, 127, 1), "ADDR"); |
| 101 |
this.setOutput(true, null); |
| 102 |
this.setColour(230); |
| 103 |
this.setTooltip(""); |
| 104 |
this.setHelpUrl(""); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
Blockly.JavaScript['read_I2C_sensor'] = function(block) { |
| 109 |
var name = block.getFieldValue('NAME'); |
| 110 |
var addr = block.getFieldValue('ADDR'); |
| 111 |
var code = 'get_generic_sensor(\''+name+'\','+addr+')'; |
| 112 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 113 |
}; |
| 114 |
|
| 115 |
Blockly.Python['read_I2C_sensor'] = function(block) { |
| 116 |
var name = block.getFieldValue('NAME'); |
| 117 |
var addr = block.getFieldValue('ADDR'); |
| 118 |
var code = 'get_generic_sensor(\''+name+'\','+addr+')'; |
| 119 |
return [code, Blockly.Python.ORDER_NONE]; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
Blockly.Blocks['read_i2c_sensor_flexible'] = { |
| 124 |
init: function() { |
| 125 |
this.appendValueInput("NAME") |
| 126 |
.setCheck(null) |
| 127 |
.appendField("Read I2C Sensor") |
| 128 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_i2c_sensor_flexible');},'SS')); |
| 129 |
this.appendDummyInput() |
| 130 |
.appendField("Parameter:") |
| 131 |
.appendField(new Blockly.FieldDropdown([["1","0"], ["2","1"], ["3","2"], ["4","3"], ["5","4"]]), "PARAM"); |
| 132 |
this.setInputsInline(false); |
| 133 |
this.setOutput(true, null); |
| 134 |
this.setColour(230); |
| 135 |
this.setTooltip(""); |
| 136 |
this.setHelpUrl(""); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
Blockly.JavaScript['read_i2c_sensor_flexible'] = function(block) { |
| 141 |
var value_name = Blockly.JavaScript.valueToCode(block, 'NAME', Blockly.JavaScript.ORDER_ATOMIC); |
| 142 |
var dropdown_param = block.getFieldValue('PARAM'); |
| 143 |
var code = 'parseAndReadSensor('+value_name+','+dropdown_param+')'; |
| 144 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 145 |
}; |
| 146 |
|
| 147 |
Blockly.Python['read_i2c_sensor_flexible'] = function(block) { |
| 148 |
var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC); |
| 149 |
var dropdown_param = block.getFieldValue('PARAM'); |
| 150 |
var code = ''; |
| 151 |
return [code, Blockly.Python.ORDER_NONE]; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
Blockly.Blocks['read_i2c_sensor_flexible_dynamic'] = { |
| 158 |
|
| 159 |
init: function() { |
| 160 |
|
| 161 |
this.appendDummyInput() |
| 162 |
.appendField(new Blockly.FieldImage("media/i2cscan.png", 100, 30, "*", this.scan.bind(this),'SS')) |
| 163 |
.appendField(new Blockly.FieldDropdown(this.generateNameOptions.bind(this),this.validateName.bind(this)), "NAME") |
| 164 |
.appendField(new Blockly.FieldImage("media/BMP280.png", 60, 60),'IMAGE') |
| 165 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_i2c_sensor_flexible_dynamic');},'SS')); |
| 166 |
this.appendDummyInput() |
| 167 |
.appendField("Parameter") |
| 168 |
.appendField(new Blockly.FieldDropdown(this.generateParameterOptions.bind(this)), 'PARAM'); |
| 169 |
this.appendDummyInput() |
| 170 |
.appendField("Configure") |
| 171 |
.appendField(new Blockly.FieldDropdown(this.generateConfigOptions.bind(this),this.validateConfiguration.bind(this)), 'CONFIG') |
| 172 |
.appendField(new Blockly.FieldDropdown(this.generateConfigSettings.bind(this),this.validateSettings.bind(this)), 'SETTING'); |
| 173 |
this.setInputsInline(false); |
| 174 |
this.setOutput(true); |
| 175 |
this.setColour(330); |
| 176 |
this.setTooltip(""); |
| 177 |
this.setHelpUrl(""); |
| 178 |
}, |
| 179 |
|
| 180 |
generateNameOptions: function() { |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if(typeof(this.availableSensors) === 'undefined'){ |
| 185 |
var channels={}; |
| 186 |
this.configParams = {}; |
| 187 |
this.activeConfigurations = {}; |
| 188 |
|
| 189 |
channels = allsensors; |
| 190 |
console.log('channels',channels); |
| 191 |
|
| 192 |
this.availableSensors = []; |
| 193 |
for(i=0;i<channels.length;i++){ |
| 194 |
this.availableSensors.push(channels[i]); |
| 195 |
} |
| 196 |
|
| 197 |
if(this.availableSensors.length==0) |
| 198 |
this.currentValue = "No Sensor Found"; |
| 199 |
else { |
| 200 |
if(this.availableSensors[0].search('/') != -1){ |
| 201 |
this.currentValue = this.availableSensors[0].split('/')[0]; |
| 202 |
}else{ |
| 203 |
this.currentValue = this.availableSensors[0]; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
chans=[]; |
| 210 |
for(i=0;i<this.availableSensors.length;i++){ |
| 211 |
s = this.availableSensors[i]; |
| 212 |
if(s.search('/') != -1){ |
| 213 |
addr = s.split(']')[0].substring(1); |
| 214 |
sensor1 = s.split(']')[1].split('/')[0]; |
| 215 |
sensor2 = s.split(']')[1].split('/')[1]; |
| 216 |
chans.push([sensor1,'['+addr+']'+sensor1]); |
| 217 |
chans.push([sensor2,'['+addr+']'+sensor2]); |
| 218 |
}else{ |
| 219 |
var name = (s.search(']') == -1)?s : s.split(']')[1]; |
| 220 |
chans.push([name,s]); |
| 221 |
} |
| 222 |
} |
| 223 |
if(chans.length>0) |
| 224 |
return chans; |
| 225 |
else |
| 226 |
return [["No Sensor Found","No Sensor Found"]]; |
| 227 |
}, |
| 228 |
|
| 229 |
validateName: function(newValue) { |
| 230 |
this.currentValue = newValue; |
| 231 |
|
| 232 |
if(!this.currentValue.startsWith("["))return [["",""]]; |
| 233 |
var otherVal = this.currentValue; |
| 234 |
if(otherVal.search(']') != -1)otherVal = otherVal.split(']')[1]; |
| 235 |
|
| 236 |
var paramsDropdown = this.getField('PARAM'); |
| 237 |
var paropts=[["0","0"]]; |
| 238 |
|
| 239 |
fetch(`/get_sensor_parameters/${otherVal}`) |
| 240 |
.then(response => response.json()) |
| 241 |
.then(data => { |
| 242 |
console.log('params updating with', this.parameters); |
| 243 |
this.parameters = data.fields; |
| 244 |
console.log('now:', this.parameters); |
| 245 |
|
| 246 |
|
| 247 |
console.log('Force an update of the parameters dropdown'); |
| 248 |
paropts = paramsDropdown.getOptions(false); |
| 249 |
paramsDropdown.doValueUpdate_(); |
| 250 |
paramsDropdown.setValue("0"); |
| 251 |
}) |
| 252 |
.catch(error => { |
| 253 |
console.error('Error fetching parameters:', error); |
| 254 |
}); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
var image = this.getField('IMAGE'); |
| 264 |
|
| 265 |
|
| 266 |
image.setValue("media/"+newValue.split(']')[1]+".jpeg"); |
| 267 |
|
| 268 |
}, |
| 269 |
|
| 270 |
generateParameterOptions: function() { |
| 271 |
if(typeof(this.currentValue) === 'undefined')return [["",""]]; |
| 272 |
|
| 273 |
try{ |
| 274 |
var channels=this.parameters; |
| 275 |
chans=[]; |
| 276 |
for(i=0;i<channels.length;i++){ |
| 277 |
chans.push([channels[i],String(i)]); |
| 278 |
} |
| 279 |
return chans; |
| 280 |
}catch(e){ |
| 281 |
console.log('couldnot generate parameter options list',e) |
| 282 |
return [["",""]]; |
| 283 |
} |
| 284 |
}, |
| 285 |
|
| 286 |
generateConfigOptions: function() { |
| 287 |
if(typeof(this.currentValue) === 'undefined')return [["",""]]; |
| 288 |
|
| 289 |
if(!this.currentValue.startsWith("["))return [["No Sensor Found","No Sensor Found"]]; |
| 290 |
var otherVal = this.currentValue; |
| 291 |
|
| 292 |
if(otherVal.search(']') != -1)otherVal = otherVal.split(']')[1]; |
| 293 |
var params = {}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
this.configParams = params; |
| 298 |
|
| 299 |
|
| 300 |
console.log('generate configuration options:'+params+','+otherVal); |
| 301 |
chans=[]; |
| 302 |
for(key in params){ |
| 303 |
if(key.length>1){ |
| 304 |
if(!(key in this.activeConfigurations)) this.activeConfigurations[key]=params[key][0]; |
| 305 |
chans.push([key,key]); |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
if(chans.length==0){ |
| 311 |
chans = [["",""]]; |
| 312 |
try{ |
| 313 |
var settingsDropdown = this.getField('SETTING'); |
| 314 |
var opts = settingsDropdown.getOptions(false); |
| 315 |
settingsDropdown.setValue(opts[0][1]); |
| 316 |
}catch(e){} |
| 317 |
} |
| 318 |
return chans; |
| 319 |
}, |
| 320 |
|
| 321 |
validateConfiguration: function(newValue) { |
| 322 |
this.currentConfig = newValue; |
| 323 |
var settingsDropdown = this.getField('SETTING'); |
| 324 |
var opts = settingsDropdown.getOptions(false); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if(!(typeof(this.activeConfigurations[newValue]) == 'undefined'))settingsDropdown.setValue(this.activeConfigurations[newValue]); |
| 329 |
}, |
| 330 |
|
| 331 |
generateConfigSettings: function() { |
| 332 |
if(typeof(this.currentConfig) === 'undefined' || typeof(this.configParams) === 'undefined')return [["",""]]; |
| 333 |
if(!(this.currentConfig in this.configParams))return [["",""]]; |
| 334 |
|
| 335 |
chans=[]; |
| 336 |
for(i=0;i<this.configParams[this.currentConfig].length;i++){ |
| 337 |
chans.push([this.configParams[this.currentConfig][i],this.configParams[this.currentConfig][i]]); |
| 338 |
} |
| 339 |
if(chans.length==0)chans = [["",""]]; |
| 340 |
console.log("Config Settings Generated:"+chans); |
| 341 |
return chans; |
| 342 |
}, |
| 343 |
|
| 344 |
validateSettings:function(value){ |
| 345 |
var name = this.getFieldValue('NAME'); |
| 346 |
|
| 347 |
var conf = this.currentConfig; |
| 348 |
|
| 349 |
if(typeof(name) === 'undefined' || typeof(conf) === 'undefined')return; |
| 350 |
if(conf in this.configParams){ |
| 351 |
|
| 352 |
if(!this.configParams[conf].includes(value)){ |
| 353 |
|
| 354 |
return; |
| 355 |
} |
| 356 |
try{ |
| 357 |
addr = name.split(']')[0].substring(1); |
| 358 |
sensor = name.split(']')[1].split('/')[0]; |
| 359 |
if(sensor === "unsupported")return; |
| 360 |
this.activeConfigurations[conf]=value; |
| 361 |
|
| 362 |
|
| 363 |
}catch(e){ |
| 364 |
|
| 365 |
} |
| 366 |
} |
| 367 |
}, |
| 368 |
|
| 369 |
|
| 370 |
scan: function() { |
| 371 |
|
| 372 |
var nameDropdown = this.getField('NAME'); |
| 373 |
|
| 374 |
fetch(`/scan_i2c`) |
| 375 |
.then(response => response.json()) |
| 376 |
.then(data => { |
| 377 |
var channels = data.sensors; |
| 378 |
console.log(data+';'+channels); |
| 379 |
this.availableSensors = []; |
| 380 |
for(i=0;i<channels.length;i++){ |
| 381 |
this.availableSensors.push(channels[i]); |
| 382 |
} |
| 383 |
var opts = nameDropdown.getOptions(false); |
| 384 |
nameDropdown.doValueUpdate_(); |
| 385 |
nameDropdown.setValue(opts[0][1]); |
| 386 |
}) |
| 387 |
.catch(error => { |
| 388 |
console.error('Error scanning I2C', error); |
| 389 |
|
| 390 |
}); |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
}; |
| 395 |
|
| 396 |
Blockly.JavaScript['read_i2c_sensor_flexible_dynamic'] = function(block) { |
| 397 |
var value_name = block.getFieldValue('NAME'); |
| 398 |
var dropdown_param = block.getFieldValue('PARAM'); |
| 399 |
var code = 'parseAndReadSensor(\''+value_name+'\','+dropdown_param+')'; |
| 400 |
|
| 401 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 402 |
}; |
| 403 |
|
| 404 |
Blockly.Python['read_i2c_sensor_flexible_dynamic'] = function(block) { |
| 405 |
var value_name = block.getFieldValue('NAME'); |
| 406 |
var dropdown_param = block.getFieldValue('PARAM'); |
| 407 |
var code = 'p.get_sensor(\''+value_name.split(']')[1].split('/')[0]+'\','+dropdown_param+')'; |
| 408 |
return [code, Blockly.Python.ORDER_NONE]; |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
Blockly.Blocks['read_i2c_sensor_flexible_dynamic_array'] = { |
| 415 |
|
| 416 |
init: function() { |
| 417 |
|
| 418 |
this.appendDummyInput() |
| 419 |
.appendField(new Blockly.FieldImage("media/i2cscan.png", 100, 30, "*", this.scan.bind(this),'SS')) |
| 420 |
.appendField(new Blockly.FieldDropdown(this.generateNameOptions.bind(this),this.validateName.bind(this)), "NAME") |
| 421 |
.appendField(new Blockly.FieldImage("media/BMP280.png", 60, 60),'IMAGE') |
| 422 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_i2c_sensor_flexible_dynamic_array');},'SS')); |
| 423 |
this.appendDummyInput() |
| 424 |
.appendField("Return All Parameters as an Array",'PARAM') |
| 425 |
this.appendDummyInput() |
| 426 |
.appendField("Configure") |
| 427 |
.appendField(new Blockly.FieldDropdown(this.generateConfigOptions.bind(this),this.validateConfiguration.bind(this)), 'CONFIG') |
| 428 |
.appendField(new Blockly.FieldDropdown(this.generateConfigSettings.bind(this),this.validateSettings.bind(this)), 'SETTING'); |
| 429 |
this.setInputsInline(false); |
| 430 |
this.setOutput(true); |
| 431 |
this.setColour(330); |
| 432 |
this.setTooltip(""); |
| 433 |
this.setHelpUrl(""); |
| 434 |
}, |
| 435 |
|
| 436 |
generateNameOptions: function() { |
| 437 |
|
| 438 |
this.parameters=["Scan First"]; |
| 439 |
|
| 440 |
if(typeof(this.availableSensors) === 'undefined'){ |
| 441 |
var channels={}; |
| 442 |
this.configParams = {}; |
| 443 |
this.activeConfigurations = {}; |
| 444 |
channels = allsensors; |
| 445 |
console.log('channels',channels); |
| 446 |
|
| 447 |
this.availableSensors = []; |
| 448 |
for(i=0;i<channels.length;i++){ |
| 449 |
this.availableSensors.push(channels[i]); |
| 450 |
} |
| 451 |
|
| 452 |
if(this.availableSensors.length==0) |
| 453 |
this.currentValue = "No Sensor Found"; |
| 454 |
else { |
| 455 |
if(this.availableSensors[0].search('/') != -1){ |
| 456 |
this.currentValue = this.availableSensors[0].split('/')[0]; |
| 457 |
}else{ |
| 458 |
this.currentValue = this.availableSensors[0]; |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
chans=[]; |
| 465 |
for(i=0;i<this.availableSensors.length;i++){ |
| 466 |
s = this.availableSensors[i]; |
| 467 |
if(s.search('/') != -1){ |
| 468 |
addr = s.split(']')[0].substring(1); |
| 469 |
sensor1 = s.split(']')[1].split('/')[0]; |
| 470 |
sensor2 = s.split(']')[1].split('/')[1]; |
| 471 |
chans.push([sensor1,'['+addr+']'+sensor1]); |
| 472 |
chans.push([sensor2,'['+addr+']'+sensor2]); |
| 473 |
}else{ |
| 474 |
var name = (s.search(']') == -1)?s : s.split(']')[1]; |
| 475 |
chans.push([name,s]); |
| 476 |
} |
| 477 |
} |
| 478 |
if(chans.length>0) |
| 479 |
return chans; |
| 480 |
else |
| 481 |
return [["No Sensor Found","No Sensor Found"]]; |
| 482 |
}, |
| 483 |
|
| 484 |
validateName: function(newValue) { |
| 485 |
this.currentValue = newValue; |
| 486 |
|
| 487 |
if(!this.currentValue.startsWith("["))return [["",""]]; |
| 488 |
var otherVal = this.currentValue; |
| 489 |
if(otherVal.search(']') != -1)otherVal = otherVal.split(']')[1]; |
| 490 |
|
| 491 |
var paramsDropdown = this.getField('PARAM'); |
| 492 |
var paropts=[["0","0"]]; |
| 493 |
|
| 494 |
HWBridge.getSensorParameters(otherVal,(vals)=>{ |
| 495 |
console.log('params updating with', this.parameters); |
| 496 |
this.parameters = JSON.parse(vals); |
| 497 |
|
| 498 |
|
| 499 |
console.log('Force an update of the parameters dropdown'); |
| 500 |
paropts = paramsDropdown.getOptions(false); |
| 501 |
paramsDropdown.doValueUpdate_(); |
| 502 |
paramsDropdown.setValue("0"); |
| 503 |
|
| 504 |
}) |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
var image = this.getField('IMAGE'); |
| 514 |
|
| 515 |
|
| 516 |
image.setValue("media/"+newValue.split(']')[1]+".jpeg"); |
| 517 |
|
| 518 |
}, |
| 519 |
|
| 520 |
generateConfigOptions: function() { |
| 521 |
if(typeof(this.currentValue) === 'undefined')return [["",""]]; |
| 522 |
|
| 523 |
if(!this.currentValue.startsWith("["))return [["No Sensor Found","No Sensor Found"]]; |
| 524 |
var otherVal = this.currentValue; |
| 525 |
|
| 526 |
if(otherVal.search(']') != -1)otherVal = otherVal.split(']')[1]; |
| 527 |
var params = {}; |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
this.configParams = params; |
| 532 |
|
| 533 |
|
| 534 |
console.log('generate configuration options:'+params+','+otherVal); |
| 535 |
chans=[]; |
| 536 |
for(key in params){ |
| 537 |
if(key.length>1){ |
| 538 |
if(!(key in this.activeConfigurations)) this.activeConfigurations[key]=params[key][0]; |
| 539 |
chans.push([key,key]); |
| 540 |
} |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
if(chans.length==0){ |
| 545 |
chans = [["",""]]; |
| 546 |
try{ |
| 547 |
var settingsDropdown = this.getField('SETTING'); |
| 548 |
var opts = settingsDropdown.getOptions(false); |
| 549 |
settingsDropdown.setValue(opts[0][1]); |
| 550 |
}catch(e){} |
| 551 |
} |
| 552 |
return chans; |
| 553 |
}, |
| 554 |
|
| 555 |
validateConfiguration: function(newValue) { |
| 556 |
this.currentConfig = newValue; |
| 557 |
var settingsDropdown = this.getField('SETTING'); |
| 558 |
var opts = settingsDropdown.getOptions(false); |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
if(!(typeof(this.activeConfigurations[newValue]) == 'undefined'))settingsDropdown.setValue(this.activeConfigurations[newValue]); |
| 563 |
}, |
| 564 |
|
| 565 |
generateConfigSettings: function() { |
| 566 |
if(typeof(this.currentConfig) === 'undefined' || typeof(this.configParams) === 'undefined')return [["",""]]; |
| 567 |
if(!(this.currentConfig in this.configParams))return [["",""]]; |
| 568 |
|
| 569 |
chans=[]; |
| 570 |
for(i=0;i<this.configParams[this.currentConfig].length;i++){ |
| 571 |
chans.push([this.configParams[this.currentConfig][i],this.configParams[this.currentConfig][i]]); |
| 572 |
} |
| 573 |
if(chans.length==0)chans = [["",""]]; |
| 574 |
console.log("Config Settings Generated:"+chans); |
| 575 |
return chans; |
| 576 |
}, |
| 577 |
|
| 578 |
validateSettings:function(value){ |
| 579 |
var name = this.getFieldValue('NAME'); |
| 580 |
|
| 581 |
var conf = this.currentConfig; |
| 582 |
|
| 583 |
if(typeof(name) === 'undefined' || typeof(conf) === 'undefined')return; |
| 584 |
if(conf in this.configParams){ |
| 585 |
|
| 586 |
if(!this.configParams[conf].includes(value)){ |
| 587 |
|
| 588 |
return; |
| 589 |
} |
| 590 |
try{ |
| 591 |
addr = name.split(']')[0].substring(1); |
| 592 |
sensor = name.split(']')[1].split('/')[0]; |
| 593 |
if(sensor === "unsupported")return; |
| 594 |
this.activeConfigurations[conf]=value; |
| 595 |
if(typeof(HWBridge) !== 'undefined')HWBridge.configure_sensor(sensor,parseInt(addr),conf,value); |
| 596 |
}catch(e){ |
| 597 |
|
| 598 |
} |
| 599 |
} |
| 600 |
}, |
| 601 |
|
| 602 |
|
| 603 |
scan: function() { |
| 604 |
|
| 605 |
var nameDropdown = this.getField('NAME'); |
| 606 |
|
| 607 |
if(typeof(HWBridge) !== 'undefined'){ |
| 608 |
HWBridge.scanI2C((vals)=>{ |
| 609 |
var channels = JSON.parse(vals); |
| 610 |
console.log(vals); |
| 611 |
this.availableSensors = []; |
| 612 |
for(i=0;i<channels.length;i++){ |
| 613 |
this.availableSensors.push(channels[i]); |
| 614 |
} |
| 615 |
var opts = nameDropdown.getOptions(false); |
| 616 |
nameDropdown.doValueUpdate_(); |
| 617 |
nameDropdown.setValue(opts[0][1]); |
| 618 |
|
| 619 |
}); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
} |
| 624 |
|
| 625 |
}; |
| 626 |
|
| 627 |
Blockly.JavaScript['read_i2c_sensor_flexible_dynamic_array'] = function(block) { |
| 628 |
var value_name = block.getFieldValue('NAME'); |
| 629 |
var code = 'parseAndReadSensorVals(\''+value_name+'\')'; |
| 630 |
|
| 631 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 632 |
}; |
| 633 |
|
| 634 |
Blockly.Python['read_i2c_sensor_flexible_dynamic_array'] = function(block) { |
| 635 |
var value_name = block.getFieldValue('NAME'); |
| 636 |
var dropdown_param = block.getFieldValue('PARAM'); |
| 637 |
var code = 'p.get_sensor(\''+value_name.split(']')[1].split('/')[0]+'\','+dropdown_param+')'; |
| 638 |
return [code, Blockly.Python.ORDER_NONE]; |
| 639 |
}; |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
Blockly.Blocks['read_BMP280'] = { |
| 649 |
init: function() { |
| 650 |
this.appendDummyInput() |
| 651 |
.appendField("Read BMP280") |
| 652 |
.appendField(new Blockly.FieldDropdown([["TEMPERATURE","0"], ["PRESSURE","1"]]), "CHANNEL") |
| 653 |
.appendField(new Blockly.FieldImage("media/BMP280.png", 30, 30, { alt: "*", flipRtl: "FALSE" })) |
| 654 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_BMP280');},'SS')); |
| 655 |
this.setOutput(true, null); |
| 656 |
this.setColour(230); |
| 657 |
this.setTooltip(""); |
| 658 |
this.setHelpUrl(""); |
| 659 |
} |
| 660 |
}; |
| 661 |
|
| 662 |
Blockly.JavaScript['read_BMP280'] = function(block) { |
| 663 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 664 |
|
| 665 |
var code = 'get_sensor(\'BMP280\',\''+dropdown_channel+'\')'; |
| 666 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 667 |
}; |
| 668 |
|
| 669 |
Blockly.Python['read_BMP280'] = function(block) { |
| 670 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 671 |
|
| 672 |
var code = 'get_sensor(\'BMP280\',\''+dropdown_channel+'\')'; |
| 673 |
return [code, Blockly.Python.ORDER_NONE]; |
| 674 |
}; |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
Blockly.Blocks['read_MS5611'] = { |
| 680 |
init: function() { |
| 681 |
this.appendDummyInput() |
| 682 |
.appendField("Read MS5611") |
| 683 |
.appendField(new Blockly.FieldDropdown([["PRESSURE","0"], ["TEMPERATURE","1"]]), "CHANNEL") |
| 684 |
.appendField(new Blockly.FieldImage("media/BMP280.png", 30, 30, { alt: "*", flipRtl: "FALSE" })) |
| 685 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_MS5611');},'SS')); |
| 686 |
this.setOutput(true, null); |
| 687 |
this.setColour(230); |
| 688 |
this.setTooltip(""); |
| 689 |
this.setHelpUrl(""); |
| 690 |
} |
| 691 |
}; |
| 692 |
|
| 693 |
Blockly.JavaScript['read_MS5611'] = function(block) { |
| 694 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 695 |
|
| 696 |
var code = 'get_sensor(\'MS5611\',\''+dropdown_channel+'\')'; |
| 697 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 698 |
}; |
| 699 |
|
| 700 |
Blockly.Python['read_MS5611'] = function(block) { |
| 701 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 702 |
|
| 703 |
var code = 'get_sensor(\'MS5611\',\''+dropdown_channel+'\')'; |
| 704 |
return [code, Blockly.Python.ORDER_NONE]; |
| 705 |
}; |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
Blockly.Blocks['read_TSL2561'] = { |
| 710 |
init: function() { |
| 711 |
this.appendDummyInput() |
| 712 |
.appendField("Read TSL2561(lum)") |
| 713 |
.appendField(new Blockly.FieldDropdown([["LUMINOSITY(total)","0"], ["Infrared","1"]]), "CHANNEL") |
| 714 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_TSL2561');},'SS')); |
| 715 |
this.setOutput(true, null); |
| 716 |
this.setColour(230); |
| 717 |
this.setTooltip(""); |
| 718 |
this.setHelpUrl(""); |
| 719 |
} |
| 720 |
}; |
| 721 |
|
| 722 |
Blockly.JavaScript['read_TSL2561'] = function(block) { |
| 723 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 724 |
var code = 'get_sensor(\'TSL2561\',\''+dropdown_channel+'\')'; |
| 725 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 726 |
}; |
| 727 |
|
| 728 |
Blockly.Python['read_TSL2561'] = function(block) { |
| 729 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 730 |
|
| 731 |
var code = 'get_sensor(\'TSL2561\',\''+dropdown_channel+'\')'; |
| 732 |
return [code, Blockly.Python.ORDER_NONE]; |
| 733 |
}; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
Blockly.Blocks['read_TSL2591'] = { |
| 738 |
init: function() { |
| 739 |
this.appendDummyInput() |
| 740 |
.appendField("Read TSL2591(lum)") |
| 741 |
.appendField(new Blockly.FieldDropdown([["Raw","0"], ["Luminosity(Vis)","1"], ["Luminosity(IR)","2"]]), "CHANNEL"); |
| 742 |
this.setOutput(true, null); |
| 743 |
this.setColour(230); |
| 744 |
this.setTooltip(""); |
| 745 |
this.setHelpUrl(""); |
| 746 |
} |
| 747 |
}; |
| 748 |
|
| 749 |
Blockly.JavaScript['read_TSL2591'] = function(block) { |
| 750 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 751 |
var code = 'get_sensor(\'TSL2591\',\''+dropdown_channel+'\')'; |
| 752 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 753 |
}; |
| 754 |
|
| 755 |
Blockly.Python['read_TSL2591'] = function(block) { |
| 756 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 757 |
|
| 758 |
var code = 'get_sensor(\'TSL2591\',\''+dropdown_channel+'\')'; |
| 759 |
return [code, Blockly.Python.ORDER_NONE]; |
| 760 |
}; |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
Blockly.Blocks['read_MAX30100'] = { |
| 765 |
init: function() { |
| 766 |
this.appendDummyInput() |
| 767 |
.appendField("Read MAX30100") |
| 768 |
.appendField(new Blockly.FieldDropdown([["RED LED","0"], ["IR LED","1"]]), "CHANNEL") |
| 769 |
.appendField(new Blockly.FieldImage("media/pulse.png", 30, 30, { alt: "*", flipRtl: "FALSE" })) |
| 770 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_MAX30100');},'SS')); |
| 771 |
this.appendDummyInput() |
| 772 |
.appendField("Heart Rate"); |
| 773 |
this.setOutput(true, null); |
| 774 |
this.setColour(230); |
| 775 |
this.setTooltip(""); |
| 776 |
this.setHelpUrl(""); |
| 777 |
} |
| 778 |
}; |
| 779 |
|
| 780 |
Blockly.JavaScript['read_MAX30100'] = function(block) { |
| 781 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 782 |
|
| 783 |
var code = 'get_sensor(\'MAX30100\',\''+dropdown_channel+'\')'; |
| 784 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 785 |
}; |
| 786 |
|
| 787 |
Blockly.Python['read_MAX30100'] = function(block) { |
| 788 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 789 |
|
| 790 |
var code = 'get_sensor(\'MAX30100\',\''+dropdown_channel+'\')'; |
| 791 |
return [code, Blockly.Python.ORDER_NONE]; |
| 792 |
}; |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
Blockly.Blocks['read_MAX6675'] = { |
| 798 |
init: function() { |
| 799 |
this.appendDummyInput() |
| 800 |
.appendField("Read Temperature from") |
| 801 |
.appendField(new Blockly.FieldImage("media/THERMOMETER.png", 30, 30)) |
| 802 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_MAX6675');},'SS')); |
| 803 |
this.appendDummyInput() |
| 804 |
.appendField("MAX6675 Module on") |
| 805 |
.appendField(new Blockly.FieldDropdown([["CS1","1"], ["CS2","2"], ["CS3","3"], ["CS4","4"]]), "CHANNEL") |
| 806 |
this.setOutput(true, null); |
| 807 |
this.setColour(230); |
| 808 |
this.setTooltip(""); |
| 809 |
this.setHelpUrl(""); |
| 810 |
} |
| 811 |
}; |
| 812 |
|
| 813 |
Blockly.JavaScript['read_MAX6675'] = function(block) { |
| 814 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 815 |
var code = 'MAX6675('+dropdown_channel+')'; |
| 816 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 817 |
}; |
| 818 |
|
| 819 |
Blockly.Python['read_MAX6675'] = function(block) { |
| 820 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 821 |
var code = 'read_MAX6675('+dropdown_channel+')'; |
| 822 |
return [code, Blockly.Python.ORDER_NONE]; |
| 823 |
}; |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
Blockly.Blocks['read_MPU6050'] = { |
| 829 |
init: function() { |
| 830 |
this.appendDummyInput() |
| 831 |
.appendField("Read MPU6050") |
| 832 |
.appendField(new Blockly.FieldDropdown([["Ax","0"], ["Ay","1"], ["Az","2"], ["Gx","4"], ["Gy","5"], ["Gz","6"]]), "CHANNEL") |
| 833 |
.appendField(new Blockly.FieldImage("media/MPU6050.png", 20, 20, { alt: "*", flipRtl: "FALSE" })) |
| 834 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_MPU6050');},'SS')); |
| 835 |
this.setOutput(true, null); |
| 836 |
this.setColour(230); |
| 837 |
this.setTooltip(""); |
| 838 |
this.setHelpUrl(""); |
| 839 |
} |
| 840 |
}; |
| 841 |
|
| 842 |
Blockly.JavaScript['read_MPU6050'] = function(block) { |
| 843 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 844 |
|
| 845 |
var code = 'get_sensor(\'MPU6050\',\''+dropdown_channel+'\')'; |
| 846 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 847 |
}; |
| 848 |
|
| 849 |
Blockly.Python['read_MPU6050'] = function(block) { |
| 850 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 851 |
|
| 852 |
var code = 'get_sensor(\'MPU6050\',\''+dropdown_channel+'\')'; |
| 853 |
return [code, Blockly.Python.ORDER_NONE]; |
| 854 |
}; |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
Blockly.Blocks['read_VL53L0X'] = { |
| 859 |
init: function() { |
| 860 |
this.appendDummyInput() |
| 861 |
.appendField("Read VL53L0X") |
| 862 |
.appendField(new Blockly.FieldDropdown([["Distance(mm)","0"]]), "CHANNEL") |
| 863 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_VL53L0X');},'SS')); |
| 864 |
this.setOutput(true, null); |
| 865 |
this.setColour(230); |
| 866 |
this.setTooltip(""); |
| 867 |
this.setHelpUrl(""); |
| 868 |
} |
| 869 |
}; |
| 870 |
|
| 871 |
Blockly.JavaScript['read_VL53L0X'] = function(block) { |
| 872 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 873 |
|
| 874 |
var code = 'get_sensor(\'VL53L0X\',\''+dropdown_channel+'\')'; |
| 875 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 876 |
}; |
| 877 |
|
| 878 |
Blockly.Python['read_VL53L0X'] = function(block) { |
| 879 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 880 |
|
| 881 |
var code = 'get_sensor(\'VL53L0X\',\''+dropdown_channel+'\')'; |
| 882 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 883 |
}; |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
Blockly.Blocks['read_ML8511'] = { |
| 888 |
init: function() { |
| 889 |
this.appendDummyInput() |
| 890 |
.appendField("Read ML8511") |
| 891 |
.appendField(new Blockly.FieldDropdown([["UV Light mW/cm^2","0"]]), "CHANNEL") |
| 892 |
.appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('read_ML8511');},'SS')); |
| 893 |
this.setOutput(true, null); |
| 894 |
this.setColour(230); |
| 895 |
this.setTooltip(""); |
| 896 |
this.setHelpUrl(""); |
| 897 |
} |
| 898 |
}; |
| 899 |
|
| 900 |
Blockly.JavaScript['read_ML8511'] = function(block) { |
| 901 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 902 |
|
| 903 |
var code = 'get_sensor(\'ML8511\',\''+dropdown_channel+'\')'; |
| 904 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 905 |
}; |
| 906 |
|
| 907 |
Blockly.Python['read_ML8511'] = function(block) { |
| 908 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 909 |
|
| 910 |
var code = 'get_sensor(\'ML8511\',\''+dropdown_channel+'\')'; |
| 911 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 912 |
}; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
Blockly.Blocks['read_TCS34725'] = { |
| 919 |
init: function() { |
| 920 |
this.appendDummyInput() |
| 921 |
.appendField("Read TCS34725(RGB)") |
| 922 |
.appendField(new Blockly.FieldDropdown([["Luminance","0"],["InfraRed","1"],["Red","2"], ["Green","3"], ["Blue","4"]]), "CHANNEL"); |
| 923 |
this.setOutput(true, null); |
| 924 |
this.setColour(230); |
| 925 |
this.setTooltip(""); |
| 926 |
this.setHelpUrl(""); |
| 927 |
} |
| 928 |
}; |
| 929 |
|
| 930 |
Blockly.JavaScript['read_TCS34725'] = function(block) { |
| 931 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 932 |
var code = 'get_sensor(\'TCS34725\',\''+dropdown_channel+'\')'; |
| 933 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 934 |
}; |
| 935 |
|
| 936 |
Blockly.Python['read_TCS34725'] = function(block) { |
| 937 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 938 |
var code = 'get_sensor(\'TCS34725\',\''+dropdown_channel+'\')'; |
| 939 |
return [code, Blockly.Python.ORDER_NONE]; |
| 940 |
}; |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
Blockly.Blocks['read_INA219'] = { |
| 945 |
init: function() { |
| 946 |
this.appendDummyInput() |
| 947 |
.appendField("Read INA219") |
| 948 |
.appendField(new Blockly.FieldDropdown([["Current","0"], ["Shunt Voltage","1"], ["Bus Voltage","2"], ["Power","3"]]), "CHANNEL"); |
| 949 |
this.setOutput(true, null); |
| 950 |
this.setColour(230); |
| 951 |
this.setTooltip(""); |
| 952 |
this.setHelpUrl(""); |
| 953 |
} |
| 954 |
}; |
| 955 |
|
| 956 |
Blockly.JavaScript['read_INA219'] = function(block) { |
| 957 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 958 |
var code = 'get_sensor(\'INA219\',\''+dropdown_channel+'\')'; |
| 959 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 960 |
}; |
| 961 |
|
| 962 |
Blockly.Python['read_INA219'] = function(block) { |
| 963 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 964 |
var code = 'get_sensor(\'INA219\',\''+dropdown_channel+'\')'; |
| 965 |
return [code, Blockly.Python.ORDER_NONE]; |
| 966 |
}; |
| 967 |
|
| 968 |
|
| 969 |
Blockly.Blocks['read_ADS1115'] = { |
| 970 |
init: function() { |
| 971 |
this.appendDummyInput() |
| 972 |
.appendField("Read ADS1115 (+/-0.25V)") |
| 973 |
.appendField(new Blockly.FieldDropdown([["Voltage","0"]]), "CHANNEL"); |
| 974 |
this.setOutput(true, null); |
| 975 |
this.setColour(230); |
| 976 |
this.setTooltip(""); |
| 977 |
this.setHelpUrl(""); |
| 978 |
} |
| 979 |
}; |
| 980 |
|
| 981 |
Blockly.JavaScript['read_ADS1115'] = function(block) { |
| 982 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 983 |
var code = 'get_sensor(\'ADS1115\',\''+dropdown_channel+'\')'; |
| 984 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 985 |
}; |
| 986 |
|
| 987 |
Blockly.Python['read_ADS1115'] = function(block) { |
| 988 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 989 |
var code = 'get_sensor(\'ADS1115\',\''+dropdown_channel+'\')'; |
| 990 |
return [code, Blockly.Python.ORDER_NONE]; |
| 991 |
}; |
| 992 |
|
| 993 |
|
| 994 |
Blockly.Blocks['read_HMC5883L'] = { |
| 995 |
init: function() { |
| 996 |
this.appendDummyInput() |
| 997 |
.appendField("Read HMC5883L") |
| 998 |
.appendField(new Blockly.FieldImage("media/MAGNETOMETER.png", 20, 20, { alt: "*", flipRtl: "FALSE" })) |
| 999 |
.appendField(new Blockly.FieldDropdown([["Hx","0"], ["Hy","1"], ["Hz","2"]]), "CHANNEL"); |
| 1000 |
this.setOutput(true, null); |
| 1001 |
this.setColour(230); |
| 1002 |
this.setTooltip(""); |
| 1003 |
this.setHelpUrl(""); |
| 1004 |
} |
| 1005 |
}; |
| 1006 |
|
| 1007 |
Blockly.JavaScript['read_HMC5883L'] = function(block) { |
| 1008 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1009 |
var code = 'get_sensor(\'HMC5883L\',\''+dropdown_channel+'\')'; |
| 1010 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 1011 |
}; |
| 1012 |
|
| 1013 |
Blockly.Python['read_HMC5883L'] = function(block) { |
| 1014 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1015 |
var code = 'get_sensor(\'HMC5883L\',\''+dropdown_channel+'\')'; |
| 1016 |
return [code, Blockly.Python.ORDER_NONE]; |
| 1017 |
}; |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
Blockly.Blocks['read_QMC5883L'] = { |
| 1022 |
init: function() { |
| 1023 |
this.appendDummyInput() |
| 1024 |
.appendField("Read QMC5883L") |
| 1025 |
.appendField(new Blockly.FieldImage("media/MAGNETOMETER.png", 20, 20, { alt: "*", flipRtl: "FALSE" })) |
| 1026 |
.appendField(new Blockly.FieldDropdown([["Hx","0"], ["Hy","1"], ["Hz","2"], ["Abs","3"]]), "CHANNEL"); |
| 1027 |
this.setOutput(true, null); |
| 1028 |
this.setColour(230); |
| 1029 |
this.setTooltip(""); |
| 1030 |
this.setHelpUrl(""); |
| 1031 |
} |
| 1032 |
}; |
| 1033 |
|
| 1034 |
Blockly.JavaScript['read_QMC5883L'] = function(block) { |
| 1035 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1036 |
var code = 'get_sensor(\'QMC5883L\',\''+dropdown_channel+'\')'; |
| 1037 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 1038 |
}; |
| 1039 |
|
| 1040 |
Blockly.Python['read_QMC5883L'] = function(block) { |
| 1041 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1042 |
var code = 'get_sensor(\'QMC5883L\',\''+dropdown_channel+'\')'; |
| 1043 |
return [code, Blockly.Python.ORDER_NONE]; |
| 1044 |
}; |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
Blockly.Blocks['set_PCA9685'] = { |
| 1049 |
init: function() { |
| 1050 |
this.appendValueInput("ANGLE") |
| 1051 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 1052 |
.appendField("SERVO(PCA9685) ") |
| 1053 |
.appendField(new Blockly.FieldDropdown([["1 Angle","1"], ["2 Angle","2"], ["3 Angle","3"], ["4 Angle","4"], ["5 Angle","5"], ["6 Angle","6"]]), "CHANNEL"); |
| 1054 |
this.setInputsInline(false); |
| 1055 |
this.setPreviousStatement(true, null); |
| 1056 |
this.setNextStatement(true, null); |
| 1057 |
this.setColour(230); |
| 1058 |
this.setTooltip("Set Angle on servo motor via PCA9685 module"); |
| 1059 |
this.setHelpUrl(""); |
| 1060 |
} |
| 1061 |
}; |
| 1062 |
|
| 1063 |
|
| 1064 |
Blockly.JavaScript['set_PCA9685'] = function(block) { |
| 1065 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1066 |
var value_angle = Blockly.JavaScript.valueToCode(block, 'ANGLE', Blockly.JavaScript.ORDER_NONE); |
| 1067 |
var code = 'set_PCA9685(\''+dropdown_channel+'\',' + value_angle+ ');\n'; |
| 1068 |
return code; |
| 1069 |
}; |
| 1070 |
|
| 1071 |
|
| 1072 |
Blockly.Python['set_PCA9685'] = function(block) { |
| 1073 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1074 |
var value_angle = Blockly.JavaScript.valueToCode(block, 'ANGLE', Blockly.Python.ORDER_NONE); |
| 1075 |
var code = 'set_PCA9685(\''+dropdown_channel+'\',' + value_angle+ ')\n'; |
| 1076 |
|
| 1077 |
return code; |
| 1078 |
}; |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
Blockly.Blocks['set_AD9833'] = { |
| 1083 |
init: function() { |
| 1084 |
this.appendValueInput("FREQ") |
| 1085 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 1086 |
.appendField("Set AD9833 ") |
| 1087 |
.appendField(new Blockly.FieldDropdown([["CS1","1"], ["CS2","2"]]), "CHANNEL") |
| 1088 |
.appendField("Frequency "); |
| 1089 |
this.setInputsInline(false); |
| 1090 |
this.setPreviousStatement(true, null); |
| 1091 |
this.setNextStatement(true, null); |
| 1092 |
this.setColour(230); |
| 1093 |
this.setTooltip("Set dual AD9833 module frequency"); |
| 1094 |
this.setHelpUrl(""); |
| 1095 |
} |
| 1096 |
}; |
| 1097 |
|
| 1098 |
|
| 1099 |
Blockly.JavaScript['set_AD9833'] = function(block) { |
| 1100 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1101 |
var freq = Blockly.JavaScript.valueToCode(block, 'FREQ', Blockly.JavaScript.ORDER_NONE); |
| 1102 |
var code = 'set_dual_AD9833('+dropdown_channel+',' + freq+ ');\n'; |
| 1103 |
return code; |
| 1104 |
}; |
| 1105 |
|
| 1106 |
|
| 1107 |
Blockly.Python['set_AD9833'] = function(block) { |
| 1108 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1109 |
var freq = Blockly.JavaScript.valueToCode(block, 'FREQ', Blockly.Python.ORDER_NONE); |
| 1110 |
var code = 'set_dual_AD9833('+dropdown_channel+',' + freq+ ')\n'; |
| 1111 |
|
| 1112 |
return code; |
| 1113 |
}; |
| 1114 |
|
| 1115 |
|
| 1116 |
Blockly.Blocks['ws2812_rgb'] = { |
| 1117 |
init: function() { |
| 1118 |
this.appendDummyInput() |
| 1119 |
.appendField("WS2812 Controller") |
| 1120 |
this.appendDummyInput() |
| 1121 |
.appendField("CE 6250 IP:") |
| 1122 |
.appendField(new Blockly.FieldTextInput("http://44.44.44.44"), "IP") |
| 1123 |
.setAlign(Blockly.ALIGN_RIGHT); |
| 1124 |
this.appendValueInput("R") |
| 1125 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 1126 |
.appendField("RED:"); |
| 1127 |
this.appendValueInput("G") |
| 1128 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 1129 |
.appendField("GREEN:"); |
| 1130 |
this.appendValueInput("B") |
| 1131 |
.setAlign(Blockly.ALIGN_RIGHT) |
| 1132 |
.appendField("BLUE:"); |
| 1133 |
this.setInputsInline(false); |
| 1134 |
this.setPreviousStatement(true, null); |
| 1135 |
this.setNextStatement(true, null); |
| 1136 |
this.setColour(230); |
| 1137 |
this.setTooltip("set R G B"); |
| 1138 |
this.setHelpUrl(""); |
| 1139 |
} |
| 1140 |
}; |
| 1141 |
|
| 1142 |
|
| 1143 |
Blockly.JavaScript['ws2812_rgb'] = function(block) { |
| 1144 |
var ip = block.getFieldValue('IP'); |
| 1145 |
var R = Blockly.JavaScript.valueToCode(block, 'R', Blockly.JavaScript.ORDER_NONE); |
| 1146 |
var G = Blockly.JavaScript.valueToCode(block, 'G', Blockly.JavaScript.ORDER_NONE); |
| 1147 |
var B = Blockly.JavaScript.valueToCode(block, 'B', Blockly.JavaScript.ORDER_NONE); |
| 1148 |
var name = block.getFieldValue('PLOTNAME'); |
| 1149 |
var code = 'sleep(0.001);\n'+'ws2812_rgb(\''+ip+'\','+R+','+G+','+B+');\n'; |
| 1150 |
console.log(code); |
| 1151 |
|
| 1152 |
return code; |
| 1153 |
}; |
| 1154 |
|
| 1155 |
|
| 1156 |
Blockly.Python['ws2812_rgb'] = function(block) { |
| 1157 |
var code = '#unimplemented\n'; |
| 1158 |
return code; |
| 1159 |
}; |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
function getSensorList(){ |
| 1167 |
fetch(`/get_reg/${channel}`) |
| 1168 |
.then(response => response.json()) |
| 1169 |
.then(data => { |
| 1170 |
console.log('got following sensors',data); |
| 1171 |
allsensors = JSON.parse(data.sensors); |
| 1172 |
}) |
| 1173 |
.catch(error => { |
| 1174 |
console.error('Error fetching sensors:', error); |
| 1175 |
}); |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
Blockly.Blocks['read_ph_meter'] = { |
| 1186 |
init: function() { |
| 1187 |
this.appendDummyInput() |
| 1188 |
.appendField("Analog pH Meter") |
| 1189 |
.appendField(new Blockly.FieldDropdown([["A1","A1"], ["A2","A2"]]), "CHANNEL"); |
| 1190 |
this.appendDummyInput() |
| 1191 |
.appendField("Slope: ") |
| 1192 |
.appendField(new Blockly.FieldNumber(0), "SLOPE"); |
| 1193 |
this.appendDummyInput() |
| 1194 |
.appendField("Offset: ") |
| 1195 |
.appendField(new Blockly.FieldNumber(0), "OFFSET"); |
| 1196 |
this.setOutput(true, null); |
| 1197 |
this.setColour(230); |
| 1198 |
this.setTooltip(""); |
| 1199 |
this.setHelpUrl(""); |
| 1200 |
} |
| 1201 |
}; |
| 1202 |
Blockly.JavaScript['read_ph_meter'] = function(block) { |
| 1203 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1204 |
var slope = Number(block.getFieldValue('SLOPE')); |
| 1205 |
var offset = Number(block.getFieldValue('OFFSET')); |
| 1206 |
var code = slope+"*get_voltage('"+dropdown_channel+"')+"+offset; |
| 1207 |
return [code, Blockly.JavaScript.ORDER_NONE]; |
| 1208 |
}; |
| 1209 |
|
| 1210 |
Blockly.Python['read_ph_meter'] = function(block) { |
| 1211 |
var dropdown_channel = block.getFieldValue('CHANNEL'); |
| 1212 |
var slope = Number(block.getFieldValue('SLOPE')); |
| 1213 |
var offset = Number(block.getFieldValue('OFFSET')); |
| 1214 |
var code = slope+"*get_voltage('"+dropdown_channel+"')+"+offset; |
| 1215 |
return [code, Blockly.Python.ORDER_NONE]; |
| 1216 |
}; |
| 1217 |
|
| 1218 |
function initSensors(interpreter, scope) { |
| 1219 |
|
| 1220 |
fetch(`/get_all_sensors`) |
| 1221 |
.then(response => response.json()) |
| 1222 |
.then(data => { |
| 1223 |
allsensors = data.sensors; |
| 1224 |
}) |
| 1225 |
.catch(error => { |
| 1226 |
console.error('Error getting sensor list:', error); |
| 1227 |
allsensors = ["[129]ADCSENS"]; |
| 1228 |
}); |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
interpreter.setProperty(scope, 'get_sensor', interpreter.createAsyncFunction( |
| 1233 |
function(sensor, param, callback) { |
| 1234 |
fetch(`/get_sensor/${sensor}/${param}`) |
| 1235 |
.then(response => response.json()) |
| 1236 |
.then(data => { |
| 1237 |
callback(myInterpreter.nativeToPseudo(data.value)); |
| 1238 |
}) |
| 1239 |
.catch(error => { |
| 1240 |
console.error('Error getting sensor value:', error); |
| 1241 |
callback(null); |
| 1242 |
}); |
| 1243 |
}) |
| 1244 |
); |
| 1245 |
|
| 1246 |
|
| 1247 |
interpreter.setProperty(scope, 'get_generic_sensor', interpreter.createAsyncFunction( |
| 1248 |
function(sensor, addr, callback) { |
| 1249 |
fetch(`/get_generic_sensor/${sensor}/${addr}`) |
| 1250 |
.then(response => response.json()) |
| 1251 |
.then(data => { |
| 1252 |
console.log(data); |
| 1253 |
callback(myInterpreter.nativeToPseudo(data.data)); |
| 1254 |
}) |
| 1255 |
.catch(error => { |
| 1256 |
console.error('Error getting generic sensor:', error); |
| 1257 |
callback(null); |
| 1258 |
}); |
| 1259 |
}) |
| 1260 |
); |
| 1261 |
|
| 1262 |
|
| 1263 |
interpreter.setProperty(scope, 'parseAndReadSensor', interpreter.createAsyncFunction( |
| 1264 |
function(descriptor, param, callback) { |
| 1265 |
const addr = descriptor.split(']')[0].substring(1); |
| 1266 |
const sensor = descriptor.split(']')[1].split('/')[0]; |
| 1267 |
fetch(`/get_generic_sensor/${sensor}/${parseInt(addr)}`) |
| 1268 |
.then(response => response.json()) |
| 1269 |
.then(data => { |
| 1270 |
callback(myInterpreter.nativeToPseudo(data.data[param])); |
| 1271 |
}) |
| 1272 |
.catch(error => { |
| 1273 |
console.error('Error parsing and reading sensor:', error); |
| 1274 |
callback(null); |
| 1275 |
}); |
| 1276 |
}) |
| 1277 |
); |
| 1278 |
|
| 1279 |
|
| 1280 |
interpreter.setProperty(scope, 'parseAndReadSensorVals', interpreter.createAsyncFunction( |
| 1281 |
function(descriptor, param, callback) { |
| 1282 |
const addr = descriptor.split(']')[0].substring(1); |
| 1283 |
const sensor = descriptor.split(']')[1].split('/')[0]; |
| 1284 |
fetch(`/get_generic_sensor/${sensor}/${parseInt(addr)}`) |
| 1285 |
.then(response => response.json()) |
| 1286 |
.then(data => { |
| 1287 |
callback(myInterpreter.nativeToPseudo(data.data)); |
| 1288 |
}) |
| 1289 |
.catch(error => { |
| 1290 |
console.error('Error parsing and reading sensor values:', error); |
| 1291 |
callback(null); |
| 1292 |
}); |
| 1293 |
}) |
| 1294 |
); |
| 1295 |
|
| 1296 |
|
| 1297 |
interpreter.setProperty(scope, 'get_generic_sensor_param', interpreter.createAsyncFunction( |
| 1298 |
function(sensor, addr, param, callback) { |
| 1299 |
fetch(`/get_generic_sensor_param/${sensor}/${addr}/${param}`) |
| 1300 |
.then(response => response.json()) |
| 1301 |
.then(data => { |
| 1302 |
callback(myInterpreter.nativeToPseudo(data)); |
| 1303 |
}) |
| 1304 |
.catch(error => { |
| 1305 |
console.error('Error getting generic sensor parameter:', error); |
| 1306 |
callback(null); |
| 1307 |
}); |
| 1308 |
}) |
| 1309 |
); |
| 1310 |
|
| 1311 |
|
| 1312 |
interpreter.setProperty(scope, 'scanI2C', interpreter.createAsyncFunction( |
| 1313 |
function(callback) { |
| 1314 |
fetch('/scanI2C') |
| 1315 |
.then(response => response.json()) |
| 1316 |
.then(data => { |
| 1317 |
callback(myInterpreter.nativeToPseudo(data)); |
| 1318 |
}) |
| 1319 |
.catch(error => { |
| 1320 |
console.error('Error scanning I2C:', error); |
| 1321 |
callback(null); |
| 1322 |
}); |
| 1323 |
}) |
| 1324 |
); |
| 1325 |
|
| 1326 |
|
| 1327 |
interpreter.setProperty(scope, 'scanI2CString', interpreter.createAsyncFunction( |
| 1328 |
function(callback) { |
| 1329 |
fetch('/scanI2CString') |
| 1330 |
.then(response => response.json()) |
| 1331 |
.then(data => { |
| 1332 |
callback(myInterpreter.nativeToPseudo(data)); |
| 1333 |
}) |
| 1334 |
.catch(error => { |
| 1335 |
console.error('Error scanning I2C string:', error); |
| 1336 |
callback(null); |
| 1337 |
}); |
| 1338 |
}) |
| 1339 |
); |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
interpreter.setProperty(scope, 'ws2812_rgb', interpreter.createNativeFunction( |
| 1345 |
function( ip,R,G, B) { |
| 1346 |
var packet ={ "body": JSON.stringify({"seg":{ "col":[[parseInt(R),parseInt(G),parseInt(B),"0"],[],[]],"pal":0}}) , "method": "POST", "mode": "cors" } ; |
| 1347 |
fetch(ip+"/json/si", packet); |
| 1348 |
}) |
| 1349 |
); |
| 1350 |
|
| 1351 |
} |
| 1352 |
|
| 1353 |
|