Enabled Arduino code generation in:

  new "code_duino" app, based on "code" to test changes
  test/playground
  test/generator/index
parent 3381393d
/**
* Blockly Apps: Code
*
* Copyright 2012 Google Inc.
* https://blockly.googlecode.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview JavaScript for Blockly's Code application.
* @author fraser@google.com (Neil Fraser)
*/
// Supported languages.
BlocklyApps.LANGUAGES =
['en'];
BlocklyApps.LANG = BlocklyApps.getLang();
document.write('<script type="text/javascript" src="generated/' +
BlocklyApps.LANG + '.js"></script>\n');
/**
* Create a namespace for the application.
*/
var Code = {};
/**
* List of tab names.
* @private
*/
Code.TABS_ = ['blocks', 'arduino', 'javascript', 'python', 'dart', 'xml'];
Code.selected = 'blocks';
/**
* Switch the visible pane when a tab is clicked.
* @param {string} clickedName Name of tab clicked.
*/
Code.tabClick = function(clickedName) {
// If the XML tab was open, save and render the content.
if (document.getElementById('tab_xml').className == 'tabon') {
var xmlTextarea = document.getElementById('content_xml');
var xmlText = xmlTextarea.value;
var xmlDom = null;
try {
xmlDom = Blockly.Xml.textToDom(xmlText);
} catch (e) {
var q =
window.confirm(BlocklyApps.getMsg('Code_badXml').replace('%1', e));
if (!q) {
// Leave the user on the XML tab.
return;
}
}
if (xmlDom) {
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom);
}
}
// Deselect all tabs and hide all panes.
for (var i = 0; i < Code.TABS_.length; i++) {
var name = Code.TABS_[i];
document.getElementById('tab_' + name).className = 'taboff';
document.getElementById('content_' + name).style.visibility = 'hidden';
}
// Select the active tab.
Code.selected = clickedName;
document.getElementById('tab_' + clickedName).className = 'tabon';
// Show the selected pane.
document.getElementById('content_' + clickedName).style.visibility =
'visible';
Code.renderContent();
Blockly.fireUiEvent(window, 'resize');
};
/**
* Populate the currently selected pane with content generated from the blocks.
*/
Code.renderContent = function() {
var content = document.getElementById('content_' + Code.selected);
// Initialize the pane.
if (content.id == 'content_xml') {
var xmlTextarea = document.getElementById('content_xml');
var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
xmlTextarea.value = xmlText;
xmlTextarea.focus();
} else if (content.id == 'content_arduino') {
var code = Blockly.Arduino.workspaceToCode();
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
code = prettyPrintOne(code, 'js');
content.innerHTML = code;
}
} else if (content.id == 'content_javascript') {
var code = Blockly.JavaScript.workspaceToCode();
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
code = prettyPrintOne(code, 'js');
content.innerHTML = code;
}
} else if (content.id == 'content_python') {
code = Blockly.Python.workspaceToCode();
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
code = prettyPrintOne(code, 'py');
content.innerHTML = code;
}
} else if (content.id == 'content_dart') {
code = Blockly.Dart.workspaceToCode();
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
code = prettyPrintOne(code, 'dart');
content.innerHTML = code;
}
}
};
/**
* Initialize Blockly. Called on page load.
*/
Code.init = function() {
BlocklyApps.init();
var rtl = BlocklyApps.isRtl();
var container = document.getElementById('content_area');
var onresize = function(e) {
var bBox = BlocklyApps.getBBox_(container);
for (var i = 0; i < Code.TABS_.length; i++) {
var el = document.getElementById('content_' + Code.TABS_[i]);
el.style.top = bBox.y + 'px';
el.style.left = bBox.x + 'px';
// Height and width need to be set, read back, then set again to
// compensate for scrollbars.
el.style.height = bBox.height + 'px';
el.style.height = (2 * bBox.height - el.offsetHeight) + 'px';
el.style.width = bBox.width + 'px';
el.style.width = (2 * bBox.width - el.offsetWidth) + 'px';
}
// Make the 'Blocks' tab line up with the toolbox.
if (Blockly.Toolbox.width) {
document.getElementById('tab_blocks').style.minWidth =
(Blockly.Toolbox.width - 38) + 'px';
// Account for the 19 pixel margin and on each side.
}
};
window.addEventListener('resize', onresize, false);
var toolbox = document.getElementById('toolbox');
Blockly.inject(document.getElementById('content_blocks'),
{path: '../../',
rtl: rtl,
toolbox: toolbox});
// Add to reserved word list: Local variables in execution evironment (runJS)
// and the infinite loop detection function.
Blockly.JavaScript.addReservedWords('code,timeouts,checkTimeout');
BlocklyApps.loadBlocks('');
if ('BlocklyStorage' in window) {
// Hook a save function onto unload.
BlocklyStorage.backupOnUnload();
}
Code.tabClick(Code.selected);
Blockly.fireUiEvent(window, 'resize');
BlocklyApps.bindClick('trashButton',
function() {Code.discard(); Code.renderContent();});
BlocklyApps.bindClick('runButton', Code.runJS);
for (var i = 0; i < Code.TABS_.length; i++) {
var name = Code.TABS_[i];
BlocklyApps.bindClick('tab_' + name,
function(name_) {return function() {Code.tabClick(name_);};}(name));
}
// Lazy-load the syntax-highlighting.
window.setTimeout(BlocklyApps.importPrettify, 1);
};
if (window.location.pathname.match(/readonly.html$/)) {
window.addEventListener('load', BlocklyApps.initReadonly);
} else {
window.addEventListener('load', Code.init);
}
/**
* Execute the user's code.
* Just a quick and dirty eval. Catch infinite loops.
*/
Code.runJS = function() {
Blockly.JavaScript.INFINITE_LOOP_TRAP = ' checkTimeout();\n';
var timeouts = 0;
var checkTimeout = function() {
if (timeouts++ > 1000000) {
throw BlocklyApps.getMsg('Code_timeout');
}
};
var code = Blockly.JavaScript.workspaceToCode();
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
try {
eval(code);
} catch (e) {
alert(BlocklyApps.getMsg('Code_badCode').replace('%1', e));
}
};
/**
* Discard all blocks from the workspace.
*/
Code.discard = function() {
var count = Blockly.mainWorkspace.getAllBlocks().length;
if (count < 2 ||
window.confirm(BlocklyApps.getMsg('Code_discard').replace('%1', count))) {
Blockly.mainWorkspace.clear();
window.location.hash = '';
}
};
This diff is collapsed.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<title>Blockly : Code</title>
<link rel="stylesheet" type="text/css" href="../common.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="../_soy/soyutils.js"></script>
<script type="text/javascript" src="../common.js"></script>
<script type="text/javascript" src="code.js"></script>
<!--script type="text/javascript" src="/storage.js"></script-->
</head>
<body>
<script type="text/javascript">
document.write(codepage.start({}, null,
{lang: BlocklyApps.LANG,
langSrc: BlocklyApps.languagePack()}));
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<title>Blockly : Code Readonly</title>
<script type="text/javascript" src="../_soy/soyutils.js"></script>
<script type="text/javascript" src="../common.js"></script>
<script type="text/javascript" src="code.js"></script>
<style>
html, body {
background-color: #fff;
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
}
#blockly>svg {
border: none;
}
#blockly {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<script type="text/javascript">
document.write(codepage.readonly({}, null,
{langSrc: BlocklyApps.languagePack()}));
</script>
</body>
</html>
html, body {
height: 100%;
}
body {
margin: 0;
overflow: hidden;
}
h1 {
margin-left: 5px;
margin-right: 5px;
}
/* Tabs */
#tabRow>td {
border: 1px solid #ccc;
}
td.tabon {
border-bottom-color: #ddd !important;
background-color: #ddd;
padding: 5px 19px;
}
td.taboff {
cursor: pointer;
padding: 5px 19px;
}
td.taboff:hover {
background-color: #eee;
}
td.tabmin {
border-top-style: none !important;
border-left-style: none !important;
border-right-style: none !important;
}
td.tabmax {
border-top-style: none !important;
border-left-style: none !important;
border-right-style: none !important;
width: 99%;
padding-left: 10px;
padding-right: 10px;
text-align: right;
}
html[dir=rtl] td.tabmax {
text-align: left;
}
table {
border-collapse: collapse;
margin: 0;
padding: 0;
border: none;
}
td {
padding: 0;
vertical-align: top;
}
.content {
visibility: hidden;
margin: 0;
padding: 1ex;
position: absolute;
direction: ltr;
}
pre.content {
overflow: scroll;
}
#content_blocks {
padding: 0;
}
.blocklySvg {
border-top: none !important;
}
#content_xml {
resize: none;
outline: none;
border: none;
font-family: monospace;
overflow: scroll;
}
#languageMenu {
vertical-align: top;
margin-top: 15px;
margin-right: 15px;
}
/* Buttons */
button {
padding: 1px 10px;
margin: 1px 5px;
}
/* Sprited icons. */
.icon21 {
height: 21px;
width: 21px;
background-image: url(icons.png);
}
.trash {
background-position: 0px 0px;
}
.link {
background-position: -21px 0px;
}
.run {
background-position: -42px 0px;
}
This diff is collapsed.
......@@ -14,6 +14,16 @@
<script type="text/javascript" src="../../generators/javascript/colour.js"></script>
<script type="text/javascript" src="../../generators/javascript/variables.js"></script>
<script type="text/javascript" src="../../generators/javascript/procedures.js"></script>
<script type="text/javascript" src="../../generators/arduino.js"></script>
<!--script type="text/javascript" src="unittest_arduino.js"></script-->
<script type="text/javascript" src="../../generators/arduino/logic.js"></script>
<script type="text/javascript" src="../../generators/arduino/loops.js"></script>
<script type="text/javascript" src="../../generators/arduino/math.js"></script>
<script type="text/javascript" src="../../generators/arduino/text.js"></script>
<script type="text/javascript" src="../../generators/arduino/lists.js"></script>
<script type="text/javascript" src="../../generators/arduino/colour.js"></script>
<script type="text/javascript" src="../../generators/arduino/variables.js"></script>
<script type="text/javascript" src="../../generators/arduino/procedures.js"></script>
<script type="text/javascript" src="../../generators/python.js"></script>
<script type="text/javascript" src="unittest_python.js"></script>
<script type="text/javascript" src="../../generators/python/logic.js"></script>
......@@ -114,6 +124,12 @@ function toXml() {
setOutput(xmlText);
}
function toArduino() {
var code = '\'use strict\';\n\n'
code += Blockly.Arduino.workspaceToCode();
setOutput(code);
}
function toJavaScript() {
var code = '\'use strict\';\n\n'
code += Blockly.JavaScript.workspaceToCode();
......@@ -366,6 +382,7 @@ h1 {
<p>
Generate:
<input type="button" value="XML" onclick="toXml()">
<input type="button" value="Arduino" onclick="toArduino()">
<input type="button" value="JavaScript" onclick="toJavaScript()">
<input type="button" value="Python" onclick="toPython()">
<input type="button" value="Dart" onclick="toDart()">
......
......@@ -4,6 +4,15 @@
<meta charset="utf-8">
<title>Blockly Playground</title>
<script type="text/javascript" src="../blockly_uncompressed.js"></script>
<script type="text/javascript" src="../../generators/arduino.js"></script>
<script type="text/javascript" src="../../generators/arduino/logic.js"></script>
<script type="text/javascript" src="../../generators/arduino/loops.js"></script>
<script type="text/javascript" src="../../generators/arduino/math.js"></script>
<script type="text/javascript" src="../../generators/arduino/text.js"></script>
<script type="text/javascript" src="../../generators/arduino/lists.js"></script>
<script type="text/javascript" src="../../generators/arduino/colour.js"></script>
<script type="text/javascript" src="../../generators/arduino/variables.js"></script>
<script type="text/javascript" src="../../generators/arduino/procedures.js"></script>
<script type="text/javascript" src="../generators/javascript.js"></script>
<script type="text/javascript" src="../generators/javascript/logic.js"></script>
<script type="text/javascript" src="../generators/javascript/loops.js"></script>
......@@ -336,6 +345,8 @@ h1 {
&nbsp;
<input type="button" value="Import from XML" onclick="fromXml()">
<br>
<input type="button" value="To Arduino" onclick="toCode('Arduino')">
&nbsp;
<input type="button" value="To JavaScript" onclick="toCode('JavaScript')">
&nbsp;
<input type="button" value="To Python" onclick="toCode('Python')">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment