range ble control
This commit is contained in:
parent
ccf7ebf5e9
commit
4a2ae5ee1a
@ -2,12 +2,15 @@
|
||||
|
||||
#define METROLOGY_SERVICE 0x1878
|
||||
#define CONFIGURATION_SERVICE 0x1879
|
||||
#define METROLOGY_RANGE_SERVICE 0x1880
|
||||
|
||||
#define ELECTRIC_CURRENT_CHAR 0x2AEE
|
||||
#define VOLTAGE_CHAR 0x2B18
|
||||
#define SAMPLING_RATE_CHAR 0x2C12
|
||||
#define ZERO_CALI_CHAR 0x2C13
|
||||
#define ZERO_CALI_NSAMP 0x2C14
|
||||
#define ELECTRIC_CURRENT_RANGE_CHAR 0x2AEF
|
||||
#define AUTO_RANGE_CHAR 0x2C15
|
||||
|
||||
#define CHAR_PRES_FORMAT 0x2904
|
||||
|
||||
|
50
main/ble.c
50
main/ble.c
@ -38,6 +38,7 @@ static uint16_t conn_handle;
|
||||
static uint16_t cs_handle[INPUTS_NUM];
|
||||
static uint16_t ev_handle[INPUTS_NUM];
|
||||
static uint16_t settings_handle[SETTINGS_CHRS_NUM];
|
||||
static uint16_t range_handle[RANGE_CHRS_NUM];
|
||||
|
||||
static uint8_t cs_notify_state[INPUTS_NUM];
|
||||
static uint8_t ev_notify_state[INPUTS_NUM];
|
||||
@ -45,6 +46,7 @@ static uint8_t ev_notify_state[INPUTS_NUM];
|
||||
static struct ble_context ev_ctxt[INPUTS_NUM];
|
||||
static struct ble_context cs_ctxt[INPUTS_NUM];
|
||||
static struct ble_context settings_ctxt[SETTINGS_CHRS_NUM];
|
||||
static struct ble_context range_ctxt[RANGE_CHRS_NUM];
|
||||
|
||||
static struct ble_gatt_chr_def current_measure_char_template = {
|
||||
.uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_CHAR),
|
||||
@ -96,6 +98,48 @@ static struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
||||
.characteristics = rvs_char_array,
|
||||
},
|
||||
|
||||
[RANGE_SVC_ID] = {
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(METROLOGY_RANGE_SERVICE),
|
||||
.characteristics = (struct ble_gatt_chr_def[]){
|
||||
[AUTO_RANGE_ID] = {
|
||||
.uuid = BLE_UUID16_DECLARE(AUTO_RANGE_CHAR),
|
||||
.access_cb = gatt_char_access_range,
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
|
||||
.min_key_size = 0,
|
||||
.val_handle = &range_handle[AUTO_RANGE_ID],
|
||||
.arg = &range_ctxt[AUTO_RANGE_ID],
|
||||
.descriptors = (struct ble_gatt_dsc_def[]){
|
||||
[0] = {
|
||||
.uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT),
|
||||
.att_flags = BLE_ATT_F_READ,
|
||||
.access_cb = gatt_char_access_range,
|
||||
.arg = &range_ctxt[AUTO_RANGE_ID],
|
||||
},
|
||||
{ 0 },
|
||||
}
|
||||
},
|
||||
[CURRENT_RANGE_ID] = {
|
||||
.uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_RANGE_CHAR),
|
||||
.access_cb = gatt_char_access_range,
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
|
||||
.min_key_size = 0,
|
||||
.val_handle = &range_handle[CURRENT_RANGE_ID],
|
||||
.arg = &range_ctxt[CURRENT_RANGE_ID],
|
||||
.descriptors = (struct ble_gatt_dsc_def[]){
|
||||
[0] = {
|
||||
.uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT),
|
||||
.att_flags = BLE_ATT_F_READ,
|
||||
.access_cb = gatt_char_access_range,
|
||||
.arg = &range_ctxt[CURRENT_RANGE_ID],
|
||||
},
|
||||
{ 0 },
|
||||
}
|
||||
},
|
||||
{ 0 },
|
||||
}
|
||||
},
|
||||
|
||||
[SETTINGS_SVC_ID] = {
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(CONFIGURATION_SERVICE),
|
||||
@ -230,6 +274,12 @@ static void generate_svc_defs(configuration* conf, measurements* meas, measureme
|
||||
settings_ctxt[i].meas = meas;
|
||||
settings_ctxt[i].meas_ctxt = meas_ctxt;
|
||||
}
|
||||
for(int i = 0; i < RANGE_CHRS_NUM; i++){
|
||||
range_ctxt[i].handle = i;
|
||||
range_ctxt[i].conf = conf;
|
||||
range_ctxt[i].meas = meas;
|
||||
range_ctxt[i].meas_ctxt = meas_ctxt;
|
||||
}
|
||||
}
|
||||
|
||||
void initBLE(configuration* conf, measurements* meas, measurements_ctxt* meas_ctxt){
|
||||
|
@ -1,11 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "resistor_ranges.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t refresh_delay;
|
||||
uint32_t* offsets;
|
||||
uint16_t zero_cali_nsamp;
|
||||
resistor_range* ranges;
|
||||
int ranges_num;
|
||||
int range;
|
||||
int auto_range;
|
||||
} configuration;
|
||||
|
@ -47,8 +47,25 @@ static struct char_pres_format zero_cali_nsamp_char_pres_format = {
|
||||
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
||||
};
|
||||
|
||||
static int
|
||||
gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
||||
static struct char_pres_format auto_range_char_pres_format = {
|
||||
.format = FORMAT_BOOL,
|
||||
.exponent = 0,
|
||||
.unit = UNITLESS_UNIT_UUID,
|
||||
.namespc = 1,
|
||||
.descrH = NSP_DESC_MAIN & 0xff,
|
||||
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
||||
};
|
||||
|
||||
static struct char_pres_format current_range_char_pres_format = {
|
||||
.format = FORMAT_INT32,
|
||||
.exponent = 0,
|
||||
.unit = UNITLESS_UNIT_UUID,
|
||||
.namespc = 1,
|
||||
.descrH = NSP_DESC_MAIN & 0xff,
|
||||
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
||||
};
|
||||
|
||||
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
||||
void *dst, uint16_t *len)
|
||||
{
|
||||
uint16_t om_len;
|
||||
@ -135,7 +152,6 @@ int gatt_char_access_zeros_cali(uint16_t conn_handle, uint16_t attr_handle, stru
|
||||
int rc = 0;
|
||||
|
||||
struct ble_context* c_ctxt = arg;
|
||||
|
||||
switch(ctxt->op){
|
||||
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
||||
{
|
||||
@ -164,3 +180,61 @@ int gatt_char_access_zeros_cali(uint16_t conn_handle, uint16_t attr_handle, stru
|
||||
|
||||
return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0;
|
||||
}
|
||||
|
||||
int gatt_char_access_range(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
|
||||
int rc = 0;
|
||||
|
||||
struct ble_context* c_ctxt = arg;
|
||||
switch(ctxt->op){
|
||||
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
||||
{
|
||||
switch (c_ctxt->handle){
|
||||
case AUTO_RANGE_ID:
|
||||
;
|
||||
uint8_t state;
|
||||
rc = gatt_svr_chr_write(ctxt->om, sizeof(state), sizeof(state), &state, NULL);
|
||||
c_ctxt->conf->auto_range = state;
|
||||
break;
|
||||
case CURRENT_RANGE_ID:
|
||||
;
|
||||
int data;
|
||||
rc = gatt_svr_chr_write(ctxt->om, sizeof(data), sizeof(data), &data, NULL);
|
||||
ESP_LOGI(TAG, "new range %d", data);
|
||||
c_ctxt->conf->range = data;
|
||||
activate_range(c_ctxt->conf->ranges, data, c_ctxt->conf->ranges_num - 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BLE_GATT_ACCESS_OP_READ_CHR:
|
||||
{
|
||||
switch (c_ctxt->handle){
|
||||
case AUTO_RANGE_ID:
|
||||
;
|
||||
rc = os_mbuf_append(ctxt->om, &c_ctxt->conf->auto_range, sizeof(c_ctxt->conf->auto_range));
|
||||
break;
|
||||
case CURRENT_RANGE_ID:
|
||||
rc = os_mbuf_append(ctxt->om, &c_ctxt->conf->range, sizeof(c_ctxt->conf->range));
|
||||
;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BLE_GATT_ACCESS_OP_READ_DSC:
|
||||
{
|
||||
switch (c_ctxt->handle){
|
||||
case AUTO_RANGE_ID:
|
||||
;
|
||||
rc = os_mbuf_append(ctxt->om, &auto_range_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE);
|
||||
break;
|
||||
case CURRENT_RANGE_ID:
|
||||
;
|
||||
rc = os_mbuf_append(ctxt->om, ¤t_range_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0;
|
||||
}
|
||||
|
@ -2,8 +2,9 @@
|
||||
|
||||
#include "host/ble_gatt.h"
|
||||
|
||||
enum ble_services {CS_SVC_ID, RVS_SVC_ID, SETTINGS_SVC_ID, SVCS_NUM};
|
||||
enum ble_services {CS_SVC_ID, RVS_SVC_ID, RANGE_SVC_ID, SETTINGS_SVC_ID, SVCS_NUM};
|
||||
enum settings_chars {RFRSH_RATE_ID, ZEROS_CALI_ID, ZEROS_CALI_NSAMP_ID, SETTINGS_CHRS_NUM};
|
||||
enum range_characteristics {AUTO_RANGE_ID, CURRENT_RANGE_ID, RANGE_CHRS_NUM};
|
||||
|
||||
#define CHAR_PRESENTATION_FORMAT_SIZE 7
|
||||
|
||||
@ -19,4 +20,5 @@ struct char_pres_format {
|
||||
int gatt_char_access_cs(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
|
||||
int gatt_char_access_ev(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
|
||||
int gatt_char_access_sampling_rate(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
|
||||
int gatt_char_access_zeros_cali(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
|
||||
int gatt_char_access_zeros_cali(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
|
||||
int gatt_char_access_range(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
|
||||
|
@ -93,10 +93,14 @@ configuration main_conf = {
|
||||
.range = R100,
|
||||
.zero_cali_nsamp = 10000,
|
||||
.auto_range = 1,
|
||||
.ranges = ranges,
|
||||
.ranges_num = R_NUM,
|
||||
};
|
||||
|
||||
static void IRAM_ATTR overrange_handler(void* arg)
|
||||
{
|
||||
if(!main_conf.auto_range) return;
|
||||
|
||||
activate_range(ranges, 0, R_NUM - 1);
|
||||
main_conf.range = 0;
|
||||
}
|
||||
@ -181,9 +185,11 @@ void app_main(void){
|
||||
|
||||
if(meas_volts[UNDERRANGE_SRC] < UNDERRANGE_MV){
|
||||
underrange_counter++;
|
||||
if(underrange_counter > UNDERRANGE_NUM){
|
||||
main_conf.range--;
|
||||
if(underrange_counter > UNDERRANGE_NUM && main_conf.auto_range){
|
||||
if(main_conf.range < R_NUM - 1)
|
||||
main_conf.range++;
|
||||
activate_range(ranges, main_conf.range, R_NUM - 1);
|
||||
underrange_counter = 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
|
Loading…
x
Reference in New Issue
Block a user