From 33000dfc5ae4aed280a77612ffb23e295b4a54e7 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 26 Oct 2009 03:22:06 +0000 Subject: add 7-segment display --- 7seg.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 2 +- aclock.c | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 7seg.c diff --git a/7seg.c b/7seg.c new file mode 100644 index 0000000..63be3cf --- /dev/null +++ b/7seg.c @@ -0,0 +1,52 @@ +/* $Id$ */ +/* + * Copyright (c) 2005 Dimitri Sokolyuk + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +char allowed[] = "0123456789:.-"; + +char *seg[][3] = { + ['0'] = { " _ ", "| |", "|_|" }, + ['1'] = { " ", " |", " |" }, + ['2'] = { " _ ", " _|", "|_ " }, + ['3'] = { " _ ", " _|", " _|" }, + ['4'] = { " ", "|_|", " |" }, + ['5'] = { " _ ", "|_ ", " _|" }, + ['6'] = { " _ ", "|_ ", "|_|" }, + ['7'] = { " _ ", " |", " |" }, + ['8'] = { " _ ", "|_|", "|_|" }, + ['9'] = { " _ ", "|_|", " _|" }, + [':'] = { " ", " * ", " * " }, + ['.'] = { " ", " ", " * " }, + ['-'] = { " ", " _ ", " " }, +}; + +void +sevenseg(char t[3][80], size_t sz, char *s) +{ + char *p; + int i; + + for (i = 0; i < 3; i++) { + *t[i] = '\0'; + for (p = s; *p; p++) + if (strchr(allowed, *p) != NULL) + strlcat(t[i], seg[(int)*p][i], sz); + } +} diff --git a/Makefile b/Makefile index d253627..16be279 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # $Id$ PROG= aclock -SRCS= aclock.c textclock.c +SRCS= aclock.c textclock.c 7seg.c LDADD= -lcurses -lm NOMAN= diff --git a/aclock.c b/aclock.c index 9ac7051..03309e3 100644 --- a/aclock.c +++ b/aclock.c @@ -15,6 +15,7 @@ #include #include #include +#include #include int die = 0; @@ -22,6 +23,7 @@ int redraw = 0; enum {RED = 1, CYAN, BLUE}; char *strtime(char *, size_t, struct tm *); +void sevenseg(char [3][80], size_t, char *); void catch(int sig) @@ -89,7 +91,8 @@ const char INFO[] = "Copyright (c) 2002 by Antek Sawicki \n" int main(void) { - char digital_time[32], stime[80]; + char digital_time[32], stime[80], dtime[3][80]; + int i; int FontHW = 2; int sXmax, sYmax, smax, hand_max, sXcen, sYcen; time_t t; @@ -98,7 +101,6 @@ main(void) struct timeval tv; int hascolors; - signal(SIGWINCH, catch); signal(SIGALRM, catch); signal(SIGINT, catch); @@ -135,29 +137,46 @@ main(void) hand_max = (smax / 2) - 1; sXcen = sXmax / 2; - sYcen = sYmax / 2; + sYcen = sYmax / 2 - 1; erase(); draw_circle(hand_max, sYcen, sXcen, FontHW); + attron(COLOR_PAIR(CYAN)); + strftime(digital_time, sizeof(digital_time), "%d-%m-%y", ltime); +#if 1 + sevenseg(dtime, sizeof(dtime[0]), digital_time); + for (i = 0; i < 3; i++) + mvprintw(i + sYcen - sYmax / 4 - 1, + sXcen - strlen(dtime[i]) / 2, dtime[i]); +#else + mvprintw(sYmax / 4, sXcen - strlen(digital_time) / 2, digital_time); +#endif + strftime(digital_time, sizeof(digital_time), "%H:%M:%S", ltime); +#if 1 + sevenseg(dtime, sizeof(dtime[0]), digital_time); + for (i = 0; i < 3; i++) + mvprintw(i + sYcen + sYmax / 4 - 1, + sXcen - strlen(dtime[i]) / 2, dtime[i]); +#else + mvprintw(3 * sYmax / 4, sXcen - strlen(digital_time) / 2, digital_time); +#endif + attroff(COLOR_PAIR(CYAN)); + strtime(stime, sizeof(stime), ltime); + attron(COLOR_PAIR(RED)); draw_hand((ltime->tm_hour * 5) + (ltime->tm_min / 10), 2 * hand_max / 3, '#', sXcen, sYcen, FontHW); attroff(COLOR_PAIR(RED)); + attron(COLOR_PAIR(CYAN)); draw_hand(ltime->tm_min, hand_max - 2, '*', sXcen, sYcen, FontHW); attroff(COLOR_PAIR(CYAN)); + attron(COLOR_PAIR(BLUE)); draw_hand(ltime->tm_sec, hand_max - 1, '.', sXcen, sYcen, FontHW); attroff(COLOR_PAIR(BLUE)); - attron(COLOR_PAIR(CYAN)); - strftime(digital_time, sizeof(digital_time), "%d-%b-%y", ltime); - mvprintw(sYmax / 4, sXcen - strlen(digital_time) / 2, digital_time); - strftime(digital_time, sizeof(digital_time), "%H:%M:%S", ltime); - mvprintw(3 * sYmax / 4, sXcen - strlen(digital_time) / 2, digital_time); - attroff(COLOR_PAIR(CYAN)); - strtime(stime, sizeof(stime), ltime); attron(COLOR_PAIR(BLUE)); mvprintw(sYmax - 1, sXcen - strlen(stime) / 2, stime); attroff(COLOR_PAIR(BLUE)); -- cgit v1.2.3