Driver for TMP06 temperature sensor. Data is sent by PWM. Tested on Hani-IoT board with TMP06BRTZ sensor.

Example usage

main.cpp

#include "mbed.h"
#include "TMP06.h"

int main()
{
    TMP06 temp_sensor(P0_1);
    float temperature;

    while (true) {
        if(temp_sensor.read(&temperature) == SUCCESS) {
            printf("Temperature: %f\n", temperature);
        }
        else {
            printf("Error!\n");
        }

        ThisThread::sleep_for(2000);
    }
}
Revision:
2:1ff2f041925a
Parent:
1:82fcfd05add8
--- a/TMP06.cpp	Tue Feb 25 11:36:34 2020 +0100
+++ b/TMP06.cpp	Tue Mar 03 14:28:12 2020 +0100
@@ -15,14 +15,15 @@
 int TMP06::read(float *temperature)
 {
     _pwm_pin.disable_irq();
-    _first_run = true;
+    _first_run = true; // blocker for "rise"
+    _started = false;  // blocker for "fall"
     _timer1.reset();
     _timer2.reset();
     _pwm_pin.rise(callback(this, &TMP06::on_rise));
     _pwm_pin.fall(callback(this, &TMP06::on_fall));
     _pwm_pin.enable_irq();
 
-    // 200 - period of sensor should be around 100, 
+    // 250 - period of sensor should be around 100, 
     // more would means there is some kind of issue
     if (_semaphore.try_acquire_for(SEM_TIMEOUT_MS)) {
         // based on TMP05/TMP06 datasheet rev. C
@@ -39,6 +40,7 @@
     if (_first_run) {
         _timer1.start();
         _first_run = false;
+        _started = true;
     }
     else {
         _timer2.stop();
@@ -49,6 +51,9 @@
 
 void TMP06::on_fall()
 {
-    _timer1.stop();
-    _timer2.start();
+    if (_started) {
+        _timer1.stop();
+        _timer2.start();
+        _started = false;
+    }
 }