@@ -66,6 +66,73 @@ UINT8 CrosEcGetMotionSensorCount(HANDLE Handle)
6666 return res.SensorCount ;
6767}
6868
69+ // Returns STATUS_NOT_FOUND if either base or lid accelerometer sensors are not found.
70+ NTSTATUS
71+ CrosEcGetAccelIndeces (HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor)
72+ {
73+ EC_REQUEST_MOTION_SENSE_INFO req{};
74+ EC_RESPONSE_MOTION_SENSE_INFO res{};
75+ BOOLEAN FoundBase = FALSE ;
76+ BOOLEAN FoundLid = FALSE ;
77+ UINT8 SensorCount = 0 ;
78+
79+ if (Handle == INVALID_HANDLE_VALUE) {
80+ TraceError (" %!FUNC! Handle is invalid" );
81+ return STATUS_INVALID_HANDLE;
82+ }
83+
84+ if (BaseSensor == nullptr || LidSensor == nullptr )
85+ {
86+ TraceError (" %!FUNC! Invalid BaseSensor or LidSensor pointer" );
87+ return STATUS_INVALID_PARAMETER;
88+ }
89+
90+ SensorCount = CrosEcGetMotionSensorCount (Handle);
91+
92+ for (UINT8 i = 0 ; i < SensorCount; i++)
93+ {
94+ req.Cmd = 1 ;
95+ req.SensorNum = i;
96+ if (0 == CrosEcSendCommand (
97+ Handle,
98+ EC_CMD_MOTION_SENSE,
99+ 1 ,
100+ &req,
101+ sizeof (req),
102+ &res,
103+ sizeof (res)
104+ )) {
105+ TraceError (" %!FUNC! EC_CMD_MOTION_SENSE_INFO failed for sensor %d" , i);
106+ continue ;
107+ }
108+ if (res.SensorType != MOTION_SENSE_TYPE_ACCEL) {
109+ TraceError (" %!FUNC! Found sensor of type %d. Not Accelerometer - ignoring." , res.SensorType );
110+ continue ;
111+ }
112+
113+ switch (res.Location ) {
114+ case MOTION_SENSE_LOCATION_BASE:
115+ TraceInformation (" %!FUNC! Found base accel sensor at index: %d" , i);
116+ FoundBase = TRUE ;
117+ *BaseSensor = i;
118+ break ;
119+ case MOTION_SENSE_LOCATION_LID:
120+ TraceInformation (" %!FUNC! Found lid accel sensor at index: %d" , i);
121+ FoundLid = TRUE ;
122+ *LidSensor = i;
123+ break ;
124+ }
125+ }
126+
127+ if (!FoundBase || !FoundLid)
128+ {
129+ TraceError (" %!FUNC! Base or Lid accelerometer sensor not found" );
130+ return STATUS_NOT_FOUND;
131+ }
132+
133+ return STATUS_SUCCESS;
134+ }
135+
69136
70137// ------------------------------------------------------------------------------
71138// Function: Initialize
@@ -97,6 +164,9 @@ AccelerometerDevice::Initialize(
97164 m_Device = Device;
98165 m_SensorInstance = SensorInstance;
99166 m_Started = FALSE ;
167+ // Sensible defaults - applies to most devices
168+ m_LidSensorIndex = 0 ;
169+ m_LidBaseSensor = 1 ;
100170
101171 SensorCount = CrosEcGetMotionSensorCount (Context->m_CrosEcHandle );
102172 TraceInformation (" %!FUNC! Found %d Sensors on this device" , SensorCount);
@@ -107,6 +177,13 @@ AccelerometerDevice::Initialize(
107177 goto Exit;
108178 }
109179
180+ Status = CrosEcGetAccelIndeces (Context->m_CrosEcHandle , &m_LidSensorIndex, &m_LidBaseSensor);
181+ if (!NT_SUCCESS (Status))
182+ {
183+ TraceError (" %!FUNC! Failed to get accelerometer indeces: %!STATUS!" , Status);
184+ goto Exit;
185+ }
186+
110187 //
111188 // Create Lock
112189 //
@@ -451,13 +528,17 @@ AccelerometerDevice::GetData(
451528 UINT16 lid_angle = lid_angle_bytes[0 ] + (lid_angle_bytes[1 ] << 8 );
452529 TraceInformation (" Lid Angle Status: %dDeg%s" , lid_angle, lid_angle == 500 ? " (Unreliable)" : " " );
453530
531+ // Lid accelerometer is relevant for screen rotation
532+ // Base accelerometer is not used in this driver
533+ // It's only used for lid angle in the EC firmware
534+ UINT SensorOffset = 6 * m_LidSensorIndex + EC_MEMMAP_ACC_DATA + 2 ;
454535 UINT16 Sensor1[6 ] = {0 };
455- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 2 , (UINT8*)&Sensor1[0 ]);
456- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 3 , (UINT8*)&Sensor1[1 ]);
457- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 4 , (UINT8*)&Sensor1[2 ]);
458- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 5 , (UINT8*)&Sensor1[3 ]);
459- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 6 , (UINT8*)&Sensor1[4 ]);
460- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 7 , (UINT8*)&Sensor1[5 ]);
536+ CrosEcReadMemU8 (Handle, SensorOffset , (UINT8*)&Sensor1[0 ]);
537+ CrosEcReadMemU8 (Handle, SensorOffset + 1 , (UINT8*)&Sensor1[1 ]);
538+ CrosEcReadMemU8 (Handle, SensorOffset + 2 , (UINT8*)&Sensor1[2 ]);
539+ CrosEcReadMemU8 (Handle, SensorOffset + 3 , (UINT8*)&Sensor1[3 ]);
540+ CrosEcReadMemU8 (Handle, SensorOffset + 4 , (UINT8*)&Sensor1[4 ]);
541+ CrosEcReadMemU8 (Handle, SensorOffset + 5 , (UINT8*)&Sensor1[5 ]);
461542 m_CachedData.Axis .X = (float ) (Sensor1[0 ] + (Sensor1[1 ] << 8 ));
462543 m_CachedData.Axis .Y = (float ) (Sensor1[2 ] + (Sensor1[3 ] << 8 ));
463544 m_CachedData.Axis .Z = (float ) (Sensor1[4 ] + (Sensor1[5 ] << 8 ));
0 commit comments