Created
September 19, 2015 14:55
-
-
Save magkopian/070c3137b0a0e11a54fc to your computer and use it in GitHub Desktop.
Ncurses version of "A natural language clock program for the RaspberryPi." More info: http://www.instructables.com/id/Natural-language-clock-for-RaspberryPi/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************* | |
* Filename: clock.c | |
* | |
* Original Author: JRV31 (http://www.instructables.com/member/JRV31/) | |
* | |
* A natural language clock program for the RaspberryPi. | |
* | |
* Compile with the command: | |
* gcc -o clock clock.c -lncurses | |
* | |
* Execute with the command: ./clock | |
* | |
**********************************************************/ | |
#include <time.h> | |
#include <stdio.h> | |
#include <termios.h> | |
#include <unistd.h> | |
#include <ncurses.h> | |
#define clrscr clear | |
char hours[24][7] = {"Twelve", | |
"One", | |
"Two", | |
"Three", | |
"Four", | |
"Five", | |
"Six", | |
"Seven", | |
"Eight", | |
"Nine", | |
"Ten", | |
"Eleven", | |
"Twelve", | |
"One", | |
"Two", | |
"Three", | |
"Four", | |
"Five", | |
"Six", | |
"Seven", | |
"Eight", | |
"Nine", | |
"Ten", | |
"Eleven"}; | |
char minutes[60][13] = {"zero", | |
"One", | |
"Two", | |
"Three", | |
"Four", | |
"Five", | |
"Six", | |
"Seven", | |
"Eight", | |
"Nine", | |
"Ten", | |
"Eleven", | |
"Twelve", | |
"Thirteen", | |
"Fourteen", | |
"Fifteen", | |
"Sixteen", | |
"Seventeen", | |
"Eighteen", | |
"Nineteen", | |
"Twenty", | |
"Twenty-one", | |
"Twenty-two", | |
"Twenty-three", | |
"Twenty-four", | |
"Twenty-five", | |
"Twenty-six", | |
"Twenty-seven", | |
"Twenty-eight", | |
"Twenty-nine", | |
"Thirty", | |
"Thirty-one", | |
"Thirty-two", | |
"Thirty-three", | |
"Thirty-four", | |
"Thirty-five", | |
"Thirty-six", | |
"Thirty-seven", | |
"Thirty-eight", | |
"Thirty-nine", | |
"Forty", | |
"Forty-one", | |
"Forty-two", | |
"Forty-three", | |
"Forty-four", | |
"Forty-five", | |
"Forty-six", | |
"Forty-seven", | |
"Forty-eight", | |
"Forty-nine", | |
"Fifty", | |
"Fifty-one", | |
"Fifty-two", | |
"Fifty-three", | |
"Fifty-four", | |
"Fifty-five", | |
"Fifty-six", | |
"Fifty-seven", | |
"Fifty-eight", | |
"Fifty-nine",}; | |
char dow[7][10] = {"Sunday","Monday","Tuesday","Wednesday", | |
"Thursday","Friday","Saturday"}; | |
char months[12][10] = {"January","February","March","April", | |
"May","June","July","August","September", | |
"October","November","December"}; | |
char days[32][14] = {"zero", | |
"First", | |
"Second", | |
"Third", | |
"Forth", | |
"Fifth", | |
"Sixth", | |
"Seventh", | |
"Eighth", | |
"Ninth", | |
"Tenth", | |
"Eleventh", | |
"Twelfth", | |
"Thirteenth", | |
"Fourteenth", | |
"Fifteenth", | |
"Sixteenth", | |
"Seventeenth", | |
"Eighteenth", | |
"Nineteenth", | |
"Twentieth", | |
"Twenty-first", | |
"Twenty-second", | |
"Twenty-third", | |
"Twenty-forth", | |
"Twenty-fifth", | |
"Twenty-sixth", | |
"Twenty-seventh", | |
"Twenty-eighth", | |
"Twenty-ninth", | |
"Thirtieth", | |
"Thirty-first"}; | |
struct tm tm2; | |
int timeh, timem, times; // Current hours and minutes and seconds. | |
int date, month, year, wday; | |
/********************************************************* | |
* gettime() function - Loads the current time into the | |
* global variables. It puts the hours in the variable | |
* timeh and the minutes in timem, and so on. | |
*********************************************************/ | |
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 the string version. | |
timeh = tm2.tm_hour; // Copy hours, minutes and seconds | |
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; | |
} | |
/********************************************************* | |
* dspsec() function - Displays the seconds in the | |
* variable times. Seconds uses the same array as minutes. | |
*********************************************************/ | |
void dspsec(T2) | |
{ | |
if(times>0) printw("and %s seconds ", *minutes+(T2*13)); | |
} | |
/********************************************************* | |
* dsptime() function - Clears the screen and displays the | |
* time in natural language. It uses the hours in the | |
* variable timeh and the minutes in timem. | |
*********************************************************/ | |
void dsptime() | |
{ | |
int timem2, timeh2, times2; | |
printw("\n\n\n\n\n\n\n\n\n The time is now:\n"); | |
switch(timem) | |
{ | |
case 0: | |
printw( "%s O'clock\n",*hours+(timeh*7)); | |
dspsec(times); | |
break; | |
case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: | |
case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: | |
case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: | |
case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39: case 40: | |
case 41: case 42: case 43: case 44: case 45: | |
if(timem==1) printw(" %s minute ",*minutes+(timem*13)); | |
else printw(" %s minutes ",*minutes+(timem*13)); | |
dspsec(times); | |
printw("past %s o'clock",*hours+(timeh*7)); | |
break; | |
case 46: case 47: case 48: case 49: case 50: | |
case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: | |
timem2 = 60-timem; | |
timeh++; | |
if(timem2==1) printw(" %s minute ",*minutes+(timem2*13)); | |
else printw(" %s minutes ",*minutes+(timem2*13)); | |
dspsec(60-times); | |
printw("until %s o'clock",*hours+(timeh*7)); | |
break; | |
} | |
if(timeh<13) printw(" AM\n"); | |
else printw(" PM\n"); | |
printw(" %s, %s %s %d\n\n\n\n\n\n\n", *dow+(wday*10), *months+(month*10), *days+(date*14) ,(1900+year)); | |
refresh(); | |
} | |
/************************************************************** | |
* main() function | |
**************************************************************/ | |
int main(void) | |
{ | |
int ptime = 70; // previous time after gettime() | |
char res = 0x00; | |
WINDOW *w = initscr(); | |
//cbreak(); | |
nodelay(w, TRUE); | |
noecho(); | |
while(1) | |
{ | |
gettime(); | |
if(ptime != times) | |
{ | |
clrscr(); | |
ptime = times; | |
dsptime(); | |
res = getch(); | |
tcflush(STDIN_FILENO, TCIFLUSH); | |
if ( res == 'q' || res == 'Q' ) { | |
endwin(); | |
return 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment