Mr Mayhem wrote:I will probably need to go around a few more times to clarify stuff, but I won't abuse it, and/or I will post some pics of my setup and trial code to benefit the ESP8266 community.
Yes, I cannot promise I'll be able to spend much time on this, but I'm trying to give back the time others have given to me
2. I have downloaded the zip you posted above, and will play with it and report back.
3. To try the new code, I assume I just need to back up the original SPISlave directory files and replace them with the new ones from your zip file? I am using Arduino IDE, I see the location of SPISlave directory is:
C:\Users\Me\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\SPISlave\
I worked on my sketch before Me-no-dev made it a library, so in my case those files sit next to my .ino sketch, but I guess replacing the library ones (after backing them up of course) should work indeed.
4. Newbie question: In SPISlave_Test.ino, (the sketch), how do I move the event handlers out of setup ()?
Mmmh, the event handler is *not* in setup(). It is only *registered by* setup(), and that is the right way to do it.
I noticed the event handlers in setup() end with });
presumably to keep them from running when setup() runs, yet be available.
No, you got it wrong
You were mislead by the fact the sample uses "anonymous functions".
Let's start simple :
In my case, the code to be called is defined as follows:
void ICACHE_RAM_ATTR handleSpiInterrupt(uint32_t data) {
...
}
This is a normal function (except that it has the extra ICACHE_RAM_ATTR directive to make sure it is kept in RAM, but that's another matter).
In my setup(), I'm using the following to "register" the above function :
SPISlave.onTrans(&handleSpiInterrupt);
Which only means "if a trans interrupt happens in the future, then you will execute the handleSpiInterrupt() function.". Nothing else is executed at that time.
Did you notice the ampersand sign (&) before the function name? That is because onTrans() expects a pointer to a function.
Now what you see in the example is a shortcut that you can use if the implementation is only used once.
So instead of writing:
void ICACHE_RAM_ATTR handleStatusSent() {
Serial.println("Status Sent");
}
void setup() {
...
SPISlave.onStatusSent(&handleStatusSent());
...
}
You can replace the function by inserting its implementation instead as follows:
void setup() {
...
SPISlave.onStatusSent([]() {
Serial.println("Status Sent");
});
...
}
The syntax []() {Serial.println("Status Sent");} is surprising but you can read the square brackets "[]" as "anonymous function", then you have the parentheses "()" for the parameters as usual (none in this case) and then the implementation between curly brackets "{Serial.println("Status Sent");}" as usual too.
And that whole expression []() {Serial.println("Status Sent");} can be passed as a parameter to "SPISlave.onStatusSent()", resulting in something like:
SPISlave.onStatusSent([]() {Serial.println("Status Sent");});
I was getting compile errors earlier when I tried to move them. I don't know how to flatten the }); magic, heh.
Does it make sense now ?
No magic, simply the end of the implementation of the function passed as a parameter
5. Could you provide me with a simple example of copying 32 bytes from myBytes[512] array into SPISlave.setData() or similiar? I will make an honest effort to experiment here, but I am more than a little unsure still on the syntax to use, given that pointers are new to me.
Well, a simple implementation would be to add the following to SPISlave_Master.ino:
void sendBytes(const uint8_t * data, int offset, int length) {
Serial.print("Master sending ");
Serial.print(length);
Serial.print(" bytes");
char myData[32];
memcpy(myData, data + offset, length);
esp.writeData(myData, length);
}
And call it using sendBytes(myBytes, 0, 32);
Hope it helps...
Vicne