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
com.h
Go to the documentation of this file.
1
24#ifndef COM_H_
25#define COM_H_
26
27#include <Arduino.h>
28#include <PacketSerial.h>
29
30#include "encoders.h"
31#include "fsm.h"
32
33#ifndef AYAB_TESTS
34 #include "version.h"
35#else
36 constexpr uint8_t FW_VERSION_MAJ = 0U;
37 constexpr uint8_t FW_VERSION_MIN = 0U;
38 constexpr uint8_t FW_VERSION_PATCH = 0U;
39 constexpr char FW_VERSION_SUFFIX[] = "";
40#endif // AYAB_TESTS
41
42constexpr uint8_t API_VERSION = 6U;
43
44constexpr uint32_t SERIAL_BAUDRATE = 115200U;
45
46constexpr uint8_t MAX_LINE_BUFFER_LEN = 25U;
47constexpr uint8_t MAX_MSG_BUFFER_LEN = 64U;
48
49enum class AYAB_API : unsigned char {
50 reqStart = 0x01,
51 cnfStart = 0xC1,
52 reqLine = 0x82,
53 cnfLine = 0x42,
54 reqInfo = 0x03,
55 cnfInfo = 0xC3,
56 reqTest = 0x04,
57 cnfTest = 0xC4,
58 indState = 0x84,
59 helpCmd = 0x25,
60 sendCmd = 0x26,
61 beepCmd = 0x27,
62 setSingleCmd = 0x28,
63 setAllCmd = 0x29,
64 readEOLsensorsCmd = 0x2A,
65 readEncodersCmd = 0x2B,
66 autoReadCmd = 0x2C,
67 autoTestCmd = 0x2D,
68 stopCmd = 0x2E,
69 quitCmd = 0x2F,
70 reqInit = 0x05,
71 cnfInit = 0xC5,
72 testRes = 0xEE,
73 debug = 0x9F
74};
75using AYAB_API_t = enum AYAB_API;
76
77// API constants
78constexpr uint8_t INDSTATE_LEN = 10U;
79constexpr uint8_t REQLINE_LEN = 3U;
80
82public:
83 virtual ~ComInterface() = default;
84
85 // any methods that need to be mocked should go here
86 virtual void init() = 0;
87 virtual void update() = 0;
88 virtual void send(uint8_t *payload, size_t length) const = 0;
89 virtual void sendMsg(AYAB_API_t id, const char *msg) = 0;
90 virtual void send_reqLine(const uint8_t lineNumber,
91 Err_t error = ErrorCode::success) const = 0;
92 virtual void send_indState(Carriage_t carriage, uint8_t position,
93 Err_t error = ErrorCode::success) const = 0;
94 virtual void onPacketReceived(const uint8_t *buffer, size_t size) = 0;
95};
96
97// Container class for the static methods that implement the serial API.
98// Dependency injection is enabled using a pointer to a global instance of
99// either `Com` or `ComMock`, both of which classes implement the
100// pure virtual methods of `ComInterface`.
101
102class GlobalCom final {
103private:
104 // singleton class so private constructor is appropriate
105 GlobalCom() = default;
106
107public:
108 // pointer to global instance whose methods are implemented
109 static ComInterface *m_instance;
110
111 static void init();
112 static void update();
113 static void send(uint8_t *payload, size_t length);
114 static void sendMsg(AYAB_API_t id, const char *msg);
115 static void send_reqLine(const uint8_t lineNumber, Err_t error = ErrorCode::success);
116 static void send_indState(Carriage_t carriage, uint8_t position,
117 Err_t error = ErrorCode::success);
118 static void onPacketReceived(const uint8_t *buffer, size_t size);
119
120private:
121 static SLIPPacketSerial m_packetSerial;
122};
123
124class Com : public ComInterface {
125public:
126 void init() final;
127 void update() final;
128 void send(uint8_t *payload, size_t length) const final;
129 void sendMsg(AYAB_API_t id, const char *msg) final;
130 void send_reqLine(const uint8_t lineNumber, Err_t error = ErrorCode::success) const final;
131 void send_indState(Carriage_t carriage, uint8_t position,
132 Err_t error = ErrorCode::success) const final;
133 void onPacketReceived(const uint8_t *buffer, size_t size) final;
134
135private:
137 uint8_t lineBuffer[MAX_LINE_BUFFER_LEN] = {0};
138 uint8_t msgBuffer[MAX_MSG_BUFFER_LEN] = {0};
139
140 void h_reqInit(const uint8_t *buffer, size_t size);
141 void h_reqStart(const uint8_t *buffer, size_t size);
142 void h_cnfLine(const uint8_t *buffer, size_t size);
143 void h_reqInfo() const;
144 void h_reqTest() const;
145 void h_unrecognized() const;
146
147 void send_cnfInfo() const;
148 void send_cnfInit(Err_t error) const;
149 void send_cnfStart(Err_t error) const;
150 void send_cnfTest(Err_t error) const;
151 uint8_t CRC8(const uint8_t *buffer, size_t len) const;
152};
153
154#endif // COM_H_
Definition com.h:124
void send_indState(Carriage_t carriage, uint8_t position, Err_t error=ErrorCode::success) const final
Send indState message.
Definition com.cpp:96
void send_cnfInit(Err_t error) const
Send cnfInit message.
Definition com.cpp:360
void h_reqTest() const
Handle reqTest (request hardware test) command.
Definition com.cpp:321
uint8_t CRC8(const uint8_t *buffer, size_t len) const
Calculate CRC8 of a buffer.
Definition com.cpp:406
void send(uint8_t *payload, size_t length) const final
Send a packet of data.
Definition com.cpp:51
void onPacketReceived(const uint8_t *buffer, size_t size) final
Callback for PacketSerial.
Definition com.cpp:121
void h_unrecognized() const
Handle unrecognized command.
Definition com.cpp:335
void send_cnfStart(Err_t error) const
Send cnfStart message.
Definition com.cpp:373
void init() final
Initialize serial communication.
Definition com.cpp:32
void send_cnfInfo() const
Send cnfInfo message.
Definition com.cpp:343
void sendMsg(AYAB_API_t id, const char *msg) final
send initial msgid followed by null-terminated string
Definition com.cpp:70
void send_reqLine(const uint8_t lineNumber, Err_t error=ErrorCode::success) const final
Send reqLine message.
Definition com.cpp:84
void h_reqStart(const uint8_t *buffer, size_t size)
Handle reqStart (start request) command.
Definition com.cpp:232
void update() final
Service the serial connection.
Definition com.cpp:42
void h_reqInfo() const
Handle reqInfo (request information) command.
Definition com.cpp:312
void h_cnfLine(const uint8_t *buffer, size_t size)
Handle cnfLine (configure line) command.
Definition com.cpp:271
void h_reqInit(const uint8_t *buffer, size_t size)
Handle reqInit (request initialization) command.
Definition com.cpp:205
void send_cnfTest(Err_t error) const
Send cnfTest message.
Definition com.cpp:385
Definition com.h:81
Definition com.h:102
A template class enabling packet-based Serial communication.
Definition PacketSerial.h:29