1#![no_std]
3
4mod command;
5use command::Command;
6
7mod version;
8use embedded_hal::i2c::Operation;
9pub use version::Version;
10
11mod status;
12pub use status::Status;
13
14#[repr(u8)]
15#[derive(Copy, Clone, Debug, Default)]
16pub enum DeviceAddr {
17 #[default]
18 ADDR1 = 0x2A,
19 ADDR2 = 0x29,
20}
21
22#[derive(Debug)]
23pub struct OpenLogger<I2C> {
24 address: DeviceAddr,
25 i2c: I2C,
26}
27
28impl<I2C> OpenLogger<I2C>
29where
30 I2C: embedded_hal::i2c::I2c,
31{
32 pub fn new(address: DeviceAddr, i2c: I2C) -> Self {
34 Self { i2c, address }
35 }
36
37 pub fn new_and_validate(address: DeviceAddr, i2c: I2C) -> Result<Self, I2C::Error> {
39 let mut logger = Self::new(address, i2c);
40 let status = logger.get_status()?;
41
42 if !status.init_ok() {
43 todo!("find the correct error type");
44 }
45
46 Ok(logger)
47 }
48
49 pub fn get_version(&mut self) -> Result<Version, I2C::Error> {
50 let mut major = [0u8; 2];
51 self.i2c.write_read(
52 self.address as u8,
53 &Command::FirmwareMajor.as_bytes(),
54 &mut major,
55 )?;
56
57 let mut minor = [0u8; 2];
58 self.i2c.write_read(
59 self.address as u8,
60 &Command::FirmwareMinor.as_bytes(),
61 &mut minor,
62 )?;
63
64 Ok(Version {
65 major: major[0],
66 minor: minor[0],
67 })
68 }
69
70 pub fn get_status(&mut self) -> Result<Status, I2C::Error> {
71 let mut result = [0u8; 1];
72 self.i2c
73 .write_read(self.address as u8, &Command::Status.as_bytes(), &mut result)?;
74
75 Ok(Status::from(result[0]))
76 }
77
78 pub fn make_directory(&mut self, directory: &str) -> Result<(), I2C::Error> {
80 let command = Command::MkDir.as_bytes();
81
82 let mut operations = [
83 Operation::Write(&command),
84 Operation::Write(directory.as_bytes()),
85 ];
86
87 self.i2c.transaction(self.address as u8, &mut operations)
88 }
89
90 pub fn append(&mut self, file: &str) -> Result<(), I2C::Error> {
92 let command = Command::OpenFile.as_bytes();
93
94 let mut operations = [
95 Operation::Write(&command),
96 Operation::Write(file.as_bytes()),
97 ];
98
99 self.i2c.transaction(self.address as u8, &mut operations)
100 }
101
102 pub fn create(&mut self, file: &str) -> Result<(), I2C::Error> {
104 let command = Command::CreateFile.as_bytes();
105
106 let mut operations = [
107 Operation::Write(&command),
108 Operation::Write(file.as_bytes()),
109 ];
110
111 self.i2c.transaction(self.address as u8, &mut operations)
112 }
113}
114
115