From cd38d9cd066277c9cdc7fb32d584a3c44adaed09 Mon Sep 17 00:00:00 2001 From: "Schoenberger, Philipp" Date: Thu, 4 Apr 2019 16:55:59 +0200 Subject: [PATCH] implement usb --- remote/include/debug.h | 2 +- remote/include/input.h | 3 +- remote/src/Multiprotocol.cpp | 5 +++ remote/src/input.cpp | 20 +++++++---- remote/src/state_fly.cpp | 52 ++++++++++++++++------------- remote/src/state_joy_usb.cpp | 64 +++++++++++++++++++++--------------- 6 files changed, 89 insertions(+), 57 deletions(-) diff --git a/remote/include/debug.h b/remote/include/debug.h index f511621..a0f0036 100644 --- a/remote/include/debug.h +++ b/remote/include/debug.h @@ -6,7 +6,7 @@ //******************** //** Debug messages ** //******************** -#define ENABLE_DBEUG // ~1k +//#define ENABLE_DBEUG // ~1k #ifdef ENABLE_DBEUG #define debug(msg, ...) { char buf[256]; sprintf(buf, msg, ##__VA_ARGS__); Serial.print(buf);} #define debugln(msg, ...) { char buf[256]; sprintf(buf, msg "\r\n", ##__VA_ARGS__); Serial.println(buf);} diff --git a/remote/include/input.h b/remote/include/input.h index 95c6db9..e379d02 100644 --- a/remote/include/input.h +++ b/remote/include/input.h @@ -57,10 +57,11 @@ class Input { void get_calibration(struct ch_config *curr_config); uint16_t *get_channel_data(void); + + uint16_t ch_raw[CH_COUNT]; private: // raw sticks input - uint16_t ch_raw[CH_COUNT]; // calculated inputs (my be inverted struct data { diff --git a/remote/src/Multiprotocol.cpp b/remote/src/Multiprotocol.cpp index a2c0f45..c7df700 100644 --- a/remote/src/Multiprotocol.cpp +++ b/remote/src/Multiprotocol.cpp @@ -66,6 +66,10 @@ uint8_t crc8; uint16_t failsafe_count; uint8_t len; +#include +USBHID HID; +HIDJoystick Joystick(HID); + // Telemetry void setup() { @@ -78,6 +82,7 @@ void setup() debugln("Multiprotocol startup"); debugln("time %s ", __TIME__); #endif + HID.begin(HID_JOYSTICK); // all inputs // outputs diff --git a/remote/src/input.cpp b/remote/src/input.cpp index ce62986..820c501 100644 --- a/remote/src/input.cpp +++ b/remote/src/input.cpp @@ -88,10 +88,11 @@ void Input::init() { this->ch_config[CH_YAW].inverted = false; this->ch_config[CH_ROLL].inverted = false; this->ch_config[CH_PITCH].inverted = false; - this->ch_config[CH_AUX1].inverted = false; - this->ch_config[CH_AUX2].inverted = false; - this->ch_config[CH_AUX3].inverted = false; - this->ch_config[CH_AUX4].inverted = false; + this->ch_config[CH_AUX1].inverted = true; + this->ch_config[CH_AUX2].inverted = true; + this->ch_config[CH_AUX3].inverted = true; + this->ch_config[CH_AUX4].inverted = true; + this->ch_config[CH_AUX5].inverted = true; } bool Input::is_centered(void) { @@ -190,8 +191,13 @@ bool Input::calibration_update(void) { } void Input::calibration_reset(void) { for (uint8_t ch = 0; ch < CH_COUNT; ch++) { - this->ch_config[ch].min = this->ch_raw[ch]; - this->ch_config[ch].max = this->ch_raw[ch]; + if (this->ch_config[ch].is_analog) { + this->ch_config[ch].min = this->ch_raw[ch]; + this->ch_config[ch].max = this->ch_raw[ch]; + }else { + this->ch_config[ch].min = CHANNEL_MIN_100; + this->ch_config[ch].max = CHANNEL_MAX_100; + } } } void Input::set_calibration(struct ch_config *new_config) @@ -214,7 +220,7 @@ void Input::update(void) { if (this->ch_config[ch].is_analog) { this->ch_raw[ch] = analogRead(this->pins[ch]); } else { - this->ch_raw[ch] = digitalRead(this->pins[ch]) == HIGH; + this->ch_raw[ch] = digitalRead(this->pins[ch]) == HIGH ? CHANNEL_MIN_100 : CHANNEL_MAX_100; } // do inverting diff --git a/remote/src/state_fly.cpp b/remote/src/state_fly.cpp index fb07818..562f872 100644 --- a/remote/src/state_fly.cpp +++ b/remote/src/state_fly.cpp @@ -8,6 +8,9 @@ #include "eeprom.h" #include "debug.h" #include "tx_def.h" +#include "pins.h" + +int16_t map16b(int16_t x, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max); LCD_state_fly::LCD_state_fly(void) { } @@ -45,8 +48,6 @@ void LCD_state_fly::enter(void) { 0b00101, 0b00101, }; lcd.createChar(rssiantenna, rssi_antenna_); - lcd.setCursor(6,1); - lcd.write(rssiantenna); byte rssi_bars_[8]; @@ -61,8 +62,6 @@ void LCD_state_fly::enter(void) { lcd.createChar(rssi_bars, rssi_bars_); - lcd.setCursor(7,1); - lcd.write(rssi_bars); byte battery_char_data[8]; battery_char_data[0] = 0b01110; @@ -75,11 +74,16 @@ void LCD_state_fly::enter(void) { battery_char_data[7] = 0b11111; lcd.createChar(battery_char, battery_char_data); - lcd.setCursor(12,1); - lcd.write(battery_char); - lcd.setCursor(12,0); lcd.write(battery_char); +#if 0 + lcd.setCursor(12,1); + lcd.write(battery_char); + lcd.setCursor(6,1); + lcd.write(rssiantenna); + lcd.setCursor(7,1); + lcd.write(rssi_bars); +#endif } void LCD_state_fly::print_time(uint16_t time) @@ -149,11 +153,6 @@ void LCD_state_fly::update(void) unsigned long time_in_ms = millis() - this->time_enter; unsigned long time_in_s = time_in_ms/1000; // to sec - this->print_time(time_in_s); - this->print_akku_quad(akku_quad); - this->print_akku_remote(akku_remote); - this->print_rssi(rssi_percent); - uint32_t end__ = micros(); uint32_t start = micros(); uint32_t next_callback_time; @@ -188,20 +187,23 @@ void LCD_state_fly::update(void) switch(call) { - case 10: - rssi_percent += 1; - if(rssi_percent > 100) - rssi_percent = 0; - this->print_rssi(rssi_percent); - break; - case 20: + + case 10: // update time time_in_ms = millis() - this->time_enter; time_in_s = time_in_ms/1000; // to sec this->print_time(time_in_s); break; +#if 0 + case 20: + rssi_percent += 1; + if(rssi_percent > 100) + rssi_percent = 0; + this->print_rssi(rssi_percent); + break; + case 30: // update akku akku_quad += 1; @@ -210,11 +212,17 @@ void LCD_state_fly::update(void) this->print_akku_quad(akku_quad); break; +#endif case 40: // update akku - akku_remote += 2; - if(akku_remote > 100) - akku_remote = 0; + akku_remote = analogRead(Battery_pin); + if (akku_remote > 3830) + akku_remote = 3830; + if (akku_remote < 3450) + akku_remote = 3450; + akku_remote = map16b(akku_remote, 3450, 3830, 0, 100); + if (akku_remote > 100) + akku_remote = 100; this->print_akku_remote(akku_remote); break; diff --git a/remote/src/state_joy_usb.cpp b/remote/src/state_joy_usb.cpp index 8a6db0d..2a10187 100644 --- a/remote/src/state_joy_usb.cpp +++ b/remote/src/state_joy_usb.cpp @@ -5,13 +5,10 @@ #include "input.h" #include "debug.h" -//#include - -/* USBHID* HID; */ -/* HIDJoystick* Joystick; */ - +int16_t map16b( int16_t x, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max); +#include +extern HIDJoystick Joystick; LCD_state_joy_usb::LCD_state_joy_usb(void) { - } void LCD_state_joy_usb::enter(void) { lcd.setCursor(0,0); @@ -19,30 +16,45 @@ void LCD_state_joy_usb::enter(void) { lcd.setCursor(0,1); lcd.print(" "); delay(500); - - /* HID = new USBHID(); */ - /* Joystick = new HIDJoystick(*HID); */ - /* HID->begin(HID_JOYSTICK); */ } void LCD_state_joy_usb::update(void) { - input.update(); - input.print(); - while(1) { - /* Joystick->X(0); */ - /* delay(500); */ - /* Joystick->X(1023); */ - /* delay(500); */ - - input.update(); - - if (input.is_menu_triggered()) { - debug("%lu menu button trigger\n", millis()); - input.print(); - break; - } + + uint16_t *ch_data= input.get_channel_data(); + + uint16_t x = map16b( ch_data[Input::CH_THROTTLE], CHANNEL_MIN_100, CHANNEL_MAX_100, 0, 1023); + uint16_t y = map16b( ch_data[Input::CH_YAW], CHANNEL_MIN_100, CHANNEL_MAX_100, 0, 1023); + + uint16_t xr = map16b( ch_data[Input::CH_ROLL], CHANNEL_MIN_100, CHANNEL_MAX_100, 0, 1023); + uint16_t yr = map16b( ch_data[Input::CH_PITCH], CHANNEL_MIN_100, CHANNEL_MAX_100, 0, 1023); + + bool bt0 = ch_data[Input::CH_AUX1] == CHANNEL_MIN_100; + bool bt1 = ch_data[Input::CH_AUX2] == CHANNEL_MIN_100; + bool bt2 = ch_data[Input::CH_AUX3] == CHANNEL_MIN_100; + bool bt3 = ch_data[Input::CH_AUX4] == CHANNEL_MIN_100; + bool bt4 = ch_data[Input::CH_AUX5] == CHANNEL_MIN_100; + delay(50); + + Joystick.X(x); + Joystick.Y(y); + Joystick.Xrotate(xr); + Joystick.Yrotate(yr); + Joystick.button(0, bt0); + Joystick.button(1, bt1); + Joystick.button(2, bt2); + Joystick.button(3, bt3); + Joystick.button(4, bt4); + + char line[17]; + snprintf(line,sizeof(line),"%lu %lu", bt0 , input.ch_raw[Input::CH_AUX1]); + lcd.setCursor(0,1); + lcd.print(line); + + + if (input.is_menu_triggered()) { + debug("%lu menu button trigger\n", millis); + new_state = s_menu; } - new_state = s_menu; } void LCD_state_joy_usb::leave(void) { lcd.setCursor(0,0);