SHARE
TWEET

Untitled

a guest Sep 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Author: Pratim Ugale <pratim.ugale@gmail.com>
  2.  *
  3.  * To change Date, Time settings of the clock, switch OFF "Automatically set time from the Internet" on your PC
  4.  *
  5.  * Run this program with root permissions(sudo)
  6.  *
  7.  */
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14.  
  15. using namespace std;
  16.  
  17. enum Volume{
  18.   LOW =    1,
  19.   MEDIUM = 2,
  20.   HIGH =   3
  21. };
  22.  
  23. enum AlarmTone{
  24.   TONE1 = 1,
  25.   TONE2 = 2,
  26.   TONE3 = 3
  27. };
  28.  
  29. class Clock{
  30.   private:
  31.     Volume volume;
  32.     AlarmTone tone;
  33.     int timeFormat = 12;
  34.  
  35.   public:
  36.     Clock(){
  37.       setVolume(LOW);
  38.       setAlarmTone(TONE1);
  39.     }
  40.     void setVolume(Volume);
  41.     void showTime();
  42.     void setTime(int date, int month, int year,int hour, int min, int sec);
  43.     void changeTimeFormat();
  44.     void setAlarmTone(AlarmTone);
  45.     void ringClock();
  46.     void timer(int seconds);
  47.     int getTimeFormat();
  48.  
  49. };
  50.  
  51. void Clock::showTime(){
  52.   while(true){
  53.     system("clear");
  54.     time_t mytime = time(NULL);
  55.     cout<<ctime(&mytime)<<endl;
  56.     usleep(1000000);
  57.   }
  58. }
  59.  
  60. void Clock::setVolume(Volume volume){
  61.   this->volume = volume;
  62.   cout<<"Volume set to "<<this->volume<<endl;
  63. }
  64.  
  65. void Clock::setTime(int date, int month, int year,int hour, int min, int sec)
  66. {
  67.     //buffer to format command
  68.     unsigned char buff[32]={0};
  69.     //formatting command with the given parameters
  70.     sprintf((char*)buff,(const char *)"date -s \"%02d/%02d/%04d %02d:%02d:%02d\"",month,date,year,hour,min,sec);
  71.     //execute formatted command using system()
  72.     system((const char *)buff);
  73. }
  74.  
  75. void Clock::setAlarmTone(AlarmTone tone){
  76.   this->tone = tone;
  77.   cout<<"Alarm Tone changed to TONE"<<this->tone<<endl;
  78. }
  79.  
  80. void Clock::changeTimeFormat(){
  81.   if(this->timeFormat == 12)
  82.     this->timeFormat = 24;
  83.   else if(this->timeFormat == 24)
  84.     this->timeFormat = 12;
  85. }
  86.  
  87. int Clock::getTimeFormat(){
  88.   return this->timeFormat;
  89. }
  90.  
  91. void Clock::timer(int seconds){
  92.   usleep(seconds * 1000000);
  93.   this->ringClock();
  94. }
  95.  
  96. void Clock::ringClock(){
  97.   if (this->volume == 1)
  98.     cout<<"Timer rings with VOLUME: LOW and TONE: TONE"<<this->tone;
  99.   else if (this->volume == 2)
  100.     cout<<"Timer rings with VOLUME: MEDIUM and TONE: TONE"<<this->tone;
  101.   else if (this->volume == 3)
  102.     cout<<"Timer rings with VOLUME: HIGH and TONE: TONE"<<this->tone;
  103.   cout<<endl;
  104.   usleep(3000000);
  105. }
  106.  
  107.  
  108. int main(){
  109.  
  110.   static Clock clock;
  111.   while(true){
  112.     system("clear");
  113.     time_t mytime = time(NULL);
  114.  
  115.     struct tm * timeinfo;
  116.     timeinfo = localtime (&mytime);
  117.     char buffer [150];
  118.     if (clock.getTimeFormat() == 24)
  119.       strftime (buffer,150,"Clock Display \n\t|\n\t-> TIME  : %R:%S %p (24 hr) \n\t-> DATE  : %d/%m/%Y\n\t-> DAY   : %A\n\t-> MONTH : %B  \n",timeinfo);
  120.     else if (clock.getTimeFormat() == 12)
  121.       strftime (buffer,150,"Clock Display \n\t|\n\t-> TIME  : %r (12 hr) \n\t-> DATE  : %d/%m/%Y\n\t-> DAY   : %A\n\t-> MONTH : %B \n",timeinfo);
  122.     puts (buffer);
  123.  
  124.     //cout<<"CURRENT TIME: "<<endl<<"\t"<<ctime(&mytime)<<endl;
  125.     cout<<"Clock Features:"<<endl;
  126.     cout<<"1. Display Time Continuously \n2. Set Volume \n3. Set Date and Time \n4. Set Alarm Tone\n5. Switch b/w 12/24 Time Formats \n6. Use Timer \n7. Start Stopwatch"<<endl;
  127.     int input;
  128.     cin>>input;
  129.     switch(input){
  130.       case 1:
  131.         clock.showTime();
  132.         break;
  133.       case 2:
  134.         cout<<endl<<"Desired Volume:"<<endl;
  135.         cout<<"1 -> LOW \n2 -> MEDIUM \n3 -> HIGH"<<endl;
  136.         int volume;
  137.         cin>>volume;
  138.         if (volume == 1)
  139.           clock.setVolume(LOW);
  140.         else if (volume == 2)
  141.           clock.setVolume(MEDIUM);
  142.         else if (volume == 3)
  143.           clock.setVolume(HIGH);
  144.         break;
  145.       case 3:
  146.         cout<<"Enter (Day, Month, Year, Hours, Minutes, Seconds) in (DD, MM, YYYY, HH, MM, SS) format seperated by enter: "<<endl;
  147.         int day, month, year, hours, minutes, seconds;
  148.         cin>>day>>month>>year>>hours>>minutes>>seconds;
  149.         clock.setTime(day, month, year, hours, minutes, seconds);
  150.         break;
  151.       case 4:
  152.         cout<<endl<<"Desired Tone:"<<endl;
  153.         cout<<"1 -> TONE1 \n2 -> TONE2 \n3 -> TONE3"<<endl;
  154.         int tone;
  155.         cin>>tone;
  156.         if (tone == 1)
  157.           clock.setAlarmTone(TONE1);
  158.         else if (tone == 2)
  159.           clock.setAlarmTone(TONE2);
  160.         else if (tone == 3)
  161.           clock.setAlarmTone(TONE3);
  162.         break;
  163.       case 5:
  164.         clock.changeTimeFormat();
  165.         break;
  166.       case 6:
  167.         cout<<"Timer value in seconds: "<<endl;
  168.         int secs;
  169.         cin>>secs;
  170.         cout<<"The timer for "<<secs<<" seconds has started."<<endl;
  171.         clock.timer(secs);
  172.         break;
  173.       case 7:
  174.         cout<<"Stopwatch started."<<endl;
  175.         int stopwatch = 0;
  176.         while (true){
  177.           system("clear");
  178.           stopwatch++;
  179.           cout<<"STOPWATCH \n\t|\n\t-> "<<stopwatch<<" seconds."<<endl<<endl;
  180.           cout<<"Press ctrl-c to stop"<<endl;
  181.           usleep(1000000);
  182.         }
  183.  
  184.     }
  185.  
  186.   }
  187.   return 0;
  188. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top