Hacker News new | ask | show | jobs
by the__alchemist 846 days ago
So, my favorite pattern in Rust for this is to use a u8, (or u16 etc)-repr Enum for register addresses and values. So, you'd do something like, assuming a direct register API vice a wrapper:

  write_register(Reg::Config as u8, value)
Where `value` may be constructed from variables or, wait for it... Might be a binary literal because it's easier to compare to the datasheet if it's a one-off vice a general API. If it's a general API, it is probably handled with a config struct etc, where each field is a u8-repr enum.

Code like this should IMO always have a reference to the relevant DS table in comments, and probably an explanation of why you're setting the bits that way.