Extracts prstlib

Moves ble sample to code/nrf-connect/samples
This commit is contained in:
rbaron 2022-11-27 18:37:42 +01:00
parent df7700cdd7
commit 5ceccfafcc
34 changed files with 122 additions and 90 deletions

View file

@ -1,2 +0,0 @@
build
build_*

View file

@ -1,17 +0,0 @@
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(b_parasite)
include_directories(src)
target_sources(app PRIVATE
src/main.c
src/prst/shtc3.c
src/prst/adc.c
src/prst/led.c
src/prst/button.c
src/prst/ble/ble.c
src/prst/ble/encoding.c
)

View file

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
add_library(prstlib STATIC
src/adc.c
src/button.c
src/led.c
src/sensors.c
src/shtc3.c
)
target_include_directories(prstlib PRIVATE include)
target_link_libraries(prstlib PUBLIC zephyr_interface)

View file

@ -4,7 +4,7 @@
#include <drivers/gpio.h>
#include <logging/log.h>
#include "macros.h"
#include "prstlib/macros.h"
#define PRST_LED_FLASH_PERIOD_MS 400

View file

@ -1,8 +1,8 @@
#ifndef _PRST_DATA_H_
#define _PRST_DATA_H_
#include "prst/adc.h"
#include "prst/shtc3.h"
#include "prstlib/adc.h"
#include "prstlib/shtc3.h"
typedef struct {
prst_adc_soil_moisture_t soil;
@ -11,4 +11,6 @@ typedef struct {
prst_shtc3_read_t shtc3;
} prst_sensors_t;
int prst_sensors_read_all(prst_sensors_t *out);
#endif // _PRST_DATA_H_

View file

@ -1,4 +1,4 @@
#include "adc.h"
#include "prstlib/adc.h"
#include <drivers/adc.h>
#include <drivers/gpio.h>
@ -6,7 +6,7 @@
#include <logging/log.h>
#include <zephyr/zephyr.h>
#include "macros.h"
#include "prstlib/macros.h"
LOG_MODULE_REGISTER(adc, LOG_LEVEL_DBG);

View file

@ -1,10 +1,10 @@
#include "button.h"
#include "prstlib/button.h"
#include <drivers/gpio.h>
#include <logging/log.h>
#include "led.h"
#include "macros.h"
#include "prstlib/led.h"
#include "prstlib/macros.h"
LOG_MODULE_REGISTER(button, LOG_LEVEL_DBG);

View file

@ -1,8 +1,8 @@
#include "led.h"
#include "prstlib/led.h"
#include <drivers/gpio.h>
#include "macros.h"
#include "prstlib/macros.h"
LOG_MODULE_REGISTER(led, LOG_LEVEL_DBG);

View file

@ -0,0 +1,27 @@
#include "prstlib/sensors.h"
#include <logging/log.h>
#include "prstlib/adc.h"
#include "prstlib/led.h"
#include "prstlib/macros.h"
LOG_MODULE_REGISTER(sensors, LOG_LEVEL_DBG);
int prst_sensors_read_all(prst_sensors_t *sensors) {
RET_IF_ERR(prst_adc_batt_read(&sensors->batt));
RET_IF_ERR(prst_adc_soil_read(sensors->batt.voltage, &sensors->soil));
RET_IF_ERR(prst_adc_photo_read(sensors->batt.voltage, &sensors->photo));
RET_IF_ERR(prst_shtc3_read(&sensors->shtc3))
LOG_DBG("Batt: %d mV", sensors->batt.millivolts);
LOG_DBG("Soil: %.0f %% (%.3f mV)", 100 * sensors->soil.percentage,
sensors->soil.adc_read.voltage);
LOG_DBG("Photo: %u lx (%.3f mV)", sensors->photo.brightness,
sensors->soil.adc_read.voltage);
LOG_DBG("Temp: %f oC", sensors->shtc3.temp_c);
LOG_DBG("Humi: %.0f %%", 100.0 * sensors->shtc3.rel_humi);
LOG_DBG("--------------------------------------------------");
return 0;
}

View file

@ -1,10 +1,10 @@
#include "shtc3.h"
#include "prstlib/shtc3.h"
#include <drivers/i2c.h>
#include <logging/log.h>
#include <zephyr/zephyr.h>
#include "prst/macros.h"
#include "prstlib/macros.h"
LOG_MODULE_REGISTER(shtc3, LOG_LEVEL_INF);

View file

@ -0,0 +1,3 @@
build
build_*
*.code-workspace

View file

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(ble)
include_directories(src)
target_sources(app PRIVATE
src/main.c
src/ble.c
src/encoding.c
)
add_subdirectory(../../prstlib prstlib)
target_include_directories(app PRIVATE ../../prstlib/include)
target_link_libraries(app PUBLIC prstlib)

View file

@ -1,5 +1,5 @@
# Enabling log has a big impact in power consumption. Only enable it while debugging.
CONFIG_LOG=n
CONFIG_LOG=y
CONFIG_PWM=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_I2C=y
@ -24,3 +24,5 @@ CONFIG_CONSOLE=n
CONFIG_RTT_CONSOLE=n
CONFIG_PINCTRL=y
CONFIG_PRST_SLEEP_DURATION_SEC=600

View file

@ -1,11 +1,11 @@
#include "ble.h"
#include <logging/log.h>
#include <prstlib/macros.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/settings/settings.h>
#include "prst/ble/encoding.h"
#include "prst/macros.h"
#include "encoding.h"
LOG_MODULE_REGISTER(ble, LOG_LEVEL_DBG);

View file

@ -1,7 +1,7 @@
#ifndef _PRST_BLE_BLE_H_
#define _PRST_BLE_BLE_H_
#include "prst/data.h"
#include <prstlib/sensors.h>
int prst_ble_init();
int prst_ble_adv_start();

View file

@ -1,8 +1,7 @@
#include "prst/ble/encoding.h"
#include "encoding.h"
#include <logging/log.h>
#include "prst/macros.h"
#include <prstlib/macros.h>
LOG_MODULE_DECLARE(ble, LOG_LEVEL_DBG);

View file

@ -1,10 +1,9 @@
#ifndef _PRST_BLE_ENCODING_H_
#define _PRST_BLE_ENCODING_H_
#include <prstlib/sensors.h>
#include <zephyr/bluetooth/bluetooth.h>
#include "prst/data.h"
int prst_ble_encode_service_data(const prst_sensors_t* sensors,
const bt_addr_le_t* bt_addr, uint8_t* out,
uint8_t out_len);

View file

@ -0,0 +1,38 @@
#include <logging/log.h>
#include <prstlib/adc.h>
#include <prstlib/button.h>
#include <prstlib/led.h>
#include <prstlib/macros.h>
#include <prstlib/sensors.h>
#include <prstlib/shtc3.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/pm.h>
#include <zephyr/pm/policy.h>
#include <zephyr/zephyr.h>
#include "ble.h"
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
int main(void) {
RET_IF_ERR(prst_adc_init());
RET_IF_ERR(prst_led_init());
RET_IF_ERR(prst_button_init());
RET_IF_ERR(prst_ble_init());
RET_IF_ERR(prst_led_flash(2));
prst_sensors_t sensors;
while (true) {
RET_IF_ERR(prst_sensors_read_all(&sensors));
RET_IF_ERR(prst_ble_adv_set_data(&sensors));
RET_IF_ERR(prst_ble_adv_start());
k_sleep(K_SECONDS(CONFIG_PRST_BLE_ADV_DURATION_SEC));
RET_IF_ERR(prst_ble_adv_stop());
k_sleep(K_SECONDS(CONFIG_PRST_SLEEP_DURATION_SEC));
}
}

View file

@ -1,50 +0,0 @@
#include <logging/log.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/pm.h>
#include <zephyr/pm/policy.h>
#include <zephyr/zephyr.h>
#include "prst/adc.h"
#include "prst/ble/ble.h"
#include "prst/button.h"
#include "prst/data.h"
#include "prst/led.h"
#include "prst/macros.h"
#include "prst/shtc3.h"
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
int main(void) {
RET_IF_ERR(prst_adc_init());
RET_IF_ERR(prst_led_init());
RET_IF_ERR(prst_button_init());
RET_IF_ERR(prst_ble_init());
RET_IF_ERR(prst_led_flash(2));
prst_sensors_t sensors;
while (true) {
RET_IF_ERR(prst_adc_batt_read(&sensors.batt));
RET_IF_ERR(prst_adc_soil_read(sensors.batt.voltage, &sensors.soil));
RET_IF_ERR(prst_adc_photo_read(sensors.batt.voltage, &sensors.photo));
RET_IF_ERR(prst_shtc3_read(&sensors.shtc3))
LOG_INF("Batt: %d mV", sensors.batt.millivolts);
LOG_INF("Soil: %.0f %% (%.3f mV)", 100 * sensors.soil.percentage,
sensors.soil.adc_read.voltage);
LOG_INF("Photo: %u lx (%.3f mV)", sensors.photo.brightness,
sensors.soil.adc_read.voltage);
LOG_INF("Temp: %f oC", sensors.shtc3.temp_c);
LOG_INF("Humi: %.0f %%", 100.0 * sensors.shtc3.rel_humi);
LOG_INF("--------------------------------------------------");
RET_IF_ERR(prst_ble_adv_set_data(&sensors));
RET_IF_ERR(prst_ble_adv_start());
k_sleep(K_SECONDS(CONFIG_PRST_BLE_ADV_DURATION_SEC));
RET_IF_ERR(prst_ble_adv_stop());
k_sleep(K_SECONDS(CONFIG_PRST_SLEEP_DURATION_SEC));
}
}