1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! A `Bytestring`, which is used in the [`str1`](crate::controllers::str1) module.
//!
//! A `Bytestring` is a vector of `u8` bytes, usually represented with hex numbers. This
//! bytestring is used to communicate with the STR1XX relay boards. You can read more about
//! then [here](https://github.com/NavasotaBrewing/readme/blob/master/hardware/STR1XX.md).
//!
//! ## Bytestring format
//! The bytestring format is as follows
//!
//! ```text
//! (MA0) (MA1) (BC) (CC) (CN) (Data)… (Data) (CS) (MAE)
//!
//! MA0 = master start 0
//! MA1 = master start 1
//! BC = bytecount from here to end
//! CC = command
//! CN = controller number
//! data = data bytes
//! CS = checksum
//! MAE = master end
//! ```
//!
//! The "master" bytes (`MA0`, `MA1`, `MAE`) are programmed into the board and *can* be changed, but we keep the defaults of
//! ```text
//! // master start bytes, in hex
//! MA0 = 0x55
//! MA1 = 0xAA
//! // master end byte, in hex
//! MAE = 0x77
//! ```
//!
//! This module keeps these as constants and they cannot be changed at this time.
//!
//! The command (`CC`) depends on the command you want to send. A full list can be seen in the
//! [software manual](https://www.smarthardware.eu/manual/str1xxxxxx_com.pdf).
//!
//! The controller number (`CN`) is the controller number programmed into the board. The default is
//! `0xFE`, but this should be changed. Every contorller should have a unique controller number.
//!
//! The data bytes are a variable number of bytes send to the board. They always start with
//! the controller number of the board, then more data bytes.
//!
//! The checksum is the sum of all the data bytes (excluding `MA0`, `MA1`, and `MAE`). The checksum
//! is always one byte. If the sum is higher than `0xFF`, then only the last byte of the number will
//! be kept. Example: `0x3DFA = 0xFA`.
//!
//! See the [software manual](https://www.smarthardware.eu/manual/str1xxxxxx_com.pdf) for more details and
//! a list of commands.
//!
//! ## Creating a `Bytestring`
//! The `MA0`, `MA1`, `CS`, and `MA0` are added automatically. You just need to provide the `BC`, `CC`, CN`, and
//! data bytes.
//!
//! ```rust
//! use brewdrivers::drivers::serial::Bytestring;
//!
//! // This command gets the status of relay 0x00 on controller 0x01
//! let bs = Bytestring::from(vec![0x07, 0x14, 0x01, 0x00, 0x01]);
//! ```
// Master start bytes
const MA0: u8 = 0x55;
const MA1: u8 = 0xAA;
// Master end byte
const MAE: u8 = 0x77;
/// The [`Bytestring`](crate::drivers::serial::Bytestring) struct, representing a message to the STR1XX board.