summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/str27seg.c
blob: f36e841e6b6e597846887af2b409acb4ef3ac716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
**  STR27SEG.C - Convert numeric strings to 7-segment strings.
**
**  Public domain by Bob Stout
**
**  Input:  A string (NUL-delimited char array) containing only digits
**          ('0' - '9' chars).
**
**  Output: The same string with each digit converted to a 7-segment
**          representation. Returns NULL on error.
*/

#include <stdio.h>
#include <ctype.h>

#define CAST(new_type,old_object) (*((new_type *)&old_object))
#define DISP(str) fputs((str), stdout)

/*
**  Define the bit significance
**
**     a
**    ---
**   |   |
**  f|   |b
**   | g |
**    ---
**   |   |
**  e|   |c
**   |   |
**    ---
**     d
*/

struct Seg7disp {
      unsigned seg_a : 1;
      unsigned seg_b : 1;
      unsigned seg_c : 1;
      unsigned seg_d : 1;
      unsigned seg_e : 1;
      unsigned seg_f : 1;
      unsigned seg_g : 1;
} Seg7digits[10] = {
      { 1, 1, 1, 1, 1, 1, 0 },      /* 0 */
      { 0, 1, 1, 0, 0, 0, 0 },      /* 1 */
      { 1, 1, 0, 1, 1, 0, 1 },      /* 2 */
      { 1, 1, 1, 1, 0, 0, 1 },      /* 3 */
      { 0, 1, 1, 0, 0, 1, 1 },      /* 4 */
      { 1, 0, 1, 1, 0, 1, 1 },      /* 5 */
      { 1, 0, 1, 1, 1, 1, 1 },      /* 6 */
      { 1, 1, 1, 0, 0, 0, 0 },      /* 7 */
      { 1, 1, 1, 1, 1, 1, 1 },      /* 8 */
      { 1, 1, 1, 1, 0, 1, 1 }       /* 9 */
};

char *str27seg(char *string)
{
      char *str;
      int ch;

      for (str = string ; *str; ++str)
      {
            if (!isdigit(*str))
                  return NULL;
            ch = CAST(int, Seg7digits[*str - '0']);
            *str = (char)(ch & 0xff);
      }
      return string;
}

#ifdef TEST

main()
{
      char tstrng[] = "0123456789", *segs;

      printf("str27seg() returned %p", segs = str27seg(tstrng));
      if (segs)
      {
            char *ptr;
            struct Seg7disp ch;
            int i;

            puts(" & produced:\n");
            for (ptr = segs ; *ptr; ++ptr)
            {
                  ch = CAST(struct Seg7disp, *ptr);
                  if (ch.seg_a)
                        DISP(" --- ");
                  else  DISP("     ");
                  DISP("  ");
            }
            puts("");
            for (i = 0; i < 3; ++i)
            {
                  for (ptr = segs ; *ptr; ++ptr)
                  {
                        ch = CAST(struct Seg7disp, *ptr);
                        if (ch.seg_f)
                              DISP("|   ");
                        else  DISP("    ");
                        if (ch.seg_b)
                              DISP("|");
                        else  DISP(" ");
                        DISP("  ");
                  }
                  puts("");
            }
            for (ptr = segs ; *ptr; ++ptr)
            {
                  ch = CAST(struct Seg7disp, *ptr);
                  if (ch.seg_g)
                        DISP(" --- ");
                  else  DISP("     ");
                  DISP("  ");
            }
            puts("");
            for (i = 0; i < 3; ++i)
            {
                  for (ptr = segs ; *ptr; ++ptr)
                  {
                        ch = CAST(struct Seg7disp, *ptr);
                        if (ch.seg_e)
                              DISP("|   ");
                        else  DISP("    ");
                        if (ch.seg_c)
                              DISP("|");
                        else  DISP(" ");
                        DISP("  ");
                  }
                  puts("");
            }
            for (ptr = segs ; *ptr; ++ptr)
            {
                  ch = CAST(struct Seg7disp, *ptr);
                  if (ch.seg_d)
                        DISP(" --- ");
                  else  DISP("     ");
                  DISP("  ");
            }
            puts("");
      }
      else  puts("\n");

      return 0;
}

#endif