Code cleanup
This commit is contained in:
parent
86c205dcca
commit
dceae922de
@ -39,8 +39,7 @@ ElrsTx::ElrsTx(HardwareSerial& serial): _serial(serial) { }
|
||||
/**
|
||||
* @brief Initialize the ELRS transmitter
|
||||
*/
|
||||
void ElrsTx::begin(int packet_rate, int tx_power)
|
||||
{
|
||||
void ElrsTx::begin(int packet_rate, int tx_power) {
|
||||
const int ELRS_SERIAL_BAUDRATE = 400000;
|
||||
|
||||
_serial.begin(ELRS_SERIAL_BAUDRATE);
|
||||
@ -56,11 +55,9 @@ void ElrsTx::begin(int packet_rate, int tx_power)
|
||||
* @param len The length of the packet
|
||||
* @return The CRC8 of the packet
|
||||
*/
|
||||
uint8_t ElrsTx::calculate_crsf_crc8(const uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
uint8_t ElrsTx::calculate_crsf_crc8(const uint8_t *ptr, uint8_t len) {
|
||||
uint8_t crc = 0;
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
{
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
crc = CRSF_CRC8TAB[crc ^ *ptr++];
|
||||
}
|
||||
return crc;
|
||||
@ -79,8 +76,7 @@ uint8_t ElrsTx::calculate_crsf_crc8(const uint8_t *ptr, uint8_t len)
|
||||
* @param packet Resulting packet
|
||||
* @param channels Channel data
|
||||
*/
|
||||
void ElrsTx::prepare_crsf_data_packet(uint8_t packet[], int16_t channels[])
|
||||
{
|
||||
void ElrsTx::prepare_crsf_data_packet(uint8_t packet[], int16_t channels[]) {
|
||||
const int CRSF_TYPE_CHANNELS = 0x16;
|
||||
|
||||
packet[0] = ELRS_ADDRESS;
|
||||
@ -136,8 +132,7 @@ void ElrsTx::prepare_crsf_data_packet(uint8_t packet[], int16_t channels[])
|
||||
* @param command Command to be sent
|
||||
* @param value Value to be sent
|
||||
*/
|
||||
void ElrsTx::prepare_crsf_cmd_packet(uint8_t packet_cmd[], uint8_t command, uint8_t value)
|
||||
{
|
||||
void ElrsTx::prepare_crsf_cmd_packet(uint8_t packet_cmd[], uint8_t command, uint8_t value) {
|
||||
const int ELRS_ADDR_RADIO = 0xEA;
|
||||
const int ELRS_TYPE_SETTINGS_WRITE = 0x2D;
|
||||
|
||||
@ -157,49 +152,37 @@ void ElrsTx::prepare_crsf_cmd_packet(uint8_t packet_cmd[], uint8_t command, uint
|
||||
* @param value Value (0-2048) to be set
|
||||
* @param channel Channel (0-11) to be set
|
||||
*/
|
||||
void ElrsTx::set_data(int16_t value, uint8_t channel)
|
||||
{
|
||||
elrs_channels[channel] = value;
|
||||
}
|
||||
void ElrsTx::set_data(int16_t value, uint8_t channel) { elrs_channels[channel] = value; }
|
||||
|
||||
/**
|
||||
* @brief Send data to ERLS TX. This function should be called within the main loop with a 1.6ms interval
|
||||
*/
|
||||
void ElrsTx::send_data()
|
||||
{
|
||||
void ElrsTx::send_data() {
|
||||
const int ELRS_PKT_RATE_COMMAND = 0x01;
|
||||
const int ELRS_POWER_COMMAND = 0x06;
|
||||
|
||||
if (elrs_init_done == true)
|
||||
{
|
||||
if (elrs_init_done == true) {
|
||||
prepare_crsf_data_packet(crsf_packet, elrs_channels);
|
||||
_serial.write(crsf_packet, CRSF_PACKET_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Setting up initial communication link between ERLS TX and Teensy (give ERLS TX time to auto detect Teensy)
|
||||
if (elrs_init_counter < 500)
|
||||
{
|
||||
if (elrs_init_counter < 500) {
|
||||
prepare_crsf_data_packet(crsf_packet, elrs_channels);
|
||||
_serial.write(crsf_packet, CRSF_PACKET_SIZE);
|
||||
elrs_init_counter++;
|
||||
}
|
||||
// Send command to update TX packet rate
|
||||
else if (elrs_init_counter < 505)
|
||||
{
|
||||
else if (elrs_init_counter < 505) {
|
||||
prepare_crsf_cmd_packet(crsf_cmd_packet, ELRS_PKT_RATE_COMMAND, _packet_rate);
|
||||
_serial.write(crsf_cmd_packet, CRSF_CMD_PACKET_SIZE);
|
||||
elrs_init_counter++;
|
||||
}
|
||||
// Send command to update TX power
|
||||
else if (elrs_init_counter < 510)
|
||||
{
|
||||
else if (elrs_init_counter < 510) {
|
||||
prepare_crsf_cmd_packet(crsf_cmd_packet, ELRS_POWER_COMMAND, _tx_power);
|
||||
_serial.write(crsf_cmd_packet, CRSF_CMD_PACKET_SIZE);
|
||||
elrs_init_counter++;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
elrs_init_done = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,8 +70,7 @@ const int ELRS_TX_POWER_250mW = 3;
|
||||
const int ELRS_TX_POWER_500mW = 4;
|
||||
const int ELRS_TX_POWER_1000mW = 5;
|
||||
|
||||
class ElrsTx
|
||||
{
|
||||
class ElrsTx {
|
||||
public:
|
||||
ElrsTx(HardwareSerial &serial);
|
||||
|
||||
@ -93,27 +92,18 @@ class ElrsTx
|
||||
|
||||
// crc implementation from CRSF protocol document rev7
|
||||
const uint8_t CRSF_CRC8TAB[256] = {
|
||||
0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54, 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
|
||||
0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06, 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
|
||||
0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0, 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
|
||||
0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2, 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
|
||||
0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9, 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
|
||||
0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B, 0xE6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, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
|
||||
0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26, 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F, 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74, 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
|
||||
0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82, 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB, 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0, 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9};
|
||||
|
||||
uint8_t calculate_crsf_crc8(const uint8_t *ptr, uint8_t len);
|
||||
void prepare_crsf_data_packet(uint8_t packet[], int16_t channels[]);
|
||||
void prepare_crsf_cmd_packet(uint8_t packet_cmd[], uint8_t command, uint8_t value);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -36,8 +36,7 @@ const int LED_OFF = 0;
|
||||
const int LED_ON = 1;
|
||||
const int LED_BLINK = 2;
|
||||
|
||||
class IndicatorLed
|
||||
{
|
||||
class IndicatorLed {
|
||||
public:
|
||||
IndicatorLed(int pin);
|
||||
void begin();
|
||||
|
||||
@ -29,12 +29,11 @@
|
||||
* Joystick based on standard teensy "usb_joystick" library for HID joystick usb data communication.
|
||||
*/
|
||||
|
||||
|
||||
#include "ElrsTx.h"
|
||||
#include "IndicatorLed.h"
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
#include <ResponsiveAnalogRead.h>
|
||||
#include "ElrsTx.h"
|
||||
#include "IndicatorLed.h"
|
||||
|
||||
IndicatorLed status_led(13);
|
||||
IndicatorLed button_led_1(22);
|
||||
@ -79,8 +78,7 @@ const int DEADZONE_X = 50;
|
||||
const int DEADZONE_Y = 50;
|
||||
const int JOYSTICK_HAT_CENTER = -1;
|
||||
|
||||
enum EEPROM_ADR
|
||||
{
|
||||
enum EEPROM_ADR {
|
||||
MAX_X1_ADR_HIGH,
|
||||
MAX_X1_ADR_LOW,
|
||||
MIN_X1_ADR_HIGH,
|
||||
@ -146,8 +144,7 @@ int joystick_calibration_mode = CALIBRATION_OFF;
|
||||
/**
|
||||
* @brief Save calibration data to EEPROM
|
||||
*/
|
||||
void save_to_eeprom()
|
||||
{
|
||||
void save_to_eeprom() {
|
||||
EEPROM.write(MAX_X1_ADR_LOW, joystick_x1_12bit_max);
|
||||
EEPROM.write(MAX_X1_ADR_HIGH, joystick_x1_12bit_max >> 8);
|
||||
EEPROM.write(MIN_X1_ADR_LOW, joystick_x1_12bit_min);
|
||||
@ -180,8 +177,7 @@ void save_to_eeprom()
|
||||
/**
|
||||
* @brief Load calibration data from EEPROM
|
||||
*/
|
||||
void load_from_eeprom()
|
||||
{
|
||||
void load_from_eeprom() {
|
||||
joystick_x1_12bit_max = (EEPROM.read(MAX_X1_ADR_HIGH) << 8);
|
||||
joystick_x1_12bit_max |= EEPROM.read(MAX_X1_ADR_LOW);
|
||||
joystick_x1_12bit_min = (EEPROM.read(MIN_X1_ADR_HIGH) << 8);
|
||||
@ -219,25 +215,21 @@ void load_from_eeprom()
|
||||
* @param analog_x2
|
||||
* @param analog_y2
|
||||
*/
|
||||
void calibrate_axis(int analog_x1, int analog_y1, int analog_x2, int analog_y2)
|
||||
{
|
||||
void calibrate_axis(int analog_x1, int analog_y1, int analog_x2, int analog_y2) {
|
||||
joystick_x1_12bit = AXIS_12BIT_CENTER;
|
||||
joystick_y1_12bit = AXIS_12BIT_CENTER;
|
||||
joystick_x2_12bit = AXIS_12BIT_CENTER;
|
||||
joystick_y2_12bit = AXIS_12BIT_CENTER;
|
||||
|
||||
// Check for calibration mode
|
||||
if (joystick_calibration_mode == CALIBRATION_INIT)
|
||||
{
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_NOT_PRESSED)
|
||||
{
|
||||
if (joystick_calibration_mode == CALIBRATION_INIT) {
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_NOT_PRESSED) {
|
||||
joystick_calibration_mode = CALIBRATION_CENTER;
|
||||
}
|
||||
}
|
||||
|
||||
// Calibrate joystick center values
|
||||
else if (joystick_calibration_mode == CALIBRATION_CENTER)
|
||||
{
|
||||
else if (joystick_calibration_mode == CALIBRATION_CENTER) {
|
||||
joystick_x1_12bit_center = analog_x1;
|
||||
joystick_y1_12bit_center = analog_y1;
|
||||
joystick_x1_12bit_max = joystick_x1_12bit_center;
|
||||
@ -252,15 +244,13 @@ void calibrate_axis(int analog_x1, int analog_y1, int analog_x2, int analog_y2)
|
||||
joystick_y2_12bit_max = joystick_y2_12bit_center;
|
||||
joystick_y2_12bit_min = joystick_y2_12bit_center;
|
||||
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_PRESSED) {
|
||||
joystick_calibration_mode = CALIBRATION_MINMAX;
|
||||
}
|
||||
}
|
||||
|
||||
// Calibrate joystick min/max values
|
||||
else if (joystick_calibration_mode == CALIBRATION_MINMAX)
|
||||
{
|
||||
else if (joystick_calibration_mode == CALIBRATION_MINMAX) {
|
||||
if (analog_x1 > joystick_x1_12bit_max) joystick_x1_12bit_max = analog_x1;
|
||||
if (analog_x1 < joystick_x1_12bit_min) joystick_x1_12bit_min = analog_x1;
|
||||
if (analog_y1 > joystick_y1_12bit_max) joystick_y1_12bit_max = analog_y1;
|
||||
@ -270,8 +260,7 @@ void calibrate_axis(int analog_x1, int analog_y1, int analog_x2, int analog_y2)
|
||||
if (analog_y2 > joystick_y2_12bit_max) joystick_y2_12bit_max = analog_y2;
|
||||
if (analog_y2 < joystick_y2_12bit_min) joystick_y2_12bit_min = analog_y2;
|
||||
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) {
|
||||
joystick_calibration_mode = CALIBRATION_OFF;
|
||||
save_to_eeprom();
|
||||
}
|
||||
@ -281,8 +270,7 @@ void calibrate_axis(int analog_x1, int analog_y1, int analog_x2, int analog_y2)
|
||||
/**
|
||||
* @brief Save joystick calibration values to EEPROM
|
||||
*/
|
||||
void send_elrs_data()
|
||||
{
|
||||
void send_elrs_data() {
|
||||
// Set ELRS analog channels
|
||||
elrs.set_data(map(joystick_x1_12bit, AXIS_12BIT_MIN, AXIS_12BIT_MAX, CRSF_DIGITAL_CHANNEL_MIN, CRSF_DIGITAL_CHANNEL_MAX), 0);
|
||||
elrs.set_data(map(joystick_y1_12bit, AXIS_12BIT_MIN, AXIS_12BIT_MAX, CRSF_DIGITAL_CHANNEL_MIN, CRSF_DIGITAL_CHANNEL_MAX), 1);
|
||||
@ -290,8 +278,7 @@ void send_elrs_data()
|
||||
elrs.set_data(map(joystick_y2_12bit, AXIS_12BIT_MIN, AXIS_12BIT_MAX, CRSF_DIGITAL_CHANNEL_MIN, CRSF_DIGITAL_CHANNEL_MAX), 3);
|
||||
|
||||
// Set ELRS digital channels
|
||||
for (int i = 4; i < CRSF_MAX_CHANNEL; i++)
|
||||
{
|
||||
for (int i = 4; i < CRSF_MAX_CHANNEL; i++) {
|
||||
elrs.set_data(CRSF_DIGITAL_CHANNEL_MIN, i);
|
||||
}
|
||||
|
||||
@ -308,8 +295,7 @@ void send_elrs_data()
|
||||
/**
|
||||
* Send USB data to PC
|
||||
*/
|
||||
void send_usb_data()
|
||||
{
|
||||
void send_usb_data() {
|
||||
// Set USB analog channels
|
||||
int joystick_x1_10bit = map(joystick_x1_12bit, AXIS_12BIT_MIN, AXIS_12BIT_MAX, AXIS_10BIT_MIN, AXIS_10BIT_MAX);
|
||||
int joystick_y1_10bit = map(joystick_y1_12bit, AXIS_12BIT_MIN, AXIS_12BIT_MAX, AXIS_10BIT_MIN, AXIS_10BIT_MAX);
|
||||
@ -319,22 +305,17 @@ void send_usb_data()
|
||||
Joystick.Zrotate(joystick_x1_10bit);
|
||||
Joystick.Z(joystick_y1_10bit);
|
||||
|
||||
if (fn_mode == 2)
|
||||
{
|
||||
if (fn_mode == 2) {
|
||||
Joystick.X(AXIS_10BIT_CENTER);
|
||||
Joystick.Y(AXIS_10BIT_CENTER);
|
||||
Joystick.sliderRight(joystick_x2_10bit);
|
||||
Joystick.sliderLeft(joystick_y2_10bit);
|
||||
}
|
||||
else if (fn_mode == 1)
|
||||
{
|
||||
} else if (fn_mode == 1) {
|
||||
Joystick.X(AXIS_10BIT_CENTER);
|
||||
Joystick.Y(joystick_y2_10bit);
|
||||
Joystick.sliderRight(joystick_x2_10bit);
|
||||
Joystick.sliderLeft(AXIS_10BIT_CENTER);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Joystick.X(joystick_x2_10bit);
|
||||
Joystick.Y(joystick_y2_10bit);
|
||||
Joystick.sliderRight(AXIS_10BIT_CENTER);
|
||||
@ -342,33 +323,27 @@ void send_usb_data()
|
||||
}
|
||||
|
||||
// Set USB digital channels
|
||||
for (int i = 1; i < 32; i++)
|
||||
{
|
||||
for (int i = 1; i < 32; i++) {
|
||||
Joystick.button(i, 0);
|
||||
}
|
||||
|
||||
Joystick.hat(JOYSTICK_HAT_CENTER);
|
||||
|
||||
if (fn_mode == 2)
|
||||
{
|
||||
if (fn_mode == 2) {
|
||||
if (digitalRead(BUTTON_TOP_LEFT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(12, 1);
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(13, 1);
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_PRESSED) Joystick.button(14, 1);
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) Joystick.button(15, 1);
|
||||
if (digitalRead(BUTTON_FRONT_RIGHT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(16, 1);
|
||||
if (digitalRead(BUTTON_FRONT_RIGHT_LOWER_PIN) == BUTTON_PRESSED) Joystick.button(17, 1);
|
||||
}
|
||||
else if (fn_mode == 1)
|
||||
{
|
||||
} else if (fn_mode == 1) {
|
||||
if (digitalRead(BUTTON_TOP_LEFT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(8, 1);
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(9, 1);
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_PRESSED) Joystick.button(10, 1);
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) Joystick.button(11, 1);
|
||||
if (digitalRead(BUTTON_FRONT_RIGHT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(1, 1);
|
||||
if (digitalRead(BUTTON_FRONT_RIGHT_LOWER_PIN) == BUTTON_PRESSED) Joystick.button(2, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(3, 1);
|
||||
if (digitalRead(BUTTON_TOP_LEFT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(4, 1);
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_UPPER_PIN) == BUTTON_PRESSED) Joystick.button(5, 1);
|
||||
@ -391,21 +366,16 @@ void send_usb_data()
|
||||
* @param deadband_value
|
||||
* @param expo_value
|
||||
*/
|
||||
int apply_calibration_12bit(int gimbal_value, int min_value, int max_value, int center_value, int deadband_value, int expo_value)
|
||||
{
|
||||
int apply_calibration_12bit(int gimbal_value, int min_value, int max_value, int center_value, int deadband_value, int expo_value) {
|
||||
int calibrated_value = AXIS_12BIT_CENTER;
|
||||
|
||||
if (gimbal_value > (center_value + deadband_value))
|
||||
{
|
||||
if (gimbal_value > (center_value + deadband_value)) {
|
||||
calibrated_value = constrain(map(gimbal_value, (center_value + deadband_value), max_value, AXIS_12BIT_CENTER, AXIS_12BIT_MAX), AXIS_12BIT_CENTER, AXIS_12BIT_MAX);
|
||||
}
|
||||
else if (gimbal_value < (center_value - deadband_value))
|
||||
{
|
||||
} else if (gimbal_value < (center_value - deadband_value)) {
|
||||
calibrated_value = constrain(map(gimbal_value, min_value, (center_value - deadband_value), AXIS_12BIT_MIN, AXIS_12BIT_CENTER), AXIS_12BIT_MIN, AXIS_12BIT_CENTER);
|
||||
}
|
||||
|
||||
if (expo_value != 0)
|
||||
{
|
||||
if (expo_value != 0) {
|
||||
float joystick_x_float = calibrated_value / float(AXIS_12BIT_MAX);
|
||||
/* Calculate expo using 9th order polynomial function with 0.5 as center point */
|
||||
float joystick_x_exp = expo_value * (0.5 + 256 * pow((joystick_x_float - 0.5), 9)) + (1 - expo_value) * joystick_x_float;
|
||||
@ -418,30 +388,25 @@ int apply_calibration_12bit(int gimbal_value, int min_value, int max_value, int
|
||||
/**
|
||||
* @brief Process input data from gimbal and buttons
|
||||
*/
|
||||
void process_input_data()
|
||||
{
|
||||
void process_input_data() {
|
||||
int analog_x1_gimbal_value = 0;
|
||||
int analog_y1_gimbal_value = 0;
|
||||
int analog_x2_gimbal_value = 0;
|
||||
int analog_y2_gimbal_value = 0;
|
||||
|
||||
if (gimbal_mode == GIMBAL_MODE_FRSKY_M10)
|
||||
{
|
||||
if (gimbal_mode == GIMBAL_MODE_FRSKY_M10) {
|
||||
analog_x1_gimbal_value = constrain(AXIS_12BIT_MAX - analog_x1.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
analog_y1_gimbal_value = constrain(analog_y1.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
analog_x2_gimbal_value = constrain(analog_x2.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
analog_y2_gimbal_value = constrain(AXIS_12BIT_MAX - analog_y2.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
}
|
||||
else if (gimbal_mode == GIMBAL_MODE_FRSKY_M7)
|
||||
{
|
||||
} else if (gimbal_mode == GIMBAL_MODE_FRSKY_M7) {
|
||||
analog_x1_gimbal_value = constrain(analog_x1.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
analog_y1_gimbal_value = constrain(AXIS_12BIT_MAX - analog_y1.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
analog_x2_gimbal_value = constrain(AXIS_12BIT_MAX - analog_x2.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
analog_y2_gimbal_value = constrain(analog_y2.getValue(), AXIS_12BIT_MIN, AXIS_12BIT_MAX);
|
||||
}
|
||||
|
||||
if (joystick_calibration_mode != CALIBRATION_OFF)
|
||||
{
|
||||
if (joystick_calibration_mode != CALIBRATION_OFF) {
|
||||
calibrate_axis(analog_x1_gimbal_value, analog_y1_gimbal_value, analog_x2_gimbal_value, analog_y2_gimbal_value);
|
||||
return;
|
||||
}
|
||||
@ -453,42 +418,32 @@ void process_input_data()
|
||||
|
||||
// Check fn mode
|
||||
fn_mode = 0;
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_LOWER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_LOWER_PIN) == BUTTON_PRESSED) {
|
||||
fn_mode = 1;
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_UPPER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_UPPER_PIN) == BUTTON_PRESSED) {
|
||||
fn_mode = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Check toggle mode buttons
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_UPPER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_FRONT_LEFT_UPPER_PIN) == BUTTON_PRESSED) {
|
||||
toggle_button_mode = true;
|
||||
}
|
||||
else if (digitalRead(BUTTON_FRONT_LEFT_LOWER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
} else if (digitalRead(BUTTON_FRONT_LEFT_LOWER_PIN) == BUTTON_PRESSED) {
|
||||
toggle_button_mode = false;
|
||||
}
|
||||
|
||||
// Check toggle arm button
|
||||
if ((fn_mode == 1) && (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) != toggle_button_arm_previous_value))
|
||||
{
|
||||
if ((digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) && (toggle_button_arm == false))
|
||||
{
|
||||
if ((fn_mode == 1) && (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) != toggle_button_arm_previous_value)) {
|
||||
if ((digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) && (toggle_button_arm == false)) {
|
||||
toggle_button_arm = true;
|
||||
}
|
||||
else if ((digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) && (toggle_button_arm == true))
|
||||
{
|
||||
} else if ((digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) && (toggle_button_arm == true)) {
|
||||
toggle_button_arm = false;
|
||||
}
|
||||
toggle_button_arm_previous_value = digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN);
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
/* Init HW */
|
||||
status_led.begin();
|
||||
status_led.blink();
|
||||
@ -524,14 +479,12 @@ void setup()
|
||||
Joystick.useManualSend(true);
|
||||
|
||||
// Check if calibration mode is enabled
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_TOP_LEFT_LOWER_PIN) == BUTTON_PRESSED) {
|
||||
joystick_calibration_mode = CALIBRATION_INIT;
|
||||
}
|
||||
|
||||
// Check if bootloader mode is enabled
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED)
|
||||
{
|
||||
if (digitalRead(BUTTON_TOP_RIGHT_LOWER_PIN) == BUTTON_PRESSED) {
|
||||
button_led_2.on();
|
||||
button_led_2.update();
|
||||
delay(200);
|
||||
@ -539,8 +492,7 @@ void setup()
|
||||
}
|
||||
|
||||
// Check what gimbal mode is selected
|
||||
if (digitalRead(GIMBAL_MODE_PIN) == LOW)
|
||||
{
|
||||
if (digitalRead(GIMBAL_MODE_PIN) == LOW) {
|
||||
gimbal_mode = GIMBAL_MODE_FRSKY_M10;
|
||||
}
|
||||
|
||||
@ -548,8 +500,7 @@ void setup()
|
||||
elrs.begin(ELRS_PACKET_RATE_500Hz, ELRS_TX_POWER_25mW);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
current_timestamp_micros = micros();
|
||||
|
||||
analog_x1.update();
|
||||
@ -558,50 +509,38 @@ void loop()
|
||||
analog_y2.update();
|
||||
|
||||
/* Process data with 1ms interval*/
|
||||
if (current_timestamp_micros >= process_data_timestamp_micros)
|
||||
{
|
||||
if (current_timestamp_micros >= process_data_timestamp_micros) {
|
||||
process_input_data();
|
||||
process_data_timestamp_micros = current_timestamp_micros + TIME_US_1ms;
|
||||
}
|
||||
|
||||
/* Update/Send USB data with 5ms interval*/
|
||||
if (current_timestamp_micros >= send_usb_timestamp_micros)
|
||||
{
|
||||
if (current_timestamp_micros >= send_usb_timestamp_micros) {
|
||||
send_usb_data();
|
||||
send_usb_timestamp_micros = current_timestamp_micros + TIME_US_5ms;
|
||||
}
|
||||
|
||||
/* Update/Send ERLS data with about 1,6ms interval */
|
||||
if (current_timestamp_micros >= send_elrs_timestamp_micros)
|
||||
{
|
||||
if (current_timestamp_micros >= send_elrs_timestamp_micros) {
|
||||
send_elrs_data();
|
||||
send_elrs_timestamp_micros = current_timestamp_micros + CRSF_TIME_BETWEEN_FRAMES_US;
|
||||
}
|
||||
|
||||
/* Update indicator with 200ms interval */
|
||||
if (current_timestamp_micros >= indicator_timestamp_micros)
|
||||
{
|
||||
if (current_timestamp_micros >= indicator_timestamp_micros) {
|
||||
button_led_1.off();
|
||||
button_led_2.off();
|
||||
|
||||
if (joystick_calibration_mode == CALIBRATION_INIT)
|
||||
{
|
||||
if (joystick_calibration_mode == CALIBRATION_INIT) {
|
||||
button_led_1.blink();
|
||||
button_led_2.blink();
|
||||
}
|
||||
else if (joystick_calibration_mode == CALIBRATION_CENTER)
|
||||
{
|
||||
} else if (joystick_calibration_mode == CALIBRATION_CENTER) {
|
||||
button_led_1.blink();
|
||||
button_led_2.off();
|
||||
}
|
||||
else if (joystick_calibration_mode == CALIBRATION_MINMAX)
|
||||
{
|
||||
} else if (joystick_calibration_mode == CALIBRATION_MINMAX) {
|
||||
button_led_1.off();
|
||||
button_led_2.blink();
|
||||
}
|
||||
else if ((joystick_x1_12bit != AXIS_12BIT_CENTER) || (joystick_y1_12bit != AXIS_12BIT_MIN) ||
|
||||
(joystick_x2_12bit != AXIS_12BIT_CENTER) || (joystick_y2_12bit != AXIS_12BIT_CENTER))
|
||||
{
|
||||
} else if ((joystick_x1_12bit != AXIS_12BIT_CENTER) || (joystick_y1_12bit != AXIS_12BIT_MIN) || (joystick_x2_12bit != AXIS_12BIT_CENTER) || (joystick_y2_12bit != AXIS_12BIT_CENTER)) {
|
||||
button_led_1.on();
|
||||
button_led_2.on();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user