Steefmaester

[classe/driver] DS3232

Sep 8th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.43 KB | None | 0 0
  1. //By Steef
  2.  
  3. //DS3232.h
  4.  
  5. #ifndef DS3232_H
  6. #define DS3232_H
  7.  
  8. #include "I2C.h"
  9.  
  10. class DS3232 {
  11.    
  12. private:
  13.    
  14.     int ADDRESS_DS3232;
  15.     I2C *bus;
  16.     int Recup_Dizaine(int num);
  17.     int Recup_Unite(int num);
  18.    
  19.     enum command
  20.     {
  21.         Second = 0x00,
  22.         Minute = 0x01,
  23.         Hour = 0x02,
  24.         Day = 0x03,
  25.         Date = 0x04,
  26.         Month = 0x05,
  27.         Year = 0x06
  28.        
  29.     };
  30.    
  31.    
  32.    
  33. public:
  34.    
  35.     DS3232(int address,I2C *bus);
  36.     ~DS3232();
  37.    
  38.     enum Week
  39.     {
  40.         Sunday = 0x01,
  41.         Monday = 0x02,
  42.         Tuesday = 0x03,
  43.         Wednesday = 0x04,
  44.         Thursday = 0x05,
  45.         Friday = 0x06,
  46.         Saturday = 0x07
  47.     };
  48.    
  49.    
  50.     void setYear(int year);
  51.     int getYear();
  52.    
  53.     void setMonth(int mouth);
  54.     int getMonth();
  55.    
  56.     void setDate(int Date);
  57.     int getDate();
  58.    
  59.     void setDay(Week day);
  60.     string getDay();
  61.    
  62.     void setHour(int hour);
  63.     int getHour();
  64.    
  65.     void setMinute(int minute);
  66.     int getMinute();
  67.    
  68.     void setSecond(int second);
  69.     int getSecond();
  70.    
  71.     string getTime();
  72.  
  73.  
  74. };
  75.  
  76. #endif
  77.  
  78.  
  79.  
  80. //DS3232.cpp
  81. #include "DS3232.h"
  82.  
  83. DS3232::DS3232(int address,I2C *bus) {
  84.    
  85.     ADDRESS_DS3232 = address;
  86.     this->bus= bus;
  87.    
  88.     bus->setAddress(ADDRESS_DS3232);
  89. }
  90.  
  91. int DS3232::Recup_Dizaine(int num){
  92.    
  93.     return ((num/10) %10);
  94.    
  95. }
  96.  
  97. int DS3232::Recup_Unite(int num){
  98.    
  99.     return (num%10);
  100.    
  101. }
  102.  
  103. void DS3232::setSecond(int second){
  104.    
  105.     bus->setAddress(ADDRESS_DS3232);
  106.    
  107.     vector<unsigned char> buffer;
  108.    
  109.     if(second<0 || second>59)
  110.         cout<<"ERROR: 0<second<59"<<endl;
  111.    
  112.     int value = (Recup_Dizaine(second)<<4)+Recup_Unite(second);
  113.    
  114.     buffer.push_back(command::Second);
  115.     buffer.push_back(value);
  116.     bus->send(buffer);
  117.    
  118.     }
  119.  
  120. void DS3232::setMinute(int minute){
  121.    
  122.     bus->setAddress(ADDRESS_DS3232);
  123.    
  124.     vector<unsigned char> buffer;
  125.    
  126.     if(minute<0 || minute>59)
  127.         cout<<"ERROR: 0<minute<59"<<endl;
  128.    
  129.     int value = (Recup_Dizaine(minute)<<4)+Recup_Unite(minute);
  130.    
  131.     buffer.push_back(command::Minute);
  132.     buffer.push_back(value);
  133.     bus->send(buffer);
  134.    
  135.     }
  136.  
  137. void DS3232::setHour(int hour){
  138.    
  139.     bus->setAddress(ADDRESS_DS3232);
  140.    
  141.     vector<unsigned char> buffer;
  142.    
  143.     if(hour<0 || hour>24)
  144.         cout<<"ERROR: 0<hour<24"<<endl;
  145.    
  146.     int value = (Recup_Dizaine(hour)<<4)+Recup_Unite(hour);
  147.    
  148.     buffer.push_back(command::Hour);
  149.     buffer.push_back(value);
  150.     bus->send(buffer);
  151.    
  152.     }
  153.  
  154. void DS3232::setDay(Week day){
  155.    
  156.     bus->setAddress(ADDRESS_DS3232);
  157.    
  158.     vector<unsigned char> buffer;
  159.        
  160.     buffer.push_back(command::Day);
  161.     buffer.push_back(day);
  162.     bus->send(buffer);
  163.    
  164.     }
  165.  
  166. void DS3232::setDate(int date){
  167.    
  168.     bus->setAddress(ADDRESS_DS3232);
  169.    
  170.     vector<unsigned char> buffer;
  171.    
  172.     if(date<1 || date>31)
  173.         cout<<"ERROR: 1<Date<31"<<endl;
  174.    
  175.     int value = (Recup_Dizaine(date)<<4)+Recup_Unite(date);
  176.    
  177.     buffer.push_back(command::Date);
  178.     buffer.push_back(value);
  179.     bus->send(buffer);
  180.    
  181.     }
  182.  
  183. void DS3232::setMonth(int month)
  184. {
  185.     bus->setAddress(ADDRESS_DS3232);
  186.    
  187.     vector<unsigned char> buffer;
  188.    
  189.     if(month<1 || month>12)
  190.         cout<<"ERROR: 0<mouth<12"<<endl;
  191.    
  192.     int value = (Recup_Dizaine(month)<<4)+Recup_Unite(month);
  193.    
  194.     buffer.push_back(command::Month);
  195.     buffer.push_back(value);
  196.     bus->send(buffer);
  197.    
  198. }
  199.  
  200. void DS3232::setYear(int year)
  201. {
  202.     bus->setAddress(ADDRESS_DS3232);
  203.    
  204.     vector<unsigned char> buffer;
  205.    
  206.     year = year-2000;
  207.    
  208.     if(year<0 || year>99)
  209.         cout<<"ERROR: 2000<year<2099"<<endl;
  210.    
  211.     int value = (Recup_Dizaine(year)<<4)+Recup_Unite(year);
  212.    
  213.     buffer.push_back(command::Year);
  214.     buffer.push_back(value);
  215.     bus->send(buffer);
  216. }
  217.  
  218.  
  219. int DS3232::getSecond()
  220. {
  221.     bus->setAddress(ADDRESS_DS3232);
  222.    
  223.     vector<unsigned char> buffer;
  224.    
  225.     bus->receive(command::Second,buffer, 1);
  226.    
  227.     int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
  228.     int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
  229.    
  230.     int res = (diz*10) +uni;
  231.    
  232.     return res;
  233.    
  234. }
  235.  
  236. int DS3232::getMinute()
  237. {
  238.     bus->setAddress(ADDRESS_DS3232);
  239.    
  240.     vector<unsigned char> buffer;
  241.    
  242.     bus->receive(command::Minute,buffer, 1);
  243.    
  244.     int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
  245.     int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
  246.    
  247.     int res = (diz*10) +uni;
  248.    
  249.     return res;
  250. }
  251.  
  252. int DS3232::getHour()
  253. {
  254.     bus->setAddress(ADDRESS_DS3232);
  255.    
  256.     vector<unsigned char> buffer;
  257.    
  258.     bus->receive(command::Hour,buffer, 1);
  259.    
  260.     int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
  261.     int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
  262.    
  263.     int res = (diz*10) +uni;
  264.    
  265.     return res;
  266. }
  267.  
  268. string DS3232::getDay()
  269. {
  270.     bus->setAddress(ADDRESS_DS3232);
  271.    
  272.     vector<unsigned char> buffer;
  273.    
  274.     bus->receive(command::Day,buffer, 1);
  275.    
  276.     switch (static_cast<int>(buffer.at(0)))
  277.     {
  278.         case Week::Sunday:
  279.             return "Sunday";
  280.            
  281.         case Week::Monday:
  282.             return "Monday";
  283.            
  284.         case Week::Tuesday:
  285.             return "Tuesday";
  286.            
  287.         case Week::Wednesday:
  288.             return "Wednesday";
  289.            
  290.         case Week::Thursday:
  291.             return "Thursday";
  292.            
  293.         case Week::Friday:
  294.             return "Friday";
  295.            
  296.         case Week::Saturday:
  297.             return "Saturday";
  298.     }
  299.    
  300.    
  301. }
  302.  
  303. int DS3232::getDate()
  304. {
  305.     bus->setAddress(ADDRESS_DS3232);
  306.    
  307.     vector<unsigned char> buffer;
  308.    
  309.     bus->receive(command::Date,buffer, 1);
  310.    
  311.     int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
  312.     int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
  313.    
  314.     int res = (diz*10) +uni;
  315.    
  316.     return res;
  317.    
  318. }
  319.  
  320. int DS3232::getMonth()
  321. {
  322.     bus->setAddress(ADDRESS_DS3232);
  323.    
  324.     vector<unsigned char> buffer;
  325.    
  326.     bus->receive(command::Month,buffer, 1);
  327.    
  328.     int diz = (static_cast<int>(buffer.at(0)) & 0x10)>>4;
  329.     int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
  330.    
  331.     int res = (diz*10) +uni;
  332.    
  333.     return res;
  334. }
  335.  
  336. int DS3232::getYear()
  337. {
  338.     bus->setAddress(ADDRESS_DS3232);
  339.    
  340.     vector<unsigned char> buffer;
  341.    
  342.     bus->receive(command::Year,buffer, 1);
  343.    
  344.     int diz = (static_cast<int>(buffer.at(0)) & 0xF0)>>4;
  345.     int uni = (static_cast<int>(buffer.at(0))) & 0x0F;
  346.    
  347.     int res = 2000+(diz*10) +uni;
  348.    
  349.     return res;
  350. }
  351.  
  352.  
  353.  
  354. string DS3232::getTime()
  355. {
  356.     string Time;
  357.     string tmp;
  358.     Time = to_string(getYear())+"-";
  359.    
  360.     tmp = to_string(getMonth());
  361.     if(tmp.size()==1)
  362.         tmp = "0" +tmp;
  363.     Time = Time + tmp+"-";
  364.    
  365.     tmp = to_string(getDate());
  366.     if(tmp.size()==1)
  367.         tmp = "0" +tmp;
  368.     Time = Time + tmp+" ";
  369.    
  370.     tmp = to_string(getHour());
  371.     if(tmp.size()==1)
  372.         tmp = "0" +tmp;
  373.     Time = Time + tmp+":";
  374.    
  375.     tmp = to_string(getMinute());
  376.     if(tmp.size()==1)
  377.         tmp = "0" +tmp;
  378.     Time = Time + tmp+":";
  379.    
  380.     tmp = to_string(getSecond());
  381.     if(tmp.size()==1)
  382.         tmp = "0" +tmp;
  383.     Time = Time + tmp;
  384.    
  385.     return Time;
  386.      
  387. }
  388.  
  389.  
  390. DS3232::~DS3232() {
  391.    
  392. }
Advertisement
Add Comment
Please, Sign In to add comment