diff --git a/test/mocks/knitting_machine.cpp b/test/mocks/knitting_machine.cpp index 66334ecc..04131356 100644 --- a/test/mocks/knitting_machine.cpp +++ b/test/mocks/knitting_machine.cpp @@ -114,6 +114,8 @@ void KnittingMachine::moveCarriageRight() { moveBeltRight(); } ++m_carriagePosition; + + updateNeedles(); } void KnittingMachine::moveCarriageLeft() { @@ -121,6 +123,8 @@ void KnittingMachine::moveCarriageLeft() { moveBeltLeft(); } --m_carriagePosition; + + updateNeedles(); } int KnittingMachine::beltPeriod() const { @@ -138,4 +142,40 @@ bool KnittingMachine::moveCarriageCenterTowardsNeedle(int position) { moveCarriageLeft(); } return true; +} + +void KnittingMachine::setNeedleCount(int count) { + m_needles.clear(); + for (int n = 0; n < count; n++) { + m_needles.push_back(Needle{.index = n, .position = NeedlePosition::A}); + } +} + +void KnittingMachine::setNeedlePosition(int needle, NeedlePosition position) { + m_needles[needle].position = position; +} + +KnittingMachine::NeedlePosition KnittingMachine::getNeedlePosition(int needle) { + return m_needles[needle].position; +} + +void KnittingMachine::setSolenoid(int solenoid, bool state) { + (void)state; + (void)solenoid; +} + +void KnittingMachine::updateNeedles() { + for (auto& needle : m_needles) { + needle.update(); + } +} + +void KnittingMachine::Needle::update() { + switch (position) { + case B: + position = D; + break; + default: + break; + } } \ No newline at end of file diff --git a/test/mocks/knitting_machine.h b/test/mocks/knitting_machine.h index f65db41b..0f5f0b5f 100644 --- a/test/mocks/knitting_machine.h +++ b/test/mocks/knitting_machine.h @@ -135,6 +135,45 @@ class KnittingMachine { */ bool moveCarriageCenterTowardsNeedle(int position); + /** + * Initialize the needle bed with the given needle count + * + * \param count Count of (programmable) needles + */ + void setNeedleCount(int count); + + /** + * Needle positions + * + * Position "C" is not indicated on the machine nor referenced + * in the manual but is useful to represent an intermediate + * position of the needle during the selection process. + */ + enum NeedlePosition { A = 0, B, C, D, E }; + + /** + * Set a needle's position + * + * \param needle the needle index (e.g. 0 to 199 on a KH-9xx) + * \param position the needle position (A, B…) + */ + void setNeedlePosition(int needle, NeedlePosition position); + + /** + * Get a needle's position + * + * \param needle the needle index (e.g. 0 to 199 on a KH-9xx) + * \returns the needle's position (A, B…) + */ + NeedlePosition getNeedlePosition(int needle); + + /** + * Set a solenoid's state + * + * \param state the solenoid's power state (true = powered) + */ + void setSolenoid(int solenoid, bool state); + private: static constexpr int STEPS_PER_NEEDLE = 4; static constexpr float POSITION_SENSOR_LOW_VOLTAGE = 0.2f; @@ -251,6 +290,33 @@ class KnittingMachine { * center and their polarity. \see addCarriageMagnet() */ std::vector> m_carriageMagnets; + + /** + * A single needle + */ + struct Needle { + /** + * Needle index (0 to needle count - 1) + */ + int index; + /** + * Current needle position (A, B…) + */ + NeedlePosition position; + + void update(); + }; + + /** + * Needles on the bed + */ + std::vector m_needles; + + /** + * Update the state of all the needles. + * Called after every carriage movement. + */ + void updateNeedles(); }; #endif // KNITTINGMACHINE_H_ \ No newline at end of file diff --git a/test/test_knitting_machine.cpp b/test/test_knitting_machine.cpp index a4979f25..384c08fa 100644 --- a/test/test_knitting_machine.cpp +++ b/test/test_knitting_machine.cpp @@ -262,4 +262,29 @@ TEST(KnittingMachine, KCarriageScanBed) { km.addCarriageMagnet(0, true); doBedScan(km); +} + +TEST(KnittingMachine, NeedleSelection) { + KnittingMachine km; + + const int testNeedle = 47; + const int testSolenoid = 0; + + km.setNeedleCount(200); + EXPECT_EQ(km.getNeedlePosition(testNeedle), KnittingMachine::A); + + km.setNeedlePosition(testNeedle, KnittingMachine::B); + EXPECT_EQ(km.getNeedlePosition(testNeedle), KnittingMachine::B); + km.setSolenoid(testSolenoid, true); + km.putCarriageCenterInFrontOfNeedle(0); + while (km.moveCarriageCenterTowardsNeedle(100)) { + } + + for (int n = 0; n < 100; n++) { + if (n == testNeedle) { + EXPECT_EQ(km.getNeedlePosition(n), KnittingMachine::D); + } else { + EXPECT_EQ(km.getNeedlePosition(n), KnittingMachine::A); + } + } } \ No newline at end of file