Update:
#1.) Firstly, thank you for pointing out that the two codes are the same. I apologize for my error and any inconveniences caused. Also, I inadvertently posted an old version of what was supposed to be the second program. Anyway, here is the first program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
// soft_pwm_blink_01.cpp
// d20m12y2022
// Blinking led with PWM signal
// NOTE: This implements a cleanup of wiringPi pin used
// and reset of the same at the end of the program, using
// snippet of code courtesy of M Heidenreich (2021), Rasp-
// berry Pi Beginner Guide, YouTube.
//
#include <iostream>
#include <wiringPi.h>
#include <signal.h>
#include <softPwm.h>
using std::cout;
using std::cin;
using std::endl;
// Initialize global variable:
int blink = 1;
// Function prototype:
void cleanup(int signal);
// Main program
int main(void)
{
signal(SIGINT, cleanup); // To capture keyboard interrupt
wiringPiSetup(); // Call wiringPi library and numbering system
softPwmCreate(15,0,100); // Call function to create PWM pin
while(blink)
{
softPwmWrite(15, 25); // Function to write PWM data to output
// device (in this case, LED)
// (where, duty cycle = 25%)
delay(1000); // sleep for 1000ms (1 second)
softPwmWrite(15, 0);
delay(1000);
softPwmWrite(15, 50);
delay(1000);
softPwmWrite(15, 0);
delay(1000);
softPwmWrite(15, 100);
delay(1000);
softPwmWrite(15, 0);
delay(1000);
}
// Clean up and reset wiringPin (not to be confused with GPIO pin)
digitalWrite(15, LOW); // Ensure LED is turned off
pinMode(15, INPUT); // Reset wiringPi pin to INPUT (that is, IN)
return 0;
}
// Function definition:
void cleanup(int signal)
{
blink = 0;
cout << "\ncleanup starts..." << endl;
cout << "cleanup ends..." << endl;
}
|
The output for the first program is:
$ ./soft_pwm_blink_01
^C
cleanup starts...
cleanup ends...
|
The second program (slightly different to the one in the initial post) is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
// soft_pwm_blink_02.cpp
// d20m12y2022
// Blinking led with PWM signal
// NOTE: This implements a cleanup of wiringPi pin used
// and reset of the same at the end of the program, using
// snippet of code courtesy of M Heidenreich (2021), Rasp-
// berry Pi Beginner Guide, YouTube.
//
#include <iostream>
#include <wiringPi.h>
#include <signal.h>
#include <softPwm.h>
using std::cout;
using std::cin;
using std::endl;
class CleanUpGPIOPin
{
public:
bool getBlink (); // Member access function
void setBlink (int signal); // Member access function
private:
int blinkState; // Member data
};
// Implement class method (See Horton I (2013) p.305):
bool CleanUpGPIOPin::getBlink()
{
return blinkState;
}
void CleanUpGPIOPin::setBlink(int b)
{
blinkState = b; // Set blinkState to 0 that is, false
// Clean up and reset wiringPin
cout << "\n Commence clean up process..." << endl;
digitalWrite(15, LOW); // Ensure LED is turned off
pinMode(15, INPUT); // Reset wiringPi pin to INPUT (that is, IN)
cout << "Clean up process complete!" << endl;
}
void cleanup(int signal)
{
CleanUpGPIOPin Regular;
Regular.setBlink(0);
}
// Main program
int main(void)
{
//cleanup = Regular.setBlink(1);
signal(SIGINT, cleanup); // To capture keyboard interrupt
wiringPiSetup(); // Call wiringPi library and numbering system
softPwmCreate(15,0,100); // Call function to create PWM pin
CleanUpGPIOPin Regular;
//Regular.setBlink(1);
cout << Regular.getBlink() << endl;
//bool blink = Regular.getBlink();
while(!Regular.getBlink())
{
softPwmWrite(15, 25); // Function to write PWM data to output
// device (in this case, LED)
// (where, duty cycle = 25%)
delay(1000); // sleep for 1000ms (1 second)
softPwmWrite(15, 0);
delay(1000);
softPwmWrite(15, 50);
delay(1000);
softPwmWrite(15, 0);
delay(1000);
softPwmWrite(15, 100);
delay(1000);
softPwmWrite(15, 0);
delay(1000);
}
// Clean up and reset wiringPin (not to be confused with GPIO pin)
digitalWrite(15, LOW); // Ensure LED is turned off
pinMode(15, INPUT); // Reset wiringPi pin to INPUT (that is, IN)
return 0;
}
|
The output for the second program is (similar to the previous post):
$ ./soft_pwm_blink_02
0
^C
Commence clean up process...
Clean up process complete!
^C
Commence clean up process...
Clean up process complete!
^C
Commence clean up process...
Clean up process complete!
^C
Commence clean up process...
Clean up process complete!
^C
Commence clean up process...
Clean up process complete!
^C
Commence clean up process...
Clean up process complete!
^Z
[2]+ Stopped ./soft_pwm_blink_02
|
#2.) Secondly, as per the compiler, I also use g++. (Please see details below):
$ g++ --version
g++ (Raspbian 10.2.1-6+rpi1) 10.2.1 20210110
|
Also, the compilation and build commands which I used are:
g++ -Wall -c soft_pwm_blink_02.cpp -l wiringPi
g++ -Wall -o soft_pwm_blink_02 soft_pwm_blink_02.cpp -l wiringPi
|
#3.) In regards to the error, that was my bad. Just as previously mentioned, I was due to a problem with the old version of the code that I initially posted. Hopefully, the following error:
non static member function |
should be resolved. By the way, here is the initial error message I got at the time:
g++ -Wall -c soft_pwm_blink_01.cpp -l wiringPi
soft_pwm_blink_01.cpp: In function ‘int main()’:
soft_pwm_blink_01.cpp:53:28: error: invalid use of non-static member function ‘void CleanUpGPIOPin::setBlink()’
53 | signal(SIGINT, Regular.setBlink); // To capture keyboard interrupt
| ~~~~~~~~^~~~~~~~
soft_pwm_blink_01.cpp:35:6: note: declared here
35 | void CleanUpGPIOPin::setBlink()
|
#4.) Thank you for your comment about considering the usefulness of global variables in so far as one can safely implement them. Your comments are well received. I would also consider enumerated constants as well just as you suggested.
#5.) Please do you understand what
signal()
function is really doing in
main()
especially in the first program? The reason I ask is because if I try to call
cleanup()
by including its parentheses within
signal()
function header, say for instance,
signal(SIGINT, cleanup()); // To capture keyboard interrupt
I think I get a compilation error. Meanwhile up until now, I wasn't aware one could call
cleanup()
without including the parentheses following strict c++ (c-11) standard syntax rules considering that
cleanup()
is also a function itself. Please, any thoughts on this?
Finally, further next comments and contributions are highly welcomed. Thank you so much.