Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.fhg.iais.roberta.syntax.sensor.generic;

import de.fhg.iais.roberta.syntax.sensor.ExternalSensor;
import de.fhg.iais.roberta.transformer.forClass.F2M;
import de.fhg.iais.roberta.transformer.forClass.NepoExpr;
import de.fhg.iais.roberta.util.ast.BlocklyProperties;
import de.fhg.iais.roberta.util.ast.ExternalSensorBean;

@NepoExpr(name = "SCD40_SENSING", category = "SENSOR", blocklyNames = {"robSensors_scd40_getSample"},
sampleValues = {@F2M(field = "SCD40_CO2", mode = "CO2"), @F2M(field = "SCD40_TEMPERATURE", mode = "TEMPERATURE"), @F2M(field = "SCD40_HUMIDITY", mode = "HUMIDITY")})
public final class Scd40Sensor extends ExternalSensor {

public Scd40Sensor(BlocklyProperties properties, ExternalSensorBean externalSensorBean) {
super(properties, externalSensorBean);
setReadOnly();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class AstFactory {
* the list of all legal modes used in the blockly frontend
*/
private static final String[] allLegalModesArray = {
"ALTITUDE", "AMBIENTLIGHT", "ANALOG", "ANGLE", "BACKWARD", "BALL", "CALIBRATE", "CALIBRATION", "CLOSING", "CO2EQUIVALENT", "COLOUR", "COMPASS",
"ALTITUDE", "AMBIENTLIGHT", "ANALOG", "ANGLE", "BACKWARD", "BALL", "CALIBRATE", "CALIBRATION", "CLOSING", "CO2", "CO2EQUIVALENT", "COLOUR", "COMPASS",
"DATE", "DEFAULT", "DEGREE", "DIGITAL", "DISTANCE", "DOWN", "EDISON_CODE", "FACE_DOWN", "FACE_UP", "FOREWARD", "FREEFALL",
"G3", "G6", "G8", "HUMIDITY", "IAQ", "IDALL", "IDONE", "LATITUDE", "LEFT", "LIGHT", "LIGHT_VALUE", "LINE", "LONGITUDE", "MODULATED", "MOTION",
"NAMEALL", "NAMEONE", "NONE", "OBSTACLE", "OFF", "OPENING", "NUMBERLINES", "PM10", "PM25", "PRESENCE", "PRESSED", "PRESSURE", "PULSEHIGH", "PULSELOW", "RATE",
Expand Down Expand Up @@ -247,4 +247,4 @@ private static void addToSampleValues(F2M[] fieldToModeArray, BlockDescriptor bl
getSensorSampleMap.put(f2M.field(), blockDescriptor);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface SC {
String ULTRASONIC = "ULTRASONIC";
String SOUND = "SOUND";
String DISTANCE = "DISTANCE";
String CO2 = "CO2";
String RESET = "RESET";
String ROTATION = "ROTATION";
String DEGREE = "DEGREE";
Expand Down Expand Up @@ -128,6 +129,7 @@ public interface SC {
String MUSIC = "MUSIC";
String ENVIRONMENTAL = "ENVIRONMENTAL";
String COLOURTCS3472 = "COLOURTCS3472";
String SCD40 = "SCD40";
String LED_BUILTIN = "LED_BUILTIN";
String GREEN = "GREEN";
String BLUE = "BLUE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import de.fhg.iais.roberta.syntax.sensor.generic.PinTouchSensor;
import de.fhg.iais.roberta.syntax.sensor.generic.PulseSensor;
import de.fhg.iais.roberta.syntax.sensor.generic.RfidSensor;
import de.fhg.iais.roberta.syntax.sensor.generic.Scd40Sensor;
import de.fhg.iais.roberta.syntax.sensor.generic.SoundSensor;
import de.fhg.iais.roberta.syntax.sensor.generic.TemperatureSensor;
import de.fhg.iais.roberta.syntax.sensor.generic.TimerReset;
Expand Down Expand Up @@ -767,6 +768,10 @@ public Phrase visitHumiditySensor(HumiditySensor humiditySensor) {
return new HumiditySensor(humiditySensor.getProperty(), new ExternalSensorBean(humiditySensor.getUserDefinedPort(), humiditySensor.getMode(), humiditySensor.getSlot(), humiditySensor.getMutation()));
}

public Phrase visitScd40Sensor(Scd40Sensor scd40Sensor) {
return new Scd40Sensor(scd40Sensor.getProperty(), new ExternalSensorBean(scd40Sensor.getUserDefinedPort(), scd40Sensor.getMode(), scd40Sensor.getSlot(), scd40Sensor.getMutation()));
}

public Phrase visitMotionSensor(MotionSensor motionSensor) {
return new MotionSensor(motionSensor.getProperty(), new ExternalSensorBean(motionSensor.getUserDefinedPort(), motionSensor.getMode(), motionSensor.getSlot(), motionSensor.getMutation()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, CO2, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, TEMPERATURE, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, HUMIDITY, - EMPTY_SLOT -]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, CO2, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, TEMPERATURE, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, HUMIDITY, - EMPTY_SLOT -]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, CO2, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, TEMPERATURE, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, HUMIDITY, - EMPTY_SLOT -]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, CO2, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, TEMPERATURE, - EMPTY_SLOT -]]]
SerialWriteAction[value: SensorExpr [Scd40Sensor [SCD, HUMIDITY, - EMPTY_SLOT -]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[UsedSensor [SCD, SCD40, CO2], UsedSensor [SCD, SCD40, TEMPERATURE], UsedSensor [SCD, SCD40, HUMIDITY]]
Actors:
[]
Methods:
[SCD40_GET_SAMPLE]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[UsedSensor [SCD, SCD40, CO2], UsedSensor [SCD, SCD40, TEMPERATURE], UsedSensor [SCD, SCD40, HUMIDITY]]
Actors:
[]
Methods:
[SCD40_GET_SAMPLE]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[UsedSensor [SCD, SCD40, CO2], UsedSensor [SCD, SCD40, TEMPERATURE], UsedSensor [SCD, SCD40, HUMIDITY]]
Actors:
[]
Methods:
[SCD40_GET_SAMPLE]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[UsedSensor [SCD, SCD40, CO2], UsedSensor [SCD, SCD40, TEMPERATURE], UsedSensor [SCD, SCD40, HUMIDITY]]
Actors:
[]
Methods:
[SCD40_GET_SAMPLE]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#define _GNU_SOURCE

#include "MicroBit.h"
#include "NEPODefs.h"
#include <list>
#include <array>
#include <stdlib.h>
MicroBit _uBit;
MicroBitI2C _i2c(MICROBIT_PIN_P20, MICROBIT_PIN_P19);


double scd40_get_sample(int mode);


int main()
{
_uBit.init();

_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(0))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(0)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(1))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(1)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(2))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(2)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
release_fiber();
}

double scd40_get_sample(int mode) {
static bool started = false;
static double values[3] = { 0, 0, 0 };
const int address = 0x62 << 1;
if ( !started ) {
char startCommand[2] = { 0x21, (char) 0xb1 };
_i2c.write(address, startCommand, 2);
_uBit.sleep(5000);
started = true;
}

char readCommand[2] = { (char) 0xec, 0x05 };
char data[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
_i2c.write(address, readCommand, 2);
_uBit.sleep(1);
if ( _i2c.read(address, data, 9) == 0 ) {
bool crcValid = true;
for ( int word = 0; word < 3; word++ ) {
uint8_t crc = 0xff;
for ( int i = 0; i < 2; i++ ) {
crc ^= (uint8_t) data[word * 3 + i];
for ( int bit = 0; bit < 8; bit++ ) {
if ( crc & 0x80 ) {
crc = (uint8_t) ((crc << 1) ^ 0x31);
} else {
crc = (uint8_t) (crc << 1);
}
}
}
if ( crc != (uint8_t) data[word * 3 + 2] ) {
crcValid = false;
}
}
if ( crcValid ) {
uint16_t co2 = ((uint8_t) data[0] << 8) | (uint8_t) data[1];
uint16_t rawTemperature = ((uint8_t) data[3] << 8) | (uint8_t) data[4];
uint16_t rawHumidity = ((uint8_t) data[6] << 8) | (uint8_t) data[7];
values[0] = co2;
values[1] = -45.0 + 175.0 * rawTemperature / 65535.0;
values[2] = 100.0 * rawHumidity / 65535.0;
}
}
return values[mode];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#define _GNU_SOURCE

#include "MicroBit.h"
#include "NEPODefs.h"
#include <list>
#include <array>
#include <stdlib.h>
MicroBit _uBit;
MicroBitI2C _i2c(MICROBIT_PIN_P20, MICROBIT_PIN_P19);


double scd40_get_sample(int mode);


int main()
{
_uBit.init();

_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(0))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(0)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(1))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(1)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(2))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(2)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
release_fiber();
}

double scd40_get_sample(int mode) {
static bool started = false;
static double values[3] = { 0, 0, 0 };
const int address = 0x62 << 1;
if ( !started ) {
char startCommand[2] = { 0x21, (char) 0xb1 };
_i2c.write(address, startCommand, 2);
_uBit.sleep(5000);
started = true;
}

char readCommand[2] = { (char) 0xec, 0x05 };
char data[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
_i2c.write(address, readCommand, 2);
_uBit.sleep(1);
if ( _i2c.read(address, data, 9) == 0 ) {
bool crcValid = true;
for ( int word = 0; word < 3; word++ ) {
uint8_t crc = 0xff;
for ( int i = 0; i < 2; i++ ) {
crc ^= (uint8_t) data[word * 3 + i];
for ( int bit = 0; bit < 8; bit++ ) {
if ( crc & 0x80 ) {
crc = (uint8_t) ((crc << 1) ^ 0x31);
} else {
crc = (uint8_t) (crc << 1);
}
}
}
if ( crc != (uint8_t) data[word * 3 + 2] ) {
crcValid = false;
}
}
if ( crcValid ) {
uint16_t co2 = ((uint8_t) data[0] << 8) | (uint8_t) data[1];
uint16_t rawTemperature = ((uint8_t) data[3] << 8) | (uint8_t) data[4];
uint16_t rawHumidity = ((uint8_t) data[6] << 8) | (uint8_t) data[7];
values[0] = co2;
values[1] = -45.0 + 175.0 * rawTemperature / 65535.0;
values[2] = 100.0 * rawHumidity / 65535.0;
}
}
return values[mode];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#define _GNU_SOURCE

#include "MicroBit.h"
#include "NEPODefs.h"
#include <list>
#include <array>
#include <stdlib.h>
MicroBit _uBit;
MicroBitI2C _i2c(MICROBIT_PIN_P20, MICROBIT_PIN_P19);


double scd40_get_sample(int mode);


int main()
{
_uBit.init();

_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(0))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(0)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(1))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(1)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
_uBit.serial.setTxBufferSize(ManagedString((scd40_get_sample(2))).length() + 2);
_uBit.serial.send(ManagedString(scd40_get_sample(2)) + "\r\n", MicroBitSerialMode::ASYNC);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
release_fiber();
}

double scd40_get_sample(int mode) {
static bool started = false;
static double values[3] = { 0, 0, 0 };
const int address = 0x62 << 1;
if ( !started ) {
char startCommand[2] = { 0x21, (char) 0xb1 };
_i2c.write(address, startCommand, 2);
_uBit.sleep(5000);
started = true;
}

char readCommand[2] = { (char) 0xec, 0x05 };
char data[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
_i2c.write(address, readCommand, 2);
_uBit.sleep(1);
if ( _i2c.read(address, data, 9) == 0 ) {
bool crcValid = true;
for ( int word = 0; word < 3; word++ ) {
uint8_t crc = 0xff;
for ( int i = 0; i < 2; i++ ) {
crc ^= (uint8_t) data[word * 3 + i];
for ( int bit = 0; bit < 8; bit++ ) {
if ( crc & 0x80 ) {
crc = (uint8_t) ((crc << 1) ^ 0x31);
} else {
crc = (uint8_t) (crc << 1);
}
}
}
if ( crc != (uint8_t) data[word * 3 + 2] ) {
crcValid = false;
}
}
if ( crcValid ) {
uint16_t co2 = ((uint8_t) data[0] << 8) | (uint8_t) data[1];
uint16_t rawTemperature = ((uint8_t) data[3] << 8) | (uint8_t) data[4];
uint16_t rawHumidity = ((uint8_t) data[6] << 8) | (uint8_t) data[7];
values[0] = co2;
values[1] = -45.0 + 175.0 * rawTemperature / 65535.0;
values[2] = 100.0 * rawHumidity / 65535.0;
}
}
return values[mode];
}
Loading