Enable GPS only when BT notify activated
This commit is contained in:
parent
19d38903cf
commit
8a898f301c
40
README.rst
40
README.rst
|
@ -1,40 +0,0 @@
|
|||
.. _uart_sample:
|
||||
|
||||
UART Driver Sample
|
||||
##################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
This sample demonstrates how to use the UART serial driver with a simple
|
||||
echo bot. It reads data from the console and echoes the characters back after
|
||||
an end of line (return key) is received.
|
||||
|
||||
The polling API is used for sending data and the interrupt-driven API
|
||||
for receiving, so that in theory the thread could do something else
|
||||
while waiting for incoming data.
|
||||
|
||||
By default, the UART peripheral that is normally used for the Zephyr shell
|
||||
is used, so that almost every board should be supported.
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
Build and flash the sample as follows, changing ``nrf52840dk_nrf52840`` for
|
||||
your board:
|
||||
|
||||
.. zephyr-app-commands::
|
||||
:zephyr-app: samples/drivers/uart/echo_bot
|
||||
:board: nrf52840dk_nrf52840
|
||||
:goals: build flash
|
||||
:compact:
|
||||
|
||||
Sample Output
|
||||
=============
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
Hello! I\'m your echo bot.
|
||||
Tell me something and press enter:
|
||||
# Type e.g. "Hi there!" and hit enter!
|
||||
Echo: Hi there!
|
|
@ -1,9 +0,0 @@
|
|||
sample:
|
||||
name: UART driver sample
|
||||
tests:
|
||||
sample.drivers.uart:
|
||||
tags: serial uart
|
||||
filter: CONFIG_SERIAL and
|
||||
CONFIG_UART_INTERRUPT_DRIVEN and
|
||||
dt_chosen_enabled("zephyr,shell-uart")
|
||||
harness: keyboard
|
|
@ -20,6 +20,13 @@ uint32_t getTs()
|
|||
return ts;
|
||||
}
|
||||
|
||||
uint32_t getUptime()
|
||||
{
|
||||
uint32_t uptime = (k_uptime_get()/1000);
|
||||
// LOG_INF("getTs: %d", ts);
|
||||
return uptime;
|
||||
}
|
||||
|
||||
bool TsOK()
|
||||
{
|
||||
return timeoffset != 0;
|
||||
|
|
|
@ -2,5 +2,6 @@
|
|||
|
||||
void setTs(uint32_t ts);
|
||||
uint32_t getTs();
|
||||
uint32_t getUptime();
|
||||
bool TsOK();
|
||||
|
||||
|
|
54
src/gps.c
54
src/gps.c
|
@ -7,6 +7,9 @@
|
|||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/uart.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
@ -20,7 +23,7 @@ LOG_MODULE_REGISTER(gps);
|
|||
|
||||
#include "lwgps.h"
|
||||
#include "gps.h"
|
||||
#include "calendar.h"
|
||||
//#include "calendar.h"
|
||||
|
||||
#define MY_STACK_SIZE 2048
|
||||
#define MY_PRIORITY 5
|
||||
|
@ -48,6 +51,9 @@ struct rtt_config_t {
|
|||
|
||||
#define MSG_SIZE 128
|
||||
|
||||
#define GPIO_PORT DT_NODELABEL(gpio0)
|
||||
const struct device *devgpio = DEVICE_DT_GET(GPIO_PORT);
|
||||
|
||||
/* queue to store up to 10 messages (aligned to 4-byte boundary) */
|
||||
K_MSGQ_DEFINE(uart_msgq, MSG_SIZE, 10, 4);
|
||||
|
||||
|
@ -68,8 +74,8 @@ const unsigned char GNSSStart[] = { 0x06,0x04,0x04,0x00,0x00,0x00,0x09,0x00 };
|
|||
const unsigned char GNSSStop[] = { 0x06,0x04,0x04,0x00,0x00,0x00,0x08,0x00 };
|
||||
const unsigned char enablePUBX0[] = { 0x06, 0x01, 0x08, 0x00, 0xf1, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||
const unsigned char disablePUBX0[] = { 0x06, 0x01, 0x08, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
const unsigned char enablePUBX4[] = { 0x06, 0x01, 0x08, 0x00, 0xf1, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||
const unsigned char disablePUBX4[] = { 0x06, 0x01, 0x08, 0x00, 0xf1, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
//const unsigned char enablePUBX4[] = { 0x06, 0x01, 0x08, 0x00, 0xf1, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||
//const unsigned char disablePUBX4[] = { 0x06, 0x01, 0x08, 0x00, 0xf1, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
const unsigned char rate2Hz2Mes[] = { 0x06, 0x08, 0x06, 0x00, 0xf4, 0x02, 0x02, 0x00, 0x00, 0x00 };
|
||||
const unsigned char rate1Hz1Mes[] = { 0x06, 0x08, 0x06, 0x00, 0xe8, 0x03, 0x01, 0x00, 0x00, 0x00 };
|
||||
const unsigned char rate1Hz5Mes[] = { 0x06, 0x08, 0x06, 0x00, 0xe8, 0x03, 0x05, 0x00, 0x00, 0x00 };
|
||||
|
@ -215,10 +221,13 @@ void sendUBX( const unsigned char *progmemBytes, size_t len)
|
|||
} // sendUBX
|
||||
|
||||
|
||||
|
||||
|
||||
void init_gps()
|
||||
{
|
||||
sendUBX(GNSSStart, sizeof(GNSSStart));
|
||||
k_msleep(100);
|
||||
// sendUBX(GNSSStart, sizeof(GNSSStart));
|
||||
gpio_pin_set(devgpio, 2, 1);
|
||||
k_msleep(1000);
|
||||
sendUBX(ubxNMEA, sizeof(ubxNMEA));
|
||||
k_msleep(100);
|
||||
sendUBX(ubxGNSS, sizeof(ubxGNSS));
|
||||
|
@ -241,10 +250,16 @@ void init_gps()
|
|||
print_uart_const(enableGSA);
|
||||
k_msleep(100);
|
||||
sendUBX(enablePUBX0, sizeof(enablePUBX0));
|
||||
k_msleep(100);
|
||||
sendUBX(enablePUBX4, sizeof(enablePUBX4));
|
||||
//k_msleep(100);
|
||||
//sendUBX(enablePUBX4, sizeof(enablePUBX4));
|
||||
}
|
||||
|
||||
static void work_init_gps_handler(struct k_work *work)
|
||||
{
|
||||
init_gps();
|
||||
}
|
||||
K_WORK_DEFINE(work_init_gps, work_init_gps_handler);
|
||||
|
||||
/*void segger_read_ep(void *, void *, void *)
|
||||
{
|
||||
|
||||
|
@ -278,15 +293,17 @@ K_THREAD_DEFINE(segger_read_tid, MY_STACK_SIZE,
|
|||
*/
|
||||
|
||||
void stop_gps() {
|
||||
sendUBX(GNSSStop, sizeof(GNSSStop));
|
||||
//sendUBX(GNSSStop, sizeof(GNSSStop));
|
||||
gpio_pin_set(devgpio, 2, 0);
|
||||
}
|
||||
|
||||
void gpsPower(bool state) {
|
||||
gps_state = state;
|
||||
if(state == true) {
|
||||
if(state == true && gps_state == false) {
|
||||
gps_state = state;
|
||||
LOG_WRN("Start GPS");
|
||||
init_gps();
|
||||
} else {
|
||||
k_work_submit(&work_init_gps);
|
||||
} else if(state == false && gps_state == true) {
|
||||
gps_state = state;
|
||||
LOG_WRN("Stop GPS");
|
||||
stop_gps();
|
||||
}
|
||||
|
@ -325,8 +342,11 @@ void gps_ep(void *, void *, void *)
|
|||
return;
|
||||
}
|
||||
|
||||
gpsPower(true);
|
||||
k_msleep(250);
|
||||
gpio_pin_configure(devgpio, 2, GPIO_OUTPUT_INACTIVE);
|
||||
|
||||
|
||||
//gpsPower(true);
|
||||
//k_msleep(250);
|
||||
gpsPower(false);
|
||||
|
||||
lwgps_init(&hgps);
|
||||
|
@ -365,7 +385,7 @@ void gps_ep(void *, void *, void *)
|
|||
//LOG_INF("NMEA msg");
|
||||
if(gps_state == true)
|
||||
{
|
||||
LOG_DBG("NMEA Sentence: %s", tx_buf);
|
||||
//LOG_DBG("NMEA Sentence: %s", tx_buf);
|
||||
lwgps_process(&hgps, tx_buf, strlen(tx_buf));
|
||||
//LOG_INF("NMEA Sentence %s", hgps.p.term_str);
|
||||
if(hgps.p.stat == STAT_GSA) {
|
||||
|
@ -416,7 +436,7 @@ void gps_ep(void *, void *, void *)
|
|||
k_fifo_put(&fifo_gps_data, data);
|
||||
k_sem_give(&gps_data_tosend);
|
||||
}
|
||||
} else if(hgps.p.stat == STAT_UBX_TIME) {
|
||||
/* } else if(hgps.p.stat == STAT_UBX_TIME) {
|
||||
if(hgps.year >= 24)
|
||||
{
|
||||
struct tm current_tm;
|
||||
|
@ -428,7 +448,7 @@ void gps_ep(void *, void *, void *)
|
|||
current_tm.tm_year = hgps.year + 2000 - 1900;
|
||||
setTs((uint32_t) timeutil_timegm64(¤t_tm));
|
||||
sendUBX(disablePUBX4, sizeof(disablePUBX4));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
} else if(strcmp(tx_buf, "$GNTXT,01,01,02,Stopping GNSS*52\r\n") != 0) {
|
||||
gpsPower(false);
|
||||
|
|
130
src/main.c
130
src/main.c
|
@ -19,6 +19,8 @@
|
|||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/pm/pm.h>
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(main);
|
||||
|
@ -29,6 +31,7 @@ LOG_MODULE_REGISTER(main);
|
|||
#include "bme280.h"
|
||||
#include "ruuvi.h"
|
||||
#include "battery.h"
|
||||
#include "calendar.h"
|
||||
|
||||
#define STACKSIZE 2048
|
||||
#define PRIORITY 7
|
||||
|
@ -40,7 +43,11 @@ static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
|
|||
#define LED2_NODE DT_ALIAS(led2)
|
||||
static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(LED2_NODE, gpios);
|
||||
|
||||
//const struct device *const cons = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
|
||||
|
||||
static K_SEM_DEFINE(ble_init_ok, 0, 1);
|
||||
static K_SEM_DEFINE(nus_notify_ready, 0, 1);
|
||||
static K_SEM_DEFINE(ccc_gps_sem, 1, 1);
|
||||
|
||||
bool btconnected = false;
|
||||
bool btfirstadv = true;
|
||||
|
@ -67,15 +74,17 @@ static ssize_t read_s16(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|||
static ssize_t on_write_rx(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags);
|
||||
static ssize_t on_read_rx(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset);
|
||||
static void nus_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value);
|
||||
static void nus_ccc_cfg_changed2(const struct bt_gatt_attr *attr, uint16_t value);
|
||||
static void ess_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value);
|
||||
static void gps_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value);
|
||||
|
||||
static uint8_t nus_rx[26];
|
||||
static uint8_t nus_tx_started;
|
||||
static int8_t gps_notify_count = 0;
|
||||
static K_FIFO_DEFINE(fifo_btsendhisto);
|
||||
struct btsendhisto_t {
|
||||
void *fifo_reserved;
|
||||
uint8_t type;
|
||||
uint32_t from;
|
||||
uint32_t current;
|
||||
};
|
||||
|
||||
enum captor_type { BATTERY, TEMPERATURE, PRESSURE };
|
||||
|
@ -172,12 +181,12 @@ void update_captors(bool force)
|
|||
|
||||
struct sensor_value sval;
|
||||
|
||||
if(btconnected == false)
|
||||
{
|
||||
//if(btconnected == false)
|
||||
//{
|
||||
temperature_toadv = findifadv(TEMPERATURE);
|
||||
pressure_toadv = findifadv(PRESSURE);
|
||||
battery_toadv = findifadv(BATTERY);
|
||||
}
|
||||
//}
|
||||
if(force == false)
|
||||
{
|
||||
temperature_torecord = findifrecord(TEMPERATURE);
|
||||
|
@ -247,6 +256,26 @@ void update_captors(bool force)
|
|||
battery_get_millivolt(&battery_millivolt);
|
||||
battery_get_percentage(&battery_percentage, battery_millivolt);
|
||||
|
||||
if(battery_millivolt < 3000) {
|
||||
gpsPower(false);
|
||||
k_msleep(1000);
|
||||
//int rc = pm_device_action_run(cons, PM_DEVICE_ACTION_SUSPEND);
|
||||
//if (rc < 0) {
|
||||
// printk("Could not suspend console (%d)\n", rc);
|
||||
//} else {
|
||||
printk("POWER Off due to battery voltage\n");
|
||||
pm_state_force(0u, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});
|
||||
|
||||
/* Now we need to go sleep. This will let the idle thread runs and
|
||||
* the pm subsystem will use the forced state. To confirm that the
|
||||
* forced state is used, lets set the same timeout used previously.
|
||||
*/
|
||||
k_sleep(K_SECONDS(2));
|
||||
|
||||
printk("ERROR: System off failed\n");
|
||||
//}
|
||||
}
|
||||
|
||||
ruuvi_updateBattery(battery_millivolt);
|
||||
ruuvi_advertise();
|
||||
|
||||
|
@ -290,12 +319,12 @@ BT_GATT_SERVICE_DEFINE(lns_svc,
|
|||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CCC(gps_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_LNS_PQ,
|
||||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CCC(gps_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
);
|
||||
|
||||
BT_GATT_SERVICE_DEFINE(cus_svc,
|
||||
|
@ -304,12 +333,11 @@ BT_GATT_SERVICE_DEFINE(cus_svc,
|
|||
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
|
||||
BT_GATT_PERM_WRITE,
|
||||
NULL, on_write_cus, &write_cus_buf),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_CUS_SAT,
|
||||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CCC(gps_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
);
|
||||
|
||||
|
||||
|
@ -320,12 +348,12 @@ BT_GATT_SERVICE_DEFINE(ess_svc,
|
|||
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_READ,
|
||||
read_s16, NULL, &temperature),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CCC(ess_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_PRESSURE,
|
||||
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_READ,
|
||||
read_u32, NULL, &pressure),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CCC(ess_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
);
|
||||
|
||||
#define BT_UUID_NUS BT_UUID_DECLARE_128(BT_UUID_NUS_VAL)
|
||||
|
@ -352,13 +380,13 @@ BT_GATT_SERVICE_DEFINE(nus_svc,
|
|||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed2, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
BT_GATT_CCC(ess_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
/* Temperature */
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_NUS_TEMPERATURE,
|
||||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(nus_ccc_cfg_changed2, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE)
|
||||
BT_GATT_CCC(ess_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE)
|
||||
);
|
||||
|
||||
|
||||
|
@ -373,11 +401,6 @@ static const struct bt_data sd[] = {
|
|||
BT_UUID_NUS_VAL),
|
||||
};
|
||||
|
||||
static void nus_ccc_cfg_changed2(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
LOG_WRN("%s: handle: %d value: %d", __func__, attr->handle, value);
|
||||
//nus_tx_started = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
||||
}
|
||||
|
||||
static ssize_t read_u32(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, uint16_t len, uint16_t offset)
|
||||
|
@ -434,7 +457,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
|
|||
printk("Connected\n");
|
||||
btconnected = true;
|
||||
k_timer_start(&timer_notify_bme280, K_NO_WAIT, K_SECONDS(10));
|
||||
gpsPower(true);
|
||||
//gpsPower(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -445,7 +468,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
|
|||
k_timer_stop(&timer_notify_bme280);
|
||||
reset_adv_state();
|
||||
update_captors(true);
|
||||
gpsPower(false);
|
||||
//gpsPower(false);
|
||||
}
|
||||
|
||||
|
||||
|
@ -454,6 +477,7 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
|
|||
.disconnected = disconnected,
|
||||
};
|
||||
|
||||
/*
|
||||
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
|
||||
{
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
|
@ -479,6 +503,7 @@ static struct bt_conn_auth_cb auth_cb_display = {
|
|||
// .pairing_complete = pairing_complete,
|
||||
// .pairing_failed = pairing_failed
|
||||
};
|
||||
*/
|
||||
|
||||
static ssize_t on_write_rx(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
|
||||
|
@ -496,12 +521,15 @@ static ssize_t on_write_rx(struct bt_conn *conn, const struct bt_gatt_attr *attr
|
|||
if(len == 11)
|
||||
{
|
||||
struct btsendhisto_t bthisto;
|
||||
uint32_t ts;
|
||||
ts = value[10] + (value[9] << 8) + (value[8] << 16) + (value[7] << 24);
|
||||
uint32_t fromts;
|
||||
uint32_t currentts;
|
||||
currentts = value[6] + (value[5] << 8) + (value[4] << 16) + (value[3] << 24);
|
||||
fromts = value[10] + (value[9] << 8) + (value[8] << 16) + (value[7] << 24);
|
||||
|
||||
LOG_INF("Send histo from %d header %d %d %d", ts, value[0], value[1], value[2]);
|
||||
LOG_INF("Send histo from %d to %d header %d %d %d", fromts, currentts, value[0], value[1], value[2]);
|
||||
//btsendhisto(ts, value[0]);
|
||||
bthisto.from = ts;
|
||||
bthisto.from = fromts;
|
||||
bthisto.current = currentts;
|
||||
bthisto.type = value[0];
|
||||
size_t size = sizeof(struct btsendhisto_t);
|
||||
char *mem_ptr = k_malloc(size);
|
||||
|
@ -524,8 +552,27 @@ static ssize_t on_read_rx(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|||
static void nus_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
LOG_WRN("%s: value: %d", __func__, value);
|
||||
k_sem_give(&nus_notify_ready);
|
||||
}
|
||||
|
||||
nus_tx_started = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
||||
static void ess_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
LOG_WRN("%s: handle: %d value: %d", __func__, attr->handle, value);
|
||||
//nus_tx_started = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
||||
}
|
||||
|
||||
static void gps_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
k_sem_take(&ccc_gps_sem, K_FOREVER);
|
||||
(value == BT_GATT_CCC_NOTIFY) ? gps_notify_count++ : gps_notify_count--;
|
||||
LOG_WRN("%s: handle: %d value: %d total: %d", __func__, attr->handle, value, gps_notify_count);
|
||||
if(gps_notify_count == 1) {
|
||||
gpsPower(true);
|
||||
} else if(gps_notify_count <= 0) {
|
||||
gps_notify_count = 0;
|
||||
gpsPower(false);
|
||||
}
|
||||
k_sem_give(&ccc_gps_sem);
|
||||
}
|
||||
|
||||
void btsendhisto_thread()
|
||||
|
@ -533,18 +580,25 @@ void btsendhisto_thread()
|
|||
for (;;) {
|
||||
struct btsendhisto_t *request = k_fifo_get(&fifo_btsendhisto,
|
||||
K_FOREVER);
|
||||
if (k_sem_take(&nus_notify_ready, K_MSEC(500)) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t bttosend[11];
|
||||
bttosend[0] = request->type;
|
||||
bttosend[2] = 0x10;
|
||||
for(int i=0;i<SIZE_DB;i++)
|
||||
{
|
||||
struct s_record r = database.db[i];
|
||||
if(r.timestamp > request->from && ((request->type == 0x30 && r.type == TEMPERATURE) || (request->type == 0x32 && r.type == PRESSURE) || request->type == 0x3a))
|
||||
uint32_t ts1=getUptime()-(request->current-request->from);
|
||||
uint32_t ts2=request->current-getUptime();
|
||||
if(r.timestamp >= ts1 && ((request->type == 0x30 && r.type == TEMPERATURE) || (request->type == 0x32 && r.type == PRESSURE) || request->type == 0x3a))
|
||||
{
|
||||
bttosend[6] = r.timestamp;
|
||||
bttosend[5] = r.timestamp >> 8;
|
||||
bttosend[4] = r.timestamp >> 16;
|
||||
bttosend[3] = r.timestamp >> 24;
|
||||
bttosend[6] = r.timestamp+ts2;
|
||||
bttosend[5] = (r.timestamp+ts2) >> 8;
|
||||
bttosend[4] = (r.timestamp+ts2) >> 16;
|
||||
bttosend[3] = (r.timestamp+ts2) >> 24;
|
||||
int32_t s32 = (int32_t) r.value;
|
||||
if(r.type == TEMPERATURE)
|
||||
{
|
||||
|
@ -558,7 +612,7 @@ void btsendhisto_thread()
|
|||
bttosend[9] = s32 >> 8;
|
||||
bttosend[8] = s32 >> 16;
|
||||
bttosend[7] = s32 >> 24;
|
||||
LOG_INF("SRec %d ts %u type %d value %d", i, r.timestamp, r.type, r.value);
|
||||
LOG_INF("SRec %d ts %u type %d value %d", i, r.timestamp+ts2, r.type, r.value);
|
||||
bt_gatt_notify_uuid(NULL, BT_UUID_NUS_TX, nus_svc.attrs, &bttosend, sizeof(bttosend));
|
||||
k_msleep(50);
|
||||
}
|
||||
|
@ -625,7 +679,7 @@ void ruuvi_advertise()
|
|||
};
|
||||
if(btfirstadv == true)
|
||||
{
|
||||
bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, NULL), ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
|
||||
bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, BT_GAP_ADV_SLOW_INT_MIN, BT_GAP_ADV_SLOW_INT_MAX, NULL), ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
|
||||
btfirstadv = false;
|
||||
} else {
|
||||
bt_le_adv_update_data(ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
|
||||
|
@ -733,6 +787,12 @@ int main(void)
|
|||
{
|
||||
LOG_INF("Hello main");
|
||||
|
||||
|
||||
//if (!device_is_ready(cons)) {
|
||||
// printk("%s: device not ready.\n", cons->name);
|
||||
// return -1;
|
||||
//}
|
||||
|
||||
led_init();
|
||||
led_test();
|
||||
|
||||
|
@ -745,9 +805,9 @@ int main(void)
|
|||
return -1;
|
||||
}
|
||||
|
||||
unsigned int passkey = 6768;
|
||||
bt_passkey_set(passkey);
|
||||
bt_conn_auth_cb_register(&auth_cb_display);
|
||||
//unsigned int passkey = 6768;
|
||||
//bt_passkey_set(passkey);
|
||||
//bt_conn_auth_cb_register(&auth_cb_display);
|
||||
|
||||
k_sem_give(&ble_init_ok);
|
||||
|
||||
|
|
|
@ -44,17 +44,17 @@ void ruuvi_updateBattery(uint16_t batt_mV)
|
|||
|
||||
void addRecord(uint8_t type, int16_t value)
|
||||
{
|
||||
if(TsOK() == true)
|
||||
{
|
||||
//if(TsOK() == true)
|
||||
//{
|
||||
if(database.index == SIZE_DB) {
|
||||
database.index = 0;
|
||||
}
|
||||
database.db[database.index].type = type;
|
||||
database.db[database.index].value = value;
|
||||
uint32_t ts = getTs();
|
||||
uint32_t ts = getUptime();
|
||||
database.db[database.index].timestamp = ts;
|
||||
LOG_INF("Record index %d ts %u type %d value %d", database.index, ts, type, value);
|
||||
database.index = database.index + 1;
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,10 +3,11 @@ CONFIG_BT_PERIPHERAL=y
|
|||
CONFIG_BT_AUTO_DATA_LEN_UPDATE=n
|
||||
CONFIG_BT_AUTO_PHY_UPDATE=n
|
||||
CONFIG_BT_DEVICE_NAME="BT GPS"
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_BT_SIGNING=y
|
||||
CONFIG_BT_FIXED_PASSKEY=y
|
||||
CONFIG_BT_SMP_SC_ONLY=y
|
||||
#CONFIG_BT_SMP=y
|
||||
#CONFIG_BT_SIGNING=y
|
||||
#CONFIG_BT_FIXED_PASSKEY=y
|
||||
#CONFIG_BT_SMP_SC_ONLY=y
|
||||
CONFIG_BT_MAX_CONN=5
|
||||
CONFIG_BT_TINYCRYPT_ECC=y
|
||||
CONFIG_BT_BAS=y
|
||||
CONFIG_BT_DIS=y
|
||||
|
@ -31,9 +32,12 @@ CONFIG_BME280_TEMP_OVER_1X=y
|
|||
CONFIG_BME280_FILTER_OFF=y
|
||||
CONFIG_BME280_MODE_FORCED=y
|
||||
|
||||
#CONFIG_UART_CONSOLE=y
|
||||
CONFIG_USE_SEGGER_RTT=y
|
||||
CONFIG_RTT_CONSOLE=y
|
||||
CONFIG_PM_DEVICE=y
|
||||
CONFIG_GPIO=y
|
||||
|
||||
CONFIG_UART_CONSOLE=y
|
||||
#CONFIG_USE_SEGGER_RTT=y
|
||||
#CONFIG_RTT_CONSOLE=y
|
||||
#CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=1024
|
||||
#CONFIG_SEGGER_RTT_BUFFER_SIZE_DOWN=512
|
||||
|
||||
|
@ -41,9 +45,9 @@ CONFIG_LOG=y
|
|||
CONFIG_LOG_DEFAULT_LEVEL=2
|
||||
CONFIG_LOG_MAX_LEVEL=4
|
||||
CONFIG_LOG_INFO_COLOR_GREEN=y
|
||||
CONFIG_LOG_BACKEND_RTT=y
|
||||
CONFIG_LOG_BACKEND_UART=n
|
||||
#CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_LOG_BACKEND_RTT=n
|
||||
CONFIG_LOG_BACKEND_UART=y
|
||||
CONFIG_STDOUT_CONSOLE=y
|
||||
|
||||
#CONFIG_BOOT_DELAY=5000
|
||||
|
||||
|
|
Loading…
Reference in New Issue