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
fsm.h
Go to the documentation of this file.
1
24#ifndef FSM_H_
25#define FSM_H_
26
27enum class OpState : unsigned char {
28 wait_for_machine,
29 init,
30 ready,
31 knit,
32 test,
33 error
34};
35using OpState_t = enum OpState;
36
37// As of APIv6, the only important distinction
38// is between `ErrorCode::success` (0) and any other value.
39// Informative error codes are provided for
40// diagnostic purposes (that is, for debugging).
41// Non-zero error codes are subject to change.
42// Such changes will be considered non-breaking.
43enum class ErrorCode : unsigned char {
44 success = 0x00,
45
46 // message not understood
47 expected_longer_message = 0x01,
48 unrecognized_msgid = 0x02,
49 unexpected_msgid = 0x03,
50 checksum_error = 0x04,
51
52 // invalid arguments
53 machine_type_invalid = 0x10,
54 needle_value_invalid = 0x11,
55 null_pointer_argument = 0x12,
56 argument_invalid = 0x13,
57 arguments_incompatible = 0x13,
58
59 // device not initialized
60 no_machine_type = 0x20,
61 no_carriage = 0x21,
62 no_direction = 0x22,
63 no_beltshift = 0x23,
64
65 // machine in wrong FSM state
66 machine_state_init = 0xE0,
67 machine_state_ready = 0xE1,
68 machine_state_knit = 0xE2,
69 machine_state_test = 0xE3,
70 wrong_machine_state = 0xEF,
71
72 // generic error codes
73 warning = 0xF0, // ignorable error
74 recoverable_error = 0xF1,
75 critical_error = 0xF2,
76 fatal_error = 0xF3,
77 unspecified_failure = 0xFF
78};
79using Err_t = enum ErrorCode;
80
81constexpr unsigned int FLASH_DELAY = 500; // ms
82
84public:
85 virtual ~FsmInterface() = default;
86
87 // any methods that need to be mocked should go here
88 virtual void init() = 0;
89 virtual OpState_t getState() = 0;
90 virtual void setState(OpState_t state) = 0;
91 virtual void dispatch() = 0;
92};
93
94// Singleton container class for static methods.
95// Dependency injection is enabled using a pointer
96// to a global instance of either `Knitter` or `KnitterMock`
97// both of which classes implement the pure virtual methods
98// of the `KnitterInterface` class.
99
100class GlobalFsm final {
101private:
102 // singleton class so private constructor is appropriate
103 GlobalFsm() = default;
104
105public:
106 // pointer to global instance whose methods are implemented
107 static FsmInterface *m_instance;
108
109 static void init();
110 static OpState_t getState();
111 static void setState(OpState_t state);
112 static void dispatch();
113};
114
115class Fsm : public FsmInterface {
116public:
117 void init() final;
118 OpState_t getState() final;
119 void setState(OpState_t state) final;
120 void dispatch() final;
121
122private:
123 void state_wait_for_machine() const;
124 void state_init();
125 void state_ready() const;
126 void state_knit() const;
127 void state_test() const;
128 void state_error();
129
130 // machine state
131 OpState_t m_currentState;
132 OpState_t m_nextState;
133
134 // error state
135 Err_t m_error;
136
137 // flashing LEDs in error state
138 bool m_flash;
139 unsigned long m_flashTime;
140};
141
142#endif // FSM_H_
Definition fsm.h:115
void state_wait_for_machine() const
Action of machine in state wait_for_machine.
Definition fsm.cpp:111
OpState_t getState() final
Get machine state.
Definition fsm.cpp:55
void state_init()
Action of machine in state OpState::init.
Definition fsm.cpp:118
void state_ready() const
Action of machine in state OpState::ready.
Definition fsm.cpp:128
void setState(OpState_t state) final
Set machine state.
Definition fsm.cpp:65
void dispatch() final
Dispatch on machine state.
Definition fsm.cpp:72
void state_error()
Action of machine in state OpState::error.
Definition fsm.cpp:155
void init() final
Initialize Finite State Machine.
Definition fsm.cpp:43
void state_knit() const
Action of machine in state OpState::knit.
Definition fsm.cpp:135
void state_test() const
Action of machine in state OpState::test.
Definition fsm.cpp:143
Definition fsm.h:83
Definition fsm.h:100