I have successfully created a library (class) for a current project but I need instances of the library to reference a global array. Not sure how to do this. I am trying using a pointer and passing that in to each instance I create but so far that has not worked.
Can someone point (!) me to an example of a library accessing global vars or tell me a better way to do this. I am defining my pointer like this:
int* sensorStatesPtr = sensorStates;
and then in loop(), just for a test I am setting a value in the array and getting the value back from my instance.
sensorStates = 33;
int i = r.pointerTest();
but the returned value doesn't reflect the change.
I suppose, worst case, I can try defining my class within the sketch- that seemed to work on another project
If it's a global array, why you don't just reference that array from your library code? If your library doesn't know the global array exists because it hasn't been declared yet, you should be able to put at the top of your library code:
extern int sensorStates;
This tells the compiler not to complain about references to the variable sensorStates and just trust that it'll be there when the time is right. For example, I made a library that implements its own version of millis() by accessing the global unsigned long timer0_overflow_counter that's defined in wiring.c. The way I accessed it was to put at the top of my library's .c file:
extern volatile unsigned long timer0_overflow_count;
Another potential solution: declare the global array as volatile, so that it saves to RAM every time it is modified and loads from RAM every time it is read. This would prevent the compiler from optimizing away RAM writes you’re actually relying on in this case. For example, maybe your line sensorStates = 33; doesn’t trigger a write to RAM before your next line (the pointer test), which could explain why what you’re reading from RAM is the old value.
If it's a global array, why you don't just reference that array from your library code?
Just didn't know how to do it- learning the bits of C/C++ I need as I go. Just tried your solution and it worked! Yay! - saves me from having to learn about pointers and such right now (I get the concept but have been failing on the implementation).
While I am here (though maybe I should break this out into a separate thread)- if you have worked with classes/objects in Wiring before - is there an easy way to create and destroy instances on the fly? In an earlier project using a custom class I couldn't figure it out and ended up creating an array of instances that I could use as needed. Total hack but it worked. Would like to do it in a less hackish way - though since I am storing the instances in an array, I guess I would have to reallocate the array each time I created or destroyed an object.
thanks for your help!
I don't have too much experience doing object-oriented microcontroller code mainly because I (stubbornly) don't trust it. I guess it feels too fancy for an 8-bit microprocessor with almost no RAM and very little flash, and I always find myself wondering how well my object-oriented code is being translated into AVR assembly instructions. At least when I'm working in C I can envision what the compiler is going to do. So I guess what I'm trying to say is: maybe someone else has some insight on dynamic mega168 memory allocation? I'd be very interested to find out if there's any library support for it.
ok - thanks. (at least you can visualize what the compiler is going to do - that end remains a dark tunnel for me so far).
I'm actually using the Wiring board for this - so I have a lot more memory to use poorly... :) It's so great to be able to go back and forth between Arduino and Wiring depending on the project needs.