Created
August 14, 2014 18:24
-
-
Save tompazourek/3a5a9774e1060ae649ff to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <time.h> | |
#include <ncurses.h> | |
#define DIGIT_SIZE_X 5 | |
#define DIGIT_SIZE_Y 7 | |
#define SPACE_SIZE 2 | |
#define COLON_SIZE 1 | |
#define CLOCK_SIZE_X (6*DIGIT_SIZE_X + 2*COLON_SIZE + 7*SPACE_SIZE) | |
#define CLOCK_SIZE_Y DIGIT_SIZE_Y | |
#define COLON " o o " | |
#define SEG_H " --- " | |
#define SEG_H_EMPTY " " | |
#define SEG_V " || " | |
#define SEG_V_EMPTY " " | |
#define H_SEG_DISTANCE 3 | |
#define V_SEG_DISTANCE 4 | |
// digits defined as segments: [digit][segment]; 1 = full; / 0 = empty | |
static _Bool digits[10][7] ={ | |
{1,1,1,0,1,1,1},/* segment numbers */ | |
{0,0,1,0,0,1,0}, | |
{1,0,1,1,1,0,1},// 000 | |
{1,0,1,1,0,1,1},// 1 2 | |
{0,1,1,1,0,1,0},// 1 2 | |
{1,1,0,1,0,1,1},// 333 | |
{1,1,0,1,1,1,1},// 4 5 | |
{1,0,1,0,0,1,0},// 4 5 | |
{1,1,1,1,1,1,1},// 666 | |
{1,1,1,1,0,1,1}}; | |
void updateTime(struct tm** timeInfo) { | |
time_t rawTime; | |
time(&rawTime); | |
*timeInfo = localtime(&rawTime); | |
} | |
void ncInit() { | |
initscr(); // initialize the curses library | |
cbreak(); // take input chars one at a time, no wait for \n | |
noecho(); | |
nl(); // tell curses to do NL->CR/NL on output | |
keypad(stdscr,1); | |
curs_set(0); // hide cursor | |
halfdelay(10); // wait 1 s for getch() | |
refresh(); | |
clear(); | |
} | |
void ncEnd() { | |
endwin(); | |
} | |
void mvaddstrV(int y, int x, const char* s) { | |
/* mvaddstr vertical */ | |
char tmp[] = {0,0}; | |
for (unsigned int i = 0; i < strlen(s); i++) { | |
*tmp = s[i]; | |
mvaddstr(y+i, x, tmp); | |
} | |
} | |
void printColon(int *y, int *x) { | |
mvaddstrV(*y, *x, COLON); | |
*x += COLON_SIZE + SPACE_SIZE; | |
} | |
void printSegH(int y, int x, _Bool space) { | |
mvaddstr(y, x, space ? SEG_H_EMPTY : SEG_H); | |
} | |
void printSegV(int y, int x, _Bool space) { | |
mvaddstrV(y, x, space ? SEG_V_EMPTY : SEG_V); | |
} | |
void printSeg(int y, int x, int segment, _Bool space) { | |
switch(segment) { | |
// horizontal | |
case 0: printSegH(y,x,space); break; | |
case 3: printSegH(y+H_SEG_DISTANCE,x,space); break; | |
case 6: printSegH(y+2*H_SEG_DISTANCE,x,space); break; | |
// vertical left | |
case 1: printSegV(y,x,space); break; | |
case 2: printSegV(y,x+V_SEG_DISTANCE,space); break; | |
// vertical right | |
case 4: printSegV(y+H_SEG_DISTANCE,x,space); break; | |
case 5: printSegV(y+H_SEG_DISTANCE,x+V_SEG_DISTANCE,space); break; | |
} | |
} | |
void printDigit(int *y, int *x, int digit) { | |
// print spaces or segments | |
for (int i = 0; i < 7; i++) printSeg(*y, *x, i, !digits[digit][i]); | |
*x += DIGIT_SIZE_X + SPACE_SIZE; | |
} | |
void printTime(struct tm* t) { | |
// set position to center the clock | |
int posY = (getmaxy(stdscr) - CLOCK_SIZE_Y)/2; | |
int posX = (getmaxx(stdscr) - CLOCK_SIZE_X)/2; | |
printDigit(&posY, &posX, t->tm_hour/10); | |
printDigit(&posY, &posX, t->tm_hour%10); | |
printColon(&posY, &posX); | |
printDigit(&posY, &posX, t->tm_min/10); | |
printDigit(&posY, &posX, t->tm_min%10); | |
printColon(&posY, &posX); | |
printDigit(&posY, &posX, t->tm_sec/10); | |
printDigit(&posY, &posX, t->tm_sec%10); | |
refresh(); | |
} | |
int main() { | |
ncInit(); | |
struct tm* timeInfo; | |
while (getch() == ERR) { | |
updateTime(&timeInfo); | |
printTime(timeInfo); | |
} | |
ncEnd(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment