From 80811fa573257c67d650c4b529f707362784f8f1 Mon Sep 17 00:00:00 2001 From: Mario Pesch Date: Thu, 3 Feb 2022 17:09:06 +0100 Subject: [PATCH] add multi interval feature closes #74 --- src/components/Blockly/blocks/time.js | 228 +++++++++--------- .../Blockly/generator/sensebox-display.js | 6 +- src/components/Blockly/generator/time.js | 86 ++++--- src/components/Blockly/msg/de/time.js | 40 +-- src/components/Blockly/msg/en/time.js | 34 +-- 5 files changed, 222 insertions(+), 172 deletions(-) diff --git a/src/components/Blockly/blocks/time.js b/src/components/Blockly/blocks/time.js index 248fe88..df43ea5 100644 --- a/src/components/Blockly/blocks/time.js +++ b/src/components/Blockly/blocks/time.js @@ -8,123 +8,135 @@ * The arduino built in functions syntax can be found in * http://arduino.cc/en/Reference/HomePage */ -import Blockly from 'blockly'; -import { getColour } from '../helpers/colour' -import * as Types from '../helpers/types' +import Blockly from "blockly"; +import { getColour } from "../helpers/colour"; +import * as Types from "../helpers/types"; - -Blockly.Blocks['time_delay'] = { - /** - * Delay block definition - * @this Blockly.Block - */ - init: function () { - this.setHelpUrl('http://arduino.cc/en/Reference/Delay'); - this.setColour(getColour().time); - this.appendValueInput('DELAY_TIME_MILI') - .setCheck(Types.NUMBER.checkList) - .appendField(Blockly.Msg.ARD_TIME_DELAY); - this.appendDummyInput() - .appendField(Blockly.Msg.ARD_TIME_MS); - this.setInputsInline(true); - this.setPreviousStatement(true, null); - this.setNextStatement(true, null); - this.setTooltip(Blockly.Msg.ARD_TIME_DELAY_TIP); - } +Blockly.Blocks["time_delay"] = { + /** + * Delay block definition + * @this Blockly.Block + */ + init: function () { + this.setHelpUrl("http://arduino.cc/en/Reference/Delay"); + this.setColour(getColour().time); + this.appendValueInput("DELAY_TIME_MILI") + .setCheck(Types.NUMBER.checkList) + .appendField(Blockly.Msg.ARD_TIME_DELAY); + this.appendDummyInput().appendField(Blockly.Msg.ARD_TIME_MS); + this.setInputsInline(true); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + this.setTooltip(Blockly.Msg.ARD_TIME_DELAY_TIP); + }, }; -Blockly.Blocks['time_delaymicros'] = { - /** - * delayMicroseconds block definition - * @this Blockly.Block - */ - init: function () { - this.setHelpUrl('http://arduino.cc/en/Reference/DelayMicroseconds'); - this.setColour(getColour().time); - this.appendValueInput('DELAY_TIME_MICRO') - .setCheck(Types.NUMBER.checkList) - .appendField(Blockly.Msg.ARD_TIME_DELAY); - this.appendDummyInput() - .appendField(Blockly.Msg.ARD_TIME_DELAY_MICROS); - this.setInputsInline(true); - this.setPreviousStatement(true, null); - this.setNextStatement(true, null); - this.setTooltip(Blockly.Msg.ARD_TIME_DELAY_MICRO_TIP); - } +Blockly.Blocks["time_delaymicros"] = { + /** + * delayMicroseconds block definition + * @this Blockly.Block + */ + init: function () { + this.setHelpUrl("http://arduino.cc/en/Reference/DelayMicroseconds"); + this.setColour(getColour().time); + this.appendValueInput("DELAY_TIME_MICRO") + .setCheck(Types.NUMBER.checkList) + .appendField(Blockly.Msg.ARD_TIME_DELAY); + this.appendDummyInput().appendField(Blockly.Msg.ARD_TIME_DELAY_MICROS); + this.setInputsInline(true); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + this.setTooltip(Blockly.Msg.ARD_TIME_DELAY_MICRO_TIP); + }, }; -Blockly.Blocks['time_millis'] = { - /** - * Elapsed time in milliseconds block definition - * @this Blockly.Block - */ - init: function () { - this.setHelpUrl('http://arduino.cc/en/Reference/Millis'); - this.setColour(getColour().time); - this.appendDummyInput() - .appendField(Blockly.Msg.ARD_TIME_MILLIS); - this.setOutput(true, Types.LARGE_NUMBER.typeId); - this.setTooltip(Blockly.Msg.ARD_TIME_MILLIS_TIP); - }, - /** @return {string} The type of return value for the block, an integer. */ - getBlockType: function () { - return Blockly.Types.LARGE_NUMBER; - } +Blockly.Blocks["time_millis"] = { + /** + * Elapsed time in milliseconds block definition + * @this Blockly.Block + */ + init: function () { + this.setHelpUrl("http://arduino.cc/en/Reference/Millis"); + this.setColour(getColour().time); + this.appendDummyInput().appendField(Blockly.Msg.ARD_TIME_MILLIS); + this.setOutput(true, Types.LARGE_NUMBER.typeId); + this.setTooltip(Blockly.Msg.ARD_TIME_MILLIS_TIP); + }, + /** @return {string} The type of return value for the block, an integer. */ + getBlockType: function () { + return Blockly.Types.LARGE_NUMBER; + }, }; -Blockly.Blocks['time_micros'] = { - /** - * Elapsed time in microseconds block definition - * @this Blockly.Block - */ - init: function () { - this.setHelpUrl('http://arduino.cc/en/Reference/Micros'); - this.setColour(getColour().time); - this.appendDummyInput() - .appendField(Blockly.Msg.ARD_TIME_MICROS); - this.setOutput(true, Types.LARGE_NUMBER.typeId); - this.setTooltip(Blockly.Msg.ARD_TIME_MICROS_TIP); - }, - /** - * Should be a long (32bit), but for for now an int. - * @return {string} The type of return value for the block, an integer. - */ - getBlockType: function () { - return Types.LARGE_NUMBER; - } +Blockly.Blocks["time_micros"] = { + /** + * Elapsed time in microseconds block definition + * @this Blockly.Block + */ + init: function () { + this.setHelpUrl("http://arduino.cc/en/Reference/Micros"); + this.setColour(getColour().time); + this.appendDummyInput().appendField(Blockly.Msg.ARD_TIME_MICROS); + this.setOutput(true, Types.LARGE_NUMBER.typeId); + this.setTooltip(Blockly.Msg.ARD_TIME_MICROS_TIP); + }, + /** + * Should be a long (32bit), but for for now an int. + * @return {string} The type of return value for the block, an integer. + */ + getBlockType: function () { + return Types.LARGE_NUMBER; + }, }; -Blockly.Blocks['infinite_loop'] = { - /** - * Waits forever, end of program. - * @this Blockly.Block - */ - init: function () { - this.setHelpUrl(''); - this.setColour(getColour().time); - this.appendDummyInput() - .appendField(Blockly.Msg.ARD_TIME_INF); - this.setInputsInline(true); - this.setPreviousStatement(true); - this.setTooltip(Blockly.Msg.ARD_TIME_INF_TIP); - } +Blockly.Blocks["infinite_loop"] = { + /** + * Waits forever, end of program. + * @this Blockly.Block + */ + init: function () { + this.setHelpUrl(""); + this.setColour(getColour().time); + this.appendDummyInput().appendField(Blockly.Msg.ARD_TIME_INF); + this.setInputsInline(true); + this.setPreviousStatement(true); + this.setTooltip(Blockly.Msg.ARD_TIME_INF_TIP); + }, }; -Blockly.Blocks['sensebox_interval_timer'] = { - init: function () { - this.setTooltip(Blockly.Msg.senseBox_interval_timer_tip); - this.setInputsInline(true); - this.setHelpUrl(''); - this.setColour(getColour().time); - this.appendDummyInput() - .appendField(Blockly.Msg.senseBox_interval_timer); - this.appendDummyInput() - .setAlign(Blockly.ALIGN_LEFT) - .appendField(new Blockly.FieldTextInput("10000"), "interval") - .appendField(Blockly.Msg.senseBox_interval); - this.appendStatementInput('DO') - .setCheck(null); - this.setPreviousStatement(true, null); - this.setNextStatement(true, null); - } +// Blockly.Blocks["sensebox_interval_timer"] = { +// init: function () { +// this.setTooltip(Blockly.Msg.senseBox_interval_timer_tip); +// this.setInputsInline(true); +// this.setHelpUrl(""); +// this.setColour(getColour().time); +// this.appendDummyInput().appendField(Blockly.Msg.senseBox_interval_timer); +// this.appendDummyInput() +// .setAlign(Blockly.ALIGN_LEFT) +// .appendField(new Blockly.FieldTextInput("10000"), "interval") +// .appendField(Blockly.Msg.senseBox_interval); +// this.appendStatementInput("DO").setCheck(null); +// this.setPreviousStatement(true, null); +// this.setNextStatement(true, null); +// }, +// }; + +Blockly.Blocks["sensebox_interval_timer"] = { + init: function () { + this.setTooltip(Blockly.Msg.senseBox_interval_timer_tip); + this.setInputsInline(true); + this.setHelpUrl(""); + this.setColour(getColour().time); + this.appendDummyInput() + .appendField(Blockly.Msg.senseBox_interval_timer) + .appendField(new Blockly.FieldTextInput("Interval"), "name"); + this.appendDummyInput() + .appendField(Blockly.Msg.senseBox_interval_time) + .setAlign(Blockly.ALIGN_LEFT) + .appendField(new Blockly.FieldTextInput("10000"), "interval") + .appendField(Blockly.Msg.senseBox_interval); + this.appendStatementInput("DO").setCheck(null); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + }, }; diff --git a/src/components/Blockly/generator/sensebox-display.js b/src/components/Blockly/generator/sensebox-display.js index 5946a34..3cd0b01 100644 --- a/src/components/Blockly/generator/sensebox-display.js +++ b/src/components/Blockly/generator/sensebox-display.js @@ -9,8 +9,12 @@ Blockly.Arduino.sensebox_display_beginDisplay = function () { Blockly.Arduino.libraries_["library_AdafruitSSD1306"] = "#include "; Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include "; + Blockly.Arduino.definitions_[ + "define_display_size" + ] = `#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 64`; Blockly.Arduino.definitions_["define_display"] = - "#define OLED_RESET 4\nAdafruit_SSD1306 display(OLED_RESET);"; + "#define OLED_RESET 4\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);"; + Blockly.Arduino.setupCode_["sensebox_display_begin"] = "senseBoxIO.powerI2C(true);\ndelay(2000);\ndisplay.begin(SSD1306_SWITCHCAPVCC, 0x3D);\ndisplay.display();\ndelay(100);\ndisplay.clearDisplay();"; var code = ""; diff --git a/src/components/Blockly/generator/time.js b/src/components/Blockly/generator/time.js index 57fc5ec..1310fa5 100644 --- a/src/components/Blockly/generator/time.js +++ b/src/components/Blockly/generator/time.js @@ -1,4 +1,4 @@ -import Blockly from 'blockly'; +import Blockly from "blockly"; /** * @license Licensed under the Apache License, Version 2.0 (the "License"): @@ -16,11 +16,15 @@ import Blockly from 'blockly'; * @param {!Blockly.Block} block Block to generate the code from. * @return {string} Completed code. */ -Blockly.Arduino['time_delay'] = function (block) { - var delayTime = Blockly.Arduino.valueToCode( - block, 'DELAY_TIME_MILI', Blockly.Arduino.ORDER_ATOMIC) || '0'; - var code = 'delay(' + delayTime + ');\n'; - return code; +Blockly.Arduino["time_delay"] = function (block) { + var delayTime = + Blockly.Arduino.valueToCode( + block, + "DELAY_TIME_MILI", + Blockly.Arduino.ORDER_ATOMIC + ) || "0"; + var code = "delay(" + delayTime + ");\n"; + return code; }; /** @@ -29,11 +33,15 @@ Blockly.Arduino['time_delay'] = function (block) { * @param {!Blockly.Block} block Block to generate the code from. * @return {string} Completed code. */ -Blockly.Arduino['time_delaymicros'] = function (block) { - var delayTimeMs = Blockly.Arduino.valueToCode( - block, 'DELAY_TIME_MICRO', Blockly.Arduino.ORDER_ATOMIC) || '0'; - var code = 'delayMicroseconds(' + delayTimeMs + ');\n'; - return code; +Blockly.Arduino["time_delaymicros"] = function (block) { + var delayTimeMs = + Blockly.Arduino.valueToCode( + block, + "DELAY_TIME_MICRO", + Blockly.Arduino.ORDER_ATOMIC + ) || "0"; + var code = "delayMicroseconds(" + delayTimeMs + ");\n"; + return code; }; /** @@ -42,9 +50,9 @@ Blockly.Arduino['time_delaymicros'] = function (block) { * @param {!Blockly.Block} block Block to generate the code from. * @return {array} Completed code with order of operation. */ -Blockly.Arduino['time_millis'] = function (block) { - var code = 'millis()'; - return [code, Blockly.Arduino.ORDER_ATOMIC]; +Blockly.Arduino["time_millis"] = function (block) { + var code = "millis()"; + return [code, Blockly.Arduino.ORDER_ATOMIC]; }; /** @@ -53,9 +61,9 @@ Blockly.Arduino['time_millis'] = function (block) { * @param {!Blockly.Block} block Block to generate the code from. * @return {array} Completed code with order of operation. */ -Blockly.Arduino['time_micros'] = function (block) { - var code = 'micros()'; - return [code, Blockly.Arduino.ORDER_ATOMIC]; +Blockly.Arduino["time_micros"] = function (block) { + var code = "micros()"; + return [code, Blockly.Arduino.ORDER_ATOMIC]; }; /** @@ -64,17 +72,39 @@ Blockly.Arduino['time_micros'] = function (block) { * @param {!Blockly.Block} block Block to generate the code from. * @return {string} Completed code. */ -Blockly.Arduino['infinite_loop'] = function (block) { - return 'while(true);\n'; +Blockly.Arduino["infinite_loop"] = function (block) { + return "while(true);\n"; }; +// Blockly.Arduino.sensebox_interval_timer = function (block) { +// var interval = this.getFieldValue("interval"); +// Blockly.Arduino.variables_["define_interval_variables"] = +// "const long interval = " + +// interval + +// ";\nlong time_start = 0;\nlong time_actual = 0;"; +// var branch = Blockly.Arduino.statementToCode(block, "DO"); +// var code = "time_start = millis();\n"; +// code += +// "if (time_start > time_actual + interval) {\n time_actual = millis();\n"; +// code += branch; +// code += "}\n"; +// return code; +// }; + Blockly.Arduino.sensebox_interval_timer = function (block) { - var interval = this.getFieldValue('interval'); - Blockly.Arduino.variables_['define_interval_variables'] = 'const long interval = ' + interval + ';\nlong time_start = 0;\nlong time_actual = 0;'; - var branch = Blockly.Arduino.statementToCode(block, 'DO'); - var code = 'time_start = millis();\n'; - code += 'if (time_start > time_actual + interval) {\n time_actual = millis();\n' - code += branch; - code += '}\n' - return code; -}; \ No newline at end of file + var intervalTime = this.getFieldValue("interval"); + var intervalName = this.getFieldValue("name"); + Blockly.Arduino.variables_[`define_interval_variables${intervalName}`] = ` + const long interval${intervalName} = ${intervalTime}; + long time_start${intervalName} = 0; + long time_actual${intervalName} = 0;`; + Blockly.Arduino.loopCodeOnce_[ + `interval_loop${intervalName}` + ] = `time_start${intervalName} = millis();\n`; + var branch = Blockly.Arduino.statementToCode(block, "DO"); + var code = ` + if (time_start${intervalName} > time_actual${intervalName} + interval${intervalName}) {\n time_actual${intervalName} = millis();\n`; + code += branch; + code += "}\n"; + return code; +}; diff --git a/src/components/Blockly/msg/de/time.js b/src/components/Blockly/msg/de/time.js index b29d97c..800c3aa 100644 --- a/src/components/Blockly/msg/de/time.js +++ b/src/components/Blockly/msg/de/time.js @@ -1,20 +1,22 @@ export const TIME = { - - /** - * Interval Block - */ - senseBox_interval_timer: "Messintervall", - senseBox_interval: "ms", - senseBox_interval_timer_tip: "Intervall", - ARD_TIME_DELAY: "Warte", - ARD_TIME_DELAY_MICROS: "Mikrosekunden", - ARD_TIME_DELAY_MICRO_TIP: "Warte eine spezifischen Zeit in Microsekunden", - ARD_TIME_DELAY_TIP: "Warte spezifische Zeit in Millisekunden", - ARD_TIME_INF: "Warte für immer (Beende Programm)", - ARD_TIME_INF_TIP: "Stoppt das Programm.", - ARD_TIME_MICROS: "Bereits vergangen Zeit (Mikrosekunden)", - ARD_TIME_MICROS_TIP: "Gibt eine Zahl in Microsekunden zurück, die der Zeitdauer des Aktuellen Programms entspricht. Muss als positiven Integer gespeichert werden", // untranslated - ARD_TIME_MILLIS: "Bereits vergangen Zeit (Millisekunden)", - ARD_TIME_MILLIS_TIP: "Gibt eine Zahl in Millisekunden zurück, die der Zeitdauer des Aktuellen Programms entspricht. Muss als positiven Integer gespeichert werden", // untranslated - ARD_TIME_MS: "Millisekunden", -} \ No newline at end of file + /** + * Interval Block + */ + senseBox_interval_timer: "Intervall: ", + senseBox_interval: "ms", + senseBox_interval_timer_tip: + "Definiere ein Intervall, das alle x Millisekunden ausgeführt wird", + ARD_TIME_DELAY: "Warte", + ARD_TIME_DELAY_MICROS: "Mikrosekunden", + ARD_TIME_DELAY_MICRO_TIP: "Warte eine spezifischen Zeit in Microsekunden", + ARD_TIME_DELAY_TIP: "Warte spezifische Zeit in Millisekunden", + ARD_TIME_INF: "Warte für immer (Beende Programm)", + ARD_TIME_INF_TIP: "Stoppt das Programm.", + ARD_TIME_MICROS: "Bereits vergangen Zeit (Mikrosekunden)", + ARD_TIME_MICROS_TIP: + "Gibt eine Zahl in Microsekunden zurück, die der Zeitdauer des Aktuellen Programms entspricht. Muss als positiven Integer gespeichert werden", // untranslated + ARD_TIME_MILLIS: "Bereits vergangen Zeit (Millisekunden)", + ARD_TIME_MILLIS_TIP: + "Gibt eine Zahl in Millisekunden zurück, die der Zeitdauer des Aktuellen Programms entspricht. Muss als positiven Integer gespeichert werden", // untranslated + ARD_TIME_MS: "Millisekunden", +}; diff --git a/src/components/Blockly/msg/en/time.js b/src/components/Blockly/msg/en/time.js index a8c6e5d..b870506 100644 --- a/src/components/Blockly/msg/en/time.js +++ b/src/components/Blockly/msg/en/time.js @@ -1,17 +1,19 @@ export const TIME = { - - senseBox_interval: "ms", - senseBox_interval_timer: "Measuring interval", - senseBox_interval_timer_tip: "Setup an Intervall", - ARD_TIME_DELAY: "wait", - ARD_TIME_DELAY_MICROS: "microseconds", - ARD_TIME_DELAY_MICRO_TIP: "Wait specific time in microseconds", - ARD_TIME_DELAY_TIP: "Wait specific time in milliseconds", - ARD_TIME_INF: "wait forever (end program)", - ARD_TIME_INF_TIP: "Wait indefinitely, stopping the program.", - ARD_TIME_MICROS: "current elapsed Time (microseconds)", - ARD_TIME_MICROS_TIP: "Returns the number of microseconds since the Arduino board began running the current program. Has to be stored in a positive long integer", - ARD_TIME_MILLIS: "current elapsed Time (milliseconds)", - ARD_TIME_MILLIS_TIP: "Returns the number of milliseconds since the Arduino board began running the current program. Has to be stored in a positive long integer", - ARD_TIME_MS: "milliseconds", -} + senseBox_interval: "ms", + senseBox_interval_timer: "Interval: ", + senseBox_interval_timer_tip: + "Define an interval to be executed every x milliseconds", + ARD_TIME_DELAY: "wait", + ARD_TIME_DELAY_MICROS: "microseconds", + ARD_TIME_DELAY_MICRO_TIP: "Wait specific time in microseconds", + ARD_TIME_DELAY_TIP: "Wait specific time in milliseconds", + ARD_TIME_INF: "wait forever (end program)", + ARD_TIME_INF_TIP: "Wait indefinitely, stopping the program.", + ARD_TIME_MICROS: "current elapsed Time (microseconds)", + ARD_TIME_MICROS_TIP: + "Returns the number of microseconds since the Arduino board began running the current program. Has to be stored in a positive long integer", + ARD_TIME_MILLIS: "current elapsed Time (milliseconds)", + ARD_TIME_MILLIS_TIP: + "Returns the number of milliseconds since the Arduino board began running the current program. Has to be stored in a positive long integer", + ARD_TIME_MS: "milliseconds", +};