Arrow / TMP06
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TMP06.cpp Source File

TMP06.cpp

00001 /* Copyright (C) 2020 Arrow Electronics */
00002 
00003 #include "TMP06.h"
00004 
00005 TMP06::TMP06(PinName pin) : _pwm_pin(pin), _semaphore(0, 1)
00006 {
00007 
00008 }
00009 
00010 TMP06::~TMP06() 
00011 {
00012     
00013 }
00014 
00015 int TMP06::read(float *temperature)
00016 {
00017     _pwm_pin.disable_irq();
00018     _first_run = true; // blocker for "rise"
00019     _started = false;  // blocker for "fall"
00020     _timer1.reset();
00021     _timer2.reset();
00022     _pwm_pin.rise(callback(this, &TMP06::on_rise));
00023     _pwm_pin.fall(callback(this, &TMP06::on_fall));
00024     _pwm_pin.enable_irq();
00025 
00026     // 250 - period of sensor should be around 100, 
00027     // more would means there is some kind of issue
00028     if (_semaphore.try_acquire_for(SEM_TIMEOUT_MS)) {
00029         // based on TMP05/TMP06 datasheet rev. C
00030         *temperature = (421 - 751 * ((float)_timer1.read_us() / (float)_timer2.read_us()));
00031         return SUCCESS;
00032     }
00033     else {
00034         return FAILURE;
00035     }
00036 }
00037 
00038 void TMP06::on_rise()
00039 {
00040     if (_first_run) {
00041         _timer1.start();
00042         _first_run = false;
00043         _started = true;
00044     }
00045     else {
00046         _timer2.stop();
00047         _semaphore.release();
00048         _pwm_pin.disable_irq();
00049     }
00050 }
00051 
00052 void TMP06::on_fall()
00053 {
00054     if (_started) {
00055         _timer1.stop();
00056         _timer2.start();
00057         _started = false;
00058     }
00059 }