- #include <Wire.h>
- #include <BLEDevice.h>
- #include <BLEServer.h>
- #include <BLEUtils.h>
- #include <BLE2902.h>
- #define I2C_SDA 21 // Nuncuck SDA
- #define I2C_CLK 22 // Nuncuck CLK
- BLECharacteristic *pCharacteristic;
- bool deviceConnected = false;
- uint8_t nunbuff[10]; // array to store ESP output
- int cnt = 0;
- int x_dat = 0; // 1byte Data
- int y_dat = 0; // 1byte Data
- int z_dat = 0; // 1byte Data
- #define SERVICE_UUID "cdb068be-4458-11e9-b210-d663bd873d93"
- #define CHARACTERISTIC_UUID "cdb06c10-4458-11e9-b210-d663bd873d93"
- #define DEVICE_NAME "esp32_BLE"
- class MyServerCallbacks: public BLEServerCallbacks {
- void onConnect(BLEServer* pServer) {
- deviceConnected = true;
- };
- void onDisconnect(BLEServer* pServer) {
- deviceConnected = false;
- }
- };
- void setup() {
- Serial.begin(115200);
- Wire.begin (); // join i2c bus with address 0x52
- nunchuck_init (); // send the initilization handshake
- BLEDevice::init(DEVICE_NAME);
- BLEServer *pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- BLEService *pService = pServer->createService(SERVICE_UUID);
- pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE |
- BLECharacteristic::PROPERTY_NOTIFY |
- BLECharacteristic::PROPERTY_INDICATE
- );
- pCharacteristic->addDescriptor(new BLE2902());
- pService->start();
- pServer->getAdvertising()->start();
- Serial.println("Waiting a client connection to notify...");
- }
- void nunchuck_init () {
- Wire.beginTransmission (0x52); // transmit to device 0x52
- Wire.write (0x40); // sends memory address *(blackNunchuck:0xF0)
- Wire.write (0x00); // sends sent a zero. *(blackNunchuck:0x55)
- Wire.endTransmission (); // stop transmitting
- }
- void send_zero () {
- Wire.beginTransmission (0x52); // transmit to device 0x52
- Wire.write (0x00); // sends one byte
- Wire.endTransmission (); // stop transmitting
- }
- void loop() {
- Wire.requestFrom (0x52, 6); // request data from nunchuck
- while (Wire.available ()) {
- nunbuff[cnt] = nunchuk_decode_byte (Wire.read ());
- cnt++;
- }
- int z = 0;
- int c = 0;
- if (cnt >= 5) {
- x_dat = nunbuff[0];
- y_dat = nunbuff[1];
- //x_dat=map(x_dat,49,205,0,255);
- //y_dat=map(y_dat,49,205,0,255);
- Serial.print("x_dat= ");
- Serial.print(x_dat);
- Serial.print(", y_dat= ");
- Serial.println(y_dat);
- if ((nunbuff[5] >> 0) & 1) z = 1; // The first bit of data of the 5byte (Nuncuck Z-button)
- if ((nunbuff[5] >> 1) & 1) c = 1; // The second bit of data of the 5byte (Nuncuck C-button)
- if ((z == 1) & (c == 1))z_dat = 0;
- if ((z == 0) & (c == 1))z_dat = 1;
- if ((z == 1) & (c == 0))z_dat = 2;
- if ((z == 0) & (c == 0))z_dat = 3;
- delay(1);
- }
- cnt = 0;
- send_zero (); // send the request for next bytes
- delay (10);
- if (deviceConnected) {
- char str1 = x_dat;
- char str2 = y_dat;
- char str3 = z_dat;
- char str4[10];
- sprintf(str4, "%d,%d,%d\n", str1, str2, str3);
- printf("%s", str4);
- pCharacteristic->setValue(str4);
- pCharacteristic->notify();
- delay (10);
- }
- }
- char nunchuk_decode_byte (char x) {
- x = (x ^ 0x17) + 0x17;
- return x;
- }