Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. // Copyright (c) Sandeep Mistry. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root
  3. for full license information.
  4.  
  5. //#define SHOW_FREE_MEMORY
  6.  
  7. #ifdef SHOW_FREE_MEMORY
  8. #include <MemoryFree.h>
  9. #endif
  10.  
  11. // Import libraries (BLEPeripheral depends on SPI)
  12. #include <SPI.h>
  13. #include <BLEPeripheral.h>
  14.  
  15. // define pins (varies per shield/board)
  16. #define BLE_REQ 0
  17. #define BLE_RDY 1
  18. #define BLE_RST 9
  19.  
  20. // create peripheral instance, see pinouts above
  21. BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
  22.  
  23. // create service
  24. BLEService testService = BLEService("fff0");
  25. // create counter characteristic
  26. BLEUnsignedShortCharacteristic testCharacteristic =
  27. BLEUnsignedShortCharacteristic("fff1", BLERead | BLEWrite |
  28. BLEWriteWithoutResponse | BLENotify /*| BLEIndicate*/);
  29.  
  30. // create user description descriptor for characteristic
  31. BLEDescriptor testDescriptor = BLEDescriptor("2901", "counter");
  32.  
  33. // last counter update time
  34. unsigned long long lastSent = 0;
  35.  
  36. void setup() {
  37. Serial.begin(9600);
  38. #if defined (__AVR_ATmega32U4__)
  39. delay(5000); //5 seconds delay for enabling to see the start up comments
  40. on the serial board
  41. #endif
  42.  
  43. blePeripheral.setLocalName("test");
  44. #if 1
  45. blePeripheral.setAdvertisedServiceUuid(testService.uuid());
  46. #else
  47. const char manufacturerData[4] = {0x12, 0x34, 0x56, 0x78};
  48. blePeripheral.setManufacturerData(manufacturerData,
  49. sizeof(manufacturerData));
  50. #endif
  51.  
  52. // set device name and appearance
  53. blePeripheral.setDeviceName("Test");
  54. blePeripheral.setAppearance(0x0080);
  55.  
  56. // add service, characteristic, and decriptor to peripheral
  57. blePeripheral.addAttribute(testService);
  58. blePeripheral.addAttribute(testCharacteristic);
  59. blePeripheral.addAttribute(testDescriptor);
  60.  
  61. // assign event handlers for connected, disconnected to peripheral
  62. blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  63. blePeripheral.setEventHandler(BLEDisconnected,
  64. blePeripheralDisconnectHandler);
  65.  
  66. // assign event handlers for characteristic
  67. testCharacteristic.setEventHandler(BLEWritten, characteristicWritten);
  68. testCharacteristic.setEventHandler(BLESubscribed,
  69. characteristicSubscribed);
  70. testCharacteristic.setEventHandler(BLEUnsubscribed,
  71. characteristicUnsubscribed);
  72.  
  73. // set initial value for characteristic
  74. testCharacteristic.setValue(0);
  75.  
  76. // begin initialization
  77. blePeripheral.begin();
  78.  
  79. Serial.println(F("BLE Peripheral"));
  80.  
  81. #ifdef SHOW_FREE_MEMORY
  82. Serial.print(F("Free memory = "));
  83. Serial.println(freeMemory());
  84. #endif
  85. }
  86.  
  87. void loop() {
  88. BLECentral central = blePeripheral.central();
  89.  
  90. if (central) {
  91. // central connected to peripheral
  92. Serial.print(F("Connected to central: "));
  93. Serial.println(central.address());
  94.  
  95. // reset counter value
  96. testCharacteristic.setValue(0);
  97.  
  98. while (central.connected()) {
  99. // central still connected to peripheral
  100. if (testCharacteristic.written()) {
  101. // central wrote new value to characteristic
  102. Serial.println(F("counter written, reset"));
  103.  
  104. // reset counter value
  105. lastSent = 0;
  106. testCharacteristic.setValue(0);
  107. }
  108.  
  109. if (millis() > 1000 && (millis() - 1000) > lastSent) {
  110. // atleast one second has passed since last increment
  111. lastSent = millis();
  112.  
  113. // increment characteristic value
  114. testCharacteristic.setValue(testCharacteristic.value() + 1);
  115.  
  116. Serial.print(F("counter = "));
  117. Serial.println(testCharacteristic.value(), DEC);
  118. }
  119. }
  120.  
  121. // central disconnected
  122. Serial.print(F("Disconnected from central: "));
  123. Serial.println(central.address());
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement