Andrea Faustinelli / espresso-for-geeks-master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lmt01.cpp Source File

lmt01.cpp

00001 /* Copyright (c) 2017 Philippe Kalaf, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 /* LMT01 temperature sensor driver */
00020 #include "lmt01.h"
00021 
00022 /* Taken from datasheet */
00023 const int LMT01::_pulse_temp_table[20][2] = {
00024                  {-40, 181}, {-30, 338}, {-20, 494}, {-10, 651}, {0, 808}, 
00025                  {10, 966}, {20, 1125}, {30, 1284}, {40, 1443}, {50, 1603},
00026                  {60, 1762}, {70, 1923}, {80, 2084}, {90, 2245}, {100, 2407},
00027                  {110, 2569}, {120, 2731}, {130, 2894}, {140, 3058}, {150, 3220}
00028                  };
00029 
00030 LMT01::LMT01(PinName pin) : _interrupt(pin), _worker_thread(osPriorityNormal, 256) {        // create the InterruptIn on the pin specified to LMT01
00031 
00032     _interrupt.mode(PullUp); // an internal pull up is used, otherwise disable here
00033 
00034     // Start worker thread for counting pulses and calculating temp
00035     _worker_thread.start(callback(this, &LMT01::_worker));
00036     _interrupt.fall(callback(this, &LMT01::_increment)); // attach increment function of this counter instance
00037 }
00038 
00039 void LMT01::_worker()
00040 {
00041     uint16_t i;
00042 
00043     while(true)
00044     {
00045         ThisThread::sleep_for(250);
00046         _pulse_count = 0;
00047         _last_pulse_count = 0;
00048 
00049         // Let's skip the first potentially partial pulse train
00050         while (_pulse_count != _last_pulse_count || _pulse_count == 0 || _last_pulse_count == 0)
00051         {
00052             _last_pulse_count = _pulse_count;
00053             ThisThread::sleep_for(1);
00054         }
00055 
00056         // OK let's now count the next pulse train from the start
00057         _pulse_count = 0;
00058         _last_pulse_count = 0;
00059 
00060         while (_pulse_count != _last_pulse_count || _pulse_count == 0 || _last_pulse_count == 0)
00061         {
00062             _last_pulse_count = _pulse_count;
00063             ThisThread::sleep_for(1);
00064         }
00065 
00066         // Find pulse/temp range from table
00067         for (i = 0; i < sizeof(_pulse_temp_table); i++)
00068             if (_last_pulse_count < _pulse_temp_table[i][1])
00069                 break;
00070 
00071         // Read/convert/store count as temperature
00072         _temperature = 
00073             (
00074              (
00075               ((_last_pulse_count - _pulse_temp_table[i-1][1]) * 1000) / 
00076               (_pulse_temp_table[i][1] - _pulse_temp_table[i-1][1])
00077              ) * 10 // at this point unit is 10^3 Celsius
00078             ) 
00079             + _pulse_temp_table[i-1][0] * 1000;
00080 
00081         ThisThread::sleep_for(250);
00082     }
00083 }
00084 
00085 void LMT01::_increment()
00086 {
00087         _pulse_count++;
00088 }
00089 
00090 // returns temperature in 10^3 Celsius
00091 int LMT01::read_int()
00092 {
00093     return _temperature;
00094 }
00095 
00096 // return temperature in Celsius
00097 float LMT01::read()
00098 {
00099     return float(_temperature)/1000;
00100 }
00101 
00102 uint16_t LMT01::get_last_pulse_count()
00103 {
00104     return _last_pulse_count;
00105 }