📄 leddim.c
字号:
**
** Side effects :
**
**--------------------------------------------------------------------------*/
static void /*RET Nothing */
TimerDimCancel( void ) /*IN Nothing */
{
if (timerDimHandle)
{
ZW_TIMER_CANCEL(timerDimHandle);
timerDimHandle = 0;
}
}
/*========================= TimerPollDimProtect =========================
** DIM_PROTECT time has past
**
** Side effects :
**
**--------------------------------------------------------------------------*/
static void /*RET Nothing */
TimerPollDimProtect( void ) /* IN Nothing */
{
pollDimProtect = TRUE;
}
/*=============================== DIMStart ==============================
** Start DIMming
**
** Side effects:
**
**--------------------------------------------------------------------------*/
static void /*RET Nothing */
DIMStart(
BYTE level, /* IN starting DIM level */
BYTE dimBits) /* IN DIMming config bits */
{
/* are we dimming allready ? */
if (dimmerNodeStatus.status & M_DS_CHANGING_DIM)
{
DIMStop(FALSE);
}
dimmerNodeStatus.level = level;
ValidateLevel();
dimmerNodeStatus.status |= M_DS_ON_OFF; /* Now ON */
/* Save new status in EEPROM */
ZW_MEM_PUT_BYTE(EEOFFSET_STATUS, dimmerNodeStatus.status);
if (dimBits == DIM_TOGGLE)
{
dimmerNodeStatus.status ^= M_DS_UP_DOWN; /* Toggle DIM direction */
dimmerNodeStatus.status |= M_DS_ROLLOVER; /* Make sure its set */
}
else
{
dimmerNodeStatus.status &= ~(M_DS_UP_DOWN | M_DS_ROLLOVER);
dimmerNodeStatus.status |= dimBits & (M_DS_UP_DOWN | M_DS_ROLLOVER);
}
dimmerNodeStatus.status |= M_DS_CHANGING_DIM; /* Indicate DIMming... */
TimerDimCancel(); /* Cancel timer if needed... */
/* Allocate timer for calling TimerChangeLevel every DIM_TIMER_FREQ, forever */
timerDimHandle = ZW_TIMER_START(TimerChangeLevel, DIM_TIMER_FREQ, FOREVER);
}
/*=============================== DIMStop ==============================
** Stop DIMming
**
** Side effects:
**
**--------------------------------------------------------------------------*/
static void /*RET Nothing */
DIMStop(
BYTE saveInE2PROM) /* IN Should the current level be saved in EEPROM */
{
TimerDimCancel(); /* Cancel timer if needed... */
dimmerNodeStatus.status &= ~(M_DS_CHANGING_DIM); /* STOP DIMming */
ValidateLevel();
if (saveInE2PROM)
{
ZW_MEM_PUT_BYTE(EEOFFSET_LEVEL, dimmerNodeStatus.level); /* Save the last value */
// ZW_MEM_PUT_BYTE(EEOFFSET_STATUS, dimmerNodeStatus.status);
}
}
/*============================= SetLightLevel ============================
** Set the light level to:
**
** CURRENT_LEVEL - The current light level stored in eeprom
** LEVEL - Use parameter bLevel as light level
** TOGGLE - Toggle the light level
** ON - Full on
** OFF - Off
**
**--------------------------------------------------------------------------*/
static void /*RET Nothing */
SetLightLevel(
BYTE bAction, /* IN Action to be done */
BYTE bLevel) /* IN Lightlevel to set */
{
if (dimmerNodeStatus.status & M_DS_CHANGING_DIM) /* If we're dimming */
{
DIMStop(FALSE); /* then STOP that */
}
switch (bAction)
{
case TOGGLE:
if ((dimmerNodeStatus.status & M_DS_ON_OFF) == 0) /* Is switch OFF ? */
/* C51 don't like recursive functions */
{
dimmerNodeStatus.status |= M_DS_ON_OFF;
LEDUpdate();
}
else
{
dimmerNodeStatus.status &= ~(M_DS_ON_OFF);
LEDallOFF(); /* Just do it! */
}
break;
case LEVEL:
if (bLevel == SWITCHED_ON)
/* C51 don't like recursive functions */
{
dimmerNodeStatus.level = LEVEL_MAX;
dimmerNodeStatus.status |= M_DS_ON_OFF;
ZW_MEM_PUT_BYTE(EEOFFSET_LEVEL, dimmerNodeStatus.level);
LEDallON(); /* Just do it! */
}
else if (bLevel == SWITCHED_OFF)
{
dimmerNodeStatus.status &= ~(M_DS_ON_OFF);
LEDallOFF(); /* Just do it! */
}
else
{
dimmerNodeStatus.level = bLevel;
dimmerNodeStatus.status |= M_DS_ON_OFF;
ZW_MEM_PUT_BYTE(EEOFFSET_LEVEL, dimmerNodeStatus.level);
LEDUpdate();
}
break;
case CURRENT_LEVEL:
dimmerNodeStatus.status |= M_DS_ON_OFF;
LEDUpdate();
break;
case ON:
dimmerNodeStatus.level = LEVEL_MAX;
dimmerNodeStatus.status |= M_DS_ON_OFF;
ZW_MEM_PUT_BYTE(EEOFFSET_LEVEL, dimmerNodeStatus.level);
LEDallON(); /* Just do it! */
break;
case OFF:
dimmerNodeStatus.status &= ~(M_DS_ON_OFF);
LEDallOFF(); /* Just do it! */
break;
}
/* Save new status in EEPROM */
ZW_MEM_PUT_BYTE(EEOFFSET_STATUS, dimmerNodeStatus.status);
}
/*============================= ApplicationPoll =========================
** Application poll function for LED dimmer
**
** Side effects:
**
**--------------------------------------------------------------------------*/
void /*RET Nothing */
ApplicationPoll( void ) /* IN Nothing */
{
BYTE lastAction = OneButtonLastAction();
ZW_DEBUG_CMD_POLL();
if (lastAction)
{
switch (pollState)
{
case POLLIDLE:
if ( (lastAction==BUTTON_WAS_PRESSED) ||
(lastAction==BUTTON_IS_HELD) )
{
buttonPress = 1; /* Button is now pressed once... */
TimerPollCancel(); /* Cancel timer if needed... */
timerPollHandle = ZW_TIMER_START(TimerPollDimProtect, DIM_PROTECT, ONE_SHOT);
pollDimProtect = FALSE;
if (protected_On_Off == PROTECTION_OFF)
{
pollState = POLLDIMPROTECT;
toggleDone = LIGHTIDLE; /* Nothing has been done with the light */
}
else
{
pollState = POLLCHILDHANDLING;
toggleDone = LIGHTNOP; /* Don't do anything */
}
}
break;
case POLLDIMPROTECT :
if (pollDimProtect) /* has dim protect time passed */
{
pollDimProtect = FALSE; /* Now seen */
toggleDone = LIGHTDIM; /* Now dimming has been started */
DIMStart(dimmerNodeStatus.level, DIM_TOGGLE);
pollState = POLLLEARNMODE;
}
break;
case POLLCHILDHANDLING:
if (pollDimProtect)
{
toggleDone = LIGHTNOP; /* Do nothing when button is released just reset state */
pollState = POLLLEARNMODE; /* Just wait for button to be released*/
}
else
{
if (lastAction==BUTTON_WAS_PRESSED) /* Has the button been pressed shortly*/
{
/*Count button presses*/
if (buttonPress++ < CHILD_PROTECT_COUNT)
{
if (timerPollHandle)
{
ZW_TIMER_RESTART(timerPollHandle);
}
}
else
{
pollState = POLLLEARNMODE;
TimerPollCancel(); /* Cancel timer if needed... */
/* Toggle the light level on/off each time the button is pressed */
toggleDone = LIGHTTOGGLE; /* Now the light has been toggled */
pollDimProtect = TRUE;
SetLightLevel(TOGGLE, LEVEL_DONTCARE);
}
}
}
break;
case POLLLEARNMODE:
/* Just wait... */
break;
}
}
else
{
if (buttonPress) /* Has it been pressed... */
{
if (toggleDone == LIGHTIDLE)
{
buttonPress = 0;
pollState = POLLIDLE;
SetLightLevel(TOGGLE, LEVEL_DONTCARE);
toggleDone = LIGHTTOGGLE;
}
else
{
if (toggleDone == LIGHTDIM)
{
DIMStop(TRUE);
}
}
if (protected_On_Off == PROTECTION_OFF)
{
buttonPress = 0;
pollState = POLLIDLE;
TimerPollCancel(); /* Cancel timer if needed... */
/* Button was released again, Start learn mode */
StartLearnModeNow();
}
else
{
if (pollDimProtect == TRUE)
{
/*Button was NOT hit again within Dim protect time*/
pollState = POLLIDLE;
TimerPollCancel(); /* Cancel timer if needed... */
/* Button was released again - If childprotect count execced, Start learn mode */
if (buttonPress >= CHILD_PROTECT_COUNT)
{
StartLearnModeNow();
}
buttonPress = 0;
}
}
}
}
}
/*============================== SendReport ==============================
**
** Function: SendReport
**
** Sends a report frame to source node.
**
** Side effects: None
**
**--------------------------------------------------------------------------*/
void /*RET Nothing */
SendReport(
BYTE sourceNode, /* IN Source node - to whom report is to be send */
BYTE classcmd, /* IN Command class, which the report belongs to */
BYTE reportparam) /* IN paramter to include in report frame */
{
txBuf.ZW_BasicReportFrame.cmdClass = classcmd;
/* Report command definition - BASIC/ALL/PROTECTION/MULTI report use the same value ;-) */
txBuf.ZW_BasicReportFrame.cmd = BASIC_REPORT;
txBuf.ZW_BasicReportFrame.value = reportparam;
/* Size of Report (BASIC/ALL/PROTECTION/MULTI GET response) frame are equal for BASIC/ALL/PROTECTION/MULTI */
ZW_SEND_DATA(sourceNode, (BYTE *)&txBuf, sizeof(txBuf.ZW_BasicReportFrame), txOption, NULL);
}
#if defined(ZW010x) || defined(ZW020x) || defined(ZW030x)
/*=========================== SendTestReport ============================
** Send current Powerlevel test results
**
** This is an application function example
**
**--------------------------------------------------------------------------*/
void
SendTestReport( void )
{
txBuf.ZW_PowerLevelTestNodeReportFrame.cmdClass = COMMAND_CLASS_POWERLEVEL;
txBuf.ZW_PowerLevelTestNodeReportFrame.cmd = POWERLEVEL_TEST_NODE_REPORT;
txBuf.ZW_PowerLevelTestNodeReportFrame.nodeId = testNodeID;
txBuf.ZW_PowerLevelTestNodeReportFrame.testStatus = testState;
txBuf.ZW_PowerLevelTestNodeReportFrame.testFrameCountMsb = (BYTE)(*((BYTE*)&testFrameSuccessCount));
txBuf.ZW_PowerLevelTestNodeReportFrame.testFrameCountLsb = (BYTE)*(&testFrameSuccessCount);
/* Send Report - we do not care if it gets there or not - if needed report can be requested again */
ZW_SEND_DATA(testSourceNodeID, (BYTE *)&txBuf, sizeof(txBuf.ZW_PowerLevelTestNodeReportFrame), txOption, NULL);
}
/*============================= SendTestDone ============================
** Test frame has been transmitted to DUT and the result is noted for
** later reporting. If not finished then another Test frame is transmitted.
** If all test frames has been transmitted then the test is stopped
** and the final result is reported to the PowerlevelTest initiator.
**
** This is an application function example
**
**--------------------------------------------------------------------------*/
void
SendTestDone(
BYTE bStatus)
{
if (bStatus == TRANSMIT_COMPLETE_OK)
{
testFrameSuccessCount++;
}
if (testFrameCount && (--testFrameCount))
{
ZW_SEND_TEST_FRAME(testNodeID, testPowerLevel, SendTestDone);
}
else
{
if (testFrameSuccessCount)
{
testState = ZW_TEST_SUCCES;
}
else
{
testState = ZW_TEST_FAILED;
}
ZW_RF_POWERLEVEL_SET(currentPower); /* Back to previous setting */
SendTestReport();
}
}
/*======================= PowerLevelTimerCancel ==========================
** Cancels PowerLevel timer
**
** This is an application function example
**
**--------------------------------------------------------------------------*/
void
PowerLevelTimerCancel( void )
{
ZW_TIMER_CANCEL(timerPowerLevelHandle);
timerPowerLevelHandle = 0;
}
/*=============================== StartTest ==============================
** Timer callback which maked sure that the RF transmit powerlevel is set
** back to normalPower after the designated time period.
**
** This is an application function example
**
**--------------------------------------------------------------------------*/
void
PowerLevelTimeout( void )
{
if (!--timerPowerLevelSec)
{
ZW_RF_POWERLEVEL_SET(normalPower); /* Reset powerlevel to normalPower */
PowerLevelTimerCancel();
}
}
/*=============================== StartTest ==============================
** Start the powerlevel test run
**
** This is an application function example
**
**--------------------------------------------------------------------------*/
void
StartTest(void)
{
testState = ZW_TEST_INPROGRESS;
currentPower = ZW_RF_POWERLEVEL_GET(); /* Get current (normalPower) */
ZW_RF_POWERLEVEL_SET(testPowerLevel);
ZW_SEND_TEST_FRAME(testNodeID, testPowerLevel, SendTestDone);
}
#endif
/*======================== ApplicationCommandHandler ====================
** Handling of a received application commands and requests
**
** This is an application function example
**
**--------------------------------------------------------------------------*/
void /*RET Nothing */
ApplicationCommandHandler(
BYTE rxStatus, /* IN Frame header info */
BYTE sourceNode, /* IN Command sender Node ID */
ZW_APPLICATION_TX_BUFFER *pCmd, /* IN Payload from the received frame, the union */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -