AYAB Arduino Firmware 0.95
The goal of the AYAB project is to provide an alternative way to control the famous Brother KH-9xx range of knitting machines using a computer
solenoids.h
Go to the documentation of this file.
1
24#ifndef SOLENOIDS_H_
25#define SOLENOIDS_H_
26
27#include "board.h"
28#include "encoders.h"
29#include <Arduino.h>
30#include <Adafruit_MCP23008.h>
31#include <Wire.h>
32
33// Different machines have a different number of solenoids.
34// {910, 930, 270}
35constexpr uint8_t SOLENOIDS_NUM[NUM_MACHINES] = {16U, 16U, 12U};
36constexpr uint8_t HALF_SOLENOIDS_NUM[NUM_MACHINES] = {8U, 8U, 6U};
37constexpr uint8_t SOLENOIDS_I2C_ADDRESS_MASK = 0x20U;
38constexpr uint8_t SOLENOID_BUFFER_SIZE = 16U;
39
41public:
42 virtual ~SolenoidsInterface() = default;
43
44 // any methods that need to be mocked should go here
45 virtual void init() = 0;
46 virtual void setSolenoid(uint8_t solenoid, bool state) = 0;
47 virtual void setSolenoids(uint16_t state) = 0;
48};
49
50// Container class for the static methods that control the solenoids.
51// Dependency injection is enabled using a pointer to a global instance of
52// either `Solenoids` or `SolenoidsMock`, both of which classes implement
53// the pure virtual methods of `SolenoidsInterface`.
54
55class GlobalSolenoids final {
56private:
57 // singleton class so private constructor is appropriate
58 GlobalSolenoids() = default;
59
60public:
61 // pointer to global instance whose methods are implemented
62 static SolenoidsInterface *m_instance;
63
64 static void init();
65 static void setSolenoid(uint8_t solenoid, bool state);
66 static void setSolenoids(uint16_t state);
67};
68
70#ifdef AYAB_TESTS
71 FRIEND_TEST(SolenoidsTest, test_init);
72 FRIEND_TEST(SolenoidsTest, test_setSolenoid1);
73 FRIEND_TEST(SolenoidsTest, test_setSolenoid2);
74 FRIEND_TEST(SolenoidsTest, test_setSolenoid3);
75#endif
76public:
77 Solenoids() = default;
78
79 void init() final;
80 void setSolenoid(uint8_t solenoid, bool state) final;
81 void setSolenoids(uint16_t state) final;
82
83private:
84 uint16_t solenoidState = 0x0000U;
85 void write(uint16_t state);
86
89};
90
91#endif // SOLENOIDS_H_
Definition Adafruit_MCP23008.h:23
Definition solenoids.h:55
Definition solenoids.h:69
void setSolenoids(uint16_t state) final
Set the state of all the solenoids.
Definition solenoids.cpp:73
void write(uint16_t state)
Definition solenoids.cpp:96
void setSolenoid(uint8_t solenoid, bool state) final
Set the state of a solenoid.
Definition solenoids.cpp:48
void init() final
Initialize I2C connection for solenoids.
Definition solenoids.cpp:31
Definition solenoids.h:40
Definition test_solenoids.cpp:33