1. #include <Wire.h>
  2. #include <BLEDevice.h>
  3. #include <BLEServer.h>
  4. #include <BLEUtils.h>
  5. #include <BLE2902.h>
  6. #define I2C_SDA 21 // Nuncuck SDA
  7. #define I2C_CLK 22 // Nuncuck CLK
  8. BLECharacteristic *pCharacteristic;
  9. bool deviceConnected = false;
  10. uint8_t nunbuff[10]; // array to store ESP output
  11. int cnt = 0;
  12. int x_dat = 0; // 1byte Data
  13. int y_dat = 0; // 1byte Data
  14. int z_dat = 0; // 1byte Data
  15. #define SERVICE_UUID "cdb068be-4458-11e9-b210-d663bd873d93"
  16. #define CHARACTERISTIC_UUID "cdb06c10-4458-11e9-b210-d663bd873d93"
  17. #define DEVICE_NAME "esp32_BLE"
  18. class MyServerCallbacks: public BLEServerCallbacks {
  19.     void onConnect(BLEServer* pServer) {
  20.       deviceConnected = true;
  21.     };
  22.     void onDisconnect(BLEServer* pServer) {
  23.       deviceConnected = false;
  24.     }
  25. };
  26. void setup() {
  27.   Serial.begin(115200);
  28.   Wire.begin (); // join i2c bus with address 0x52
  29.   nunchuck_init (); // send the initilization handshake
  30.   BLEDevice::init(DEVICE_NAME);
  31.   BLEServer *pServer = BLEDevice::createServer();
  32.   pServer->setCallbacks(new MyServerCallbacks());
  33.   BLEService *pService = pServer->createService(SERVICE_UUID);
  34.   pCharacteristic = pService->createCharacteristic(
  35.                       CHARACTERISTIC_UUID,
  36.                       BLECharacteristic::PROPERTY_READ |
  37.                       BLECharacteristic::PROPERTY_WRITE |
  38.                       BLECharacteristic::PROPERTY_NOTIFY |
  39.                       BLECharacteristic::PROPERTY_INDICATE
  40.                     );
  41.   pCharacteristic->addDescriptor(new BLE2902());
  42.   pService->start();
  43.   pServer->getAdvertising()->start();
  44.   Serial.println("Waiting a client connection to notify...");
  45. }
  46. void nunchuck_init () {
  47.   Wire.beginTransmission (0x52); // transmit to device 0x52
  48.   Wire.write (0x40); // sends memory address *(blackNunchuck:0xF0)
  49.   Wire.write (0x00); // sends sent a zero. *(blackNunchuck:0x55)
  50.   Wire.endTransmission (); // stop transmitting
  51. }
  52. void send_zero () {
  53.   Wire.beginTransmission (0x52); // transmit to device 0x52
  54.   Wire.write (0x00); // sends one byte
  55.   Wire.endTransmission (); // stop transmitting
  56. }
  57. void loop() {
  58.   Wire.requestFrom (0x52, 6); // request data from nunchuck
  59.   while (Wire.available ()) {
  60.     nunbuff[cnt] = nunchuk_decode_byte (Wire.read ());
  61.     cnt++;
  62.   }
  63.   int z = 0;
  64.   int c = 0;
  65.   if (cnt >= 5) {
  66.     x_dat = nunbuff[0];
  67.     y_dat = nunbuff[1];
  68.     //x_dat=map(x_dat,49,205,0,255);
  69.     //y_dat=map(y_dat,49,205,0,255);
  70.     Serial.print("x_dat= ");
  71.     Serial.print(x_dat);
  72.     Serial.print(", y_dat= ");
  73.     Serial.println(y_dat);
  74.     if ((nunbuff[5] >> 0) & 1) z = 1; // The first bit of data of the 5byte (Nuncuck Z-button)
  75.     if ((nunbuff[5] >> 1) & 1) c = 1; // The second bit of data of the 5byte (Nuncuck C-button)
  76.     if ((z == 1) & (c == 1))z_dat = 0;
  77.     if ((z == 0) & (c == 1))z_dat = 1;
  78.     if ((z == 1) & (c == 0))z_dat = 2;
  79.     if ((z == 0) & (c == 0))z_dat = 3;
  80.     delay(1);
  81.   }
  82.   cnt = 0;
  83.   send_zero (); // send the request for next bytes
  84.   delay (10);
  85.   if (deviceConnected) {
  86.     char str1 = x_dat;
  87.     char str2 = y_dat;
  88.     char str3 = z_dat;
  89.     char str4[10];
  90.     sprintf(str4, "%d,%d,%d\n", str1, str2, str3);
  91.     printf("%s", str4);
  92.     pCharacteristic->setValue(str4);
  93.     pCharacteristic->notify();
  94.     delay (10);
  95.   }
  96. }
  97. char nunchuk_decode_byte (char x) {
  98.   x = (x ^ 0x17) + 0x17;
  99.   return x;
  100. }