-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsevseg_display.c
164 lines (136 loc) · 2.76 KB
/
sevseg_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
/*
* C file for the 7 segment display peripheral
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <miniat/miniat.h>
#include "sevseg_display.h"
#include "SDL/edison_seven_segment.h"
struct sevseg_display
{
int connected;
m_uword address;
m_bus *bus;
};
/*
*Function for creating a new 7 segment display
*
*Allocates the memory space for the peripheral,
*
*/
sevseg_display *sevseg_display_new(m_uword address)
{
sevseg_display *display = (sevseg_display *)malloc(sizeof(sevseg_display));
if(display)
{
display -> bus = (m_bus *)malloc(sizeof(m_bus));
if(!display -> bus)
{
free(display);
display = NULL;
}
else
{
display -> address = address;
display -> connected = 0;
}
}
return display;
}
/*
*Connects the 7 segment display peripheral to the bus
*
*
*/
void sevseg_display_bus_connector_set(sevseg_display *display, m_bus *bus)
{
if(display && bus)
{
if(!display -> connected)
{
free(display -> bus);
}
display -> bus = bus;
display -> connected = 1;
}
return;
}
/*
*Free's the 7 segment display
*
*The command free simply does the opposite of memory allocation (free's memory location)
*
*/
void sevseg_display_free(sevseg_display *display)
{
if(display)
{
if(!display -> connected)
{
free(display -> bus);
}
free(display);
}
return;
}
/*
*Updates the current state of the 7 segment display
*
*This is where is must update the graphics
*
*If display is set (peripheral was created), then
*set the data that's being passed through the bus
*to generate the correct graphics.
*
*/
void sevseg_display_clock(edison_sevenseg *sevseg ,sevseg_display *display)
{
int *state = malloc(sizeof(int)*8);
int i;
if(display)
{
if((display -> bus -> req) && (display -> bus -> address == display -> address) && (!display -> bus -> ack))
{
display -> bus -> ack = M_HIGH;
// Convert Decimal Number to binary and store into an array of size 8
state = decimal_to_binary(display -> bus -> data);
edison_sevenseg_set_state(sevseg, state);
}
else if(display -> bus -> ack && ((display -> bus -> address == display -> address)))
{
display -> bus -> ack = M_LOW;
}
}
return;
}
/*
* Sets the seven segment display bus
*
*/
void sevseg_display_set_bus(sevseg_display *display, m_bus bus)
{
if(display)
{
memcpy(display -> bus, &bus, sizeof(bus));
}
return;
}
/*
* Function to convert a decimal value to binary.
* This returns an array
*
*/
int *decimal_to_binary ( int segment_data )
{
int *binary = malloc(sizeof(int)*8);
int *state = malloc(sizeof(int)*8);
int i;
for( i=0; i <8; i++) {
binary[i] = segment_data % 2;
segment_data = segment_data/2;
}
return binary;
}