Commit 5d56f545 authored by Philippe Kalaf's avatar Philippe Kalaf

Use thread in pressuresensor instead of doing all work in ISR

parent c79ee807
......@@ -19,7 +19,11 @@
/* Analog output pressure sensor driver */
#include "pressuresensor.h"
PressureSensor::PressureSensor(PinName pin) : _input(pin) { // create the AnalogIn
PressureSensor::PressureSensor(PinName pin) :
_input(pin),
_worker_thread(osPriorityNormal, 256)
{
_worker_thread.start(callback(this, &PressureSensor::_worker));
}
// returns pressure in bar
......@@ -44,15 +48,20 @@ int PressureSensor::read_pa()
return pressure_pascal;
}
void PressureSensor::worker()
void PressureSensor::_worker()
{
if(_read_count != -1)
while(true)
{
_read_count++;
// keep calculating average
_average_pressure = ( _average_pressure * (_read_count-1)
+ read_pa() )
/_read_count;
if(_read_count != -1)
{
_read_count++;
// keep calculating average
_average_pressure = ( _average_pressure * (_read_count-1)
+ read_pa() )
/_read_count;
}
ThisThread::sleep_for(200);
}
}
......@@ -60,12 +69,10 @@ void PressureSensor::start_count()
{
_read_count = 0;
_average_pressure = 0;
_ticker.attach(callback(this, &PressureSensor::worker), 0.1);
}
float PressureSensor::stop_count()
{
_read_count = -1;
_ticker.detach();
return float(_average_pressure) / 100000;
}
......@@ -38,9 +38,9 @@ private:
int _read_count;
int _average_pressure;
Ticker _ticker;
Thread _worker_thread;
void worker();
void _worker();
};
#endif// PressureSensor_h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment