Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

i2C is not multithreading safe #1074

Open
fmuntean opened this issue Sep 25, 2020 · 1 comment
Open

i2C is not multithreading safe #1074

fmuntean opened this issue Sep 25, 2020 · 1 comment

Comments

@fmuntean
Copy link

When using background threads to read the sensors and the main thread to update the screen the board hangs.
A simple program like this hangs after few seconds:

#include <Arduino.h>
#include <mbed.h>

#include <RGB_LED.h>
#include <I2C.h>
#include <HTS221Sensor.h>

Thread _background;

RGB_LED led;

DevI2C* i2c;
HTS221Sensor* sensor;
void setupSensor(){
i2c = new DevI2C(D14, D15);
sensor = new HTS221Sensor(*i2c); //Temperature and Humidity Sensor
sensor -> init(NULL); // init the sensor
sensor -> enable();// enable
// read id
unsigned char id;
sensor -> readId(&id);
Serial.printf("HTS221 ID: %d\r\n", id);
}

void readSensor(){
// get humidity
float humidity, temperature;
sensor -> getHumidity(&humidity);
Serial.printf("Humidity: %.1f%%\n\r",humidity);
// get temperature
sensor -> getTemperature(&temperature);
Serial.printf("Temperature: %.1fC\n\r",temperature);
}

static void backgroundTask(){
while(1){
led.setColor(0,5,0);
delay(500);
led.turnOff();
delay(500);

readSensor();

}
}

void setup(){
Serial.begin(115200);

Screen.init();

Screen.print("Setup");
setupSensor();
readSensor();
_background.start(&backgroundTask);

Screen.print("Setup Complete");

}

unsigned long uptime=0;
void loop(){
Serial.println(uptime++);
delay(1000);
readSensor();
Screen.print(3,"..");

}

@github-actions
Copy link

This issue has no recent activities, please take a look and provide updates for it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant