esp-nimble-cpp/examples/mesh/basic_on_off_level_server/main/main.cpp

65 lines
1.9 KiB
C++
Raw Permalink Normal View History

2020-08-27 18:55:24 +02:00
#include "NimBLEDevice.h"
#include "driver/gpio.h"
extern "C" {void app_main(void);}
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
// LED pins
#define LEDR GPIO_NUM_16
#define LEDG GPIO_NUM_17
#define LEDB GPIO_NUM_18
#define OUTPUT_PIN (1ULL<<LEDG)
static uint8_t onOffVal = 0;
static int16_t levelVal = 0;
class onOffSrvModelCallbacks : public NimBLEMeshModelCallbacks {
2020-09-07 18:32:35 +02:00
void setOnOff(NimBLEMeshModel *pModel, uint8_t val) {
printf("on/off set val %d, transition time: %dms\n", val, pModel->getTransTime());
2020-08-27 18:55:24 +02:00
onOffVal = val;
gpio_set_level(LEDG, !onOffVal);
}
2020-09-07 18:32:35 +02:00
uint8_t getOnOff(NimBLEMeshModel *pModel) {
printf("on/off get val %d\n", onOffVal);
2020-08-27 18:55:24 +02:00
return onOffVal;
}
};
class levelSrvModelCallbacks : public NimBLEMeshModelCallbacks {
2020-09-07 18:32:35 +02:00
void setLevel(NimBLEMeshModel *pModel, int16_t val) {
printf("Level set val %d, transition time: %dms\n", val, pModel->getTransTime());
2020-08-27 18:55:24 +02:00
levelVal = val;
}
2020-09-07 18:32:35 +02:00
int16_t getLevel(NimBLEMeshModel *pModel) {
2020-08-27 18:55:24 +02:00
printf("Level get val %d\n", levelVal);
return levelVal;
}
};
void app_main(void) {
gpio_config_t io_conf;
io_conf.intr_type = (gpio_int_type_t)GPIO_PIN_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = OUTPUT_PIN;
io_conf.pull_down_en = (gpio_pulldown_t)0;
io_conf.pull_up_en = (gpio_pullup_t)0;
2020-09-07 18:32:35 +02:00
gpio_config(&io_conf);
2020-08-27 18:55:24 +02:00
gpio_set_level(LEDG, 1);
2020-09-07 18:32:35 +02:00
2020-08-27 18:55:24 +02:00
NimBLEDevice::init("");
NimBLEMeshNode *pMesh = NimBLEDevice::createMeshNode(NimBLEUUID(SERVICE_UUID),0);
NimBLEMeshElement* pElem = pMesh->getElement();
2020-09-07 18:32:35 +02:00
NimBLEMeshModel* pModel = pElem->createModel(BT_MESH_MODEL_ID_GEN_ONOFF_SRV, new onOffSrvModelCallbacks());
pModel->setValue(onOffVal);
2020-08-27 18:55:24 +02:00
//pElem = pMesh->createElement();
2020-09-07 18:32:35 +02:00
pModel = pElem->createModel(BT_MESH_MODEL_ID_GEN_LEVEL_SRV, new levelSrvModelCallbacks());
pModel->setValue(levelVal);
2020-08-27 18:55:24 +02:00
pMesh->start();
2020-09-07 18:32:35 +02:00
printf("Mesh Started");
2020-08-27 18:55:24 +02:00
}