pub fn read_source(text: &str, into: &mut [u8; 8192]) -> Result<(), LoadError>Expand description
Reads an image out of the C source the reference implementation carries it in.
Semtech distributes each image as an array of byte literals rather than as a file of bytes, so converting it by hand is the first thing that stands between a new board and a running gateway. This reads the literals in the order they are written, which turns that step into naming the file.
Anything that is not a byte literal is skipped, so the declaration around the array, the commas, and the closing brace need no stripping.
§Arguments
text- the file, as text.into- where the bytes go, which is exactly what a microcontroller holds.
§Returns
Ok(()) once every byte is in place.
§Errors
Returns LoadError::WrongSize carrying how many literals the text holds when that is
not FIRMWARE_LEN, rather than loading a partial image. A count below it means the file
is truncated, and one above means it carries more than the single array.
§Examples
use pamoja_radios::sx1302::firmware::{read_source, FIRMWARE_LEN};
// The shape Semtech ships, shortened here to what fits on the page.
let mut source = String::from("static uint8_t arb_firmware[8192] = {\n");
for at in 0..FIRMWARE_LEN {
source.push_str(&format!("0x{:02X}, ", at % 256));
}
source.push_str("\n};\n");
let mut image = [0u8; FIRMWARE_LEN];
read_source(&source, &mut image).expect("the array holds a whole image");
assert_eq!(image[0], 0x00);
assert_eq!(image[255], 0xFF);