If your specific motor driver chip is not supported by existing classes, you can create a custom driver by inheriting from the StepperControl abstract class. The core logic for background movement is already handled by the base class; you only need to implement the hardware-specific pin toggling.
Required Method:
void onStep(boolean direction): Logic to advance the motor by a single step based on the direction parameter.
Optional Methods (implement if applicable to your hardware):
void onEnable(): Logic to enable the motor driver.void onDisable(): Logic to disable the motor driver.void onBrake(): Logic to put the motor into a short brake state.StepperControl *setStepType(int mode): Logic to set the step type mode (e.g., Full Step, Half Step) based on the mode parameter.
Implementation Example (Stepper_A3967):
struct Stepper_A3967 : StepperControl {
// ... constructor and pin definitions ...
void onStep(boolean direction) override {
digitalWrite(dirPin,direction);
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
}
void onEnable() override {
digitalWrite(enablePin,0);
}
void onDisable() override {
digitalWrite(enablePin,1);
}
StepperControl *setStepType(int mode) override {
switch(mode){
case FULL_STEP_TWO_PHASE:
digitalWrite(m1Pin,LOW);
digitalWrite(m2Pin,LOW);
break;
// ... other cases ...
}
return(this);
}
};
#include "HomeSpan.h"
//////////////////////////
struct Stepper_A3967 : StepperControl {
int m1Pin;
int m2Pin;
int stepPin;
int dirPin;
int enablePin;
//////////////////////////
Stepper_A3967(int m1Pin, int m2Pin, int stepPin, int dirPin, int enablePin, std::pair<uint32_t, uint32_t> taskParams = {1,0}) : StepperControl(taskParams.first,taskParams.second){
this->m1Pin=m1Pin;
this->m2Pin=m2Pin;
this->stepPin=stepPin;
this->dirPin=dirPin;
this->enablePin=enablePin;
pinMode(m1Pin,OUTPUT);
pinMode(m2Pin,OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enablePin,OUTPUT);
setStepType(FULL_STEP_TWO_PHASE);
}
//////////////////////////
void onStep(boolean direction) override {
digitalWrite(dirPin,direction);
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
}
//////////////////////////
void onEnable() override {
digitalWrite(enablePin,0);
}
//////////////////////////
void onDisable() override {
digitalWrite(enablePin,1);
}
//////////////////////////
StepperControl *setStepType(int mode) override {
switch(mode){
case FULL_STEP_TWO_PHASE:
digitalWrite(m1Pin,LOW);
digitalWrite(m2Pin,LOW);
break;
case HALF_STEP:
digitalWrite(m1Pin,HIGH);
digitalWrite(m2Pin,LOW);
break;
case QUARTER_STEP:
digitalWrite(m1Pin,LOW);
digitalWrite(m2Pin,HIGH);
break;
case EIGHTH_STEP:
digitalWrite(m1Pin,HIGH);
digitalWrite(m2Pin,HIGH);
break;
default:
ESP_LOGE(STEPPER_TAG,"Unknown StepType=%d",mode);
}
return(this);
}
};