-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightControl.c
More file actions
249 lines (205 loc) · 4.79 KB
/
Copy pathlightControl.c
File metadata and controls
249 lines (205 loc) · 4.79 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "lightControl.h"
#include "shiftRegisters.h"
#include <util/delay.h>
#ifdef DEBUG
#include "can.h"
#endif
#include <stdint.h>
// Internal model of the LED states
static unsigned char ledStates[ LED_COUNT ];
// The current number of lights lit up representing RPM
static uint8_t currentNumLights = 0;
static uint8_t currentGear = 0;
// Shift register output patterns that produce decimal digits on 7 segment display
static unsigned char segmentPatterns[ NUM_GEARS + 1][ SEGMENT_DISPLAY_LED_COUNT ] = {
{ 1, 0, 1, 0, 1, 0, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0, 1},
{ 1, 0, 1, 1, 0, 0, 1, 1},
{ 1, 0, 0, 1, 1, 0, 1, 1},
{ 1, 1, 0, 0, 1, 0, 0, 1}
};
/*
* Shift the bits stored in the internal model of the LEDS to the outputs of the shift registers.
*
*/
void updateLights()
{
#ifdef DEBUG
// Count lights
uint8_t count = 0;
for( uint8_t i = 0; i < 50; ++i )
{
if( ledStates[ i ] == 1 )
{
count++;
}
}
CANMessage message;
message.id = 0xAB;
message.data[0] = count;
message.data[1] = ledStates[ AUTO_UP_LED_1 ];
message.data[2] = ledStates[ AUTO_UP_LED_2 ];
message.data[3] = ledStates[ CRITICAL_ERROR_LED ];
message.data[4] = ledStates[ WARNING_LED ];
message.data[5] = ledStates[ DRS_LED ];
message.data[6] = ledStates[ HOLD_LED ];
message.data[7] = currentGear;
message.length = 8;
sendCAN( &message );
#endif
shiftPattern( ledStates, LED_COUNT );
}
/*
* It's gettin hot in here
*
*/
void shiftAll()
{
unsigned char pattern[LED_COUNT];
for(int i = 0; i < LED_COUNT; ++i)
pattern[i] = 1;
shiftPattern( pattern, LED_COUNT );
}
/*
* Based on a given RPM reading, change the internal model of the shift lights.
* @param[in] percentLit - percentage of the shift lights to illuminate. 0-100
*
*/
void setRPM( uint8_t percentLit )
{
if( percentLit > 100 )
percentLit = 100;
unsigned int numLights = ((float) percentLit / 100) * SHIFT_LIGHT_COUNT;
if( numLights != currentNumLights)
{
currentNumLights = numLights;
for( unsigned short int i = 0; i < SHIFT_LIGHT_COUNT; ++i )
{
if( i < numLights )
{
ledStates[ i ] = 1;
}
else
{
ledStates[ i ] = 0;
}
}
}
}
void clearRPM()
{
for( uint8_t i = 0; i < SHIFT_LIGHT_COUNT; ++i )
{
ledStates[i] = 0;
}
updateLights();
}
/*
* Change the internal model for the autoup indicating LEDs to a given state.
* @param[in] state - 1 or 0, the given state
*
*/
void setAutoUp( unsigned char state )
{
ledStates[ AUTO_UP_LED_1 ] = state & 1;
ledStates[ AUTO_UP_LED_2 ] = state & 1;
}
/*
* Change the internal model for the critical error indicator LED to a given state.
* @param[in] state - 1 or 0, the given state
*
*/
void setError( unsigned char state )
{
ledStates[ CRITICAL_ERROR_LED ] = state;
}
/*
* Change the internal model for the warning indicator LED to a given state.
* @param[in] state - 1 or 0, the given state
*
*/
void setWarning( unsigned char state )
{
ledStates[ WARNING_LED ] = state;
}
/*
* Change the internal model for the DRS indicator LED to a given state.
* @param[in] state - 1 or 0, the given state
*
*/
void setDRS( unsigned char state )
{
ledStates[ DRS_LED ] = state;
}
/*
* Change the internal model for the hold indicator LED to a given state.
* @param[in] state - 1 or 0, the given state
*
*/
void setHold( unsigned char state )
{
ledStates[ HOLD_LED ] = state;
}
/*
* Change the internal model for the seven-segment LEDs composing gear position based
* on a given gear position.
* @param[in] position - 1-4, the given gear position
*
*/
void setGearPosition( unsigned short int position )
{
currentGear = position;
if( position > NUM_GEARS )
{
for( unsigned short int i = SEGMENT_DISPLAY_START_LED; i < SEGMENT_DISPLAY_LED_COUNT + SEGMENT_DISPLAY_START_LED; ++i )
{
ledStates[ i ] = 0;
}
}
else
{
for( unsigned short int i = SEGMENT_DISPLAY_START_LED; i < SEGMENT_DISPLAY_LED_COUNT + SEGMENT_DISPLAY_START_LED; ++i )
{
ledStates[ i ] = segmentPatterns[ position ][ i - SEGMENT_DISPLAY_START_LED ];
}
}
}
void write7Seg( int s0, int s1, int s2, int s3, int s4, int s5, int s6, int s7 )
{
uint8_t pattern[] = { s0, s1, s2, s3, s4, s5, s6, s7 };
for( uint8_t i = SEGMENT_DISPLAY_START_LED; i < SEGMENT_DISPLAY_LED_COUNT + SEGMENT_DISPLAY_START_LED; ++i )
{
ledStates[ i ] = pattern[ i - SEGMENT_DISPLAY_START_LED ];
}
updateLights();
}
void lightShow()
{
clearRPM();
setAutoUp(0);
updateLights();
for( uint8_t i = 0; i < SHIFT_LIGHT_COUNT/2; ++i )
{
ledStates[SHIFT_LIGHT_COUNT/2+i] = 1;
ledStates[SHIFT_LIGHT_COUNT/2-i] = 1;
updateLights();
_delay_ms(5);
}
setAutoUp(1);
updateLights();
_delay_ms(5);
setAutoUp(0);
updateLights();
}
/*
* For flexing
*/
void writeBinary( uint32_t num )
{
uint8_t pos = 0;
for( uint8_t i = SHIFT_LIGHT_COUNT - 1; i > 17; --i )
{
ledStates[ i ] = (num & ( 1 << pos )) != 0;
++pos;
}
}