-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleds.c
311 lines (251 loc) · 6.53 KB
/
leds.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* leds.c - driver for all LEDs (buttons and discrete)
*
* Author: Dan Green (danngreen1@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* See http://creativecommons.org/licenses/MIT/ for more information.
*
* -----------------------------------------------------------------------------
*/
#include <dig_pins.h>
#include "globals.h"
#include "leds.h"
#include "timekeeper.h"
#include "params.h"
#include "calibration.h"
#include "system_settings.h"
extern volatile uint32_t ping_ledbut_tmr;
extern volatile uint32_t loopled_tmr[2];
extern volatile uint32_t divmult_time[2];
extern volatile uint32_t ping_time;
extern uint8_t mode[NUM_CHAN][NUM_CHAN_MODES];
extern uint8_t global_mode[NUM_GLOBAL_MODES];
extern float global_param[NUM_GLOBAL_PARAMS];
extern uint32_t flag_acknowlegde_qcm;
//extern uint32_t flash_firmware_version;
uint8_t loop_led_state[NUM_CHAN]={0,0};
void update_ping_ledbut(void)
{
if (global_mode[CALIBRATE])
LED_PINGBUT_OFF;
else if (global_mode[SYSTEM_SETTINGS])
{
//LED_PINGBUT_OFF;
}
else if (flag_acknowlegde_qcm)
{
flag_acknowlegde_qcm--;
if (
(flag_acknowlegde_qcm & (1<<8))
|| (!global_mode[QUANTIZE_MODE_CHANGES] && (flag_acknowlegde_qcm & (1<<6)))
)
{
LED_PINGBUT_ON;
LED_REV1_ON;
LED_REV2_ON;
}else{
LED_PINGBUT_OFF;
LED_REV1_OFF;
LED_REV2_OFF;
}
}
else
{
if (ping_ledbut_tmr>=ping_time)
{
LED_PINGBUT_ON;
reset_ping_ledbut_tmr();
}
else if (ping_ledbut_tmr >= (ping_time>>1))
{
LED_PINGBUT_OFF;
}
}
}
/*
|| (mode[channel][WINDOWMODE_POT]==WINDOW && (flicker_ctr & 0x3FFFF) <= 0x5000)
|| (mode[channel][TIMEMODE_POT]==MOD_READWRITE_TIME_NOQ && (flicker_ctr & 0xFFFF) > 0xC000)
*/
void chase_all_lights(uint32_t delaytime)
{
LED_LOOP1_ON;
delay_ms(delaytime);
LED_LOOP1_OFF;
delay_ms(delaytime);
LED_PINGBUT_ON;
delay_ms(delaytime);
LED_PINGBUT_OFF;
delay_ms(delaytime);
LED_LOOP2_ON;
delay_ms(delaytime);
LED_LOOP2_OFF;
delay_ms(delaytime);
LED_REV1_ON;
delay_ms(delaytime);
LED_REV1_OFF;
delay_ms(delaytime);
LED_INF1_ON;
delay_ms(delaytime);
LED_INF1_OFF;
delay_ms(delaytime);
LED_INF2_ON;
delay_ms(delaytime);
LED_INF2_OFF;
delay_ms(delaytime);
LED_REV2_ON;
delay_ms(delaytime);
LED_REV2_OFF;
delay_ms(delaytime);
//infinite loop to force user to reset
}
void blink_all_lights(uint32_t delaytime)
{
LED_LOOP1_ON;
LED_PINGBUT_ON;
LED_LOOP2_ON;
LED_REV1_ON;
LED_INF1_ON;
LED_INF2_ON;
LED_REV2_ON;
delay_ms(delaytime);
LED_LOOP1_OFF;
LED_PINGBUT_OFF;
LED_LOOP2_OFF;
LED_REV1_OFF;
LED_INF1_OFF;
LED_INF2_OFF;
LED_REV2_OFF;
delay_ms(delaytime);
}
void update_channel_leds(void)
{
uint8_t channel;
for (channel=0;channel<NUM_CHAN;channel++)
{
//if (!mode[channel][CONTINUOUS_REVERSE])
//{
if (loopled_tmr[channel] >= divmult_time[channel] && (mode[channel][INF]==INF_OFF))
{
reset_loopled_tmr(channel);
}
else if (loopled_tmr[channel] >= (divmult_time[channel]>>1))
{
loop_led_state[channel]=0;
if (channel==0) {
CLKOUT1_OFF;
} else {
CLKOUT2_OFF;
}
}
else if (mode[channel][LOOP_CLOCK_GATETRIG] == TRIG_MODE && loopled_tmr[channel] >= TRIG_TIME)
{
if (channel==0)
CLKOUT1_OFF;
else
CLKOUT2_OFF;
}
//}
}
}
void update_INF_REV_ledbut(uint8_t channel)
{
static uint32_t flicker_ctr=0;
uint8_t t;
flicker_ctr-=(1<<25);
if (global_mode[CALIBRATE])
{
update_calibration_button_leds();
}
else if (global_mode[SYSTEM_SETTINGS])
{
update_system_settings_button_leds();
}
else
{
//let the ping button function handle the rev lights blinking in ack_qcm state
if (!flag_acknowlegde_qcm)
{
//create a flicker by inverting the led state
t = mode[channel][CONTINUOUS_REVERSE] && (flicker_ctr<(1<<23));
if (mode[channel][REV] == t)
{
if (channel==0) LED_REV1_OFF;
else LED_REV2_OFF;
} else
{
if (channel==0) LED_REV1_ON;
else LED_REV2_ON;
}
}
//create a flicker by inverting the state
t = mode[channel][PING_LOCKED] && (flicker_ctr<(1<<28));
if ((mode[channel][INF]!=INF_ON && mode[channel][INF]!=INF_TRANSITIONING_ON) == t)
{
if (channel==0) LED_INF1_ON;
else LED_INF2_ON;
}
else
{
if (channel==0) LED_INF1_OFF;
else LED_INF2_OFF;
}
}
}
void init_LED_PWM_IRQ(void)
{
TIM_TimeBaseInitTypeDef tim;
NVIC_InitTypeDef nvic;
RCC_APB1PeriphClockCmd(LED_TIM_RCC, ENABLE);
nvic.NVIC_IRQChannel = LED_TIM_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 2;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
TIM_TimeBaseStructInit(&tim);
// tim.TIM_Period = 17500; //168MHz / 2 / 17500 = 4.8kHz (208.3us) ... / 32 =
tim.TIM_Period = 4375; //168MHz / 2 / 4375 = 19.2kHz
tim.TIM_Prescaler = 0;
tim.TIM_ClockDivision = 0;
tim.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(LED_TIM, &tim);
TIM_ITConfig(LED_TIM, TIM_IT_Update, ENABLE);
TIM_Cmd(LED_TIM, ENABLE);
}
//runs @ 208uS (4.8kHz), with 32 steps => 6.6ms PWM period = 150Hz
void LED_PWM_IRQHandler(void)
{
static uint32_t loop_led_PWM_ctr=0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
//begin1: 300ns - 450ns
if (loop_led_state[0] && (loop_led_PWM_ctr<global_param[LOOP_LED_BRIGHTNESS]))
LED_LOOP1_ON;
else
LED_LOOP1_OFF;
if (loop_led_state[1] && (loop_led_PWM_ctr<global_param[LOOP_LED_BRIGHTNESS]))
LED_LOOP2_ON;
else
LED_LOOP2_OFF;
if (loop_led_PWM_ctr++>32)
loop_led_PWM_ctr=0;
//end1: 300ns - 450ns
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}