robert@464: /* robert@464: * StatusLED.cpp robert@464: * robert@464: * Routines for manipulating the status LED robert@464: * robert@464: * (c) 2014 Andrew McPherson and Victor Zappi robert@464: * QMUL, Centre for Digital Music robert@464: */ robert@464: robert@464: #include robert@464: #include "StatusLED.h" robert@464: #include robert@464: robert@464: extern int gShouldStop; robert@464: extern int gVerbose; robert@464: robert@464: using namespace std; robert@464: robert@464: StatusLED::StatusLED() { robert@464: gpio_number = -1; robert@464: milliseconds_on = 0; robert@464: milliseconds_off = 100; robert@464: blink_thread = -1; robert@464: } robert@464: robert@464: StatusLED::~StatusLED() { robert@464: if(gpio_number >= 0) { robert@464: this_should_stop = true; robert@464: pthread_join(blink_thread, NULL); robert@464: gpio_unexport(gpio_number); robert@464: } robert@464: } robert@464: robert@464: bool StatusLED::init(int gpio_pin) { robert@464: gpio_number = gpio_pin; robert@464: this_should_stop = false; robert@464: robert@464: if(gpio_export(gpio_number)) { robert@464: if(gVerbose) robert@464: cout << "Warning: couldn't export status LED pin\n"; robert@464: } robert@464: if(gpio_set_dir(gpio_number, OUTPUT_PIN)) { robert@464: if(gVerbose) robert@464: cout << "Couldn't set direction on status LED pin\n"; robert@464: return false; robert@464: } robert@464: if(gpio_set_value(gpio_number, LOW)) { robert@464: if(gVerbose) robert@464: cout << "Couldn't set value on status LED pin\n"; robert@464: return false; robert@464: } robert@464: robert@464: robert@464: if ( pthread_create(&blink_thread, NULL, static_blink_loop, this) ) robert@464: { robert@464: cout << "Error:unable to create status LED thread" << endl; robert@464: return false; robert@464: } robert@464: robert@464: return true; robert@464: } robert@464: robert@464: void StatusLED::on() { robert@464: milliseconds_on = 100; robert@464: milliseconds_off = 0; robert@464: } robert@464: robert@464: void StatusLED::off() { robert@464: milliseconds_on = 0; robert@464: milliseconds_off = 100; robert@464: } robert@464: robert@464: void StatusLED::blink(int ms_on, int ms_off) { robert@464: milliseconds_on = ms_on; robert@464: milliseconds_off = ms_off; robert@464: } robert@464: robert@464: void* StatusLED::blink_loop(void *) { robert@464: while(!gShouldStop && !this_should_stop) { robert@464: if(milliseconds_on != 0) robert@464: gpio_set_value(gpio_number, HIGH); robert@464: usleep(1000 * milliseconds_on); robert@464: if(gShouldStop) robert@464: break; robert@464: if(milliseconds_off != 0) robert@464: gpio_set_value(gpio_number, LOW); robert@464: usleep(1000 * milliseconds_off); robert@464: } robert@464: pthread_exit(NULL); robert@464: }