Re: Simulating a seven-segment display

Discussion in 'C Programming' started by luserXtrog, Jul 3, 2009.

  1. luserXtrog

    luserXtrog Guest

    On Jul 2, 5:02 am, "0m"
    <> wrote:
    > On Jul 2, 5:31 pm, "0m"
    >
    >
    >
    > <> wrote:
    > > $ cat a.c
    > > /* a.c */

    >
    > > static const char segs[16][7 + 1] =
    > > {
    > >         "ABCDEF ", /* 0 */
    > >         " BC    ", /* 1 */
    > >         "AB DE G", /* 2 */
    > >         "ABCD  G", /* 3 */
    > >         " BC  FG", /* 4 */
    > >         "A CD FG", /* 5 */
    > >         "A CDEFG", /* 6 */
    > >         "ABC    ", /* 7 */
    > >         "ABCDEFG", /* 8 */
    > >         "ABC  FG", /* 9 */
    > >         "ABC EFG", /* 0xA */
    > >         "  CDEFG", /* 0xB */
    > >         "A  DEF ", /* 0xC */
    > >         " BCDE G", /* 0xD */
    > >         "A  DEFG", /* 0xE */
    > >         "A   EFG", /* 0xF */

    >
    > > };

    >
    > > void lightup(char seg[5][3], const char c)
    > > {
    > >         char i, *p;

    >
    > >         if (c >= '0' && c <= '9')       i = c - '0';
    > >         else if (c >= 'A' && c <= 'Z')  i = c - 'A' + 10;
    > >         else if (c >= 'a' && c <= 'z')  i = c - 'a' + 10;

    >
    >         else return;
    >
    >
    >
    > >         p = segs;
    > >         while (*p){
    > >                 switch(*p++){
    > >                 case 'A':
    > >                         seg[0][1] = '-';
    > >                         break;
    > >                 case 'B':
    > >                         seg[1][2] = '|';
    > >                         break;
    > >                 case 'C':
    > >                         seg[3][2] = '|';
    > >                         break;
    > >                 case 'D':
    > >                         seg[4][1] = '-';
    > >                         break;
    > >                 case 'E':
    > >                         seg[3][0] = '|';
    > >                         break;
    > >                 case 'F':
    > >                         seg[1][0] = '|';
    > >                         break;
    > >                 case 'G':
    > >                         seg[2][1] = '-';
    > >                         break;
    > >                 }
    > >         }

    >
    > > }

    >
    > > void showup(char seg[5][3], int lncnt, int clcnt)
    > > {
    > >         int i, j;
    > >         for (i = 0; i != lncnt; i++){
    > >                 for (j = 0; j != clcnt; j++){
    > >                         printf("%c", seg[j]);
    > >                 }
    > >                 printf("\n");
    > >         }

    >
    > > }

    >
    > > void cleanup(char seg[5][3], int lncnt, int clcnt)
    > > {
    > >         int i, j;
    > >         for (i = 0; i != lncnt; i++){
    > >                 for (j = 0; j != clcnt; j++){
    > >                         seg[j] = ' ';
    > >                 }
    > >         }

    >
    > > }

    >
    > > #include <stdio.h>
    > > int main(void)
    > > {
    > >         char seg[5][3] =
    > >         {
    > >                 "   ",
    > >                 "   ",
    > >                 "   ",
    > >                 "   ",
    > >                 "   "
    > >         };

    >
    > >         lightup(seg, '8');
    > >         showup(seg, 5, 3);
    > >         cleanup(seg, 5, 3);
    > >         printf("\n");
    > >         lightup(seg, 'b');
    > >         showup(seg, 5, 3);

    >
    > >         return 0;}

    >
    > > $

    >
    > It must be the worst piece in this thread though I spend a whole
    > afternoon on this piece. Currently, it can't display multiple 7-seg
    > digits on one line. Why I can't come up with code like Peter Nilsson's
    > and luserXtrog's above. (how old are you two guys :))
    >
    > Sigh.


    I turn 31 this year.
    I'm still in awe of Peter's code. It illustrates explicitly what
    I had to do by intuition. That first enum is brilliant.

    I think you should be able to extend this to display multiple
    units per line. Try expanding the seg array out to the total
    width, and adding an offset to i which increases by the width
    of unit after each digit.

    Sometimes a smoke/porn/tomb-raider break helps see it from a
    different angle.

    --
    lxt
     
    luserXtrog, Jul 3, 2009
    #1
    1. Advertising

  2. luserXtrog

    luserXtrog Guest

    On Jul 3, 1:02 am, luserXtrog <> wrote:
    > On Jul 2, 5:02 am, "0m"
    > (how old are you two guys :))
    >
    > > Sigh.

    >
    > I turn 31 this year.
    > I'm still in awe of Peter's code. It illustrates explicitly what
    > I had to do by intuition. That first enum is brilliant.
    >
    > I think you should be able to extend this to display multiple
    > units per line. Try expanding the seg array out to the total
    > width, and adding an offset to i which increases by the width
    > of unit after each digit.
    >
    > Sometimes a smoke/porn/tomb-raider break helps see it from a
    > different angle.
    >


    Or all three. Here's my take 2, with garbage refusal:

    #include <stdio.h>
    #include <string.h>

    int ao(int c) {
    switch(c) {
    case '0':return 0;
    case '1':return 1;
    case '2':return 2;
    case '3':return 3;
    case '4':return 4;
    case '5':return 5;
    case '6':return 6;
    case '7':return 7;
    case '8':return 8;
    case '9':return 9;
    case 'A':case 'a':return 10;
    case 'B':case 'b':return 11;
    case 'C':case 'c':return 12;
    case 'D':case 'd':return 13;
    case 'E':case 'e':return 14;
    case 'F':case 'f':return 15;
    default:return -1;
    }
    }

    enum {
    A = 1 << 0,
    B = 1 << 1,
    C = 1 << 2,
    D = 1 << 3,
    E = 1 << 4,
    F = 1 << 5,
    G = 1 << 6,
    H = 1 << 7 };

    int seg[] = {
    /*0*/ A+B+C+D+E+F,
    /*1*/ B+C,
    /*2*/ A+B+ D+E+ G,
    /*3*/ A+B+C+D+ G,
    /*4*/ B+C+ F+G,
    /*5*/ A+ C+D+ F+G,
    /*6*/ A+ C+D+E+F+G,
    /*7*/ A+B+C,
    /*8*/ A+B+C+D+E+F+G,
    /*9*/ A+B+C+ F+G,
    /*A*/ A+B+C+ E+F+G,
    /*b*/ C+D+E+F+G,
    /*C*/ A+ D+E+F,
    /*d*/ B+C+D+E+ G,
    /*E*/ A+ D+E+F+G,
    /*F*/ A+ E+F+G,
    };

    int print(char *s) {
    int r;
    for (r=0; r<3; r++) {
    int i;
    for (i=0; s; i++) {
    switch(r) {
    case 0:
    putchar(' ');
    putchar( (seg[ao(s)] & A)? '_' : ' ');
    putchar(' ');
    break;
    case 1:
    putchar( (seg[ao(s)] & F)? '|' : ' ');
    putchar( (seg[ao(s)] & G)? '_' : ' ');
    putchar( (seg[ao(s)] & B)? '|' : ' ');
    break;
    case 2:
    putchar( (seg[ao(s)] & E)? '|' : ' ');
    putchar( (seg[ao(s)] & D)? '_' : ' ');
    putchar( (seg[ao(s)] & C)? '|' : ' ');
    break;
    }
    }
    putchar('\n');
    }
    }

    int main(int ac, char **av) {
    for (++av,--ac;ac;ac--,av++) {
    if (strspn(*av, "012345789abcdefABCDEF") != strlen(*av))
    fprintf(stderr, "Hex only!\n");
    else
    print(*av);
    }

    return 0;
    }

    --
    lxt
     
    luserXtrog, Jul 4, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,907
    darshana
    Oct 26, 2008
  2. Elnikety
    Replies:
    2
    Views:
    1,430
    Elnikety
    May 7, 2007
  3. electronsteve

    7 segment display

    electronsteve, Nov 7, 2008, in forum: VHDL
    Replies:
    0
    Views:
    1,134
    electronsteve
    Nov 7, 2008
  4. Felipe Ribeiro

    Simulating a seven-segment display

    Felipe Ribeiro, Jul 1, 2009, in forum: C Programming
    Replies:
    16
    Views:
    1,061
    Squeamizh
    Jul 22, 2009
  5. 0m
    Replies:
    2
    Views:
    997
    Richard Bos
    Jul 5, 2009
Loading...

Share This Page