As I believe I mentioned you will see code that uses: 0 or NULL, or nullptr.
I believe at times, just passing 0 may have created warning messages about conversions...
So for example if the first argument passed in the SPI.transfer was defined as uint8_t * instead of void *
you might at times see code where the person is very specific and passes in: (uint8_t*)0
Or you might see them pass in (void*)0 as this is a pointer to more or less anything... But you can not really use it, until you cast it to some other pointer to something...
Sorry I am not C/C++ language instructor, so my explanations may not very concise or complete.
And I hope I properly mentioned in previous post, that the &SPI_DMA_ready was for a earlier version of this code that was never released.
In c++ there are at least three different ways to pass things to a functions: The data, A pointer to the data, or by reference:
a) By value: example in the transfer function: the count of items to transfer is by value. The value you passed in to the call is put into a local variable, The function called can use this value and the like, and the function that called it has no idea if the called function modified this value or not. Sorry I am skipping over things like if I pass in an object, it creates a copy of that object...
b) by Pointer: example your txBuffer and more appropriately return buffer which you are passing 0 to... What is actually passed to the function is the address of the place where your data is... The called function can use and modify this data, and the calling code will see these modifications (example data returned by SPI)... So when passing by pointer, the use of the & says get the address of what follows.
So &SPI_DMA-ready says pass in the address of this function:
In practice the original code may have be defined like:
Code:
void SPI_DMA_ready() {
}
...
SPI.transfer(txbuffer, 0, pixCount, &SPI_DMA_ready);
...
int transfer(void *buffer, void *rxbuffer, int count, void (*callback)()) {
....
if (callback) (*callback)();
}
Perfectly clear
Often times people maybe don't check callback to be a valid pointer, example you pass 1, and you deference it and program faults.
c) more recently by reference, which sort of tries to make passing data easier and helps avoid the faults...
You specify in a function header that you are passing a parameter by reference by the &...
What does that mean, for one there is no pointer so you don't have the concept of not passing in a parameter. And the compiler can type check what you pass in is valid..
Note passing in by reference is not restricted to objects or ... But also to simple normal types like integer.
Example difference:
Code:
void my_function(int val) {
val++;
}
void loop() {
int local_val = 0;
my_function(local_val );
Serial.println(local_val );
}
Will print 0's
But:
Code:
void my_function(int &val) {
val++;
}
void loop() {
int local_val = 0;
my_function(local_val );
Serial.println(local_val );
}
Will print 1's as the incremented val in the function updates local_val...
Which is more or less like using pointers:
Code:
void my_function(int *val) {
*val++;
}
void loop() {
int local_val = 0;
my_function(&local_val );
Serial.println(local_val );
}
Again sorry, probably clear as mud...