/** * @file * @author Walter Schilling (schilling@msoe.edu) * @version 1.0 * * @section LICENSE This code is distributed to students to demonstrate general control of a light. * * * @section DESCRIPTION * * This program will allow the user to control two lights. The lights will be controlled by pressing and releasing a pushbutton. */ #include #include #include #include "GPIO.h" #include #include using namespace SWE4211RPi; using namespace std; #define SLEEPLENGTH (1) static bool keepGoing = true; /** * This method will be a thread. It takes four parameters in. * @param output This is the output GPIO controlled by this thread * @param input This is the input pin that is to be controlled by this thread. * @param priority This is the execution priority of this thread. * @rate This is how often the thread is to execute. */ void downButtonThread(GPIO* output, GPIO* input, int priority, int rate) { // Setup the operating thread to be a real time thread. struct sched_param p; p.__sched_priority = priority; printf("%d\n", priority); if (sched_setscheduler(0, SCHED_FIFO, &p) != 0) { printf("Failed to set the scheduler\n"); exit(-1); } // Loop over the data, turning things on and off as is necessary. while (keepGoing) { // Read pushbutton and set output accordingly. if (input->getValue() == GPIO::GPIO_LOW) { // The button is pressed. Turn the light on. output->setValue(GPIO::GPIO_LOW); } else { output->setValue(GPIO::GPIO_HIGH); } usleep(rate); } } /** * This program will control the LED. It essentially will turn the LED on if the button is pressed and off if the button is released. */ int main(int argc, char* argv[]) { // Check to determine if the command line usage is correct or not. if (argc != 5) { cerr << "Usage: " << argv[0] << " "; exit(-1); } // Determine the period and the blink count. int GPIOPout0 = atoi(argv[1]); int GPIOPout1 = atoi(argv[2]); int GPIOPin0 = atoi(argv[3]); int GPIOPin1 = atoi(argv[4]); // Instantiate a new instance of a GPIO port. GPIO outGPIO0(GPIOPout0, GPIO::GPIO_OUT); GPIO outGPIO1(GPIOPout1, GPIO::GPIO_OUT); GPIO inGPIO0(GPIOPin0, GPIO::GPIO_IN); GPIO inGPIO1(GPIOPin1, GPIO::GPIO_IN); // Start up the new threads running. std::thread t1(downButtonThread, &outGPIO0, &inGPIO0, sched_get_priority_max(SCHED_FIFO), SLEEPLENGTH); std::thread t2(downButtonThread, &outGPIO1, &inGPIO1, sched_get_priority_max(SCHED_FIFO), SLEEPLENGTH); string msg; cin >> msg; cout << msg; keepGoing = false; t1.join(); t2.join(); }