update blocks and code

This commit is contained in:
Mario Pesch 2021-12-21 09:31:48 +01:00
parent 6fe4c3aab3
commit f38e21a94b
9 changed files with 175 additions and 152 deletions

View File

@ -354,7 +354,7 @@ Blockly.Blocks["sensebox_button"] = {
if (this.getInput("extraField") == null) {
this.appendDummyInput("extraField")
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.senseBox_pressure_referencePressure)
.appendField(Blockly.Msg.senseBox_button_longPress_time)
.appendField(new Blockly.FieldTextInput("1000"), "time")
.appendField("ms");
}

View File

@ -11,7 +11,7 @@ Blockly.Arduino.sensebox_sensor_temp_hum = function () {
Blockly.Arduino.libraries_["library_adafruithdc1000"] =
"#include <Adafruit_HDC1000.h>";
Blockly.Arduino.definitions_["define_hdc"] =
"Adafruit_HDC1000 hdc = Adafruit_HDC1000();;";
"Adafruit_HDC1000 hdc = Adafruit_HDC1000();";
Blockly.Arduino.setupCode_["sensebox_sensor_temp_hum"] = "hdc.begin();";
var code = `hdc.read${dropdown_name}()`;
return [code, Blockly.Arduino.ORDER_ATOMIC];
@ -35,45 +35,42 @@ Blockly.Arduino.sensebox_sensor_uv_light = function () {
}
if (dropdown_name === "Illuminance") {
Blockly.Arduino.codeFunctions_["read_reg"] = `
int read_reg(byte address, uint8_t reg)
{
int i = 0;
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)address, (uint8_t)1);
delay(1);
if(Wire.available())
i = Wire.read();
return i;
}
int read_reg(byte address, uint8_t reg)
{
int i = 0;
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)address, (uint8_t)1);
delay(1);
if(Wire.available())
i = Wire.read();
return i;
}
`;
Blockly.Arduino.codeFunctions_["write_reg"] = `
void write_reg(byte address, uint8_t reg, uint8_t val)
{
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}`;
void write_reg(byte address, uint8_t reg, uint8_t val)
{
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}`;
Blockly.Arduino.codeFunctions_["Lightsensor_begin"] = `
void Lightsensor_begin()
{
Wire.begin();
unsigned int u = 0;
u = read_reg(0x29, 0x80 | 0x0A); //id register
if ((u & 0xF0) == 0xA0) // TSL45315
void Lightsensor_begin()
{
Wire.begin();
unsigned int u = 0;
u = read_reg(0x29, 0x80 | 0x0A); //id register
if ((u & 0xF0) == 0xA0) // TSL45315
{
write_reg(0x29, 0x80 | 0x00, 0x03); //control: power on
write_reg(0x29, 0x80 | 0x01, 0x02); //config: M=4 T=100ms
delay(120);
lightsensortype = 0; //TSL45315
}
else
else
{
LTR.begin();
LTR.setControl(gain, false, false);
@ -82,46 +79,45 @@ Blockly.Arduino.sensebox_sensor_uv_light = function () {
delay(10); //Wait 10 ms (max) - wakeup time from standby
lightsensortype = 1; //
}
}
}
`;
Blockly.Arduino.codeFunctions_["Lightsensor_getIlluminance"] = `
unsigned int Lightsensor_getIlluminance()
{
unsigned int lux = 0;
if (lightsensortype == 0) // TSL45315
unsigned int Lightsensor_getIlluminance()
{
unsigned int u = (read_reg(0x29, 0x80 | 0x04) << 0); //data low
u |= (read_reg(0x29, 0x80 | 0x05) << 8); //data high
lux = u * 4; // calc lux with M=4 and T=100ms
}
else if (lightsensortype == 1) //LTR-329ALS-01
{
delay(100);
unsigned int data0, data1;
for (int i = 0; i < 5; i++) {
if (LTR.getData(data0, data1)) {
if(LTR.getLux(gain, integrationTime, data0, data1, lux));
if(lux > 0) break;
else delay(10);
}
else {
unsigned int lux = 0;
if (lightsensortype == 0) // TSL45315
{
unsigned int u = (read_reg(0x29, 0x80 | 0x04) << 0); //data low
u |= (read_reg(0x29, 0x80 | 0x05) << 8); //data high
lux = u * 4; // calc lux with M=4 and T=100ms
}
else if (lightsensortype == 1) //LTR-329ALS-01
{
delay(100);
unsigned int data0, data1;
for (int i = 0; i < 5; i++) {
if (LTR.getData(data0, data1)) {
if(LTR.getLux(gain, integrationTime, data0, data1, lux));
if(lux > 0) break;
else delay(10);
}
else {
byte error = LTR.getError();
}
}
}
return lux;
}
}
`;
Blockly.Arduino.definitions_["define_lightsensor"] = `
bool lightsensortype = 0; //0 for tsl - 1 for ltr
//settings for LTR sensor
LTR329 LTR;
unsigned char gain = 1;
unsigned char integrationTime = 0;
unsigned char measurementRate = 3;
`;
bool lightsensortype = 0; //0 for tsl - 1 for ltr
//settings for LTR sensor
LTR329 LTR;
unsigned char gain = 1;
unsigned char integrationTime = 0;
unsigned char measurementRate = 3;
`;
Blockly.Arduino.setupCode_["sensebox_sensor_illuminance"] =
"Lightsensor_begin();";
code = "Lightsensor_getIlluminance()";
@ -191,10 +187,18 @@ bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::FILTER_X16,
Adafruit_BMP280::STANDBY_MS_500);
`;
if (dropdown_name === "Pressure" || dropdown_name === "Temperature") {
code = "bmp.read" + dropdown_name + "()";
} else if (dropdown_name === "Altitude") {
code = "bmp.readAltitude(" + referencePressure + ")";
switch (dropdown_name) {
case "temperature":
code = "bmp.readTemperature()";
break;
case "pressure":
code = "bmp.readPressure()/100";
break;
case "altitude":
code = "bmp.readAltitude(" + referencePressure + ")";
break;
default:
code = "";
}
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

View File

@ -53,7 +53,6 @@ Blockly.Arduino.sensebox_get_ip = function () {
Blockly.Arduino.sensebox_startap = function (block) {
var ssid = this.getFieldValue("SSID");
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
Blockly.Arduino.definitions_["define_network"] = "Bee* b = new Bee();";
Blockly.Arduino.setupCode_["wifi_startAP"] = `WiFi.beginAP(${ssid});`;
var code = "";
return code;

View File

@ -1,114 +1,134 @@
import Blockly from 'blockly';
import Blockly from "blockly";
/**
* Webserver Blocks by Lucas Steinmann
*
*/
Blockly.Arduino.sensebox_initialize_http_server = function (block) {
var box_id = this.getFieldValue('Port');
Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"';
Blockly.Arduino.codeFunctions_['define_wifi_server'] = 'WiFiServer server(' + box_id + ');';
Blockly.Arduino.setupCode_['sensebox_wifi_server_beging'] = 'server.begin();';
return '';
var box_id = this.getFieldValue("Port");
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
'#include "SenseBoxMCU.h"';
Blockly.Arduino.libraries_["library_http"] = "#include .h>";
Blockly.Arduino.codeFunctions_["define_wifi_server"] =
"WiFiServer server(" + box_id + ");";
Blockly.Arduino.setupCode_["sensebox_wifi_server_beging"] = "server.begin();";
return "";
};
Blockly.Arduino.sensebox_http_on_client_connect = function (block) {
var onConnect = Blockly.Arduino.statementToCode(block, 'ON_CONNECT');
var code = '';
code += 'WiFiClient client = server.available();\n';
code += 'if (client && client.available()) {\n';
code += ' String request_string = listenClient(client);\n';
code += ' Request request;\n';
code += ' if (parseRequestSafe(request_string, request)) {\n';
code += onConnect;
code += ' }\n';
code += ' delay(1);\n';
code += ' client.stop();\n';
code += ' delay(1);\n';
code += '}\n';
return code;
var onConnect = Blockly.Arduino.statementToCode(block, "ON_CONNECT");
var code = "";
code += "WiFiClient client = server.available();\n";
code += "if (client && client.available()) {\n";
code += " String request_string = listenClient(client);\n";
code += " Request request;\n";
code += " if (parseRequestSafe(request_string, request)) {\n";
code += onConnect;
code += " }\n";
code += " delay(1);\n";
code += " client.stop();\n";
code += " delay(1);\n";
code += "}\n";
return code;
};
Blockly.Arduino.sensebox_http_method = function (block) {
var code = "request.method";
return [code, Blockly.Arduino.ORDER_ATOMIC];
var code = "request.method";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_http_uri = function (block) {
var code = "request.uri";
return [code, Blockly.Arduino.ORDER_ATOMIC];
var code = "request.uri";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_http_protocol_version = function (block) {
var code = "request.protocol_version";
return [code, Blockly.Arduino.ORDER_ATOMIC];
var code = "request.protocol_version";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_http_user_agent = function (block) {
var code = "request.user_agent";
return [code, Blockly.Arduino.ORDER_ATOMIC];
var code = "request.user_agent";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_generate_html_doc = function (block) {
var header = Blockly.Arduino.valueToCode(block, 'HEADER', Blockly.Arduino.ORDER_NONE) || '""';
var body = Blockly.Arduino.valueToCode(block, 'BODY', Blockly.Arduino.ORDER_NONE) || '""';
var code = 'buildHTML(' + header + ', ' + body + ')';
return [code, Blockly.Arduino.ORDER_ATOMIC];
var header =
Blockly.Arduino.valueToCode(block, "HEADER", Blockly.Arduino.ORDER_NONE) ||
'""';
var body =
Blockly.Arduino.valueToCode(block, "BODY", Blockly.Arduino.ORDER_NONE) ||
'""';
var code = "buildHTML(" + header + ", " + body + ")";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_generate_http_succesful_response = function (block) {
var content = Blockly.Arduino.valueToCode(block, 'CONTENT', Blockly.Arduino.ORDER_NONE) || '""';
var code = 'client.println(buildSuccessfulResponse(request, ' + content + '));\n';
return code;
var content =
Blockly.Arduino.valueToCode(block, "CONTENT", Blockly.Arduino.ORDER_NONE) ||
'""';
var code =
"client.println(buildSuccessfulResponse(request, " + content + "));\n";
return code;
};
Blockly.Arduino.sensebox_generate_http_not_found_response = function (block) {
var code = 'client.println(buildNotFoundResponse(request));\n';
return code;
var code = "client.println(buildNotFoundResponse(request));\n";
return code;
};
Blockly.Arduino.sensebox_ip_address = function (block) {
var code = "b->getIpAddress()";
return [code, Blockly.Arduino.ORDER_ATOMIC];
var code = "b->getIpAddress()";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_general_html_tag = function (block) {
var tag = this.getFieldValue('TAG');
var code = 'buildTag("' + tag + '",';
var n = 0;
var branch = Blockly.Arduino.valueToCode(block, 'DO' + n, Blockly.Arduino.ORDER_NONE);
if (branch.length > 0) {
code += '\n ' + branch;
} else {
code += '""';
}
for (n = 1; n <= block.additionalChildCount_; n++) {
branch = Blockly.Arduino.valueToCode(block, 'DO' + n, Blockly.Arduino.ORDER_NONE);
code += ' +' + branch;
}
return [code + ')', Blockly.Arduino.ORDER_ATOMIC];
var tag = this.getFieldValue("TAG");
var code = 'buildTag("' + tag + '",';
var n = 0;
var branch = Blockly.Arduino.valueToCode(
block,
"DO" + n,
Blockly.Arduino.ORDER_NONE
);
if (branch.length > 0) {
code += "\n " + branch;
} else {
code += '""';
}
for (n = 1; n <= block.additionalChildCount_; n++) {
branch = Blockly.Arduino.valueToCode(
block,
"DO" + n,
Blockly.Arduino.ORDER_NONE
);
code += " +" + branch;
}
return [code + ")", Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.sensebox_web_readHTML = function (block) {
var filename = this.getFieldValue('FILENAME');
Blockly.Arduino.libraries_['library_spi'] = '#include <SPI.h>';
Blockly.Arduino.libraries_['library_sd'] = '#include <SD.h>';
Blockly.Arduino.codeFunctions_['define_sd' + filename] = 'File webFile;';
Blockly.Arduino.setupCode_['sensebox_sd'] = 'SD.begin(28);';
var func = [
'String generateHTML(){',
' webFile = SD.open("' + filename + '", FILE_READ);',
' String finalString ="";',
' while (webFile.available())',
' {',
' finalString+=(char)webFile.read();',
' }',
' return finalString;',
'}'];
var functionName = Blockly.Arduino.addFunction(
'generateHTML', func.join('\n'));
var code = functionName + '()';
return [code, Blockly.Arduino.ORDER_ATOMIC];
var filename = this.getFieldValue("FILENAME");
Blockly.Arduino.libraries_["library_spi"] = "#include <SPI.h>";
Blockly.Arduino.libraries_["library_sd"] = "#include <SD.h>";
Blockly.Arduino.codeFunctions_["define_sd" + filename] = "File webFile;";
Blockly.Arduino.setupCode_["sensebox_sd"] = "SD.begin(28);";
var func = [
"String generateHTML(){",
' webFile = SD.open("' + filename + '", FILE_READ);',
' String finalString ="";',
" while (webFile.available())",
" {",
" finalString+=(char)webFile.read();",
" }",
" return finalString;",
"}",
];
var functionName = Blockly.Arduino.addFunction(
"generateHTML",
func.join("\n")
);
var code = functionName + "()";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

View File

@ -9,10 +9,10 @@ export const SENSORS = {
* BMP280
*/
senseBox_pressure_sensor: "Luftdruck-/Temperatursensor (BMP280)",
senseBox_pressure: "Luftdruck in Pa",
senseBox_pressure_dimension: "Luftdruck in Pa",
senseBox_pressure: "Luftdruck in hPa",
senseBox_pressure_dimension: "Luftdruck in hPa",
senseBox_pressure_tip:
"Schließe den Sensor an einen der 5 **I2C-Anschlüsse** an. Der Sensor gibt dir den Messwert für den Luftdruck in Pa. Um die korrekte Höhe über NN zu berechnen benötigt der Sensor einen aktuellen Referenzwert.",
"Schließe den Sensor an einen der 5 **I2C-Anschlüsse** an. Der Sensor gibt dir den Messwert für den Luftdruck in hPa. Um die korrekte Höhe über NN zu berechnen benötigt der Sensor einen aktuellen Referenzwert.",
senseBox_pressure_referencePressure: "Luftdruck auf NN",
senseBox_pressure_referencePressure_dim: "hPa",
senseBox_pressure_helpurl: "",

View File

@ -745,8 +745,8 @@ Blockly.Msg.senseBox_lux_tip = "Helligkeitssensor";
Blockly.Msg.senseBox_poti = "Potenziometer";
Blockly.Msg.senseBox_poti_tip = "Potenziometer";
Blockly.Msg.senseBox_pressure_sensor = "Luftdruck-/Temperatursensor (BMP280)";
Blockly.Msg.senseBox_pressure = "Luftdruck in Pa";
Blockly.Msg.senseBox_pressure_dimension = "Luftdruck in Pa";
Blockly.Msg.senseBox_pressure = "Luftdruck in hPa";
Blockly.Msg.senseBox_pressure_dimension = "Luftdruck in hPa";
Blockly.Msg.senseBox_pressure_tip = "Luftdrucksensor";
Blockly.Msg.senseBox_pressure_referencePressure = "Luftdruck auf NN";
Blockly.Msg.senseBox_pressure_referencePressure_dim = "hPa";

View File

@ -9,10 +9,10 @@ export const SENSORS = {
* BMP280
*/
senseBox_pressure_sensor: "Airpressure/Temperature Sensor (BMP280)",
senseBox_pressure: "Airpressure in Pa",
senseBox_pressure_dimension: "Airpressure in Pa",
senseBox_pressure: "Airpressure in hPa",
senseBox_pressure_dimension: "Airpressure in hPa",
senseBox_pressure_tip:
"Connect the sensor to one of the 5 **I2C ports**. The sensor gives you the measured value for the air pressure in Pa. To calculate the correct altitude above sea level the sensor needs a current reference value.",
"Connect the sensor to one of the 5 **I2C ports**. The sensor gives you the measured value for the air pressure in hPa. To calculate the correct altitude above sea level the sensor needs a current reference value.",
senseBox_pressure_referencePressure: "Pressure at Sea Level",
senseBox_pressure_referencePressure_dim: "hPa",
senseBox_pressure_helpurl: "",

View File

@ -887,13 +887,13 @@ Blockly.Msg.senseBox_output_timestamp = "timestamp";
Blockly.Msg.senseBox_piezo_tip = "simple piezo to make sound";
Blockly.Msg.senseBox_poti = "Potentiometer";
Blockly.Msg.senseBox_poti_tip = "Potentiometer";
Blockly.Msg.senseBox_pressure = "Airpressure in Pa";
Blockly.Msg.senseBox_pressure = "Airpressure in hPa";
Blockly.Msg.senseBox_pressure_referencePressure = "Pressure at Sea Level";
Blockly.Msg.senseBox_pressure_referencePressure_dim = "hPa";
Blockly.Msg.senseBox_pressure_sensor =
"Airpressure/Temperature Sensor (BMP280)";
Blockly.Msg.senseBox_pressure_tip =
"airpressure sensor can measure the airpressure in Pa";
"airpressure sensor can measure the airpressure in hPa";
Blockly.Msg.senseBox_rgb_led = "RGB-LED";
Blockly.Msg.senseBox_rgb_led_tip = "RGB-LED";
Blockly.Msg.senseBox_sd_create_file = "Create file on SD-Card";

View File

@ -94,7 +94,7 @@ class Toolbox extends React.Component {
<Block type="sensebox_wifi" />
<Block type="sensebox_wifi_status" />
<Block type="sensebox_wifi_rssi" />
<Block type="sensebox_wifi_ip" />
<Block type="sensebox_get_ip" />
<Block type="sensebox_startap" />
</Category>
<Category name="SD" colour={getColour().sensebox}>