Untitled
unknown
plain_text
2 years ago
3.0 kB
9
Indexable
#include "esp_log.h"
#include "host/ble_hs.h"
#include "driver/adc.h"
#include <esp_adc_cal.h>
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"
#include "VLNotificationsHandler.hpp"
#include "VLSvcsChrs.hpp"
struct TaskParameters
{
VLNotificationsHandler *handlerInstance;
bool booleanValue;
};
static esp_err_t err = ESP_OK;
VLNotificationsHandler::VLNotificationsHandler()
{
vl_voltage_value = 0;
previous_voltage_value = 0;
}
uint32_t VLNotificationsHandler::voltage_task_handler()
{
adc1_config_width(ADC_WIDTH_MAX);
adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_6);
// Calculate ADC characteristics i.e. gain and offset factors
esp_adc_cal_characteristics_t characteristics;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0, &characteristics);
// Read ADC and obtain result in mV
// while (1)
// {
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_MAX), &characteristics);
printf("%ld mV\n", voltage);
// vTaskDelay(pdMS_TO_TICKS(1000));
// }
return voltage;
}
void VLNotificationsHandler::voltage_change_event(void *args)
{
TaskParameters *params = static_cast<TaskParameters *>(args); // REIKIA INJECTINT VLAdvertising instanca ir taip checkint ar uzsubscribinta
bool booleanValue = params->booleanValue;
VLNotificationsHandler *instance = params->handlerInstance;
uint32_t current_voltage;
uint16_t chr_val_handle;
while (1)
{
current_voltage = VLNotificationsHandler::voltage_task_handler();
if (current_voltage != instance->previous_voltage_value && current_voltage > 0 && true)
{
instance->vl_voltage_value = current_voltage;
int rc = ble_gatts_find_chr(&vl_device_info_svc_uuid.u, &vl_device_voltage_chr_uuid.u, NULL, &chr_val_handle);
if (rc != 0)
{
printf("Error finding characteristic!\n");
break;
}
ble_gatts_chr_updated(chr_val_handle);
ESP_LOGI("NOTIFICATION", "VOLTAGE CHANGED _ NOTIFICATION SENT");
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
int VLNotificationsHandler::voltage_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt,
void *arg)
{
VLNotificationsHandler *instance = (VLNotificationsHandler *)arg;
const ble_uuid_t *uuid;
uuid = ctxt->chr->uuid;
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR)
{
ESP_LOGI("DEBUG", "VOLTAGE BEING SENT");
int rc = os_mbuf_append(ctxt->om, &instance->vl_voltage_value, sizeof(instance->vl_voltage_value));
if (rc != 0)
{
printf("Error appending data to mbuf!\n");
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
return 0;
}
return -1;
}
Editor is loading...