-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlight.c
More file actions
202 lines (160 loc) · 4.5 KB
/
light.c
File metadata and controls
202 lines (160 loc) · 4.5 KB
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
//___________________________________________
// Joshua Schapiro
// light.c
// created: 09/07/2010
// updated:
//
//___________________________________________
#include "light.h"
uint8_t deviceAddress;
uint8_t writeAddress;
uint8_t commandCode;
uint16_t colors[4];
uint8_t colors8[8];
uint8_t timeOut;
void Light_Init(uint8_t address){
deviceAddress = address;
LightPort.MASTER.CTRLA = TWI_MASTER_ENABLE_bm;
//Set Master Read Acknowledge Action to ACK
LightPort.MASTER.CTRLC = TWI_MASTER_ACKACT_bm;
//Want 400 kHz I2C signal
//14.7456Mhz/(2*400kHz) - 5 = 13
LightPort.MASTER.BAUD = 13;
LightPort.MASTER.STATUS =TWI_MASTER_BUSSTATE_IDLE_gc;
gainSelector = 0;
Light_setGain();
}
uint8_t Light_readByte(uint8_t location){
uint8_t tmp;
//Send start condition, and device address + write
writeAddress = deviceAddress & ~0x01;
commandCode = 0b10000000 | location;
LightPort.MASTER.ADDR = writeAddress;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return 0;
}
}
LightPort.MASTER.DATA = commandCode;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return 0;
}
}
writeAddress = deviceAddress | 0x01; // start with address & read
LightPort.MASTER.ADDR = writeAddress;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_RIF_bm)) {
//Wait for Read flag to come on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return 0;
}
}
tmp = LightPort.MASTER.DATA;
LightPort.MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc; // stop
return tmp;
}
void Light_readColors(void){
while(Light_readByte(Control) && ADC_Valid_bm == 0);
//Send start condition, and device address + write
writeAddress = deviceAddress & ~0x01;
commandCode = 0b10000000 | 0x10;
LightPort.MASTER.ADDR = writeAddress;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return;
}
}
LightPort.MASTER.DATA = commandCode;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return;
}
}
writeAddress = deviceAddress | 0x01; // start with address & read
LightPort.MASTER.ADDR = writeAddress;
for(uint8_t i = 0; i < 8; i++){
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_RIF_bm)) {
//Wait for Read flag to come on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return;
}
}
colors8[i] = LightPort.MASTER.DATA;
//If more bytes to read, send ACK and start byte read
if(i < 7){
LightPort.MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
}
}
LightPort.MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc; // stop
colors[green] = colors8[1]*256 + colors8[0];
colors[red] = colors8[3]*256 + colors8[2];
colors[blue] = colors8[5]*256 + colors8[4];
colors[clear] = colors8[7]*256 + colors8[6];
}
uint16_t Light_returnColor(uint8_t color){
return colors[color];
}
void Light_writeByte(uint8_t location, uint8_t toSend){
//Send start condition, and device address + write
writeAddress = deviceAddress & ~0x01;
commandCode = 0b10000000 | location;
LightPort.MASTER.ADDR = writeAddress;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return;
}
}
LightPort.MASTER.DATA = commandCode;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
/*_delay_ms(1);
timeOut++;
if(timeOut == 0){
return;
}*/
}
LightPort.MASTER.DATA = toSend;
timeOut = 0;
while (!(LightPort.MASTER.STATUS & TWI_MASTER_WIF_bm)) {
//Wait for write flag to turn on
_delay_ms(1);
timeOut++;
if(timeOut == 0){
return;
}
}
LightPort.MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc; // stop
}
void Light_setGain(void){
Light_writeByte(Control, Power_bm);
Light_writeByte(Timing, (integrationTime[gainSelector] | Integ_Mode_Auto_bm));
Light_writeByte(Gain, (gainSetting[gainSelector]));
Light_writeByte(Control, (ADC_En_bm | Power_bm));
}