Commit 3137b1c9 authored by Philippe Kalaf's avatar Philippe Kalaf

Rewrite temperature sensor drivers to be more efficient and enable both sensors

parent c5a1b903
...@@ -45,9 +45,7 @@ BrewControl::BrewControl( PinName brew_pin, ...@@ -45,9 +45,7 @@ BrewControl::BrewControl( PinName brew_pin,
PinName pump_control_pin, PinName pump_control_pin,
PinName pressure_sensor_pin, PinName pressure_sensor_pin,
PinName temp_sensor_pin, PinName temp_sensor_pin,
#ifdef TEMP2
PinName temp2_sensor_pin, PinName temp2_sensor_pin,
#endif
PinName boiler_pwm_pin PinName boiler_pwm_pin
) : ) :
_brew_switch(brew_pin, 0), _brew_switch(brew_pin, 0),
...@@ -570,9 +568,5 @@ uint16_t BrewControl::get_last_pulse_count_side() ...@@ -570,9 +568,5 @@ uint16_t BrewControl::get_last_pulse_count_side()
uint16_t BrewControl::get_last_pulse_count_top() uint16_t BrewControl::get_last_pulse_count_top()
{ {
#ifdef TEMP2
return _temp2_sensor.get_last_pulse_count(); return _temp2_sensor.get_last_pulse_count();
#else
return 0;
#endif
} }
...@@ -27,47 +27,67 @@ const int LMT01::_pulse_temp_table[20][2] = { ...@@ -27,47 +27,67 @@ const int LMT01::_pulse_temp_table[20][2] = {
{110, 2569}, {120, 2731}, {130, 2894}, {140, 3058}, {150, 3220} {110, 2569}, {120, 2731}, {130, 2894}, {140, 3058}, {150, 3220}
}; };
LMT01::LMT01(PinName pin) : _interrupt(pin) { // create the InterruptIn on the pin specified to LMT01 LMT01::LMT01(PinName pin) : _interrupt(pin), _worker_thread(osPriorityNormal, 1024) { // create the InterruptIn on the pin specified to LMT01
_interrupt.mode(PullUp); // an internal pull up is used, otherwise disable here
_interrupt.fall(callback(this, &LMT01::increment)); // attach increment function of this counter instance _interrupt.mode(PullUp); // an internal pull up is used, otherwise disable here
_pulse_timer.start(); _interrupt.fall(callback(this, &LMT01::_increment)); // attach increment function of this counter instance
_ticker.attach(callback(this, &LMT01::worker), 0.5);
// Start worker thread for counting pulses and calculating temp
_worker_thread.start(callback(this, &LMT01::_worker));
} }
void LMT01::worker() void LMT01::_worker()
{ {
// Find pulse/temp range from table
uint16_t i; uint16_t i;
for (i = 0; i < sizeof(_pulse_temp_table); i++)
if (_last_pulse_count < _pulse_temp_table[i][1])
break;
// Read/convert/store count as temperature
_temperature =
(
(
((_last_pulse_count - _pulse_temp_table[i-1][1]) * 1000) /
(_pulse_temp_table[i][1] - _pulse_temp_table[i-1][1])
) * 10 // at this point unit is 10^3 Celsius
)
+ _pulse_temp_table[i-1][0] * 1000;
}
void LMT01::increment() while(true)
{
// Detect when pulses stop so we convert the reading and reset timer
// 10ms is long enough to indicate pulses have stopped
if (_pulse_timer.read_ms() > 10)
{ {
_pulse_timer.reset(); _interrupt.enable_irq();
_last_pulse_count = _pulse_count; ThisThread::sleep_for(250);
_pulse_count = 0; _pulse_count = 0;
_last_pulse_count = 0;
// Let's skip the first potentially partial pulse train
while (_pulse_count != _last_pulse_count || _pulse_count == 0 || _last_pulse_count == 0)
{
_last_pulse_count = _pulse_count;
ThisThread::sleep_for(1);
}
// OK let's now count the next pulse train from the start
_pulse_count = 0;
_last_pulse_count = 0;
while (_pulse_count != _last_pulse_count || _pulse_count == 0 || _last_pulse_count == 0)
{
_last_pulse_count = _pulse_count;
ThisThread::sleep_for(1);
}
// Find pulse/temp range from table
for (i = 0; i < sizeof(_pulse_temp_table); i++)
if (_last_pulse_count < _pulse_temp_table[i][1])
break;
// Read/convert/store count as temperature
_temperature =
(
(
((_last_pulse_count - _pulse_temp_table[i-1][1]) * 1000) /
(_pulse_temp_table[i][1] - _pulse_temp_table[i-1][1])
) * 10 // at this point unit is 10^3 Celsius
)
+ _pulse_temp_table[i-1][0] * 1000;
_interrupt.disable_irq();
ThisThread::sleep_for(250);
} }
else }
{
_pulse_timer.reset(); void LMT01::_increment()
{
_pulse_count++; _pulse_count++;
}
} }
// returns temperature in 10^3 Celsius // returns temperature in 10^3 Celsius
......
...@@ -30,7 +30,6 @@ public: ...@@ -30,7 +30,6 @@ public:
static const int _pulse_temp_table[20][2]; static const int _pulse_temp_table[20][2];
void increment();
int read_int(); int read_int();
float read(); float read();
uint16_t get_last_pulse_count(); uint16_t get_last_pulse_count();
...@@ -41,10 +40,10 @@ private: ...@@ -41,10 +40,10 @@ private:
volatile uint16_t _last_pulse_count; volatile uint16_t _last_pulse_count;
// temperature in 10^3 Celsius // temperature in 10^3 Celsius
volatile int _temperature; volatile int _temperature;
Timer _pulse_timer; Thread _worker_thread;
Ticker _ticker;
void worker(); void _worker();
void _increment();
}; };
#endif// lmt01_h #endif// lmt01_h
...@@ -83,14 +83,9 @@ bool power_save; ...@@ -83,14 +83,9 @@ bool power_save;
// pump phasecontrol signal (PUMP_CTRL) // pump phasecontrol signal (PUMP_CTRL)
// pressure sensor (PRESSURE_IN) // pressure sensor (PRESSURE_IN)
// side temperature sensor (TEMP_IN) // side temperature sensor (TEMP_IN)
// top temperature sensor (TEMP2_IN) - Not Implemented // top temperature sensor (TEMP2_IN)
// boiler control ssr (BOILER_CTRL) // boiler control ssr (BOILER_CTRL)
#ifdef TEMP2
BrewControl brew_control(p10, p15, p12, p13, p20, p25, p14, p26); BrewControl brew_control(p10, p15, p12, p13, p20, p25, p14, p26);
#else
BrewControl brew_control(p10, p15, p12, p13, p20, p25, p26);
#endif
// Menu structures // Menu structures
// //
......
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