您现在的位置是:首页 >技术交流 >利用好ai工具善用promote-->重构代码,以nrf52832读取tmp117,battery的数据并广播等为例网站首页技术交流

利用好ai工具善用promote-->重构代码,以nrf52832读取tmp117,battery的数据并广播等为例

m0_7788 2024-06-17 11:19:22
简介利用好ai工具善用promote-->重构代码,以nrf52832读取tmp117,battery的数据并广播等为例

tmp117.h

#ifndef TMP117_H
#define TMP117_H
#include <stdint.h>
// 初始化 TMP117 温度传感器
void tmp117_init(void);
// 读取 TMP117 温度值
float tmp117_read_temperature(void);
#endif // TMP117_H

tmp117.c

#include "tmp117.h"
#include "nrf_drv_twi.h"

#define TMP117_ADDRESS 0x48
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(0);

static void twi_init(void)
{
    ret_code_t err_code;
    const nrf_drv_twi_config_t twi_config = {
       .scl                = 27,
       .sda                = 26,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };
    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    nrf_drv_twi_enable(&m_twi);
}
void tmp117_init(void)
{
    twi_init();
    uint8_t config_reg[2] = {0x01, 0x00};
    nrf_drv_twi_tx(&m_twi, TMP117_ADDRESS, config_reg, sizeof(config_reg), false);
}

float tmp117_read_temperature(void)
{
    uint8_t data[2];
    nrf_drv_twi_tx(&m_twi, TMP117_ADDRESS, &data[0], 1, true);
    nrf_drv_twi_rx(&m_twi, TMP117_ADDRESS, &data[0], sizeof(data));

    int16_t raw_temperature = (data[0] << 8) | data[1];
    float temperature = raw_temperature * 0.0078125;

    return temperature;
}

advertising.h

#ifndef ADVERTISING_H
#define ADVERTISING_H
#include <stdint.h>
// 初始化广播模块
void advertising_init(void);
// 更新广播数据
void advertising_update(float temperature);
//void advertising_update(uint8_t battery_level, float temperature, int16_t x, int16_t y, int16_t z);
#endif // ADVERTISING_H

advertising.c

#include "advertising.h"
#include "ble_advdata.h"
#include "ble_gap.h"
#include "nrf_ble_gatt.h"
#include "nrf_sdh_ble.h"

#define APP_BLE_CONN_CFG_TAG            1
#define DEVICE_NAME                     "My Device"
#define MANUFACTURER_NAME               "My Company"
#define APP_ADV_INTERVAL                64
#define APP_ADV_DURATION                18000
#define BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE (0x02)

static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
static ble_gap_adv_data_t m_adv_data;
static ble_gap_adv_params_t m_adv_params;

static void advertising_init_params(void)
{
    memset(&m_adv_params, 0, sizeof(m_adv_params));
    m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
    m_adv_params.p_peer_addr     = NULL;
    m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval        = APP_ADV_INTERVAL;
    m_adv_params.duration        = APP_ADV_DURATION;
    m_adv_params.max_adv_evts    = 0;
}

static void advertising_init_data(float temperature)
{
    memset(&m_adv_data, 0, sizeof(m_adv_data));

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0xFFFF;
    manuf_data.data.p_data = (uint8_t*)&temperature;
    manuf_data.data.size   = sizeof(temperature);

    ble_advdata_t advdata;
    memset(&advdata, 0, sizeof(advdata));
    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = true;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    advdata.uuids_complete.uuid_cnt = 0;
    advdata.uuids_solicited.uuid_cnt = 0;
    advdata.p_manuf_specific_data   = &manuf_data;

    uint32_t err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    //ble_advdata_encode 函数将广播数据编码为字节数组
    APP_ERROR_CHECK(err_code);
}

void advertising_init(void)
{
    advertising_init_params();
    float temperature = 0.0;
    advertising_init_data(temperature);

    uint32_t err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    APP_ERROR_CHECK(err_code);
}
void advertising_update(float temperature)
{
    advertising_init_data(temperature);
    uint32_t err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    APP_ERROR_CHECK(err_code);
}

main.c
#include “nrf_delay.h”
#include “nrf_log.h”
#include “nrf_log_ctrl.h”
#include “nrf_log_default_backends.h”
#include “nrf_pwr_mgmt.h”
#include “ble_radio_notification.h”
#include “advertising.h”
#include “tmp117.h”

int main(void)

{
    ret_code_t err_code;

    // 初始化日志模块
    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    // 初始化电源管理模块
    nrf_pwr_mgmt_init();
    // 初始化 TMP117 温度传感器
    tmp117_init();
    // 初始化广播模块
    advertising_init();
    // 初始化无线电通知功能
    ble_radio_notification_init(APP_IRQ_PRIORITY_LOWEST, NRF_RADIO_NOTIFICATION_DISTANCE_5500US, radio_notification_interleave);

    while (true)
    {
        // 读取 TMP117 温度值
        float temperature = tmp117_read_temperature();
        // 更新广播数据
        advertising_update(temperature);
        NRF_LOG_INFO("Temperature: %f", temperature);
        // 延时 1 秒
        nrf_delay_ms(1000);
        // 处理电源管理事件
        nrf_pwr_mgmt_run();
    }
}

battery.h

#ifndef BATTERY_H
#define BATTERY_H
#include <stdint.h>
// 初始化电池电量传感器
void battery_init(void);
// 读取电池电量值
uint8_t battery_read(void);
#endif // BATTERY_H

battery.c

#include "battery.h"
#include "nrf_saadc.h"
#define BATTERY_CHANNEL 0

static void saadc_init(void)
{
    ret_code_t err_code;

    nrf_saadc_channel_config_t channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);

    err_code = nrf_saadc_init(NULL);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_saadc_channel_init(BATTERY_CHANNEL, &channel_config);
    APP_ERROR_CHECK(err_code);
}

void battery_init(void)
{
    saadc_init();
}

uint8_t battery_read(void)
{
    nrf_saadc_value_t adc_value;
    ret_code_t err_code = nrf_saadc_channel_convert(BATTERY_CHANNEL, &adc_value);
    APP_ERROR_CHECK(err_code);

    uint8_t battery_level = (uint8_t)(((float)adc_value / 4096.0) * 100.0);

    return battery_level;
}

bma400.h

#ifndef BMA400_H
#define BMA400_H

#include <stdint.h>

// 初始化 BMA400 加速度传感器
void bma400_init(void);
// 读取 BMA400 加速度值
void bma400_read_acceleration(int16_t* x, int16_t* y, int16_t* z);

#endif // BMA400_H

bma400.c

#include "bma400.h"
#include "nrf_drv_twi.h"

#define BMA400_ADDRESS 0x14

static const nrf_drv_twi_t m_twi = NRF_TWI_INSTANCE(0);

static void twi_init(void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = 27,
       .sda                = 26,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    n_drv_twi_enable(&m_twi);
}

void bma400_init(void)
{
    twi_init();

    uint8_t config_reg[2] = {0x7E, 0x03};
    nrf_drv_twi_tx(&m_twi, BMA400_ADDRESS, config_reg, sizeof(config_reg), false);

    config_reg[0] = 0x7D;
    config_reg[1] = 0x00;
    nrf_drv_twi_tx(&m_twi, BMA400_ADDRESS, config_reg, sizeof(config_reg), false);
}

void bma400_read_acceleration(int16_t* x, int16_t* y, int16_t* z)
{
    uint8_t data[6];
    nrf_drv_twi_tx(&m_twi, BMA400_ADDRESS, &data[0], 1, true);
    nrf_drv_twi_rx(&m_twi, BMA400_ADDRESS, &data[0], sizeof(data));

    *x = (data[1] << 8) | data[0];
    *y = (data[3] << 8) | data[2];
    *z = (data[5] << 8) | data[4];
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。