📄 nled.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
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 <bsp.h>
#include <DrvLib.h>
BOOL InitializeAddresses(VOID);
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;
HANDLE gLEDThread;
HANDLE gLEDEvent;
DWORD gLEDTimeout;
#define NUM_LEDS (2)
//#define DBGNLED 1
#define DBGNLED 0 // 0 by shin...0627
// Pointer to device control registers
volatile static S3C6410_GPIO_REG *v_pGPIORegs = NULL;
NLED_SETTINGS_INFO g_ShadowSettingsInfo[NUM_LEDS];
CRITICAL_SECTION g_Lock; // Protects g_ShadowSettingsInfo
const struct NLED_SUPPORTS_INFO g_LEDSupportsInfo[NUM_LEDS] = {
// 0 - LED
{
0, // LedNum
250000, // lCycleAdjust
FALSE, // fAdjustTotalCycleTime
FALSE, // fAdjustOnTime
FALSE, // 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
}
};
BOOL InitializeAddresses(VOID)
{
BOOL RetValue = TRUE;
v_pGPIORegs = (volatile S3C6410_GPIO_REG *)DrvLib_MapIoSpace(S3C6410_BASE_REG_PA_GPIO, sizeof(S3C6410_GPIO_REG), FALSE);
if (v_pGPIORegs == NULL)
{
RETAILMSG(1, (TEXT("[NLED:ERR] v_pGPIORegs DrvLib_MapIoSpace() Failed!\r\n")));
return FALSE;
}
InitializeCriticalSection(&g_Lock);
return(RetValue);
}
void NLED_Thread(void)
{
bool LED_Blink=0;
gLEDTimeout = INFINITE;
gLEDEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
for (;;)
{
WaitForSingleObject(gLEDEvent, gLEDTimeout);
if (gLEDTimeout == 250)
{
LED_Blink ^= 1;
if (LED_Blink == TRUE)
v_pGPIORegs->GPNDAT |= (0x1 << 13);
else
v_pGPIORegs->GPNDAT &= ~(0x1 << 13);
}
}
}
// 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")));
// RETAILMSG(1, (TEXT("[NLED:MSG] NLedDriverInitialize: invoked\r\n")));
RETAILMSG(0, (TEXT("[NLED:MSG] NLedDriverInitialize: invoked\r\n"))); // 0 by shin...0627
if (!InitializeAddresses())
return (FALSE);
v_pGPIORegs->GPNDAT &= ~(0x1 << 13);
v_pGPIORegs->GPNCON &= ~(0x3 << 26);
v_pGPIORegs->GPNCON |= (0x1 << 26);
gLEDThread= CreateThread(0, 0, (LPTHREAD_START_ROUTINE) NLED_Thread, 0, 0, 0);
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")));
RETAILMSG(1, (TEXT("[NLED:MSG] NLedDriverDeInitialize: invoked\r\n")));
if (v_pGPIORegs != NULL)
{
DrvLib_UnmapIoSpace((PVOID)v_pGPIORegs);
v_pGPIORegs = NULL;
}
if(gLEDThread)
{
CloseHandle(gLEDThread);
gLEDThread = NULL;
}
if(gLEDEvent)
{
CloseHandle(gLEDEvent);
gLEDEvent = NULL;
}
DeleteCriticalSection(&g_Lock);
return (TRUE);
}
void GetLEDInfoFromHardware(const UINT LedNum, NLED_SETTINGS_INFO *pInfo)
{
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] ++GetLEDInfoFromHardware\r\n")));
EnterCriticalSection(&g_Lock);
memcpy(pInfo, &g_ShadowSettingsInfo[LedNum], sizeof(*pInfo));
LeaveCriticalSection(&g_Lock);
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] --GetLEDInfoFromHardware\r\n")));
}
BOOL SetLEDInfoToHardware(const NLED_SETTINGS_INFO *pInfo)
{
UINT LedNum = pInfo->LedNum;
LONG OnTime = pInfo->OnTime; // Convert from units of 1ms to units of 100ms
LONG OffTime = pInfo->OffTime; // Convert from units of 1ms to units of 100ms
INT OffOnBlink = pInfo->OffOnBlink;
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] ++SetLEDInfoToHardware\r\n")));
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] LednNum=%d, pInfo->OnTime=%d, pInfo->OffTime=%d, pInfo->OffOnBlink=%d\r\n"),pInfo->LedNum, pInfo->OnTime, pInfo->OffTime, 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;
}
// 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);
memcpy(&g_ShadowSettingsInfo[LedNum], pInfo, sizeof(*pInfo));
g_ShadowSettingsInfo[LedNum].TotalCycleTime = pInfo->OnTime + pInfo->OffTime;
if(LedNum == 0)
{
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] Chanage LED 0 Configuration\r\n")));
if(OffOnBlink == 0) // Led Off
{
gLEDTimeout = INFINITE;
v_pGPIORegs->GPNDAT &= ~(0x1 << 13);
}
else if(OffOnBlink == 1) // Led On
{
gLEDTimeout = INFINITE;
v_pGPIORegs->GPFDAT |= (0x1 << 13);
}
else if(OffOnBlink == 2) // Led Blink
{
gLEDTimeout = 250;
SetEvent(gLEDEvent);
}
else
{
RETAILMSG(DBGNLED,(TEXT("[NLED:ERR] LED0 Invalid Parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return (FALSE);
}
}
else if (LedNum == 1)
{
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] Chanage LED 1(Vibrator) Configuration\r\n")));
if(OffOnBlink == 0) //Vibrator off
{
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] Vibrator Off\r\n")));
}
else if(OffOnBlink == 1) //Vibrator On
{
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] Vibrator On\r\n")));
}
else
{
RETAILMSG(DBGNLED,(TEXT("[NLED:ERR] Vibrator Invalid Parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return (FALSE);
}
}
else
{
SetLastError(ERROR_INVALID_PARAMETER);
return (FALSE);
}
LeaveCriticalSection(&g_Lock);
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] --SetLEDInfoToHardware\r\n")));
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"));
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] ++NLedDriverGetDeviceInfo\r\n")));
if ( nInfoId == NLED_COUNT_INFO_ID ) {
struct NLED_COUNT_INFO *p = (struct NLED_COUNT_INFO*)pOutput;
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] NLedDriverGetDeviceInfo:: NLED_COUNT_INFO_ID\r\n")));
__try {
p -> cLeds = NUM_LEDS;
}
__except(EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
fOk = FALSE;
}
} else if ( nInfoId == NLED_SUPPORTS_INFO_ID ) {
__try {
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] NLedDriverGetDeviceInfo:: NLED_SUPPORTS_INFO_ID\r\n")));
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;
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] NLedDriverGetDeviceInfo:: NLED_SETTINGS_INFO_ID\r\n")));
__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);
}
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] --NLedDriverGetDeviceInfo\r\n")));
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;
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] ++NLedDriverSetDevice\r\n")));
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) {
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] LedNum=%d, OnTime=%d, OffTime=%d, OffOnBlink=%d\r\n"),Info.LedNum, Info.OnTime, Info.OffTime, Info.OffOnBlink));
fOk = SetLEDInfoToHardware(&Info);
}
} else {
fOk = FALSE;
SetLastError(ERROR_INVALID_PARAMETER);
}
RETAILMSG(DBGNLED,(TEXT("[NLED:MSG] --NLedDriverSetDevice\r\n")));
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 + -