services mag

This commit is contained in:
leo 2022-12-22 16:23:43 +01:00
parent a3b8ed0a54
commit 7616115618
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
6 changed files with 129 additions and 83 deletions

View File

@ -37,10 +37,8 @@ static int gatt_svr_chr_access_hum(uint16_t conn_handle, uint16_t attr_handle, s
static int gatt_svr_chr_access_batt_level(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); static int gatt_svr_chr_access_batt_level(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_power_voltage(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); static int gatt_svr_chr_access_power_voltage(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_power_pin(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_power_pout(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_power_format(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_magfield_update(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_magfield_x(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); static int gatt_svr_chr_access_magfield_x(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_magfield_y(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); static int gatt_svr_chr_access_magfield_y(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
static int gatt_svr_chr_access_magfield_z(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); static int gatt_svr_chr_access_magfield_z(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg);
@ -97,14 +95,6 @@ char_pres_format_t voltage_char_pres_format = {
.descr = NSP_DESC_MAIN, .descr = NSP_DESC_MAIN,
}; };
char_pres_format_t power_char_pres_format = {
.format = FORMAT_INT16,
.exponent = -3,
.unit = POWER_UNIT_UUID,
.namespc = 1,
.descr = NSP_DESC_MAIN,
};
char_pres_format_t magfield_char_pres_format = { char_pres_format_t magfield_char_pres_format = {
.format = FORMAT_INT32, .format = FORMAT_INT32,
.exponent = -12, .exponent = -12,
@ -243,42 +233,20 @@ static struct ble_gatt_svc_def gatt_svr_svcs[] = {
{ 0 }, { 0 },
}, },
}, },
[POWER_CHAR_PIN] = {
.uuid = BLE_UUID16_DECLARE(ALIM_POWER_IN_UUID),
.access_cb = gatt_svr_chr_access_power_pin,
.val_handle = &pow_handle[POWER_CHAR_PIN],
.flags = BLE_GATT_CHR_F_READ,
.descriptors = (struct ble_gatt_dsc_def[]){
{
.uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT),
.att_flags = BLE_ATT_F_READ,
.access_cb = gatt_svr_chr_access_power_format,
},
{ 0 }, { 0 },
}, },
}, },
[POWER_CHAR_POUT] = { [MAGFIELD_SVC_IDX] = {
.uuid = BLE_UUID16_DECLARE(ALIM_POWER_OUT_UUID),
.access_cb = gatt_svr_chr_access_power_pout,
.val_handle = &pow_handle[POWER_CHAR_POUT],
.flags = BLE_GATT_CHR_F_READ,
.descriptors = (struct ble_gatt_dsc_def[]){
{
.uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT),
.att_flags = BLE_ATT_F_READ,
.access_cb = gatt_svr_chr_access_power_format,
},
{ 0 },
},
},
{ 0 },
},
},
[MAGFIELD_SVC_UUID] = {
// magnetic field measure (debug?) // magnetic field measure (debug?)
.type = BLE_GATT_SVC_TYPE_PRIMARY, .type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID32_DECLARE(MAGFIELD_SVC_UUID), .uuid = BLE_UUID32_DECLARE(MAGFIELD_SVC_UUID),
.characteristics = (struct ble_gatt_chr_def[]){ .characteristics = (struct ble_gatt_chr_def[]){
[MAGFIELD_UPDATE] = {
.uuid = BLE_UUID16_DECLARE(MAGFIELD_UPDATE_UUID),
.access_cb = gatt_svr_chr_access_magfield_update,
.val_handle = &magfield_handle[MAGFIELD_UPDATE],
.flags = BLE_GATT_CHR_F_WRITE,
},
[MAGFIELD_CHAR_X] = { [MAGFIELD_CHAR_X] = {
.uuid = BLE_UUID16_DECLARE(MAGFIELD_X_UUID), .uuid = BLE_UUID16_DECLARE(MAGFIELD_X_UUID),
.access_cb = gatt_svr_chr_access_magfield_x, .access_cb = gatt_svr_chr_access_magfield_x,
@ -322,7 +290,7 @@ static struct ble_gatt_svc_def gatt_svr_svcs[] = {
}, },
}, },
{ 0 }, { 0 },
}, }
}, },
{ 0 }, { 0 },
}; };
@ -446,75 +414,96 @@ static int gatt_svr_chr_access_power_voltage(uint16_t conn_handle, uint16_t attr
} }
static int gatt_svr_chr_access_power_pin(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
static int gatt_svr_chr_access_magfield_x(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
int rc = 0; int rc = 0;
ESP_LOGI(NIMBLE_LOG_TAG, "batt power access event"); ESP_LOGI(NIMBLE_LOG_TAG, "magfield x access event");
switch(ctxt->op){ switch(ctxt->op){
case BLE_GATT_ACCESS_OP_READ_CHR: case BLE_GATT_ACCESS_OP_READ_CHR:
{ {
update_power_inout_readings(main_app_conf->power_conf->data); int32_t bx = main_app_conf->power_conf->data->bx;
uint16_t powerin = main_app_conf->power_conf->data->pin; rc = os_mbuf_append(ctxt->om, &bx, sizeof(bx));
rc = os_mbuf_append(ctxt->om, &powerin, sizeof(powerin));
}
break;
case BLE_GATT_ACCESS_OP_READ_DSC:
{
uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid);
switch(uuid){
case CHAR_PRES_FORMAT:
rc = os_mbuf_append(ctxt->om, &voltage_char_pres_format, 7);
break;
}
} }
break; break;
} }
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
static int gatt_svr_chr_access_power_pout(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ static int gatt_svr_chr_access_magfield_y(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
int rc = 0; int rc = 0;
ESP_LOGI(NIMBLE_LOG_TAG, "batt power access event"); ESP_LOGI(NIMBLE_LOG_TAG, "magfield y access event");
switch(ctxt->op){ switch(ctxt->op){
case BLE_GATT_ACCESS_OP_READ_CHR: case BLE_GATT_ACCESS_OP_READ_CHR:
{ {
update_power_inout_readings(main_app_conf->power_conf->data); int32_t by = main_app_conf->power_conf->data->by;
uint16_t powerout = main_app_conf->power_conf->data->pout; rc = os_mbuf_append(ctxt->om, &by, sizeof(by));
rc = os_mbuf_append(ctxt->om, &powerout, sizeof(powerout));
}
break;
case BLE_GATT_ACCESS_OP_READ_DSC:
{
uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid);
switch(uuid){
case CHAR_PRES_FORMAT:
rc = os_mbuf_append(ctxt->om, &voltage_char_pres_format, 7);
break;
}
} }
break; break;
} }
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
static int gatt_svr_chr_access_power_format(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ static int gatt_svr_chr_access_magfield_z(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
int rc = 0;
ESP_LOGI(NIMBLE_LOG_TAG, "magfield z access event");
switch(ctxt->op){
case BLE_GATT_ACCESS_OP_READ_CHR:
{
int32_t bz = main_app_conf->power_conf->data->bz;
rc = os_mbuf_append(ctxt->om, &bz, sizeof(bz));
}
break;
}
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
static int gatt_svr_chr_access_magfield_format(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
int rc = 0; int rc = 0;
if(ctxt->op != BLE_GATT_ACCESS_OP_READ_DSC){ if(ctxt->op != BLE_GATT_ACCESS_OP_READ_DSC){
ESP_LOGE(NIMBLE_LOG_TAG, "wrong callback called"); ESP_LOGE(NIMBLE_LOG_TAG, "wrong callback called");
} }
ESP_LOGI(NIMBLE_LOG_TAG, "batt voltage pres access event"); ESP_LOGI(NIMBLE_LOG_TAG, "magfield pres access event");
uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid); uint16_t uuid = ble_uuid_u16(ctxt->dsc->uuid);
switch(uuid){ switch(uuid){
case CHAR_PRES_FORMAT: case CHAR_PRES_FORMAT:
rc = os_mbuf_append(ctxt->om, &power_char_pres_format, 7); rc = os_mbuf_append(ctxt->om, &magfield_char_pres_format, 7);
break; break;
} }
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
static int gatt_svr_chr_access_magfield_update(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
int rc = 0;
if(ctxt->op != BLE_GATT_ACCESS_OP_WRITE_CHR){
ESP_LOGE(NIMBLE_LOG_TAG, "magfield update wrong callback called");
return 0;
}
ESP_LOGI(NIMBLE_LOG_TAG, "magfield update access event");
uint8_t command;
rc = ble_hs_mbuf_to_flat(ctxt->om, &command, 1, NULL);
if (rc != 0) {
return BLE_ATT_ERR_UNLIKELY;
}
switch(command){
case 0:
update_magfield_readings(main_app_conf->power_conf->data);
break;
case 1:
update_magfield_readings_cal(main_app_conf->power_conf->data);
break;
}
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
static void ble_advertise(void){ static void ble_advertise(void){
main_app_conf->sensor->enabled = false; main_app_conf->sensor->enabled = false;

View File

@ -38,6 +38,7 @@
#define MAGFIELD_X_UUID 0x3B91 #define MAGFIELD_X_UUID 0x3B91
#define MAGFIELD_Y_UUID 0x23E8 #define MAGFIELD_Y_UUID 0x23E8
#define MAGFIELD_Z_UUID 0x7F84 #define MAGFIELD_Z_UUID 0x7F84
#define MAGFIELD_UPDATE_UUID 0x5D4B
#define DEFAULT_NAME "CO2_ble" #define DEFAULT_NAME "CO2_ble"
#define DEFAULT_NAME_LEN 7 #define DEFAULT_NAME_LEN 7
@ -167,12 +168,11 @@ struct batt_level_char_descr {
enum{ enum{
POWER_CHAR_VOLTAGE, POWER_CHAR_VOLTAGE,
POWER_CHAR_PIN,
POWER_CHAR_POUT,
POWER_CHAR_NB, POWER_CHAR_NB,
}; };
enum{ enum{
MAGFIELD_UPDATE,
MAGFIELD_CHAR_X, MAGFIELD_CHAR_X,
MAGFIELD_CHAR_Y, MAGFIELD_CHAR_Y,
MAGFIELD_CHAR_Z, MAGFIELD_CHAR_Z,

View File

@ -0,0 +1,3 @@
idf_component_register(SRCS "power_inout.c"
INCLUDE_DIRS "include"
REQUIRES nvs_flash MMC56x3)

View File

@ -0,0 +1,25 @@
#ifndef POWER_DATA_H
#define POWER_DATA_H
#include "nvs_flash.h"
struct power_data {
int32_t bx, by, bz;
int32_t bridge_offset[3];
};
typedef struct power_data power_data_t;
struct power_conf {
power_data_t* data;
};
typedef struct power_conf power_conf_t;
void get_current_inout(int32_t in, int32_t out);
void update_power_inout_readings(power_data_t* data);
power_conf_t* get_power_configuration(nvs_handle_t nvs);
void update_bridge_offset(power_data_t* data);
void update_magfield_readings(power_data_t* data);
void update_magfield_readings_cal(power_data_t* data);
#endif

View File

@ -0,0 +1,33 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "power_inout.h"
#include "MMC56x3.h"
power_conf_t* get_power_configuration(nvs_handle_t nvs){
power_conf_t* conf = malloc(sizeof(power_conf_t));
power_data_t* data = malloc(sizeof(power_data_t));
conf->data = data;
return conf;
}
void update_magfield_readings(power_data_t* data){
int32_t bx, by, bz;
MMC56x3_get_mag_field(&bx, &by, &bz);
MMC56x3_to_pT(bx, by, bz, &data->bx, &data->by, &data->bz);
}
void update_magfield_readings_cal(power_data_t* data){
int32_t bx, by, bz;
MMC56x3_get_mag_field(&bx, &by, &bz);
bx+=data->bridge_offset[0];
by+=data->bridge_offset[1];
bz+=data->bridge_offset[2];
MMC56x3_to_pT(bx, by, bz, &data->bx, &data->by, &data->bz);
}
void update_bridge_offset(power_data_t* data){
MMC56x3_get_bridge_offset(&data->bridge_offset[0], &data->bridge_offset[1], &data->bridge_offset[2]);
MMC56x3_set();
}

View File

@ -18,6 +18,7 @@
#include "driver/i2c.h" #include "driver/i2c.h"
#include "ledController.h" #include "ledController.h"
#include "power_inout.h"
#include "scd4x_i2c.h" #include "scd4x_i2c.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "sensirion_i2c_hal.h" #include "sensirion_i2c_hal.h"
@ -45,7 +46,7 @@ void app_main(void){
.min_freq_mhz = 40, .min_freq_mhz = 40,
.light_sleep_enable = true, // enable light sleep .light_sleep_enable = true, // enable light sleep
}; };
//ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
configuration_data_t* conf = malloc(sizeof(configuration_data_t)); configuration_data_t* conf = malloc(sizeof(configuration_data_t));
init_conf_from_nvs(conf); init_conf_from_nvs(conf);
@ -61,18 +62,13 @@ void app_main(void){
init_battery_level_adc(conf->battery_conf); init_battery_level_adc(conf->battery_conf);
MMC56x3_set();
MMC56x3_reset();
uint8_t id=0; uint8_t id=0;
MMC56x3_get_product_ID(&id); MMC56x3_get_product_ID(&id);
uint8_t temp; uint8_t temp;
MMC56x3_get_temperature(&temp); MMC56x3_get_temperature(&temp);
int32_t offx, offy, offz;
MMC56x3_get_bridge_offset(&offx, &offy, &offz);
MMC56x3_set();
ESP_LOGI("MAIN", "MMC5603 product id %d", id); ESP_LOGI("MAIN", "MMC5603 product id %d", id);
ESP_LOGI("MAIN", "MMC measured temp is %d", temp); ESP_LOGI("MAIN", "MMC measured temp is %d", temp);
ESP_LOGI("MAIN", "MMC bridge offset; x %ld, y %ld, z %ld", offx, offy, offz); update_bridge_offset(conf->power_conf->data);
init_scd4x(); init_scd4x();
scd4x_power_down(); scd4x_power_down();