-
Notifications
You must be signed in to change notification settings - Fork 0
/
button_block.c
170 lines (143 loc) · 2.43 KB
/
button_block.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
/*
* C file for the Button Block peripheral
*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <miniat/miniat.h>
#include "button_block.h"
struct button_block
{
int connected;
m_uword address;
m_bus *bus;
};
/*
* Function for creating a new Button block
*
* Allocates the memory space for the peripheral,
*
*/
button_block *button_block_new(m_uword address)
{
button_block *buttons = (button_block *)malloc(sizeof(button_block));
if(buttons)
{
buttons -> bus = (m_bus *)malloc(sizeof(m_bus));
if(!buttons -> bus)
{
free(buttons);
buttons = NULL;
}
else
{
buttons -> address = address;
buttons -> connected = 0;
}
}
return buttons;
}
/*
* Connects the Button block to the bus
*
*
*/
void button_block_bus_connector_set(button_block *buttons, m_bus *bus)
{
if(buttons && bus)
{
if(!buttons -> connected)
{
free(buttons -> bus);
}
buttons -> bus = bus;
buttons -> connected = 1;
}
return;
}
/*
* Free's the Button block
*
* The command free simply does the opposite of memory allocation (free's memory location)
*
*/
void button_block_free(button_block *buttons)
{
if(buttons)
{
if(!buttons -> connected)
{
free(buttons -> bus);
}
free(buttons);
}
return;
}
/*
* Reads the states of the buttons to update the bus and the SDL
*
*
*
*/
void button_block_clock(button_block *buttons, edison_button *sdl_buttons[BUTTON_NUMBER])
{
bool btnStates[BUTTON_NUMBER];
int i;
if(buttons)
{
// 0x4020 - READBTN
if((buttons -> bus -> req) && (buttons -> bus -> address == buttons -> address) && (!buttons -> bus -> ack))
{
buttons -> bus -> ack = M_HIGH;
for(i = 0; i < BUTTON_NUMBER; i++)
{
btnStates[i] = edison_button_get_state(sdl_buttons[i]);
}
buttons -> bus -> data = boolToDec(btnStates);
}
else if(buttons -> bus -> ack && ((buttons -> bus -> address == buttons -> address)))
{
buttons -> bus -> ack = M_LOW;
}
}
return;
}
/*
* Gets the Buttons block's bus
*
*
*
*/
m_bus button_block_get_bus(button_block *buttons)
{
m_bus bus = { 0 };
if(!buttons)
{
return bus;
}
return *(buttons -> bus);
}
/*
* boolToBin
*
* This functions transforms an array of booleans
* in a binary integer
*
*/
int boolToDec(bool *states)
{
int number;
int i;
for(i = 0; i < (sizeof(states) / sizeof(states[0])); i++)
{
if(states[i])
{
number += pow(2, i);
}
}
return number;
}