update code generation to replace mcu library
This commit is contained in:
@@ -7,12 +7,13 @@ import Blockly from "blockly";
|
||||
|
||||
Blockly.Arduino.sensebox_sensor_temp_hum = function () {
|
||||
var dropdown_name = this.getFieldValue("NAME");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.definitions_["define_hdc"] = "HDC1080 hdc;";
|
||||
|
||||
Blockly.Arduino.libraries_["library_adafruithdc1000"] =
|
||||
"#include <Adafruit_HDC1000.h>";
|
||||
Blockly.Arduino.definitions_["define_hdc"] =
|
||||
"Adafruit_HDC1000 hdc = Adafruit_HDC1000();;";
|
||||
Blockly.Arduino.setupCode_["sensebox_sensor_temp_hum"] = "hdc.begin();";
|
||||
var code = "hdc.get" + dropdown_name + "()";
|
||||
var code = `hdc.read${dropdown_name}()`;
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
@@ -24,24 +25,114 @@ Blockly.Arduino.sensebox_sensor_temp_hum = function () {
|
||||
Blockly.Arduino.sensebox_sensor_uv_light = function () {
|
||||
var dropdown_name = this.getFieldValue("NAME");
|
||||
let code = "";
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
if (dropdown_name === "UvIntensity") {
|
||||
Blockly.Arduino.definitions_["define_veml"] = "VEML6070 veml;";
|
||||
Blockly.Arduino.libraries_["library_veml6070"] = "#include <VEML6070.h>";
|
||||
Blockly.Arduino.definitions_["define_veml"] = "VEML6070 veml;;";
|
||||
Blockly.Arduino.setupCode_["sensebox_sensor_uv_light"] = "veml.begin();";
|
||||
code = "veml.get" + dropdown_name + "()";
|
||||
}
|
||||
// if (dropdown_name === 'Illuminance') {
|
||||
// Blockly.Arduino.definitions_['define_tsl'] = 'TSL45315 tsl;'
|
||||
// Blockly.Arduino.setupCode_['sensebox_sensor_illuminance'] = 'tsl.begin();'
|
||||
// code = 'tsl.get' + dropdown_name + '()';
|
||||
// }
|
||||
if (dropdown_name === "Illuminance") {
|
||||
Blockly.Arduino.definitions_["define_lightsensor"] =
|
||||
"Lightsensor lightsensor;";
|
||||
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;
|
||||
}
|
||||
`;
|
||||
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();
|
||||
}`;
|
||||
|
||||
Blockly.Arduino.codeFunctions_["Lightsensor_begin"] = `
|
||||
|
||||
|
||||
|
||||
void Lightsensor_begin()
|
||||
{
|
||||
Wire.begin();
|
||||
unsigned int u = 0;
|
||||
DEBUG(F("Checking lightsensortype"));
|
||||
u = read_reg(0x29, 0x80 | 0x0A); //id register
|
||||
if ((u & 0xF0) == 0xA0) // TSL45315
|
||||
{
|
||||
DEBUG(F("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
|
||||
{
|
||||
DEBUG(F("LTR329"));
|
||||
LTR.begin();
|
||||
LTR.setControl(gain, false, false);
|
||||
LTR.setMeasurementRate(integrationTime, measurementRate);
|
||||
LTR.setPowerUp(); //power on with default settings
|
||||
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 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));
|
||||
else DEBUG(F("LTR sensor saturated"));
|
||||
if(lux > 0) break;
|
||||
else delay(10);
|
||||
}
|
||||
else {
|
||||
DEBUG2(F("LTR getData error "));
|
||||
byte error = LTR.getError();
|
||||
Serial.println(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
`;
|
||||
Blockly.Arduino.setupCode_["sensebox_sensor_illuminance"] =
|
||||
"lightsensor.begin();";
|
||||
code = "lightsensor.get" + dropdown_name + "()";
|
||||
"Lightsensor_begin()";
|
||||
code = "Lightsensor_getIlluminance();";
|
||||
}
|
||||
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
@@ -55,8 +146,7 @@ Blockly.Arduino.sensebox_sensor_uv_light = function () {
|
||||
Blockly.Arduino.sensebox_sensor_bmx055_accelerometer = function () {
|
||||
var dropdown_value = this.getFieldValue("VALUE");
|
||||
var range = this.getFieldValue("RANGE");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.definitions_["define_bmx"] = "BMX055 bmx;";
|
||||
Blockly.Arduino.setupCode_["sensebox_sensor_bmx055"] =
|
||||
"bmx.beginAcc(" + range + ");";
|
||||
@@ -72,14 +162,17 @@ Blockly.Arduino.sensebox_sensor_bmx055_accelerometer = function () {
|
||||
Blockly.Arduino.sensebox_sensor_sds011 = function () {
|
||||
var dropdown_name = this.getFieldValue("NAME");
|
||||
var serial_name = this.getFieldValue("SERIAL");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.codeFunctions_["define_sds011"] =
|
||||
"SDS011 my_sds(" + serial_name + ");";
|
||||
Blockly.Arduino.variables_["variables_sds011"] = "float p10,p25;\n";
|
||||
Blockly.Arduino.setupCode_["sensebox_sensor_sds011"] =
|
||||
serial_name + ".begin(9600);";
|
||||
var code = "my_sds.get" + dropdown_name + "()";
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_["SdsDustSensor"] = `#include "SdsDustSensor.h"`;
|
||||
Blockly.Arduino.definitions_["define_sds011"] =
|
||||
"SdsDustSensor sds(" + serial_name + ");";
|
||||
Blockly.Arduino.setupCode_["sds011_begin"] = "sds.begin();";
|
||||
Blockly.Arduino.setupCode_["sds011_setActiveReporting"] =
|
||||
"sds.setActiveReportingMode();";
|
||||
Blockly.Arduino.loopCodeOnce_[
|
||||
"sds011_getData"
|
||||
] = `PmResult pm = sds.readPm();`;
|
||||
var code = `pm.${dropdown_name}`;
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
@@ -92,14 +185,23 @@ Blockly.Arduino.sensebox_sensor_pressure = function () {
|
||||
var dropdown_name = this.getFieldValue("NAME");
|
||||
var code = "";
|
||||
var referencePressure = this.getFieldValue("referencePressure");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.definitions_["define_pressure"] = "BMP280 bmp_sensor;";
|
||||
Blockly.Arduino.setupCode_["sensebox_bmp_sensor"] = "bmp_sensor.begin();";
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_[
|
||||
"adafruit_bmp280"
|
||||
] = `#include <Adafruit_BMP280.h>`;
|
||||
Blockly.Arduino.definitions_["define_pressure"] = "Adafruit_BMP280 bmp;";
|
||||
Blockly.Arduino.setupCode_["sensebox_bmp_sensor"] = "bmp.begin();";
|
||||
Blockly.Arduino.setupCode_["bmp_setSampling"] = `
|
||||
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
|
||||
Adafruit_BMP280::SAMPLING_X2,
|
||||
Adafruit_BMP280::SAMPLING_X16,
|
||||
Adafruit_BMP280::FILTER_X16,
|
||||
Adafruit_BMP280::STANDBY_MS_500);
|
||||
`;
|
||||
if (dropdown_name === "Pressure" || dropdown_name === "Temperature") {
|
||||
code = "bmp_sensor.get" + dropdown_name + "()";
|
||||
code = "bmp.read" + dropdown_name + "()";
|
||||
} else if (dropdown_name === "Altitude") {
|
||||
code = "bmp_sensor.getAltitude(" + referencePressure + ")";
|
||||
code = "bmp.readAltitude(" + referencePressure + ")";
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
@@ -223,18 +325,19 @@ Blockly.Arduino.sensebox_sensor_ultrasonic_ranger = function () {
|
||||
var dropdown_pin_RX = this.getFieldValue("ultrasonic_trigger");
|
||||
var dropdown_pin_TX = this.getFieldValue("ultrasonic_echo");
|
||||
var port = this.getFieldValue("port");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.definitions_["var_ultrasonic" + port] =
|
||||
"Ultrasonic Ultrasonic" +
|
||||
port +
|
||||
"(" +
|
||||
dropdown_pin_RX +
|
||||
"," +
|
||||
dropdown_pin_TX +
|
||||
");";
|
||||
var maxDistance = this.getFieldValue("maxDistance");
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_["library_newPing"] = `#include <NewPing.h>`;
|
||||
Blockly.Arduino.variables_["define_newPingVariables" + port] = `
|
||||
#define TRIGGER_PIN_${port} ${dropdown_pin_RX}
|
||||
#define ECHO_PIN_${port} ${dropdown_pin_TX}
|
||||
#define MAX_DISTANCE_${port} ${maxDistance}
|
||||
`;
|
||||
Blockly.Arduino.definitions_[
|
||||
"define_newPing" + port
|
||||
] = `NewPing sonar${port}(TRIGGER_PIN_${port}, ECHO_PIN_${port}, MAX_DISTANCE_${port})`;
|
||||
var code;
|
||||
code = "Ultrasonic" + port + ".getDistance()";
|
||||
code = `sonar${port}.ping_cm()`;
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
@@ -245,11 +348,37 @@ Blockly.Arduino.sensebox_sensor_ultrasonic_ranger = function () {
|
||||
|
||||
Blockly.Arduino.sensebox_sensor_sound = function () {
|
||||
var dropdown_pin = this.getFieldValue("PIN");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.definitions_["define_microphone"] =
|
||||
"Microphone microphone(" + dropdown_pin + ");";
|
||||
var code = "microphone.getValue()";
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.codeFunctions_["define_sound"] = `
|
||||
float getSoundValue(int pin) {
|
||||
unsigned long start = millis(); // Start des Messintervalls
|
||||
unsigned int peakToPeak = 0; // Abstand von maximalem zu minimalem Amplitudenausschlag
|
||||
unsigned int signalMax = 0;
|
||||
unsigned int signalMin = 1023;
|
||||
const int sampleTime = 100;
|
||||
unsigned int micValue;
|
||||
|
||||
// Sammle Daten für 100 Millisekunden
|
||||
while (millis() - start < sampleTime)
|
||||
{
|
||||
micValue = analogRead(pin); // Messe den aktuellen Wert
|
||||
if (micValue < 1023) // sortiere Fehlmessungen aus, deren Werte über dem max Wert 1024 liegen
|
||||
{
|
||||
if (micValue > signalMax)
|
||||
{
|
||||
signalMax = micValue; // speichere den maximal gemessenen Wert
|
||||
}
|
||||
else if (micValue < signalMin)
|
||||
{
|
||||
signalMin = micValue; // speichere den minimal gemessenen Wert
|
||||
}
|
||||
}
|
||||
}
|
||||
peakToPeak = signalMax - signalMin; // max - min = Abstand von maximalem zu minimalem Amplitudenausschlag
|
||||
double volts = (peakToPeak * 5.0) / 1023; // wandle in Volt um
|
||||
return volts;
|
||||
}`;
|
||||
var code = "getSoundValue(" + dropdown_pin + ")";
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
@@ -261,12 +390,14 @@ Blockly.Arduino.sensebox_sensor_sound = function () {
|
||||
Blockly.Arduino.sensebox_button = function () {
|
||||
var dropdown_pin = this.getFieldValue("PIN");
|
||||
var dropown_function = this.getFieldValue("FUNCTION");
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_["library_jcButtons"] = `#include <JC_Button.h>`;
|
||||
Blockly.Arduino.definitions_["define_button" + dropdown_pin + ""] =
|
||||
"Button button_" + dropdown_pin + "(" + dropdown_pin + ");";
|
||||
Blockly.Arduino.setupCode_["setup_button" + dropdown_pin + ""] =
|
||||
"button_" + dropdown_pin + ".begin();";
|
||||
Blockly.Arduino.loopCodeOnce_["loop_button" + dropdown_pin + ""] =
|
||||
"button_" + dropdown_pin + ".read();";
|
||||
var code = "";
|
||||
if (dropown_function === "isPressed") {
|
||||
code = "button_" + dropdown_pin + ".isPressed()";
|
||||
@@ -274,6 +405,9 @@ Blockly.Arduino.sensebox_button = function () {
|
||||
code = "button_" + dropdown_pin + ".getSwitch()";
|
||||
} else if (dropown_function === "wasPressed") {
|
||||
code = "button_" + dropdown_pin + ".wasPressed()";
|
||||
} else if (dropown_function === "longPress") {
|
||||
var time = this.getFieldValue("time");
|
||||
code = "button_" + dropdown_pin + ".pressedFor(" + time + ")";
|
||||
}
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
@@ -285,10 +419,9 @@ Blockly.Arduino.sensebox_button = function () {
|
||||
|
||||
Blockly.Arduino.sensebox_scd30 = function () {
|
||||
var dropdown = this.getFieldValue("dropdown");
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_["scd30_library"] =
|
||||
'#include "SparkFun_SCD30_Arduino_Library.h"';
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.definitions_["SCD30"] = "SCD30 airSensor;";
|
||||
Blockly.Arduino.setupCode_["init_scd30"] = ` Wire.begin();
|
||||
if (airSensor.begin() == false)
|
||||
@@ -320,11 +453,10 @@ Blockly.Arduino.sensebox_scd30 = function () {
|
||||
|
||||
Blockly.Arduino.sensebox_gps = function () {
|
||||
var dropdown = this.getFieldValue("dropdown");
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_["gps_library"] =
|
||||
"#include <SparkFun_u-blox_GNSS_Arduino_Library.h>";
|
||||
Blockly.Arduino.libraries_["library_wire"] = "#include <Wire.h>";
|
||||
Blockly.Arduino.libraries_["library_senseBoxMCU"] =
|
||||
'#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.definitions_["GPS"] = "SFE_UBLOX_GNSS myGNSS;";
|
||||
Blockly.Arduino.setupCode_["init_gps"] = ` Wire.begin();
|
||||
|
||||
@@ -384,6 +516,7 @@ return tsBuffer;
|
||||
*/
|
||||
|
||||
Blockly.Arduino.sensebox_sensor_truebner_smt50 = function () {
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
var dropdown_port = this.getFieldValue("Port");
|
||||
var dropdown_value = this.getFieldValue("value");
|
||||
var dropdown_pin = 1;
|
||||
@@ -436,6 +569,7 @@ Blockly.Arduino.sensebox_sensor_watertemperature = function () {
|
||||
if (dropdown_port === "C") {
|
||||
dropdown_pin = 5;
|
||||
}
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
Blockly.Arduino.libraries_["library_oneWire"] = '#include "OneWire.h"';
|
||||
Blockly.Arduino.libraries_["library_oneDallasTemperature"] =
|
||||
'#include "DallasTemperature.h"';
|
||||
@@ -489,6 +623,7 @@ float getWindspeed(){
|
||||
*/
|
||||
|
||||
Blockly.Arduino.sensebox_soundsensor_dfrobot = function () {
|
||||
Blockly.Arduino.libraries_["library_senseBoxIO"] = "#include <senseBoxIO.h>";
|
||||
var dropdown_port = this.getFieldValue("Port");
|
||||
var dropdown_pin = 1;
|
||||
if (dropdown_port === "A") {
|
||||
|
||||
Reference in New Issue
Block a user