Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
buglloc committed Jun 25, 2024
1 parent baf4d38 commit b69b711
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 77 deletions.
26 changes: 17 additions & 9 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ source "Kconfig.zephyr"
config PW_SIGN_KEY
string "Signing key"

config PW_ROTATE_PERIOD_SEC
int "How ofen message will be rotated"
config PW_UPDATE_PERIOD_SEC
int "How ofen update adv message data"
default 15

config ADV_BTN_TIMES
Expand All @@ -15,14 +15,22 @@ config ADV_USE_PUB_ADDRESS
bool "Advertise using identity address."
default y

config ADV_INTERVAL_MIN_MS
int "Minimum periodic advertising interval"
default 100
config ADV_INTERVAL_MIN
int "Minimum periodic advertising interval in 0.625 ms units"
default 160

config ADV_INTERVAL_MAX_MS
int "Maximum periodic advertising interval"
default 150
config ADV_INTERVAL_MAX
int "Maximum periodic advertising interval in 0.625 ms units"
default 240

config PW_BATT_VOLTAGE
config PW_BATT
bool "Have battery support"
default y

config PW_BATT_MIN_VOLTAGE
int "Max battery mVoltage"
default 1700

config PW_BATT_MAX_VOLTAGE
int "Max battery mVoltage"
default 3000
2 changes: 1 addition & 1 deletion boards/holyiot_21014.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
zephyr,resolution = <14>;
zephyr,oversampling = <8>;
};
};
};
32 changes: 24 additions & 8 deletions prj.conf
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
CONFIG_BT_DEVICE_NAME="PinkyWinky"
CONFIG_PW_SIGN_KEY="lol-kek-cheburek"

CONFIG_MAIN_STACK_SIZE=2048
CONFIG_MAIN_STACK_SIZE=4096

CONFIG_LOG=n
CONFIG_SERIAL=n
CONFIG_CONSOLE=n
CONFIG_UART_CONSOLE=n
CONFIG_STDOUT_CONSOLE=n
CONFIG_PRINTK=n
CONFIG_EARLY_CONSOLE=n

CONFIG_ADC=y

CONFIG_BT=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_PER_ADV=n
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=64

CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_SHA1=y
Expand All @@ -28,4 +26,22 @@ CONFIG_I2C=n
CONFIG_SPI=n

CONFIG_BOOT_BANNER=n
CONFIG_BOOT_DELAY=0

CONFIG_BT=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_PER_ADV=n
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=128

# Disable Bluetooth features not needed
CONFIG_BT_DEBUG_NONE=y
CONFIG_BT_SMP=n
CONFIG_BT_DIS=n
CONFIG_BT_BAS=n
CONFIG_BT_HRS=n
CONFIG_BT_PHY_UPDATE=n
CONFIG_BT_GATT_CACHING=n
CONFIG_BT_GATT_SERVICE_CHANGED=n

# Disable Bluetooth controller features not needed
CONFIG_BT_CTLR_PHY_2M=n
43 changes: 22 additions & 21 deletions pw-linux.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@
"C_Cpp.default.compilerPath": "/usr/bin/arm-none-eabi-gcc",
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
"files.associations": {
"printk.h": "c",
"gatt.h": "c",
"*.c_": "c",
"gpio.h": "c",
"typeinfo": "c",
"chrono": "c",
"text_encoding": "c",
"optional": "c",
"ratio": "c",
"system_error": "c",
"array": "c",
"functional": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"variant": "c",
"charconv": "c",
"format": "c",
"cstdint": "c",
"stdbool.h": "c"
},
"printk.h": "c",
"gatt.h": "c",
"*.c_": "c",
"gpio.h": "c",
"typeinfo": "c",
"chrono": "c",
"text_encoding": "c",
"optional": "c",
"ratio": "c",
"system_error": "c",
"array": "c",
"functional": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"variant": "c",
"charconv": "c",
"format": "c",
"cstdint": "c",
"stdbool.h": "c",
"compare": "c"
},
"zephyr-ide.projects": {},
"python.defaultInterpreterPath": "~/zephyr/.venv/bin/python",
"python.terminal.activateEnvironment": true,
Expand Down
68 changes: 44 additions & 24 deletions src/batt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@

LOG_MODULE_REGISTER(pw_batt);

#if DT_NODE_EXISTS(DT_PATH(zephyr_user)) && \
#if CONFIG_PW_BATT && DT_NODE_EXISTS(DT_PATH(zephyr_user)) && \
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channels) && \
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channel_names)

static const struct adc_dt_spec adc_channel = ADC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), batt);
static uint16_t pw_adc_buf;
static struct adc_sequence pw_adc_sequence = {
.buffer = &pw_adc_buf,
/* buffer size in bytes, not number of samples */
.buffer_size = sizeof(pw_adc_buf),
};
static uint8_t pw_batt_cur_percent = 0;

int pw_batt_enable()
{
Expand All @@ -30,48 +25,68 @@ int pw_batt_enable()
err = adc_channel_setup_dt(&adc_channel);
if (err < 0) {
LOG_ERR("could not setup ADC channel (err %d)", err);
return -1;
}

err = adc_sequence_init_dt(&adc_channel, &pw_adc_sequence);
if (err) {
LOG_ERR("could not initialize dt (err %d)", err);
return -1;
return -2;
}

LOG_INF("Battery initialized at %s", adc_channel.dev->name);
return 0;
return pw_batt_refresh_data();
}

uint8_t pw_batt_percent()
{
return pw_batt_cur_percent;
}

int pw_batt_refresh_data()
{
int err;
uint16_t sample_buff = 0;
struct adc_sequence sequence = {
.buffer = &sample_buff,
.buffer_size = sizeof(sample_buff),
};

err = adc_sequence_init_dt(&adc_channel, &sequence);
if (err) {
LOG_ERR("could not initialize dt (err %d)", err);
return -1;
}

err = adc_read_dt(&adc_channel, &pw_adc_sequence);
err = adc_read_dt(&adc_channel, &sequence);
if (err) {
LOG_ERR("could not read dt (err %d)", err);
return 1;
return -2;
}

int32_t val_mv = (int32_t)pw_adc_buf;
int32_t val_mv = (int32_t)sample_buff;
err = adc_raw_to_millivolts_dt(&adc_channel, &val_mv);
if (err) {
LOG_ERR("could not convert dt voltage (err %d)", err);
return 2;
return -3;
}

if (val_mv <= CONFIG_PW_BATT_MIN_VOLTAGE) {
pw_batt_cur_percent = 0;
return 0;
}

uint8_t percent = val_mv * 100 / CONFIG_PW_BATT_VOLTAGE;
if (percent > 100) {
return 100;
if (val_mv >= CONFIG_PW_BATT_MAX_VOLTAGE) {
pw_batt_cur_percent = 100;
return 0;
}

return percent;
val_mv -= CONFIG_PW_BATT_MIN_VOLTAGE;
val_mv *= 100;
val_mv /= CONFIG_PW_BATT_MAX_VOLTAGE - CONFIG_PW_BATT_MIN_VOLTAGE;
pw_batt_cur_percent = (uint8_t) val_mv;
return 0;
}

#else

int pw_batt_enable()
{
LOG_WRN("Battery is disabled or not available");
return 0;
}

Expand All @@ -80,4 +95,9 @@ uint8_t pw_batt_percent()
return 0;
}

#endif
int pw_batt_refresh_data()
{
return 0;
}

#endif
1 change: 1 addition & 0 deletions src/batt.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


int pw_batt_enable();
int pw_batt_refresh_data();
uint8_t pw_batt_percent();

#endif
13 changes: 1 addition & 12 deletions src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ int pw_ble_init_adv_data()
return 0;
}

void pw_ble_adv_timer_cb(struct k_timer *dummy)
{
LOG_INF("rotate data");
pw_ble_refresh_data();
}

K_TIMER_DEFINE(pw_ble_adv_timer, pw_ble_adv_timer_cb, NULL);

int pw_ble_enable()
{
int err;
Expand All @@ -149,7 +141,7 @@ int pw_ble_enable()
err = bt_le_ext_adv_create(
BT_LE_ADV_PARAM(
BT_LE_ADV_OPT_EXT_ADV | PW_BLE_EXT_ADV_OPTS,
CONFIG_ADV_INTERVAL_MIN_MS, CONFIG_ADV_INTERVAL_MAX_MS,
CONFIG_ADV_INTERVAL_MIN, CONFIG_ADV_INTERVAL_MAX,
NULL
),
NULL,
Expand All @@ -172,9 +164,6 @@ int pw_ble_enable()
LOG_ERR("failed to start extended advertising (err %d)", err);
return -1;
}

LOG_INF("start adv msg rotation...");
k_timer_start(&pw_ble_adv_timer, K_SECONDS(CONFIG_PW_ROTATE_PERIOD_SEC), K_SECONDS(CONFIG_PW_ROTATE_PERIOD_SEC));

LOG_INF("BLE initialized");
return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/ble.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef H_PW_BLE_
#define H_PW_BLE_

#define BLE_ADV_INT_UNIT 0.625

int pw_ble_enable();
int pw_ble_refresh_data();

Expand Down
5 changes: 3 additions & 2 deletions src/btn.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

LOG_MODULE_REGISTER(pw_btn);

#define BUTTON_DEBOUNCE_DELAY_MS 100
#define BUTTON_DEBOUNCE_DELAY_MS (100)
#define BUTTON_PRESS_TIMEOUT (CONFIG_ADV_BTN_TIMES * CONFIG_ADV_INTERVAL_MAX * BLE_ADV_INT_UNIT)

#define SW_NODE DT_ALIAS(button_sw)
#if !DT_NODE_HAS_STATUS(SW_NODE, okay)
Expand Down Expand Up @@ -79,5 +80,5 @@ void pw_btn_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins
pw_btn_pressed = true;
pw_led_on();
pw_ble_refresh_data();
k_timer_start(&pw_btn_release_timer, K_MSEC(CONFIG_ADV_BTN_TIMES * CONFIG_ADV_INTERVAL_MAX_MS), K_NO_WAIT);
k_timer_start(&pw_btn_release_timer, K_MSEC(BUTTON_PRESS_TIMEOUT), K_NO_WAIT);
}
15 changes: 15 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

#include "led.h"
Expand Down Expand Up @@ -42,5 +43,19 @@ int main(void)
}

LOG_INF("PinkyWinky was started");
for (;;) {
k_sleep(K_SECONDS(CONFIG_PW_UPDATE_PERIOD_SEC));

err = pw_batt_refresh_data();
if (err) {
LOG_ERR("batt refresh failed (err %d)", err);
}

err = pw_ble_refresh_data();
if (err) {
LOG_ERR("ble refresh failed (err %d)", err);
}
}

return 0;
}

0 comments on commit b69b711

Please sign in to comment.