[GitHub] mjurczak commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app
mjurczak commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app URL: https://github.com/apache/mynewt-core/pull/819#discussion_r169182831 ## File path: apps/blecsc/src/main.c ## @@ -0,0 +1,323 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include + +#include "sysinit/sysinit.h" +#include "os/os.h" +#include "console/console.h" +#include "config/config.h" +#include "nimble/ble.h" +#include "host/ble_hs.h" +#include "services/gap/ble_svc_gap.h" +#include "blecsc_sens.h" + +/* Wheel size for simulation calculations */ +#define CSC_SIM_WHEEL_CIRCUMFERENCE_MM2000 +/* Simulated cadence lower limit */ +#define CSC_SIM_CRANK_RPM_MIN 20 +/* Simulated cadence upper limit */ +#define CSC_SIM_CRANK_RPM_MAX 100 +/* Simulated speed lower limit */ +#define CSC_SIM_SPEED_KPH_MIN 0 +/* Simulated speed upper limit */ +#define CSC_SIM_SPEED_KPH_MAX 35 + +/* Log data */ +struct log blecsc_log; + +/* Noticication status */ +static bool notify_state = false; + +/* Connection handle */ +static uint16_t conn_handle; + +static uint8_t blecsc_addr_type; + +/* Advertised device name */ +static const char *device_name = "blecsc_sensor"; + +/* Advertised device appearance */ +static const uint16_t device_appearance = 1157; + +/* Measurement and notification timer */ +static struct os_callout blecsc_measure_timer; + +/* Variable holds current CSC measurement state */ +static struct ble_csc_measurement_state csc_measurement_state; + +/* Variable holds simulted speed (kilometers per hour) */ +static uint16_t csc_sim_speed_kph = CSC_SIM_SPEED_KPH_MIN; + +/* Variable holds simulated cadence (RPM) */ +static uint8_t csc_sim_crank_rpm = CSC_SIM_CRANK_RPM_MIN; + + +static int blecsc_gap_event(struct ble_gap_event *event, void *arg); + + +/* + * Enables advertising with parameters: + * o General discoverable mode + * o Undirected connectable mode + */ +static void +blecsc_advertise(void) +{ +struct ble_gap_adv_params adv_params; +struct ble_hs_adv_fields fields; +int rc; + +/* + * Set the advertisement data included in our advertisements: + * o Flags (indicates advertisement type and other general info) + * o Advertising tx power + * o Device name + */ +memset(&fields, 0, sizeof(fields)); + +/* + * Advertise two flags: + * o Discoverability in forthcoming advertisement (general) + * o BLE-only (BR/EDR unsupported) + */ +fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; + +/* + * Indicate that the TX power level field should be included; have the + * stack fill this value automatically. This is done by assigning the + * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. + */ +fields.tx_pwr_lvl_is_present = 1; +fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + +fields.name = (uint8_t *)device_name; +fields.name_len = strlen(device_name); +fields.name_is_complete = 1; + +/* + * Set appearance to: Cycling Speed and Cadence Sensor. + */ +fields.appearance = device_appearance; +fields.appearance_is_present = 1; + +rc = ble_gap_adv_set_fields(&fields); +if (rc != 0) { +BLECSC_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc); +return; +} + +/* Begin advertising */ +memset(&adv_params, 0, sizeof(adv_params)); +adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; +adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; +rc = ble_gap_adv_start(blecsc_addr_type, NULL, BLE_HS_FOREVER, + &adv_params, blecsc_gap_event, NULL); +if (rc != 0) { +BLECSC_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc); +return; +} +} + + +/* Update simulated CSC measurements. + * Each call increments wheel and crank revolution counters by one and + * computes last event time in order to match simulated candence and speed. + * Last event time is expressedd in 1/1024th of second units. + * + * 60 * 1024 + * crank_dt =
[GitHub] mjurczak commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app
mjurczak commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app URL: https://github.com/apache/mynewt-core/pull/819#discussion_r169182831 ## File path: apps/blecsc/src/main.c ## @@ -0,0 +1,323 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include + +#include "sysinit/sysinit.h" +#include "os/os.h" +#include "console/console.h" +#include "config/config.h" +#include "nimble/ble.h" +#include "host/ble_hs.h" +#include "services/gap/ble_svc_gap.h" +#include "blecsc_sens.h" + +/* Wheel size for simulation calculations */ +#define CSC_SIM_WHEEL_CIRCUMFERENCE_MM2000 +/* Simulated cadence lower limit */ +#define CSC_SIM_CRANK_RPM_MIN 20 +/* Simulated cadence upper limit */ +#define CSC_SIM_CRANK_RPM_MAX 100 +/* Simulated speed lower limit */ +#define CSC_SIM_SPEED_KPH_MIN 0 +/* Simulated speed upper limit */ +#define CSC_SIM_SPEED_KPH_MAX 35 + +/* Log data */ +struct log blecsc_log; + +/* Noticication status */ +static bool notify_state = false; + +/* Connection handle */ +static uint16_t conn_handle; + +static uint8_t blecsc_addr_type; + +/* Advertised device name */ +static const char *device_name = "blecsc_sensor"; + +/* Advertised device appearance */ +static const uint16_t device_appearance = 1157; + +/* Measurement and notification timer */ +static struct os_callout blecsc_measure_timer; + +/* Variable holds current CSC measurement state */ +static struct ble_csc_measurement_state csc_measurement_state; + +/* Variable holds simulted speed (kilometers per hour) */ +static uint16_t csc_sim_speed_kph = CSC_SIM_SPEED_KPH_MIN; + +/* Variable holds simulated cadence (RPM) */ +static uint8_t csc_sim_crank_rpm = CSC_SIM_CRANK_RPM_MIN; + + +static int blecsc_gap_event(struct ble_gap_event *event, void *arg); + + +/* + * Enables advertising with parameters: + * o General discoverable mode + * o Undirected connectable mode + */ +static void +blecsc_advertise(void) +{ +struct ble_gap_adv_params adv_params; +struct ble_hs_adv_fields fields; +int rc; + +/* + * Set the advertisement data included in our advertisements: + * o Flags (indicates advertisement type and other general info) + * o Advertising tx power + * o Device name + */ +memset(&fields, 0, sizeof(fields)); + +/* + * Advertise two flags: + * o Discoverability in forthcoming advertisement (general) + * o BLE-only (BR/EDR unsupported) + */ +fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; + +/* + * Indicate that the TX power level field should be included; have the + * stack fill this value automatically. This is done by assigning the + * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. + */ +fields.tx_pwr_lvl_is_present = 1; +fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + +fields.name = (uint8_t *)device_name; +fields.name_len = strlen(device_name); +fields.name_is_complete = 1; + +/* + * Set appearance to: Cycling Speed and Cadence Sensor. + */ +fields.appearance = device_appearance; +fields.appearance_is_present = 1; + +rc = ble_gap_adv_set_fields(&fields); +if (rc != 0) { +BLECSC_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc); +return; +} + +/* Begin advertising */ +memset(&adv_params, 0, sizeof(adv_params)); +adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; +adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; +rc = ble_gap_adv_start(blecsc_addr_type, NULL, BLE_HS_FOREVER, + &adv_params, blecsc_gap_event, NULL); +if (rc != 0) { +BLECSC_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc); +return; +} +} + + +/* Update simulated CSC measurements. + * Each call increments wheel and crank revolution counters by one and + * computes last event time in order to match simulated candence and speed. + * Last event time is expressedd in 1/1024th of second units. + * + * 60 * 1024 + * crank_dt =