enumclass StepperIterator { X1, X2, Y };
struct StepperHandle
{
StepperPins pins;
float actual_position_mm;
uint16_t actual_position_steps;
float target_position_mm;
uint16_t target_position_steps;
float center_position_mm;
bool is_busy;
Direction direction;
ControlState control_state;
};
class Stepper
{
public:
void foo()
{
for (auto stepper : stepper_iterator)
{
reset_actual_position(stepper_handle(stepper));
}
}
void reset_actual_position(StepperHandle& stepper)
{
stepper.actual_position_mm = 0;
}
private:
StepperHandle& stepper_handle(StepperIterator stepper_instance)
{
switch(stepper_instance)
{
case StepperIterator::X1:
{
return x1;
}
case StepperIterator::X2:
{
return x2;
}
case StepperIterator::Y:
{
return y;
}
default:
{
while (true) {}
}
}
}
StepperHandle x {};
StepperHandle y {};
StepperHandle z {};
};
Now the problem is that my StepperHandle instances x,y,z are not handled as this code would describe it. The different struct members are getting reseted when calling the instance in other functions, but always forwarding the instance by stepper_handle(). The advantage of this function is that my Code gets so much shorter instead of calling x,y,z seperately in every function.
I would appreciate if you can see where my instance handling leaks..