How would I pass a variable to LEDfunc on that function call? Let's say I wanted to pass int intensity to control LED output intensity. It seems that there may be several ways of doing this. I think a #difine macro might be the best bet, but I've been unable to make it work.
I think the way the code was posted is fine.
I had no trouble looking at it.
People who have issues displaying attachments don't have their browsers setup correctly.
Anyway, I don't understand what you are asking.
Are you saying you want to pass an int (integer) called intensity to control output intensity?
Are you wanting to have/pass in a function pointer? Or are you just wanting to pass in some data?
I don't understand what you really wanting to do.
While you can pass around function pointers, c++ has added capabilities over C that can often be used instead.
There are capabilities like constructors that can be used to initialize the members of an object.
This could be used to initialize the function pointer.
But if you need to change it then you would also need a member where you could pass in the function.
However if you were wanting to have a common method name and have it replaced by specific h/w specific methods, then you may want to look at using c++ virtual functions with separate classes using inheritance.
Then you could declare your object using the appropriate class for the h/w you are using.
Can describe a bit more in general terms what you are wanting to do?
And describe it without presuming a solution like using function pointers.
#include "stripLED.h"
void LED::byIndex(int i) {
switch(i) {
case 0 : red(); break;
case 1 : green(); break;
case 2 : blue(); break;
case 3 : white(); break;
default : LEDclear(); break;
}
}
void LED::red() {
Serial.println(0); // output on red pin at 0 - 255 power
}
void LED::green() {
Serial.println(1); // output on red pin at 0 - 255 power
}
void LED::blue() {
Serial.println(2); // output on red pin at 0 - 255 power
}
void LED::white() {
Serial.println(3); // output on red pin at 0 - 255 power
}
void LED::LEDclear() {
Serial.println(4); // output on red pin at 0 - 255 power
}
It compiles, not positive it works.
Unless you REALLY want to use function pointers. By Why bother with the hassle?
additionally I don't understand why you are using 4 methods + array of pointers for colors instead of one simple method and handover the color.
Can you describe your use case in other words: what do you want to achieve, How should your interface / parameters should look like - not how you think you would solve it?
*) ps: think about if you ever need a signed intensity ... is an int (int16_t) really your best choice?
// 1.0 - working
#include "stripLED.h"
#include "Arduino.h"
LED strip1;
void setup ()
{
Serial.begin (9600);
Serial.println ();
}
void loop ()
{
for (int i = 0; i < 5; i++)
{
LED::LEDfunction LEDfunc = strip1.LED::LEDarray [i];
(strip1.*LEDfunc) (255);
delay(500);
}
}
stripLED.h:
#ifndef STRIPLED_H
#define STRIPLED_H
#include <Arduino.h>
class LED
{
public:
void red(int brightness);
void green(int brightness);
void blue(int brightness);
void white(int brightness);
void LEDclear(int brightness);
// typedef for class function
typedef void (LED::*LEDfunction) (int brightness);
// array of function pointers
LEDfunction LEDarray [5] =
{
&LED::red,
&LED::green,
&LED::blue,
&LED::white,
&LED::LEDclear,
};
};
#endif
stripLED.cpp:
#include "stripLED.h"
#include <Arduino.h>
void LED::red(int brightness)
{
Serial.println(0); // output on red pin at 0 - 255 power
}
void LED::green(int brightness)
{
Serial.println(1); // output on red pin at 0 - 255 power
}
void LED::blue(int brightness)
{
Serial.println(2); // output on red pin at 0 - 255 power
}
void LED::white(int brightness)
{
Serial.println(3); // output on red pin at 0 - 255 power
}
void LED::LEDclear(int brightness)
{
Serial.println(4); // output on red pin at 0 - 255 power
}
I'm simply trying to pass an integer to LEDfunc such as:
int intensity = 200;
(strip1.*LEDfunc) (intensity);
This, obviously, doesn't work. I've tried casting and using a reference to it via pointer, but have not been successful. Anyway, I do like jimLee's idea of putting the switch statement into the .cpp file. I may try to implement this rather than the pointer to function array. My original code has switch statements in the .ino file and it was getting really long and unreadable. So this would clean it up significantly.
Yes! Thank you for this. Not sure why I hadn't thought of this. The problem with my original code was that the .ino had multiple switch statements (I only posted a short example of what I'm trying to do) and the code was getting really long and difficult to read.
Your idea would significantly simplify my code and I would rather not use pointers to functions anyway.
Thank you for the reply and your points are well taken.
I only posted a small example of what I'm trying to accomplish. My original code was much, much longer and was written in haste, becoming almost unreadable. I decided to re-write the code and clean up the .ino to make it more manageable.
I'm going to try jimLee's suggestion as it seems pretty obvious.
Enlighten me please on how I configure Safari on my iPhone to read directly such attachments.
No clue. I haven't used anything Apple since the early 80's.
But surely there is a way to do it.
Super Short codes such as those are better posted directly in line with code tags in my opinion as done by JimLee.
I tend to agree that inline is good for very short code segments.
However, I think the forum s/w should handle the inlining of attached code vs beating on users to do it.
(Computers should work for us, not create extra work for us)
Just like the recent change the Arduino forum did for inlining attached images, which is the way most other forums handle it, they should also fix the forum s/w to inline code if it is attached and under some number of line threshold - just like many other forums do.
That way users could have one single simple consistent way to include extra information regardless of whether it is an image or code.
i.e. they simply attach their images or code and the forum inlines it if it meets the criteria for inlining.
Users could see the inlining in their browser, and could pull up attached code into their favorite view/editing tool.
There is no direct way - a .cpp or a .h or a .ino are not known web extension to display straight in the browser.
It involves downloading the file in a separate area (the File application) and then finding a different application to try to open the file (which I don’t care to look for) as If you started typing an answer and need to go back and forth the two apps you are at risk of losing your answer...
So long story short unless OP modifies the suffix to be .txt so that the browser knows how to display the content of the file - it’s a drag and many participants will just skip and not contribute when there was a straightforward way to just post inline with code tags... it's definitely not as simple as
People who have issues displaying attachments don't have their browsers setup correctly
+1 on your idea for automatically inlining the code if it's short enough... (and support for color coding in code would be good too to highlight where there are issues)
J-M-L:
+1 on your idea for automatically inlining the code if it's short enough... (and support for color coding in code would be good too to highlight where there are issues)
I use other forums where this is done. (not the syntax highlighting, but automatic inlining for short code attachments)
cems1:
I'm simply trying to pass an integer to LEDfunc such as:
int intensity = 200;
(strip1.*LEDfunc) (intensity);
That really isn't what you are trying to do, but rather that is how you are attempting to implement whatever you are really wanting/trying to do.
Which is the point of my question in post #3 and same for what noiasca asked in post #6
Actually, johnwasser did solve the exact question I asked, so yes, that IS what I was trying to do. jimLee's solution is simpler and more to the point, so I am going with that, for the moment.
cems1:
Actually, johnwasser did solve the exact question I asked, so yes, that IS what I was trying to do.
While John, may have helped you solve the specific question you asked,
others were taking a broader view to try to get you to a solution that solved your actual problem.
i.e. . you were focused on the details of a very specific implementation of code that you had decided on to try to solve your functional needs, but you weren't and still haven't described the actual problem you are trying to solve.
Consider your next statement:
jimLee's solution is simpler and more to the point, so I am going with that, for the moment.
This points to using an alternate solution, which means that the actual problem being solved is not what the actual question was.
Would you rather I posted the nearly one thousand lines of code from my original program and then posed the question?
If john was the only person to respond, I would have been completely happy with that, as he did solve the exact question I asked.
But guess what...even without "taking a broader view", the specific question was answered, AND, I have an even more elegant solution, regardless.
So thanks for your input, this will be my last post on this subject and I'll let you have the last word (as I'm sure you'll take the liberty to do so anyway).
Thanks again to john and jim who ACTUALLY helped me out.
I thought this was generally a technical based forum? I've never had a twitter account, but if I did, I would imagine that this would be the same s*** that I would encounter.