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