-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCDFinalCode.c
317 lines (271 loc) · 5.65 KB
/
LCDFinalCode.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Name: Sharon Umute
* Student ID: V00852291
* Title: display_ext.c
* Extension: Pressing the right button allows you to change either message oone or message two
* Once selection is made, press left button to exit
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include "main.h"
#include "lcd_drv.h"
#include <avr/io.h>
#include <util/delay.h>
#define MAX_LENGTH 50
char msg_one[] = "Sharon Umute";
char msg_two[] = "UVIC Comp Sci";
char msg_ast[] = "****************";
char msg_dot[] = "................";
char blank[] = " ";
char rev_one[80];
char rev_two[80];
char* strings[] = {msg_one, msg_two, msg_ast, msg_dot};
//Reverse the strings
char* reverse_strncpy(char *dst, const char* src, size_t dstsiz)
{
if (dst != NULL && dstsiz > 0)
{
const size_t len = strlen(src);
const size_t last = len - 1;
size_t i = 0;
if (dstsiz > len)
{
for (; i < len; i++)
{
dst[i] = src[last - i];
}
}
dst[i] = '\0';
return dst;
}
return NULL;
}
//initialize string reversal
void string_init(){
reverse_strncpy(rev_one, strings[0], sizeof(rev_one));
reverse_strncpy(rev_two, strings[1], sizeof(rev_two));
}
//displays line 1
void display_line1(){
lcd_puts(msg_one);
}
//displays line 2
void display_line2(){
lcd_puts(msg_two);
}
//displays line 3
void display_line3(){
lcd_puts(rev_one);
}
//displays line 4
void display_line4(){
lcd_puts(rev_two);
}
//displays all asterisk
void display_all_ast(){
lcd_xy(0,0);
lcd_puts(msg_ast);
lcd_xy(0,1);
lcd_puts(msg_ast);
}
//displays all dots
void display_all_dot(){
lcd_xy(0,0);
lcd_puts(msg_dot);
lcd_xy(0,1);
lcd_puts(msg_dot);
}
//clear the screen
void lcd_clr(){
lcd_xy(0,0);
lcd_puts(blank);
lcd_xy(0,1);
lcd_puts(blank);
}
//get button pressed data
int button(){
ADCSRA |= 0x40;
while (ADCSRA & 0x40)
;
unsigned int val = ADCL;
unsigned int val2 = ADCH;
val += (val2 << 8);
if (val < 50) //btnRIGHT
return(1);
else if (val < 195) //btnUP
return(2);
else if (val < 380) //btnDOWN
return(4);
else if (val < 555) //btnLEFT
return(8);
else if (val < 790)
return(16); //btnSELECT
else{
return 0;
}
}
//delay
void delay_seconds(int seconds){
_delay_ms(1000*seconds);
}
//Change messages
void switch_msg(int valu){
bool exit=false;
int option;
int option2;
int val;
if (valu==1){
lcd_clr();
lcd_xy(0,0);
lcd_puts("Press Left");
lcd_xy(0,1);
lcd_puts("To EXIT");
delay_seconds(2);
lcd_clr();
lcd_xy(0,0);
lcd_puts("> Msg One");
lcd_xy(0,1);
lcd_puts(" Msg Two");
option=1;
while(!exit){
val =button();
if (val==4){
lcd_clr();
lcd_xy(0,0);
lcd_puts(" Msg One");
lcd_xy(0,1);
lcd_puts("> Msg Two");
option=2;
val=0;
}else if(val==2){
lcd_clr();
lcd_xy(0,0);
lcd_puts("> Msg One");
lcd_xy(0,1);
lcd_puts(" Msg Two");
option=1;
val=0;
}else if (val==16){
lcd_clr();
lcd_xy(0,0);
lcd_puts("> Hello");
lcd_xy(0,1);
lcd_puts(" Muppets");
option2=1;
while(!exit){
val =button();
if (val==4){
lcd_clr();
lcd_xy(0,0);
lcd_puts(" Hello");
lcd_xy(0,1);
lcd_puts("> Muppets");
option2=2;
val=0;
}else if(val==2){
lcd_clr();
lcd_xy(0,0);
lcd_puts("> Hello");
lcd_xy(0,1);
lcd_puts(" Muppets");
option2=1;
val=0;
}else if (val==16){
delay_seconds(2);
if(option==1){
if(option2==1){
strlcpy(msg_one, "Hello", sizeof(msg_one));
reverse_strncpy(rev_one, strings[0], sizeof(rev_one));
}else{
strlcpy(msg_one, "Muppets", sizeof(msg_one));
reverse_strncpy(rev_one, strings[0], sizeof(rev_one));
}
}else if(option==2){
if(option2==1){
strlcpy(msg_two, "Hello", sizeof(msg_two));
reverse_strncpy(rev_two, strings[1], sizeof(rev_two));
}else{
strlcpy(msg_two, "Muppets", sizeof(msg_two));
reverse_strncpy(rev_two, strings[1], sizeof(rev_two));
}
}
val=0;
}else if (val==8){
exit=true;
val=0;
}
}
val=0;
}else if (val==8){
exit=true;
}
}
}
}
int main( void )
{
DDRL = 0xFF;
DDRB = 0xFF;
ADCSRA = 0x87;
ADMUX = 0x40;
lcd_init();
lcd_clr();
string_init();
int i;
int step=1;
for (i=0; i<12; i++){
switch_msg (button());
//A switch statment was used to better get button data
//this way it checks buttons after each step, rather than each iteration of the whole process
switch(step){
case 1:
lcd_clr();
lcd_xy(0,0);
display_line1();
lcd_xy(0,1);
display_line2();
delay_seconds(1);
step=2;
break;
case 2:
lcd_clr();
lcd_xy(1,0);
display_line2();
lcd_xy(1,1);
display_line3();
delay_seconds(1);
step=3;
break;
case 3:
lcd_clr();
lcd_xy(0,0);
display_line3();
lcd_xy(0,1);
display_line4();
delay_seconds(1);
step=4;
break;
case 4:
lcd_clr();
lcd_xy(1,0);
display_line4();
lcd_xy(1,1);
display_line1();
delay_seconds(2);
lcd_clr();
step=1;
break;
}
}
for(;;){
lcd_clr();
display_all_ast();
delay_seconds(1);
lcd_clr();
display_all_dot();
delay_seconds(1);
}
}