annotate examples/d-box/StatusLED.cpp @ 340:69d86429a1cf prerelease

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