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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crateMbusError;
/// Maximum number of coils that can be read/written in a single Modbus PDU (2000 coils).
pub const MAX_COILS_PER_PDU: usize = 2000;
/// Maximum number of bytes needed to represent the coil states for 2000 coils (250 bytes).
pub const MAX_COIL_BYTES: usize = MAX_COILS_PER_PDU.div_ceil; // 250 bytes for 2000 coils
/// Represents the state of a block of contiguous coils.
///
/// In the Modbus protocol, coils are 1-bit boolean values (ON = `true`, OFF = `false`) used to represent
/// discrete outputs. To optimize network traffic and memory, these bits are tightly packed into
/// bytes. This struct manages a specific continuous range of coils and abstracts away the complex
/// bitwise operations required to get and set individual coil states.
///
/// The `values` array stores these coil states. Each byte in `values` holds 8 coil states,
/// where the least significant bit (LSB) of the first byte corresponds to `from_address`,
/// the next bit to `from_address + 1`, and so on. `MAX_COIL_BYTES` is calculated to
/// accommodate `MAX_COILS_PER_PDU` coils (2000 coils require 250 bytes).
///
/// # Examples
///
/// ```rust
/// use mbus_core::models::coil::Coils;
/// use mbus_core::errors::MbusError;
///
/// // Initialize a block of 8 coils starting at Modbus address 100.
/// // Initially all coils are OFF (0).
/// let mut coils = Coils::new(100, 8).unwrap();
///
/// // Verify initial state: all coils are false
/// assert_eq!(coils.value(100).unwrap(), false);
/// assert_eq!(coils.value(107).unwrap(), false);
///
/// // Set coil at address 100 (offset 0) to ON
/// // Internal values: `values[0]` becomes `0b0000_0001`
/// coils.set_value(100, true).unwrap();
/// assert_eq!(coils.value(100).unwrap(), true);
/// assert_eq!(coils.values()[..1], [0b0000_0001]);
/// assert_eq!(coils.values()[..1], [0b0000_0001]);
///
/// // Set coil at address 102 (offset 2) to ON
/// // Internal values: `values[0]` becomes `0b0000_0101`
/// coils.set_value(102, true).unwrap();
/// assert_eq!(coils.value(102).unwrap(), true);
/// assert_eq!(coils.values()[..1], [0b0000_0101]);
/// assert_eq!(coils.values()[..1], [0b0000_0101]);
///
/// // Set coil at address 101 (offset 1) to ON
/// // Internal values: `values[0]` becomes `0b0000_0111`
/// coils.set_value(101, true).unwrap();
/// assert_eq!(coils.value(101).unwrap(), true);
/// assert_eq!(coils.values()[..1], [0b0000_0111]);
/// assert_eq!(coils.values()[..1], [0b0000_0111]);
///
/// // Set coil at address 100 back to OFF
/// // Internal values: `values[0]` becomes `0b0000_0110`
/// coils.set_value(100, false).unwrap();
/// assert_eq!(coils.value(100).unwrap(), false);
/// assert_eq!(coils.values()[..1], [0b0000_0110]);
/// assert_eq!(coils.values()[..1], [0b0000_0110]);
///
/// // Example with `with_values` for loading pre-packed data
/// let pre_packed_data = [0b1010_1010, 0b0101_0101]; // Two bytes for 16 coils
/// let mut loaded_coils = Coils::new(200, 16).unwrap()
/// .with_values(&pre_packed_data, 16)
/// .expect("Valid quantity and data");
///
/// assert_eq!(loaded_coils.value(200).unwrap(), false); // LSB of 0b1010_1010 is 0
/// assert_eq!(loaded_coils.value(201).unwrap(), true); // Next bit is 1
/// assert_eq!(loaded_coils.value(208).unwrap(), true); // LSB of 0b0101_0101 is 1 (first bit of second byte)
/// ```
/// Provides operations for reading and writing Modbus coils.