//* comment *//
|
|
|
|
Copyright (C) 2015, 2016 Walter Bender
|
|
Copyright (C) 2015 Martin Abente Lahaye
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//* globals *//
|
|
|
|
var RODI_BASE_URL = 'http://192.168.4.1:1234';
|
|
|
|
var rodiSensorsCache = null;
|
|
var rodiSensorsValidFor = 100;
|
|
var rodiSensorsCacheExpires = 0;
|
|
|
|
var rodiLightSensorCache = null;
|
|
var rodiLightSensorValidFor = 100;
|
|
var rodiLightSensorCacheExpires = 0;
|
|
|
|
var rodiMoveStatus = _('stop');
|
|
|
|
var rodiDistanceCache = null;
|
|
var rodiDistanceValidFor = 100;
|
|
var rodiDistanceCacheExpires = 0;
|
|
|
|
var doRodiGetDistance = function (logo, blk) {
|
|
if (logo.inStatusMatrix) {
|
|
logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
|
|
} else {
|
|
if (Date.now() > rodiDistanceCacheExpires) {
|
|
|
|
var apiUrl = RODI_BASE_URL + '/5/';
|
|
var distance = doSyncCall(logo, blk, apiUrl);
|
|
|
|
if (distance != null) {
|
|
rodiDistanceCacheExpires = Date.now() + rodiDistanceValidFor;
|
|
rodiDistanceCache = distance;
|
|
return distance;
|
|
} else {
|
|
return 100;
|
|
}
|
|
} else {
|
|
return rodiDistanceCache;
|
|
}
|
|
}
|
|
};
|
|
|
|
var doRodiGetMoveStatus = function (logo, blk) {
|
|
if (logo.inStatusMatrix) {
|
|
logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
|
|
} else {
|
|
return rodiMoveStatus;
|
|
}
|
|
};
|
|
|
|
var doRodiMove = function (logo, blk, left, right) {
|
|
var apiUrl = RODI_BASE_URL + '/3/' + left + '/' + right + '/';
|
|
if (left === right) {
|
|
if (left < 0) {
|
|
rodiMoveStatus = _('back');
|
|
} else if (left > 0) {
|
|
rodiMoveStatus = _('forward');
|
|
} else {
|
|
rodiMoveStatus = _('stop');
|
|
}
|
|
} else {
|
|
if (left > right) {
|
|
rodiMoveStatus = _('right');
|
|
} else {
|
|
rodiMoveStatus = _('left');
|
|
}
|
|
}
|
|
doAsyncCall(logo, blk, apiUrl)
|
|
};
|
|
|
|
var doRodiGetSensor = function (logo, blk, index) {
|
|
if (logo.inStatusMatrix) {
|
|
logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
|
|
} else {
|
|
if (Date.now() > rodiSensorsCacheExpires) {
|
|
|
|
var apiUrl = RODI_BASE_URL + '/2/';
|
|
var sensors = doSyncCall(logo, blk, apiUrl);
|
|
|
|
if (sensors != null) {
|
|
rodiSensorsCacheExpires = Date.now() + rodiSensorsValidFor;
|
|
rodiSensorsCache = sensors;
|
|
return sensors[index];
|
|
} else {
|
|
return -1;
|
|
}
|
|
|
|
} else {
|
|
return rodiSensorsCache[index];
|
|
}
|
|
}
|
|
};
|
|
|
|
var doRodiGetLightSensor = function (logo, blk) {
|
|
if (logo.inStatusMatrix) {
|
|
logo.statusFields.push([blk, logo.blocks.blockList[blk].name]);
|
|
} else {
|
|
if (Date.now() > rodiLightSensorCacheExpires) {
|
|
|
|
var apiUrl = RODI_BASE_URL + '/7/';
|
|
var sensor = doSyncCall(logo, blk, apiUrl);
|
|
|
|
if (sensor != null) {
|
|
rodiLightSensorCacheExpires = Date.now() + rodiLightSensorValidFor;
|
|
rodiLightSensorCache = sensor;
|
|
return sensor;
|
|
} else {
|
|
return -1;
|
|
}
|
|
|
|
} else {
|
|
return rodiLightSensorCache;
|
|
}
|
|
}
|
|
};
|
|
|
|
var doAsyncCall = function (logo, blk, url){
|
|
console.log(url);
|
|
|
|
try {
|
|
var request = new XMLHttpRequest();
|
|
request.open('GET', url, true);
|
|
request.send(null);
|
|
} catch (e) {
|
|
if (logo != null) {
|
|
logo.errorMsg(e, blk);
|
|
}
|
|
console.log(e);
|
|
}
|
|
};
|
|
|
|
var doSyncCall = function (logo, blk, url) {
|
|
console.log(url);
|
|
|
|
var request = new XMLHttpRequest();
|
|
try {
|
|
request.open('GET', url, false);
|
|
request.send(null);
|
|
return JSON.parse(request.responseText);
|
|
} catch (e) {
|
|
if (logo != null) {
|
|
logo.errorMsg(e, blk);
|
|
}
|
|
console.log(e);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
function doRodiStop(){
|
|
doRodiMove(null, null, 0, 0);
|
|
};
|
|
|
|
blocks.logo.stage.addEventListener('__STOP__', doRodiStop, false);
|
|
|
|
//* block:rodiblink *//
|
|
|
|
var rodiBlink = new ProtoBlock('rodiblink');
|
|
rodiBlink.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodiblink'] = rodiBlink;
|
|
rodiBlink.staticLabels.push(_('blink'));
|
|
rodiBlink.adjustWidthToLabel();
|
|
rodiBlink.oneArgBlock();
|
|
rodiBlink.defaults.push(1000);
|
|
|
|
//* flow:rodiblink *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
var conns = block.connections;
|
|
|
|
var rate = logo.parseArg(logo, turtle, conns[1]);
|
|
var apiUrl = RODI_BASE_URL + '/1/' + rate + '/';
|
|
doAsyncCall(logo, blk, apiUrl);
|
|
|
|
//* block:rodising *//
|
|
|
|
var rodiSing = new ProtoBlock('rodising');
|
|
rodiSing.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodising'] = rodiSing;
|
|
rodiSing.staticLabels.push(_('play'));
|
|
rodiSing.staticLabels.push(_('note'));
|
|
rodiSing.staticLabels.push(_('duration'));
|
|
rodiSing.adjustWidthToLabel();
|
|
rodiSing.twoArgBlock();
|
|
rodiSing.dockTypes[1] = 'numberin';
|
|
rodiSing.dockTypes[2] = 'numberin';
|
|
rodiSing.defaults.push(31);
|
|
rodiSing.defaults.push(250);
|
|
|
|
//* flow:rodising *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
var conns = block.connections;
|
|
|
|
var note = logo.parseArg(logo, turtle, conns[1]);
|
|
var duration = logo.parseArg(logo, turtle, conns[2]);
|
|
var apiUrl = RODI_BASE_URL + '/4/' + note + '/' + duration + '/';
|
|
doAsyncCall(logo, blk, apiUrl);
|
|
|
|
//* block:rodipixel *//
|
|
|
|
var rodiPixel = new ProtoBlock('rodipixel');
|
|
rodiPixel.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodipixel'] = rodiPixel;
|
|
rodiPixel.staticLabels.push(_('pixel color'));
|
|
rodiPixel.staticLabels.push(_('red'));
|
|
rodiPixel.staticLabels.push(_('green'));
|
|
rodiPixel.staticLabels.push(_('blue'));
|
|
rodiPixel.adjustWidthToLabel();
|
|
rodiPixel.threeArgBlock();
|
|
rodiPixel.dockTypes[1] = 'numberin';
|
|
rodiPixel.dockTypes[2] = 'numberin';
|
|
rodiPixel.dockTypes[3] = 'numberin';
|
|
rodiPixel.defaults.push(0);
|
|
rodiPixel.defaults.push(0);
|
|
rodiPixel.defaults.push(0);
|
|
|
|
//* flow:rodipixel *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
var conns = block.connections;
|
|
|
|
var red = logo.parseArg(logo, turtle, conns[1]);
|
|
var green = logo.parseArg(logo, turtle, conns[2]);
|
|
var blue = logo.parseArg(logo, turtle, conns[3]);
|
|
|
|
var apiUrl = RODI_BASE_URL + '/6/' + red + '/' + green + '/' + blue + '/';
|
|
doAsyncCall(logo, blk, apiUrl);
|
|
|
|
//* block:rodiled *//
|
|
|
|
var rodiLed = new ProtoBlock('rodiled');
|
|
rodiLed.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodiled'] = rodiLed;
|
|
rodiLed.staticLabels.push(_('led'));
|
|
rodiLed.adjustWidthToLabel();
|
|
rodiLed.oneArgBlock();
|
|
rodiLed.defaults.push(1);
|
|
|
|
//* flow:rodiled *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
var conns = block.connections;
|
|
|
|
var state = logo.parseArg(logo, turtle, conns[1]);
|
|
var apiUrl = RODI_BASE_URL + '/8/' + state + '/';
|
|
doAsyncCall(logo, blk, apiUrl);
|
|
|
|
//* block:rodisenselight *//
|
|
|
|
var rodiSenseLight = new ProtoBlock('rodisenselight');
|
|
rodiSenseLight.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodisenselight'] = rodiSenseLight;
|
|
rodiSenseLight.staticLabels.push(_('light intensity'));
|
|
rodiSenseLight.adjustWidthToLabel();
|
|
rodiSenseLight.parameterBlock();
|
|
|
|
//* arg:rodisenselight *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
block.value = doRodiGetLightSensor(logo, blk);
|
|
|
|
//* block:rodisenseleft *//
|
|
|
|
var rodiSenseLeft = new ProtoBlock('rodisenseleft');
|
|
rodiSenseLeft.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodisenseleft'] = rodiSenseLeft;
|
|
rodiSenseLeft.staticLabels.push(_('infrared light (left)'));
|
|
rodiSenseLeft.adjustWidthToLabel();
|
|
rodiSenseLeft.parameterBlock();
|
|
|
|
//* arg:rodisenseleft *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
block.value = doRodiGetSensor(logo, blk, 0);
|
|
|
|
//* block:rodisenseright *//
|
|
|
|
var rodiSenseRight = new ProtoBlock('rodisenseright');
|
|
rodiSenseRight.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodisenseright'] = rodiSenseRight;
|
|
rodiSenseRight.staticLabels.push(_('infrared light (right)'));
|
|
rodiSenseRight.adjustWidthToLabel();
|
|
rodiSenseRight.parameterBlock();
|
|
|
|
//* arg:rodisenseright *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
block.value = doRodiGetSensor(logo, blk, 1);
|
|
|
|
//* block:rodisee *//
|
|
|
|
var rodiSee = new ProtoBlock('rodisee');
|
|
rodiSee.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodisee'] = rodiSee;
|
|
rodiSee.staticLabels.push(_('distance'));
|
|
rodiSee.adjustWidthToLabel();
|
|
rodiSee.parameterBlock();
|
|
|
|
//* arg:rodisee *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
block.value = doRodiGetDistance(logo, blk);
|
|
|
|
//* block:rodimovestatus *//
|
|
|
|
var rodiMoveStatus = new ProtoBlock('rodimovestatus');
|
|
rodiMoveStatus.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodimovestatus'] = rodiMoveStatus;
|
|
rodiMoveStatus.staticLabels.push(_('status'));
|
|
rodiMoveStatus.adjustWidthToLabel();
|
|
rodiMoveStatus.parameterBlock();
|
|
|
|
//* arg:rodimovestatus *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
block.value = doRodiGetMoveStatus(logo, blk);
|
|
|
|
//* block:rodimove *//
|
|
|
|
var rodiMove = new ProtoBlock('rodimove');
|
|
rodiMove.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodimove'] = rodiMove;
|
|
rodiMove.staticLabels.push(_('move'));
|
|
rodiMove.staticLabels.push(_('left'));
|
|
rodiMove.staticLabels.push(_('right'));
|
|
rodiMove.adjustWidthToLabel();
|
|
rodiMove.twoArgBlock();
|
|
rodiMove.dockTypes[1] = 'numberin';
|
|
rodiMove.dockTypes[2] = 'numberin';
|
|
rodiMove.defaults.push(100);
|
|
rodiMove.defaults.push(100);
|
|
|
|
//* flow:rodimove *//
|
|
|
|
var block = logo.blocks.blockList[blk];
|
|
var conns = block.connections;
|
|
|
|
var left = logo.parseArg(logo, turtle, conns[1]);
|
|
var right = logo.parseArg(logo, turtle, conns[2]);
|
|
doRodiMove(logo, blk, left, right);
|
|
|
|
//* block:rodileft *//
|
|
|
|
var rodiLeft = new ProtoBlock('rodileft');
|
|
rodiLeft.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodileft'] = rodiLeft;
|
|
rodiLeft.staticLabels.push(_('left'));
|
|
rodiLeft.adjustWidthToLabel();
|
|
rodiLeft.zeroArgBlock();
|
|
|
|
//* flow:rodileft *//
|
|
|
|
doRodiMove(logo, blk, -100, 100);
|
|
|
|
//* block:rodiright *//
|
|
|
|
var rodiRight = new ProtoBlock('rodiright');
|
|
rodiRight.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodiright'] = rodiRight;
|
|
rodiRight.staticLabels.push(_('right'));
|
|
rodiRight.adjustWidthToLabel();
|
|
rodiRight.zeroArgBlock();
|
|
|
|
//* flow:rodiright *//
|
|
|
|
doRodiMove(logo, blk, 100, -100);
|
|
|
|
//* block:rodiback *//
|
|
|
|
var rodiBack = new ProtoBlock('rodiback');
|
|
rodiBack.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodiback'] = rodiBack;
|
|
rodiBack.staticLabels.push(_('back'));
|
|
rodiBack.adjustWidthToLabel();
|
|
rodiBack.zeroArgBlock();
|
|
|
|
//* flow:rodiback *//
|
|
|
|
doRodiMove(logo, blk, -100, -100);
|
|
|
|
//* block:rodiforward *//
|
|
|
|
var rodiForward = new ProtoBlock('rodiforward');
|
|
rodiForward.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodiforward'] = rodiForward;
|
|
rodiForward.staticLabels.push(_('forward'));
|
|
rodiForward.adjustWidthToLabel();
|
|
rodiForward.zeroArgBlock();
|
|
|
|
//* flow:rodiforward *//
|
|
|
|
doRodiMove(logo, blk, 100, 100);
|
|
|
|
//* block:rodistop *//
|
|
|
|
var rodiStop = new ProtoBlock('rodistop');
|
|
rodiStop.palette = palettes.dict['rodi'];
|
|
blocks.protoBlockDict['rodistop'] = rodiStop;
|
|
rodiStop.staticLabels.push(_('stop'));
|
|
rodiStop.adjustWidthToLabel();
|
|
rodiStop.zeroArgBlock();
|
|
|
|
//* flow:rodistop *//
|
|
|
|
doRodiMove(logo, blk, 0, 0);
|
|
|
|
//* palette-icon:rodi *//
|
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" id="svg2" version="1.1" height="55" width="55"> <path d="M 35.516203,8.5290201 C 22.94617,8.4975021 20.536671,8.9380889 15.981939,12.108873 8.040645,17.63697 5.5333178,28.725893 10.310252,37.186933 c 5.057023,8.956964 16.647266,12.342556 25.705513,7.50901 2.79002,-1.488868 6.53595,-5.248313 8.015725,-8.041727 1.480563,-2.794869 2.377437,-4.759722 2.678979,-10.981156 l 0,-17.1440399 -11.194266,0 z M 26.938244,13.920134 A 13.657546,13.657546 0 0 1 40.596015,27.577846 13.657546,13.657546 0 0 1 26.938244,41.235617 13.657546,13.657546 0 0 1 13.280472,27.577846 13.657546,13.657546 0 0 1 26.938244,13.920134 Z M 26.59609,22.93256 a 5.1346998,5.1346998 0 0 0 -5.13424,5.134241 5.1346998,5.1346998 0 0 0 5.13424,5.135393 5.1346998,5.1346998 0 0 0 5.134242,-5.135393 5.1346998,5.1346998 0 0 0 -5.134242,-5.134241 z" style="fill:#009dd8;fill-opacity:1" /> </svg>
|
|
|
|
//* palette-fill:rodi *// #6a8df6
|
|
//* palette-stroke:rodi *// #5a7de6
|
|
//* palette-highlight:rodi *// #ced8f8
|
|
//* palette-stroke-highlight:rodi *// #000000
|