From 3c64c9b0e9da1b81d6fcf93bceaae305a2709b65 Mon Sep 17 00:00:00 2001 From: Fractal147 <35138372+Fractal147@users.noreply.github.com> Date: Fri, 8 Jan 2021 03:17:41 +0000 Subject: [PATCH] Fix stepper half half step mode (#1397) * Fixed half half step mode Half step mode originally had second and third steps mixed up for output A. https://github.com/esphome/issues/issues/1655 * Updated half step mode to have no branching Code based off comments in PR: https://github.com/esphome/esphome/pull/1397#issuecomment-739413973 * removed variable declarations in the switch/case Oops. No explicit declarations. Rearranged to be tidier, same output sequence. * Fixed typo bracket Minor typo fixed - logic complete to do branchless half stepping. * Format the brackets to satisfy clang --- esphome/components/uln2003/uln2003.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/esphome/components/uln2003/uln2003.cpp b/esphome/components/uln2003/uln2003.cpp index b1a397ad6c..38eadc9dc8 100644 --- a/esphome/components/uln2003/uln2003.cpp +++ b/esphome/components/uln2003/uln2003.cpp @@ -70,14 +70,8 @@ void ULN2003::write_step_(int32_t step) { } case ULN2003_STEP_MODE_HALF_STEP: { // A, AB, B, BC, C, CD, D, DA - if (i == 0 || i == 2 || i == 7) - res |= 1 << 0; - if (i == 1 || i == 2 || i == 3) - res |= 1 << 1; - if (i == 3 || i == 4 || i == 5) - res |= 1 << 2; - if (i == 5 || i == 6 || i == 7) - res |= 1 << 3; + res |= 1 << (i >> 1); + res |= 1 << (((i + 1) >> 1) & 0x3); break; } case ULN2003_STEP_MODE_WAVE_DRIVE: {