|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+use core::fmt::Write;
|
|
|
2
|
+
|
|
1
|
3
|
use embedded_hal::blocking::delay::DelayUs;
|
|
2
|
4
|
use embedded_hal::digital::v2::OutputPin;
|
|
3
|
5
|
use epd_waveshare::epd4in2::EPD4in2;
|
|
|
@@ -133,96 +135,79 @@ impl Display {
|
|
133
|
135
|
}
|
|
134
|
136
|
}
|
|
135
|
137
|
|
|
|
138
|
+struct WriteBuffer<T> {
|
|
|
139
|
+ buffer: T,
|
|
|
140
|
+ length: usize,
|
|
|
141
|
+}
|
|
|
142
|
+
|
|
|
143
|
+impl<T> Write for WriteBuffer<T>
|
|
|
144
|
+where
|
|
|
145
|
+ T: AsMut<[u8]>,
|
|
|
146
|
+{
|
|
|
147
|
+ fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
|
|
148
|
+ let bytes = s.as_bytes();
|
|
|
149
|
+ if bytes.len() <= self.buffer.as_mut().len() - self.length {
|
|
|
150
|
+ self.buffer.as_mut()[self.length..bytes.len() + self.length].copy_from_slice(bytes);
|
|
|
151
|
+ self.length += bytes.len();
|
|
|
152
|
+ Ok(())
|
|
|
153
|
+ } else {
|
|
|
154
|
+ Err(core::fmt::Error)
|
|
|
155
|
+ }
|
|
|
156
|
+ }
|
|
|
157
|
+}
|
|
|
158
|
+
|
|
|
159
|
+impl<T> WriteBuffer<T>
|
|
|
160
|
+where
|
|
|
161
|
+ T: AsRef<[u8]>,
|
|
|
162
|
+{
|
|
|
163
|
+ fn to_str<'a>(&'a self) -> &'a str {
|
|
|
164
|
+ unsafe {
|
|
|
165
|
+ // Safe, we did not use any non-UTF characters above.
|
|
|
166
|
+ core::str::from_utf8_unchecked(&self.buffer.as_ref()[0..self.length])
|
|
|
167
|
+ }
|
|
|
168
|
+ }
|
|
|
169
|
+}
|
|
|
170
|
+
|
|
136
|
171
|
struct TempHumStrings {
|
|
137
|
|
- temp: [u8; 9],
|
|
138
|
|
- temp_length: usize,
|
|
139
|
|
- hum: [u8; 6],
|
|
140
|
|
- hum_length: usize,
|
|
|
172
|
+ temp: WriteBuffer<[u8; 12]>,
|
|
|
173
|
+ hum: WriteBuffer<[u8; 12]>,
|
|
141
|
174
|
}
|
|
142
|
175
|
|
|
143
|
176
|
impl TempHumStrings {
|
|
144
|
177
|
fn new(data: TemperatureHumidity) -> TempHumStrings {
|
|
145
|
|
- // TODO: Refactor the printing code.
|
|
146
|
|
- let (temp, temp_length) = {
|
|
147
|
|
- let mut x = data.temperature;
|
|
148
|
|
- let mut length = 0;
|
|
149
|
|
- let mut buffer = [0u8; 9];
|
|
150
|
|
- let mut cursor = &mut buffer[..];
|
|
151
|
|
- if x < 0 {
|
|
152
|
|
- cursor[0] = '-' as u8;
|
|
153
|
|
- x = -x;
|
|
154
|
|
- cursor = &mut cursor[1..];
|
|
155
|
|
- length += 1;
|
|
156
|
|
- }
|
|
157
|
|
- let orig_x = x;
|
|
158
|
|
- x = i32::min(x, 9999);
|
|
159
|
|
- if orig_x >= 1000 {
|
|
160
|
|
- cursor[0] = '0' as u8 + (x / 1000) as u8;
|
|
161
|
|
- cursor = &mut cursor[1..];
|
|
162
|
|
- x %= 1000;
|
|
163
|
|
- length += 1;
|
|
164
|
|
- }
|
|
165
|
|
- if orig_x >= 100 {
|
|
166
|
|
- cursor[0] = '0' as u8 + (x / 100) as u8;
|
|
167
|
|
- cursor = &mut cursor[1..];
|
|
168
|
|
- x %= 100;
|
|
169
|
|
- length += 1;
|
|
170
|
|
- }
|
|
171
|
|
- cursor[0] = '0' as u8 + (x / 10) as u8;
|
|
172
|
|
- cursor[1] = ',' as u8;
|
|
173
|
|
- cursor[2] = '0' as u8 + (x % 10) as u8;
|
|
174
|
|
- cursor[3] = 0xc2;
|
|
175
|
|
- cursor[4] = 0xb0;
|
|
176
|
|
- cursor[5] = 'C' as u8;
|
|
177
|
|
- length += 6;
|
|
178
|
|
- (buffer, length)
|
|
179
|
|
- };
|
|
180
|
|
- let (hum, hum_length) = {
|
|
181
|
|
- let mut x = data.humidity;
|
|
182
|
|
- let mut length = 0;
|
|
183
|
|
- let mut buffer = [0u8; 6];
|
|
184
|
|
- let mut cursor = &mut buffer[..];
|
|
185
|
|
- x = u32::min(x, 1000);
|
|
186
|
|
- let orig_x = x;
|
|
187
|
|
- if orig_x >= 1000 {
|
|
188
|
|
- cursor[0] = '1' as u8;
|
|
189
|
|
- cursor = &mut cursor[1..];
|
|
190
|
|
- x %= 1000;
|
|
191
|
|
- length += 1;
|
|
192
|
|
- }
|
|
193
|
|
- if orig_x >= 100 {
|
|
194
|
|
- cursor[0] = '0' as u8 + (x / 100) as u8;
|
|
195
|
|
- cursor = &mut cursor[1..];
|
|
196
|
|
- x %= 100;
|
|
197
|
|
- length += 1;
|
|
198
|
|
- }
|
|
199
|
|
- cursor[0] = '0' as u8 + (x / 10) as u8;
|
|
200
|
|
- cursor[1] = ',' as u8;
|
|
201
|
|
- cursor[2] = '0' as u8 + (x % 10) as u8;
|
|
202
|
|
- cursor[3] = '%' as u8;
|
|
203
|
|
- length += 4;
|
|
204
|
|
- (buffer, length)
|
|
|
178
|
+ let mut strings = TempHumStrings {
|
|
|
179
|
+ temp: WriteBuffer {
|
|
|
180
|
+ buffer: [0u8; 12],
|
|
|
181
|
+ length: 0,
|
|
|
182
|
+ },
|
|
|
183
|
+ hum: WriteBuffer {
|
|
|
184
|
+ buffer: [0u8; 12],
|
|
|
185
|
+ length: 0,
|
|
|
186
|
+ },
|
|
205
|
187
|
};
|
|
206
|
|
- TempHumStrings {
|
|
207
|
|
- temp,
|
|
208
|
|
- temp_length,
|
|
209
|
|
- hum,
|
|
210
|
|
- hum_length,
|
|
211
|
|
- }
|
|
|
188
|
+ write!(
|
|
|
189
|
+ strings.temp,
|
|
|
190
|
+ "{},{}°C",
|
|
|
191
|
+ data.temperature / 10,
|
|
|
192
|
+ (data.temperature % 10).abs()
|
|
|
193
|
+ )
|
|
|
194
|
+ .ok();
|
|
|
195
|
+ write!(
|
|
|
196
|
+ strings.hum,
|
|
|
197
|
+ "{},{}%",
|
|
|
198
|
+ data.humidity / 10,
|
|
|
199
|
+ data.humidity % 10
|
|
|
200
|
+ )
|
|
|
201
|
+ .ok();
|
|
|
202
|
+ strings
|
|
212
|
203
|
}
|
|
213
|
204
|
|
|
214
|
205
|
fn temperature<'a>(&'a self) -> &'a str {
|
|
215
|
|
- unsafe {
|
|
216
|
|
- // Safe, we did not use any non-UTF characters above.
|
|
217
|
|
- core::str::from_utf8_unchecked(&self.temp[0..self.temp_length])
|
|
218
|
|
- }
|
|
|
206
|
+ self.temp.to_str()
|
|
219
|
207
|
}
|
|
220
|
208
|
|
|
221
|
209
|
fn humidity<'a>(&'a self) -> &'a str {
|
|
222
|
|
- unsafe {
|
|
223
|
|
- // Safe, we did not use any non-UTF characters above.
|
|
224
|
|
- core::str::from_utf8_unchecked(&self.hum[0..self.hum_length])
|
|
225
|
|
- }
|
|
|
210
|
+ self.hum.to_str()
|
|
226
|
211
|
}
|
|
227
|
212
|
}
|
|
228
|
213
|
|
|
|
@@ -317,53 +302,21 @@ impl<'a> TempHumTile<'a> {
|
|
317
|
302
|
}
|
|
318
|
303
|
}
|
|
319
|
304
|
|
|
320
|
|
-struct PressureString {
|
|
321
|
|
- buffer: [u8; 9],
|
|
322
|
|
- length: usize,
|
|
323
|
|
-}
|
|
|
305
|
+struct PressureString(WriteBuffer<[u8; 12]>);
|
|
324
|
306
|
|
|
325
|
307
|
impl PressureString {
|
|
326
|
308
|
fn new(pressure: u32) -> PressureString {
|
|
327
|
|
- let mut x = pressure;
|
|
328
|
|
- let mut length = 0;
|
|
329
|
|
- let mut buffer = [0u8; 9];
|
|
330
|
|
- let mut cursor = &mut buffer[..];
|
|
331
|
|
- // Round to one decimal place.
|
|
332
|
|
- x = (x + 5) / 10;
|
|
333
|
|
- x = u32::min(x, 99999);
|
|
334
|
|
- if pressure >= 10000 {
|
|
335
|
|
- cursor[0] = '0' as u8 + (x / 10000) as u8;
|
|
336
|
|
- cursor = &mut cursor[1..];
|
|
337
|
|
- x %= 10000;
|
|
338
|
|
- length += 1;
|
|
339
|
|
- }
|
|
340
|
|
- if pressure >= 1000 {
|
|
341
|
|
- cursor[0] = '0' as u8 + (x / 1000) as u8;
|
|
342
|
|
- cursor = &mut cursor[1..];
|
|
343
|
|
- x %= 1000;
|
|
344
|
|
- length += 1;
|
|
345
|
|
- }
|
|
346
|
|
- if pressure >= 100 {
|
|
347
|
|
- cursor[0] = '0' as u8 + (x / 100) as u8;
|
|
348
|
|
- cursor = &mut cursor[1..];
|
|
349
|
|
- x %= 100;
|
|
350
|
|
- length += 1;
|
|
351
|
|
- }
|
|
352
|
|
- cursor[0] = '0' as u8 + (x / 10) as u8;
|
|
353
|
|
- cursor[1] = ',' as u8;
|
|
354
|
|
- cursor[2] = '0' as u8 + (x % 10) as u8;
|
|
355
|
|
- cursor[3] = 'h' as u8;
|
|
356
|
|
- cursor[4] = 'P' as u8;
|
|
357
|
|
- cursor[5] = 'a' as u8;
|
|
358
|
|
- length += 6;
|
|
359
|
|
- PressureString { buffer, length }
|
|
|
309
|
+ let pressure = (pressure + 5) / 10;
|
|
|
310
|
+ let mut buffer = WriteBuffer {
|
|
|
311
|
+ buffer: [0u8; 12],
|
|
|
312
|
+ length: 0,
|
|
|
313
|
+ };
|
|
|
314
|
+ write!(buffer, "{},{}hPa", pressure / 10, pressure % 10).ok();
|
|
|
315
|
+ PressureString(buffer)
|
|
360
|
316
|
}
|
|
361
|
317
|
|
|
362
|
318
|
fn to_str<'a>(&'a self) -> &'a str {
|
|
363
|
|
- unsafe {
|
|
364
|
|
- // Safe, we did not use any non-UTF characters above.
|
|
365
|
|
- core::str::from_utf8_unchecked(&self.buffer[0..self.length])
|
|
366
|
|
- }
|
|
|
319
|
+ self.0.to_str()
|
|
367
|
320
|
}
|
|
368
|
321
|
}
|
|
369
|
322
|
|