/****************************************************************** * Filename: clock.c * * RaspberryPi alarm clock With special reminder feature. * * Compile: gcc -o clock -Wall -I/usr/local/include -L/usr/local/lib * clock.c -lwiringPi -lwiringPiDev * * Execute: sudo ./clock * * All pin numbers are wiringPi numbers unless otherwise specified. ******************************************************************/ #include #include #include #include static int lcdhandle; // LCD screen char hours[24][3] = {"12","01","02","03","04","05","06","07", "08","09","10","11","12","01","02","03", "04","05","06","07","08","09","10","11"}; char dow[7][4] = {"Sun","Mon","Tue","Wed", "Thu","Fri","Sat"}; char months[12][4] = {"Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec"}; struct tm tm2; // Structures for getting time. int timeh, timem, times; // Current time elements. int date, month, year, wday; int apin = 5; // Alarm button int hpin = 6; // Set hours button int mpin = 12; // Set minutes button int spin = 13; // Special button int PWMpin = 1; // Alarm sound pin int aflag = 0; // Alarm flag int sflag = 0; // special flag int alh = 0; // Alarm hours int alm = 0; // Alarm minutes /********************************************************* * gettime() function - Get system time and load it into * global variables. *********************************************************/ void gettime() { time_t now; char *str; now = time(NULL); // Get the time. str = ctime(&now); // Convert it to a string. strptime(str,"%a %b %d %H:%M:%S %Y",&tm2); // Convert from string. timeh = tm2.tm_hour; // Copy times timem = tm2.tm_min; // into global variables. times = tm2.tm_sec; date = tm2.tm_mday; month = tm2.tm_mon; year = tm2.tm_year; wday = tm2.tm_wday; } /********************************************************* * dsptime() function - Displays the time. *********************************************************/ void dsptime() { lcdPosition (lcdhandle, 6, 0); lcdPrintf(lcdhandle,"%s:", *hours+(timeh*3)); // Display hours lcdPrintf(lcdhandle,"%02d", timem); // Display minutes if(timeh<12) lcdPrintf(lcdhandle," AM"); // Display AM/PM else lcdPrintf(lcdhandle," PM"); lcdPosition (lcdhandle, 0, 1); lcdPrintf(lcdhandle," %s %s %d %d ", *dow+(wday*4), *months+(month*4), date ,(1900+year)); if(sflag>0) { lcdPosition(lcdhandle,0, 3); lcdPrintf(lcdhandle,"* "); } else { lcdPosition(lcdhandle,0, 3); lcdPrintf(lcdhandle," "); } if(aflag==0) lcdPrintf(lcdhandle,"Alarm: OFF "); else { lcdPrintf(lcdhandle,"Alarm: %s:%02d ", *hours+(alh*3), alm); if(alh<12) lcdPrintf(lcdhandle,"AM");// Display AM/PM else lcdPrintf(lcdhandle,"PM"); } } /********************************************************* * dspalarm() function - Display the alarm time. *********************************************************/ void dspalarm() { lcdPosition (lcdhandle, 0, 1); lcdPrintf(lcdhandle,"Set Alarm: %s:%02d ", *hours+(alh*3), alm); if(alh<12) lcdPrintf(lcdhandle,"AM");// Display AM/PM else lcdPrintf(lcdhandle,"PM"); } /********************************************************* * setalarm() function - Set the time for the alarm * and turn it on. *********************************************************/ void setalarm() { lcdClear(lcdhandle); dspalarm(); while(aflag==2) { while(digitalRead(hpin)==0) // Is minute button pressed? { alh++; // Increment alarm hour if(alh>23) alh=0; // Hour can't be > 23 dspalarm(); // Redo the display delay(500); // Time to read it } delay(25); // Debounce while(digitalRead(mpin)==0) // Is minute button pressed? { alm+=5; // Add 5 to alarm minutes. if(alm>59) alm=0; // Minute can't be > 59 dspalarm(); // Redo the display delay(500); // Time to read it } delay(25); // Debounce if(digitalRead(apin)==0) { while(digitalRead(apin)==0); delay(25); aflag = 3; } } } /************************************************************** * main() function **************************************************************/ int main() { int ptime = 70; // previous time after gettime() int pday = 32; // Prevoius day wiringPiSetup(); // Setup required by wiringPi lcdhandle = lcdInit (4, 20, 4, 11,10, 0,2,3,4,0,0,0,0) ; pinMode(apin, INPUT); // Set alarm pin to input pullup pullUpDnControl(apin, PUD_UP); pinMode(hpin, INPUT); // Set hours pin to input pullup pullUpDnControl(hpin, PUD_UP); pinMode(mpin, INPUT); // Set minutes pin to input pullup pullUpDnControl(mpin, PUD_UP); pinMode(spin, INPUT); // Set special pin to input pullup pullUpDnControl(spin, PUD_UP); pinMode (PWMpin, PWM_OUTPUT); // Alarm sound pin pwmSetMode(PWM_MODE_MS); // Mark/Space mode pwmSetClock(100); // Default 100 (int) pwmSetRange(1024); // Default 1024 (unsigned int) int duty = 200; // Duty cycle (0 - range) while(1) { // Get and display system time gettime(); if(ptime != times) { ptime = times; // Save time as prevoius time dsptime(); // Display the time } // Toggle special flag if black button pressed or new day. if(digitalRead(spin)==0) // If button pressed toggle { // special flag while(digitalRead(spin)==0) // debounce { delay(25); } sflag = !sflag; // toggle } if(date != pday) // Is it a new day? { lcdClear(lcdhandle); sflag = !sflag; // If so toggle special flag, pday = date; // and save date as previous. } // Sound alarm. if(timeh==alh) // If alarm hour { if(timem==alm) // and alarm minute { if(aflag==1) // and alarm flag == 1 { pwmWrite(PWMpin, duty); // sound alarm } } } if(aflag==0) pwmWrite(PWMpin, 0); // Alarm off if button pressed if(timem!=alm) pwmWrite(PWMpin, 0); // or 1 minute has passsed // Turn alarm off, set alarm. if(digitalRead(apin)==0) // Is alarm button pressed? { while(digitalRead(apin)==0) delay(25); // debounce if(aflag==1) aflag=0; // If alarm flag set clear it. else aflag = 2; // If alarm flag clear } if(aflag==2) setalarm(); // run alarm set function if(timem != alm) if(aflag == 3) aflag = 1; } return 0; }