-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdip_switch.c
187 lines (157 loc) · 2.69 KB
/
dip_switch.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
/*
* C file for the DIP Switch peripheral
*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <miniat/miniat.h>
#include "dip_switch.h"
#include "button_block.h"
#define SWITCHES_NUMBER 4
struct dip_switch
{
int connected;
m_uword address;
m_bus *bus;
};
/*
* Function for creating a new DIP Switch
*
* Allocates the memory space for the peripheral,
*
*/
dip_switch *dip_switch_new(m_uword address)
{
dip_switch *switches = (dip_switch *)malloc(sizeof(dip_switch));
if(switches)
{
switches -> bus = (m_bus *)malloc(sizeof(m_bus));
if(!switches -> bus)
{
free(switches);
switches = NULL;
}
else
{
switches -> address = address;
switches -> connected = 0;
}
}
return switches;
}
/*
* Connects the DIP Switch to the bus
*
*
*/
void dip_switch_bus_connector_set(dip_switch *switches, m_bus *bus)
{
if(switches && bus)
{
if(!switches -> connected)
{
free(switches -> bus);
}
switches -> bus = bus;
switches -> connected = 1;
}
return;
}
/*
* Free's the DIP Switch
*
* The command free simply does the opposite of memory allocation (free's memory location)
*
*/
void dip_switch_free(dip_switch *switches)
{
if(switches)
{
if(!switches -> connected)
{
free(switches -> bus);
}
free(switches);
}
return;
}
/*
* Updates the current state of the DIP Switch
*
*
*
*/
void dip_switch_clock(dip_switch *switches, edison_dipswitch *edison_switches)
{
bool switch_states[SWITCHES_NUMBER];
int *temp_states;
int i;
int j = SWITCHES_NUMBER - 1;
temp_states = malloc(sizeof(int) * SWITCHES_NUMBER);
if(switches)
{
// 0x4030 - READDIP
if((switches -> bus -> req) && (switches -> bus -> address == switches -> address) && (!switches -> bus -> ack))
{
switches -> bus -> ack = M_HIGH;
temp_states = edison_dipswitch_get_state(edison_switches);
for(i = 0; i < SWITCHES_NUMBER; i++)
{
if(temp_states[j] == 1)
{
switch_states[i] = true;
}
else
{
switch_states[i] = false;
}
j--;
}
switches -> bus -> data = boolToDecimal(switch_states);
}
else if(switches -> bus -> ack && ((switches -> bus -> address == switches -> address)))
{
switches -> bus -> ack = M_LOW;
}
}
return;
}
/*
* Gets the switches DIP Switch bus
*
*
*
*/
m_bus dip_switch_get_bus(dip_switch *switches)
{
m_bus bus = { 0 };
if(!switches)
{
return bus;
}
return *(switches -> bus);
}
/*
* boolToBin
*
* This functions transforms an array of booleans
* in a binary integer
*
*/
int boolToDecimal(bool *states)
{
int number;
int i;
for(i = 0; i < SWITCHES_NUMBER; i++)
{
if(states[i])
{
number += pow(2, i);
}
}
return number;
}