Skip to content

Commit 23f653a

Browse files
authored
General examples updates (#7727)
* Remove inline code comments for the struct description * Renamed ESPNow examples * Changed example from C++ class to C struct * Updated MultiHomedServers * Updated BT Serial examples * Added short overview of exaples - missing Enterprise * Added draft of Enterprise header note * Fixed note in README * Fixed compilation error * Changed FunctionalInterrupt example * Fixes * Returned FunctionalInterrupts to original state + added modified version as new example
1 parent 12bd525 commit 23f653a

File tree

14 files changed

+301
-152
lines changed

14 files changed

+301
-152
lines changed

libraries/BLE/examples/BLE5_extended_scan/BLE5_extended_scan.ino

-20
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@
1818
uint32_t scanTime = 100; //In 10ms (1000ms)
1919
BLEScan* pBLEScan;
2020

21-
/**
22-
* @brief extend adv report parameters
23-
*/
24-
//typedef struct {
25-
// esp_ble_gap_adv_type_t event_type; /*!< extend advertising type */
26-
// uint8_t addr_type; /*!< extend advertising address type */
27-
// esp_bd_addr_t addr; /*!< extend advertising address */
28-
// esp_ble_gap_pri_phy_t primary_phy; /*!< extend advertising primary phy */
29-
// esp_ble_gap_phy_t secondly_phy; /*!< extend advertising secondary phy */
30-
// uint8_t sid; /*!< extend advertising sid */
31-
// uint8_t tx_power; /*!< extend advertising tx power */
32-
// int8_t rssi; /*!< extend advertising rssi */
33-
// uint16_t per_adv_interval; /*!< periodic advertising interval */
34-
// uint8_t dir_addr_type; /*!< direct address type */
35-
// esp_bd_addr_t dir_addr; /*!< direct address */
36-
// esp_ble_gap_ext_adv_data_status_t data_status; /*!< data type */
37-
// uint8_t adv_data_len; /*!< extend advertising data length */
38-
// uint8_t adv_data[251]; /*!< extend advertising data */
39-
//} esp_ble_gap_ext_adv_reprot_t;
40-
4121
class MyBLEExtAdvertisingCallbacks: public BLEExtAdvertisingCallbacks {
4222
void onResult(esp_ble_gap_ext_adv_reprot_t report) {
4323
if(report.event_type & ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY){

libraries/BLE/examples/BLE5_periodic_sync/BLE5_periodic_sync.ino

-20
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,6 @@ static esp_ble_gap_periodic_adv_sync_params_t periodic_adv_sync_params = {
2525
.sync_timeout = 1000, // timeout: 1000 * 10ms
2626
};
2727

28-
/**
29-
* @brief extend adv report parameters
30-
*/
31-
//typedef struct {
32-
// esp_ble_gap_adv_type_t event_type; /*!< extend advertising type */
33-
// uint8_t addr_type; /*!< extend advertising address type */
34-
// esp_bd_addr_t addr; /*!< extend advertising address */
35-
// esp_ble_gap_pri_phy_t primary_phy; /*!< extend advertising primary phy */
36-
// esp_ble_gap_phy_t secondly_phy; /*!< extend advertising secondary phy */
37-
// uint8_t sid; /*!< extend advertising sid */
38-
// uint8_t tx_power; /*!< extend advertising tx power */
39-
// int8_t rssi; /*!< extend advertising rssi */
40-
// uint16_t per_adv_interval; /*!< periodic advertising interval */
41-
// uint8_t dir_addr_type; /*!< direct address type */
42-
// esp_bd_addr_t dir_addr; /*!< direct address */
43-
// esp_ble_gap_ext_adv_data_status_t data_status; /*!< data type */
44-
// uint8_t adv_data_len; /*!< extend advertising data length */
45-
// uint8_t adv_data[251]; /*!< extend advertising data */
46-
//} esp_ble_gap_ext_adv_reprot_t;
47-
4828
class MyBLEExtAdvertisingCallbacks : public BLEExtAdvertisingCallbacks
4929
{
5030
void onResult(esp_ble_gap_ext_adv_reprot_t params)

libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
#include "BluetoothSerial.h"
88

9+
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
10+
const char *pin = "1234"; // Change this to more secure PIN.
11+
12+
String device_name = "ESP32-BT-Slave";
13+
914
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
1015
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
1116
#endif
@@ -18,8 +23,13 @@ BluetoothSerial SerialBT;
1823

1924
void setup() {
2025
Serial.begin(115200);
21-
SerialBT.begin("ESP32test"); //Bluetooth device name
22-
Serial.println("The device started, now you can pair it with bluetooth!");
26+
SerialBT.begin(device_name); //Bluetooth device name
27+
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
28+
//Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
29+
#ifdef USE_PIN
30+
SerialBT.setPin(pin);
31+
Serial.println("Using PIN");
32+
#endif
2333
}
2434

2535
void loop() {

libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino

+56-27
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,81 @@
1-
//This example code is in the Public Domain (or CC0 licensed, at your option.)
2-
//By Victor Tchistiak - 2019
1+
// This example code is in the Public Domain (or CC0 licensed, at your option.)
2+
// By Victor Tchistiak - 2019
33
//
4-
//This example demostrates master mode bluetooth connection and pin
5-
//it creates a bridge between Serial and Classical Bluetooth (SPP)
6-
//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018
4+
// This example demonstrates master mode Bluetooth connection to a slave BT device using PIN (password)
5+
// defined either by String "slaveName" by default "OBDII" or by MAC address
76
//
7+
// This example creates a bridge between Serial and Classical Bluetooth (SPP)
8+
// This is an extension of the SerialToSerialBT example by Evandro Copercini - 2018
9+
//
10+
// DO NOT try to connect to phone or laptop - they are master
11+
// devices, same as the ESP using this code - it will NOT work!
12+
//
13+
// You can try to flash a second ESP32 with the example SerialToSerialBT - it should
14+
// automatically pair with ESP32 running this code
815

916
#include "BluetoothSerial.h"
1017

18+
#define USE_NAME // Comment this to use MAC address instead of a slaveName
19+
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
20+
1121
#if !defined(CONFIG_BT_SPP_ENABLED)
1222
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
1323
#endif
1424

1525
BluetoothSerial SerialBT;
1626

17-
String MACadd = "AA:BB:CC:11:22:33";
18-
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
19-
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
20-
String name = "OBDII";
21-
const char *pin = "1234"; //<- standard pin would be provided by default
22-
bool connected;
27+
#ifdef USE_NAME
28+
String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
29+
#else
30+
String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
31+
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device
32+
#endif
33+
34+
String myName = "ESP32-BT-Master";
2335

2436
void setup() {
37+
bool connected;
2538
Serial.begin(115200);
26-
//SerialBT.setPin(pin);
27-
SerialBT.begin("ESP32test", true);
28-
//SerialBT.setPin(pin);
29-
Serial.println("The device started in master mode, make sure remote BT device is on!");
30-
31-
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
32-
// to resolve name to address first, but it allows to connect to different devices with the same name.
33-
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
34-
connected = SerialBT.connect(name);
35-
//connected = SerialBT.connect(address);
36-
39+
40+
SerialBT.begin(myName, true);
41+
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
42+
43+
#ifndef USE_NAME
44+
SerialBT.setPin(pin);
45+
Serial.println("Using PIN");
46+
#endif
47+
48+
// connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
49+
// to resolve slaveName to address first, but it allows to connect to different devices with the same name.
50+
// Set CoreDebugLevel to Info to view devices Bluetooth address and device names
51+
#ifdef USE_NAME
52+
connected = SerialBT.connect(slaveName);
53+
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
54+
#else
55+
connected = SerialBT.connect(address);
56+
Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
57+
#endif
58+
3759
if(connected) {
38-
Serial.println("Connected Succesfully!");
60+
Serial.println("Connected Successfully!");
3961
} else {
4062
while(!SerialBT.connected(10000)) {
41-
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
63+
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
4264
}
4365
}
44-
// disconnect() may take upto 10 secs max
66+
// Disconnect() may take up to 10 secs max
4567
if (SerialBT.disconnect()) {
46-
Serial.println("Disconnected Succesfully!");
68+
Serial.println("Disconnected Successfully!");
4769
}
48-
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
70+
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
4971
SerialBT.connect();
72+
if(connected) {
73+
Serial.println("Reconnected Successfully!");
74+
} else {
75+
while(!SerialBT.connected(10000)) {
76+
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
77+
}
78+
}
5079
}
5180

5281
void loop() {

libraries/ESP32/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino

+22-22
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@
77
class Button
88
{
99
public:
10-
Button(uint8_t reqPin) : PIN(reqPin){
11-
pinMode(PIN, INPUT_PULLUP);
12-
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
13-
};
14-
~Button() {
15-
detachInterrupt(PIN);
16-
}
17-
18-
void ARDUINO_ISR_ATTR isr() {
19-
numberKeyPresses += 1;
20-
pressed = true;
21-
}
22-
23-
void checkPressed() {
24-
if (pressed) {
25-
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
26-
pressed = false;
27-
}
28-
}
10+
Button(uint8_t reqPin) : PIN(reqPin){
11+
pinMode(PIN, INPUT_PULLUP);
12+
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
13+
};
14+
~Button() {
15+
detachInterrupt(PIN);
16+
}
17+
18+
void ARDUINO_ISR_ATTR isr() {
19+
numberKeyPresses += 1;
20+
pressed = true;
21+
}
22+
23+
void checkPressed() {
24+
if (pressed) {
25+
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
26+
pressed = false;
27+
}
28+
}
2929

3030
private:
31-
const uint8_t PIN;
31+
const uint8_t PIN;
3232
volatile uint32_t numberKeyPresses;
3333
volatile bool pressed;
3434
};
@@ -42,6 +42,6 @@ void setup() {
4242
}
4343

4444
void loop() {
45-
button1.checkPressed();
46-
button2.checkPressed();
45+
button1.checkPressed();
46+
button2.checkPressed();
4747
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <Arduino.h>
2+
3+
#define BUTTON1 16
4+
#define BUTTON2 17
5+
6+
struct Button {
7+
uint8_t PIN;
8+
volatile uint32_t numberKeyPresses;
9+
volatile int pressed;
10+
};
11+
12+
void isr(void* param) {
13+
struct Button *button = (struct Button*) param;
14+
button->numberKeyPresses += 1;
15+
button->pressed = 1;
16+
}
17+
18+
void checkPressed(struct Button* button) {
19+
if(button->pressed) {
20+
Serial.printf("Button on pin %u has been pressed %u times\n", button->PIN, button->numberKeyPresses);
21+
button->pressed = 0;
22+
}
23+
}
24+
25+
struct Button button1 = {BUTTON1, 0, 0};
26+
struct Button button2 = {BUTTON2, 0, 0};
27+
28+
void setup() {
29+
Serial.begin(115200);
30+
pinMode(button1.PIN, INPUT_PULLUP);
31+
pinMode(button2.PIN, INPUT_PULLUP);
32+
attachInterruptArg(button1.PIN, isr, (void*)&button1, FALLING);
33+
attachInterruptArg(button2.PIN, isr, (void*)&button2, FALLING);
34+
}
35+
36+
void loop() {
37+
checkPressed(&button1);
38+
checkPressed(&button2);
39+
}

0 commit comments

Comments
 (0)