embedded_drivers/
lib.rs

1#![no_std]
2
3use core::fmt;
4use core::str;
5
6pub mod bme280;
7pub mod bmp180;
8pub mod esp_at;
9pub mod pmsx003;
10
11pub mod lm75;
12
13pub mod onewire;
14
15/// A fmt::Write for bytes.
16pub struct ByteMutWriter<'a> {
17    buf: &'a mut [u8],
18    cursor: usize,
19}
20
21impl<'a> ByteMutWriter<'a> {
22    pub fn new(buf: &'a mut [u8]) -> Self {
23        ByteMutWriter { buf, cursor: 0 }
24    }
25
26    pub fn as_str(&self) -> &str {
27        unsafe { str::from_utf8_unchecked(&self.buf[0..self.cursor]) }
28    }
29
30    #[inline]
31    pub fn capacity(&self) -> usize {
32        self.buf.len()
33    }
34
35    pub fn clear(&mut self) {
36        self.cursor = 0;
37    }
38
39    pub fn len(&self) -> usize {
40        self.cursor
41    }
42
43    pub fn empty(&self) -> bool {
44        self.cursor == 0
45    }
46
47    pub fn full(&self) -> bool {
48        self.capacity() == self.cursor
49    }
50
51    pub fn write_byte(&mut self, b: u8) {
52        if !self.full() {
53            self.buf[self.cursor] = b;
54            self.cursor += 1;
55        }
56    }
57
58    pub fn write_char(&mut self, c: char) {
59        self.write_byte(c as u8);
60    }
61}
62
63impl fmt::Write for ByteMutWriter<'_> {
64    fn write_str(&mut self, s: &str) -> fmt::Result {
65        let cap = self.capacity();
66        for (i, &b) in self.buf[self.cursor..cap]
67            .iter_mut()
68            .zip(s.as_bytes().iter())
69        {
70            *i = b;
71        }
72        self.cursor = usize::min(cap, self.cursor + s.as_bytes().len());
73        Ok(())
74    }
75}