Steefmaester

[classe/driver] DS1621

Sep 8th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. //By Steef
  2.  
  3. //DS1621.h
  4. #ifndef DS1621_H
  5. #define DS1621_H
  6.  
  7. #include "I2C.h"
  8.  
  9. using namespace std;
  10.  
  11. class DS1621 {
  12.  
  13. private:
  14.    
  15.     int ADDRESS_DS1621;
  16.     I2C *bus;
  17.     enum command
  18.     {
  19.         ReadTemperature = 0xAA,
  20.         ReadCounter = 0xA8,
  21.         ReadSlope = 0xA9,
  22.         StartConvertT = 0xEE,
  23.         StopConvertT = 0x22,
  24.         AccessTH = 0xA1,
  25.         AccessTL = 0xA2,
  26.         AccessConfig = 0xAC
  27.     };
  28.     bool init();
  29.    
  30. public:    
  31.  
  32.     DS1621(int address,I2C *bus);
  33.    
  34.     enum unite
  35.     {
  36.         celcius = 1,
  37.         fahrenheit = 2,
  38.         kelvin = 3
  39.     };
  40.    
  41.    
  42.     float getTemperature(unite choix);
  43.  
  44. };
  45.  
  46. #endif
  47.  
  48.  
  49.  
  50. //DS1621.cpp
  51. #include "DS1621.h"
  52.  
  53. DS1621::DS1621(int address, I2C* bus)
  54. {
  55.     ADDRESS_DS1621 = address;
  56.     this->bus= bus;
  57.    
  58.     init();
  59. }
  60. bool DS1621::init()
  61. {
  62.     vector<unsigned char> buffer;
  63.      
  64.     bus->setAddress(ADDRESS_DS1621);
  65.    
  66.     buffer.push_back(command::AccessConfig);
  67.     buffer.push_back(0x00);
  68.     if(bus->send(buffer) == -1)
  69.         return false;
  70.    
  71.     if(bus->send(command::StartConvertT) == -1)
  72.         return false;
  73.     else
  74.         return true;    
  75. }
  76.  
  77. float DS1621::getTemperature(unite choix)
  78. {
  79.     if(bus->getAddress()!= ADDRESS_DS1621)
  80.         init();
  81.    
  82.     vector<unsigned char> buffer;
  83.     usleep(600000);                   //attente d'initialisation dans le registre
  84.     bus->receive(command::ReadTemperature,buffer, 2);
  85.    
  86.     if(buffer.size() == 2){
  87.        
  88.         float temperature = static_cast<float>(buffer.at(0));
  89.         int virgule = static_cast<int>(buffer.at(1));
  90.  
  91.         if((temperature && 128) == 128)
  92.             temperature = temperature - 256;
  93.  
  94.         if(virgule == 128)
  95.             temperature = temperature + 0.5;
  96.        
  97.        
  98.         if(choix == unite::celcius)
  99.             return temperature;
  100.         else if (choix == unite::fahrenheit)
  101.             {
  102.             temperature = 1.8*temperature + 32;
  103.             return temperature;
  104.             }
  105.         else if (choix == unite::kelvin)
  106.             {
  107.             temperature = temperature +273.15;
  108.             return temperature;
  109.             }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment