Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>
  4. #include <BLEServer.h>
  5. #include <BLE2902.h>
  6.  
  7. BLEDevice ble;
  8. BLECharacteristic *measurement;
  9. bool deviceConnected = false;
  10.  
  11.  
  12. class MyServerCallbacks: public BLEServerCallbacks {
  13.     void onConnect(BLEServer* pServer) {
  14.       deviceConnected = true;
  15.     };
  16.  
  17.     void onDisconnect(BLEServer* pServer) {
  18.       deviceConnected = false;
  19.     }
  20. };
  21.  
  22.  
  23. static char feat[2] = {0x03, 0x00};
  24. static char measure[12] = {0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00};
  25. uint8_t pos = 6;
  26.  
  27. static uint8_t test_service_uuid128[32] = {
  28.     /* LSB <--------------------------------------------------------------------------------> MSB */
  29.     //first uuid, 16bit, [12],[13] is the value
  30.     0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0x00, 0x00,
  31.     //second uuid, 32bit, [12], [13], [14], [15] is the value
  32.     0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0xAB, 0xCD,
  33. };
  34.  
  35. /* LED pin */
  36. byte ledPin = 14;
  37. /* pin that is attached to interrupt */
  38. byte interruptPin = 25;
  39. /* hold the state of LED when toggling */
  40. volatile byte state = LOW;
  41.  
  42. unsigned long timeold = 0;
  43. unsigned long braketime = 0;
  44. unsigned long pulses = 0;
  45.  
  46. //union { uint32_t longrotations; uint8_t rotations[4]; } wheel;
  47.  
  48. //union { uint16_t longrotations; uint8_t rotations[2]; } crank;
  49.  
  50. unsigned long wheel;
  51.  
  52. unsigned long crank;
  53.  
  54. int delta;
  55. byte accel = 0;
  56. byte brake = 0;
  57.  
  58. void setup() {
  59.   Serial.begin(115200);
  60.   Serial.println("Starting BLE work!");
  61.   pinMode(ledPin, OUTPUT);
  62.   /* set the interrupt pin as input pullup*/
  63.   pinMode(interruptPin, INPUT_PULLUP);
  64.   pinMode(26, INPUT_PULLUP);
  65.   /* attach interrupt to the pin
  66.   function blink will be invoked when interrupt occurs
  67.   interrupt occurs whenever the pin change value */
  68.   attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
  69.   Wire.begin(21,22);
  70.   timeold = millis();
  71.  
  72.  
  73.   ble.init("MyESP32");
  74.   BLEServer *pServer = new BLEServer();
  75.   pServer->setCallbacks(new MyServerCallbacks());
  76.  
  77.   BLEService *deviceinfo = pServer->createService(BLEUUID((uint16_t) 0x180A));
  78.  
  79.   BLECharacteristic *manu_name = deviceinfo->createCharacteristic(
  80.                                          BLEUUID((uint16_t) 0x2A29),
  81.                                          BLECharacteristic::PROPERTY_READ
  82.                                        );
  83.  
  84.  
  85.   BLEService *pService = pServer->createService(BLEUUID((uint16_t)0x1816));
  86.  
  87.   BLECharacteristic *feature = pService->createCharacteristic(
  88.                                          BLEUUID((uint16_t)0x2A5C),
  89.                                          BLECharacteristic::PROPERTY_READ
  90.                                        );
  91.  
  92.   BLECharacteristic *location = pService->createCharacteristic(
  93.                                          BLEUUID((uint16_t)0x2A5D),
  94.                                          BLECharacteristic::PROPERTY_READ
  95.                                        );
  96.  
  97.   BLECharacteristic *controlpoint = pService->createCharacteristic(
  98.                                          BLEUUID((uint16_t) 0x2A55),
  99.                                          BLECharacteristic::PROPERTY_WRITE |
  100.                                          BLECharacteristic::PROPERTY_INDICATE
  101.                                          );
  102.  
  103.   measurement = pService->createCharacteristic(
  104.                                          BLEUUID((uint16_t) 0x2A5B),
  105.                                          BLECharacteristic::PROPERTY_NOTIFY
  106.                                        );
  107.  
  108.   measurement->addDescriptor(new BLE2902());
  109.   manu_name->setValue("Jabberwock");
  110.   feature->setValue((uint8_t*)feat, sizeof(feat));
  111.   location->setValue(&pos, 1);
  112.   measurement->setValue(std::string(measure, sizeof(measure)));
  113.  
  114.   pService->start();
  115.   deviceinfo->start();
  116.  
  117. //  attr.icon = 0x485;
  118.   //GAP_BleAttrDBUpdate(GATT_UUID_GAP_ICON, &attr);
  119.  
  120.   BLEAdvertising *pAdvertising = pServer->getAdvertising();
  121.   pAdvertising->setAppearance(1157);
  122.   Serial.println("Check");
  123.   pAdvertising->setServiceUUID("00001816-0000-1000-8000-00805f9b34fb");
  124.   pAdvertising->start();
  125.   Serial.println("Characteristic defined! Now you can read it in your phone!!!");
  126.  }
  127.  
  128. void loop() {
  129.  
  130.    if ((millis() % 1000) == 0) {
  131.      if (deviceConnected) {
  132.             measurement->setValue(std::string(measure, sizeof(measure)));
  133.             measurement->notify();
  134.           }
  135.  }
  136.  
  137.   if (millis() - braketime > 200) {
  138.     brake = 0;
  139.   }
  140.   if (millis() - timeold > 300) {
  141.     delta = 300;
  142.   }
  143.  
  144.   accel = map(constrain (delta, 100, 200), 100, 200, 0, 255);
  145.  
  146.   Wire.beginTransmission(0x28); // transmit to device #44 (0x2c)
  147.   Wire.write(0xAA);            // sends instruction byte  
  148.   Wire.write(accel);             // sends potentiometer value byte  
  149.   Wire.endTransmission();     // stop transmitting
  150.  
  151.   Wire.beginTransmission(0x28); // transmit to device #44 (0x2c)
  152.   Wire.write(0xA9);            // sends instruction byte  
  153.   Wire.write(brake);             // sends potentiometer value byte  
  154.   Wire.endTransmission();     // stop transmitting
  155.  
  156.  
  157. }
  158.  
  159. /* interrupt function toggle the LED */
  160. void blink() {
  161.   state = !state;
  162.   digitalWrite(ledPin, state);
  163.   Serial.println("Interrupted!");
  164.  
  165.     if (digitalRead(26) == HIGH) {
  166.  
  167.         pulses = pulses + 1;
  168.        
  169.         if ((pulses % 3) == 0) {
  170.           wheel = wheel + 1;
  171.           measure[1]= wheel;
  172.           measure[2]= wheel >> 8;
  173.           measure[3]= wheel >> 16;
  174.           measure[4]= wheel >> 24;
  175.           measure[5]= millis();
  176.           measure[6]= millis() >> 8;
  177.  
  178.           if (deviceConnected) {
  179.             measurement->setValue(std::string(measure, sizeof(measure)));
  180.             measurement->notify();
  181.           }
  182.         }
  183.  
  184.        if ((pulses % 8) == 0) {
  185.          crank = crank + 1;
  186.          measure[7]= crank;
  187.          measure[8]= crank >> 8;
  188.          measure[9]= millis();
  189.          measure[10]= millis() >> 8;
  190.  
  191.          if (deviceConnected) {
  192.             measurement->setValue(std::string(measure, sizeof(measure)));
  193.             measurement->notify();
  194.          }
  195.        }
  196.       delta = millis() - timeold;
  197.       timeold = millis();
  198.       brake = 0;
  199.   }
  200.   else
  201.   {
  202.      braketime = millis();
  203.      if (brake > 127) {
  204.       brake = 255;
  205.     }
  206.     else
  207.     {
  208.       brake = 128;
  209.       //  accel = 0;
  210.     }
  211.   }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement