📄 snled.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name:
Abstract:
Notes:
--*/
#include <windows.h>
#include <nled.h>
#include <led_drvr.h>
#include <types.h>
#include <ceddk.h>
#include <bsp.h>
typedef struct {
unsigned __int8 OnOffBlink; // 0=off, 1=on, 2=blink
unsigned __int8 OnTime; // in units of 100ms
unsigned __int8 OffTime; // in units of 100ms
} NotificationLED;
#define NUM_LEDS 2
volatile NotificationLED *g_NotificationLEDs; // array of NUM_LEDs NotificationLED structs
NLED_SETTINGS_INFO g_ShadowSettingsInfo[NUM_LEDS];
CRITICAL_SECTION g_Lock; // Protects g_NotificatioNLEDs and g_ShadowSettingsInfo
const struct NLED_SUPPORTS_INFO g_LEDSupportsInfo[NUM_LEDS] = {
// 0 - LED
{
0, // LedNum
100, // lCycleAdjust
FALSE, // fAdjustTotalCycleTime
TRUE, // fAdjustOnTime
TRUE, // fAdjustOffTime
FALSE, // fMetaCycleOn
FALSE // fMetaCycleOff
},
// 1 - Vibrate
{
1, // LedNum
-1, // lCycleAdjust (-1 indicates it is the Vibrate device)
FALSE, // fAdjustTotalCycleTime
FALSE, // fAdjustOnTime
FALSE, // fAdjustOffTime
FALSE, // fMetaCycleOn
FALSE // fMetaCycleOff
}
};
// The NLED MDD calls this routine to initialize the underlying NLED hardware.
// This routine should return TRUE if successful. If there's a problem
// it should return FALSE and call SetLastError() to pass back the reason
// for the failure.
BOOL WINAPI
NLedDriverInitialize(
VOID
)
{
DEBUGMSG(ZONE_PDD, (_T("NLedDriverInitialize: invoked\r\n")));
PHYSICAL_ADDRESS ioPhysicalBase = { BSP_BASE_REG_PA_LED, 0};
ULONG inIoSpace = 0;
if (HalTranslateBusAddress(Internal,0, ioPhysicalBase,&inIoSpace,&ioPhysicalBase)) {
// Map it if it is Memeory Mapped IO.
g_NotificationLEDs = (volatile NotificationLED *)MmMapIoSpace(ioPhysicalBase, sizeof(NotificationLED)*NUM_LEDS,FALSE);
if (g_NotificationLEDs == NULL) {
return FALSE;
}
InitializeCriticalSection(&g_Lock);
} else {
return FALSE;
}
return TRUE;
}
// The NLED MDD calls this routine to deinitialize the underlying NLED
// hardware as the NLED driver is unloaded. It should return TRUE if
// successful. If there's a problem this routine should return FALSE
// and call SetLastError() to pass back the reason for the failure.
BOOL WINAPI
NLedDriverDeInitialize(
VOID
)
{
DEBUGMSG(ZONE_PDD, (_T("NLedDriverDeInitialize: invoked\r\n")));
MmUnmapIoSpace((PVOID)g_NotificationLEDs, sizeof(NotificationLED)*NUM_LEDS);
DeleteCriticalSection(&g_Lock);
return (TRUE);
}
void GetLEDInfoFromHardware(const UINT LedNum, NLED_SETTINGS_INFO *pInfo)
{
EnterCriticalSection(&g_Lock);
memcpy(pInfo, &g_ShadowSettingsInfo[LedNum], sizeof(*pInfo));
LeaveCriticalSection(&g_Lock);
}
BOOL SetLEDInfoToHardware(const NLED_SETTINGS_INFO *pInfo)
{
UINT LedNum = pInfo->LedNum;
LONG OnTime = pInfo->OnTime / 100; // Convert from units of 1ms to units of 100ms
LONG OffTime = pInfo->OffTime / 100; // Convert from units of 1ms to units of 100ms
INT OffOnBlink = pInfo->OffOnBlink;
// Validate the pInfo fields
if (LedNum >= NUM_LEDS
|| OnTime < 0
|| OffTime < 0
|| OffOnBlink < 0
|| OffOnBlink > 2
|| (pInfo->MetaCycleOn != 0 && OnTime == 0) // allow MetaCycleOn=1 only if OnTime != 0
|| pInfo->MetaCycleOff != 0) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
// Validation is successful. Update the hardware and the shadow settings
// Restrict OnTime and OffTime to 0...255, which is what the emulator supports
if (OnTime > 255) {
OnTime = 255;
}
if (OffTime > 255) {
OffTime = 255;
}
// CE uses "Off" and "Blink" to represent the states of the Vibrate hardware.
// But the emulator uses Off and On. So convert Blink to On here.
if (LedNum == 1 && OffOnBlink == 2) {
OffOnBlink = 1;
}
// The order matters: the write to OnOffBlink should come at the end, as it
// is what triggers the emulator to update how it displays notifications.
EnterCriticalSection(&g_Lock);
g_NotificationLEDs[LedNum].OnTime = (unsigned __int8)OnTime;
g_NotificationLEDs[LedNum].OffTime = (unsigned __int8)OffTime;
g_NotificationLEDs[LedNum].OnOffBlink = (unsigned __int8)OffOnBlink;
memcpy(&g_ShadowSettingsInfo[LedNum], pInfo, sizeof(*pInfo));
g_ShadowSettingsInfo[LedNum].TotalCycleTime = pInfo->OnTime + pInfo->OffTime;
LeaveCriticalSection(&g_Lock);
return TRUE;
}
// This routine retrieves information about the NLED device(s) that
// this driver supports. The nInfoId parameter indicates what specific
// information is being queried and pOutput is a buffer to be filled in.
// The size of pOutput depends on the type of data being requested. This
// routine returns TRUE if successful, or FALSE if there's a problem -- in
// which case it also calls SetLastError() to pass back more complete
// error information. The NLED MDD invokes this routine when an application
// calls NLedGetDeviceInfo().
BOOL
WINAPI
NLedDriverGetDeviceInfo(
INT nInfoId,
PVOID pOutput // note: this is an IN/OUT parameter
)
{
BOOL fOk = TRUE;
SETFNAME(_T("NLedDriverGetDeviceInfo"));
if ( nInfoId == NLED_COUNT_INFO_ID ) {
struct NLED_COUNT_INFO *p = (struct NLED_COUNT_INFO*)pOutput;
__try {
p -> cLeds = NUM_LEDS;
}
__except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
fOk = FALSE;
}
} else if ( nInfoId == NLED_SUPPORTS_INFO_ID ) {
__try {
UINT LedNum = ((NLED_SUPPORTS_INFO*)pOutput)->LedNum;
if (LedNum < NUM_LEDS) {
memcpy(pOutput, &g_LEDSupportsInfo[LedNum], sizeof(NLED_SUPPORTS_INFO));
} else {
fOk = FALSE;
SetLastError(ERROR_INVALID_PARAMETER);
}
}
__except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
fOk = FALSE;
}
} else if ( nInfoId == NLED_SETTINGS_INFO_ID ) {
NLED_SETTINGS_INFO Info;
UINT LedNum;
__try {
LedNum = ((NLED_SETTINGS_INFO*)pOutput)->LedNum;
}
__except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
fOk = FALSE;
}
if (fOk) {
if (LedNum < NUM_LEDS) {
GetLEDInfoFromHardware(LedNum, &Info);
} else {
fOk = FALSE;
SetLastError(ERROR_INVALID_PARAMETER);
}
}
if (fOk) {
Info.LedNum = LedNum;
__try {
memcpy(pOutput, &Info, sizeof(Info));
}
__except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
fOk = FALSE;
}
}
} else {
fOk = FALSE;
SetLastError(ERROR_INVALID_PARAMETER);
}
DEBUGMSG(ZONE_PDD || (!fOk && ZONE_WARN),
(_T("%s: returning %d\r\n"), pszFname, fOk));
return (fOk);
}
// This routine changes the configuration of an LED. The nInfoId parameter
// indicates what kind of configuration information is being changed.
// Currently only the NLED_SETTINGS_INFO_ID value is supported. The pInput
// parameter points to a buffer containing the data to be updated. The size
// of the buffer depends on the value of nInfoId. This routine returns TRUE
// if successful or FALSE if there's a problem -- in which case it also calls
// SetLastError(). The NLED MDD invokes this routine when an application
// calls NLedSetDevice().
BOOL
WINAPI
NLedDriverSetDevice(
INT nInfoId,
PVOID pInput
)
{
BOOL fOk = TRUE;
if ( nInfoId == NLED_SETTINGS_INFO_ID ) {
struct NLED_SETTINGS_INFO Info;
__try {
memcpy(&Info, pInput, sizeof(Info));
}
__except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
fOk = FALSE;
}
if (fOk) {
fOk = SetLEDInfoToHardware(&Info);
}
} else {
fOk = FALSE;
SetLastError(ERROR_INVALID_PARAMETER);
}
return (fOk);
}
// This routine is invoked by the driver MDD when the system suspends or
// resumes. The power_down flag indicates whether the system is powering
// up or powering down.
VOID WINAPI
NLedDriverPowerDown(
BOOL power_down
)
{
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -