Skip to content

Commit

Permalink
KH270: avoid wrapping pixel modulo 256
Browse files Browse the repository at this point in the history
Using modulo 256 arithmetic caused the computed solenoid number to
jump when crossing the left edge of the pattern.
This caused corruption on the leftmost needles of the bed.
  • Loading branch information
jonathanperret committed Jan 19, 2025
1 parent 1995b8c commit 13d5000
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/ayab/knitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,22 @@ bool Knitter::calculatePixelAndSolenoid() {
return false;
}

m_pixelToSet = m_position - startOffset;
// Unsigned 8-bit arithmetic computes modulo 256 — this is not
// appropriate when you have 12 solenoids, it causes pixel -1 to
// have more than 1 solenoid of difference from pixel 0.
// So instead we use a 16-bit int to compute the pixel, then
// convert it back to an unsigned 8-bit integer using a multiple
// of the number of solenoids as the modulus.
// We only handle the underflow case, because the machine with 12
// solenoids (KH270) has only 112 needles and can therefore never
// have positions that cause an 8-bit overflow.
int pixelToSet = (int)m_position - startOffset;

if (pixelToSet < 0) {
pixelToSet += Machine_t::Kh270 == m_machineType ? 252 : 256;
}

m_pixelToSet = pixelToSet;

if (!beltShift) {
m_solenoidToSet = (m_pixelToSet + bulkyOffset) % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
Expand Down

0 comments on commit 13d5000

Please sign in to comment.