📄 lightbulb.c
字号:
/****************************************************************************
*
* MODULE: lightbulb.c
*
* COMPONENT: $RCSfile: LightBulb.c,v $
*
* VERSION: $Name: JN-AN-1002-1v4 $
*
* REVISION: $Revision: 1.2 $
*
* DATED: $Date: 2006/08/15 13:56:05 $
*
* STATUS: $State: Exp $
*
* AUTHOR: Ian Morris
*
* DESCRIPTION:
* Performs the function of the bulb in a light bulb switching system. Runs
* on the controller board of the evaluation kit and forms the PAN coordinator
* of the system. Once at least once light switch has joined the network the
* then coordinator waits for a data frame to arrive. Upon reception the state
* of the bulb (four LEDs on the controller board) is toggled.
*
* CHANGE HISTORY:
*
* $Log: LightBulb.c,v $ * Revision 1.2 2006/08/15 13:56:05 imorr * Updated so that extended address of associating nodes is stored correctly * * Revision 1.1 2006/08/15 13:22:52 imorr * Initial version *
*
*
* LAST MODIFIED BY: $Author: imorr $
* $Modtime: $
*
*
****************************************************************************
*
* (c) Copyright 2000 JENNIC Ltd
*
****************************************************************************/
/****************************************************************************/
/*** Include files ***/
/****************************************************************************/
#include <jendefs.h>
#include <AppHardwareApi.h>
#include <AppQueueApi.h>
#include <mac_sap.h>
#include <mac_pib.h>
#include <Utilities.h>
/****************************************************************************/
/*** Macro Definitions ***/
/****************************************************************************/
/* Light bulb LED control */
#define LIGHTBULB_LED_OUTPUTS_MASK 0x0003C000UL
#define LIGHTBULB_LED_1_MASK 0x00004000UL
#define LIGHTBULB_LED_2_MASK 0x00008000UL
#define LIGHTBULB_LED_3_MASK 0x00010000UL
#define LIGHTBULB_LED_4_MASK 0x00020000UL
#define OFF 0
#define ON 1
/* Network parameters */
#define PAN_ID 0x0401U
#define COORD_ADDR 0x0502U
/* Light switch device data */
#define MAX_LIGHT_SWITCHES 2
#define LIGHT_SWITCH_NODE_ADDR_BASE 0x1000U
/****************************************************************************/
/*** Type Definitions ***/
/****************************************************************************/
/* System states with respect to screen display being shown */
typedef enum
{
E_STATE_INIT,
E_STATE_START_COORDINATOR,
E_STATE_RUNNING_LIGHT_BULB_APP
} teState;
/* Used to track an association between extended address and short address */
typedef struct
{
uint32 u32ExtAddrLo;
uint32 u32ExtAddrHi;
uint16 u16ShortAddr;
} tsAssocNodes;
/* All application data with scope within the entire file is kept here,
including all stored node data, GUI settings and current state */
typedef struct
{
struct
{
tsAssocNodes asAssocNodes[MAX_LIGHT_SWITCHES];
uint8 u8AssociatedNodes;
} sNode;
struct
{
teState eState;
uint8 u8Channel;
} sSystem;
} tsCoordData;
/****************************************************************************/
/*** Local Function Prototypes ***/
/****************************************************************************/
/****************************************************************************/
/*** Exported Variables ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Variables ***/
/****************************************************************************/
/* Handles from the MAC */
PRIVATE void *s_pvMac;
PRIVATE MAC_Pib_s *s_psMacPib;
PRIVATE tsCoordData sCoordData;
PRIVATE bool_t bLightBulbState;
/****************************************************************************/
/*** Exported Functions ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Functions ***/
/****************************************************************************/
PRIVATE void vInitSystem(void);
PRIVATE bool_t bStartCoordinator(void);
PRIVATE void vHandleNodeAssociation(MAC_MlmeDcfmInd_s *psMlmeInd);
PRIVATE void vProcessEventQueues(void);
PRIVATE void vProcessIncomingMlme(MAC_MlmeDcfmInd_s *psMlmeInd);
PRIVATE void vProcessIncomingData(MAC_McpsDcfmInd_s *psMcpsInd);
PRIVATE void vProcessIncomingHwEvent(AppQApiHwInd_s *psAHI_Ind);
PRIVATE void vLightBulb(bool_t bState);
PRIVATE void vLightBulbInit(void);
/****************************************************************************
*
* NAME: AppColdStart
*
* DESCRIPTION:
* Runs application.
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* Entry point for a power on reset or wake from sleep mode.
****************************************************************************/
PUBLIC void AppColdStart(void)
{
/* General initialisation: set up MAC API */
vInitSystem();
vLightBulbInit();
sCoordData.sSystem.eState = E_STATE_INIT;
while (1)
{
vProcessEventQueues(); /* Process event(s) posted by interrupt(s) */
switch (sCoordData.sSystem.eState)
{
case E_STATE_INIT:
sCoordData.sSystem.u8Channel = 0;
sCoordData.sSystem.eState = E_STATE_START_COORDINATOR;
break;
case E_STATE_START_COORDINATOR:
if(bStartCoordinator())
{
vAHI_DioSetOutput(0, LIGHTBULB_LED_1_MASK);
sCoordData.sSystem.eState = E_STATE_RUNNING_LIGHT_BULB_APP;
}
break;
case E_STATE_RUNNING_LIGHT_BULB_APP:
vProcessEventQueues(); /* Process event(s) posted by interrupt(s) */
break;
}
}
}
/****************************************************************************
*
* NAME: AppWarmStart
*
* DESCRIPTION:
* Entry point for a wake from sleep mode with the memory contents held. We
* are not using this mode and so should never get here.
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PUBLIC void AppWarmStart(void)
{
AppColdStart();
}
/****************************************************************************
*
* NAME: vInitSystem
*
* DESCRIPTION:
* Initialises stack and hardware, sets non-default values in the 802.15.4
* PIB.
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PRIVATE void vInitSystem(void)
{
/* Initialise stack and hardware interfaces. We aren't using callbacks
at all, just monitoring the upward queues in a loop */
u32AppQApiInit(NULL, NULL, NULL);
u32AHI_Init();
/* Set up the MAC handles. Must be called AFTER u32AppQApiInit() */
s_pvMac = pvAppApiGetMacHandle();
s_psMacPib = MAC_psPibGetHandle(s_pvMac);
sCoordData.sNode.u8AssociatedNodes = 0;
/* Set Pan ID and short address in PIB (also sets match registers in hardware) */
MAC_vPibSetPanId(s_pvMac, PAN_ID);
MAC_vPibSetShortAddr(s_pvMac, COORD_ADDR);
s_psMacPib->bAssociationPermit = 1;
MAC_vPibSetRxOnWhenIdle(s_pvMac, TRUE, FALSE);
}
/****************************************************************************
*
* NAME: vLightBulbInit
*
* DESCRIPTION:
* Initialises state of light bulb, configure DIO lines that drive the LEDs
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PRIVATE void vLightBulbInit(void)
{
/* Light bulb simulation uses LEDs D1-4 that are connected to GPIO14-17.
Set these IO lines to outputs and then set them high to turn the LEDs
off. */
vAHI_DioSetDirection(0, LIGHTBULB_LED_OUTPUTS_MASK);
vLightBulb(OFF);
bLightBulbState = OFF;
}
/****************************************************************************
*
* NAME: vProcessEventQueues
*
* DESCRIPTION:
* Check each of the three event queues and process and items found.
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -