1 /* example from http://barnyard.syr.edu/quickies/led.c */
3 /* led.c: print out number as if on 7 line led display. I.e., write integer
4 given on command line like this:
9 We assume the terminal behaves like a classical teletype. So the top
10 lines of all digits have to be printed first, then the middle lines of
15 compile: cc -o led led.c
17 If you just want to link in the subroutine print_led that does all the
18 work, compile with -DNO_MAIN, and declare the following in any source file
21 extern void print_led(unsigned long x, char *buf);
23 Bug: you cannot call repeatedly to print more than one number to a line.
24 That would require curses or some other terminal API that allows moving the
25 cursor to a previous line.
38 /* Print the top line of the digit d into buffer.
39 Does not null terminate buffer. */
41 void topline(int d
, char *p
){
46 /* all these have _ on top line */
64 /* Print the middle line of the digit d into the buffer.
65 Does not null terminate. */
67 void midline(int d
, char *p
){
71 /* those that have leading | on middle line */
86 /* those that have _ on middle line */
103 /* those that have closing | on middle line */
121 /* Print the bottom line of the digit d. Does not null terminate. */
123 void botline(int d
, char *p
){
128 /* those that have leading | on bottom line */
141 /* those that have _ on bottom line */
157 /* those that have closing | on bottom line */
176 /* Write the led representation of integer to string buffer. */
178 void print_led(unsigned long x
, char *buf
)
182 static int d
[MAX_DIGITS
];
185 /* extract digits from x */
187 n
= ( x
== 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */
190 d
[n
++] = (int)(x
%10L);
191 if(n
>= MAX_DIGITS
)break;
195 /* print top lines of all digits */
202 *buf
++='\n'; /* move teletype to next line */
204 /* print middle lines of all digits */
213 /* print bottom lines of all digits */
226 char buf
[5*MAX_DIGITS
];
227 print_led(1234567, buf
);
234 int main(int argc
, char **argv
)
239 static int d
[MAX_DIGITS
];
240 char buf
[5*MAX_DIGITS
];
243 fprintf(stderr
,"led: usage: led integer\n");
247 /* fetch argument from command line */
254 fprintf(stderr
,"led: %d must be non-negative\n",x
);
266 /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/