-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd_display.c
177 lines (145 loc) · 2.81 KB
/
lcd_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
/*
* C file for the LCD display peripheral
*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <miniat/miniat.h>
#include "lcd_display.h"
struct lcd_display
{
int connected;
m_uword address;
m_bus *bus;
};
/*
* Function for creating a new LCD display
*
* Allocates the memory space for the peripheral,
*
*/
lcd_display *lcd_display_new(m_uword address)
{
lcd_display *lcd = (lcd_display *)malloc(sizeof(lcd_display));
if(lcd)
{
lcd -> bus = (m_bus *)malloc(sizeof(m_bus));
if(!lcd -> bus)
{
free(lcd);
lcd = NULL;
}
else
{
lcd -> address = address;
lcd -> connected = 0;
}
}
return lcd;
}
/*
* Connects the LCD display to the bus
*
*
*/
void lcd_display_bus_connector_set(lcd_display *lcd, m_bus *bus)
{
if(lcd && bus)
{
if(!lcd -> connected)
{
free(lcd -> bus);
}
lcd -> bus = bus;
lcd -> connected = 1;
}
return;
}
/*
* Free's the LCD display
*
* The command free simply does the opposite of memory allocation (free's memory location)
*
*/
void lcd_display_free(lcd_display *lcd)
{
if(lcd)
{
if(!lcd -> connected)
{
free(lcd -> bus);
}
free(lcd);
}
return;
}
/*
* LCD clock
*
* Updates the display with the information sent
*
*/
void lcd_display_clock(lcd_display *lcd, edison_lcd_display *edison_lcd)
{
char character;
if(lcd)
{
// 0x4040 - CURSOR X
if((lcd -> bus -> req) && (lcd -> bus -> address == lcd -> address) && (!lcd -> bus -> ack))
{
lcd -> bus -> ack = M_HIGH;
if((int)lcd -> bus -> data >= 0 && (int)lcd -> bus -> data < CHARS_PER_LINE)
{
edison_lcd_display_set_cursor_x(edison_lcd, (int)lcd -> bus -> data);
}
}
else if(lcd -> bus -> ack && ((lcd -> bus -> address == lcd -> address)))
{
lcd -> bus -> ack = M_LOW;
}
// 0x4041 - CURSOR Y
else if((lcd -> bus -> req) && (lcd -> bus -> address == lcd -> address + 1) && (!lcd -> bus -> ack))
{
lcd -> bus -> ack = M_HIGH;
if((int)lcd -> bus -> data >= 0 && (int)lcd -> bus -> data < NUMBER_LINES)
{
edison_lcd_display_set_cursor_y(edison_lcd, (int)lcd -> bus -> data);
}
}
else if(lcd -> bus -> ack && ((lcd -> bus -> address == lcd -> address + 1)))
{
lcd -> bus -> ack = M_LOW;
}
// 0x4042 - WRITING
else if((lcd -> bus -> req) && (lcd -> bus -> address == lcd -> address + 2) && (!lcd -> bus -> ack))
{
lcd -> bus -> ack = M_HIGH;
character = (char)lcd -> bus -> data;
//printf("%c\n", character);
edison_lcd_display_set_char(edison_lcd, character);
}
else if(lcd -> bus -> ack && ((lcd -> bus -> address == lcd -> address + 2)))
{
lcd -> bus -> ack = M_LOW;
}
}
return;
}
/*
* Gets the LCD display's bus
*
*
*
*/
m_bus lcd_display_get_bus(lcd_display *lcd)
{
m_bus bus = { 0 };
if(!lcd)
{
return bus;
}
return *(lcd -> bus);
}