Advertisement
ptrawt

light_switch

Oct 21st, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.28 KB | None | 0 0
  1. #include <hal_lcd.h>
  2. #include <hal_led.h>
  3. #include <hal_joystick.h>
  4. #include <hal_assert.h>
  5. #include <hal_board.h>
  6. #include <hal_int.h>
  7. #include "hal_mcu.h"
  8. #include "hal_button.h"
  9. #include "hal_rf.h"
  10. #include "util_lcd.h"
  11. #include "basic_rf.h"
  12. #include <stdio.h>
  13.  
  14.  
  15. /***********************************************************************************
  16. * CONSTANTS
  17. */
  18. // Application parameters
  19. #define RF_CHANNEL                25      // 2.4 GHz RF channel
  20.  
  21. // BasicRF address definitions
  22. #define PAN_ID                0x2007
  23. #define SWITCH_ADDR           0xCAFE
  24. #define LIGHT_ADDR            0xB00B
  25. #define APP_PAYLOAD_LENGTH        1
  26. #define LIGHT_TOGGLE_CMD          0
  27.  
  28. // Application states
  29. #define IDLE                      0
  30. #define SEND_CMD                  1
  31.  
  32. // Application role
  33. #define NONE                      0
  34. #define SWITCH                    1
  35. #define LIGHT                     2
  36. #define APP_MODES                 2
  37.  
  38. /***********************************************************************************
  39. * LOCAL VARIABLES
  40. */
  41. static uint8 pTxData[APP_PAYLOAD_LENGTH];
  42. static uint8 pRxData[APP_PAYLOAD_LENGTH];
  43. static basicRfCfg_t basicRfConfig;
  44. static uint8 state = 0;
  45.  
  46. // Mode menu
  47. static menuItem_t pMenuItems[] =
  48. {
  49. #ifdef ASSY_EXP4618_CC2420
  50.   // Using Softbaugh 7-seg display
  51.   " L S    ", SWITCH,
  52.   " LIGHT  ", LIGHT
  53. #else
  54.   // SRF04EB and SRF05EB
  55.   "Switch",   SWITCH,
  56.   "Light",    LIGHT
  57. #endif
  58. };
  59.  
  60. static menu_t pMenu =
  61. {
  62.   pMenuItems,
  63.   N_ITEMS(pMenuItems)
  64. };
  65.  
  66.  
  67. #ifdef SECURITY_CCM
  68. // Security key
  69. static uint8 key[]= {
  70.     0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  71.     0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  72. };
  73. #endif
  74.  
  75. /***********************************************************************************
  76. * LOCAL FUNCTIONS
  77. */
  78. static void appLight();
  79. static void appSwitch();
  80. static uint8 appSelectMode(void);
  81.  
  82.  
  83. /***********************************************************************************
  84. * @fn          appLight
  85. *
  86. * @brief       Application code for light application. Puts MCU in endless
  87. *              loop waiting for user input from joystick.
  88. *
  89. * @param       basicRfConfig - file scope variable. Basic RF configuration data
  90. *              pRxData - file scope variable. Pointer to buffer for RX data
  91. *
  92. * @return      none
  93. */
  94. static void appLight()
  95. {
  96.   /*************************************************by boo
  97.     halLcdWriteLine(HAL_LCD_LINE_1, "Light");
  98.     halLcdWriteLine(HAL_LCD_LINE_2, "Ready");
  99.   */  
  100. #ifdef ASSY_EXP4618_CC2420
  101.     halLcdClearLine(1);
  102.     halLcdWriteSymbol(HAL_LCD_SYMBOL_RX, 1);
  103. #endif
  104.  
  105.     // Initialize BasicRF
  106.     basicRfConfig.myAddr = LIGHT_ADDR;
  107.     if(basicRfInit(&basicRfConfig)==FAILED) {
  108.       HAL_ASSERT(FALSE);
  109.     }
  110.     basicRfReceiveOn();
  111.  
  112.     // Main loop
  113.     while (TRUE) {
  114.         while(!basicRfPacketIsReady());
  115.  
  116.         if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) {
  117.             if(pRxData[0] == LIGHT_TOGGLE_CMD) {
  118.                 halLedToggle(1);
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. /**********************************************************************************
  126. * @fn          appSwitch
  127. *
  128. * @brief       Application code for switch application. Puts MCU in
  129. *              endless loop to wait for commands from from switch
  130. *
  131. * @param       basicRfConfig - file scope variable. Basic RF configuration data
  132. *              pTxData - file scope variable. Pointer to buffer for TX
  133. *              payload
  134. *              appState - file scope variable. Holds application state
  135. *
  136. * @return      none
  137. */
  138. static void appSwitch()
  139. {
  140. /****************************************************************by boo  
  141.   halLcdWriteLine(HAL_LCD_LINE_1, "Switch");
  142.     halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push");
  143.     halLcdWriteLine(HAL_LCD_LINE_3, "Send Command");*/
  144. #ifdef ASSY_EXP4618_CC2420
  145.     halLcdClearLine(1);
  146.     halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1);
  147. #endif
  148.  
  149.  
  150.     // Initialize BasicRF
  151.     basicRfConfig.myAddr = SWITCH_ADDR;
  152.     if(basicRfInit(&basicRfConfig)==FAILED) {
  153.       HAL_ASSERT(FALSE);
  154.     }
  155.    
  156.     pTxData[0] = LIGHT_TOGGLE_CMD;
  157.  
  158.     // Keep Receiver off when not needed to save power
  159.     basicRfReceiveOff();
  160.  
  161.     // Main loop
  162.     while (TRUE) {
  163.         //if( halJoystickPushed() )**********************by boo
  164.          if(halButtonPushed()==HAL_BUTTON_1)//**************by boo
  165.         {
  166.           basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);
  167.            
  168.             // Put MCU to sleep. It will wake up on joystick interrupt
  169.             halIntOff();
  170.             halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
  171.             // interrupt enable
  172.             halIntOn();
  173.         }
  174.     }
  175. }
  176.  
  177.  
  178. /***********************************************************************************
  179. * @fn          main
  180. *
  181. * @brief       This is the main entry of the "Light Switch" application.
  182. *              After the application modes are chosen the switch can
  183. *              send toggle commands to a light device.
  184. *
  185. * @param       basicRfConfig - file scope variable. Basic RF configuration
  186. *              data
  187. *              appState - file scope variable. Holds application state
  188. *
  189. * @return      none
  190. */
  191. void main(void)
  192. {
  193.     //uint8 appMode = SWITCH;
  194.     //uint8 appMode = LIGHT;
  195.  
  196.     // Config basicRF
  197.     basicRfConfig.panId = PAN_ID;
  198.     basicRfConfig.channel = RF_CHANNEL;
  199.     basicRfConfig.ackRequest = TRUE;
  200. #ifdef SECURITY_CCM
  201.     basicRfConfig.securityKey = key;
  202. #endif
  203.  
  204.     // Initalise board peripherals
  205.     halBoardInit();
  206.  
  207.  
  208.     // Initalise hal_rf
  209.     if(halRfInit()==FAILED) {
  210.       HAL_ASSERT(FALSE);
  211.     }
  212.  
  213.     // Indicate that device is powered
  214.     //halLedSet(2);      //นุฑีLED2
  215.     halLedSet(1);      //นุฑีLED1
  216.  
  217.  
  218.     //appSwitch();         //key1      P0_1
  219.     appLight();  //LED1      P1_0
  220.    
  221.    // Role is undefined. This code should not be reached
  222.     HAL_ASSERT(FALSE);
  223. }
  224.  
  225.  
  226. /****************************************************************************************
  227. * @fn          appSelectMode
  228. *
  229. * @brief       Select application mode
  230. *
  231. * @param       none
  232. *
  233. * @return      uint8 - Application mode chosen
  234. */
  235. static uint8 appSelectMode(void)
  236. {
  237.     halLcdWriteLine(1, "Device Mode: ");
  238.  
  239.     return utilMenuSelect(&pMenu);
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement