Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions FrameworkSensors/Clients.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,27 @@ typedef class _SimpleDeviceOrientationDevice : public _ComboDevice
NTSTATUS UpdateCachedThreshold();

} SimpleDeviceOrientationDevice, *PSimpleDeviceOrientationDevice;



//
// Hinge Angle ----------------------------------------------------------------
//
typedef class _HingeAngleDevice : public _ComboDevice
{
private:
typedef struct _HingeAngleSample
{
FLOAT Angle;
} HingeAngleSample;

HingeAngleSample m_CachedThresholds;
HingeAngleSample m_CachedData;
HingeAngleSample m_LastSample;

public:
NTSTATUS Initialize(_In_ WDFDEVICE Device, _In_ SENSOROBJECT SensorObj);
NTSTATUS GetData(_In_ HANDLE Device);
NTSTATUS UpdateCachedThreshold();

} HingeAngleDevice, *PHingeAngleDevice;
154 changes: 107 additions & 47 deletions FrameworkSensors/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,69 +21,49 @@

#include "Device.tmh"

#define ENABLE_ALS_SENSOR 0
#define ENABLE_ORIENTATION_SENSOR 0
#define ENABLE_ACCEL_SENSOR 1

//---------------------------------------
// Declare and map devices below
// Dynamic sensor detection
//---------------------------------------
enum Device
{
#if ENABLE_ALS_SENSOR
Device_Als,
#endif
#if ENABLE_ORIENTATION_SENSOR
Device_SimpleDeviceOrientation,
#endif
#if ENABLE_ACCEL_SENSOR
Device_Accelerometer,
#endif
// Keep this last
Device_Count
};

static const ULONG SensorInstanceCount = Device_Count;
static SENSOROBJECT SensorInstancesBuffer[SensorInstanceCount]; // Global buffer to avoid allocate and free
#define MAX_SENSOR_COUNT 4

typedef enum {
SENSOR_KIND_ACCELEROMETER,
SENSOR_KIND_HINGEANGLE,
SENSOR_KIND_ALS,
} SensorKind;

static ULONG SensorInstanceCount = 0;
static SENSOROBJECT SensorInstancesBuffer[MAX_SENSOR_COUNT];

static size_t SensorSizes[MAX_SENSOR_COUNT];
static SensorKind SensorKinds[MAX_SENSOR_COUNT];

inline size_t GetDeviceSizeAtIndex(
_In_ ULONG Index)
{
size_t result = 0;
switch (static_cast<Device>(Index))
if (Index < SensorInstanceCount)
{
#if ENABLE_ALS_SENSOR
case Device_Als: result = sizeof(AlsDevice); break;
#endif
#if ENABLE_ORIENTATION_SENSOR
case Device_SimpleDeviceOrientation:result = sizeof(SimpleDeviceOrientationDevice); break;
#endif
#if ENABLE_ACCEL_SENSOR
case Device_Accelerometer: result = sizeof(AccelerometerDevice); break;
#endif
default: break; // invalid
return SensorSizes[Index];
}
return result;
return 0;
}

void AllocateDeviceAtIndex(
_In_ ULONG Index,
_Inout_ PComboDevice* ppDevice
)
{
switch (static_cast<Device>(Index))
if (Index >= SensorInstanceCount)
{
return;
}

switch (SensorKinds[Index])
{
#if ENABLE_ALS_SENSOR
case Device_Als: *ppDevice = new(*ppDevice) AlsDevice; break;
#endif
#if ENABLE_ORIENTATIONACCEL_SENSOR
case Device_SimpleDeviceOrientation:*ppDevice = new(*ppDevice) SimpleDeviceOrientationDevice; break;
#endif
#if ENABLE_ACCEL_SENSOR
case Device_Accelerometer: *ppDevice = new(*ppDevice) AccelerometerDevice; break;
#endif

default: break; // invalid (let driver fail)
case SENSOR_KIND_ACCELEROMETER: *ppDevice = new(*ppDevice) AccelerometerDevice; break;
case SENSOR_KIND_HINGEANGLE: *ppDevice = new(*ppDevice) HingeAngleDevice; break;
case SENSOR_KIND_ALS: *ppDevice = new(*ppDevice) AlsDevice; break;
default: break;
}
}

Expand Down Expand Up @@ -225,6 +205,86 @@ OnPrepareHardware(

SENSOR_FunctionEnter();

//
// Dynamic sensor detection
//
SensorInstanceCount = 0;

// Detect accelerometer + hinge angle via motion sensor presence
{
UINT8 acc_status = 0;
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
if (acc_status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)
{
UINT8 base = 0, lid = 0;
if (NT_SUCCESS(CrosEcGetAccelIndeces(Handle, &base, &lid)))
{
TraceInformation("COMBO %!FUNC! Detected accelerometer (base=%d, lid=%d)", base, lid);
SensorSizes[SensorInstanceCount] = sizeof(AccelerometerDevice);
SensorKinds[SensorInstanceCount] = SENSOR_KIND_ACCELEROMETER;
SensorInstanceCount++;

TraceInformation("COMBO %!FUNC! Detected hinge angle (base+lid accels present)");
SensorSizes[SensorInstanceCount] = sizeof(HingeAngleDevice);
SensorKinds[SensorInstanceCount] = SENSOR_KIND_HINGEANGLE;
SensorInstanceCount++;
}
}
}

// Detect ALS via motion sensor enumeration, then fallback to memmap
{
BOOLEAN alsFound = FALSE;
UINT8 sensorCount = CrosEcGetMotionSensorCount(Handle);
for (UINT8 idx = 0; idx < sensorCount && !alsFound; idx++)
{
EC_REQUEST_MOTION_SENSE_INFO infoReq{};
EC_RESPONSE_MOTION_SENSE_INFO infoRes{};

infoReq.Cmd = 1;
infoReq.SensorNum = idx;
if (0 != CrosEcSendCommand(
Handle,
EC_CMD_MOTION_SENSE_CMD,
1,
&infoReq,
sizeof(infoReq),
&infoRes,
sizeof(infoRes)))
{
if (infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT ||
infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT_RGB)
{
TraceInformation("COMBO %!FUNC! Detected ALS via motion sensor index %d", idx);
alsFound = TRUE;
}
}
}

// Fallback: check EC_MEMMAP_ALS for non-zero value
if (!alsFound)
{
UINT8 alsVal[2] = {0};
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 0, &alsVal[0]);
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 1, &alsVal[1]);
UINT16 alsReading = alsVal[0] + (alsVal[1] << 8);
if (alsReading != 0)
{
TraceInformation("COMBO %!FUNC! Detected ALS via memmap (reading=%d)", alsReading);
alsFound = TRUE;
}
}

if (alsFound && SensorInstanceCount < MAX_SENSOR_COUNT)
{
SensorSizes[SensorInstanceCount] = sizeof(AlsDevice);
SensorKinds[SensorInstanceCount] = SENSOR_KIND_ALS;
SensorInstanceCount++;
}
}

TraceInformation("COMBO %!FUNC! Detected %d sensor(s) total", SensorInstanceCount);

for (ULONG Count = 0; Count < SensorInstanceCount; Count++)
{
PComboDevice pDevice = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions FrameworkSensors/EcCommunication.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest);

#ifdef __cplusplus
}

// Motion sensor helpers (defined in AccelerometerClient.cpp)
UINT8 CrosEcGetMotionSensorCount(HANDLE Handle);
NTSTATUS CrosEcGetAccelIndeces(HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor);

#endif
2 changes: 1 addition & 1 deletion FrameworkSensors/FrameworkSensors.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" />
</ImportGroup>
<ItemGroup Label="WrappedTaskItems">
<ClCompile Include="AlsClient.cpp; Clients.cpp; device.cpp; driver.cpp; EcCommunication.cpp; SimpleDeviceOrientationClient.cpp; AccelerometerClient.cpp; ">
<ClCompile Include="AlsClient.cpp; Clients.cpp; device.cpp; driver.cpp; EcCommunication.cpp; SimpleDeviceOrientationClient.cpp; AccelerometerClient.cpp; HingeAngleClient.cpp; ">
<WppEnabled>true</WppEnabled>
<WppDllMacro>true</WppDllMacro>
<WppModuleName>FrameworkSensors</WppModuleName>
Expand Down
Loading
Loading