Skip to content

Commit b276fdd

Browse files
committed
Add accelerometer support
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 4ff42b8 commit b276fdd

1 file changed

Lines changed: 57 additions & 6 deletions

File tree

framework_lib/src/power.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ const EC_MEMMAP_BATT_SERIAL: u16 = 0x70; // Battery Serial Number String
5454
const EC_MEMMAP_BATT_TYPE: u16 = 0x78; // Battery Type String
5555
const EC_MEMMAP_ALS: u16 = 0x80; // ALS readings in lux (2 X 16 bits)
5656
// Unused 0x84 - 0x8f
57-
const _EC_MEMMAP_ACC_STATUS: u16 = 0x90; // Accelerometer status (8 bits )
58-
// Unused 0x91
59-
const _EC_MEMMAP_ACC_DATA: u16 = 0x92; // Accelerometers data 0x92 - 0x9f
60-
// 0x92: u16Lid Angle if available, LID_ANGLE_UNRELIABLE otherwise
61-
// 0x94 - 0x99: u161st Accelerometer
62-
// 0x9a - 0x9f: u162nd Accelerometer
57+
const EC_MEMMAP_ACC_STATUS: u16 = 0x90; // Accelerometer status (8 bits )
58+
// Unused 0x91
59+
const EC_MEMMAP_ACC_DATA: u16 = 0x92; // Accelerometers data 0x92 - 0x9f
60+
// 0x92: u16Lid Angle if available, LID_ANGLE_UNRELIABLE otherwise
61+
// 0x94 - 0x99: u161st Accelerometer
62+
// 0x9a - 0x9f: u162nd Accelerometer
63+
const LID_ANGLE_UNRELIABLE: u16 = 500;
6364
const _EC_MEMMAP_GYRO_DATA: u16 = 0xa0; // Gyroscope data 0xa0 - 0xa5
6465
// Unused 0xa6 - 0xdf
6566

@@ -163,6 +164,28 @@ impl From<PowerInfo> for ReducedPowerInfo {
163164
}
164165
}
165166

167+
#[derive(Debug)]
168+
struct AccelData {
169+
x: u16,
170+
y: u16,
171+
z: u16,
172+
}
173+
impl From<Vec<u8>> for AccelData {
174+
fn from(t: Vec<u8>) -> Self {
175+
Self {
176+
x: u16::from_le_bytes([t[0], t[1]]),
177+
y: u16::from_le_bytes([t[2], t[3]]),
178+
z: u16::from_le_bytes([t[4], t[5]]),
179+
}
180+
}
181+
}
182+
impl fmt::Display for AccelData {
183+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184+
write!(f, "X: {:>5} Y: {:>5}, Z: {:>5}", self.x, self.y, self.z)
185+
}
186+
}
187+
188+
166189
fn read_string(ec: &CrosEc, address: u16) -> String {
167190
let bytes = ec.read_memory(address, EC_MEMMAP_TEXT_MAX).unwrap();
168191
String::from_utf8_lossy(bytes.as_slice()).replace(['\0'], "")
@@ -200,6 +223,34 @@ pub fn get_als_reading(ec: &CrosEc) -> Option<u32> {
200223
pub fn print_sensors(ec: &CrosEc) {
201224
let als_int = get_als_reading(ec).unwrap();
202225
println!("ALS: {:>4} Lux", als_int);
226+
227+
// bit 4 = busy
228+
// bit 7 = present
229+
// #define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK 0x0f
230+
let acc_status = ec.read_memory(EC_MEMMAP_ACC_STATUS, 0x01).unwrap()[0];
231+
// While busy, keep reading
232+
233+
let lid_angle = ec.read_memory(EC_MEMMAP_ACC_DATA, 0x02).unwrap();
234+
let lid_angle = u16::from_le_bytes([lid_angle[0], lid_angle[1]]);
235+
let accel_1 = ec.read_memory(EC_MEMMAP_ACC_DATA + 2, 0x06).unwrap();
236+
let accel_2 = ec.read_memory(EC_MEMMAP_ACC_DATA + 8, 0x06).unwrap();
237+
238+
println!("Accelerometers:");
239+
println!(" Status Bit: {} 0x{:X}", acc_status, acc_status);
240+
println!(" Present: {}", (acc_status & 0x80) > 0);
241+
println!(" Busy: {}", (acc_status & 0x8) > 0);
242+
print!(" Lid Angle: ");
243+
if lid_angle == LID_ANGLE_UNRELIABLE {
244+
println!("Unreliable");
245+
} else {
246+
println!("{} Deg", lid_angle);
247+
}
248+
println!(" Sensor 1: {}", AccelData::from(accel_1));
249+
println!(" Sensor 2: {}", AccelData::from(accel_2));
250+
// Accelerometers
251+
// Lid Angle: 26 Deg
252+
// Sensor 1: 00.00 X 00.00 Y 00.00 Z
253+
// Sensor 2: 00.00 X 00.00 Y 00.00 Z
203254
}
204255

205256
pub fn print_thermal(ec: &CrosEc) {

0 commit comments

Comments
 (0)