⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snled.cpp

📁 mx27 f14v2 源代码。包括ADS板上诸多驱动的源码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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().
//
// Parameters:
//      InputParm
//          [in] INT    nInfoId
//          [in] PVOID  pInput
//
// Returns:
//      This routine returns TRUE if successful, or FALSE if there's a problem
//
//-----------------------------------------------------------------------------
BOOL WINAPI NLedDriverSetDevice(INT nInfoId, PVOID pInput)
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("+NLedDriverSetDevice\r\n")));

    HANDLE hThread = NULL;
    UINT LedIndex;

    struct NLED_SETTINGS_INFO *pNledSettingsInfo = (struct NLED_SETTINGS_INFO*)pInput;

    if((nInfoId != NLED_SETTINGS_INFO_ID) || (pNledSettingsInfo->LedNum >= NLED_MAX_LED))
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("NLedDriverSetDevice: Invalid Parameters\r\n")));
        return FALSE;
    }

    LedIndex = pNledSettingsInfo->LedNum;
    g_NLedPddContext[LedIndex].NLedSettingsInfo.LedNum = pNledSettingsInfo->LedNum;
    g_NLedPddContext[LedIndex].NLedSettingsInfo.OffOnBlink = pNledSettingsInfo->OffOnBlink;

    if(g_NLedPddContext[LedIndex].bLEDBlinkState) //force to exit blink thread
    {
        SetEvent(g_NLedPddContext[LedIndex].hNLedBlinkThreadEvent);
        DEBUGMSG(ZONE_PDD, (TEXT("NLedDriverSetDevice: WaitForSingleObject - hNLedBlinkThreadStoppedEvent\r\n")));
        WaitForSingleObject(g_NLedPddContext[LedIndex].hNLedBlinkThreadStoppedEvent, INFINITE);
    }

    if(pNledSettingsInfo->OffOnBlink == 0) // OffOnBlink setting - OFF
    {
        TurnOffLED(LedIndex);
        g_NLedPddContext[LedIndex].bLEDState = FALSE;
    }
    else if(pNledSettingsInfo->OffOnBlink == 1) // OffOnBlink setting - ON
    {
        TurnOnLED( LedIndex);
        g_NLedPddContext[LedIndex].bLEDState = TRUE;
	}
    else if(pNledSettingsInfo->OffOnBlink == 2) // OffOnBlink setting - BLINK
    {
    	// Validation checking ...
        // Round up OnTime and OffTime values 1 millisecond
        if(pNledSettingsInfo->OnTime < 0 || pNledSettingsInfo->OffTime < 0)
        {
            return 0;
        }

        if(pNledSettingsInfo->MetaCycleOn < 0 ||pNledSettingsInfo->MetaCycleOff < 0)
        {
            return FALSE;
        }

		if(pNledSettingsInfo->TotalCycleTime<0)
        {
			return FALSE;
        }

		// check ok
        g_NLedPddContext[LedIndex].NLedSettingsInfo = *(pNledSettingsInfo);

        if(g_NLedPddContext[LedIndex].NLedSettingsInfo.OnTime < 1000)
            g_NLedPddContext[LedIndex].NLedSettingsInfo.OnTime = 1000;

        if(g_NLedPddContext[LedIndex].NLedSettingsInfo.OffTime < 1000)
            g_NLedPddContext[LedIndex].NLedSettingsInfo.OffTime = 1000;

        g_NLedPddContext[LedIndex].NLedSettingsInfo.TotalCycleTime =
            g_NLedPddContext[LedIndex].NLedSettingsInfo.OnTime + g_NLedPddContext[LedIndex].NLedSettingsInfo.OffTime;

        hThread = CreateThread(
                        NULL,                                           // Default thread security descriptor
                        0,                                              // Default stack size
                        LEDBlinkThread,                                 // Start routine
                        &g_NLedPddContext[LedIndex].NLedSettingsInfo,   // Start routine parameter
                        0,                                              // Run immediately
                        NULL                                            // Thread ID
                        );

        if (hThread == NULL)
        {
            g_NLedPddContext[LedIndex].bLEDBlinkState = FALSE;
            return FALSE;
        }

		CeSetThreadPriority(hThread, CeGetThreadPriority(hThread) - 1); // force the thread to run at once
        CloseHandle(hThread);
    }
    else
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("NLedDriverSetDevice: Invalid OffOnBlink\r\n")));
        return FALSE;
    }

    DEBUGMSG(ZONE_FUNCTION, (TEXT("-NLedDriverSetDevice\r\n")));

    return TRUE;
}

//-----------------------------------------------------------------------------
//
// Function: NLedDriverPowerDown
//
// 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.
//
// Parameters:
//      InputParm
//          [in] BOOL power_down
//
// Returns:
//          None
//
//-----------------------------------------------------------------------------
VOID WINAPI NLedDriverPowerDown(BOOL power_down)
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("+NLedDriverPowerDown\r\n")));

    for(int i=0; i<NLED_MAX_LED; i++)
    {
        if(power_down)
        {
            if(g_NLedPddContext[i].bLEDState == TRUE)
                TurnOffLED(i);  //Trun Off LED when LED is on
        }
        else
        {
            if(g_NLedPddContext[i].bLEDState == TRUE)
                TurnOnLED(i);  //restore LED to on
        }
    }

    DEBUGMSG(ZONE_FUNCTION, (TEXT("-NLedDriverPowerDown\r\n")));

    return;
}

//-----------------------------------------------------------------------------
//
// Function: TurnOnLED
//
// This routine is Turn On LED
//
// Parameters:
//      [in] DWORD LEDNum
//
// Returns:
//
//-----------------------------------------------------------------------------
void TurnOnLED(DWORD LEDNum)
{
    if(LEDNum == 0)
    {
        OUTREG16(&g_pPBC->BCTRL1_SET, CSP_BITFMASK(PBC_BCTRL1_LED_1));   // Turn On LED1
    }
    else if(LEDNum == 1)
    {
        OUTREG16(&g_pPBC->BCTRL1_SET, CSP_BITFMASK(PBC_BCTRL1_LED_2));  // Turn On LED2
    }
}

//-----------------------------------------------------------------------------
//
// Function: TurnOffLED
//
// This routine is Turn Off LED
//
// Parameters:
//      [in] DWORD LEDNum
//
// Returns:
//
//----------------------------------------------------------------------------
void TurnOffLED(DWORD LEDNum)
{
    if(LEDNum == 0)
    {
        OUTREG16(&g_pPBC->BCTRL1_CLEAR, CSP_BITFMASK(PBC_BCTRL1_LED_1));   // Turn Off LED1
    }
    else if(LEDNum == 1)
    {
        OUTREG16(&g_pPBC->BCTRL1_CLEAR, CSP_BITFMASK(PBC_BCTRL1_LED_2));   // Turn Off LED2
    }
}


//-----------------------------------------------------------------------------
//
// Function: NLedDriverSetDeviceThread
//
// This routine is invoked by CreateThread() function in NLedDriverSetDevice when
// caller attempts to change the configuration of a notification LED to 'Blink' mode
//
// Parameters:
//      InputParm
//          [in] LPVOID lParam
//
// Returns:
//      This routine returns 0
//
//-----------------------------------------------------------------------------
DWORD WINAPI LEDBlinkThread(LPVOID lParam)
{
    DEBUGMSG(ZONE_FUNCTION, (TEXT("+NLedDriverSetDeviceThread\r\n")));

    NLED_SETTINGS_INFO *pNledSettingsInfo = (struct NLED_SETTINGS_INFO*)lParam;
    UINT LedIndex = pNledSettingsInfo->LedNum;
    LONG lOnTimeOut = (DWORD)(pNledSettingsInfo->OnTime/1000); // to convert micro to milli-seconds
    LONG lOffTimeOut = (DWORD)(pNledSettingsInfo->OffTime/1000); // to convert micro to milli-seconds
	INT nMetaCycleOn = (DWORD)(pNledSettingsInfo->MetaCycleOn);
	INT nMetaCycleOff = (DWORD)(pNledSettingsInfo->MetaCycleOff);
	BOOL bForeverMetaCycleOn = (nMetaCycleOn==0);
	BOOL bForeverMetaCycleOff = (nMetaCycleOff==0);

	g_NLedPddContext[LedIndex].bLEDBlinkState = TRUE;

    for ( ; ; )
    {
        if(bForeverMetaCycleOn || --nMetaCycleOn>0)
		{
	        TurnOnLED(LedIndex);
    	    g_NLedPddContext[LedIndex].bLEDState = TRUE;

			if(WaitForSingleObject(g_NLedPddContext[LedIndex].hNLedBlinkThreadEvent, lOnTimeOut) == WAIT_OBJECT_0)
			{
				break;
			}
        }
        else	
			break;

        if(bForeverMetaCycleOff || --nMetaCycleOff>0)
		{
	        TurnOffLED(LedIndex);
	        g_NLedPddContext[LedIndex].bLEDState = FALSE;

	        if(WaitForSingleObject(g_NLedPddContext[LedIndex].hNLedBlinkThreadEvent, lOffTimeOut) == WAIT_OBJECT_0)
			{
	            break;
			}
        }
        else	
			break;
    }

    SetEvent(g_NLedPddContext[LedIndex].hNLedBlinkThreadStoppedEvent);

    g_NLedPddContext[LedIndex].bLEDBlinkState = FALSE;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("-NLedDriverSetDeviceThread\r\n")));

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -