Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.60 KB | None | 0 0
  1. /*
  2.  
  3. Copyright (c) 2012-2014 RedBearLab
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  6. and associated documentation files (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11.  
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  16. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17.  
  18. */
  19.  
  20. #include "mbed.h"
  21. #include "ble/BLE.h"
  22. #include "Servo.h"
  23.  
  24.  
  25. #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
  26. #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
  27. #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
  28.  
  29. #define TXRX_BUF_LEN                     20
  30.  
  31. #define DIGITAL_OUT_PIN                  P0_9       //TXD
  32. #define DIGITAL_IN_PIN                   P0_10      //CTS
  33. #define PWM_PIN                          P0_11      //RXD
  34. #define SERVO_PIN                        P0_8       //RTS
  35. #define ANALOG_IN_PIN                    P0_4       //P04
  36.  
  37. BLE             ble;
  38.  
  39. DigitalOut      LED_SET(DIGITAL_OUT_PIN);
  40. DigitalIn       BUTTON(DIGITAL_IN_PIN);
  41. PwmOut          PWM(PWM_PIN);
  42. AnalogIn        ANALOG(ANALOG_IN_PIN);
  43. Servo           MYSERVO(SERVO_PIN);
  44.  
  45. Serial pc(USBTX, USBRX);
  46.  
  47. static uint8_t analog_enabled = 0;
  48. static uint8_t old_state = 0;
  49.  
  50. // The Nordic UART Service
  51. static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
  52. static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
  53. static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
  54. static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
  55.  
  56.  
  57. uint8_t txPayload[TXRX_BUF_LEN] = {0,};
  58. uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
  59.  
  60. static uint8_t rx_buf[TXRX_BUF_LEN];
  61. static uint8_t rx_len=0;
  62.  
  63. // data pass for i2c
  64. static char buffer[6] = "hello\0";
  65.  
  66. GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
  67.                                      
  68. GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
  69.                                      
  70. GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
  71.  
  72. GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
  73.  
  74.  
  75.  
  76. void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
  77. {
  78.     pc.printf("Disconnected \r\n");
  79.     pc.printf("Restart advertising \r\n");
  80.     ble.gap().startAdvertising();
  81. }
  82.  
  83. void WrittenHandler(const GattWriteCallbackParams *Handler)
  84. {  
  85.     uint8_t buf[TXRX_BUF_LEN];
  86.     uint16_t bytesRead;
  87.    
  88.     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
  89.     {
  90.         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
  91.         memset(txPayload, 0, TXRX_BUF_LEN);
  92.         memcpy(txPayload, buf, TXRX_BUF_LEN);      
  93.         int index;
  94.         for(index=0; index<bytesRead; index++)
  95.             pc.putc(buf[index]);
  96.            
  97.         if(buf[0] == 0x01)
  98.         {
  99.             if(buf[1] == 0x01)
  100.                 LED_SET = 1;
  101.             else
  102.                 LED_SET = 0;    
  103.         }
  104.         else if(buf[0] == 0xA0)
  105.         {
  106.             if(buf[1] == 0x01)
  107.                 analog_enabled = 1;
  108.             else
  109.                 analog_enabled = 0;
  110.         }
  111.         else if(buf[0] == 0x02)
  112.         {
  113.             float value = (float)buf[1]/255;
  114.             PWM = value;
  115.         }
  116.         else if(buf[0] == 0x03)
  117.         {
  118.             MYSERVO.write(buf[1]);
  119.         }
  120.         else if(buf[0] == 0x04)
  121.         {
  122.             analog_enabled = 0;
  123.             PWM = 0;
  124.             MYSERVO.write(0);
  125.             LED_SET = 0;
  126.             old_state = 0;    
  127.         }
  128.  
  129.     }
  130. }
  131.  
  132. void uartCB(void)
  133. {  
  134.     while(pc.readable())    
  135.     {
  136.         rx_buf[rx_len++] = pc.getc();    
  137.         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
  138.         {
  139.             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
  140.             pc.printf("RecHandler \r\n");
  141.             pc.printf("Length: ");
  142.             pc.putc(rx_len);
  143.             pc.printf("\r\n");
  144.             rx_len = 0;
  145.             break;
  146.         }
  147.     }
  148. }
  149.  
  150. void m_status_check_handle(void)
  151. {  
  152.     uint8_t buf[3];
  153.     if (analog_enabled)  // if analog reading enabled
  154.     {
  155.         // Read and send out
  156.         float s = ANALOG;
  157.         uint16_t value = s*1024;
  158.         buf[0] = (0x0B);
  159.         buf[1] = (value >> 8);
  160.         buf[2] = (value);
  161.         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
  162.     }
  163.    
  164.     // If digital in changes, report the state
  165.     if (BUTTON != old_state)
  166.     {
  167.         old_state = BUTTON;
  168.        
  169.         if (BUTTON == 1)
  170.         {
  171.             buf[0] = (0x0A);
  172.             buf[1] = (0x01);
  173.             buf[2] = (0x00);    
  174.             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
  175.         }
  176.         else
  177.         {
  178.             buf[0] = (0x0A);
  179.             buf[1] = (0x00);
  180.             buf[2] = (0x00);
  181.            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
  182.         }
  183.     }
  184. }
  185.  
  186.  
  187. int main(void)
  188. {  
  189.     Ticker ticker;
  190.     ticker.attach_us(m_status_check_handle, 200000);
  191.    
  192.     ble.init();
  193.     ble.onDisconnection(disconnectionCallback);
  194.     ble.onDataWritten(WrittenHandler);  
  195.    
  196.     pc.baud(9600);
  197.     pc.printf("SimpleChat Init \r\n");
  198.  
  199.     pc.attach( uartCB , pc.RxIrq);
  200.    
  201.     pc.printf("buffer setup at %p", buffer);
  202.    
  203.     // setup advertising
  204.     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
  205.     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
  206.     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
  207.                                     (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
  208.     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
  209.                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
  210.     // 100ms; in multiples of 0.625ms.
  211.     ble.setAdvertisingInterval(160);
  212.  
  213.     ble.addService(uartService);
  214.    
  215.     ble.startAdvertising();
  216.    
  217.     pc.printf("Advertising Start \r\n");
  218.    
  219.     while(1)
  220.     {
  221.         ble.waitForEvent();
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement