teemuatlut / TMCStepper

MIT License
501 stars 196 forks source link

add callback function to TMC2208/TMC2209 #291

Open vector76 opened 10 months ago

vector76 commented 10 months ago

I had a problem using TMCStepper from Marlin where Marlin controls a multiplexer to route the serial line to the appropriate stepper. This is similar in spirit to the SSwitch multiplexing that is used by TMC2208Stepper, except that the pins are not Arduino pins, but are extended pins available only to Marlin. (Specifically, it uses the I2S output of the ESP32 and shift register to generate several more output pins.)

To achieve the appropriate multiplexer switching in preReadCommunication and preWriteCommunication, I have added a callback function. Ordinarily the callback function is null and has no effect, but if a callback function and callback parameters are passed to the constructor, then it invokes the function during preReadCommunication and preWriteCommunication.

The parameters cb_vala and cb_valb are opaque to TMCStepper, but the callback function unpacks the values to know which pins and what values need to be assigned to the multiplexer pins.


What are your thoughts on whether this can be merged into the TMCStepper library? Eventually I am hoping the Marlin code that uses the callback can also be merged into the Marlin code, but for that to work, this would be a prerequisite.