Module avr_oxide::util::persist

source ·
Expand description

An incredibly simple implementation for persisting data types to/from a writer/reader.

Why use this instead of something nice like serde and postcard? Because most of the time, those things depend on stuff we don’t have in the AVR world (not just no_std, but missing things like atomic datatypes), and even when you do get them to build - they’re just too big for our devices.

§Usage

Simply implement the Persist trait on anything you wish to be able to load or save from anything which implements avr_oxide::io::Read or avr_oxide::io::Write.

Implementations are provided for the basic numeric types and for Vec types.

Furthermore, in the oxide_macros::Persist there is a Derive trait provided that will implement for simple Structs or Enums:

use oxide_macros::Persist;
use avr_oxide::util::persist::Persist;
use avr_oxide::hal::generic::eeprom::EepromSpace;

#[derive(Persist)]
enum MyEnumeration {
  Something,
  SomethingNumbered(u8),
  SomethingComplex { first: u16, second: u32 }
}

#[derive(Persist)]
#[persist(magicnumber = 1)]
struct MyDataStructure {
  number: u8,
  vector: Vec<u16>,
  something: MyEnumeration
}

pub fn load_save_object() -> ! {
  let eeprom = &mut avr_oxide::hardware::nvm::eeprom::instance();

  let mut thing : MyDataStructure = Persist::load_with(eeprom.reader()).unwrap();
  thing.number += 1u8;
  thing.save_with(eeprom.writer()).unwrap();

  loop{}
}

The optional attribute magicnumber allows you to specify the ‘magic number’ (u8) which will be used to identify this structure. This can be used for versioning your structure, to ensure that deserialisation will fail if the magic number (or version) does not match.

Modules§

Enums§

Traits§

  • Trait implemented by types that can be read/written from a Reader/Writer as a simple binary serialisation format.

Type Aliases§