Charith Dassanayake / CPPToPigpio
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CPPToPigpio.cc Source File

CPPToPigpio.cc

00001 #ifndef _CPP_TO_PIGPIO_MASTER
00002 #define _CPP_TO_PIGPIO_MASTER
00003 #include <stdlib.h>
00004 #include <pigpio.h> // include GPIO library
00005 #include <signal.h> // needed to clean up CTL C abort
00006 #include <sys/types.h>
00007 #include <unistd.h>
00008 
00009 namespace CPPToPigpio{
00010 
00011     uint32_t CPPToPigpio::_number_active_devices = 0; //Initialize the static var
00012 
00013     void CPPToPigpio::errHandler (int) //unused param
00014     {
00015         gpioTerminate(); //release GPIO locks & resources
00016         signal(SIGINT, SIG_DFL); //exit program
00017         kill(getppid(), SIGINT); //kill it off
00018         kill(getpid(), SIGINT);
00019         exit(0);
00020     }
00021     
00022     void  CPPToPigpio::exitHandler() 
00023     {
00024         gpioTerminate(); //release GPIO locks & resources on exit
00025     }
00026 
00027     void CPPToPigpio::_setupErrorHandlers()
00028     {
00029         signal (SIGQUIT, CPPToPigpio::errHandler);// CTL C and STOP button
00030         signal (SIGINT,  CPPToPigpio::errHandler); // GPIO exit & cleanup
00031         signal (SIGTERM, CPPToPigpio::errHandler);
00032         signal (SIGABRT, CPPToPigpio::errHandler);
00033         atexit(CPPToPigpio::exitHandler);  // exit handler cleanup 
00034     }
00035 
00036     void CPPToPigpio::_handlePigpioInitialisationIfNeeded()
00037     {
00038         if(_number_active_devices == 0){
00039             gpioInitialise();
00040             CPPToPigpio::_setupErrorHandlers();
00041         }
00042         ++_number_active_devices;
00043     }
00044 
00045     void CPPToPigpio::_handlePigpioTermination()
00046     {
00047         --_number_active_devices;
00048         if(_number_active_devices == 0)
00049             gpioTerminate();
00050 
00051     }
00052 
00053 }
00054 #endif