|
21 | 21 |
|
22 | 22 | #include "Device.tmh" |
23 | 23 |
|
24 | | -#define ENABLE_ALS_SENSOR 0 |
25 | | -#define ENABLE_ORIENTATION_SENSOR 0 |
26 | | -#define ENABLE_ACCEL_SENSOR 1 |
27 | | - |
28 | 24 | //--------------------------------------- |
29 | | -// Declare and map devices below |
| 25 | +// Dynamic sensor detection |
30 | 26 | //--------------------------------------- |
31 | | -enum Device |
32 | | -{ |
33 | | -#if ENABLE_ALS_SENSOR |
34 | | - Device_Als, |
35 | | -#endif |
36 | | -#if ENABLE_ORIENTATION_SENSOR |
37 | | - Device_SimpleDeviceOrientation, |
38 | | -#endif |
39 | | -#if ENABLE_ACCEL_SENSOR |
40 | | - Device_Accelerometer, |
41 | | -#endif |
42 | | - // Keep this last |
43 | | - Device_Count |
44 | | -}; |
45 | | - |
46 | | -static const ULONG SensorInstanceCount = Device_Count; |
47 | | -static SENSOROBJECT SensorInstancesBuffer[SensorInstanceCount]; // Global buffer to avoid allocate and free |
| 27 | +#define MAX_SENSOR_COUNT 4 |
| 28 | + |
| 29 | +typedef enum { |
| 30 | + SENSOR_KIND_ACCELEROMETER, |
| 31 | + SENSOR_KIND_ALS, |
| 32 | +} SensorKind; |
| 33 | + |
| 34 | +static ULONG SensorInstanceCount = 0; |
| 35 | +static SENSOROBJECT SensorInstancesBuffer[MAX_SENSOR_COUNT]; |
| 36 | + |
| 37 | +static size_t SensorSizes[MAX_SENSOR_COUNT]; |
| 38 | +static SensorKind SensorKinds[MAX_SENSOR_COUNT]; |
48 | 39 |
|
49 | 40 | inline size_t GetDeviceSizeAtIndex( |
50 | 41 | _In_ ULONG Index) |
51 | 42 | { |
52 | | - size_t result = 0; |
53 | | - switch (static_cast<Device>(Index)) |
| 43 | + if (Index < SensorInstanceCount) |
54 | 44 | { |
55 | | -#if ENABLE_ALS_SENSOR |
56 | | - case Device_Als: result = sizeof(AlsDevice); break; |
57 | | -#endif |
58 | | -#if ENABLE_ORIENTATION_SENSOR |
59 | | - case Device_SimpleDeviceOrientation:result = sizeof(SimpleDeviceOrientationDevice); break; |
60 | | -#endif |
61 | | -#if ENABLE_ACCEL_SENSOR |
62 | | - case Device_Accelerometer: result = sizeof(AccelerometerDevice); break; |
63 | | -#endif |
64 | | - default: break; // invalid |
| 45 | + return SensorSizes[Index]; |
65 | 46 | } |
66 | | - return result; |
| 47 | + return 0; |
67 | 48 | } |
68 | 49 |
|
69 | 50 | void AllocateDeviceAtIndex( |
70 | 51 | _In_ ULONG Index, |
71 | 52 | _Inout_ PComboDevice* ppDevice |
72 | 53 | ) |
73 | 54 | { |
74 | | - switch (static_cast<Device>(Index)) |
| 55 | + if (Index >= SensorInstanceCount) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + switch (SensorKinds[Index]) |
75 | 61 | { |
76 | | -#if ENABLE_ALS_SENSOR |
77 | | - case Device_Als: *ppDevice = new(*ppDevice) AlsDevice; break; |
78 | | -#endif |
79 | | -#if ENABLE_ORIENTATIONACCEL_SENSOR |
80 | | - case Device_SimpleDeviceOrientation:*ppDevice = new(*ppDevice) SimpleDeviceOrientationDevice; break; |
81 | | -#endif |
82 | | -#if ENABLE_ACCEL_SENSOR |
83 | | - case Device_Accelerometer: *ppDevice = new(*ppDevice) AccelerometerDevice; break; |
84 | | -#endif |
85 | | - |
86 | | - default: break; // invalid (let driver fail) |
| 62 | + case SENSOR_KIND_ACCELEROMETER: *ppDevice = new(*ppDevice) AccelerometerDevice; break; |
| 63 | + case SENSOR_KIND_ALS: *ppDevice = new(*ppDevice) AlsDevice; break; |
| 64 | + default: break; |
87 | 65 | } |
88 | 66 | } |
89 | 67 |
|
@@ -259,6 +237,81 @@ OnPrepareHardware( |
259 | 237 | ); |
260 | 238 | } |
261 | 239 |
|
| 240 | + // |
| 241 | + // Dynamic sensor detection |
| 242 | + // |
| 243 | + SensorInstanceCount = 0; |
| 244 | + |
| 245 | + // Detect accelerometer + hinge angle via motion sensor presence |
| 246 | + { |
| 247 | + UINT8 acc_status = 0; |
| 248 | + CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status); |
| 249 | + if (acc_status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT) |
| 250 | + { |
| 251 | + UINT8 base = 0, lid = 0; |
| 252 | + if (NT_SUCCESS(CrosEcGetAccelIndeces(Handle, &base, &lid))) |
| 253 | + { |
| 254 | + TraceInformation("COMBO %!FUNC! Detected accelerometer (base=%d, lid=%d)", base, lid); |
| 255 | + SensorSizes[SensorInstanceCount] = sizeof(AccelerometerDevice); |
| 256 | + SensorKinds[SensorInstanceCount] = SENSOR_KIND_ACCELEROMETER; |
| 257 | + SensorInstanceCount++; |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + // Detect ALS via motion sensor enumeration, then fallback to memmap |
| 263 | + { |
| 264 | + BOOLEAN alsFound = FALSE; |
| 265 | + UINT8 sensorCount = CrosEcGetMotionSensorCount(Handle); |
| 266 | + for (UINT8 idx = 0; idx < sensorCount && !alsFound; idx++) |
| 267 | + { |
| 268 | + EC_REQUEST_MOTION_SENSE_INFO infoReq{}; |
| 269 | + EC_RESPONSE_MOTION_SENSE_INFO infoRes{}; |
| 270 | + |
| 271 | + infoReq.Cmd = 1; |
| 272 | + infoReq.SensorNum = idx; |
| 273 | + if (0 != CrosEcSendCommand( |
| 274 | + Handle, |
| 275 | + EC_CMD_MOTION_SENSE_CMD, |
| 276 | + 1, |
| 277 | + &infoReq, |
| 278 | + sizeof(infoReq), |
| 279 | + &infoRes, |
| 280 | + sizeof(infoRes))) |
| 281 | + { |
| 282 | + if (infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT || |
| 283 | + infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT_RGB) |
| 284 | + { |
| 285 | + TraceInformation("COMBO %!FUNC! Detected ALS via motion sensor index %d", idx); |
| 286 | + alsFound = TRUE; |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + // Fallback: check EC_MEMMAP_ALS for non-zero value |
| 292 | + if (!alsFound) |
| 293 | + { |
| 294 | + UINT8 alsVal[2] = {0}; |
| 295 | + CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 0, &alsVal[0]); |
| 296 | + CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 1, &alsVal[1]); |
| 297 | + UINT16 alsReading = alsVal[0] + (alsVal[1] << 8); |
| 298 | + if (alsReading != 0) |
| 299 | + { |
| 300 | + TraceInformation("COMBO %!FUNC! Detected ALS via memmap (reading=%d)", alsReading); |
| 301 | + alsFound = TRUE; |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + if (alsFound && SensorInstanceCount < MAX_SENSOR_COUNT) |
| 306 | + { |
| 307 | + SensorSizes[SensorInstanceCount] = sizeof(AlsDevice); |
| 308 | + SensorKinds[SensorInstanceCount] = SENSOR_KIND_ALS; |
| 309 | + SensorInstanceCount++; |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + TraceInformation("COMBO %!FUNC! Detected %d sensor(s) total", SensorInstanceCount); |
| 314 | + |
262 | 315 | for (ULONG Count = 0; Count < SensorInstanceCount; Count++) |
263 | 316 | { |
264 | 317 | PComboDevice pDevice = nullptr; |
|
0 commit comments