From dad94b853aefe7464abb032debbea360d10dd5d9 Mon Sep 17 00:00:00 2001 From: dlktdr Date: Sat, 26 Oct 2024 15:18:45 -0700 Subject: [PATCH 1/2] BLE joystick changes --- firmware/src/arduino_nano_33_ble.overlay | 20 ++- firmware/src/src/basetrackersettings.h | 80 ++++----- firmware/src/src/ble.cpp | 66 ++++--- firmware/src/src/boards/features.cpp | 3 + firmware/src/src/btjoystick.cpp | 209 ++++++++++++++++------- firmware/src/src/btparahead.cpp | 18 +- firmware/src/src/include/ble.h | 2 +- firmware/src/src/include/btjoystick.h | 2 +- firmware/src/src/include/defines.h | 4 +- firmware/src/src/include/joystick.h | 66 ++++++- firmware/src/src/joystick.cpp | 107 ++++++------ firmware/src/src/sense.cpp | 4 +- firmware/src/src/soc_flash.cpp | 4 +- firmware/src/src/trackersettings.cpp | 5 +- firmware/src/zephyr/nrf_prj.conf | 11 +- gui/src/HeadTracker.pro | 2 + gui/src/basetrackersettings.h | 84 ++++----- gui/src/mainwindow.cpp | 14 ++ settings/settings.csv | 38 ++--- 19 files changed, 469 insertions(+), 270 deletions(-) diff --git a/firmware/src/arduino_nano_33_ble.overlay b/firmware/src/arduino_nano_33_ble.overlay index 0a650014..29bdedd3 100644 --- a/firmware/src/arduino_nano_33_ble.overlay +++ b/firmware/src/arduino_nano_33_ble.overlay @@ -25,9 +25,10 @@ }; /* Flash Layout - * Bootloader 0x00000 - 0x10000 (Size 0x10000 65536 Bytes) - * Code 0x10000 - 0xFC000 (Size 0xEC000 966656 Bytes) - * NVM Data 0xFC000 - 0x100000 (Size 0x4000 16384 Bytes) + * Bootloader 0x00000 - 0x10000 (Size 0x10000 65536 Bytes) + * Code 0x10000 - 0xFB000 (Size 0xEB000 962560 Bytes) + * BTPersistance 0xFB000 - 0xFD000 (Size 0x2000 8192 Bytes) + * HTData 0xFD000 - 0x100000 (Size 0x3000 12288 Bytes) */ /delete-node/ &storage_partition; @@ -40,13 +41,18 @@ code_partition: partition@10000 { label = "code"; - reg = <0x10000 0xEC000>; + reg = <0x10000 0xEB000>; read-only; }; - storage_partition: partition@FC000 { - label = "storagept"; - reg = <0xFC000 0x4000>; + storage_partition: partition@FB000 { + label = "storagept"; + reg = <0xFB000 0x3000>; + }; + + ht_data_partition: partition@FD000 { + label = "htdatapt"; + reg = <0xFE000 0x2000>; }; }; }; diff --git a/firmware/src/src/basetrackersettings.h b/firmware/src/src/basetrackersettings.h index 10e9dea8..1a08e525 100644 --- a/firmware/src/src/basetrackersettings.h +++ b/firmware/src/src/basetrackersettings.h @@ -751,9 +751,9 @@ class BaseTrackerSettings { inline const bool& getCh5Arm() {return ch5arm;} void setCh5Arm(bool val=false) { ch5arm = val; } - // Bluetooth Mode (0-Off, 1- Head, 2-Receive, 3-Scanner, BT_HID_Joystick) - inline const uint8_t& getBtMode() {return btmode;} - bool setBtMode(uint8_t val=0) { + // Bluetooth Mode (-1=Uninit, 0-Disable, 1-Head, 2-Receive, 3-Scanner, 4-Gamepad) + inline const int8_t& getBtMode() {return btmode;} + bool setBtMode(int8_t val=0) { if(val >= 0 && val <= 4) { btmode = val; return true; @@ -852,58 +852,58 @@ class BaseTrackerSettings { btpairedaddress[17] = '\0'; } - // Raw Sensor Mag X + // Raw Sensor Mag X(uT) void setDataMagX(float val) { magx = val; } - // Raw Sensor Mag Y + // Raw Sensor Mag Y(uT) void setDataMagY(float val) { magy = val; } - // Raw Sensor Mag Z + // Raw Sensor Mag Z(uT) void setDataMagZ(float val) { magz = val; } - // Raw Sensor Gyro X + // Raw Sensor Gyro X(dps) void setDataGyroX(float val) { gyrox = val; } - // Raw Sensor Gyro Y + // Raw Sensor Gyro Y(dps) void setDataGyroY(float val) { gyroy = val; } - // Raw Sensor Gyro Z + // Raw Sensor Gyro Z(dps) void setDataGyroZ(float val) { gyroz = val; } - // Raw Sensor Accel X + // Raw Sensor Accel X(g) void setDataAccX(float val) { accx = val; } - // Raw Sensor Accel Y + // Raw Sensor Accel Y(g) void setDataAccY(float val) { accy = val; } - // Raw Sensor Accel Z + // Raw Sensor Accel Z(g) void setDataAccZ(float val) { accz = val; } - // Calibrated Mag X + // Calibrated Mag X(uT) void setDataOff_MagX(float val) { off_magx = val; } - // Calibrated Mag Y + // Calibrated Mag Y(uT) void setDataOff_MagY(float val) { off_magy = val; } - // Calibrated Mag Z + // Calibrated Mag Z(uT) void setDataOff_MagZ(float val) { off_magz = val; } - // Calibrated Gyro X + // Calibrated Gyro X(dps) void setDataOff_GyroX(float val) { off_gyrox = val; } - // Calibrated Gyro Y + // Calibrated Gyro Y(dps) void setDataOff_GyroY(float val) { off_gyroy = val; } - // Calibrated Gyro Z + // Calibrated Gyro Z(dps) void setDataOff_GyroZ(float val) { off_gyroz = val; } - // Calibrated Accel X + // Calibrated Accel X(g) void setDataOff_AccX(float val) { off_accx = val; } - // Calibrated Accel Y + // Calibrated Accel Y(g) void setDataOff_AccY(float val) { off_accy = val; } - // Calibrated Accel Z + // Calibrated Accel Z(g) void setDataOff_AccZ(float val) { off_accz = val; } // Board Tilt Output (us) @@ -1552,7 +1552,7 @@ class BaseTrackerSettings { bool sboutinv = true; // SBUS Transmit Inverted bool crsftxinv = false; // Invert CRSF output bool ch5arm = false; // Set channel 5 to 2000us - uint8_t btmode = 0; // Bluetooth Mode (0-Off, 1- Head, 2-Receive, 3-Scanner, BT_HID_Joystick) + int8_t btmode = 0; // Bluetooth Mode (-1=Uninit, 0-Disable, 1-Head, 2-Receive, 3-Scanner, 4-Gamepad) bool rstonwave = false; // Reset on Proximity Sense bool butlngps = false; // Long Press on the Button to Enable/Disable Tilt Roll and Pan bool rstontlt = false; // Reset Center on a Head Tilt @@ -1570,24 +1570,24 @@ class BaseTrackerSettings { char btpairedaddress[19]; // Bluetooth Remote address to Pair With // Real Time Data - float magx = 0; // Raw Sensor Mag X - float magy = 0; // Raw Sensor Mag Y - float magz = 0; // Raw Sensor Mag Z - float gyrox = 0; // Raw Sensor Gyro X - float gyroy = 0; // Raw Sensor Gyro Y - float gyroz = 0; // Raw Sensor Gyro Z - float accx = 0; // Raw Sensor Accel X - float accy = 0; // Raw Sensor Accel Y - float accz = 0; // Raw Sensor Accel Z - float off_magx = 0; // Calibrated Mag X - float off_magy = 0; // Calibrated Mag Y - float off_magz = 0; // Calibrated Mag Z - float off_gyrox = 0; // Calibrated Gyro X - float off_gyroy = 0; // Calibrated Gyro Y - float off_gyroz = 0; // Calibrated Gyro Z - float off_accx = 0; // Calibrated Accel X - float off_accy = 0; // Calibrated Accel Y - float off_accz = 0; // Calibrated Accel Z + float magx = 0; // Raw Sensor Mag X(uT) + float magy = 0; // Raw Sensor Mag Y(uT) + float magz = 0; // Raw Sensor Mag Z(uT) + float gyrox = 0; // Raw Sensor Gyro X(dps) + float gyroy = 0; // Raw Sensor Gyro Y(dps) + float gyroz = 0; // Raw Sensor Gyro Z(dps) + float accx = 0; // Raw Sensor Accel X(g) + float accy = 0; // Raw Sensor Accel Y(g) + float accz = 0; // Raw Sensor Accel Z(g) + float off_magx = 0; // Calibrated Mag X(uT) + float off_magy = 0; // Calibrated Mag Y(uT) + float off_magz = 0; // Calibrated Mag Z(uT) + float off_gyrox = 0; // Calibrated Gyro X(dps) + float off_gyroy = 0; // Calibrated Gyro Y(dps) + float off_gyroz = 0; // Calibrated Gyro Z(dps) + float off_accx = 0; // Calibrated Accel X(g) + float off_accy = 0; // Calibrated Accel Y(g) + float off_accz = 0; // Calibrated Accel Z(g) uint16_t tiltout = 0; // Board Tilt Output (us) uint16_t rollout = 0; // Board Roll Output (us) uint16_t panout = 0; // Board Pan Output (us) diff --git a/firmware/src/src/ble.cpp b/firmware/src/src/ble.cpp index 30cca515..9b5756b3 100644 --- a/firmware/src/src/ble.cpp +++ b/firmware/src/src/ble.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "io.h" @@ -35,7 +36,7 @@ LOG_MODULE_REGISTER(ble); // Globals volatile bool bleconnected = false; volatile bool btscanonly = false; -btmodet curmode = BTDISABLE; +btmodet curmode = BTNOTINIT; // UUID's struct bt_uuid_16 ccc = BT_UUID_INIT_16(0x2902); @@ -53,22 +54,30 @@ struct k_poll_event btRunEvents[1] = { void bt_ready(int error) { + if (IS_ENABLED(CONFIG_SETTINGS)) { + LOG_INF("Loading Presistent Settings"); + settings_load_subtree("bt"); + } k_poll_signal_raise(&btThreadRunSignal, 1); + LOG_INF("Ready"); } void bt_init() { + LOG_INF("Initializing"); int err = bt_enable(bt_ready); if (err) { - LOG_ERR("Bluetooth init failed (err %d)", err); + LOG_ERR("Initialization failed (err %d)", err); return; } - LOG_INF("Bluetooth initialized"); + } void bt_Thread() { - int64_t usduration = 0; + uint64_t usduration = 0; + int bt_inverval = 0; + uint64_t btPeriod = BT_PERIOD; while (1) { k_poll(btRunEvents, 1, K_FOREVER); @@ -77,12 +86,15 @@ void bt_Thread() continue; } - usduration = micros64(); - // Check if bluetooth mode has changed - if(curmode != trkset.getBtMode()) + if(curmode != trkset.getBtMode()) { + btPeriod = BT_PERIOD; BTSetMode((btmodet)trkset.getBtMode()); + } + usduration = micros64(); + + int rv = 0; switch (curmode) { case BTPARAHEAD: BTHeadExecute(); // Peripheral BLE device @@ -93,7 +105,16 @@ void bt_Thread() case BTSCANONLY: break; case BTJOYSTICK: - BTJoystickExecute(); + rv = BTJoystickExecute(); + if(rv != bt_inverval) { + bt_inverval = rv; + btPeriod = bt_inverval * 1250; + if(btPeriod < 7500) { + LOG_ERR("Joystick Interval Too Short, Setting to 7500"); + btPeriod = 7500; + } + LOG_INF("Joystick Interval Set to %d, Period %llu", bt_inverval, btPeriod); + } break; default: break; @@ -101,11 +122,11 @@ void bt_Thread() // Adjust sleep for a more accurate period usduration = micros64() - usduration; - if (BT_PERIOD - usduration < - BT_PERIOD * 0.7) { // Took a long time. Will crash if sleep is too short - k_usleep(BT_PERIOD); + if (usduration > btPeriod) { // Took a long time. Will crash if sleep is too short + LOG_ERR("Missed deadline (%llu) period(%llu)", usduration, btPeriod); + k_usleep(btPeriod); } else { - k_usleep(BT_PERIOD - usduration); + k_usleep(btPeriod - usduration); } } } @@ -117,18 +138,24 @@ void BTSetMode(btmodet mode) k_sem_give(&btPauseSem); + // Clear persistent pairing data if the user changed the mode + // otherwise if mode = BTNOTINIT, then we are just starting up + if(IS_ENABLED(CONFIG_SETTINGS) && curmode != BTNOTINIT) { + LOG_INF("Clearing Pairing Data"); + bt_unpair(BT_ID_DEFAULT, NULL); + } + // Shut Down + btscanonly = false; switch (curmode) { case BTPARAHEAD: BTHeadStop(); break; case BTPARARMT: BTRmtStop(); - btscanonly = false; break; case BTSCANONLY: BTRmtStop(); - btscanonly = false; break; case BTJOYSTICK: BTJoystickStop(); @@ -159,10 +186,9 @@ void BTSetMode(btmodet mode) default: break; } + curmode = mode; k_sem_take(&btPauseSem, K_NO_WAIT); - - curmode = mode; } btmodet BTGetMode() { return curmode; } @@ -239,19 +265,19 @@ int8_t BTGetRSSI() bool leparamrequested(struct bt_conn *conn, struct bt_le_conn_param *param) { - LOG_INF("Bluetooth Params Request. IntMax:%d IntMin:%d Lat:%d Timeout:%d", param->interval_max, + LOG_INF("Params Requested. IntMax:%d IntMin:%d Lat:%d Timeout:%d", param->interval_max, param->interval_min, param->latency, param->timeout); return true; } void leparamupdated(struct bt_conn *conn, uint16_t interval, uint16_t latency, uint16_t timeout) { - LOG_INF("Bluetooth Params Updated. Int:%d Lat:%d Timeout:%d", interval, latency, timeout); + LOG_INF("Params Updated. Int:%d Lat:%d Timeout:%d", interval, latency, timeout); } void securitychanged(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { - LOG_INF("Bluetooth Security Changed. Lvl:%d Err:%d", level, err); + LOG_INF("Security Changed. Lvl:%d Err:%d", level, err); } const char *printPhy(int phy) @@ -271,7 +297,7 @@ const char *printPhy(int phy) void lephyupdated(struct bt_conn *conn, struct bt_conn_le_phy_info *param) { - LOG_INF("Bluetooth PHY Updated. RxPHY:%d TxPHY:%d", param->rx_phy, param->tx_phy); + LOG_INF("PHY Updated. RxPHY:%s TxPHY:%s", printPhy(param->rx_phy), printPhy(param->tx_phy)); } #else diff --git a/firmware/src/src/boards/features.cpp b/firmware/src/src/boards/features.cpp index 82c9c792..c28eddad 100644 --- a/firmware/src/src/boards/features.cpp +++ b/firmware/src/src/boards/features.cpp @@ -82,6 +82,9 @@ void getBoardFeatures(DynamicJsonDocument &json) #endif #if defined(CONFIG_BT) array.add("BT"); + #if defined(CONFIG_BT_SETTINGS) + array.add("BTJOYSTICK"); + #endif #endif #if defined(CONFIG_WIFI) array.add("WIFI"); diff --git a/firmware/src/src/btjoystick.cpp b/firmware/src/src/btjoystick.cpp index 29aea7f0..c113ec1b 100644 --- a/firmware/src/src/btjoystick.cpp +++ b/firmware/src/src/btjoystick.cpp @@ -33,12 +33,15 @@ LOG_MODULE_REGISTER(btjoystick); -#if defined(CONFIG_BT) +#if defined(CONFIG_BT) && defined(CONFIG_BT_SETTINGS) -static hidreport_s report; +static struct HidReportInput1 btreport; static uint16_t bthidchans[16]; static struct bt_conn *curconn = NULL; static char _address[18] = "00:00:00:00:00:00"; +static char _joystickname[] = "HT"; + +K_SEM_DEFINE(btJoystick_sem, 0, 1); enum { HIDS_REMOTE_WAKE = BIT(0), @@ -62,16 +65,6 @@ static struct hids_info info = { .flags = HIDS_NORMALLY_CONNECTABLE, }; -static struct bt_le_adv_param my_param = { - .id = BT_ID_DEFAULT, - .sid = 0, - .secondary_max_skip = 0, - .options = BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_USE_NAME, - .interval_min = (BT_GAP_ADV_FAST_INT_MIN_2), - .interval_max = (BT_GAP_ADV_FAST_INT_MAX_2), - .peer = (NULL), -}; - enum { HIDS_INPUT = 0x01, HIDS_OUTPUT = 0x02, @@ -79,35 +72,42 @@ enum { }; static struct hids_report input = { - .id = 0x00, + .id = 0x01, .type = HIDS_INPUT, }; -static uint8_t simulate_input; +static uint8_t notify_hid_subscribed = false; static uint8_t ctrl_point; +static int btinterval = 0; static ssize_t read_info(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { + LOG_INF("BLE - Reading Info"); return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, sizeof(struct hids_info)); } static ssize_t read_report_map(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { - return bt_gatt_attr_read(conn, attr, buf, len, offset, hid_report_desc, sizeof(hid_report_desc)); + LOG_INF("BLE - Reading Report Map"); + return bt_gatt_attr_read(conn, attr, buf, len, offset, hid_gamepad_report_desc, sizeof(hid_gamepad_report_desc)); } static ssize_t read_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { - return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, sizeof(hidreport_s)); + LOG_INF("BLE - Reading Report"); + return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data, sizeof(struct hids_report)); } static void input_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value) { LOG_INF("BLE - Notifications Requested"); - simulate_input = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0; + notify_hid_subscribed = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0; + // On subscribe, give semaphore to initiate notifications + if(notify_hid_subscribed) + k_sem_give(&btJoystick_sem); } static ssize_t read_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, @@ -142,9 +142,9 @@ struct bt_gatt_attr bthid_attr[] = { read_report_map, NULL, NULL), // Attribute 5,6 BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, - BT_GATT_PERM_READ, read_input_report, NULL, NULL), + BT_GATT_PERM_READ_ENCRYPT, read_input_report, NULL, NULL), // Attribute 7 - BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), + BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, read_report, NULL, &input), BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point), @@ -152,71 +152,127 @@ struct bt_gatt_attr bthid_attr[] = { struct bt_gatt_service bthid_svc = BT_GATT_SERVICE(bthid_attr); -static const struct bt_data ad[] = { +// static struct bt_le_adv_param my_joyparam = { +// .id = BT_ID_DEFAULT, +// .sid = 0, +// .secondary_max_skip = 0, +// .options = BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME, +// .interval_min = (BT_GAP_ADV_FAST_INT_MIN_2), +// .interval_max = (BT_GAP_ADV_FAST_INT_MAX_2), +// .peer = (NULL), +// }; + +static const struct bt_data hidad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), - BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_HIDS_VAL), - BT_UUID_16_ENCODE(BT_UUID_BAS_VAL)), + BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_HIDS_VAL)), + BT_DATA(BT_DATA_NAME_COMPLETE, _joystickname, 2), BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE, 0xC4, 0x03), }; +char addr_str[50]; + static void connected(struct bt_conn *conn, uint8_t err) { - char addr[BT_ADDR_LE_STR_LEN]; - bleconnected = true; + if(bleconnected) { + LOG_WRN("Bluetooth already connected"); + return; + } // Stop Advertising - bt_le_adv_stop(); - curconn = bt_conn_ref(conn); + LOG_INF("Stopping Advertising"); + // bt_le_adv_stop(); + bleconnected = true; - if (err) { - LOG_ERR("Failed to connect to %s (%u)\n", addr, err); + curconn = bt_conn_ref(conn); + if(curconn == NULL) { + LOG_ERR("Failed to get connection reference"); return; } - struct bt_conn_info info; - bt_conn_get_info(conn, &info); - char addr_str[50]; + bt_conn_get_info(curconn, &info); + bt_addr_le_to_str(info.le.dst, addr_str, sizeof(addr_str)); LOG_INF("Connected to Address %s", addr_str); + + int rv = bt_conn_set_security(curconn, BT_SECURITY_L2); + if (rv) { + LOG_ERR("Failed to set security (err %d)", rv); + } } -static void disconnected(struct bt_conn *conn, uint8_t reason) +// Joystick HID Report notify complete callback. This is called when the report has been sent. +// prevent bufffer from filling. + +void btJoystickNotifyCompleteCB(struct bt_conn *conn, void *user_data) { - LOG_WRN("Bluetooth disconnected (reason %d)", reason); + k_sem_give(&btJoystick_sem); +} - // Start advertising - int err = bt_le_adv_start(&my_param, ad, ARRAY_SIZE(ad), NULL, 0); +int startAdvertising() +{ + int err; + err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, hidad, ARRAY_SIZE(hidad), NULL, 0); if (err) { - LOG_ERR("Advertising failed to start (err %d)", err); - return; + LOG_ERR("Advertising failed to start (err %d) (size %d)", err, ARRAY_SIZE(hidad)); + return err; + } else { + LOG_INF("Advertising successfully (size %d)", ARRAY_SIZE(hidad)); } + return err; +} - if (curconn) bt_conn_unref(curconn); - curconn = NULL; +static void disconnected(struct bt_conn *conn, uint8_t reason) +{ + LOG_WRN("Bluetooth disconnected (reason %d)", reason); + + // if (curconn) { + // LOG_WRN("Cleaning up connection"); + // bt_conn_unref(curconn); + // } + //curconn = NULL; bleconnected = false; + notify_hid_subscribed = false; + //k_sem_reset(&btJoystick_sem); + // startAdvertising(); +} + +void btjoyparamupdated(struct bt_conn *conn, uint16_t interval, uint16_t latency, uint16_t timeout) +{ + LOG_INF("Params Updated. Int:%d Lat:%d Timeout:%d", interval, latency, timeout); + btinterval = interval; } -static struct bt_conn_cb conn_callbacks = { +static struct bt_conn_cb btj_conn_callbacks = { .connected = connected, .disconnected = disconnected, .le_param_req = leparamrequested, - .le_param_updated = leparamupdated, + .le_param_updated = btjoyparamupdated, + .security_changed = securitychanged, .le_phy_updated = lephyupdated, }; + void BTJoystickStop() { + + // Stop Advertising + LOG_INF("Stopping Advertising"); // Stop Advertising bt_le_adv_stop(); // If connection open kill it if (curconn) { + LOG_INF("Disconnecting Active Connection"); bt_conn_disconnect(curconn, 0); bt_conn_unref(curconn); } - + curconn = NULL; + notify_hid_subscribed = false; + bleconnected = false; + LOG_INF("Unregistering HID Service"); bt_gatt_service_unregister(&bthid_svc); + bt_conn_cb_register(NULL); } static bt_addr_le_t addrarry[CONFIG_BT_ID_MAX]; @@ -224,50 +280,83 @@ static size_t addrcnt = 1; void BTJoystickStart() { - LOG_INF("Starting Bluetooth HID Joystick"); + LOG_INF("Starting HID Joystick"); bleconnected = false; + btinterval = 0; + notify_hid_subscribed = false; + k_sem_reset(&btJoystick_sem); for (int i = 0; i < 16; i++) { bthidchans[i] = TrackerSettings::PPM_CENTER; } + bt_set_name(_joystickname); - bt_gatt_service_register(&bthid_svc); - bt_conn_cb_register(&conn_callbacks); - bt_set_name("HeadTracker Joystick"); - - int err = bt_le_adv_start(&my_param, ad, ARRAY_SIZE(ad), NULL, 0); - if (err) { - LOG_ERR("Advertising failed to start (err %d)\n", err); - return; + LOG_INF("Registering HID Service"); + if(bt_gatt_service_register(&bthid_svc)) { + LOG_ERR("Failed to register HID Service"); } + LOG_INF("Registering Callbacks"); + bt_conn_cb_register(&btj_conn_callbacks); - // Discover BT Address + // Discover BT Address bt_id_get(addrarry, &addrcnt); if (addrcnt > 0) bt_addr_le_to_str(&addrarry[0], _address, sizeof(_address)); - - LOG_INF("Advertising successfully started\n"); + startAdvertising(); } -void BTJoystickExecute() +struct bt_gatt_notify_params ntfy_params = { + .uuid = BT_UUID_HIDS_REPORT, + .attr = NULL, + .data = static_cast(&btreport.buttons[0]), + .len = sizeof(struct HidReportInput1) - sizeof(HidReportInput1::ReportId), + .func = btJoystickNotifyCompleteCB, + .user_data = NULL, +}; + +int BTJoystickExecute() { if (bleconnected) { clearLEDFlag(LED_BTSCANNING); setLEDFlag(LED_BTCONNECTED); - buildJoystickHIDReport(report, bthidchans); - if (simulate_input) - bt_gatt_notify(NULL, &bthid_svc.attrs[6], (void *)&report, sizeof(report)); - + // Send notifications if subsribed and buffer is free + if (notify_hid_subscribed) { + if (k_sem_take(&btJoystick_sem, K_NO_WAIT) == 0) { + buildJoystickHIDReport(btreport, bthidchans); + bt_gatt_notify_cb(curconn,&ntfy_params); + + static int mcount = 0; + static int64_t mmic = millis64() + 1000; + if (mmic < millis64()) { // Every Second + mmic = millis64() + 1000; + LOG_INF("Notify Rate = %d", mcount); + mcount = 0; + } + mcount++; + + } + } } else { // Scanning setLEDFlag(LED_BTSCANNING); clearLEDFlag(LED_BTCONNECTED); } + return btinterval; } void BTJoystickSetChannel(int channel, const uint16_t value) { bthidchans[channel] = value; } - const char *BTJoystickGetAddress() { return _address; } +#else + +// BT_SETTINGS not configured. Cannnot use an BLE HID without encryption. Device will not re-connect +// after connection loss without persistent settings. + +void BTJoystickStop() {} +void BTJoystickStart() {} +int BTJoystickExecute() {return 0;} +void BTJoystickSetChannel(int channel, const uint16_t value) {} +const char *BTJoystickGetAddress() { return "BT_DISABLED"; } + #endif diff --git a/firmware/src/src/btparahead.cpp b/firmware/src/src/btparahead.cpp index 1641546c..c9dc52e7 100644 --- a/firmware/src/src/btparahead.cpp +++ b/firmware/src/src/btparahead.cpp @@ -158,7 +158,7 @@ void BTHeadStart() chan_vals[i] = TrackerSettings::PPM_CENTER; } - LOG_INF("BLE Starting Head Bluetooth"); + LOG_INF("Starting Head Bluetooth"); bt_gatt_service_register(&bthead_svc); bt_conn_cb_register(&conn_callbacks); @@ -171,7 +171,7 @@ void BTHeadStart() return; } - LOG_INF("BLE Started Advertising"); + LOG_INF("Started Advertising"); // Discover BT Address bt_id_get(addrarry, &addrcnt); @@ -183,18 +183,18 @@ void BTHeadStart() void BTHeadStop() { - LOG_INF("BLE Stopping Head Bluetooth"); + LOG_INF("Stopping Head Bluetooth"); // Stop Advertising int rv = bt_le_adv_stop(); if (rv) { - LOG_ERR("BLE Unable to Stop advertising"); + LOG_ERR("Unable to Stop advertising"); } else { - LOG_INF("BLE Stopped Advertising"); + LOG_INF("Stopped Advertising"); } if (curconn) { - LOG_INF("BLE Disconnecting Active Connection"); + LOG_INF("Disconnecting Active Connection"); bt_conn_disconnect(curconn, 0); bt_conn_unref(curconn); } @@ -291,7 +291,7 @@ static ssize_t write_cmd(struct bt_conn *conn, const struct bt_gatt_attr *attr, LOG_INF("BLE:%.*s", len, (char*)buf); if(strncmp((const char *)buf, "Flash", 5) == 0) { - LOG_INF("Bluetooth Configurator Requested Flash"); + LOG_INF("Configurator Requested Flash"); k_sem_give(&saveToFlash_sem); } @@ -348,9 +348,9 @@ K_TIMER_DEFINE(my_timer, hasSecurityChangedTimer, NULL); static void connected(struct bt_conn *conn, uint8_t err) { if (err) { - LOG_ERR("Bluetooth Connection failed %d", err); + LOG_ERR("Connection failed %d", err); } else { - LOG_INF("Bluetooth connected :)"); + LOG_INF("Connected :)"); } // Stop Advertising diff --git a/firmware/src/src/include/ble.h b/firmware/src/src/include/ble.h index 055a2e26..f27d3cb3 100644 --- a/firmware/src/src/include/ble.h +++ b/firmware/src/src/include/ble.h @@ -71,7 +71,7 @@ bluetoothMode bluetoothModes[] = { }; */ -typedef enum { BTDISABLE = 0, BTPARAHEAD, BTPARARMT, BTSCANONLY, BTJOYSTICK } btmodet; +typedef enum { BTNOTINIT = -1, BTDISABLE = 0, BTPARAHEAD, BTPARARMT, BTSCANONLY, BTJOYSTICK } btmodet; void bt_Thread(); void bt_init(); diff --git a/firmware/src/src/include/btjoystick.h b/firmware/src/src/include/btjoystick.h index e571663c..54c20ce9 100644 --- a/firmware/src/src/include/btjoystick.h +++ b/firmware/src/src/include/btjoystick.h @@ -3,6 +3,6 @@ void BTJoystickStop(); void BTJoystickStart(); -void BTJoystickExecute(); +int BTJoystickExecute(); const char *BTJoystickGetAddress(); void BTJoystickSetChannel(int channel, const uint16_t value); diff --git a/firmware/src/src/include/defines.h b/firmware/src/src/include/defines.h index c204b684..ee252541 100644 --- a/firmware/src/src/include/defines.h +++ b/firmware/src/src/include/defines.h @@ -69,7 +69,7 @@ #if defined(CONFIG_SOC_SERIES_NRF52X) #define IO_STACK_SIZE 512 #define SERIAL_STACK_SIZE 4096 -#define BT_STACK_SIZE 1024 +#define BT_STACK_SIZE 2048 #define SENSOR_STACK_SIZE 1024 #define CALCULATE_STACK_SIZE 1024 #define UARTTX_STACK_SIZE 1024 @@ -110,7 +110,7 @@ #define IO_THREAD_PRIO PRIORITY_LOW #define SERIAL_THREAD_PRIO PRIORITY_LOW #define DATA_THREAD_PRIO PRIORITY_LOW -#define BT_THREAD_PRIO PRIORITY_HIGH + 1 +#define BT_THREAD_PRIO -PRIORITY_MED #define SENSOR_THREAD_PRIO PRIORITY_HIGH #define CALCULATE_THREAD_PRIO PRIORITY_HIGH #define UARTRX_THREAD_PRIO PRIORITY_LOW - 2 diff --git a/firmware/src/src/include/joystick.h b/firmware/src/src/include/joystick.h index 4828a0be..ed6f4e11 100644 --- a/firmware/src/src/include/joystick.h +++ b/firmware/src/src/include/joystick.h @@ -1,15 +1,63 @@ #pragma once -#define JOYSTICK_BUTTON_HIGH 1750 -#define JOYSTICK_BUTTON_LOW 1250 +#define JOYSTICK_BUTTON_HIGH (400) +#define JOYSTICK_BUTTON_LOW (-JOYSTICK_BUTTON_HIGH) -typedef struct { - uint8_t but[2]; - uint16_t channels[8]; -} hidreport_s; +static const uint8_t hid_gamepad_report_desc[] = +{ + 0x05, 0x01, // UsagePage(Generic Desktop[0x0001]) + 0x09, 0x05, // UsageId(Gamepad[0x0005]) + 0xA1, 0x01, // Collection(Application) + 0x85, 0x01, // ReportId(1) + 0x05, 0x09, // UsagePage(Button[0x0009]) + 0x19, 0x01, // UsageIdMin(Button 1[0x0001]) + 0x29, 0x10, // UsageIdMax(Button 16[0x0010]) + 0x15, 0x00, // LogicalMinimum(0) + 0x25, 0x01, // LogicalMaximum(1) + 0x95, 0x10, // ReportCount(16) + 0x75, 0x01, // ReportSize(1) + 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) + 0x05, 0x01, // UsagePage(Generic Desktop[0x0001]) + 0x09, 0x30, // UsageId(X[0x0030]) + 0x09, 0x31, // UsageId(Y[0x0031]) + 0x09, 0x32, // UsageId(Z[0x0032]) + 0x09, 0x33, // UsageId(Rx[0x0033]) + 0x09, 0x34, // UsageId(Ry[0x0034]) + 0x09, 0x35, // UsageId(Rz[0x0035]) + 0x09, 0x36, // UsageId(Slider[0x0036]) + 0x09, 0x37, // UsageId(Dial[0x0037]) + 0x16, 0x00, 0xFE, // LogicalMinimum(-512) + 0x26, 0xFF, 0x01, // LogicalMaximum(511) + 0x95, 0x08, // ReportCount(8) + 0x75, 0x0A, // ReportSize(10) + 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) + 0xC0, // EndCollection() +}; -extern const uint8_t hid_report_desc[56]; +#pragma pack(push,1) -void buildJoystickHIDReport(hidreport_s &report, uint16_t chans[16]); +#define HID_REPORT_INPUT1_ID (1) +struct HidReportInput1 +{ + uint8_t ReportId = HID_REPORT_INPUT1_ID; + uint8_t buttons[2]; + int16_t ch1:10; + int16_t ch2:10; + int16_t ch3:10; + int16_t ch4:10; + int16_t ch5:10; + int16_t ch6:10; + int16_t ch7:10; + int16_t ch8:10; +} __packed; + +#pragma pack(pop) + +// Bytes 1 - ID +// Bytes 2 - Buttons 1-8 +// Bytes 10 - Channels 1-8 (10 bits each) = 80 bits = 10 bytes +BUILD_ASSERT(sizeof(HidReportInput1) == 13, "HID Report wrong size is not packed correctly"); + +void buildJoystickHIDReport(struct HidReportInput1 &report, uint16_t chans[16]); void joystick_init(void); -void set_JoystickChannels(uint16_t chans[16]); +void set_JoystickChannels(uint16_t chans[16]); \ No newline at end of file diff --git a/firmware/src/src/joystick.cpp b/firmware/src/src/joystick.cpp index 2ac54f3c..ea182993 100644 --- a/firmware/src/src/joystick.cpp +++ b/firmware/src/src/joystick.cpp @@ -13,73 +13,77 @@ #include "io.h" #include "joystick.h" +#include "trackersettings.h" LOG_MODULE_REGISTER(joystick); static const struct device *hdev; -static hidreport_s report; - -const uint8_t hid_report_desc[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x05, // USAGE (Game Pad) - 0xa1, 0x01, // COLLECTION (Application) - 0xa1, 0x00, // COLLECTION (Physical) - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x10, // USAGE_MAXIMUM (Button 8) - 0x15, 0x00, // LOG_INFCAL_MINIMUM (0) - 0x25, 0x01, // LOG_INFCAL_MAXIMUM (1) - 0x95, 0x10, // REPORT_COUNT (8) - 0x75, 0x01, // REPORT_SIZE (1) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x09, 0x32, // USAGE (Z) - 0x09, 0x33, // USAGE (Rx) - 0x09, 0x34, // USAGE (Ry) - 0x09, 0x35, // USAGE (Rz) - 0x09, 0x36, // USAGE (Slider) - 0x09, 0x36, // USAGE (Slider) - 0x16, 0x00, 0x00, // LOG_INFCAL_MINIMUM (0) - 0x26, 0xFF, 0x03, // LOG_INFCAL_MAXIMUM (1024) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x08, // REPORT_COUNT (8) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0xc0, // END_COLLECTION - 0xc0 // END_COLLECTION -}; - -void buildJoystickHIDReport(hidreport_s &report, uint16_t chans[16]) -{ - memcpy(report.channels, chans, sizeof(report.channels)); +static struct HidReportInput1 usb_hid_report; - report.but[0] = 0; - report.but[1] = 0; +void buildJoystickHIDReport(struct HidReportInput1 &report, uint16_t chans[16]) +{ + for(unsigned int i = 0; i < sizeof(struct HidReportInput1); i++) + ((uint8_t *)&report)[i] = 0; + report.ReportId = 1; for (int i = 0; i < 8; i++) { - if (report.channels[i] == 0) // If disabled, center it - report.channels[i] = 1500; + int curchval = (int)chans[i]; + if (curchval == 0) // If disabled (equals zero), center it + curchval = TrackerSettings::PPM_CENTER; - if (report.channels[i] >= JOYSTICK_BUTTON_HIGH) { - report.but[0] |= 1 << (i * 2); - report.but[1] |= 1 << ((i - 4) * 2); - } + curchval -= TrackerSettings::PPM_CENTER; // Shift the center to zero + + if(curchval > 511) curchval = 511; + if(curchval < -511) curchval = -511; - if (report.channels[i] <= JOYSTICK_BUTTON_LOW) { - report.but[0] |= 1 << ((i * 2) + 1); - report.but[1] |= 1 << (((i - 4) * 2) + 1); + if (curchval >= JOYSTICK_BUTTON_HIGH) { + if(i < 4) + report.buttons[0] |= (1 << (i * 2) & 0xFF); + else + report.buttons[1] |= (1 << ((i - 4) * 2) & 0xFF); } - report.channels[i] -= 988; // Shift from center so it's 0-1024 + if (curchval <= JOYSTICK_BUTTON_LOW) { + if(i < 4) + report.buttons[0] |= (1 << (i * 2 + 1) & 0xFF); + else + report.buttons[1] |= (1 << ((i - 4) * 2 + 1) & 0xFF); + } + switch(i) { + case 0: + report.ch1 = curchval; + break; + case 1: + report.ch2 = curchval; + break; + case 2: + report.ch3 = curchval; + break; + case 3: + report.ch4 = curchval; + break; + case 4: + report.ch5 = curchval; + break; + case 5: + report.ch6 = curchval; + break; + case 6: + report.ch7 = curchval; + break; + case 7: + report.ch8 = curchval; + break; + } } + } void set_JoystickChannels(uint16_t chans[16]) { - buildJoystickHIDReport(report, chans); #if defined(CONFIG_USB_DEVICE_HID) - hid_int_ep_write(hdev, (uint8_t *)&report, sizeof(report), NULL); + buildJoystickHIDReport(usb_hid_report, chans); + hid_int_ep_write(hdev, (uint8_t *)&usb_hid_report, sizeof(usb_hid_report), NULL); #endif } @@ -96,9 +100,8 @@ void joystick_init(void) return; } - usb_hid_register_device(hdev, hid_report_desc, sizeof(hid_report_desc), NULL); + usb_hid_register_device(hdev, hid_gamepad_report_desc, sizeof(hid_gamepad_report_desc), NULL); usb_hid_init(hdev); - // USB enabled in serial.cpp } diff --git a/firmware/src/src/sense.cpp b/firmware/src/src/sense.cpp index 6b6b25f8..6024b15d 100644 --- a/firmware/src/src/sense.cpp +++ b/firmware/src/src/sense.cpp @@ -97,7 +97,7 @@ static uint16_t channel_data[16]; Madgwick madgwick; -int64_t usduration = 0; +int64_t usduration = 0; //TODO unsinged int64_t senseUsDuration = 0; const struct device *i2c_dev = nullptr; @@ -680,7 +680,7 @@ void calculate_Thread() // 14 Set USB Joystick Channels, Only 8 channels, Half rate or USB is overwhelmed static uint32_t joystick_update = 0; - if(joystick_update++ > 0) { + if(joystick_update++ > 1) { joystick_update = 0; set_JoystickChannels(channel_data); } diff --git a/firmware/src/src/soc_flash.cpp b/firmware/src/src/soc_flash.cpp index 5c4cd31c..da1c0438 100644 --- a/firmware/src/src/soc_flash.cpp +++ b/firmware/src/src/soc_flash.cpp @@ -34,7 +34,7 @@ spi_flash_mmap_handle_t handle; LOG_MODULE_REGISTER(soc_flash); -#define FLASH_PARTITION storage_partition +#define FLASH_PARTITION ht_data_partition #define FLASH_OFFSET FIXED_PARTITION_OFFSET(FLASH_PARTITION) #define FLASH_SIZE FIXED_PARTITION_SIZE(FLASH_PARTITION) #define FLASH_DEVICE FIXED_PARTITION_DEVICE(FLASH_PARTITION) @@ -156,7 +156,7 @@ int socWriteFlash(const uint8_t *datain, int len) return -1; } - LOG_INF("Flash write succeeded"); + LOG_INF("Flash write succeeded, %d%% used", (len * 100) / FLASH_SIZE); k_sem_take(&flashWriteSemaphore, K_NO_WAIT); return 0; diff --git a/firmware/src/src/trackersettings.cpp b/firmware/src/src/trackersettings.cpp index af60b5fa..18645a38 100644 --- a/firmware/src/src/trackersettings.cpp +++ b/firmware/src/src/trackersettings.cpp @@ -69,14 +69,15 @@ void TrackerSettings::saveToEEPROM() uint8_t buffer[TX_RNGBUF_SIZE]; k_mutex_lock(&data_mutex, K_FOREVER); + json.clear(); setJSONSettings(json); int len = serializeJson(json, buffer, TX_RNGBUF_SIZE); k_mutex_unlock(&data_mutex); if (socWriteFlash(buffer, len)) { - LOG_ERR("Flash Write Failed"); + LOG_ERR("Flash Write Failed size(%d)", len); } else { - LOG_INF("Saved to Flash"); + LOG_INF("Saved to Flash size(%d)", len); } } diff --git a/firmware/src/zephyr/nrf_prj.conf b/firmware/src/zephyr/nrf_prj.conf index 6a94e154..c6a28608 100644 --- a/firmware/src/zephyr/nrf_prj.conf +++ b/firmware/src/zephyr/nrf_prj.conf @@ -113,16 +113,23 @@ CONFIG_BT_CENTRAL=y CONFIG_BT_GATT_CLIENT=y CONFIG_BT_CTLR_TX_PWR_PLUS_8=y CONFIG_BT_DEVICE_NAME_DYNAMIC=y -CONFIG_BT_DEVICE_NAME_MAX=30 +CONFIG_BT_DEVICE_NAME="HT" +#CONFIG_BT_DEVICE_NAME_MAX=30 +CONFIG_BT_DIS=n CONFIG_BT_GATT_DYNAMIC_DB=y CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n CONFIG_BT_L2CAP_TX_MTU=65 CONFIG_BT_CTLR_RX_BUFFERS=2 -#CONFIG_BT_CTLR_LLL_PRIO=0 CONFIG_BT_USER_PHY_UPDATE=y CONFIG_BT_CTLR_PHY_CODED=y CONFIG_BT_CTLR_PHY_2M=y +# Required for proper reconnection of encrypted BLE connection +# on reboot +CONFIG_BT_SETTINGS=y +CONFIG_NVS=y +CONFIG_SETTINGS=y + # Other CONFIG_ZERO_LATENCY_IRQS=y CONFIG_REBOOT=y diff --git a/gui/src/HeadTracker.pro b/gui/src/HeadTracker.pro index 705854e8..8835c03f 100644 --- a/gui/src/HeadTracker.pro +++ b/gui/src/HeadTracker.pro @@ -30,6 +30,7 @@ SOURCES += \ calibratewizard/calaccelerometer.cpp \ calibration.cpp \ channelviewer.cpp \ + comboboxdelegate.cpp \ diagnosticdisplay.cpp \ firmwarewizard.cpp \ gainslider.cpp \ @@ -52,6 +53,7 @@ HEADERS += \ calibratewizard/calaccelerometer.h \ calibration.h \ channelviewer.h \ + comboboxdelegate.h \ diagnosticdisplay.h \ firmwarewizard.h \ gainslider.h \ diff --git a/gui/src/basetrackersettings.h b/gui/src/basetrackersettings.h index b785dce3..b4b0e6c7 100644 --- a/gui/src/basetrackersettings.h +++ b/gui/src/basetrackersettings.h @@ -258,7 +258,7 @@ class BaseTrackerSettings : public QObject descriptions["sboutinv"] = "SBUS Transmit Inverted"; descriptions["crsftxinv"] = "Invert CRSF output"; descriptions["ch5arm"] = "Set channel 5 to 2000us"; - descriptions["btmode"] = "Bluetooth Mode (0-Off, 1- Head, 2-Receive, 3-Scanner, BT_HID_Joystick)"; + descriptions["btmode"] = "Bluetooth Mode (-1=Uninit, 0-Disable, 1-Head, 2-Receive, 3-Scanner, 4-Gamepad)"; descriptions["rstonwave"] = "Reset on Proximity Sense"; descriptions["butlngps"] = "Long Press on the Button to Enable/Disable Tilt Roll and Pan"; descriptions["rstontlt"] = "Reset Center on a Head Tilt"; @@ -271,24 +271,24 @@ class BaseTrackerSettings : public QObject descriptions["ppmframe"] = "PPM Frame Length (us)"; descriptions["ppmsync"] = "PPM Sync Pulse Length (us)"; descriptions["ppmchcnt"] = "PPM channels to output"; - descriptions["magx"] = "Raw Sensor Mag X"; - descriptions["magy"] = "Raw Sensor Mag Y"; - descriptions["magz"] = "Raw Sensor Mag Z"; - descriptions["gyrox"] = "Raw Sensor Gyro X"; - descriptions["gyroy"] = "Raw Sensor Gyro Y"; - descriptions["gyroz"] = "Raw Sensor Gyro Z"; - descriptions["accx"] = "Raw Sensor Accel X"; - descriptions["accy"] = "Raw Sensor Accel Y"; - descriptions["accz"] = "Raw Sensor Accel Z"; - descriptions["off_magx"] = "Calibrated Mag X"; - descriptions["off_magy"] = "Calibrated Mag Y"; - descriptions["off_magz"] = "Calibrated Mag Z"; - descriptions["off_gyrox"] = "Calibrated Gyro X"; - descriptions["off_gyroy"] = "Calibrated Gyro Y"; - descriptions["off_gyroz"] = "Calibrated Gyro Z"; - descriptions["off_accx"] = "Calibrated Accel X"; - descriptions["off_accy"] = "Calibrated Accel Y"; - descriptions["off_accz"] = "Calibrated Accel Z"; + descriptions["magx"] = "Raw Sensor Mag X(uT)"; + descriptions["magy"] = "Raw Sensor Mag Y(uT)"; + descriptions["magz"] = "Raw Sensor Mag Z(uT)"; + descriptions["gyrox"] = "Raw Sensor Gyro X(dps)"; + descriptions["gyroy"] = "Raw Sensor Gyro Y(dps)"; + descriptions["gyroz"] = "Raw Sensor Gyro Z(dps)"; + descriptions["accx"] = "Raw Sensor Accel X(g)"; + descriptions["accy"] = "Raw Sensor Accel Y(g)"; + descriptions["accz"] = "Raw Sensor Accel Z(g)"; + descriptions["off_magx"] = "Calibrated Mag X(uT)"; + descriptions["off_magy"] = "Calibrated Mag Y(uT)"; + descriptions["off_magz"] = "Calibrated Mag Z(uT)"; + descriptions["off_gyrox"] = "Calibrated Gyro X(dps)"; + descriptions["off_gyroy"] = "Calibrated Gyro Y(dps)"; + descriptions["off_gyroz"] = "Calibrated Gyro Z(dps)"; + descriptions["off_accx"] = "Calibrated Accel X(g)"; + descriptions["off_accy"] = "Calibrated Accel Y(g)"; + descriptions["off_accz"] = "Calibrated Accel Z(g)"; descriptions["tiltout"] = "Board Tilt Output (us)"; descriptions["rollout"] = "Board Roll Output (us)"; descriptions["panout"] = "Board Pan Output (us)"; @@ -1140,12 +1140,12 @@ class BaseTrackerSettings : public QObject bool getCh5Arm() {return _setting["ch5arm"].toBool();} void setCh5Arm(bool val=false) { _setting["ch5arm"] = val; } - // Bluetooth Mode (0-Off, 1- Head, 2-Receive, 3-Scanner, BT_HID_Joystick) - uint8_t getBtMode() { - return _setting["btmode"].toUInt(); + // Bluetooth Mode (-1=Uninit, 0-Disable, 1-Head, 2-Receive, 3-Scanner, 4-Gamepad) + int8_t getBtMode() { + return _setting["btmode"].toInt(); } - bool setBtMode(uint8_t val=0) { - if(val <= 4) { + bool setBtMode(int8_t val=0) { + if(val >= 0 && val <= 4) { _setting["btmode"] = val; return true; } @@ -1259,58 +1259,58 @@ class BaseTrackerSettings : public QObject _setting["btpairedaddress"] = val; } - // Raw Sensor Mag X + // Raw Sensor Mag X(uT) float getDataMagX() { return _data["magx"].toFloat(); } - // Raw Sensor Mag Y + // Raw Sensor Mag Y(uT) float getDataMagY() { return _data["magy"].toFloat(); } - // Raw Sensor Mag Z + // Raw Sensor Mag Z(uT) float getDataMagZ() { return _data["magz"].toFloat(); } - // Raw Sensor Gyro X + // Raw Sensor Gyro X(dps) float getDataGyroX() { return _data["gyrox"].toFloat(); } - // Raw Sensor Gyro Y + // Raw Sensor Gyro Y(dps) float getDataGyroY() { return _data["gyroy"].toFloat(); } - // Raw Sensor Gyro Z + // Raw Sensor Gyro Z(dps) float getDataGyroZ() { return _data["gyroz"].toFloat(); } - // Raw Sensor Accel X + // Raw Sensor Accel X(g) float getDataAccX() { return _data["accx"].toFloat(); } - // Raw Sensor Accel Y + // Raw Sensor Accel Y(g) float getDataAccY() { return _data["accy"].toFloat(); } - // Raw Sensor Accel Z + // Raw Sensor Accel Z(g) float getDataAccZ() { return _data["accz"].toFloat(); } - // Calibrated Mag X + // Calibrated Mag X(uT) float getDataOff_MagX() { return _data["off_magx"].toFloat(); } - // Calibrated Mag Y + // Calibrated Mag Y(uT) float getDataOff_MagY() { return _data["off_magy"].toFloat(); } - // Calibrated Mag Z + // Calibrated Mag Z(uT) float getDataOff_MagZ() { return _data["off_magz"].toFloat(); } - // Calibrated Gyro X + // Calibrated Gyro X(dps) float getDataOff_GyroX() { return _data["off_gyrox"].toFloat(); } - // Calibrated Gyro Y + // Calibrated Gyro Y(dps) float getDataOff_GyroY() { return _data["off_gyroy"].toFloat(); } - // Calibrated Gyro Z + // Calibrated Gyro Z(dps) float getDataOff_GyroZ() { return _data["off_gyroz"].toFloat(); } - // Calibrated Accel X + // Calibrated Accel X(g) float getDataOff_AccX() { return _data["off_accx"].toFloat(); } - // Calibrated Accel Y + // Calibrated Accel Y(g) float getDataOff_AccY() { return _data["off_accy"].toFloat(); } - // Calibrated Accel Z + // Calibrated Accel Z(g) float getDataOff_AccZ() { return _data["off_accz"].toFloat(); } // Board Tilt Output (us) diff --git a/gui/src/mainwindow.cpp b/gui/src/mainwindow.cpp index 45fc230e..13b8e93c 100644 --- a/gui/src/mainwindow.cpp +++ b/gui/src/mainwindow.cpp @@ -1336,6 +1336,20 @@ void MainWindow::featuresReceiveComplete() } if(features.contains("BT")) { ui->tabBT->setEnabled(true); + ui->cmbBtMode->blockSignals(true); + ui->cmbBtMode->clear(); + ui->cmbBtMode->addItem("Disabled"); + ui->cmbBtMode->addItem("FrSky - Wireless Transmitter (Head)"); + ui->cmbBtMode->addItem("FrSky - Wireless Receiver (Remote)"); + ui->cmbBtMode->addItem("FrSky - Wireless Receiver - Scanner Mode"); + if(features.contains("BTJOYSTICK")) { + ui->cmbBtMode->addItem("Bluetooth HID Joystick"); + ui->cmbBtMode->setCurrentIndex(trkset.getBtMode()<=4?trkset.getBtMode():0); + } else { + ui->cmbBtMode->setCurrentIndex(trkset.getBtMode()<=3?trkset.getBtMode():0); + } + ui->cmbBtMode->blockSignals(false); + } if(features.contains("PWM1CH") || features.contains("PWM2CH") || diff --git a/settings/settings.csv b/settings/settings.csv index 57c351dc..07587880 100644 --- a/settings/settings.csv +++ b/settings/settings.csv @@ -41,15 +41,15 @@ u8,Const,BT_MODE_REMOTE,2,,,,,,, u8,Const,BT_MODE_SCANNER,3,,,,,,, u8,Const,BT_MODE_HIDJOYSTICK,4,,,,,,, ,,,,,,,,,, -float,Data,MagX,,,,Raw Sensor Mag X,,1,3, -float ,Data,MagY,,,,Raw Sensor Mag Y,,1,3, -float ,Data,MagZ,,,,Raw Sensor Mag Z,,1,3, -float,Data,GyroX,,,,Raw Sensor Gyro X,,1,3, -float,Data,GyroY,,,,Raw Sensor Gyro Y,,1,3, -float,Data,GyroZ,,,,Raw Sensor Gyro Z,,1,3, -float,Data,AccX,,,,Raw Sensor Accel X,,1,3, -float,Data,AccY,,,,Raw Sensor Accel Y,,1,3, -float,Data,AccZ,,,,Raw Sensor Accel Z,,1,3, +float,Data,MagX,,,,Raw Sensor Mag X(uT),,1,3, +float ,Data,MagY,,,,Raw Sensor Mag Y(uT),,1,3, +float ,Data,MagZ,,,,Raw Sensor Mag Z(uT),,1,3, +float,Data,GyroX,,,,Raw Sensor Gyro X(dps),,1,3, +float,Data,GyroY,,,,Raw Sensor Gyro Y(dps),,1,3, +float,Data,GyroZ,,,,Raw Sensor Gyro Z(dps),,1,3, +float,Data,AccX,,,,Raw Sensor Accel X(g),,1,3, +float,Data,AccY,,,,Raw Sensor Accel Y(g),,1,3, +float,Data,AccZ,,,,Raw Sensor Accel Z(g),,1,3, u16,Data,ChOut[16],,,,Channel Outputs,,1,, u16,Data,BtCh[8],,,,Bluetooth Inputs,,1,, u16,Data,PpmCh[16],,,,PPM Inputs,,1,, @@ -57,15 +57,15 @@ u16,Data,UartCh[16],,,,Uart Channels (Sbus/Crsf),,1,, float,Data,Quat[4],,,,Quaternion Output (Tilt / Roll / Pan),,1,3, char,Data,BtAddr[18],,,,Local Bluetooth Address,,10,, char,Data,BtRmt[18],,,,Remote Bluetooth Address,,10,, -float,Data,Off_MagX,,,,Calibrated Mag X,,2,3, -float,Data,Off_MagY,,,,Calibrated Mag Y,,2,3, -float,Data,Off_MagZ,,,,Calibrated Mag Z,,2,3, -float,Data,Off_GyroX,,,,Calibrated Gyro X,,2,3, -float,Data,Off_GyroY,,,,Calibrated Gyro Y,,2,3, -float,Data,Off_GyroZ,,,,Calibrated Gyro Z,,2,3, -float,Data,Off_AccX,,,,Calibrated Accel X,,2,3, -float,Data,Off_AccY,,,,Calibrated Accel Y,,2,3, -float,Data,Off_AccZ,,,,Calibrated Accel Z,,2,3, +float,Data,Off_MagX,,,,Calibrated Mag X(uT),,2,3, +float,Data,Off_MagY,,,,Calibrated Mag Y(uT),,2,3, +float,Data,Off_MagZ,,,,Calibrated Mag Z(uT),,2,3, +float,Data,Off_GyroX,,,,Calibrated Gyro X(dps),,2,3, +float,Data,Off_GyroY,,,,Calibrated Gyro Y(dps),,2,3, +float,Data,Off_GyroZ,,,,Calibrated Gyro Z(dps),,2,3, +float,Data,Off_AccX,,,,Calibrated Accel X(g),,2,3, +float,Data,Off_AccY,,,,Calibrated Accel Y(g),,2,3, +float,Data,Off_AccZ,,,,Calibrated Accel Z(g),,2,3, u16,Data,TiltOut,,,,Board Tilt Output (us),,1,, u16,Data,RollOut,,,,Board Roll Output (us),,1,, u16,Data,PanOut,,,,Board Pan Output (us),,1,, @@ -168,7 +168,7 @@ bool,Setting,CrsfTxInv,FALSE,,,Invert CRSF output,,,, bool,Setting,Ch5Arm,FALSE,,,Set channel 5 to 2000us,,,, ,,,,,,,,,, Bluetooth Settings,,,,,,,,,, -u8,Setting,BtMode,0,0,4,"Bluetooth Mode (0-Off, 1- Head, 2-Receive, 3-Scanner, BT_HID_Joystick)",,,, +s8,Setting,BtMode,0,0,4,"Bluetooth Mode (-1=Uninit, 0-Disable, 1-Head, 2-Receive, 3-Scanner, 4-Gamepad)",,,, char,Setting,BtPairedAddress[17],,,,Bluetooth Remote address to Pair With,,,, ,,,,,,,,,, Other Features,,,,,,,,,, From 74ac3bf8f2daac829e56b1561ba663613ceb05a0 Mon Sep 17 00:00:00 2001 From: dlktdr Date: Sat, 26 Oct 2024 15:30:38 -0700 Subject: [PATCH 2/2] Update HeadTracker.pro --- gui/src/HeadTracker.pro | 2 -- 1 file changed, 2 deletions(-) diff --git a/gui/src/HeadTracker.pro b/gui/src/HeadTracker.pro index 8835c03f..705854e8 100644 --- a/gui/src/HeadTracker.pro +++ b/gui/src/HeadTracker.pro @@ -30,7 +30,6 @@ SOURCES += \ calibratewizard/calaccelerometer.cpp \ calibration.cpp \ channelviewer.cpp \ - comboboxdelegate.cpp \ diagnosticdisplay.cpp \ firmwarewizard.cpp \ gainslider.cpp \ @@ -53,7 +52,6 @@ HEADERS += \ calibratewizard/calaccelerometer.h \ calibration.h \ channelviewer.h \ - comboboxdelegate.h \ diagnosticdisplay.h \ firmwarewizard.h \ gainslider.h \