ESP32C6 RGB Builtin LED and BLE

fbigrat
Posts: 5
Joined: Tue Nov 28, 2023 2:31 pm

ESP32C6 RGB Builtin LED and BLE

Postby fbigrat » Tue Nov 28, 2023 2:43 pm

Hi every one.
I want to display the state of a BLE connection (connected or not) by using the builtin RGB Led (Green; connected; Red : Disconnected).
To do so i use the RMT as said in this example : https://espressif-docs.readthedocs-host ... /rmt.html.
But this does not seem to work whith BLE.
Here is my code.
Is something wrong, or theses ressources simply cannot work together ? Is ther another way to pilot thr RGB LED ?
Thanks
FB
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4.  
  5. #define sensorPin 34
  6.  
  7. #define SERVICE_UUID                      "9aab5446-bb20-11ec-8422-0242ac120002"
  8. #define CHARACTERISTIC_UUID_Read_Write    "9aab56bc-bb20-11ec-8422-0242ac120002"
  9.  
  10. #define BUILTIN_RGBLED_PIN   8   // RGB builtin Led on ESP32C6
  11.  
  12. #define NR_OF_LEDS   1
  13. #define NR_OF_ALL_BITS 24*NR_OF_LEDS
  14.  
  15. int sensorValue=0;
  16. int incomingByte = 0;   // for incoming serial data
  17. int n=1;
  18. String LedCommand="";
  19.  
  20. int color_On_Connect[] =  { 0xFF, 0x00, 0x00 };     // Green Red Blue values
  21. int color_On_Disconnect[] =  { 0x00, 0xFF, 0x00 };  // Green Red Blue values
  22. int color_On_Data[] =  { 0xFF, 0xFF, 0x00 };        // Green Red Blue values
  23. int color_Off[] =  { 0x00, 0x00, 0x00 };            // Green Red Blue values
  24.  
  25. rmt_data_t led_data[NR_OF_ALL_BITS];
  26.  
  27. void LED_Command(int*);
  28.  
  29. BLEServer *BLE_Server = NULL;
  30. BLECharacteristic *OnReadWriteCharacteristic;
  31. BLEService *BLE_Service;
  32. BLEAdvertising *BLE_Advertising;
  33.  
  34. bool deviceConnected = false;
  35. class MyServerCallbacks: public BLEServerCallbacks
  36. {
  37.     void onConnect(BLEServer* pServer)
  38.     {
  39.       deviceConnected = true;
  40.       BLEDevice::stopAdvertising();
  41.       //Serial.println("BT connecté");
  42.       LED_Command(color_On_Connect);
  43.       delay(1000);
  44.       LED_Command(color_Off);
  45.     };
  46.  
  47.     void onDisconnect(BLEServer* pServer)
  48.     {
  49.       deviceConnected = false;
  50.       BLEDevice::startAdvertising();
  51.       //Serial.println("BT NON connecté");
  52.       LED_Command(color_On_Disconnect);
  53.       delay(1000);
  54.       LED_Command(color_Off);
  55.     }
  56.  
  57. };
  58.  
  59. class MyReadWriteCallbacks: public BLECharacteristicCallbacks
  60. {
  61.     void onWrite(BLECharacteristic *pOnWrite)
  62.     {
  63.      
  64.       String value = pOnWrite->getValue();
  65.  
  66.       if (value.length() > 0) {
  67.         LedCommand = "";
  68.         for (int i = 0; i < value.length(); i++){
  69.              LedCommand = LedCommand + value[i];
  70.         }
  71.  
  72.         if (LedCommand.equals("on")) LED_Command(color_On_Data);
  73.         if (LedCommand.equals("off")) LED_Command(color_Off);
  74.       }
  75.      
  76.     }
  77.    
  78.     void onRead(BLECharacteristic *pOnRead)
  79.     {
  80.       /*
  81.       sensorValue=analogRead(sensorPin);
  82.       pOnRead->setValue(("Data Nr " + String(n) + " : " + String(sensorValue)).c_str());
  83.       pOnRead->notify();
  84.       n++;
  85.       */
  86.     }
  87.  
  88. };
  89.  
  90.  
  91. void setup() {
  92.  
  93.   rmtInit(BUILTIN_RGBLED_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000);
  94.  
  95.   LED_Command(color_Off);
  96.  
  97.   BLEDevice::init("ESP32_BLE_Test");
  98.  
  99.   BLEDevice::setMTU(256);
  100.  
  101.   BLE_Server = BLEDevice::createServer();
  102.  
  103.   BLE_Server->setCallbacks(new MyServerCallbacks());    
  104.  
  105.   BLE_Service = BLE_Server->createService(SERVICE_UUID);
  106.  
  107.   OnReadWriteCharacteristic = BLE_Service->createCharacteristic(
  108.                                          CHARACTERISTIC_UUID_Read_Write,
  109.                                          BLECharacteristic::PROPERTY_READ |
  110.                                          BLECharacteristic::PROPERTY_WRITE                                        
  111.                                        );
  112.  
  113.   OnReadWriteCharacteristic->setCallbacks(new MyReadWriteCallbacks());  
  114.  
  115.   BLE_Service->start();
  116.  
  117.   //Send_Data_Characteristic->setValue("Bonjour le monde...");
  118.  
  119.   BLE_Advertising = BLEDevice::getAdvertising();
  120.  
  121.   BLE_Advertising->addServiceUUID(SERVICE_UUID);
  122.  
  123.   BLE_Advertising->setScanResponse(true);
  124.  
  125.   BLE_Advertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue //0x06
  126.   BLE_Advertising->setMaxPreferred(0x12);  // 0x12
  127.  
  128.   BLEDevice::startAdvertising();
  129.  
  130. }
  131.  
  132. void loop() {
  133.   // put your main code here, to run repeatedly:
  134.  
  135. }
  136.  
  137. void LED_Command(int* color){
  138.   //
  139. // Note: This example uses Neopixel LED board, 32 LEDs chained one
  140. //      after another, each RGB LED has its 24 bit value
  141. //      for color configuration (8b for each color)
  142. //
  143. //      Bits encoded as pulses as follows:
  144. //
  145. //      "0":
  146. //         +-------+              +--
  147. //         |       |              |
  148. //         |       |              |
  149. //         |       |              |
  150. //      ---|       |--------------|
  151. //         +       +              +
  152. //         | 0.4us |   0.85 0us   |
  153. //
  154. //      "1":
  155. //         +-------------+       +--
  156. //         |             |       |
  157. //         |             |       |
  158. //         |             |       |
  159. //         |             |       |
  160. //      ---+             +-------+
  161. //         |    0.8us    | 0.4us |
  162.  
  163.  // Init data with only one led ON
  164.     int led, col, bit;
  165.     int i=0;
  166.    
  167.         for (col=0; col<3; col++ ) {
  168.             for (bit=0; bit<8; bit++){
  169.                 if ( (color[col] & (1<<(7-bit)))) {
  170.                     led_data[i].level0 = 1;
  171.                     led_data[i].duration0 = 8;
  172.                     led_data[i].level1 = 0;
  173.                     led_data[i].duration1 = 4;
  174.                 } else {
  175.                     led_data[i].level0 = 1;
  176.                     led_data[i].duration0 = 4;
  177.                     led_data[i].level1 = 0;
  178.                     led_data[i].duration1 = 8;
  179.                 }
  180.                 i++;
  181.             }
  182.         }
  183.    
  184.     // Send the data and wait until it is done
  185.     rmtWriteAsync(BUILTIN_RGBLED_PIN, led_data, NR_OF_ALL_BITS);
  186. }

Who is online

Users browsing this forum: No registered users and 129 guests