Merge branch 'master' into ui-updates
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* @license
|
||||
*
|
||||
*
|
||||
* Copyright 2019 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -24,13 +24,13 @@
|
||||
// More on generating code:
|
||||
// https://developers.google.com/blockly/guides/create-custom-blocks/generating-code
|
||||
|
||||
import * as Blockly from 'blockly/core';
|
||||
import * as Blockly from "blockly/core";
|
||||
|
||||
/**
|
||||
* Arduino code generator.
|
||||
* @type !Blockly.Generator
|
||||
*/
|
||||
Blockly['Arduino'] = new Blockly.Generator('Arduino');
|
||||
Blockly["Arduino"] = new Blockly.Generator("Arduino");
|
||||
|
||||
/**
|
||||
* List of illegal variable names.
|
||||
@@ -39,156 +39,153 @@ Blockly['Arduino'] = new Blockly.Generator('Arduino');
|
||||
* accidentally clobbering a built-in object or function.
|
||||
* @private
|
||||
*/
|
||||
Blockly['Arduino'].addReservedWords(
|
||||
// http://arduino.cc/en/Reference/HomePage
|
||||
'setup,loop,if,else,for,switch,case,while,' +
|
||||
'do,break,continue,return,goto,define,include,' +
|
||||
'HIGH,LOW,INPUT,OUTPUT,INPUT_PULLUP,true,false,' +
|
||||
'interger, constants,floating,point,void,boolean,char,' +
|
||||
'unsigned,byte,int,word,long,float,double,string,String,array,' +
|
||||
'static, volatile,const,sizeof,pinMode,digitalWrite,digitalRead,' +
|
||||
'analogReference,analogRead,analogWrite,tone,noTone,shiftOut,shitIn,' +
|
||||
'pulseIn,millis,micros,delay,delayMicroseconds,min,max,abs,constrain,' +
|
||||
'map,pow,sqrt,sin,cos,tan,randomSeed,random,lowByte,highByte,bitRead,' +
|
||||
'bitWrite,bitSet,bitClear,ultraSonicDistance,parseDouble,setNeoPixelColor,' +
|
||||
'bit,attachInterrupt,detachInterrupt,interrupts,noInterrupts',
|
||||
'short',
|
||||
'isBtnPressed'
|
||||
Blockly["Arduino"].addReservedWords(
|
||||
// http://arduino.cc/en/Reference/HomePage
|
||||
"setup,loop,if,else,for,switch,case,while," +
|
||||
"do,break,continue,return,goto,define,include," +
|
||||
"HIGH,LOW,INPUT,OUTPUT,INPUT_PULLUP,true,false," +
|
||||
"interger, constants,floating,point,void,boolean,char," +
|
||||
"unsigned,byte,int,word,long,float,double,string,String,array," +
|
||||
"static, volatile,const,sizeof,pinMode,digitalWrite,digitalRead," +
|
||||
"analogReference,analogRead,analogWrite,tone,noTone,shiftOut,shitIn," +
|
||||
"pulseIn,millis,micros,delay,delayMicroseconds,min,max,abs,constrain," +
|
||||
"map,pow,sqrt,sin,cos,tan,randomSeed,random,lowByte,highByte,bitRead," +
|
||||
"bitWrite,bitSet,bitClear,ultraSonicDistance,parseDouble,setNeoPixelColor," +
|
||||
"bit,attachInterrupt,detachInterrupt,interrupts,noInterrupts",
|
||||
"short",
|
||||
"isBtnPressed"
|
||||
);
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
*
|
||||
*/
|
||||
Blockly['Arduino'].ORDER_ATOMIC = 0; // 0 "" ...
|
||||
Blockly['Arduino'].ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] .
|
||||
Blockly['Arduino'].ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
|
||||
Blockly['Arduino'].ORDER_MULTIPLICATIVE = 3; // * / % ~/
|
||||
Blockly['Arduino'].ORDER_ADDITIVE = 4; // + -
|
||||
Blockly['Arduino'].ORDER_LOGICAL_NOT = 4.4; // !
|
||||
Blockly['Arduino'].ORDER_SHIFT = 5; // << >>
|
||||
Blockly['Arduino'].ORDER_MODULUS = 5.3; // %
|
||||
Blockly['Arduino'].ORDER_RELATIONAL = 6; // is is! >= > <= <
|
||||
Blockly['Arduino'].ORDER_EQUALITY = 7; // === !== === !==
|
||||
Blockly['Arduino'].ORDER_BITWISE_AND = 8; // &
|
||||
Blockly['Arduino'].ORDER_BITWISE_XOR = 9; // ^
|
||||
Blockly['Arduino'].ORDER_BITWISE_OR = 10; // |
|
||||
Blockly['Arduino'].ORDER_LOGICAL_AND = 11; // &&
|
||||
Blockly['Arduino'].ORDER_LOGICAL_OR = 12; // ||
|
||||
Blockly['Arduino'].ORDER_CONDITIONAL = 13; // expr ? expr : expr
|
||||
Blockly['Arduino'].ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
|
||||
Blockly['Arduino'].ORDER_COMMA = 18; // ,
|
||||
Blockly['Arduino'].ORDER_NONE = 99; // (...)
|
||||
|
||||
|
||||
|
||||
Blockly["Arduino"].ORDER_ATOMIC = 0; // 0 "" ...
|
||||
Blockly["Arduino"].ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] .
|
||||
Blockly["Arduino"].ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
|
||||
Blockly["Arduino"].ORDER_MULTIPLICATIVE = 3; // * / % ~/
|
||||
Blockly["Arduino"].ORDER_ADDITIVE = 4; // + -
|
||||
Blockly["Arduino"].ORDER_LOGICAL_NOT = 4.4; // !
|
||||
Blockly["Arduino"].ORDER_SHIFT = 5; // << >>
|
||||
Blockly["Arduino"].ORDER_MODULUS = 5.3; // %
|
||||
Blockly["Arduino"].ORDER_RELATIONAL = 6; // is is! >= > <= <
|
||||
Blockly["Arduino"].ORDER_EQUALITY = 7; // === !== === !==
|
||||
Blockly["Arduino"].ORDER_BITWISE_AND = 8; // &
|
||||
Blockly["Arduino"].ORDER_BITWISE_XOR = 9; // ^
|
||||
Blockly["Arduino"].ORDER_BITWISE_OR = 10; // |
|
||||
Blockly["Arduino"].ORDER_LOGICAL_AND = 11; // &&
|
||||
Blockly["Arduino"].ORDER_LOGICAL_OR = 12; // ||
|
||||
Blockly["Arduino"].ORDER_CONDITIONAL = 13; // expr ? expr : expr
|
||||
Blockly["Arduino"].ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
|
||||
Blockly["Arduino"].ORDER_COMMA = 18; // ,
|
||||
Blockly["Arduino"].ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {} workspace
|
||||
*
|
||||
*
|
||||
* @param {} workspace
|
||||
*
|
||||
* Blockly Types
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly['Arduino'].init = function (workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly['Arduino'].libraries_ = Object.create(null);
|
||||
Blockly["Arduino"].init = function (workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly["Arduino"].libraries_ = Object.create(null);
|
||||
|
||||
Blockly['Arduino'].definitions_ = Object.create(null);
|
||||
Blockly["Arduino"].definitions_ = Object.create(null);
|
||||
|
||||
// creates a list of code to be setup before the setup block
|
||||
Blockly['Arduino'].setupCode_ = Object.create(null);
|
||||
// creates a list of code to be setup before the setup block
|
||||
Blockly["Arduino"].setupCode_ = Object.create(null);
|
||||
|
||||
// creates a list of code to be setup before the setup block
|
||||
Blockly['Arduino'].loraSetupCode_ = Object.create(null);
|
||||
// creates a list of code to be setup before the setup block
|
||||
Blockly["Arduino"].phyphoxSetupCode_ = Object.create(null);
|
||||
|
||||
// creates a list of code for the loop to be runned once
|
||||
Blockly['Arduino'].loopCodeOnce_ = Object.create(null)
|
||||
// creates a list of code to be setup before the setup block
|
||||
Blockly["Arduino"].loraSetupCode_ = Object.create(null);
|
||||
|
||||
// creates a list of code for the loop to be runned once
|
||||
Blockly['Arduino'].codeFunctions_ = Object.create(null)
|
||||
// creates a list of code for the loop to be runned once
|
||||
Blockly["Arduino"].loopCodeOnce_ = Object.create(null);
|
||||
|
||||
// creates a list of code variables
|
||||
Blockly['Arduino'].variables_ = Object.create(null)
|
||||
// creates a list of code for the loop to be runned once
|
||||
Blockly["Arduino"].codeFunctions_ = Object.create(null);
|
||||
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly['Arduino'].functionNames_ = Object.create(null);
|
||||
// creates a list of code variables
|
||||
Blockly["Arduino"].variables_ = Object.create(null);
|
||||
|
||||
Blockly['Arduino'].variablesInitCode_ = '';
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly["Arduino"].functionNames_ = Object.create(null);
|
||||
|
||||
if (!Blockly['Arduino'].variableDB_) {
|
||||
Blockly['Arduino'].variableDB_ = new Blockly.Names(
|
||||
Blockly['Arduino'].RESERVED_WORDS_
|
||||
);
|
||||
} else {
|
||||
Blockly['Arduino'].variableDB_.reset();
|
||||
}
|
||||
Blockly["Arduino"].variablesInitCode_ = "";
|
||||
|
||||
Blockly['Arduino'].variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
if (!Blockly["Arduino"].variableDB_) {
|
||||
Blockly["Arduino"].variableDB_ = new Blockly.Names(
|
||||
Blockly["Arduino"].RESERVED_WORDS_
|
||||
);
|
||||
} else {
|
||||
Blockly["Arduino"].variableDB_.reset();
|
||||
}
|
||||
|
||||
// We don't have developer variables for now
|
||||
// // Add developer variables (not created or named by the user).
|
||||
// var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
// for (var i = 0; i < devVarList.length; i++) {
|
||||
// defvars.push(Blockly['Arduino'].variableDB_.getName(devVarList[i],
|
||||
// Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
// }
|
||||
Blockly["Arduino"].variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
|
||||
const doubleVariables = workspace.getVariablesOfType('Number');
|
||||
let i = 0;
|
||||
let variableCode = '';
|
||||
for (i = 0; i < doubleVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
'double ' +
|
||||
Blockly['Arduino'].variableDB_.getName(
|
||||
doubleVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
' = 0; \n\n';
|
||||
}
|
||||
// We don't have developer variables for now
|
||||
// // Add developer variables (not created or named by the user).
|
||||
// var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
// for (var i = 0; i < devVarList.length; i++) {
|
||||
// defvars.push(Blockly['Arduino'].variableDB_.getName(devVarList[i],
|
||||
// Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
// }
|
||||
|
||||
const stringVariables = workspace.getVariablesOfType('String');
|
||||
for (i = 0; i < stringVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
'String ' +
|
||||
Blockly['Arduino'].variableDB_.getName(
|
||||
stringVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
' = ""; \n\n';
|
||||
}
|
||||
const doubleVariables = workspace.getVariablesOfType("Number");
|
||||
let i = 0;
|
||||
let variableCode = "";
|
||||
for (i = 0; i < doubleVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
"double " +
|
||||
Blockly["Arduino"].variableDB_.getName(
|
||||
doubleVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
" = 0; \n\n";
|
||||
}
|
||||
|
||||
const booleanVariables = workspace.getVariablesOfType('Boolean');
|
||||
for (i = 0; i < booleanVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
'boolean ' +
|
||||
Blockly['Arduino'].variableDB_.getDistinctName(
|
||||
booleanVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
' = false; \n\n';
|
||||
}
|
||||
const stringVariables = workspace.getVariablesOfType("String");
|
||||
for (i = 0; i < stringVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
"String " +
|
||||
Blockly["Arduino"].variableDB_.getName(
|
||||
stringVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
' = ""; \n\n';
|
||||
}
|
||||
|
||||
const colourVariables = workspace.getVariablesOfType('Colour');
|
||||
for (i = 0; i < colourVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
'RGB ' +
|
||||
Blockly['Arduino'].variableDB_.getName(
|
||||
colourVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
' = {0, 0, 0}; \n\n';
|
||||
}
|
||||
const booleanVariables = workspace.getVariablesOfType("Boolean");
|
||||
for (i = 0; i < booleanVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
"boolean " +
|
||||
Blockly["Arduino"].variableDB_.getDistinctName(
|
||||
booleanVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
" = false; \n\n";
|
||||
}
|
||||
|
||||
Blockly['Arduino'].variablesInitCode_ = variableCode;
|
||||
const colourVariables = workspace.getVariablesOfType("Colour");
|
||||
for (i = 0; i < colourVariables.length; i += 1) {
|
||||
variableCode +=
|
||||
"RGB " +
|
||||
Blockly["Arduino"].variableDB_.getName(
|
||||
colourVariables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
) +
|
||||
" = {0, 0, 0}; \n\n";
|
||||
}
|
||||
|
||||
Blockly["Arduino"].variablesInitCode_ = variableCode;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -196,88 +193,95 @@ Blockly['Arduino'].init = function (workspace) {
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly['Arduino'].finish = function (code) {
|
||||
let libraryCode = '';
|
||||
let variablesCode = '';
|
||||
let codeFunctions = '';
|
||||
let functionsCode = '';
|
||||
let definitionsCode = '';
|
||||
let loopCodeOnce = '';
|
||||
let setupCode = '';
|
||||
let preSetupCode = '';
|
||||
let loraSetupCode = '';
|
||||
let devVariables = '\n';
|
||||
Blockly["Arduino"].finish = function (code) {
|
||||
let libraryCode = "";
|
||||
let variablesCode = "";
|
||||
let codeFunctions = "";
|
||||
let functionsCode = "";
|
||||
let definitionsCode = "";
|
||||
let phyphoxSetupCode = "";
|
||||
let loopCodeOnce = "";
|
||||
let setupCode = "";
|
||||
let preSetupCode = "";
|
||||
let loraSetupCode = "";
|
||||
let devVariables = "\n";
|
||||
|
||||
for (const key in Blockly['Arduino'].libraries_) {
|
||||
libraryCode += Blockly['Arduino'].libraries_[key] + '\n';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].libraries_) {
|
||||
libraryCode += Blockly["Arduino"].libraries_[key] + "\n";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].variables_) {
|
||||
variablesCode += Blockly['Arduino'].variables_[key] + '\n';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].variables_) {
|
||||
variablesCode += Blockly["Arduino"].variables_[key] + "\n";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].definitions_) {
|
||||
definitionsCode += Blockly['Arduino'].definitions_[key] + '\n';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].definitions_) {
|
||||
definitionsCode += Blockly["Arduino"].definitions_[key] + "\n";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].loopCodeOnce_) {
|
||||
loopCodeOnce += Blockly['Arduino'].loopCodeOnce_[key] + '\n';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].loopCodeOnce_) {
|
||||
loopCodeOnce += Blockly["Arduino"].loopCodeOnce_[key] + "\n";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].codeFunctions_) {
|
||||
codeFunctions += Blockly['Arduino'].codeFunctions_[key] + '\n';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].codeFunctions_) {
|
||||
codeFunctions += Blockly["Arduino"].codeFunctions_[key] + "\n";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].functionNames_) {
|
||||
functionsCode += Blockly['Arduino'].functionNames_[key] + '\n';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].functionNames_) {
|
||||
functionsCode += Blockly["Arduino"].functionNames_[key] + "\n";
|
||||
}
|
||||
|
||||
for (const key in Blockly["Arduino"].setupCode_) {
|
||||
preSetupCode += Blockly["Arduino"].setupCode_[key] + "\n" || "";
|
||||
}
|
||||
|
||||
for (const key in Blockly["Arduino"].loraSetupCode_) {
|
||||
loraSetupCode += Blockly["Arduino"].loraSetupCode_[key] + "\n" || "";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].setupCode_) {
|
||||
preSetupCode += Blockly['Arduino'].setupCode_[key] || '';
|
||||
}
|
||||
for (const key in Blockly["Arduino"].phyphoxSetupCode_) {
|
||||
phyphoxSetupCode += Blockly["Arduino"].phyphoxSetupCode_[key] + "\n" || "";
|
||||
}
|
||||
|
||||
for (const key in Blockly['Arduino'].loraSetupCode_) {
|
||||
loraSetupCode += Blockly['Arduino'].loraSetupCode_[key] || '';
|
||||
}
|
||||
setupCode =
|
||||
"\nvoid setup() { \n" +
|
||||
preSetupCode +
|
||||
"\n" +
|
||||
phyphoxSetupCode +
|
||||
"\n" +
|
||||
loraSetupCode +
|
||||
"\n}\n";
|
||||
|
||||
let loopCode = "\nvoid loop() { \n" + loopCodeOnce + code + "\n}\n";
|
||||
|
||||
setupCode = '\nvoid setup() { \n' + preSetupCode + '\n' + loraSetupCode + '\n}\n';
|
||||
// Convert the definitions dictionary into a list.
|
||||
code =
|
||||
devVariables +
|
||||
"\n" +
|
||||
libraryCode +
|
||||
"\n" +
|
||||
variablesCode +
|
||||
"\n" +
|
||||
definitionsCode +
|
||||
"\n" +
|
||||
codeFunctions +
|
||||
"\n" +
|
||||
Blockly["Arduino"].variablesInitCode_ +
|
||||
"\n" +
|
||||
functionsCode +
|
||||
"\n" +
|
||||
setupCode +
|
||||
"\n" +
|
||||
loopCode;
|
||||
|
||||
let loopCode = '\nvoid loop() { \n' + loopCodeOnce + code + '\n}\n';
|
||||
// Clean up temporary data.
|
||||
delete Blockly["Arduino"].definitions_;
|
||||
delete Blockly["Arduino"].functionNames_;
|
||||
delete Blockly["Arduino"].loopCodeOnce_;
|
||||
delete Blockly["Arduino"].variablesInitCode_;
|
||||
delete Blockly["Arduino"].libraries_;
|
||||
Blockly["Arduino"].variableDB_.reset();
|
||||
|
||||
|
||||
// Convert the definitions dictionary into a list.
|
||||
code =
|
||||
devVariables +
|
||||
'\n' +
|
||||
libraryCode +
|
||||
'\n' +
|
||||
variablesCode +
|
||||
'\n' +
|
||||
definitionsCode +
|
||||
'\n' +
|
||||
codeFunctions +
|
||||
'\n' +
|
||||
Blockly['Arduino'].variablesInitCode_ +
|
||||
'\n' +
|
||||
functionsCode +
|
||||
'\n' +
|
||||
setupCode +
|
||||
'\n' +
|
||||
loopCode
|
||||
;
|
||||
|
||||
// Clean up temporary data.
|
||||
delete Blockly['Arduino'].definitions_;
|
||||
delete Blockly['Arduino'].functionNames_;
|
||||
delete Blockly['Arduino'].loopCodeOnce_;
|
||||
delete Blockly['Arduino'].variablesInitCode_;
|
||||
delete Blockly['Arduino'].libraries_;
|
||||
Blockly['Arduino'].variableDB_.reset();
|
||||
|
||||
return code;
|
||||
return code;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -286,8 +290,8 @@ Blockly['Arduino'].finish = function (code) {
|
||||
* @param {string} line Line of generated code.
|
||||
* @return {string} Legal line of code.
|
||||
*/
|
||||
Blockly['Arduino'].scrubNakedValue = function (line) {
|
||||
return line + ';\n';
|
||||
Blockly["Arduino"].scrubNakedValue = function (line) {
|
||||
return line + ";\n";
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -297,14 +301,14 @@ Blockly['Arduino'].scrubNakedValue = function (line) {
|
||||
* @return {string} Arduino string.
|
||||
* @private
|
||||
*/
|
||||
Blockly['Arduino'].quote_ = function (string) {
|
||||
// Can't use goog.string.quote since Google's style guide recommends
|
||||
// JS string literals use single quotes.
|
||||
string = string
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/\n/g, '\\\n')
|
||||
.replace(/'/g, "\\'");
|
||||
return '"' + string + '"';
|
||||
Blockly["Arduino"].quote_ = function (string) {
|
||||
// Can't use goog.string.quote since Google's style guide recommends
|
||||
// JS string literals use single quotes.
|
||||
string = string
|
||||
.replace(/\\/g, "\\\\")
|
||||
.replace(/\n/g, "\\\n")
|
||||
.replace(/'/g, "\\'");
|
||||
return '"' + string + '"';
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -317,43 +321,42 @@ Blockly['Arduino'].quote_ = function (string) {
|
||||
* @return {string} Arduino code with comments and subsequent blocks added.
|
||||
* @private
|
||||
*/
|
||||
Blockly['Arduino'].scrub_ = function (block, code) {
|
||||
let commentCode = '';
|
||||
// Only collect comments for blocks that aren't inline.
|
||||
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
||||
// Collect comment for this block.
|
||||
let comment = block.getCommentText();
|
||||
//@ts-ignore
|
||||
comment = comment ? Blockly.utils.string.wrap(
|
||||
comment,
|
||||
Blockly['Arduino'].COMMENT_WRAP - 3
|
||||
) : null;
|
||||
if (comment) {
|
||||
if (block.getProcedureDef) {
|
||||
// Use a comment block for function comments.
|
||||
commentCode +=
|
||||
'/**\n' +
|
||||
Blockly['Arduino'].prefixLines(comment + '\n', ' * ') +
|
||||
' */\n';
|
||||
} else {
|
||||
commentCode += Blockly['Arduino'].prefixLines(comment + '\n', '// ');
|
||||
}
|
||||
}
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
for (let i = 0; i < block.inputList.length; i++) {
|
||||
if (block.inputList[i].type === Blockly.INPUT_VALUE) {
|
||||
const childBlock = block.inputList[i].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
const comment = Blockly['Arduino'].allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly['Arduino'].prefixLines(comment, '// ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Blockly["Arduino"].scrub_ = function (block, code) {
|
||||
let commentCode = "";
|
||||
// Only collect comments for blocks that aren't inline.
|
||||
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
||||
// Collect comment for this block.
|
||||
let comment = block.getCommentText();
|
||||
//@ts-ignore
|
||||
comment = comment
|
||||
? Blockly.utils.string.wrap(comment, Blockly["Arduino"].COMMENT_WRAP - 3)
|
||||
: null;
|
||||
if (comment) {
|
||||
if (block.getProcedureDef) {
|
||||
// Use a comment block for function comments.
|
||||
commentCode +=
|
||||
"/**\n" +
|
||||
Blockly["Arduino"].prefixLines(comment + "\n", " * ") +
|
||||
" */\n";
|
||||
} else {
|
||||
commentCode += Blockly["Arduino"].prefixLines(comment + "\n", "// ");
|
||||
}
|
||||
}
|
||||
const nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
const nextCode = Blockly['Arduino'].blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
for (let i = 0; i < block.inputList.length; i++) {
|
||||
if (block.inputList[i].type === Blockly.INPUT_VALUE) {
|
||||
const childBlock = block.inputList[i].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
const comment = Blockly["Arduino"].allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly["Arduino"].prefixLines(comment, "// ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
const nextCode = Blockly["Arduino"].blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
|
||||
@@ -7,8 +7,9 @@ import "./sensebox-web";
|
||||
import "./sensebox-display";
|
||||
import "./sensebox-lora";
|
||||
import "./sensebox-led";
|
||||
import "./sensebox-sd";
|
||||
import "./sensebox";
|
||||
import "./sensebox-ble";
|
||||
import "./sensebox-sd";
|
||||
import "./mqtt";
|
||||
import "./logic";
|
||||
import "./text";
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
|
||||
import * as Blockly from 'blockly/core';
|
||||
import * as Blockly from "blockly/core";
|
||||
|
||||
/**
|
||||
* @license Licensed under the Apache License, Version 2.0 (the "License"):
|
||||
@@ -19,15 +18,15 @@ import * as Blockly from 'blockly/core';
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_number'] = function (block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue('NUM'));
|
||||
if (code === Infinity) {
|
||||
code = 'INFINITY';
|
||||
} else if (code === -Infinity) {
|
||||
code = '-INFINITY';
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
Blockly.Arduino["math_number"] = function (block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue("NUM"));
|
||||
if (code === Infinity) {
|
||||
code = "INFINITY";
|
||||
} else if (code === -Infinity) {
|
||||
code = "-INFINITY";
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -37,27 +36,27 @@ Blockly.Arduino['math_number'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_arithmetic'] = function (block) {
|
||||
var OPERATORS = {
|
||||
ADD: [' + ', Blockly.Arduino.ORDER_ADDITIVE],
|
||||
MINUS: [' - ', Blockly.Arduino.ORDER_ADDITIVE],
|
||||
MULTIPLY: [' * ', Blockly.Arduino.ORDER_MULTIPLICATIVE],
|
||||
DIVIDE: [' / ', Blockly.Arduino.ORDER_MULTIPLICATIVE],
|
||||
POWER: [null, Blockly.Arduino.ORDER_NONE] // Handle power separately.
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.Arduino.valueToCode(block, 'B', order) || '0';
|
||||
var code;
|
||||
// Power in C++ requires a special case since it has no operator.
|
||||
if (!operator) {
|
||||
code = 'Math.pow(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
Blockly.Arduino["math_arithmetic"] = function (block) {
|
||||
var OPERATORS = {
|
||||
ADD: [" + ", Blockly.Arduino.ORDER_ADDITIVE],
|
||||
MINUS: [" - ", Blockly.Arduino.ORDER_ADDITIVE],
|
||||
MULTIPLY: [" * ", Blockly.Arduino.ORDER_MULTIPLICATIVE],
|
||||
DIVIDE: [" / ", Blockly.Arduino.ORDER_MULTIPLICATIVE],
|
||||
POWER: [null, Blockly.Arduino.ORDER_NONE], // Handle power separately.
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue("OP")];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, "A", order) || "0";
|
||||
var argument1 = Blockly.Arduino.valueToCode(block, "B", order) || "0";
|
||||
var code;
|
||||
// Power in C++ requires a special case since it has no operator.
|
||||
if (!operator) {
|
||||
code = "Math.pow(" + argument0 + ", " + argument1 + ")";
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -66,90 +65,103 @@ Blockly.Arduino['math_arithmetic'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_single'] = function (block) {
|
||||
var operator = block.getFieldValue('OP');
|
||||
var code;
|
||||
var arg;
|
||||
if (operator === 'NEG') {
|
||||
// Negation is a special case given its different operator precedents.
|
||||
arg = Blockly.Arduino.valueToCode(block, 'NUM',
|
||||
Blockly.Arduino.ORDER_UNARY_PREFIX) || '0';
|
||||
if (arg[0] === '-') {
|
||||
// --3 is not legal in C++ in this context.
|
||||
arg = ' ' + arg;
|
||||
}
|
||||
code = '-' + arg;
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_PREFIX];
|
||||
Blockly.Arduino["math_single"] = function (block) {
|
||||
var operator = block.getFieldValue("OP");
|
||||
var code;
|
||||
var arg;
|
||||
if (operator === "NEG") {
|
||||
// Negation is a special case given its different operator precedents.
|
||||
arg =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"NUM",
|
||||
Blockly.Arduino.ORDER_UNARY_PREFIX
|
||||
) || "0";
|
||||
if (arg[0] === "-") {
|
||||
// --3 is not legal in C++ in this context.
|
||||
arg = " " + arg;
|
||||
}
|
||||
if (operator === 'ABS' || operator.substring(0, 5) === 'ROUND') {
|
||||
arg = Blockly.Arduino.valueToCode(block, 'NUM',
|
||||
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '0';
|
||||
} else if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') {
|
||||
arg = Blockly.Arduino.valueToCode(block, 'NUM',
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0';
|
||||
} else {
|
||||
arg = Blockly.Arduino.valueToCode(block, 'NUM',
|
||||
Blockly.Arduino.ORDER_NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses.
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = 'abs(' + arg + ')';
|
||||
break;
|
||||
case 'ROOT':
|
||||
code = 'sqrt(' + arg + ')';
|
||||
break;
|
||||
case 'LN':
|
||||
code = 'log(' + arg + ')';
|
||||
break;
|
||||
case 'EXP':
|
||||
code = 'exp(' + arg + ')';
|
||||
break;
|
||||
case 'POW10':
|
||||
code = 'pow(10,' + arg + ')';
|
||||
break;
|
||||
case 'ROUND':
|
||||
code = 'round(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDUP':
|
||||
code = 'ceil(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDDOWN':
|
||||
code = 'floor(' + arg + ')';
|
||||
break;
|
||||
case 'SIN':
|
||||
code = 'sin(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
case 'COS':
|
||||
code = 'cos(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
case 'TAN':
|
||||
code = 'tan(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses.
|
||||
switch (operator) {
|
||||
case 'LOG10':
|
||||
code = 'log(' + arg + ') / log(10)';
|
||||
break;
|
||||
case 'ASIN':
|
||||
code = 'asin(' + arg + ') / M_PI * 180';
|
||||
break;
|
||||
case 'ACOS':
|
||||
code = 'acos(' + arg + ') / M_PI * 180';
|
||||
break;
|
||||
case 'ATAN':
|
||||
code = 'atan(' + arg + ') / M_PI * 180';
|
||||
break;
|
||||
default:
|
||||
throw new Error('Unknown math operator: ' + operator);
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_MULTIPLICATIVE];
|
||||
code = "-" + arg;
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_PREFIX];
|
||||
}
|
||||
if (operator === "ABS" || operator.substring(0, 5) === "ROUND") {
|
||||
arg =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"NUM",
|
||||
Blockly.Arduino.ORDER_UNARY_POSTFIX
|
||||
) || "0";
|
||||
} else if (operator === "SIN" || operator === "COS" || operator === "TAN") {
|
||||
arg =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"NUM",
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE
|
||||
) || "0";
|
||||
} else {
|
||||
arg =
|
||||
Blockly.Arduino.valueToCode(block, "NUM", Blockly.Arduino.ORDER_NONE) ||
|
||||
"0";
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses.
|
||||
switch (operator) {
|
||||
case "ABS":
|
||||
code = "abs(" + arg + ")";
|
||||
break;
|
||||
case "ROOT":
|
||||
code = "sqrt(" + arg + ")";
|
||||
break;
|
||||
case "LN":
|
||||
code = "log(" + arg + ")";
|
||||
break;
|
||||
case "EXP":
|
||||
code = "exp(" + arg + ")";
|
||||
break;
|
||||
case "POW10":
|
||||
code = "pow(10," + arg + ")";
|
||||
break;
|
||||
case "ROUND":
|
||||
code = "round(" + arg + ")";
|
||||
break;
|
||||
case "ROUNDUP":
|
||||
code = "ceil(" + arg + ")";
|
||||
break;
|
||||
case "ROUNDDOWN":
|
||||
code = "floor(" + arg + ")";
|
||||
break;
|
||||
case "SIN":
|
||||
code = "sin(" + arg + " / 180 * Math.PI)";
|
||||
break;
|
||||
case "COS":
|
||||
code = "cos(" + arg + " / 180 * Math.PI)";
|
||||
break;
|
||||
case "TAN":
|
||||
code = "tan(" + arg + " / 180 * Math.PI)";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses.
|
||||
switch (operator) {
|
||||
case "LOG10":
|
||||
code = "log(" + arg + ") / log(10)";
|
||||
break;
|
||||
case "ASIN":
|
||||
code = "asin(" + arg + ") / M_PI * 180";
|
||||
break;
|
||||
case "ACOS":
|
||||
code = "acos(" + arg + ") / M_PI * 180";
|
||||
break;
|
||||
case "ATAN":
|
||||
code = "atan(" + arg + ") / M_PI * 180";
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unknown math operator: " + operator);
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -161,16 +173,16 @@ Blockly.Arduino['math_single'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.Arduino['math_constant'] = function (block) {
|
||||
var CONSTANTS = {
|
||||
'PI': ['M_PI', Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
'E': ['M_E', Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', Blockly.Arduino.ORDER_MULTIPLICATIVE],
|
||||
'SQRT2': ['M_SQRT2', Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
'SQRT1_2': ['M_SQRT1_2', Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
'INFINITY': ['INFINITY', Blockly.Arduino.ORDER_ATOMIC]
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
Blockly.Arduino["math_constant"] = function (block) {
|
||||
var CONSTANTS = {
|
||||
PI: ["M_PI", Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
E: ["M_E", Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
GOLDEN_RATIO: ["(1 + sqrt(5)) / 2", Blockly.Arduino.ORDER_MULTIPLICATIVE],
|
||||
SQRT2: ["M_SQRT2", Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
SQRT1_2: ["M_SQRT1_2", Blockly.Arduino.ORDER_UNARY_POSTFIX],
|
||||
INFINITY: ["INFINITY", Blockly.Arduino.ORDER_ATOMIC],
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue("CONSTANT")];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -180,63 +192,72 @@ Blockly.Arduino['math_constant'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_number_property'] = function (block) {
|
||||
var number_to_check = Blockly.Arduino.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0';
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property === 'PRIME') {
|
||||
var func = [
|
||||
'boolean ' + Blockly.Arduino.DEF_FUNC_NAME + '(int n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if (n == 2 || n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is NaN, negative, is 1.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (isnan(n) || (n <= 1) || (n == 1) || (n % 2 == 0) || ' +
|
||||
'(n % 3 == 0)) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for (int x = 6; x <= sqrt(n) + 1; x += 6) {',
|
||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}'];
|
||||
var funcName = Blockly.Arduino.addFunction('mathIsPrime', func.join('\n'));
|
||||
Blockly.Arduino.addInclude('math', '#include <math.h>');
|
||||
code = funcName + '(' + number_to_check + ')';
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 == 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 == 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
Blockly.Arduino.addInclude('math', '#include <math.h>');
|
||||
code = '(floor(' + number_to_check + ') == ' + number_to_check + ')';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY':
|
||||
var divisor = Blockly.Arduino.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0';
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_EQUALITY];
|
||||
Blockly.Arduino["math_number_property"] = function (block) {
|
||||
var number_to_check =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"NUMBER_TO_CHECK",
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE
|
||||
) || "0";
|
||||
var dropdown_property = block.getFieldValue("PROPERTY");
|
||||
var code;
|
||||
if (dropdown_property === "PRIME") {
|
||||
var func = [
|
||||
"boolean " + Blockly.Arduino.DEF_FUNC_NAME + "(int n) {",
|
||||
" // https://en.wikipedia.org/wiki/Primality_test#Naive_methods",
|
||||
" if (n == 2 || n == 3) {",
|
||||
" return true;",
|
||||
" }",
|
||||
" // False if n is NaN, negative, is 1.",
|
||||
" // And false if n is divisible by 2 or 3.",
|
||||
" if (isnan(n) || (n <= 1) || (n == 1) || (n % 2 == 0) || " +
|
||||
"(n % 3 == 0)) {",
|
||||
" return false;",
|
||||
" }",
|
||||
" // Check all the numbers of form 6k +/- 1, up to sqrt(n).",
|
||||
" for (int x = 6; x <= sqrt(n) + 1; x += 6) {",
|
||||
" if (n % (x - 1) == 0 || n % (x + 1) == 0) {",
|
||||
" return false;",
|
||||
" }",
|
||||
" }",
|
||||
" return true;",
|
||||
"}",
|
||||
];
|
||||
var funcName = Blockly.Arduino.addFunction("mathIsPrime", func.join("\n"));
|
||||
Blockly.Arduino.addInclude("math", "#include <math.h>");
|
||||
code = funcName + "(" + number_to_check + ")";
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case "EVEN":
|
||||
code = number_to_check + " % 2 == 0";
|
||||
break;
|
||||
case "ODD":
|
||||
code = number_to_check + " % 2 == 1";
|
||||
break;
|
||||
case "WHOLE":
|
||||
Blockly.Arduino.addInclude("math", "#include <math.h>");
|
||||
code = "(floor(" + number_to_check + ") == " + number_to_check + ")";
|
||||
break;
|
||||
case "POSITIVE":
|
||||
code = number_to_check + " > 0";
|
||||
break;
|
||||
case "NEGATIVE":
|
||||
code = number_to_check + " < 0";
|
||||
break;
|
||||
case "DIVISIBLE_BY":
|
||||
var divisor =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"DIVISOR",
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE
|
||||
) || "0";
|
||||
code = number_to_check + " % " + divisor + " == 0";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_EQUALITY];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -247,19 +268,25 @@ Blockly.Arduino['math_number_property'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_change'] = function (block) {
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'DELTA',
|
||||
Blockly.Arduino.ORDER_ADDITIVE) || '0';
|
||||
var varName = Blockly.Arduino.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' += ' + argument0 + ';\n';
|
||||
Blockly.Arduino["math_change"] = function (block) {
|
||||
var argument0 =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"DELTA",
|
||||
Blockly.Arduino.ORDER_ADDITIVE
|
||||
) || "0";
|
||||
var varName = Blockly.Arduino.variableDB_.getName(
|
||||
block.getFieldValue("VAR"),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
);
|
||||
return varName + " += " + argument0 + ";\n";
|
||||
};
|
||||
|
||||
/** Rounding functions have a single operand. */
|
||||
Blockly.Arduino['math_round'] = Blockly.Arduino['math_single'];
|
||||
Blockly.Arduino["math_round"] = Blockly.Arduino["math_single"];
|
||||
|
||||
/** Trigonometry functions have a single operand. */
|
||||
Blockly.Arduino['math_trig'] = Blockly.Arduino['math_single'];
|
||||
Blockly.Arduino["math_trig"] = Blockly.Arduino["math_single"];
|
||||
|
||||
/**
|
||||
* Generator for the math function to a list.
|
||||
@@ -268,7 +295,7 @@ Blockly.Arduino['math_trig'] = Blockly.Arduino['math_single'];
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_on_list'] = Blockly.Arduino.noGeneratorCodeInline;
|
||||
Blockly.Arduino["math_on_list"] = Blockly.Arduino.noGeneratorCodeInline;
|
||||
|
||||
/**
|
||||
* Generator for the math modulo function (calculates remainder of X/Y).
|
||||
@@ -276,13 +303,21 @@ Blockly.Arduino['math_on_list'] = Blockly.Arduino.noGeneratorCodeInline;
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_modulo'] = function (block) {
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0';
|
||||
var argument1 = Blockly.Arduino.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0';
|
||||
var code = argument0 + ' % ' + argument1;
|
||||
return [code, Blockly.Arduino.ORDER_MULTIPLICATIVE];
|
||||
Blockly.Arduino["math_modulo"] = function (block) {
|
||||
var argument0 =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"DIVIDEND",
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE
|
||||
) || "0";
|
||||
var argument1 =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
"DIVISOR",
|
||||
Blockly.Arduino.ORDER_MULTIPLICATIVE
|
||||
) || "0";
|
||||
var code = argument0 + " % " + argument1;
|
||||
return [code, Blockly.Arduino.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -291,18 +326,34 @@ Blockly.Arduino['math_modulo'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_constrain'] = function (block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE',
|
||||
Blockly.Arduino.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Arduino.valueToCode(block, 'LOW',
|
||||
Blockly.Arduino.ORDER_NONE) || '0';
|
||||
var argument2 = Blockly.Arduino.valueToCode(block, 'HIGH',
|
||||
Blockly.Arduino.ORDER_NONE) || '0';
|
||||
var code = '(' + argument0 + ' < ' + argument1 + ' ? ' + argument1 +
|
||||
' : ( ' + argument0 + ' > ' + argument2 + ' ? ' + argument2 + ' : ' +
|
||||
argument0 + '))';
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
Blockly.Arduino["math_constrain"] = function (block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 =
|
||||
Blockly.Arduino.valueToCode(block, "VALUE", Blockly.Arduino.ORDER_NONE) ||
|
||||
"0";
|
||||
var argument1 =
|
||||
Blockly.Arduino.valueToCode(block, "LOW", Blockly.Arduino.ORDER_NONE) ||
|
||||
"0";
|
||||
var argument2 =
|
||||
Blockly.Arduino.valueToCode(block, "HIGH", Blockly.Arduino.ORDER_NONE) ||
|
||||
"0";
|
||||
var code =
|
||||
"(" +
|
||||
argument0 +
|
||||
" < " +
|
||||
argument1 +
|
||||
" ? " +
|
||||
argument1 +
|
||||
" : ( " +
|
||||
argument0 +
|
||||
" > " +
|
||||
argument2 +
|
||||
" ? " +
|
||||
argument2 +
|
||||
" : " +
|
||||
argument0 +
|
||||
"))";
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -312,28 +363,26 @@ Blockly.Arduino['math_constrain'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['math_random_int'] = function (block) {
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'FROM',
|
||||
Blockly.Arduino.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Arduino.valueToCode(block, 'TO',
|
||||
Blockly.Arduino.ORDER_NONE) || '0';
|
||||
var functionName = Blockly.Arduino.variableDB_.getDistinctName(
|
||||
'math_random_int', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Arduino.setups_['init_rand'] = 'randomSeed(analogRead(0));';
|
||||
Blockly.Arduino.math_random_int.random_function = functionName;
|
||||
var func = [
|
||||
'int ' + Blockly.Arduino.DEF_FUNC_NAME + '(int min, int max) {',
|
||||
' if (min > max) {',
|
||||
' // Swap min and max to ensure min is smaller.',
|
||||
' int temp = min;',
|
||||
' min = max;',
|
||||
' max = temp;',
|
||||
' }',
|
||||
' return min + (rand() % (max - min + 1));',
|
||||
'}'];
|
||||
var funcName = Blockly.Arduino.addFunction('mathRandomInt', func.join('\n'));
|
||||
var code = funcName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
Blockly.Arduino["math_random_int"] = function (block) {
|
||||
var argument0 =
|
||||
Blockly.Arduino.valueToCode(block, "FROM", Blockly.Arduino.ORDER_NONE) ||
|
||||
"0";
|
||||
var argument1 =
|
||||
Blockly.Arduino.valueToCode(block, "TO", Blockly.Arduino.ORDER_NONE) || "0";
|
||||
Blockly.Arduino.setupCode_["init_rand"] = "randomSeed(analogRead(0));";
|
||||
Blockly.Arduino.functionNames_[
|
||||
"math_random_int"
|
||||
] = `int mathRandomInt (int min, int max) {\n
|
||||
if (min > max) {
|
||||
int temp = min;
|
||||
min = max;
|
||||
max = temp;
|
||||
}
|
||||
return min + (rand() % (max - min + 1));
|
||||
}
|
||||
`;
|
||||
var code = `mathRandomInt(${argument0},${argument1});`;
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -342,6 +391,6 @@ Blockly.Arduino['math_random_int'] = function (block) {
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.Arduino['math_random_float'] = function (block) {
|
||||
return ['(rand() / RAND_MAX)', Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
Blockly.Arduino["math_random_float"] = function (block) {
|
||||
return ["(rand() / RAND_MAX)", Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import * as Blockly from "blockly/core";
|
||||
|
||||
Blockly.Arduino.sensebox_phyphox_init = function () {
|
||||
var name = this.getFieldValue("devicename");
|
||||
Blockly.Arduino.libraries_["phyphox_library"] = `#include <phyphoxBle.h>`;
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
"phyphox_start"
|
||||
] = `PhyphoxBLE::start("${name}");`;
|
||||
var code = ``;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino.sensebox_phyphox_experiment = function () {
|
||||
var experimentname = "experiment";
|
||||
var title = this.getFieldValue("title").replace(/[^a-zA-Z0-9]/g, "");
|
||||
var description = this.getFieldValue("description");
|
||||
var branch = Blockly.Arduino.statementToCode(this, "view");
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`PhyphoxBleExperiment_${experimentname}`
|
||||
] = `PhyphoxBleExperiment ${experimentname};`;
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`setTitle_${title}`
|
||||
] = `${experimentname}.setTitle("${title}");`;
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`setCategory_senseBoxExperiments}`
|
||||
] = `${experimentname}.setCategory("senseBox Experimente");`;
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`setDescription_${description}`
|
||||
] = `${experimentname}.setDescription("${description}");`;
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`addView_${experimentname}`
|
||||
] = `PhyphoxBleExperiment::View firstView;\nfirstView.setLabel("Messwerte"); //Create a "view"`;
|
||||
Blockly.Arduino.phyphoxSetupCode_[`addGraph`] = `${branch}`;
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`addView_firstview`
|
||||
] = `${experimentname}.addView(firstView);`; //Attach view to experiment
|
||||
Blockly.Arduino.phyphoxSetupCode_[
|
||||
`addExperiment_${experimentname}`
|
||||
] = `PhyphoxBLE::addExperiment(${experimentname});`; //Attach experiment to server
|
||||
|
||||
var code = ``;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino["sensebox_phyphox_timestamp"] = function () {
|
||||
var code = 0;
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Arduino["sensebox_phyphox_channel"] = function () {
|
||||
var channel = parseFloat(this.getFieldValue("channel"));
|
||||
var code = channel;
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Arduino.sensebox_phyphox_sendchannel = function (block) {
|
||||
var channel = this.getFieldValue("channel");
|
||||
var value =
|
||||
Blockly.Arduino.valueToCode(this, "value", Blockly.Arduino.ORDER_ATOMIC) ||
|
||||
"1";
|
||||
|
||||
var code = `float channel${channel} = ${value};\n`;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino.sensebox_phyphox_graph = function () {
|
||||
var label = this.getFieldValue("label").replace(/[^a-zA-Z0-9]/g, "");
|
||||
var unitx = this.getFieldValue("unitx");
|
||||
var unity = this.getFieldValue("unity");
|
||||
var labelx = this.getFieldValue("labelx");
|
||||
var labely = this.getFieldValue("labely");
|
||||
var style = this.getFieldValue("style");
|
||||
var channelX =
|
||||
Blockly.Arduino.valueToCode(
|
||||
this,
|
||||
"channel0",
|
||||
Blockly.Arduino.ORDER_ATOMIC
|
||||
) || 0;
|
||||
var channelY =
|
||||
Blockly.Arduino.valueToCode(
|
||||
this,
|
||||
"channel1",
|
||||
Blockly.Arduino.ORDER_ATOMIC
|
||||
) || 1;
|
||||
|
||||
var code = `PhyphoxBleExperiment::Graph ${label};\n`; //Create graph which will plot random numbers over time
|
||||
code += `${label}.setLabel("${label}");\n`;
|
||||
code += `${label}.setUnitX("${unitx}");\n`;
|
||||
code += `${label}.setUnitY("${unity}");\n`;
|
||||
code += `${label}.setLabelX("${labelx}");\n`;
|
||||
code += `${label}.setLabelY("${labely}");\n`;
|
||||
code += `${label}.setStyle("${style}");\n`;
|
||||
code += `${label}.setChannel(${channelX}, ${channelY});\n`;
|
||||
code += `firstView.addElement(${label});\n`;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino.sensebox_phyphox_experiment_send = function () {
|
||||
var branch = Blockly.Arduino.statementToCode(this, "sendValues");
|
||||
var blocks = this.getDescendants();
|
||||
console.log(blocks);
|
||||
var count = 0;
|
||||
if (blocks !== undefined) {
|
||||
for (var i = 0; i < blocks.length; i++) {
|
||||
if (blocks[i].type === "sensebox_phyphox_sendchannel") {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count === 5) {
|
||||
}
|
||||
var string = "";
|
||||
|
||||
for (var j = 1; j <= count; j++) {
|
||||
console.log("append");
|
||||
if (string === "") {
|
||||
string += `channel${j}`;
|
||||
} else if (string !== "") {
|
||||
string += `, channel${j}`;
|
||||
}
|
||||
}
|
||||
Blockly.Arduino.loopCodeOnce_["phyphox_poll"] = `PhyphoxBLE::poll();`;
|
||||
var code = `${branch}\nPhyphoxBLE::write(${string});`;
|
||||
return code;
|
||||
};
|
||||
Reference in New Issue
Block a user