boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

00182.c (4442B)


      1 /* example from http://barnyard.syr.edu/quickies/led.c */
      2 
      3 /* led.c: print out number as if on 7 line led display. I.e., write integer
      4    given on command line like this:  
      5       _   _       _  
      6    |  _|  _| |_| |_  
      7    | |_   _|   |  _| etc.
      8 
      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
     11    all digits, etc.
     12 
     13    By Terry R. McConnell
     14 
     15 compile: cc -o led led.c
     16 
     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
     19 that uses the call:
     20 
     21 extern void print_led(unsigned long x, char *buf);
     22 
     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.
     26 
     27 */
     28 
     29 
     30 
     31 #include <stdlib.h>
     32 #include <stdio.h>
     33 
     34 #define MAX_DIGITS 32
     35 #define NO_MAIN
     36 
     37 
     38 /* Print the top line of the digit d into buffer. 
     39    Does not null terminate buffer. */
     40 
     41 void topline(int d, char *p){
     42 
     43    *p++ = ' ';
     44    switch(d){
     45 
     46       /* all these have _ on top line */
     47 
     48       case 0:
     49       case 2:
     50       case 3:
     51       case 5:
     52       case 7:
     53       case 8:
     54       case 9:
     55          *p++ = '_';
     56          break;
     57       default:
     58          *p++=' ';
     59 
     60    }
     61    *p++=' ';
     62 }
     63 
     64 /* Print the middle line of the digit d into the buffer. 
     65    Does not null terminate. */
     66 
     67 void midline(int d, char *p){
     68 
     69    switch(d){
     70 
     71       /* those that have leading | on middle line */
     72 
     73       case 0:
     74       case 4:
     75       case 5:
     76       case 6:
     77       case 8:
     78       case 9:
     79          *p++='|';
     80          break;
     81       default:
     82          *p++=' ';	
     83    }
     84    switch(d){
     85 
     86       /* those that have _ on middle line */
     87 
     88       case 2:
     89       case 3:
     90       case 4:
     91       case 5:
     92       case 6:
     93       case 8:
     94       case 9:
     95          *p++='_';
     96          break;
     97       default:
     98          *p++=' ';
     99 
    100    }
    101    switch(d){
    102 
    103       /* those that have closing | on middle line */
    104 
    105       case 0:
    106       case 1:
    107       case 2:
    108       case 3:
    109       case 4:
    110       case 7:
    111       case 8:
    112       case 9:
    113          *p++='|';
    114          break;
    115       default:
    116          *p++=' ';
    117 
    118    }
    119 }
    120 
    121 /* Print the bottom line of the digit d. Does not null terminate. */
    122 
    123 void botline(int d, char *p){
    124 
    125 
    126    switch(d){
    127 
    128       /* those that have leading | on bottom line */
    129 
    130       case 0:
    131       case 2:
    132       case 6:
    133       case 8:
    134          *p++='|';
    135          break;
    136       default:
    137          *p++=' ';	
    138    }
    139    switch(d){
    140 
    141       /* those that have _ on bottom line */
    142 
    143       case 0:
    144       case 2:
    145       case 3:
    146       case 5:
    147       case 6:
    148       case 8:
    149          *p++='_';
    150          break;
    151       default:
    152          *p++=' ';
    153 
    154    }
    155    switch(d){
    156 
    157       /* those that have closing | on bottom line */
    158 
    159       case 0:
    160       case 1:
    161       case 3:
    162       case 4:
    163       case 5:
    164       case 6:
    165       case 7:
    166       case 8:
    167       case 9:
    168          *p++='|';
    169          break;
    170       default:
    171          *p++=' ';
    172 
    173    }
    174 }
    175 
    176 /* Write the led representation of integer to string buffer. */
    177 
    178 void print_led(unsigned long x, char *buf)
    179 {
    180 
    181    int i=0,n;
    182    static int d[MAX_DIGITS];
    183 
    184 
    185    /* extract digits from x */
    186 
    187    n = ( x == 0L ? 1 : 0 );  /* 0 is a digit, hence a special case */
    188 
    189    while(x){
    190       d[n++] = (int)(x%10L);
    191       if(n >= MAX_DIGITS)break;
    192       x = x/10L;
    193    }
    194 
    195    /* print top lines of all digits */
    196 
    197    for(i=n-1;i>=0;i--){
    198       topline(d[i],buf);
    199       buf += 3;
    200       *buf++=' ';
    201    }
    202    *buf++='\n'; /* move teletype to next line */
    203 
    204    /* print middle lines of all digits */
    205 
    206    for(i=n-1;i>=0;i--){
    207       midline(d[i],buf);
    208       buf += 3;
    209       *buf++=' ';
    210    }
    211    *buf++='\n';
    212 
    213    /* print bottom lines of all digits */
    214 
    215    for(i=n-1;i>=0;i--){
    216       botline(d[i],buf);
    217       buf += 3;
    218       *buf++=' ';
    219    }
    220    *buf++='\n';
    221    *buf='\0';
    222 }
    223 
    224 int main()
    225 {
    226    char buf[5*MAX_DIGITS];
    227    print_led(1234567, buf);
    228    printf("%s\n",buf);
    229 
    230    return 0;
    231 }
    232 
    233 #ifndef NO_MAIN
    234 int main(int argc, char **argv)
    235 {
    236 
    237    int i=0,n;
    238    long x;
    239    static int d[MAX_DIGITS];
    240    char buf[5*MAX_DIGITS];
    241 
    242    if(argc != 2){
    243       fprintf(stderr,"led: usage: led integer\n");
    244       return 1;
    245    }
    246 
    247    /* fetch argument from command line */
    248 
    249    x = atol(argv[1]);
    250 
    251    /* sanity check */
    252 
    253    if(x<0){
    254       fprintf(stderr,"led: %d must be non-negative\n",x);
    255       return 1;
    256    }
    257 
    258    print_led(x,buf);
    259    printf("%s\n",buf);
    260 
    261    return 0;
    262 
    263 }
    264 #endif
    265 
    266 /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/