标签:
最近突然想自己做一个智能小冰箱玩一玩,于是决定动手试一试。
半导体制冷片一只
散热风扇
12V电源一台
智能恒温箱电路板
完整代码以及电路图我放在了GITHUB上,欢迎下载哦。
https://github.com/dingbig/bluttooth_ice_box
1 /************************************************************************************************** 2 Filename: simpleBLEPeripheral.c 3 Revised: $Date: 2010-08-06 08:56:11 -0700 (Fri, 06 Aug 2010) $ 4 Revision: $Revision: 23333 $ 5 6 Description: This file contains the Simple BLE Peripheral sample application 7 for use with the CC2540 Bluetooth Low Energy Protocol Stack. 8 9 Copyright 2010 - 2014 Texas Instruments Incorporated. All rights reserved. 10 11 IMPORTANT: Your use of this Software is limited to those specific rights 12 granted under the terms of a software license agreement between the user 13 who downloaded the software, his/her employer (which must be your employer) 14 and Texas Instruments Incorporated (the "License"). You may not use this 15 Software unless you agree to abide by the terms of the License. The License 16 limits your use, and you acknowledge, that the Software may not be modified, 17 copied or distributed unless embedded on a Texas Instruments microcontroller 18 or used solely and exclusively in conjunction with a Texas Instruments radio 19 frequency transceiver, which is integrated into your product. Other than for 20 the foregoing purpose, you may not use, reproduce, copy, prepare derivative 21 works of, modify, distribute, perform, display or sell this Software and/or 22 its documentation for any purpose. 23 24 YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE 25 PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 26 INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 27 NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL 28 TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, 29 NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER 30 LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES 31 INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE 32 OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT 33 OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES 34 (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. 35 36 Should you have any questions regarding your right to use this Software, 37 contact Texas Instruments Incorporated at www.TI.com. 38 **************************************************************************************************/ 39 40 /********************************************************************* 41 * INCLUDES 42 */ 43 44 #include "bcomdef.h" 45 #include "OSAL.h" 46 #include "OSAL_PwrMgr.h" 47 48 #include "OnBoard.h" 49 #include "hal_adc.h" 50 #include "hal_led.h" 51 #include "hal_key.h" 52 #include "hal_lcd.h" 53 54 #include "gatt.h" 55 56 #include "hci.h" 57 58 #include "gapgattserver.h" 59 #include "gattservapp.h" 60 #include "devinfoservice.h" 61 #include "simpleGATTprofile.h" 62 #include "ds18b20.h" 63 #include "stdio.h" 64 #include "oled.h" 65 66 #if defined( CC2540_MINIDK ) 67 #include "simplekeys.h" 68 #endif 69 70 #include "peripheral.h" 71 72 #include "gapbondmgr.h" 73 74 #include "simpleBLEPeripheral.h" 75 76 #if defined FEATURE_OAD 77 #include "oad.h" 78 #include "oad_target.h" 79 #endif 80 81 /********************************************************************* 82 * MACROS 83 */ 84 85 /********************************************************************* 86 * CONSTANTS 87 */ 88 89 // How often to perform periodic event 90 #define SBP_PERIODIC_EVT_PERIOD 5000 91 #define SBP_TEMP_EVT_PERIOD 500 92 93 94 // What is the advertising interval when device is discoverable (units of 625us, 160=100ms) 95 #define DEFAULT_ADVERTISING_INTERVAL 160 96 97 // Limited discoverable mode advertises for 30.72s, and then stops 98 // General discoverable mode advertises indefinitely 99 100 #if defined ( CC2540_MINIDK ) 101 #define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_LIMITED 102 #else 103 #define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL 104 #endif // defined ( CC2540_MINIDK ) 105 106 // Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled 107 #define DEFAULT_DESIRED_MIN_CONN_INTERVAL 80 108 109 // Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled 110 #define DEFAULT_DESIRED_MAX_CONN_INTERVAL 800 111 112 // Slave latency to use if automatic parameter update request is enabled 113 #define DEFAULT_DESIRED_SLAVE_LATENCY 0 114 115 // Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled 116 #define DEFAULT_DESIRED_CONN_TIMEOUT 1000 117 118 // Whether to enable automatic parameter update request when a connection is formed 119 #define DEFAULT_ENABLE_UPDATE_REQUEST TRUE 120 121 // Connection Pause Peripheral time value (in seconds) 122 #define DEFAULT_CONN_PAUSE_PERIPHERAL 6 123 124 // Company Identifier: Texas Instruments Inc. (13) 125 #define TI_COMPANY_ID 0x000D 126 127 #define INVALID_CONNHANDLE 0xFFFF 128 129 // Length of bd addr as a string 130 #define B_ADDR_STR_LEN 15 131 132 /********************************************************************* 133 * TYPEDEFS 134 */ 135 136 /********************************************************************* 137 * GLOBAL VARIABLES 138 */ 139 140 /********************************************************************* 141 * EXTERNAL VARIABLES 142 */ 143 144 /********************************************************************* 145 * EXTERNAL FUNCTIONS 146 */ 147 148 /********************************************************************* 149 * LOCAL VARIABLES 150 */ 151 static uint8 simpleBLEPeripheral_TaskID; // Task ID for internal task/event processing 152 153 static gaprole_States_t gapProfileState = GAPROLE_INIT; 154 155 // GAP - SCAN RSP data (max size = 31 bytes) 156 static uint8 scanRspData[] = 157 { 158 // complete name 159 0x14, // length of this data 160 GAP_ADTYPE_LOCAL_NAME_COMPLETE, 161 0x51, // ‘Q‘ 162 0x69, // ‘i‘ 163 0x6d, // ‘m‘ 164 0x70, // ‘p‘ 165 0x6c, // ‘l‘ 166 0x65, // ‘e‘ 167 0x42, // ‘B‘ 168 0x4c, // ‘L‘ 169 0x45, // ‘E‘ 170 0x50, // ‘P‘ 171 0x65, // ‘e‘ 172 0x72, // ‘r‘ 173 0x69, // ‘i‘ 174 0x70, // ‘p‘ 175 0x68, // ‘h‘ 176 0x65, // ‘e‘ 177 0x72, // ‘r‘ 178 0x61, // ‘a‘ 179 0x6c, // ‘l‘ 180 181 // connection interval range 182 0x05, // length of this data 183 GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE, 184 LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ), // 100ms 185 HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ), 186 LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ), // 1s 187 HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ), 188 189 // Tx power level 190 0x02, // length of this data 191 GAP_ADTYPE_POWER_LEVEL, 192 0 // 0dBm 193 }; 194 195 // GAP - Advertisement data (max size = 31 bytes, though this is 196 // best kept short to conserve power while advertisting) 197 static uint8 advertData[] = 198 { 199 // Flags; this sets the device to use limited discoverable 200 // mode (advertises for 30 seconds at a time) instead of general 201 // discoverable mode (advertises indefinitely) 202 0x02, // length of this data 203 GAP_ADTYPE_FLAGS, 204 DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED, 205 206 // service UUID, to notify central devices what services are included 207 // in this peripheral 208 0x03, // length of this data 209 GAP_ADTYPE_16BIT_MORE, // some of the UUID‘s, but not all 210 LO_UINT16( SIMPLEPROFILE_SERV_UUID ), 211 HI_UINT16( SIMPLEPROFILE_SERV_UUID ), 212 213 }; 214 215 // GAP GATT Attributes 216 static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple BLE Peripheral"; 217 218 /********************************************************************* 219 * LOCAL FUNCTIONS 220 */ 221 static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg ); 222 static void simpleBLEPeripheral_ProcessGATTMsg( gattMsgEvent_t *pMsg ); 223 static void peripheralStateNotificationCB( gaprole_States_t newState ); 224 static void performPeriodicTask( void ); 225 static void simpleProfileChangeCB( uint8 paramID ); 226 227 #if defined( CC2540_MINIDK ) 228 static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys ); 229 #endif 230 231 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 232 static char *bdAddr2Str ( uint8 *pAddr ); 233 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 234 235 236 237 /********************************************************************* 238 * PROFILE CALLBACKS 239 */ 240 241 // GAP Role Callbacks 242 static gapRolesCBs_t simpleBLEPeripheral_PeripheralCBs = 243 { 244 peripheralStateNotificationCB, // Profile State Change Callbacks 245 NULL // When a valid RSSI is read from controller (not used by application) 246 }; 247 248 // GAP Bond Manager Callbacks 249 static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs = 250 { 251 NULL, // Passcode callback (not used by application) 252 NULL // Pairing / Bonding state Callback (not used by application) 253 }; 254 255 // Simple GATT Profile Callbacks 256 static simpleProfileCBs_t simpleBLEPeripheral_SimpleProfileCBs = 257 { 258 simpleProfileChangeCB // Charactersitic value change callback 259 }; 260 /********************************************************************* 261 * PUBLIC FUNCTIONS 262 */ 263 264 /********************************************************************* 265 * @fn SimpleBLEPeripheral_Init 266 * 267 * @brief Initialization function for the Simple BLE Peripheral App Task. 268 * This is called during initialization and should contain 269 * any application specific initialization (ie. hardware 270 * initialization/setup, table initialization, power up 271 * notificaiton ... ). 272 * 273 * @param task_id - the ID assigned by OSAL. This ID should be 274 * used to send messages and set timers. 275 * 276 * @return none 277 */ 278 void SimpleBLEPeripheral_Init( uint8 task_id ) 279 { 280 simpleBLEPeripheral_TaskID = task_id; 281 282 osal_set_event(simpleBLEPeripheral_TaskID, SBP_START_TEMP_TIMER_EVT); 283 // Setup the GAP 284 VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL ); 285 286 // Setup the GAP Peripheral Role Profile 287 { 288 #if defined( CC2540_MINIDK ) 289 // For the CC2540DK-MINI keyfob, device doesn‘t start advertising until button is pressed 290 uint8 initial_advertising_enable = FALSE; 291 #else 292 // For other hardware platforms, device starts advertising upon initialization 293 uint8 initial_advertising_enable = TRUE; 294 #endif 295 296 // By setting this to zero, the device will go into the waiting state after 297 // being discoverable for 30.72 second, and will not being advertising again 298 // until the enabler is set back to TRUE 299 uint16 gapRole_AdvertOffTime = 0; 300 301 uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; 302 uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; 303 uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; 304 uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; 305 uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; 306 307 // Set the GAP Role Parameters 308 GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable ); 309 GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime ); 310 311 GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData ); 312 GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData ); 313 314 GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request ); 315 GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval ); 316 GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval ); 317 GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency ); 318 GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout ); 319 } 320 321 // Set the GAP Characteristics 322 GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName ); 323 324 // Set advertising interval 325 { 326 uint16 advInt = DEFAULT_ADVERTISING_INTERVAL; 327 328 GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt ); 329 GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt ); 330 GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt ); 331 GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt ); 332 } 333 334 // Setup the GAP Bond Manager 335 { 336 uint32 passkey = 0; // passkey "000000" 337 uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; 338 uint8 mitm = TRUE; 339 uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; 340 uint8 bonding = TRUE; 341 GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey ); 342 GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode ); 343 GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm ); 344 GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap ); 345 GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding ); 346 } 347 348 // Initialize GATT attributes 349 GGS_AddService( GATT_ALL_SERVICES ); // GAP 350 GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes 351 DevInfo_AddService(); // Device Information Service 352 SimpleProfile_AddService( GATT_ALL_SERVICES ); // Simple GATT Profile 353 #if defined FEATURE_OAD 354 VOID OADTarget_AddService(); // OAD Profile 355 #endif 356 357 // Setup the SimpleProfile Characteristic Values 358 { 359 uint8 charValue1 = 1; 360 uint8 charValue2 = 2; 361 uint8 charValue3 = 3; 362 uint8 charValue4 = 4; 363 uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 }; 364 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 ); 365 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 ); 366 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 ); 367 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 ); 368 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 ); 369 } 370 371 372 #if defined( CC2540_MINIDK ) 373 374 SK_AddService( GATT_ALL_SERVICES ); // Simple Keys Profile 375 376 // Register for all key events - This app will handle all key events 377 RegisterForKeys( simpleBLEPeripheral_TaskID ); 378 379 // makes sure LEDs are off 380 HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); 381 382 // For keyfob board set GPIO pins into a power-optimized state 383 // Note that there is still some leakage current from the buzzer, 384 // accelerometer, LEDs, and buttons on the PCB. 385 386 P0SEL = 0; // Configure Port 0 as GPIO 387 P1SEL = 0; // Configure Port 1 as GPIO 388 P2SEL = 0; // Configure Port 2 as GPIO 389 390 P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons), 391 // all others (P0.2-P0.7) as output 392 P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output 393 P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output 394 395 P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons) 396 P1 = 0; // All pins on port 1 to low 397 P2 = 0; // All pins on port 2 to low 398 399 #endif // #if defined( CC2540_MINIDK ) 400 401 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 402 403 #if defined FEATURE_OAD 404 #if defined (HAL_IMAGE_A) 405 HalLcdWriteStringValue( "BLE Peri-A", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 ); 406 #else 407 HalLcdWriteStringValue( "BLE Peri-B", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 ); 408 #endif // HAL_IMAGE_A 409 #else 410 HalLcdWriteString( "BLE Peripheral", HAL_LCD_LINE_1 ); 411 #endif // FEATURE_OAD 412 413 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 414 415 // Register callback with SimpleGATTprofile 416 VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs ); 417 418 // Enable clock divide on halt 419 // This reduces active current while radio is active and CC254x MCU 420 // is halted 421 HCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT ); 422 423 #if defined ( DC_DC_P0_7 ) 424 425 // Enable stack to toggle bypass control on TPS62730 (DC/DC converter) 426 HCI_EXT_MapPmIoPortCmd( HCI_EXT_PM_IO_PORT_P0, HCI_EXT_PM_IO_PORT_PIN7 ); 427 428 #endif // defined ( DC_DC_P0_7 ) 429 430 // Setup a delayed profile startup 431 osal_set_event( simpleBLEPeripheral_TaskID, SBP_START_DEVICE_EVT ); 432 433 } 434 435 436 float last_temp; 437 /********************************************************************* 438 * @fn SimpleBLEPeripheral_ProcessEvent 439 * 440 * @brief Simple BLE Peripheral Application Task event processor. This function 441 * is called to process all events for the task. Events 442 * include timers, messages and any other user defined events. 443 * 444 * @param task_id - The OSAL assigned task ID. 445 * @param events - events to process. This is a bit map and can 446 * contain more than one event. 447 * 448 * @return events not processed 449 */ 450 uint16 SimpleBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events ) 451 { 452 453 VOID task_id; // OSAL required parameter that isn‘t used in this function 454 455 if ( events & SYS_EVENT_MSG ) 456 { 457 uint8 *pMsg; 458 459 if ( (pMsg = osal_msg_receive( simpleBLEPeripheral_TaskID )) != NULL ) 460 { 461 simpleBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ); 462 463 // Release the OSAL message 464 VOID osal_msg_deallocate( pMsg ); 465 } 466 467 // return unprocessed events 468 return (events ^ SYS_EVENT_MSG); 469 } 470 471 if ( events & SBP_START_DEVICE_EVT ) 472 { 473 // Start the Device 474 VOID GAPRole_StartDevice( &simpleBLEPeripheral_PeripheralCBs ); 475 476 // Start Bond Manager 477 VOID GAPBondMgr_Register( &simpleBLEPeripheral_BondMgrCBs ); 478 479 // Set timer for first periodic event 480 osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD ); 481 482 return ( events ^ SBP_START_DEVICE_EVT ); 483 } 484 485 if ( events & SBP_PERIODIC_EVT ) 486 { 487 // Restart timer 488 if ( SBP_PERIODIC_EVT_PERIOD ) 489 { 490 osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD ); 491 } 492 493 // Perform periodic application task 494 performPeriodicTask(); 495 496 return (events ^ SBP_PERIODIC_EVT); 497 } 498 499 if(events & SBP_START_TEMP_TIMER_EVT) { 500 if(SBP_TEMP_EVT_PERIOD) 501 { 502 osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_START_TEMP_TIMER_EVT, SBP_TEMP_EVT_PERIOD ); 503 } 504 505 { 506 uint16 numBytes; 507 float ft; 508 char buf[64]; 509 extern float DS18B20_ReadMain(void); 510 P0SEL &= ~(BV(7)); 511 ft = DS18B20_ReadMain(); 512 printf("done.. %.1f\r\n", ft); 513 //int len = sprintf(buf,">%.1f", ft); 514 if(ft <100 && ft > -40) { 515 if(last_temp!=ft) { 516 oled_printf(FONT_32, 32,2," ", ft); 517 oled_printf(FONT_32, 32,2,"%.1f", ft); 518 last_temp = ft; 519 } 520 } 521 522 if(ft > 4) { 523 void relay_ctl(int id, int status); 524 relay_ctl(1,1); 525 relay_ctl(2,1); 526 relay_ctl(3,1); 527 relay_ctl(4,1); 528 529 //red on 530 P2DIR |= BV(0); 531 P2SEL &=~(BV(0)); 532 P2_0 = 0; 533 534 } else { 535 relay_ctl(1,0); 536 relay_ctl(2,0); 537 relay_ctl(3,0); 538 relay_ctl(4,0); 539 //red off 540 P2DIR |= BV(0); 541 P2SEL &=~(BV(0)); 542 P2_0 = 1; 543 } 544 545 546 } 547 return events ^ SBP_START_TEMP_TIMER_EVT; 548 } 549 550 printf("unknown events\r\n"); 551 // Discard unknown events 552 return 0; 553 } 554 555 /********************************************************************* 556 * @fn simpleBLEPeripheral_ProcessOSALMsg 557 * 558 * @brief Process an incoming task message. 559 * 560 * @param pMsg - message to process 561 * 562 * @return none 563 */ 564 static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg ) 565 { 566 printf("on msg\r\n"); 567 switch ( pMsg->event ) 568 { 569 #if defined( CC2540_MINIDK ) 570 case KEY_CHANGE: 571 simpleBLEPeripheral_HandleKeys( ((keyChange_t *)pMsg)->state, 572 ((keyChange_t *)pMsg)->keys ); 573 break; 574 #endif // #if defined( CC2540_MINIDK ) 575 576 case GATT_MSG_EVENT: 577 // Process GATT message 578 simpleBLEPeripheral_ProcessGATTMsg( (gattMsgEvent_t *)pMsg ); 579 break; 580 581 default: 582 // do nothing 583 break; 584 } 585 } 586 587 #if defined( CC2540_MINIDK ) 588 /********************************************************************* 589 * @fn simpleBLEPeripheral_HandleKeys 590 * 591 * @brief Handles all key events for this device. 592 * 593 * @param shift - true if in shift/alt. 594 * @param keys - bit field for key events. Valid entries: 595 * HAL_KEY_SW_2 596 * HAL_KEY_SW_1 597 * 598 * @return none 599 */ 600 static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys ) 601 { 602 uint8 SK_Keys = 0; 603 604 VOID shift; // Intentionally unreferenced parameter 605 606 if ( keys & HAL_KEY_SW_1 ) 607 { 608 SK_Keys |= SK_KEY_LEFT; 609 } 610 611 if ( keys & HAL_KEY_SW_2 ) 612 { 613 614 SK_Keys |= SK_KEY_RIGHT; 615 616 // if device is not in a connection, pressing the right key should toggle 617 // advertising on and off 618 // Note: If PLUS_BROADCASTER is define this condition is ignored and 619 // Device may advertise during connections as well. 620 #ifndef PLUS_BROADCASTER 621 if( gapProfileState != GAPROLE_CONNECTED ) 622 { 623 #endif // PLUS_BROADCASTER 624 uint8 current_adv_enabled_status; 625 uint8 new_adv_enabled_status; 626 627 //Find the current GAP advertisement status 628 GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, ¤t_adv_enabled_status ); 629 630 if( current_adv_enabled_status == FALSE ) 631 { 632 new_adv_enabled_status = TRUE; 633 } 634 else 635 { 636 new_adv_enabled_status = FALSE; 637 } 638 639 //change the GAP advertisement status to opposite of current status 640 GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status ); 641 #ifndef PLUS_BROADCASTER 642 } 643 #endif // PLUS_BROADCASTER 644 } 645 646 // Set the value of the keys state to the Simple Keys Profile; 647 // This will send out a notification of the keys state if enabled 648 SK_SetParameter( SK_KEY_ATTR, sizeof ( uint8 ), &SK_Keys ); 649 } 650 #endif // #if defined( CC2540_MINIDK ) 651 652 /********************************************************************* 653 * @fn simpleBLEPeripheral_ProcessGATTMsg 654 * 655 * @brief Process GATT messages 656 * 657 * @return none 658 */ 659 static void simpleBLEPeripheral_ProcessGATTMsg( gattMsgEvent_t *pMsg ) 660 { 661 GATT_bm_free( &pMsg->msg, pMsg->method ); 662 } 663 664 /********************************************************************* 665 * @fn peripheralStateNotificationCB 666 * 667 * @brief Notification from the profile of a state change. 668 * 669 * @param newState - new state 670 * 671 * @return none 672 */ 673 static void peripheralStateNotificationCB( gaprole_States_t newState ) 674 { 675 #ifdef PLUS_BROADCASTER 676 static uint8 first_conn_flag = 0; 677 #endif // PLUS_BROADCASTER 678 679 680 switch ( newState ) 681 { 682 case GAPROLE_STARTED: 683 { 684 uint8 ownAddress[B_ADDR_LEN]; 685 uint8 systemId[DEVINFO_SYSTEM_ID_LEN]; 686 687 GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress); 688 689 // use 6 bytes of device address for 8 bytes of system ID value 690 systemId[0] = ownAddress[0]; 691 systemId[1] = ownAddress[1]; 692 systemId[2] = ownAddress[2]; 693 694 // set middle bytes to zero 695 systemId[4] = 0x00; 696 systemId[3] = 0x00; 697 698 // shift three bytes up 699 systemId[7] = ownAddress[5]; 700 systemId[6] = ownAddress[4]; 701 systemId[5] = ownAddress[3]; 702 703 DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId); 704 705 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 706 // Display device address 707 HalLcdWriteString( bdAddr2Str( ownAddress ), HAL_LCD_LINE_2 ); 708 HalLcdWriteString( "Initialized", HAL_LCD_LINE_3 ); 709 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 710 } 711 break; 712 713 case GAPROLE_ADVERTISING: 714 { 715 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 716 HalLcdWriteString( "Advertising", HAL_LCD_LINE_3 ); 717 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 718 } 719 break; 720 721 #ifdef PLUS_BROADCASTER 722 /* After a connection is dropped a device in PLUS_BROADCASTER will continue 723 * sending non-connectable advertisements and shall sending this change of 724 * state to the application. These are then disabled here so that sending 725 * connectable advertisements can resume. 726 */ 727 case GAPROLE_ADVERTISING_NONCONN: 728 { 729 uint8 advertEnabled = FALSE; 730 731 // Disable non-connectable advertising. 732 GAPRole_SetParameter(GAPROLE_ADV_NONCONN_ENABLED, sizeof(uint8), 733 &advertEnabled); 734 735 // Reset flag for next connection. 736 first_conn_flag = 0; 737 } 738 break; 739 #endif //PLUS_BROADCASTER 740 741 case GAPROLE_CONNECTED: 742 { 743 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 744 HalLcdWriteString( "Connected", HAL_LCD_LINE_3 ); 745 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 746 747 #ifdef PLUS_BROADCASTER 748 // Only turn advertising on for this state when we first connect 749 // otherwise, when we go from connected_advertising back to this state 750 // we will be turning advertising back on. 751 if ( first_conn_flag == 0 ) 752 { 753 uint8 advertEnabled = FALSE; // Turn on Advertising 754 755 // Disable connectable advertising. 756 GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8), 757 &advertEnabled); 758 759 // Set to true for non-connectabel advertising. 760 advertEnabled = TRUE; 761 762 // Enable non-connectable advertising. 763 GAPRole_SetParameter(GAPROLE_ADV_NONCONN_ENABLED, sizeof(uint8), 764 &advertEnabled); 765 766 first_conn_flag = 1; 767 } 768 #endif // PLUS_BROADCASTER 769 } 770 break; 771 772 case GAPROLE_CONNECTED_ADV: 773 { 774 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 775 HalLcdWriteString( "Connected Advertising", HAL_LCD_LINE_3 ); 776 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 777 } 778 break; 779 case GAPROLE_WAITING: 780 { 781 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 782 HalLcdWriteString( "Disconnected", HAL_LCD_LINE_3 ); 783 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 784 785 #ifdef PLUS_BROADCASTER 786 uint8 advertEnabled = TRUE; 787 788 // Enabled connectable advertising. 789 GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8), 790 &advertEnabled); 791 #endif //PLUS_BROADCASTER 792 } 793 break; 794 795 case GAPROLE_WAITING_AFTER_TIMEOUT: 796 { 797 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 798 HalLcdWriteString( "Timed Out", HAL_LCD_LINE_3 ); 799 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 800 801 #ifdef PLUS_BROADCASTER 802 // Reset flag for next connection. 803 first_conn_flag = 0; 804 #endif //#ifdef (PLUS_BROADCASTER) 805 } 806 break; 807 808 case GAPROLE_ERROR: 809 { 810 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 811 HalLcdWriteString( "Error", HAL_LCD_LINE_3 ); 812 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 813 } 814 break; 815 816 default: 817 { 818 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 819 HalLcdWriteString( "", HAL_LCD_LINE_3 ); 820 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 821 } 822 break; 823 824 } 825 826 gapProfileState = newState; 827 828 #if !defined( CC2540_MINIDK ) 829 VOID gapProfileState; // added to prevent compiler warning with 830 // "CC2540 Slave" configurations 831 #endif 832 833 834 } 835 836 /********************************************************************* 837 * @fn performPeriodicTask 838 * 839 * @brief Perform a periodic application task. This function gets 840 * called every five seconds as a result of the SBP_PERIODIC_EVT 841 * OSAL event. In this example, the value of the third 842 * characteristic in the SimpleGATTProfile service is retrieved 843 * from the profile, and then copied into the value of the 844 * the fourth characteristic. 845 * 846 * @param none 847 * 848 * @return none 849 */ 850 static void performPeriodicTask( void ) 851 { 852 uint8 valueToCopy; 853 uint8 stat; 854 855 // Call to retrieve the value of the third characteristic in the profile 856 stat = SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &valueToCopy); 857 858 if( stat == SUCCESS ) 859 { 860 /* 861 * Call to set that value of the fourth characteristic in the profile. Note 862 * that if notifications of the fourth characteristic have been enabled by 863 * a GATT client device, then a notification will be sent every time this 864 * function is called. 865 */ 866 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &valueToCopy); 867 } 868 } 869 870 /********************************************************************* 871 * @fn simpleProfileChangeCB 872 * 873 * @brief Callback from SimpleBLEProfile indicating a value change 874 * 875 * @param paramID - parameter ID of the value that was changed. 876 * 877 * @return none 878 */ 879 static void simpleProfileChangeCB( uint8 paramID ) 880 { 881 uint8 newValue; 882 883 switch( paramID ) 884 { 885 case SIMPLEPROFILE_CHAR1: 886 SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue ); 887 888 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 889 HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); 890 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 891 892 break; 893 894 case SIMPLEPROFILE_CHAR3: 895 SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue ); 896 897 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 898 HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); 899 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 900 901 break; 902 903 default: 904 // should not reach here! 905 break; 906 } 907 } 908 909 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 910 /********************************************************************* 911 * @fn bdAddr2Str 912 * 913 * @brief Convert Bluetooth address to string. Only needed when 914 * LCD display is used. 915 * 916 * @return none 917 */ 918 char *bdAddr2Str( uint8 *pAddr ) 919 { 920 uint8 i; 921 char hex[] = "0123456789ABCDEF"; 922 static char str[B_ADDR_STR_LEN]; 923 char *pStr = str; 924 925 *pStr++ = ‘0‘; 926 *pStr++ = ‘x‘; 927 928 // Start from end of addr 929 pAddr += B_ADDR_LEN; 930 931 for ( i = B_ADDR_LEN; i > 0; i-- ) 932 { 933 *pStr++ = hex[*--pAddr >> 4]; 934 *pStr++ = hex[*pAddr & 0x0F]; 935 } 936 937 *pStr = 0; 938 939 return str; 940 } 941 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 942 943 /********************************************************************* 944 *********************************************************************/
标签:
原文地址:http://www.cnblogs.com/dingdaheng/p/5535578.html