Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //By Steef
- //DS1621.h
- #ifndef DS1621_H
- #define DS1621_H
- #include "I2C.h"
- using namespace std;
- class DS1621 {
- private:
- int ADDRESS_DS1621;
- I2C *bus;
- enum command
- {
- ReadTemperature = 0xAA,
- ReadCounter = 0xA8,
- ReadSlope = 0xA9,
- StartConvertT = 0xEE,
- StopConvertT = 0x22,
- AccessTH = 0xA1,
- AccessTL = 0xA2,
- AccessConfig = 0xAC
- };
- bool init();
- public:
- DS1621(int address,I2C *bus);
- enum unite
- {
- celcius = 1,
- fahrenheit = 2,
- kelvin = 3
- };
- float getTemperature(unite choix);
- };
- #endif
- //DS1621.cpp
- #include "DS1621.h"
- DS1621::DS1621(int address, I2C* bus)
- {
- ADDRESS_DS1621 = address;
- this->bus= bus;
- init();
- }
- bool DS1621::init()
- {
- vector<unsigned char> buffer;
- bus->setAddress(ADDRESS_DS1621);
- buffer.push_back(command::AccessConfig);
- buffer.push_back(0x00);
- if(bus->send(buffer) == -1)
- return false;
- if(bus->send(command::StartConvertT) == -1)
- return false;
- else
- return true;
- }
- float DS1621::getTemperature(unite choix)
- {
- if(bus->getAddress()!= ADDRESS_DS1621)
- init();
- vector<unsigned char> buffer;
- usleep(600000); //attente d'initialisation dans le registre
- bus->receive(command::ReadTemperature,buffer, 2);
- if(buffer.size() == 2){
- float temperature = static_cast<float>(buffer.at(0));
- int virgule = static_cast<int>(buffer.at(1));
- if((temperature && 128) == 128)
- temperature = temperature - 256;
- if(virgule == 128)
- temperature = temperature + 0.5;
- if(choix == unite::celcius)
- return temperature;
- else if (choix == unite::fahrenheit)
- {
- temperature = 1.8*temperature + 32;
- return temperature;
- }
- else if (choix == unite::kelvin)
- {
- temperature = temperature +273.15;
- return temperature;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment