So I have a question that might help others as well. I have my master (ESP8266) which essentially receives liner actuator movement request via WiFi and then must tell the Teensy 3.2 (basically in the role of motor controller) what direction and how many steps
to take in that direction. I understand the master - > slave relationship. Basically I can set up the Teensy as shown below and receive data from the ESP8266...
Code:
Wire.begin(I2C_SLAVE, TEENSY_I2C_ADDRESS, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
Wire.onRequest(receiveEvent);
.
.
void receiveEvent(size_t len)
{
dbLen = len;
Wire.read(databuf, len);
// do something with the data from the 8266...
}
Now I know I can also set up a receiveEvent callback in the slave where it will send data back to the master when the master requests this. But in this case, the time the Teensy (slave) spends moving the
actuator is unknown and varies considerable from call to call. So one time it might spend 20 millis on a movement while other time it may spend 15 seconds. And that is the point of my question - can the slave
indiscriminately send data back to the master without the master requesting this? In this scenario the motor controller might meet the upper or lower bounds of the actuator and move only enough to trip the
limit switch. To ensure the user program does not get out of sync, the Teensy motor controller (slave) need to send how many steps was actually taken back to the ESP8266 (which might be less than the number requested
if it hits the limit switches). As you could see, the master would have no way of knowing when the slave was done and therefore would have to poll the slave until the operation was done - sounds inefficient to
me.
I was hoping I could setup something like that shown below in the slave and have then send it to the master when the motor movement is finished...
Code:
void DoMotorControlReply()
{
char szDebug[100] = { 0 };
bProcessReplyCommand = false;
memset(databuf, 0, MEM_LEN);
#ifdef DEBUG
sprintf(szDebug, "The return code is %u and the steps are %u", mc.uiCode, mc.uiSteps);
#endif // DEBUG
_DebugPrintln(szDebug);
sprintf(databuf, "%s#%s|", mc.uiCode, mc.uiStepsTaken);
Wire.write(databuf, MEM_LEN); // fill Tx buffer (send full mem)
}
So is this possible - or do I need to poll from the master if the operation has completed? Am I overthinking this one - i.e. when the master requests data from the slave, can there be a large delay before the slave sends data?