forked from ianhalpern/rentshare-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rentshare_display.c
192 lines (153 loc) · 5.11 KB
/
rentshare_display.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#define BIT_ISSET( flags, flag ) ( ( (flags) & (flag) ) == (flag) )
#define SegA 9
#define SegB 10
#define SegC 11
#define SegD 12
#define SegE 13
#define SegF 8
#define SegG 7
#define Dig0 34
#define Dig1 32
#define Dig2 30
#define Dig3 28
#define Dig4 26
#define Dig5 24
#define Dig6 22
#define Dig7 6
#define REFRESH_RATE 1
#define SERIAL_TERM_CHAR 0
#define SERIAL_BUFFER_SIZE 9
const char seg_num_to_pin[] = { SegA, SegB, SegC, SegD, SegE, SegF, SegG };
const char digit_num_to_pin[] = { Dig0, Dig1, Dig2, Dig3, Dig4, Dig5, Dig6, Dig7 };
const char seg_num_to_bit[] = { 0b1000000, 0b0100000, 0b0010000, 0b0001000, 0b0000100, 0b0000010, 0b0000001 };
const char char_to_seg_map[] = {
/* digits */
0b1111110 /*0*/, 0b0110000 /*1*/, 0b1101101 /*2*/, 0b1111001 /*3*/, 0b0110011 /*4*/,
0b1011011 /*5*/, 0b0011111 /*6*/, 0b1110000 /*7*/, 0b1111111 /*8*/, 0b1110011 /*9*/,
/* letters */
0b1110111 /*A*/, 0b0011111 /*B*/, 0b1001110 /*C*/, 0b0111101 /*D*/, 0b1001111 /*E*/, 0b1000111 /*F*/,
0b1111011 /*G*/, 0b0110111 /*H*/, 0b0000110 /*I*/, 0b0111000 /*J*/, 0b1111110 /*K*/, 0b0001110 /*L*/,
0b0110111 /*M*/, 0b0010101 /*N*/, 0b1111110 /*O*/, 0b1100111 /*P*/, 0b1111110 /*Q*/, 0b0000101 /*R*/,
0b1011011 /*S*/, 0b1110000 /*T*/, 0b0111110 /*U*/, 0b0111110 /*V*/, 0b1111110 /*W*/, 0b1100111 /*X*/,
0b0110111 /*Y*/, 0b0110000 /*Z*/,
/* blank */
0b0000000 };
char display[SERIAL_BUFFER_SIZE] = { '8', '8', '8', '8', '8', '8', '8', '8' };
void setup() {
for ( char i = 0; i < sizeof( seg_num_to_pin ); i++ )
pinMode( seg_num_to_pin[i], OUTPUT );
for ( char i = 0; i < sizeof( digit_num_to_pin ); i++ )
pinMode( digit_num_to_pin[i], OUTPUT );
clear();
Serial.begin(9600);
}
void clear() {
for ( char i = 0; i < sizeof( seg_num_to_pin ); i++ )
digitalWrite( seg_num_to_pin[i], LOW );
for ( char i = 0; i < sizeof( digit_num_to_pin ); i++ )
digitalWrite( digit_num_to_pin[i], HIGH );
}
char asciiToSegMap( char c ) {
//Serial.print(c - 48,DEC);
if ( c <= 57 && c >= 48 )
return char_to_seg_map[ c - 48 ];
if ( c <= 122 && c >= 97 )
return char_to_seg_map[ c - 87 ];
if ( c <= 90 && c >= 65 )
return char_to_seg_map[ c - 55 ];
if ( c == 32 )
return char_to_seg_map[ sizeof( char_to_seg_map ) - 1 ];
return char_to_seg_map[ sizeof( char_to_seg_map ) - 1 ];
}
bool digitContainsSeg( char c, char seg ) {
return BIT_ISSET( asciiToSegMap( c ), seg_num_to_bit[ seg ] );
}
void draw() {
for ( char seg_num = 0; seg_num < sizeof( seg_num_to_pin ); seg_num++ ) {
digitalWrite( seg_num_to_pin[ seg_num ], HIGH );
for ( char j = 0; j < sizeof( display ); j++ )
if ( digitContainsSeg( display[j], seg_num ) )
digitalWrite( digit_num_to_pin[j], LOW );
delay( REFRESH_RATE );
clear();
}
}
/*
Randomizer!!!
Upon an update from the serial bus, keep randomizing digits until the correct numbers are hit
*/
void update_sequence() {
// create an array of random digits
int ready = 0;
char target_display[SERIAL_BUFFER_SIZE];
memcpy( target_display, display, sizeof(display) );
for ( char i=0; i<sizeof( display ); i++ ) {
display[i] = 48;
if ( (target_display[i] < 48 || target_display[i] > 57) && (target_display[i] != 0 && target_display[i] != 10) ) {
Serial.print( "TARGET_DISPLAY[i]: ");
Serial.println( target_display[i], DEC );
return;
}
}
while ( ready < SERIAL_BUFFER_SIZE ) {
ready = 0;
for ( char i=0; i<sizeof( display ); i++ ) {
// if none of the digits are ready, randomize all (chars from '0' to '9'); else if a digit is ready, keep it, otherwise, randomize it
if ( display[i] != target_display[i] ) {
display[i] = random(48, 60);
if ( display[i] > 57 )
display[i] = (display[i] - 58) * 10;
}
//display[i] = ready > 0 ? ( display[i] == target_display[i] ? display[i] : random(49, 58) ) : random(49, 58);
if ( display[i] == target_display[i] )
ready++;
}
int delay_count=0;
while ( delay_count++ < 10 )
draw();
//delay(10);
}
}
void loop() {
draw();
if ( Serial.available() > 0 ) {
char buffer[ SERIAL_BUFFER_SIZE ] = {0,0,0,0,0,0,0,0,0};
// read the incoming byte:
Serial.readBytesUntil( SERIAL_TERM_CHAR, buffer, sizeof( buffer ) );
// say what you got:
Serial.print("I received: ");
Serial.println( buffer );
memcpy( display, buffer, sizeof( display ) );
update_sequence();
/*
if ( incomingByte == 116 ) {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
playMelody( melody_mario, beats_mario, sizeof(melody_mario) / 2 );
}
else if ( incomingByte == 112 ) {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
playMelody( melody_gadget, beats_gadget, sizeof(melody_gadget) / 2 );
}
}
*/
}
/*
t++;
if ( t % 10000 ) {
for ( char c = 0; c < 1000; c++ ) {
char pos = 6;
if ( display[pos] == 9 )
while ( display[pos] == 9 ) {
display[pos] = 0;
pos--;
}
display[pos]++;
// for ( char i = 0; i < 1000 / ( REFRESH_RATE * 7 ); i++ )
draw();
}
}*/
}