11//! Get information about system power (battery, AC, PD ports)
22
3+ use alloc:: format;
34use alloc:: string:: String ;
45use alloc:: vec;
56use alloc:: vec:: Vec ;
@@ -10,11 +11,11 @@ use log::Level;
1011
1112use crate :: ccgx:: { AppVersion , Application , BaseVersion , ControllerVersion , MainPdVersions } ;
1213use crate :: chromium_ec:: command:: EcRequestRaw ;
13- use crate :: chromium_ec:: commands:: { EcRequestReadPdVersion , EcRequestUsbPdPowerInfo } ;
14- use crate :: chromium_ec:: { print_err_ref , CrosEc , CrosEcDriver , EcResult } ;
14+ use crate :: chromium_ec:: commands:: * ;
15+ use crate :: chromium_ec:: * ;
1516use crate :: smbios;
1617use crate :: smbios:: get_platform;
17- use crate :: util:: Platform ;
18+ use crate :: util:: { Platform , PlatformFamily } ;
1819
1920/// Maximum length of strings in memmap
2021const EC_MEMMAP_TEXT_MAX : u16 = 8 ;
@@ -245,9 +246,15 @@ pub fn print_memmap_version_info(ec: &CrosEc) {
245246}
246247
247248/// Not supported on TGL EC
248- pub fn get_als_reading ( ec : & CrosEc ) -> Option < u32 > {
249+ pub fn get_als_reading ( ec : & CrosEc , index : usize ) -> Option < u32 > {
249250 let als = ec. read_memory ( EC_MEMMAP_ALS , 0x04 ) ?;
250- Some ( u32:: from_le_bytes ( [ als[ 0 ] , als[ 1 ] , als[ 2 ] , als[ 3 ] ] ) )
251+ let offset = index + 4 * index;
252+ Some ( u32:: from_le_bytes ( [
253+ als[ offset] ,
254+ als[ 1 + offset] ,
255+ als[ 2 + offset] ,
256+ als[ 3 + offset] ,
257+ ] ) )
251258}
252259
253260pub fn get_accel_data ( ec : & CrosEc ) -> ( AccelData , AccelData , LidAngle ) {
@@ -274,8 +281,40 @@ pub fn get_accel_data(ec: &CrosEc) -> (AccelData, AccelData, LidAngle) {
274281}
275282
276283pub fn print_sensors ( ec : & CrosEc ) {
277- let als_int = get_als_reading ( ec) . unwrap ( ) ;
278- println ! ( "ALS: {:>4} Lux" , als_int) ;
284+ let mut has_als = false ;
285+ let mut accel_locations = vec ! [ ] ;
286+
287+ match ec. motionsense_sensor_info ( ) {
288+ Ok ( sensors) => {
289+ info ! ( "Sensors: {}" , sensors. len( ) ) ;
290+ for sensor in sensors {
291+ info ! ( " Type: {:?}" , sensor. sensor_type) ;
292+ info ! ( " Location: {:?}" , sensor. location) ;
293+ info ! ( " Chip: {:?}" , sensor. chip) ;
294+ if sensor. sensor_type == MotionSenseType :: Light {
295+ has_als = true ;
296+ }
297+ if sensor. sensor_type == MotionSenseType :: Accel {
298+ accel_locations. push ( sensor. location ) ;
299+ }
300+ }
301+ }
302+ Err ( EcError :: Response ( EcResponseStatus :: InvalidCommand ) ) => {
303+ debug ! ( "Motionsense commands not supported" )
304+ }
305+ err => _ = print_err ( err) ,
306+ }
307+
308+ // If we can't detect it based on motionsense
309+ let als_family = matches ! (
310+ smbios:: get_family( ) ,
311+ Some ( PlatformFamily :: Framework13 ) | Some ( PlatformFamily :: Framework16 )
312+ ) ;
313+
314+ if has_als || als_family {
315+ let als_int = get_als_reading ( ec, 0 ) . unwrap ( ) ;
316+ println ! ( "ALS: {:>4} Lux" , als_int) ;
317+ }
279318
280319 // bit 4 = busy
281320 // bit 7 = present
@@ -294,18 +333,22 @@ pub fn print_sensors(ec: &CrosEc) {
294333 debug ! ( " Status Bit: {} 0x{:X}" , acc_status, acc_status) ;
295334 debug ! ( " Present: {}" , present) ;
296335 debug ! ( " Busy: {}" , ( acc_status & 0x8 ) > 0 ) ;
297- print ! ( " Lid Angle: " ) ;
336+ print ! ( " Lid Angle: " ) ;
298337 if lid_angle == LID_ANGLE_UNRELIABLE {
299338 println ! ( "Unreliable" ) ;
300339 } else {
301340 println ! ( "{} Deg" , lid_angle) ;
302341 }
303- println ! ( " Sensor 1: {}" , AccelData :: from( accel_1) ) ;
304- println ! ( " Sensor 2: {}" , AccelData :: from( accel_2) ) ;
305- // Accelerometers
306- // Lid Angle: 26 Deg
307- // Sensor 1: 00.00 X 00.00 Y 00.00 Z
308- // Sensor 2: 00.00 X 00.00 Y 00.00 Z
342+ println ! (
343+ " {:<12} {}" ,
344+ format!( "{:?} Sensor:" , accel_locations[ 0 ] ) ,
345+ AccelData :: from( accel_1)
346+ ) ;
347+ println ! (
348+ " {:<12} {}" ,
349+ format!( "{:?} Sensor:" , accel_locations[ 1 ] ) ,
350+ AccelData :: from( accel_2)
351+ ) ;
309352 }
310353}
311354
0 commit comments