|
| 1 | +#include <chrono> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include "ble_gatt_server.hpp" |
| 5 | +#include "gfps_service.hpp" |
| 6 | + |
| 7 | +using namespace std::chrono_literals; |
| 8 | + |
| 9 | +extern "C" void app_main(void) { |
| 10 | + espp::Logger logger({.tag = "Gfps Service Example", .level = espp::Logger::Verbosity::INFO}); |
| 11 | + logger.info("Starting"); |
| 12 | + |
| 13 | + //! [gfps service example] |
| 14 | + |
| 15 | + // NOTE: esp-nimble-cpp already depends on nvs_flash and initializes |
| 16 | + // nvs_flash in the NimBLEDevice::init(), so we don't have to do that |
| 17 | + // to store bonding info |
| 18 | + |
| 19 | + // create the GATT server |
| 20 | + espp::BleGattServer ble_gatt_server; |
| 21 | + std::string device_name = "ESP++ GFPS Example"; |
| 22 | + ble_gatt_server.set_log_level(espp::Logger::Verbosity::INFO); |
| 23 | + ble_gatt_server.set_callbacks({ |
| 24 | + .connect_callback = [&](NimBLEConnInfo &conn_info) { logger.info("Device connected"); }, |
| 25 | + .disconnect_callback = [&](NimBLEConnInfo &conn_info) { logger.info("Device disconnected"); }, |
| 26 | + .authentication_complete_callback = |
| 27 | + [&](NimBLEConnInfo &conn_info) { logger.info("Device authenticated"); }, |
| 28 | + }); |
| 29 | + ble_gatt_server.init(device_name); |
| 30 | + ble_gatt_server.set_advertise_on_disconnect(true); |
| 31 | + |
| 32 | + // let's create a GFPS service |
| 33 | + espp::GfpsService gfps_service; |
| 34 | + gfps_service.init(ble_gatt_server.server()); |
| 35 | + |
| 36 | + // now that we've made the input characteristic, we can start the service |
| 37 | + gfps_service.start(); |
| 38 | + ble_gatt_server.start_services(); // starts the device info service and battery service |
| 39 | + // NOTE: we could also directly start them ourselves if we wanted to |
| 40 | + // control the order of starting the services |
| 41 | + // e.g.: |
| 42 | + // ble_gatt_server.battery_service().start(); |
| 43 | + // ble_gatt_server.device_info_service().start(); |
| 44 | + |
| 45 | + // now start the gatt server |
| 46 | + ble_gatt_server.start(); |
| 47 | + |
| 48 | + // let's set some of the service data |
| 49 | + auto &battery_service = ble_gatt_server.battery_service(); |
| 50 | + battery_service.set_battery_level(99); |
| 51 | + |
| 52 | + auto &device_info_service = ble_gatt_server.device_info_service(); |
| 53 | + uint8_t vendor_source = 0x02; // USB |
| 54 | + uint16_t vid = 0x045E; // Microsoft |
| 55 | + uint16_t pid = 0x02FD; // Xbox One Controller |
| 56 | + uint16_t product_version = 0x0100; |
| 57 | + device_info_service.set_pnp_id(vendor_source, vid, pid, product_version); |
| 58 | + device_info_service.set_manufacturer_name("ESP-CPP"); |
| 59 | + // NOTE: this is NOT required to be the same as the GFPS SKU Name |
| 60 | + device_info_service.set_model_number("espp-gfps-01"); |
| 61 | + device_info_service.set_serial_number("1234567890"); |
| 62 | + device_info_service.set_software_version("1.0.0"); |
| 63 | + device_info_service.set_firmware_version("1.0.0"); |
| 64 | + device_info_service.set_hardware_version("1.0.0"); |
| 65 | + |
| 66 | + // now lets start advertising |
| 67 | + espp::BleGattServer::AdvertisingData adv_data = { |
| 68 | + .name = device_name, |
| 69 | + .appearance = 0x03C4, // Gamepad |
| 70 | + .services = {}, |
| 71 | + .service_data = |
| 72 | + {// these are the service data that we want to advertise |
| 73 | + {gfps_service.uuid(), gfps_service.get_service_data()}}, |
| 74 | + }; |
| 75 | + espp::BleGattServer::AdvertisingParameters adv_params = {}; |
| 76 | + ble_gatt_server.start_advertising(adv_data, adv_params); |
| 77 | + |
| 78 | + // now lets update the battery level every second |
| 79 | + uint8_t battery_level = 99; |
| 80 | + while (true) { |
| 81 | + auto start = std::chrono::steady_clock::now(); |
| 82 | + |
| 83 | + // update the battery level |
| 84 | + battery_service.set_battery_level(battery_level); |
| 85 | + battery_level = (battery_level % 100) + 1; |
| 86 | + |
| 87 | + // sleep |
| 88 | + std::this_thread::sleep_until(start + 1s); |
| 89 | + } |
| 90 | + //! [gfps service example] |
| 91 | +} |
0 commit comments