Velocity Reviews > Re: Simulating a seven-segment display

# Re: Simulating a seven-segment display

lovecreatesbeauty@gmail.c0m
Guest
Posts: n/a

 07-02-2009
On Jul 2, 5:31 pm, "(E-Mail Removed)0m"
<(E-Mail Removed)> 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[i];
> 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[i][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[i][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.

Lew Pitcher
Guest
Posts: n/a

 07-03-2009
On July 2, 2009 06:02, in comp.lang.c, (E-Mail Removed)0m
((E-Mail Removed)) wrote:

> On Jul 2, 5:31 pm, "(E-Mail Removed)0m"
> <(E-Mail Removed)> 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;

You may want to re-examine this statement. See below

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

You may want to re-examine this statement.

The C standard does not guarantee that alphabetic characters are allocated
in a continuous sequence. There are platforms where the alphabetic
characters are allocated "out of sequence", or with varying increments
(i.e., where 'J' - 'I' > 1 ).

While your above two statements will compile (likely without any
diagnostic), and will execute (again, likely without any diagnostic), on
some platforms (maybe yours), they will produce unintended and unexpected
results. (Likely that i will be set such that segs[i] will be
out-of-bounds.

> else return;
>
>> p = segs[i];
>> 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[i][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[i][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.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Richard Bos
Guest
Posts: n/a

 07-05-2009
luserXtrog <(E-Mail Removed)> wrote:

> int seg[] =3D {
> /*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,
> };

Or even

/*a*/ A+B+C+D+E+ G,
/*b*/ C+D+E+F+G,
/*c*/ A+ D+E+F,
/*d*/ B+C+D+E+ G,
/*e*/ A+B+ D+E+F+G,
/*f*/ A+ E+F+G,

Or possibly

/*c*/ D+E+ G,

Richard