robert@464
|
1 /*
|
robert@464
|
2 * StatusLED.cpp
|
robert@464
|
3 *
|
robert@464
|
4 * Routines for manipulating the status LED
|
robert@464
|
5 *
|
robert@464
|
6 * (c) 2014 Andrew McPherson and Victor Zappi
|
robert@464
|
7 * QMUL, Centre for Digital Music
|
robert@464
|
8 */
|
robert@464
|
9
|
robert@464
|
10 #include <iostream>
|
robert@464
|
11 #include "StatusLED.h"
|
robert@464
|
12 #include <GPIOcontrol.h>
|
robert@464
|
13
|
robert@464
|
14 extern int gShouldStop;
|
robert@464
|
15 extern int gVerbose;
|
robert@464
|
16
|
robert@464
|
17 using namespace std;
|
robert@464
|
18
|
robert@464
|
19 StatusLED::StatusLED() {
|
robert@464
|
20 gpio_number = -1;
|
robert@464
|
21 milliseconds_on = 0;
|
robert@464
|
22 milliseconds_off = 100;
|
robert@464
|
23 blink_thread = -1;
|
robert@464
|
24 }
|
robert@464
|
25
|
robert@464
|
26 StatusLED::~StatusLED() {
|
robert@464
|
27 if(gpio_number >= 0) {
|
robert@464
|
28 this_should_stop = true;
|
robert@464
|
29 pthread_join(blink_thread, NULL);
|
robert@464
|
30 gpio_unexport(gpio_number);
|
robert@464
|
31 }
|
robert@464
|
32 }
|
robert@464
|
33
|
robert@464
|
34 bool StatusLED::init(int gpio_pin) {
|
robert@464
|
35 gpio_number = gpio_pin;
|
robert@464
|
36 this_should_stop = false;
|
robert@464
|
37
|
robert@464
|
38 if(gpio_export(gpio_number)) {
|
robert@464
|
39 if(gVerbose)
|
robert@464
|
40 cout << "Warning: couldn't export status LED pin\n";
|
robert@464
|
41 }
|
robert@464
|
42 if(gpio_set_dir(gpio_number, OUTPUT_PIN)) {
|
robert@464
|
43 if(gVerbose)
|
robert@464
|
44 cout << "Couldn't set direction on status LED pin\n";
|
robert@464
|
45 return false;
|
robert@464
|
46 }
|
robert@464
|
47 if(gpio_set_value(gpio_number, LOW)) {
|
robert@464
|
48 if(gVerbose)
|
robert@464
|
49 cout << "Couldn't set value on status LED pin\n";
|
robert@464
|
50 return false;
|
robert@464
|
51 }
|
robert@464
|
52
|
robert@464
|
53
|
robert@464
|
54 if ( pthread_create(&blink_thread, NULL, static_blink_loop, this) )
|
robert@464
|
55 {
|
robert@464
|
56 cout << "Error:unable to create status LED thread" << endl;
|
robert@464
|
57 return false;
|
robert@464
|
58 }
|
robert@464
|
59
|
robert@464
|
60 return true;
|
robert@464
|
61 }
|
robert@464
|
62
|
robert@464
|
63 void StatusLED::on() {
|
robert@464
|
64 milliseconds_on = 100;
|
robert@464
|
65 milliseconds_off = 0;
|
robert@464
|
66 }
|
robert@464
|
67
|
robert@464
|
68 void StatusLED::off() {
|
robert@464
|
69 milliseconds_on = 0;
|
robert@464
|
70 milliseconds_off = 100;
|
robert@464
|
71 }
|
robert@464
|
72
|
robert@464
|
73 void StatusLED::blink(int ms_on, int ms_off) {
|
robert@464
|
74 milliseconds_on = ms_on;
|
robert@464
|
75 milliseconds_off = ms_off;
|
robert@464
|
76 }
|
robert@464
|
77
|
robert@464
|
78 void* StatusLED::blink_loop(void *) {
|
robert@464
|
79 while(!gShouldStop && !this_should_stop) {
|
robert@464
|
80 if(milliseconds_on != 0)
|
robert@464
|
81 gpio_set_value(gpio_number, HIGH);
|
robert@464
|
82 usleep(1000 * milliseconds_on);
|
robert@464
|
83 if(gShouldStop)
|
robert@464
|
84 break;
|
robert@464
|
85 if(milliseconds_off != 0)
|
robert@464
|
86 gpio_set_value(gpio_number, LOW);
|
robert@464
|
87 usleep(1000 * milliseconds_off);
|
robert@464
|
88 }
|
robert@464
|
89 pthread_exit(NULL);
|
robert@464
|
90 }
|