-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparkle_main.c
728 lines (612 loc) · 18.3 KB
/
sparkle_main.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//*****************************************************************************
//
// sparkle_main.c - Sparkling main program
//
// Sets on PWM signal for LED control when interrupt signal is detected
// from PIR movement detector. After a period of no movement, turns the
// signal off.
//
// PIR signal interrupt is trigged on both signal edges.
// Interrupt turns on the PWM generator signal, ramping up the PWM pulse width.
// PWM pulse is used to drive the MOSFET, and thus smoothly lighting up the LED strip.
//
// Each interrupt resets the idle timer (TIMER1).
// When the no movement is detected for defined period (60sec), timer runs out
// causing another interrupt. This interrupt starts a PWM pulse width ramp-down,
// dimming the leds and finally turns off the PWM generator output.
//
// Pins used on EK-TM4C123GLX board
//
// PA5 - PIR sensor input
// PE4 - HW switch input
// PA2 - IR detector input
// PB6 - PWM generator output for IR LED
// PC5 - PWM generator output for LED strip 1 (motion trigged)
// PC4 - PWM generator output for LED strip 2 (switch trigged)
// PF2 - On-board led, blue component
//
//*****************************************************************************
// Todo: make this runtime selectable, by the user buttons, etc.
#define RUN_AS_MASTER
//#define RUN_AS_SLAVE
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
void GPIOLightSwitchIsr(void);
void GPIOMotionDetectorIsr(void);
void SendIRCode(uint32_t code);
void blink_n(uint32_t n);
void IRIntHandler(void);
void IRTimerIsr(void);
void IdleTimerIsr(void);
void rampGenericPWM(bool rampDirection, uint32_t pwmBase, uint32_t pwm,
uint32_t pwmBit, uint32_t delay);
int decodePulseBuffer(uint32_t *pulse_buf);
void delay_ms(uint32_t ui32Us);
void delay_us(uint32_t ui32Us);
#define LIGHTS_ON_PERIOD_SEC 60
#define SHORT_IDLE_TIME_MINS 3
#define LONG_IDLE_TIME_MINS 10
#define PWM_FREQUENCY 500
#define PWM_LOW 10
#define PWM_STEP 10
#define PWM_RAMP_UP true
#define PWM_RAMP_DOWN false
#define ON 1
#define OFF 0
// define PORT and PIN that the detector is connected to
#define IR_PORT GPIO_PORTA_BASE
#define IR_PIN GPIO_PIN_2
#define IR_TIMER_BASE TIMER0_BASE
#define IR_TIMER TIMER_A
#define IR_TIMER_PERIPH SYSCTL_PERIPH_TIMER0
#define IR_TIMER_CFG TIMER_CFG_A_ONE_SHOT
#define IR_TIMER_INT1 INT_TIMER1A
#define IR_TIMER_INT2 TIMER_TIMA_TIMEOUT
#define IR_TIMEOUT_VAL 20000 // 20 ms (20000 us) for now
#define MAX_PULSE_COUNT 100
#define IR_MAX_BITS_VAL 6 // couple of extra bits
// Todo: set better binary values
#define AMBIENCE_ON 0x1
#define AMBIENCE_OFF 0x2
#define SWITCH_ON 0x5
#define SWITCH_OFF 0x6
bool idleTimerTrigged = 0, motionDetectorTrigged = 0, lightSwitchTrigged = 0;
int receivedIRCode = 0;
uint32_t ulPeriod;
uint32_t ui32Load;
uint32_t ulIdleMinutes = 0;
volatile uint32_t ir_pulse_count = 0, ir_timeout_flag, ir_ppct;
volatile uint32_t g_ulIRPeriod, g_ulCountsPerMicrosecond;
uint32_t pulse_buf[MAX_PULSE_COUNT + 1]; // pulse width count buffer
#ifndef RUN_AS_MASTER
#ifndef RUN_AS_SLAVE
#error Define either role
#endif
#endif
#ifdef RUN_AS_MASTER
#ifdef RUN_AS_SLAVE
#error Define either role
#endif
#endif
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void ConfigureUART(void) {
//
// Enable the GPIO Peripheral used by the UART.
//
// ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure GPIO Pins for UART mode.
//
ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
}
void InitClocksGPIOAndTimer() {
uint32_t ui32PWMClock;
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPULazyStackingEnable();
//
// Set the clocking to run directly from the crystal.
//
ROM_SysCtlClockSet(
SYSCTL_SYSDIV_40 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
ROM_SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
// PWM Setup
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIOPinConfigure(GPIO_PC4_M0PWM6);
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4);
GPIOPinConfigure(GPIO_PC5_M0PWM7);
GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_5);
ui32PWMClock = SysCtlClockGet();
ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1;
PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, ui32Load);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, PWM_LOW);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, PWM_LOW);
// Started as not active
PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, false);
PWMOutputState(PWM0_BASE, PWM_OUT_7_BIT, false);
PWMGenEnable(PWM0_BASE, PWM_GEN_3);
// PWM Setup for IR LED
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB6_M0PWM0);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
PWMGenConfigure(PWM0_BASE, PWM_GEN_0,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 1050/8);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 525/8);
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, false);
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
//
// Enable peripheral and register interrupt handler
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOIntRegister(GPIO_PORTA_BASE, GPIOMotionDetectorIsr);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOIntRegister(GPIO_PORTE_BASE, GPIOLightSwitchIsr);
#ifdef RUN_AS_MASTER
GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_5);
GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_5, GPIO_BOTH_EDGES);
#else
GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2);
GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_2, GPIO_FALLING_EDGE);
#endif
GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_4);
GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_4, GPIO_RISING_EDGE);
//
// Enable the pin interrupts.
//
GPIOIntEnable(GPIO_PORTA_BASE, GPIO_INT_PIN_2 | GPIO_INT_PIN_5);
GPIOIntEnable(GPIO_PORTE_BASE, GPIO_INT_PIN_4);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
TimerConfigure(TIMER0_BASE, TIMER_CFG_A_ONE_SHOT);
TimerConfigure(TIMER1_BASE, TIMER_CFG_A_ONE_SHOT);
TimerIntRegister(TIMER0_BASE, TIMER_A, IRTimerIsr);
TimerIntRegister(TIMER1_BASE, TIMER_A, IdleTimerIsr);
ulPeriod = (SysCtlClockGet()); // once per second
TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod);
TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod * LIGHTS_ON_PERIOD_SEC);
TimerLoadSet(TIMER1_BASE, TIMER_B, ulPeriod * 3);
IntEnable(INT_TIMER0A);
IntEnable(INT_TIMER1A);
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
//
// Enable the GPIO port that is used for the on-board LED.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
//
// Enable the GPIO pin for blue LED component (PF2).
//
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
g_ulCountsPerMicrosecond = ROM_SysCtlClockGet() / 1000000;
// 10ms = timeout delay
g_ulIRPeriod = g_ulCountsPerMicrosecond * IR_TIMEOUT_VAL;
}
//
// ramp the PWM pulse widht up or down
//
void rampTopPWM(bool rampDirection) {
rampGenericPWM(rampDirection, PWM0_BASE, PWM_OUT_7, PWM_OUT_7_BIT,
SysCtlClockGet() / 2048);
}
void rampBottomPWM(bool rampDirection) {
rampGenericPWM(rampDirection, PWM0_BASE, PWM_OUT_6, PWM_OUT_6_BIT,
SysCtlClockGet() / 4096);
}
void rampGenericPWM(bool rampDirection, uint32_t pwmBase, uint32_t pwm,
uint32_t pwmBit, uint32_t delay) {
// Start value is the current PWM pulse width regardless the ramp direction
uint32_t i = PWMPulseWidthGet(pwmBase, pwm);
if (rampDirection == PWM_RAMP_UP) {
uint32_t targetPwmLoad = PWMGenPeriodGet(pwmBase, PWM_GEN_3);
PWMOutputState(pwmBase, pwmBit, true);
for (; i < targetPwmLoad; i += PWM_STEP)
{
PWMPulseWidthSet(pwmBase, pwm, i);
SysCtlDelay(delay);
}
} else // rampDirection == PWM_RAMP_DOWN
{
for (; i > PWM_LOW; i -= PWM_STEP)
{
PWMPulseWidthSet(pwmBase, pwm, i);
SysCtlDelay(delay);
}
PWMOutputState(pwmBase, pwmBit, false);
}
}
//
// (re)start timer for idle period counting (no movement -> lights out)
//
void startIdleDetectionTimer(uint32_t period, bool resetCounter) {
TimerLoadSet(TIMER1_BASE, TIMER_A, period);
TimerEnable(TIMER1_BASE, TIMER_A);
if (resetCounter)
{
ulIdleMinutes = 0;
}
}
//
// Sets the defined status led on or off
//
void setStatusLedState(bool statusOn) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, statusOn ? GPIO_PIN_2 : 0);
}
//
// returns motion detector GPIO pin status
//
bool motionDetectorGPIOActive() {
return GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5) != 0;
}
//
// returns light switch GPIO pin status
//
int lightSwitchGPIOActive() {
return GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4);
}
//
// returns idle timer running status, indicates whether lights are still on or not
//
bool idleTimerRunning() {
return TimerValueGet(TIMER1_BASE, TIMER_A)
!= TimerLoadGet(TIMER1_BASE, TIMER_A);
}
//
// returns idle timer remaining value in wall clock time
//
uint32_t idleTimerRemainingSecondsGet() {
return (TimerValueGet(TIMER1_BASE, TIMER_A) + SysCtlClockGet() / 2)
/ SysCtlClockGet();
}
//*****************************************************************************
//
// Main loop
//
//*****************************************************************************
int main(void) {
// Initialize relevant GPIO pins and periodic timer
InitClocksGPIOAndTimer();
// Initialize the UART.
ConfigureUART();
UARTprintf("Started!\n");
// Init to shorter period
ulPeriod = (SysCtlClockGet() * LIGHTS_ON_PERIOD_SEC);
#ifdef RUN_AS_MASTER
uint32_t ulIdleMinutesLimit = SHORT_IDLE_TIME_MINS;
int lightSwitchState = 0;
#else
bool ambienceOn = false;
bool lightSwitchOn = false;
#endif
// Set the blue led on at startup. Will shut down in first periodic timer trig.
setStatusLedState(false);
while (1) {
#ifdef RUN_AS_MASTER
if (lightSwitchTrigged) {
int i, realHit = 1;
for (i = 0; i < 50; i++) {
SysCtlDelay(SysCtlClockGet() / 1000);
if (lightSwitchGPIOActive() == 0) {
realHit = 0;
break;
}
}
if (realHit) {
UARTprintf("Light switch hit\n");
if (lightSwitchState == OFF) {
lightSwitchState = ON;
UARTprintf("Switching ON\n");
SendIRCode(SWITCH_ON);
rampBottomPWM(PWM_RAMP_UP);
// Use longer idle period
ulIdleMinutesLimit = LONG_IDLE_TIME_MINS;
startIdleDetectionTimer(ulPeriod, true);
} else {
lightSwitchState = OFF;
UARTprintf("Switching OFF\n");
SendIRCode(SWITCH_OFF);
rampBottomPWM(PWM_RAMP_DOWN);
// Use shorter idle period
ulIdleMinutesLimit = SHORT_IDLE_TIME_MINS;
startIdleDetectionTimer(ulPeriod, true);
}
} else {
UARTprintf("False hit\n");
}
lightSwitchTrigged = 0;
}
if (idleTimerTrigged) {
// idle (no movement detected) timer trigged
idleTimerTrigged = 0;
ulIdleMinutes ++;
if (ulIdleMinutes < ulIdleMinutesLimit)
{
startIdleDetectionTimer(ulPeriod, false);
blink_n(1);
}
else
{
UARTprintf("Idle timer trigged, turning leds off\n");
if (motionDetectorGPIOActive()) {
// Level is still high - let's restart the timer.
UARTprintf("Level still high, restarting timer\n");
startIdleDetectionTimer(ulPeriod, true);
} else {
setStatusLedState(false);
// Turn the PMW output off
SendIRCode(AMBIENCE_OFF);
rampTopPWM(PWM_RAMP_DOWN);
if (lightSwitchState == ON)
{
SendIRCode(SWITCH_OFF);
rampBottomPWM(PWM_RAMP_DOWN);
}
}
}
}
if (motionDetectorTrigged) {
// Movement detected
motionDetectorTrigged = 0;
UARTprintf("motionDetectorTrigged\n");
// Send this in any case. Rx shall maintain its own state
SendIRCode(AMBIENCE_ON);
if (!idleTimerRunning()) {
setStatusLedState(true);
// Turn the PMW output on
rampTopPWM(PWM_RAMP_UP);
if (lightSwitchState == ON)
{
SendIRCode(SWITCH_ON);
rampBottomPWM(PWM_RAMP_UP);
}
UARTprintf("Starting timer\n");
} else {
// Leds are on at this point - no need to set those on again
UARTprintf("RE-Starting timer\n");
}
startIdleDetectionTimer(ulPeriod, true);
}
//if (timerTrigged)
{
// signal the state using on-board led and UART printouts
bool gpioActive = motionDetectorGPIOActive();
if (gpioActive) {
UARTprintf("Input active (motion detected)\n");
}
if (!idleTimerRunning()) {
UARTprintf("Timer not running\n");
} else {
UARTprintf("Timer running for %d\n",
idleTimerRemainingSecondsGet());
if (gpioActive) {
setStatusLedState(true);
} else {
setStatusLedState(false);
}
}
}
#else
UARTprintf("SLAVE LOOP: %d\n", ir_pulse_count);
if (receivedIRCode != 0) {
uint32_t _code = receivedIRCode;
receivedIRCode = 0;
switch (_code) {
case AMBIENCE_ON:
if (!ambienceOn)
{
rampTopPWM(PWM_RAMP_UP);
ambienceOn = true;
}
break;
case AMBIENCE_OFF:
if (ambienceOn)
{
rampTopPWM(PWM_RAMP_DOWN);
ambienceOn = false;
}
break;
case SWITCH_ON:
if (!lightSwitchOn)
{
rampBottomPWM(PWM_RAMP_UP);
lightSwitchOn = true;
}
break;
case SWITCH_OFF:
if (lightSwitchOn)
{
rampBottomPWM(PWM_RAMP_DOWN);
lightSwitchOn = false;
}
break;
default:
break;
}
// Blink out the code
if (_code < 10)
{
// blink_n(_code);
}
}
#endif
//
// Sleep until next interrupt
//
SysCtlSleep();
}
}
void blink_n(uint32_t n) {
setStatusLedState(false);
delay_ms(1000);
int i;
for (i = 0; i < n; i++) {
setStatusLedState(true);
delay_ms(200);
setStatusLedState(false);
delay_ms(200);
}
}
// 3ms => bit0=6ms, bit1=9ms
#define T1 3
#define T2 (2*T1)
#define T4 (4*T1)
// Send bit pattern
// Start pulse first, then the bits, lsb first
void SendIRCode(uint32_t code) {
// 1. send start pattern
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true);
delay_ms(T4);
// 2. send code bit-by-bit
int i;
// UARTprintf("TX: sending %x\n", code);
for (i = 0; i < IR_MAX_BITS_VAL; i++) {
// transmit start of the bit (PWM off)
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, false);
delay_ms(T1);
// transmit end of the bit (PWM on)
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true);
// int bit = (code >> i) & 0x1; // LSB first
int bit = (code >> ((IR_MAX_BITS_VAL - 1) - i)) & 0x1; // MSB first
// UARTprintf("tx: bit %d - %d\n", i, bit);
if (bit)
{
delay_ms(T2);
} else
{
delay_ms(T1);
}
}
// 3. Set the PWM off
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, false);
// debug blink-out
// blink_n(code);
}
// Interrupt handler for the GPIO motion detector signal
void GPIOMotionDetectorIsr(void) {
GPIOIntClear(GPIO_PORTA_BASE, GPIO_INT_PIN_2 | GPIO_INT_PIN_5);
#ifdef RUN_AS_MASTER
motionDetectorTrigged = 1;
#else
IRIntHandler();
#endif
}
// Interrupt handler for the GPIO light switch signal
void GPIOLightSwitchIsr(void) {
GPIOIntClear(GPIO_PORTE_BASE, GPIO_INT_PIN_4);
lightSwitchTrigged = 1;
}
// Interrupt handler for the idle movement timer
void IdleTimerIsr(void) {
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
idleTimerTrigged = 1;
}
// Interrupt handler IR detection timeout
void IRTimerIsr(void) {
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
GPIOIntTypeSet(IR_PORT, IR_PIN, GPIO_FALLING_EDGE);
receivedIRCode = decodePulseBuffer(pulse_buf);
ir_pulse_count = 0;
//timerTrigged = 1;
}
int decodePulseBuffer(unsigned int *pulse_buf) {
unsigned int i, bitThreshold;
unsigned int code;
// First pulse is a start bit representing 4 time periods(approx. 2.4ms??)
// calculate 2.5 time periods as the threshold between 2t and 3t
// any pulse width greater that 2.5t will be considered a logical '1'
bitThreshold = (pulse_buf[0] * 5) / 8;
if (bitThreshold < T1 * 1000) {
// some disturbance
return 0;
}
UARTprintf("bit threshold value: %d", bitThreshold);
code = 0;
i = 0;
for (i = 0; i < IR_MAX_BITS_VAL; i++) { //Start with pulse/bit 12 (MSB)
code <<= 1;
if (pulse_buf[i + 1] > bitThreshold) { // If a '1' is detected..
code |= 1;
}
UARTprintf("[%d] - Len: %d - Code: %d\n", i, pulse_buf[i], code);
}
UARTprintf("Received code: %d\n", code);
return (int) code;
}
void IRIntHandler(void) {
uint32_t ulTimerVal;
ulTimerVal = TimerValueGet(IR_TIMER_BASE, IR_TIMER); //Read timer value
// Reset the timer
TimerLoadSet(IR_TIMER_BASE, IR_TIMER, g_ulIRPeriod);
ir_timeout_flag = 0;
if (ir_pulse_count == 0) {
// Change the IO pin to trig on RISING, because after this we are
// counting IR detector pulses, starting with RISING edge
GPIOIntTypeSet(IR_PORT, IR_PIN, GPIO_RISING_EDGE);
// Start the timer
TimerEnable(IR_TIMER_BASE, IR_TIMER);
} else {
TimerEnable(IR_TIMER_BASE, IR_TIMER);
if (ir_pulse_count < MAX_PULSE_COUNT)
pulse_buf[ir_pulse_count - 1] = (int) (g_ulIRPeriod - ulTimerVal)
/ g_ulCountsPerMicrosecond;
}
ir_pulse_count++;
}
void delay_ms(uint32_t ui32Ms) {
// 1 clock cycle = 1 / SysCtlClockGet() second
// 1 SysCtlDelay = 3 clock cycle = 3 / SysCtlClockGet() second
// 1 second = SysCtlClockGet() / 3
// 0.001 second = 1 ms = SysCtlClockGet() / 3 / 1000
ROM_SysCtlDelay(ui32Ms * (ROM_SysCtlClockGet() / 3 / 1000));
}
void delay_us(uint32_t ui32Us) {
ROM_SysCtlDelay(ui32Us * (ROM_SysCtlClockGet() / 3 / 1000000));
}