sparkfun_openlog/
lib.rs

1//! A platform-agnostic, embedded-hal driver for the [Sparkfun OpenLogger](https://www.sparkfun.com/products/13712).
2#![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    /// Create a new OpenLogger instance.
33    pub fn new(address: DeviceAddr, i2c: I2C) -> Self {
34        Self { i2c, address }
35    }
36
37    /// Create a new OpenLogger instance and validate that it is ready for writing.
38    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    /// Create a new directory, relative to the current directory.
79    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    /// Open and append to a file.
91    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    /// Create a new file, but don't open it for writing.
103    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/*
116    Outstanding commands:
117
118    virtual size_t write(uint8_t character);
119    int writeString(String string);
120    bool syncFile(void);
121
122    boolean setI2CAddress(uint8_t addr); //Set the I2C address we read and write to
123    boolean changeDirectory(String directoryName); //Change to the given directory
124    int32_t size(String fileName); //Given a file name, read the size of the file
125
126    void read(uint8_t* userBuffer, uint16_t bufferSize, String fileName); //Read the contents of a file into the provided buffer
127
128    boolean searchDirectory(String options); //Search the current directory for a given wildcard
129    String getNextDirectoryItem(); //Return the next file or directory from the search
130
131    uint32_t removeFile(String thingToDelete); //Remove file
132    uint32_t removeDirectory(String thingToDelete); //Remove a directory including the contents of the directory
133    uint32_t remove(String thingToDelete, boolean removeEverthing); //Remove file or directory including the contents of the directory
134*/