Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //By Steef
- //DS3232.h
- #ifndef DS3232_H
- #define DS3232_H
- #include "I2C.h"
- class DS3232 {
- private:
- int ADDRESS_DS3232;
- I2C *bus;
- int Recup_Dizaine(int num);
- int Recup_Unite(int num);
- enum command
- {
- Second = 0x00,
- Minute = 0x01,
- Hour = 0x02,
- Day = 0x03,
- Date = 0x04,
- Month = 0x05,
- Year = 0x06
- };
- public:
- DS3232(int address,I2C *bus);
- ~DS3232();
- enum Week
- {
- Sunday = 0x01,
- Monday = 0x02,
- Tuesday = 0x03,
- Wednesday = 0x04,
- Thursday = 0x05,
- Friday = 0x06,
- Saturday = 0x07
- };
- void setYear(int year);
- int getYear();
- void setMonth(int mouth);
- int getMonth();
- void setDate(int Date);
- int getDate();
- void setDay(Week day);
- string getDay();
- void setHour(int hour);
- int getHour();
- void setMinute(int minute);
- int getMinute();
- void setSecond(int second);
- int getSecond();
- string getTime();
- };
- #endif
- //DS3232.cpp
- #include "DS3232.h"
- DS3232::DS3232(int address,I2C *bus) {
- ADDRESS_DS3232 = address;
- this->bus= bus;
- bus->setAddress(ADDRESS_DS3232);
- }
- int DS3232::Recup_Dizaine(int num){
- return ((num/10) %10);
- }
- int DS3232::Recup_Unite(int num){
- return (num%10);
- }
- void DS3232::setSecond(int second){
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- if(second<0 || second>59)
- cout<<"ERROR: 0<second<59"<<endl;
- int value = (Recup_Dizaine(second)<<4)+Recup_Unite(second);
- buffer.push_back(command::Second);
- buffer.push_back(value);
- bus->send(buffer);
- }
- void DS3232::setMinute(int minute){
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- if(minute<0 || minute>59)
- cout<<"ERROR: 0<minute<59"<<endl;
- int value = (Recup_Dizaine(minute)<<4)+Recup_Unite(minute);
- buffer.push_back(command::Minute);
- buffer.push_back(value);
- bus->send(buffer);
- }
- void DS3232::setHour(int hour){
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- if(hour<0 || hour>24)
- cout<<"ERROR: 0<hour<24"<<endl;
- int value = (Recup_Dizaine(hour)<<4)+Recup_Unite(hour);
- buffer.push_back(command::Hour);
- buffer.push_back(value);
- bus->send(buffer);
- }
- void DS3232::setDay(Week day){
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- buffer.push_back(command::Day);
- buffer.push_back(day);
- bus->send(buffer);
- }
- void DS3232::setDate(int date){
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- if(date<1 || date>31)
- cout<<"ERROR: 1<Date<31"<<endl;
- int value = (Recup_Dizaine(date)<<4)+Recup_Unite(date);
- buffer.push_back(command::Date);
- buffer.push_back(value);
- bus->send(buffer);
- }
- void DS3232::setMonth(int month)
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- if(month<1 || month>12)
- cout<<"ERROR: 0<mouth<12"<<endl;
- int value = (Recup_Dizaine(month)<<4)+Recup_Unite(month);
- buffer.push_back(command::Month);
- buffer.push_back(value);
- bus->send(buffer);
- }
- void DS3232::setYear(int year)
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- year = year-2000;
- if(year<0 || year>99)
- cout<<"ERROR: 2000<year<2099"<<endl;
- int value = (Recup_Dizaine(year)<<4)+Recup_Unite(year);
- buffer.push_back(command::Year);
- buffer.push_back(value);
- bus->send(buffer);
- }
- int DS3232::getSecond()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Second,buffer, 1);
- int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
- int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
- int res = (diz*10) +uni;
- return res;
- }
- int DS3232::getMinute()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Minute,buffer, 1);
- int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
- int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
- int res = (diz*10) +uni;
- return res;
- }
- int DS3232::getHour()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Hour,buffer, 1);
- int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
- int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
- int res = (diz*10) +uni;
- return res;
- }
- string DS3232::getDay()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Day,buffer, 1);
- switch (static_cast<int>(buffer.at(0)))
- {
- case Week::Sunday:
- return "Sunday";
- case Week::Monday:
- return "Monday";
- case Week::Tuesday:
- return "Tuesday";
- case Week::Wednesday:
- return "Wednesday";
- case Week::Thursday:
- return "Thursday";
- case Week::Friday:
- return "Friday";
- case Week::Saturday:
- return "Saturday";
- }
- }
- int DS3232::getDate()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Date,buffer, 1);
- int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
- int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
- int res = (diz*10) +uni;
- return res;
- }
- int DS3232::getMonth()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Month,buffer, 1);
- int diz = (static_cast<int>(buffer.at(0)) & 0x10)>>4;
- int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
- int res = (diz*10) +uni;
- return res;
- }
- int DS3232::getYear()
- {
- bus->setAddress(ADDRESS_DS3232);
- vector<unsigned char> buffer;
- bus->receive(command::Year,buffer, 1);
- int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
- int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
- int res = 2000+(diz*10) +uni;
- return res;
- }
- string DS3232::getTime()
- {
- string Time;
- string tmp;
- Time = to_string(getYear())+"-";
- tmp = to_string(getMonth());
- if(tmp.size()==1)
- tmp = "0" +tmp;
- Time = Time + tmp+"-";
- tmp = to_string(getDate());
- if(tmp.size()==1)
- tmp = "0" +tmp;
- Time = Time + tmp+" ";
- tmp = to_string(getHour());
- if(tmp.size()==1)
- tmp = "0" +tmp;
- Time = Time + tmp+":";
- tmp = to_string(getMinute());
- if(tmp.size()==1)
- tmp = "0" +tmp;
- Time = Time + tmp+":";
- tmp = to_string(getSecond());
- if(tmp.size()==1)
- tmp = "0" +tmp;
- Time = Time + tmp;
- return Time;
- }
- DS3232::~DS3232() {
- }
Advertisement
Add Comment
Please, Sign In to add comment