diff --git a/examples/esp32_everloop/Cargo.toml b/examples/esp32_everloop/Cargo.toml index 7dd2867..e354eb2 100644 --- a/examples/esp32_everloop/Cargo.toml +++ b/examples/esp32_everloop/Cargo.toml @@ -10,4 +10,4 @@ cty = "0.2" esp-idf-sys = "0.1.2" [build-dependencies] -esp_idf_build = "0.1" \ No newline at end of file +esp_idf_build = ">= 0.1.0, < 0.1.1" \ No newline at end of file diff --git a/src/bus/esp/bus.rs b/src/bus/esp/bus.rs index a7e6628..e392ad6 100644 --- a/src/bus/esp/bus.rs +++ b/src/bus/esp/bus.rs @@ -188,19 +188,12 @@ fn spi_address_bytes(address: u16, readnwrite: bool) -> HardwareAddress { } impl MatrixBus for Bus { - fn write(&self, write_buffer: &mut [u8]) { - // Unpack the write address from the first 32-bits - let buffer_u32 = unsafe { core::intrinsics::transmute::<&mut [u8], &mut [u32]>(write_buffer) }; - let address = u16::try_from(buffer_u32[0]).unwrap(); - // Write actual data to the address - self.write_address(address, &write_buffer[crate::MATRIXBUS_HEADER_BYTES..]) + fn write(&self, address: u16, write_buffer: &[u8]) { + self.write_address(address, write_buffer) } - fn read(&self, read_buffer: &mut [u8]) { - // Unpack the read address from the first 32-bits - let buffer_u32 = unsafe { core::intrinsics::transmute::<&mut [u8], &mut [u32]>(read_buffer) }; - let address = u16::try_from(buffer_u32[0]).unwrap(); - self.read_address(address, &mut read_buffer[crate::MATRIXBUS_HEADER_BYTES..]) + fn read(&self, address: u16, read_buffer: &mut [u8]) { + self.read_address(address, read_buffer) } fn close(&self) { diff --git a/src/bus/mod.rs b/src/bus/mod.rs index e87eeca..66d81d0 100644 --- a/src/bus/mod.rs +++ b/src/bus/mod.rs @@ -33,7 +33,7 @@ pub trait MatrixBus { /// // send buffer /// bus.write(unsafe { std::mem::transmute::<&mut [u32], &mut [u8]>(&mut buffer) }); /// ``` - fn write(&self, write_buffer: &mut [u8]); + fn write(&self, address: u16, write_buffer: &[u8]); /// Send a read buffer to the MATRIX Bus. The buffer requires an `address` to request and /// the `byte_length` of what's expected to be returned. Once sent, the buffer return with populated @@ -57,7 +57,7 @@ pub trait MatrixBus { /// // returned data will start at buffer[2] /// println!("{:?}", buffer); /// ``` - fn read(&self, read_buffer: &mut [u8]); + fn read(&self, address: u16, read_buffer: &mut [u8]); /// If possible, close the connection to the MATRIX Bus. fn close(&self); diff --git a/src/bus/std_bus/direct/mod.rs b/src/bus/std_bus/direct/mod.rs index 2752652..e1ffcd1 100644 --- a/src/bus/std_bus/direct/mod.rs +++ b/src/bus/std_bus/direct/mod.rs @@ -68,15 +68,17 @@ impl Bus { } impl MatrixBus for Bus { - fn write(&self, write_buffer: &mut [u8]) { + fn write(&self, address: u16, write_buffer: &[u8]) { unsafe { // TODO: .... + unimplemented!() } } - fn read(&self, read_buffer: &mut [u8]) { + fn read(&self, address: u16, read_buffer: &mut [u8]) { unsafe { // TODO: .... + unimplemented!() } } diff --git a/src/bus/std_bus/kernel.rs b/src/bus/std_bus/kernel.rs index b604c57..9a72264 100644 --- a/src/bus/std_bus/kernel.rs +++ b/src/bus/std_bus/kernel.rs @@ -1,6 +1,5 @@ use super::super::MatrixBus; -use crate::info; -use crate::{bus::memory_map::*, error::Error, Device}; +use crate::{as_mut_u8_slice, as_u8_slice, bus::memory_map::*, error::Error, Device, info}; use nix::fcntl::{open, OFlag}; // https://linux.die.net/man/3/open use nix::sys::stat::Mode; use nix::unistd::close; @@ -61,18 +60,65 @@ impl Bus { } } +// Calculate number of i32 needed to contain: +// - Address +// - Buffer size +// - buffer.len() bytes +fn ioctl_buffer_i32(buffer: &[u8]) -> usize { + const SIZE_OF_I32: usize = core::mem::size_of::(); + // `(x + y - 1) / y` is the same as `ceiling(x as f32/y as f32) as usize`: + // |buffer.len() | return + // |-|-| + // | 0 | 2 + 0 + // | 1 - 4 | 2 + 1 + // | 5 - 8 | 2 + 2 + 2 + (buffer.len() + SIZE_OF_I32 - 1) / SIZE_OF_I32 +} + impl MatrixBus for Bus { - fn write(&self, write_buffer: &mut [u8]) { + fn write(&self, address: u16, write_buffer: &[u8]) { unsafe { + // Pack request into word-sized/aligned buffer + // Bytes: + // [0..4] = address + // [4..8] = byte size of write payload + // [8..8+sizeof(payload)] = payload + let write_buffer = { + let mut retval = vec![0i32; ioctl_buffer_i32(write_buffer)]; + retval[0] = address as i32; + retval[1] = write_buffer.len() as i32; + as_mut_u8_slice(&mut retval[2..])[..write_buffer.len()].copy_from_slice(write_buffer); + retval + }; // TODO: error handling. Not sure if an error here would be worth recovering from. - ioctl_write(self.regmap_fd, write_buffer).expect("error in IOCTL WRITE"); + ioctl_write(self.regmap_fd, as_u8_slice(&write_buffer[..])).expect("error in IOCTL WRITE"); } } - fn read(&self, read_buffer: &mut [u8]) { + fn read(&self, address: u16, read_buffer: &mut [u8]) { unsafe { + // Pack request into word-sized/aligned buffer + // Bytes: + // [0..4] = address + // [4..8] = byte size of read request + // [8..8+sizeof(payload)] = destination for read payload + let mut buffer = { + let mut retval = vec![0i32; ioctl_buffer_i32(read_buffer)]; + retval[0] = address as i32; + retval[1] = read_buffer.len() as i32; + retval + }; // TODO: error handling. Not sure if an error here would be worth recovering from. - ioctl_read(self.regmap_fd, read_buffer).expect("error in IOCTL READ"); + ioctl_read(self.regmap_fd, as_mut_u8_slice(&mut buffer[..])).expect("error in IOCTL READ"); + // Copy read data back into original argument buffer + read_buffer.copy_from_slice( + &as_u8_slice( + // Skip address and size words + &buffer[2..] + ) + // Limit to size of destination buffer (request was word-aligned) + [..read_buffer.len()] + ); } } diff --git a/src/error.rs b/src/error.rs index b6853fe..5561bc4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,7 +24,9 @@ pub enum Error { /// ESP-IDF call failed. #[cfg(not(feature = "std"))] #[fail(display = "esp-idf error: {}", error)] - EspIdf { error: crate::bus::esp::error::EspError }, + EspIdf { + error: crate::bus::esp::error::EspError, + }, } with_std! { diff --git a/src/everloop/mod.rs b/src/everloop/mod.rs index 55bfe79..0ed26da 100644 --- a/src/everloop/mod.rs +++ b/src/everloop/mod.rs @@ -2,21 +2,21 @@ mod led; use crate::bus::memory_map::*; use crate::bus::MatrixBus; use heapless::Vec; -use typenum::Unsigned; pub use led::Rgbw; +use typenum::Unsigned; /// Bytes to set LED color as [R, G, B, W]. type LedBytes = [u8; 4]; /// Heapless capacity type large enough maximum number of possible LEDs across all devices. type MaxLeds = heapless::consts::U35; /// Heapless capacity type large enough for byte array of maximum possible LEDs and header bytes as passed to `MatrixBus` methods. -type MaxLedBytes = heapless::consts::U148; +type MaxLedBytes = heapless::consts::U140; const LED_BYTES: usize = core::mem::size_of::(); fn _compile_time_checks() { // Need to enforce constraint that certain const/enum values correspond to heapless capacity type "values". - // One way to accomplish this is to use both as the length of an array and assign them to eachother. If the values + // One way to accomplish this is to use both as the length of an array and assign them to eachother. If the values // differ, the arrays are different lengths and type, and Rust will trigger a compiler error. { // Require `MaxLeds` == `MATRIX_CREATOR_LEDS` @@ -25,7 +25,7 @@ fn _compile_time_checks() { } { // Require `MaxLedBytes` has enough space to maximum possible LEDs and header bytes - type ValueArray = [u8; device_info::MATRIX_CREATOR_LEDS as usize * LED_BYTES + crate::MATRIXBUS_HEADER_BYTES]; + type ValueArray = [u8; device_info::MATRIX_CREATOR_LEDS as usize * LED_BYTES]; let _unused: ValueArray = [0u8; MaxLedBytes::USIZE]; } } @@ -59,8 +59,6 @@ impl<'a> Everloop<'a> { // create write buffer let led_bytes = device_leds * LED_BYTES; let mut request: Vec = Vec::new(); - request.extend_from_slice(&(fpga_address::EVERLOOP as i32).to_ne_bytes()).unwrap(); - request.extend_from_slice(&(led_bytes as i32).to_ne_bytes()).unwrap(); // each LED RGBW requires 4 bytes // store all LED colors given for led in leds { @@ -68,16 +66,20 @@ impl<'a> Everloop<'a> { } // set remaining LEDs to black for _ in 0..(device_leds - leds.len()) { - request.extend_from_slice(&Rgbw::black().to_bytes()).unwrap(); + request + .extend_from_slice(&Rgbw::black().to_bytes()) + .unwrap(); } // render LEDs self.bus - .write(&mut request); + .write(fpga_address::EVERLOOP, &mut (request[..led_bytes])); } /// Set all MATRIX LEDs to a single color pub fn set_all(&self, color: Rgbw) { - let leds: Vec = core::iter::repeat(color).take(self.bus.device_leds() as usize).collect(); + let leds: Vec = core::iter::repeat(color) + .take(self.bus.device_leds() as usize) + .collect(); self.set(&leds) } } diff --git a/src/gpio/bank.rs b/src/gpio/bank.rs index d191b4c..af905d8 100644 --- a/src/gpio/bank.rs +++ b/src/gpio/bank.rs @@ -1,6 +1,5 @@ -use crate::bus::memory_map::*; -use crate::bus::MatrixBus; -use core::intrinsics::transmute; +use crate::as_u8_slice; +use crate::bus::{memory_map::*, MatrixBus}; /// Bank contains functions to configure a PWM. /// A bank is a set of 4 pins, starting from pin 0 and going in order. @@ -59,12 +58,8 @@ impl<'a> Bank<'a> { /// Send a bank configuration to the MATRIX bus. fn bus_write(&self, memory_offset: u16, timer_setup: u16) { // create and populate write buffer - let mut buffer: [u32; 3] = [0; 3]; - buffer[0] = (memory_offset) as u32; // address to write to - buffer[1] = 2; // byte length of timer_setup - buffer[2] = timer_setup as u32; + let buffer = [timer_setup; 1]; - self.bus - .write(unsafe { transmute::<&mut [u32], &mut [u8]>(&mut buffer) }); + self.bus.write(memory_offset, as_u8_slice(&buffer)); } } diff --git a/src/gpio/mod.rs b/src/gpio/mod.rs index 8cef176..40edcf5 100644 --- a/src/gpio/mod.rs +++ b/src/gpio/mod.rs @@ -1,11 +1,9 @@ -use crate::bus::MatrixBus; -use crate::Error; +use crate::bus::{memory_map::*, MatrixBus}; +use crate::{as_mut_u8_slice, as_u8_slice, Error}; pub mod bank; pub mod config; -use crate::bus::memory_map::*; pub use bank::*; pub use config::*; -use core::intrinsics::transmute; use core::sync::atomic::{AtomicU16, Ordering}; /// Controls the GPIO pins on a MATRIX device. @@ -56,14 +54,14 @@ impl<'a> Gpio<'a> { Gpio::is_pin_valid(pin).unwrap(); // create read buffer - let mut data: [u32; 3] = [0; 3]; + let mut data = [0u16; 1]; // update read buffer - self.bus_read(&mut data, 2, 1); // all pin states are encoded as a single u16. 2 bytes needed (8*2 = 16 pins) + self.bus_read(&mut data, 1); // all pin states are encoded as a single u16. 2 bytes needed (8*2 = 16 pins) // bit operation to extract the current pin's state let mask = 0x1 << pin; - let state = (data[2] & mask) >> pin; + let state = (data[0] & mask) >> pin; match state { 0 => false, @@ -78,16 +76,16 @@ impl<'a> Gpio<'a> { /// Returns the current digital value of every MATRIX GPIO pin (0->15) pub fn get_states(&self) -> [bool; 16] { // create read buffer - let mut data: [u32; 3] = [0; 3]; + let mut data = [0u16; 1]; // update read buffer - self.bus_read(&mut data, 2, 1); // all pin states are encoded as a single u16. 2 bytes needed (8*2 = 16 pins) + self.bus_read(&mut data, 1); // all pin states are encoded as a single u16. 2 bytes needed (8*2 = 16 pins) // bit operation to extract each pin state (0-15) let mut pins: [bool; 16] = [false; 16]; for i in 0..16 { let mask = 0x1 << i; - let state = ((data[2] & mask) >> i) as u8; + let state = ((data[0] & mask) >> i) as u8; pins[i] = match state { 0 => false, @@ -102,16 +100,11 @@ impl<'a> Gpio<'a> { } /// Shortener to populate a read buffer, through `bus.read`, for GPIO pin information. - fn bus_read(&self, buffer: &mut [u32], buffer_length: u32, address_offset: u16) { - // address to query - buffer[0] = (fpga_address::GPIO + address_offset) as u32; - // size of expected data (bytes) - buffer[1] = buffer_length; - + fn bus_read(&self, buffer: &mut [u16], address_offset: u16) { // populate buffer // the buffer will be passed a value that contains the state of each GPIO pin self.bus - .read(unsafe { transmute::<&mut [u32], &mut [u8]>(buffer) }); + .read(fpga_address::GPIO + address_offset, as_mut_u8_slice(buffer)); } } @@ -150,13 +143,10 @@ impl<'a> Gpio<'a> { /// Shortener to send pin configurations through `bus.write`. fn bus_write(&self, value: u16, address_offset: u16) { - let mut buffer: [u32; 3] = [0; 3]; - buffer[0] = (fpga_address::GPIO + address_offset) as u32; // address to write to - buffer[1] = 2; // byte length of u16 value - buffer[2] = value as u32; + let buffer = [value; 1]; self.bus - .write(unsafe { transmute::<&mut [u32], &mut [u8]>(&mut buffer) }); + .write(fpga_address::GPIO + address_offset, as_u8_slice(&buffer)); } /// Set the prescaler value for a specific bank diff --git a/src/info.rs b/src/info.rs index e370245..b7beb45 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,17 +1,14 @@ use crate::bus::{memory_map::*, MatrixBus}; -use crate::{Device, Error}; -use core::intrinsics::transmute; +use crate::{as_mut_u8_slice, Device, Error}; /// Return the type of MATRIX device being used and the version of the board. pub fn get_device_info(bus: &dyn MatrixBus) -> Result<(Device, u32), Error> { // create read buffer - let mut data: [i32; 4] = [0; 4]; - data[0] = fpga_address::CONF as i32; - data[1] = 8; // device_name(4 bytes) device_version(4 bytes) + let mut data = [0i32; 2]; // device_name(4 bytes) device_version(4 bytes) - bus.read(unsafe { transmute::<&mut [i32], &mut [u8]>(&mut data) }); - let device_name = data[2]; - let device_version = data[3]; + bus.read(fpga_address::CONF, as_mut_u8_slice(&mut data)); + let device_name = data[0]; + let device_version = data[1]; Ok(( match device_name { @@ -26,16 +23,12 @@ pub fn get_device_info(bus: &dyn MatrixBus) -> Result<(Device, u32), Error> { /// Updates the Bus to have the last known FPGA frequency of the MATRIX device. pub fn get_fpga_frequency(bus: &dyn MatrixBus) -> Result { // create read buffer - let mut data: [i32; 3] = [0; 3]; - data[0] = (fpga_address::CONF + 4) as i32; - data[1] = 4; // value0(2 bytes) value1(2bytes) // TODO: ask what these values represent + let mut data = [0u16; 2]; // value0(2 bytes) value1(2bytes) // TODO: ask what these values represent + bus.read(fpga_address::CONF + 4, as_mut_u8_slice(&mut data)); - bus.read(unsafe { transmute::<&mut [i32], &mut [u8]>(&mut data) }); - - // extract both u16 numbers from u32 - let value0 = data[2] >> 16; // store 1st 16 bits - let value1 = !(value0 << 16) & data[2]; // store 2nd 16 bits - let frequency = (device_info::FPGA_CLOCK * value0 as u32) / value1 as u32; + let value0 = data[0] as u32; // store 1st 16 bits + let value1 = data[1] as u32; // store 2nd 16 bits + let frequency = (device_info::FPGA_CLOCK * value0) / value1; Ok(frequency) } diff --git a/src/lib.rs b/src/lib.rs index ad088bd..cd99692 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,13 +18,33 @@ macro_rules! with_std { ($($i:item)*) => ($(#[cfg(feature = "std")]$i)*) } #[macro_export] macro_rules! without_std { ($($i:item)*) => ($(#[cfg(not(feature = "std"))]$i)*) } -/// Buffers passed to `impl MatrixBus` methods contain: -/// | Bytes | | -/// |-|-| -/// | 0-3 | Address for SPI operation -/// | 4-7 | Size of data -/// | 8.. | Data -const MATRIXBUS_HEADER_BYTES: usize = core::mem::size_of::() * 2; +fn as_slice<'a, A, B>(orig: &[A]) -> &'a [B] { + unsafe { + use core::mem::size_of; + core::slice::from_raw_parts( + orig.as_ptr() as *const _, + orig.len() * size_of::() / size_of::(), + ) + } +} + +fn as_mut_slice<'a, A, B>(orig: &mut [A]) -> &'a mut [B] { + unsafe { + use core::mem::size_of; + core::slice::from_raw_parts_mut( + orig.as_ptr() as *mut _, + orig.len() * size_of::() / size_of::(), + ) + } +} + +fn as_u8_slice<'a, A>(orig: &[A]) -> &'a [u8] { + as_slice(orig) +} + +fn as_mut_u8_slice<'a, A>(orig: &mut [A]) -> &'a mut [u8] { + as_mut_slice(orig) +} /// The Different types of MATRIX Devices #[derive(Copy, Clone, Debug, PartialEq)] @@ -37,3 +57,16 @@ pub enum Device { /// Placeholder until the device is known. Unknown, } + +#[cfg(test)] +mod tests { + use super::*; + use core::mem::size_of_val; + #[test] + fn as_slice() { + let i32_array = [0i32; 4]; + let u16_array = [0u16; 4]; + assert_eq!(as_u8_slice(&i32_array).len(), size_of_val(&i32_array)); + assert_eq!(as_u8_slice(&u16_array).len(), size_of_val(&u16_array)); + } +} diff --git a/src/sensors/mod.rs b/src/sensors/mod.rs index 7c6432e..74f4a1b 100644 --- a/src/sensors/mod.rs +++ b/src/sensors/mod.rs @@ -1,7 +1,6 @@ use crate::bus::{memory_map::*, MatrixBus}; -use crate::Device; +use crate::{as_mut_u8_slice, Device}; mod data; -use core::intrinsics::transmute; use data::*; /// Communicates with the main sensors on the MATRIX Creator. @@ -22,94 +21,73 @@ impl<'a> Sensors<'a> { /// Return the latest UV sensor value. pub fn read_uv(&self) -> f32 { - const BUFFER_LENGTH: usize = get_buffer_length(UV_BYTES); - // create read buffer - let mut data: [i32; BUFFER_LENGTH] = [0; BUFFER_LENGTH]; - data[0] = (fpga_address::MCU + (mcu_offset::UV >> 1)) as i32; - data[1] = UV_BYTES; - + let mut data = [0i32; get_buffer_length(UV_BYTES)]; + let address = fpga_address::MCU + (mcu_offset::UV >> 1); // populate buffer - self.bus - .read(unsafe { transmute::<&mut [i32], &mut [u8]>(&mut data) }); - - data[2] as f32 / 1000.0 + self.bus.read(address, as_mut_u8_slice(&mut data)); + data[0] as f32 / 1000.0 } /// Return the latest Pressure sensor values. pub fn read_pressure(&self) -> Pressure { - const BUFFER_LENGTH: usize = get_buffer_length(PRESSURE_BYTES); - // create read buffer - let mut data: [i32; BUFFER_LENGTH] = [0; BUFFER_LENGTH]; - data[0] = (fpga_address::MCU + (mcu_offset::PRESSURE >> 1)) as i32; - data[1] = PRESSURE_BYTES; + let mut data = [0i32; get_buffer_length(PRESSURE_BYTES)]; + let address = fpga_address::MCU + (mcu_offset::PRESSURE >> 1); // populate buffer - self.bus - .read(unsafe { transmute::<&mut [i32], &mut [u8]>(&mut data) }); - + self.bus.read(address, as_mut_u8_slice(&mut data)); Pressure { - pressure: data[3] as f32 / 1000.0, - altitude: data[2] as f32 / 1000.0, - temperature: data[4] as f32 / 1000.0, + pressure: data[1] as f32 / 1000.0, + altitude: data[0] as f32 / 1000.0, + temperature: data[2] as f32 / 1000.0, } } /// Return the latest Humidity sensor values. pub fn read_humidity(&self) -> Humidity { - const BUFFER_LENGTH: usize = get_buffer_length(HUMIDITY_BYTES); - // create read buffer - let mut data: [i32; BUFFER_LENGTH] = [0; BUFFER_LENGTH]; - data[0] = (fpga_address::MCU + (mcu_offset::HUMIDITY >> 1)) as i32; - data[1] = HUMIDITY_BYTES; - + let mut data = [0i32; get_buffer_length(HUMIDITY_BYTES)]; + let address = fpga_address::MCU + (mcu_offset::HUMIDITY >> 1); // populate buffer - self.bus - .read(unsafe { transmute::<&mut [i32], &mut [u8]>(&mut data) }); - + self.bus.read(address, as_mut_u8_slice(&mut data)); Humidity { - humidity: data[2] as f32 / 1000.0, - temperature: data[3] as f32 / 1000.0, + humidity: data[0] as f32 / 1000.0, + temperature: data[1] as f32 / 1000.0, } } /// Return the latest IMU sensor values. pub fn read_imu(&self) -> Imu { - const BUFFER_LENGTH: usize = get_buffer_length(IMU_BYTES); - // create read buffer - let mut data: [i32; BUFFER_LENGTH] = [0; BUFFER_LENGTH]; - data[0] = (fpga_address::MCU + (mcu_offset::IMU >> 1)) as i32; - data[1] = IMU_BYTES; + let mut data = [0i32; get_buffer_length(IMU_BYTES)]; + let address = fpga_address::MCU + (mcu_offset::IMU >> 1); // populate read buffer - self.bus - .read(unsafe { transmute::<&mut [i32], &mut [u8]>(&mut data) }); + self.bus.read(address, as_mut_u8_slice(&mut data)); Imu { - accel_x: data[2] as f32 / 1000.0, - accel_y: data[3] as f32 / 1000.0, - accel_z: data[4] as f32 / 1000.0, + accel_x: data[0] as f32 / 1000.0, + accel_y: data[1] as f32 / 1000.0, + accel_z: data[2] as f32 / 1000.0, - gyro_x: data[5] as f32 / 1000.0, - gyro_y: data[6] as f32 / 1000.0, - gyro_z: data[7] as f32 / 1000.0, + gyro_x: data[3] as f32 / 1000.0, + gyro_y: data[4] as f32 / 1000.0, + gyro_z: data[5] as f32 / 1000.0, - mag_x: data[8] as f32 / 1000.0, - mag_y: data[9] as f32 / 1000.0, - mag_z: data[10] as f32 / 1000.0, + mag_x: data[6] as f32 / 1000.0, + mag_y: data[7] as f32 / 1000.0, + mag_z: data[9] as f32 / 1000.0, // TODO: ask why we have these. They seem to be unused. - mag_offset_x: data[11] as f32, - mag_offset_y: data[12] as f32, - mag_offset_z: data[13] as f32, + mag_offset_x: data[9] as f32, + mag_offset_y: data[10] as f32, + mag_offset_z: data[11] as f32, // These values are already floats so we just need to treat them as one. - yaw: f32::from_bits(data[14] as u32), - pitch: f32::from_bits(data[15] as u32), - roll: f32::from_bits(data[16] as u32), + yaw: f32::from_bits(data[12] as u32), + pitch: f32::from_bits(data[13] as u32), + roll: f32::from_bits(data[14] as u32), } } } @@ -117,8 +95,6 @@ impl<'a> Sensors<'a> { /// Calculate the size a read buffer needs to be for a sensor. /// /// Since all sensor's values are a byte each, we can divide it by 4 to see how many values need to be stored. -/// -/// 2 is added to make room for the `address` and `byte_length` of `bus.read`. const fn get_buffer_length(sensor_bytes: i32) -> usize { - (sensor_bytes / 4 + 2) as usize + (sensor_bytes / 4) as usize }