Created
December 10, 2016 11:33
-
-
Save darkwave/704c2150e894993f08374d3c39e6f94c to your computer and use it in GitHub Desktop.
mBot reloaded :-)
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
<html> | |
<head> | |
<script src="blockly_compressed_horizontal.js"></script> | |
<script src="javascript_compressed.js"></script> | |
<script src="blocks_compressed.js"></script> | |
<script src="msg/js/en.js"></script> | |
<script src="blocks_horizontal/control.js"></script> | |
<script src="blocks_horizontal/event.js"></script> | |
<script src="blocks_horizontal/wedo.js"></script> | |
<script src="blocks_horizontal/default_toolbox.js"></script> | |
</head> | |
<body> | |
<div id="blocklyDiv"></div> | |
</body> | |
<script> | |
var workspace = Blockly.inject('blocklyDiv', { | |
comments: false, | |
disable: false, | |
collapse: false, | |
media: 'media/', | |
readOnly: false, | |
scrollbars: true, | |
toolbox: Blockly.Blocks.defaultToolbox, | |
trashcan: true, | |
grid: {spacing: 16, | |
length: 1, | |
colour: '#2C344A', | |
snap: false | |
}, | |
zoom: { | |
controls: true, | |
wheel: true, | |
startScale: 1.0, | |
maxScale: 4, | |
minScale: 0.25, | |
scaleSpeed: 1.1 | |
}, | |
colours: { | |
workspace: '#334771', | |
flyout: '#283856', | |
scrollbar: '#24324D', | |
scrollbarHover: '#0C111A', | |
insertionMarker: '#FFFFFF', | |
insertionMarkerOpacity: 0.3, | |
fieldShadow: 'rgba(255, 255, 255, 0.3)', | |
dragShadowOpacity: 0.6 | |
} | |
}); | |
Blockly.JavaScript['event_whenflagclicked'] = function(block) { | |
// TODO: Assemble JavaScript into code variable. | |
var code = '...;\n'; | |
return code; | |
}; | |
Blockly.JavaScript['dropdown_wedo_setcolor'] = function(block) { | |
var value = block.getFieldValue("CHOICE"); | |
var code = value; | |
return code; | |
}; | |
//wedo_setcolor | |
var tweenCounter = 0; | |
Blockly.JavaScript['wedo_setcolor'] = function(block) { | |
var value = Blockly.JavaScript.statementToCode(block, "CHOICE").trim();//, Blockly.JavaScript.ORDER_ADDITION); | |
//var code = 'setLed(\'' + value + '\');' + "\n"; | |
var newR = 0;var newG = 0;var newB = 0; | |
console.log(value); | |
if (value === 'yellow') { | |
newR = 255; | |
newG = 100; | |
newB = 0; | |
} else if (value === 'mystery') { | |
newR = Math.floor(Math.random() * 255); | |
newG = Math.floor(Math.random() * 255); | |
newB = Math.floor(Math.random() * 255); | |
} else if (value === 'coral') { | |
newR = 255; | |
newG = 127; | |
newB = 80; | |
} else if (value === 'magenta') { | |
newR = 255; | |
newG = 0; | |
newB = 255; | |
} | |
tweenCounter++; | |
code = "var tween" + tweenCounter +" = new TWEEN.Tween({r: 0, g: 0, b: 0}).to({ r: "+newR+", g: "+ newG + ", b: " + newB + " }, 1000).onUpdate(function() { setLed(this.r,this.g,this.b); }); lastTween.chain(tween" + tweenCounter +"); lastTween = tween" + tweenCounter +";"; | |
return code; | |
} | |
function runCode() { | |
var code = Blockly.JavaScript.workspaceToCode(workspace); | |
tweenCounter = 0; | |
var colors = { r: 0, g: 0, b: 0}; | |
TWEEN.removeAll(); | |
preCode = "var firstTween = new TWEEN.Tween(); var lastTween = firstTween;"; | |
postCode = "firstTween.start();" | |
eval(preCode + code + postCode); | |
} | |
var TWEEN = require('tween.js'); | |
requestAnimationFrame(animate); | |
function animate(time) { | |
requestAnimationFrame(animate); | |
TWEEN.update(time); | |
} | |
const {remote} = require('electron') | |
const {Menu, MenuItem} = remote | |
const menu = Menu.getApplicationMenu() | |
menu.append(new MenuItem({label: 'RUN', click() { runCode() }})) | |
Menu.setApplicationMenu(menu) | |
var HID = require('node-hid'); | |
var TWEEN = require('tween.js'); | |
var devices = HID.devices(); | |
//console.log(devices); | |
var currentPath = ""; | |
for (i=0; i < devices.length; i++) { | |
document.write(devices[i].product + devices[i].path); | |
currentPath = devices[i].path; | |
} | |
var device = new HID.HID(currentPath);//"1046", "65535"); | |
//console.log(device); | |
device.on("data", function(data) { | |
//console.log("data", data); | |
//device.write([0, 7, 0xff, 0x55, 0x04, 0x60, 0x01, 0x11, 0x02]); | |
}); | |
device.on("error", function(error) { | |
console.log("error:" + error); | |
}); | |
//following Orion Protocol http://learn.makeblock.com/makeblock-orion-protocol/ | |
//var changeLED = [0, 12, 0xff, 0x55, 0x09, 0x00, 0x02, 0x08, 0x07, 0x02, 0x00, 255, 100, 0]; | |
function setLed(r,g,b) { | |
var currentColor = [0, 12, 0xff, 0x55, 0x09, 0x00, 0x02, 0x08, 0x07, 0x02, 0x00, r, g, b]; | |
device.write(currentColor); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment