Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions firmware/src/arduino_nano_33_ble.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>;
};
};
};
Expand Down
80 changes: 40 additions & 40 deletions firmware/src/src/basetrackersettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
66 changes: 46 additions & 20 deletions firmware/src/src/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>
#include <zephyr/kernel.h>

#include "io.h"
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -93,19 +105,28 @@ 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;
}

// 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);
}
}
}
Expand All @@ -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();
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions firmware/src/src/boards/features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading