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

📄 homesensorcoord.c

📁 Micro controladores jennic wireless networking zigbee
💻 C
📖 第 1 页 / 共 5 页
字号:
/****************************************************************************
 *
 * MODULE:             Demo coordinator code
 *
 * COMPONENT:          $RCSfile: HomeSensorCoord.c,v $
 *
 * VERSION:            $Name:  $
 *
 * REVISION:           $Revision: 1.4 $
 *
 * DATED:              $Date: 2007/06/21 08:17:07 $
 *
 * STATUS:             $State: Exp $
 *
 * AUTHOR:             CJG
 *
 * DESCRIPTION:
 * Coordinator for demonstrator. Acts as time coordinator for up to four
 * sensor endpoints and manages LCD panel and keys on central coordinator.
 *
 * LAST MODIFIED BY:   $Author: jahme $
 *                     $Modtime: $
 *
 ****************************************************************************
 *
 *  (c) Copyright 2007 JENNIC Ltd
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include "jendefs.h"
#include "LcdDriver.h"
#include "ALSdriver.h"
#include "HTSdriver.h"
#include "HomeSensorConfig.h"
#include <AppHardwareApi.h>
#include "JennicLogo.h"
#include <AppQueueApi.h>
#include <mac_sap.h>
#include <mac_pib.h>
#include <string.h>
#include "LedControl.h"
#include "Button.h"
#include "Utilities.h"
#include "gdb.h"

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/
/* Block (time slice) values */
#define BLOCK_TIME_IN_32K_PERIODS 1600
#define BLOCK_MIN_RX          2
#define BLOCK_UPDATE          (BLOCK_MIN_RX + DEMO_ENDPOINTS)
#define BLOCK_START_TEMP      13
#define BLOCK_READ_TEMP       15
#define BLOCK_START_HUMIDITY  16
#define BLOCK_READ_HUMIDITY   18
#define BLOCK_READ_LIGHT      19
#define MAX_BLOCKS            20

/* Control screen and alarm values */
#define CONTROL_LIST_LEN      4
#define TEMP_HIGH_MAX         100
#define LIGHT_HIGH_MAX        6

/* Setup screen values */
#define SETUP_LIST_LEN        2

#define FRAMES_MISSED_INDICATION 9

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/
/* Holds all stored data for a particular sensor for a node */
typedef struct
{
    uint8 u8NowValue;
    uint8 u8HighAlarm;
    uint8 u8LowAlarm;
    uint8 au8GraphData[DEMO_HISTORY_LEN];
} tsNodeElementData;

/* Holds all stored data for a node */
typedef struct
{
    bool_t boDeviceOn;
    tsNodeElementData asNodeElementData[DEMO_SENSOR_LIST_LEN];
    uint8 u8PrevRxCount;
    uint8 u8FramesMissed;
    uint8 u8SwitchOn;
    uint8 u8Rssi;
} tsNodeData;

/* Used to track an association between extended address and short address */
typedef struct
{
    MAC_ExtAddr_s sExtAddr;
    uint16 u16ShortAddr;
} tsAssocNodes;

/* System states with respect to screen display being shown */
typedef enum
{
    E_STATE_NETWORK,
    E_STATE_NODE,
    E_STATE_NODE_CONTROL,
    E_STATE_SET_CHANNEL,
    E_STATE_SETUP_SCREEN,
    E_STATE_SCANNING
} teState;

/* Button values */
typedef enum
{
    E_KEY_0 = BUTTON_0_MASK,
    E_KEY_1 = BUTTON_1_MASK,
    E_KEY_2 = BUTTON_2_MASK,
    E_KEY_3 = BUTTON_3_MASK,
    E_KEYS_0_AND_3 = (BUTTON_0_MASK | BUTTON_3_MASK)
} teKeyValues;

/* 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
    {
        tsNodeData   asNodeData[DEMO_ENDPOINTS];
        tsAssocNodes asAssocNodes[DEMO_ENDPOINTS];
        bool_t       bLocalNode;
        uint8        u8AssociatedNodes;
    } sNode;

    struct
    {
        uint16 u16Hi;
        uint16 u16Lo;
    } sLightSensor;

    struct
    {
        teSensor eCurrentSensor;
        uint8    u8CurrentNode;
        uint8    u8GraphPos;
        uint8    u8ControlSelection;
        uint8    u8SetupSelection;
        bool_t   bShowFourNodes;
    } sGui;

    struct
    {
        teState eState;
        uint8   u8Channel;
        uint32  u32AppApiVersion;
        uint32  u32HwApiVersion;
        uint32  u32CalibratedTimeout;
    } sSystem;
} tsDemoData;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void vInitSystem(void);
PRIVATE void vInitCoord(void);
PRIVATE void vSetTimer(void);
PRIVATE void vProcessCurrentTimeBlock(uint8 u8TimeBlock);
PRIVATE bool_t bProcessKeys(uint8 *pu8Keys);
PRIVATE uint8 u8UpdateTimeBlock(uint8 u8TimeBlock);
PRIVATE void vProcessInterrupts(void);
PRIVATE void vProcessUpdateBlock(void);
PRIVATE void vProcessIncomingData(MAC_McpsDcfmInd_s *psMcpsInd);
PRIVATE void vProcessIncomingMlme(MAC_MlmeDcfmInd_s *psMlmeInd);
PRIVATE bool_t bProcessForTimeout(AppQApiHwInd_s *psHardwareInd);
PRIVATE void vProcessNetworkKeyPress(uint8 u8KeyMap);
PRIVATE void vUpdateNetworkSensor(teSensor eSensor);
PRIVATE void vProcessNodeKeyPress(uint8 u8KeyMap);
PRIVATE void vProcessNodeControlKeyPress(uint8 u8KeyMap);
PRIVATE void vProcessSetChannelKeyPress(uint8 u8KeyMap);
PRIVATE void vProcessSetupKeyPress(uint8 u8KeyMap);
PRIVATE void vBuildSetChannelScreen(void);
PRIVATE void vUpdateSetChannelScreen(void);
PRIVATE void vBuildSetupScreen(void);
PRIVATE void vUpdateSetupScreen(uint8 u8Selection, bool_t boUpdate);
PRIVATE void vBuildNetworkScreen(teSensor eSensor);
PRIVATE void vUpdateNetworkScreen(teSensor eSensor);
PRIVATE void vBuildNodeScreen(uint8 u8Node);
PRIVATE void vUpdateNodeScreen(uint8 u8Node);
PRIVATE void vBuildNodeControlScreen(uint8 u8Node);
PRIVATE void vUpdateNodeControlScreen(uint8 u8Node, uint8 u8Selection, bool_t boUpdate);
PRIVATE void vLcdUpdateElement(tsNodeData *psNodeData, teSensor eSensor,
                               uint8 u8Row, uint8 u8Col, bool_t bShowRssi);
PRIVATE void vDrawGraph(uint8 *pu8GraphData, uint8 u8StartCol,
                        uint8 u8StartRow);
PRIVATE void vStringCopy(char *pcFrom,char *pcTo);
PRIVATE void vValToDec(char *pcOutString, uint8 u8Value, char *pcLabel);
PRIVATE void vAdjustAlarm(uint8 *pu8Value, uint8 u8MaxValue, uint8 u8OffValue, bool_t bUpNotDown);
PRIVATE void vStartBeacon(void);
PRIVATE void vUpdateBeaconPayload(void);
PRIVATE void vDisplayError(char *pcErrorMessage, uint32 u32Data);
PRIVATE void vWriteOnOff(bool_t bOnOff, uint8 u8Row, uint8 u8Col);
PRIVATE void vToggleOnOff(bool_t *pbItem);
PRIVATE void vWriteRowLabel(uint8 u8Selection, char **ppcRowName, uint8 u8ListLen);

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/

/* Handles from the MAC */
PRIVATE void *s_pvMac;
PRIVATE MAC_Pib_s *s_psMacPib;

/* File scope data */
PRIVATE tsDemoData sDemoData;

/* Row and column positions of info fields on LCD */
static const uint8 au8NodeLcdRow[DEMO_ENDPOINTS] = {0,  0, 3,  3};
static const uint8 au8NodeLcdCol[DEMO_ENDPOINTS] = {0, 64, 0, 64};

static const char *apcNodeNameList[DEMO_ENDPOINTS] = {
    "Hall", "Bedroom", "Lounge", "Bathroom"};

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 * Entry point for application from boot loader. Initialises system and runs
 * main loop.
 *
 * RETURNS:
 * Never returns.
 *
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    uint8 u8Keys;
    uint8 u8TimeBlock;
    bool_t bExitMainLoop;

    /* Debug hooks: include these regardless of whether debugging or not */
    HAL_GDB_INIT();
    HAL_BREAKPOINT();

    /* General initialisation: reset hardware, set some values in PIB */
    vInitSystem();

    /* Main application loop. This loop is iterated whenever a soft reset is
       performed */
    while (1)
    {
        /* Initialise software elements */
        vInitCoord();

        /* Set simple scheduler to perform no activity */
        u8TimeBlock = MAX_BLOCKS;

        /* Build channel set screen */
        vBuildSetChannelScreen();

        /* Change to channel setting state */
        sDemoData.sSystem.eState = E_STATE_SET_CHANNEL;

        /* Get current key state. This is necessary after a soft reset as keys
           will still be down when entering the main loop */
        u8Keys = u8ButtonReadFfd();

        /* Run main loop. The timer ensures that this runs 20 times per second,
           which allows a simple scheduler to run to start reads from the
           sensors, read results from the sensors and update the display. It
           also allows for simple button debounce by only sampling the buttons
           once during each interval. */
        do
        {
            /* Set the timer for the next interval */
            vSetTimer();

            /* Perform scheduler action */
            vProcessCurrentTimeBlock(u8TimeBlock);

            /* Check keys. Returns TRUE if 'reset' combination has been pressed */
            bExitMainLoop = bProcessKeys(&u8Keys);

            /* Increment scheduler time block for next time */
            u8TimeBlock = u8UpdateTimeBlock(u8TimeBlock);

            /* Service upward indications and confirmations from MLME and
               MCPS, and process interrupts from hardware. This function
               will exit when timer fires */
            vProcessInterrupts();
        } while (bExitMainLoop == FALSE);
    }
}

/****************************************************************************
 *
 * NAME: AppWarmStart
 *
 * DESCRIPTION:
 * Entry point for application from boot loader. Simply jumps to AppColdStart
 * as, in this instance, application will never warm start.
 *
 * RETURNS:
 * Never returns.
 *
 ****************************************************************************/
PUBLIC void AppWarmStart(void)
{
    AppColdStart();
}

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/

/****************************************************************************
 *
 * NAME: vInitSystem
 *
 * DESCRIPTION:
 * Initialises stack and hardware. Also sets non-default values in the
 * 802.15.4 PIB and starts the first read of the light sensor. Subsequent
 * reads of this sensor occur automatically.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInitSystem(void)
{
    /* Initialise stack and hardware interfaces, and register peripheral
       interrupts with AppQueueApi handler. We aren't using callbacks
       at all, just monitoring the upward queues in a loop */
    sDemoData.sSystem.u32AppApiVersion = u32AppQApiInit(NULL, NULL, NULL);
    sDemoData.sSystem.u32HwApiVersion = u32AHI_Init();

    /* Set up the MAC handles. Must be called AFTER u32AppQApiInit() */
    s_pvMac = pvAppApiGetMacHandle();
    s_psMacPib = MAC_psPibGetHandle(s_pvMac);

    /* Set up buttons and LEDs */
    vLedControl(0, FALSE);
    vLedControl(1, FALSE);
    vLedControl(2, FALSE);
    vLedControl(3, FALSE);
    vLedInitFfd();
    vButtonInitFfd();

    /* Set up hardware and splash screen */
    vLcdResetDefault();
    vALSreset();
    vHTSreset();

    /* Start ambient light sensor now: it automatically keeps re-sampling after this */
    vALSstartReadChannel(0);

    /* Set Pan ID and short address in PIB (also sets match registers in hardware) */
    MAC_vPibSetPanId(s_pvMac, DEMO_PAN_ID);
    MAC_vPibSetShortAddr(s_pvMac, DEMO_COORD_ADDR);
    s_psMacPib->bAssociationPermit = TRUE;

    /* Calibrate wake timer */
    sDemoData.sSystem.u32CalibratedTimeout = BLOCK_TIME_IN_32K_PERIODS * 10000 / u32AHI_WakeTimerCalibrate();

    /* Enable timer to use for sequencing */
    vAHI_WakeTimerEnable(E_AHI_WAKE_TIMER_0, TRUE);
}

/****************************************************************************
 *
 * NAME: vInitCoord
 *
 * DESCRIPTION:
 * Initialises software structures and variables. Endpoint data is reset and
 * the GUI is set to the default condition.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInitCoord(void)
{
    tsNodeData        *psNodeData;
    tsNodeElementData *psNodeElementData;
    uint8             *pu8GraphData;
    int                i, j, k;

    /* Initialise node data */
    for (i = 0; i < DEMO_ENDPOINTS; i++)
    {
        psNodeData = &sDemoData.sNode.asNodeData[i];
        psNodeData->boDeviceOn = FALSE;

        /* Set label */
        for (j = 0; j < DEMO_SENSOR_LIST_LEN; j++)
        {

⌨️ 快捷键说明

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