diff --git a/README.md b/README.md index d32ec6f..4596de7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +### FEATURES +SILENT MODE: +During power up, push up the PITCH stick to the maximum then press power ON. + ### LAYOUT DIAGRAM ### diff --git a/sounds/pitchdecrease.mp3 b/sounds/pitchdecrease.mp3 new file mode 100644 index 0000000..93cf3ea Binary files /dev/null and b/sounds/pitchdecrease.mp3 differ diff --git a/sounds/pitchdecrease.raw b/sounds/pitchdecrease.raw new file mode 100644 index 0000000..785eeb8 Binary files /dev/null and b/sounds/pitchdecrease.raw differ diff --git a/sounds/pitchincrease.mp3 b/sounds/pitchincrease.mp3 new file mode 100644 index 0000000..a17f9ce Binary files /dev/null and b/sounds/pitchincrease.mp3 differ diff --git a/sounds/pitchincrease.raw b/sounds/pitchincrease.raw new file mode 100644 index 0000000..9b93ea6 Binary files /dev/null and b/sounds/pitchincrease.raw differ diff --git a/sounds/rolldecrease.mp3 b/sounds/rolldecrease.mp3 new file mode 100644 index 0000000..d5391e7 Binary files /dev/null and b/sounds/rolldecrease.mp3 differ diff --git a/sounds/rolldecrease.raw b/sounds/rolldecrease.raw new file mode 100644 index 0000000..68793a9 Binary files /dev/null and b/sounds/rolldecrease.raw differ diff --git a/sounds/rollincrease.mp3 b/sounds/rollincrease.mp3 new file mode 100644 index 0000000..3abba68 Binary files /dev/null and b/sounds/rollincrease.mp3 differ diff --git a/sounds/rollincrease.raw b/sounds/rollincrease.raw new file mode 100644 index 0000000..9d1ee0f Binary files /dev/null and b/sounds/rollincrease.raw differ diff --git a/sounds/throttledecrease.mp3 b/sounds/throttledecrease.mp3 new file mode 100644 index 0000000..7a88c6d Binary files /dev/null and b/sounds/throttledecrease.mp3 differ diff --git a/sounds/throttledecrease.raw b/sounds/throttledecrease.raw new file mode 100644 index 0000000..6b0e67d Binary files /dev/null and b/sounds/throttledecrease.raw differ diff --git a/sounds/throttleincrease.mp3 b/sounds/throttleincrease.mp3 new file mode 100644 index 0000000..e46edd8 Binary files /dev/null and b/sounds/throttleincrease.mp3 differ diff --git a/sounds/throttleincrease.raw b/sounds/throttleincrease.raw new file mode 100644 index 0000000..b9256fd Binary files /dev/null and b/sounds/throttleincrease.raw differ diff --git a/sounds/yawdecrease.mp3 b/sounds/yawdecrease.mp3 new file mode 100644 index 0000000..d9acdf4 Binary files /dev/null and b/sounds/yawdecrease.mp3 differ diff --git a/sounds/yawdecrease.raw b/sounds/yawdecrease.raw new file mode 100644 index 0000000..08cca6b Binary files /dev/null and b/sounds/yawdecrease.raw differ diff --git a/sounds/yawincrease.mp3 b/sounds/yawincrease.mp3 new file mode 100644 index 0000000..081f218 Binary files /dev/null and b/sounds/yawincrease.mp3 differ diff --git a/sounds/yawincrease.raw b/sounds/yawincrease.raw new file mode 100644 index 0000000..4406930 Binary files /dev/null and b/sounds/yawincrease.raw differ diff --git a/src/BLE.cpp b/src/BLE.cpp index 94f512d..1bd7818 100644 --- a/src/BLE.cpp +++ b/src/BLE.cpp @@ -41,6 +41,77 @@ void BLE::update(void) { bleGamepad.setAxes(yaw, throttle, roll, pitch, 0, 0, DPAD_CENTERED); + uint32_t m = millis(); + if(button1) { + if((uint32_t)(m - last_button1) > 100) { + bleGamepad.release(BUTTON_1); + button1 = false; + button1_sent = false; + } else { + // Only sent once and delay for 100ms + if(!button1_sent) { + bleGamepad.press(BUTTON_1); + button1_sent = true; + } + } + } + + if(button2) { + if((uint32_t)(m - last_button2) > 100) { + bleGamepad.release(BUTTON_2); + button2 = false; + button2_sent = false; + } else { + // Only sent once and delay for 100ms + if(!button2_sent) { + bleGamepad.press(BUTTON_2); + button2_sent = true; + } + } + } + + if(button3) { + if((uint32_t)(m - last_button3) > 100) { + bleGamepad.release(BUTTON_3); + button3 = false; + button3_sent = false; + } else { + // Only sent once and delay for 100ms + if(!button3_sent) { + bleGamepad.press(BUTTON_3); + button3_sent = true; + } + } + } + + if(button4) { + if((uint32_t)(m - last_button4) > 100) { + bleGamepad.release(BUTTON_4); + button4 = false; + button4_sent = false; + } else { + // Only sent once and delay for 100ms + if(!button4_sent) { + bleGamepad.press(BUTTON_4); + button4_sent = true; + } + } + } + + if(button5) { + if((uint32_t)(m - last_button5) > 100) { + bleGamepad.release(BUTTON_5); + button5 = false; + button5_sent = false; + } else { + // Only sent once and delay for 100ms + if(!button5_sent) { + bleGamepad.press(BUTTON_5); + button5_sent = true; + } + } + } + sent_counter_raw++; last_updated = millis(); diff --git a/src/BLE.h b/src/BLE.h index 1c98e82..08c660c 100644 --- a/src/BLE.h +++ b/src/BLE.h @@ -14,6 +14,23 @@ class BLE { int16_t sent_counter_raw = 0; int16_t sent_counter = 0; + bool button1 = false; + bool button2 = false; + bool button3 = false; + bool button4 = false; + bool button5 = false; + bool button1_sent = false; + bool button2_sent = false; + bool button3_sent = false; + bool button4_sent = false; + bool button5_sent = false; + uint32_t last_button1 = 0; + uint32_t last_button2 = 0; + uint32_t last_button3 = 0; + uint32_t last_button4 = 0; + uint32_t last_button5 = 0; + + BLE(void); void begin(void); void end(void); diff --git a/src/BLEGamepad.cpp b/src/BLEGamepad.cpp index 692f63f..1437d39 100644 --- a/src/BLEGamepad.cpp +++ b/src/BLEGamepad.cpp @@ -70,6 +70,43 @@ void BLEGamepad::show(TFT_eSprite *m) { uint16_t right_gimbal_x = map(inputs.roll, 0, 4096, 0, 90); m->fillCircle(right_gimbal_x + 161, right_gimbal_y + 150, 5, TFT_YELLOW); + // Draw the switches + uint32_t last_millis = millis(); + if((uint32_t)(last_millis - last_button1) > 500) { + m->drawRect(0, 40, 60, 60, TFT_BLUE); // Button 1 + } else { + m->drawRect(0, 40, 60, 60, TFT_BLUE); // Button 1 + m->fillRect(1, 41, 60, 60, TFT_BLUE); + } + + if((uint32_t)(last_millis - last_button2) > 500) { + m->drawRect(65, 40, 60, 60, TFT_BLUE); // Button 2 + } else { + m->drawRect(65, 40, 60, 60, TFT_BLUE); // Button 2 + m->fillRect(66, 41, 60, 60, TFT_BLUE); + } + + if((uint32_t)(last_millis - last_button3) > 500) { + m->drawRect(130, 40, 60, 60, TFT_BLUE); // Button 3 + } else { + m->drawRect(130, 40, 60, 60, TFT_BLUE); // Button 3 + m->fillRect(131, 41, 60, 60, TFT_BLUE); + } + + if((uint32_t)(last_millis - last_button4) > 500) { + m->drawRect(195, 40, 60, 60, TFT_BLUE); // Button 4 + } else { + m->drawRect(195, 40, 60, 60, TFT_BLUE); // Button 4 + m->fillRect(196, 41, 60, 60, TFT_BLUE); + } + + if((uint32_t)(last_millis - last_button5) > 500) { + m->drawRect(260, 40, 60, 60, TFT_BLUE); // Button 5 + } else { + m->drawRect(260, 40, 60, 60, TFT_BLUE); // Button 5 + m->fillRect(261, 41, 60, 60, TFT_BLUE); + } + m->setCursor(90, 130); m->print("BLE GAMEPAD"); @@ -94,6 +131,111 @@ void BLEGamepad::show(TFT_eSprite *m) { // Temporary connect from here... if(M5.BtnA.wasPressed()) { } + + // Handling inputs + Event &e = M5.Buttons.event; + coordinate = M5.Touch.getPressPoint(); + + // Making sure not retrigger... + if((uint32_t)(millis() - touch_time) > 100) { + if(e & (E_TOUCH)) { + Serial.printf("E_TOUCH X:%d, Y:%d\r\n", e.to.x, e.to.y); + + // Determine touched positions: + // Left gimbal: + // - Top + // - MinX: 110 + // - MinY: 150 + // - MaxX: 120 + // - MaxY: 180 + // - Bottom + // - MinX: 110 + // - MinY: 200 + // - MaxX: 120 + // - MaxY: 220 + // - Left + // - MinX: 80 + // - MinY: 180 + // - MaxX: 105 + // - MaxY: 210 + // - Right + // - MinX: 110 + // - MinY: 150 + // - MaxX: 160 + // - MaxY: 190 + + if(e.to.x >= 110 && e.to.x <= 120 && e.to.y >= 140 && e.to.y <= 180) { + // Minus throttle + // Serial.printf("Left: TOP\n"); + inputs.trim_throttle-=20; + speak.speak_trim_word = TRIM_THROTTLE_INCREASE; + speak.speak_now_trim = true; + } else if(e.to.x >= 110 && e.to.x <= 170 && e.to.y >= 200 && e.to.y <= 220) { + // Add throttle + // Serial.printf("Left: BOTTOM\n"); + inputs.trim_throttle+=20; + speak.speak_trim_word = TRIM_THROTTLE_DECREASE; + speak.speak_now_trim = true; + } else if(e.to.x >= 60 && e.to.x <= 105 && e.to.y >= 180 && e.to.y <= 210) { + // Minus Yaw + // Serial.printf("Left: LEFT\n"); + inputs.trim_yaw-=20; + speak.speak_trim_word = TRIM_YAW_DECREASE; + speak.speak_now_trim = true; + } else if(e.to.x >= 110 && e.to.x <= 160 && e.to.y >= 150 && e.to.y <= 190) { + // Add Yaw + // Serial.printf("Left: RIGHT\n"); + inputs.trim_yaw+=20; + speak.speak_trim_word = TRIM_YAW_INCREASE; + speak.speak_now_trim = true; + } else if(e.to.x >= 250 && e.to.x <= 230 && e.to.y >= 187 && e.to.y <= 197) { + // Add Row + inputs.trim_roll+=20; + speak.speak_trim_word = TRIM_ROLL_INCREASE; + speak.speak_now_trim = true; + } else if(e.to.x >= 180 && e.to.x <= 175 && e.to.y >= 187 && e.to.y <= 197) { + // Minus Row + inputs.trim_roll+=20; + speak.speak_trim_word = TRIM_ROLL_DECREASE; + speak.speak_now_trim = true; + // TODO: Continue the rest..... + } else if(e.to.x >= 0 && e.to.x <= 60 && e.to.y >= 40 && e.to.y <= 100) { + // Button 1 + ble.last_button1 = millis(); // Setting the time so the system knows how long to trigger + last_button1 = ble.last_button1; // For remembering last press on UI + ble.button1 = true; // Send to BLE let the process to run through + } else if(e.to.x >= 65 && e.to.x <= 125 && e.to.y >= 40 && e.to.y <= 100) { + // Button 2 + ble.last_button2 = millis(); // Setting the time so the system knows how long to trigger + last_button2 = ble.last_button2; // For remembering last press on UI + ble.button2 = true; // Send to BLE let the process to run through + } else if(e.to.x >= 130 && e.to.x <= 190 && e.to.y >= 40 && e.to.y <= 100) { + // Button 3 + ble.last_button3 = millis(); // Setting the time so the system knows how long to trigger + last_button3 = ble.last_button3; // For remembering last press on UI + ble.button3 = true; // Send to BLE let the process to run through + } else if(e.to.x >= 195 && e.to.x <= 255 && e.to.y >= 40 && e.to.y <= 100) { + // Button 4 + ble.last_button4 = millis(); // Setting the time so the system knows how long to trigger + last_button4 = ble.last_button4; // For remembering last press on UI + ble.button4 = true; // Send to BLE let the process to run through + } else if(e.to.x >= 260 && e.to.x <= 310 && e.to.y >= 40 && e.to.y <= 100) { + // Button 5 + ble.last_button5 = millis(); // Setting the time so the system knows how long to trigger + last_button5 = ble.last_button5; // For remembering last press on UI + ble.button5 = true; // Send to BLE let the process to run through + } + touch_time = millis(); + } + } + + /*** + if(e & (E_RELEASE)) { + Serial.printf("E_MOVE X:%d, Y:%d\r\n", e & E_MOVE ? e.from.x : e.to.x, e & E_MOVE ? e.from.y : e.to.y); + } + if(coordinate.x > 0 || coordinate.y > 0) + Serial.printf("x:%d, y:%d \r\n", coordinate.x, coordinate.y); + ***/ } BLEGamepad blegamepad; diff --git a/src/BLEGamepad.h b/src/BLEGamepad.h index ab54bd1..df859d4 100644 --- a/src/BLEGamepad.h +++ b/src/BLEGamepad.h @@ -6,14 +6,53 @@ #include "BLE.h" #include "Inputs.h" #include "Screen.h" +#include "Speak.h" + +enum gesture_axis { + NONE_AXIS, + HORIZONTAL, + VERTICAL +}; + +enum gimbal { + NONE_GIMBAL, + LEFT, + RIGHT +}; + +enum gesture_meanings { + NONE_GESTURE, + INCREASE, + DECREASE +}; + +enum trim_names { + NONE_TRIM, + TRIM_YAW_DECREASE, + TRIM_YAW_INCREASE, + TRIM_THROTTLE_DECREASE, + TRIM_THROTTLE_INCREASE, + TRIM_ROLL_INCREASE, + TRIM_ROLL_DECREASE, + TRIM_PITCH_INCREASE, + TRIM_PITCH_DECREASE +}; class BLEGamepad { public: + TouchPoint_t coordinate; + uint32_t touch_time = 0; bool ble_begin = false; bool updated = false; float voltage = 0.00; float current = 0.00; + uint32_t last_button1 = 0; + uint32_t last_button2 = 0; + uint32_t last_button3 = 0; + uint32_t last_button4 = 0; + uint32_t last_button5 = 0; + BLEGamepad(void); void begin(void); void show(TFT_eSprite *m); diff --git a/src/Inputs.cpp b/src/Inputs.cpp index e04e8fd..124d895 100644 --- a/src/Inputs.cpp +++ b/src/Inputs.cpp @@ -17,6 +17,15 @@ void Inputs::begin(void) { pitch_pool[i] = 0; roll_pool[i] = 0; } + + // Pre-read + read(); + + // Activate silent mode... + if(pitch_raw < 1000) speak.silent = true; + + // Activate low volume mode.... + if(pitch_raw > 3000) speak.low_volume = true; } @@ -26,10 +35,26 @@ void Inputs::read(void) { pitch_raw = analogRead(PITCH_PIN); roll_raw = analogRead(ROLL_PIN); - throttle = pool_insert(THROTTLE, throttle_raw); - yaw = pool_insert(YAW, yaw_raw); - pitch = pool_insert(PITCH, pitch_raw); - roll = pool_insert(ROLL, roll_raw); + if(invert_throttle) { + throttle_raw = 4095 - throttle_raw; + } + + if(invert_yaw) { + yaw_raw = 4095 - yaw_raw; + } + + if(invert_pitch) { + pitch_raw = 4095 - pitch_raw; + } + + if(invert_roll) { + roll_raw = 4095 - roll_raw; + } + + throttle = pool_insert(THROTTLE, throttle_raw) + trim_throttle; + yaw = pool_insert(YAW, yaw_raw) + trim_yaw; + pitch = pool_insert(PITCH, pitch_raw) + trim_pitch; + roll = pool_insert(ROLL, roll_raw) + trim_roll; } diff --git a/src/Inputs.h b/src/Inputs.h index 1861be6..f75ee9f 100644 --- a/src/Inputs.h +++ b/src/Inputs.h @@ -3,6 +3,7 @@ #include #include +#include "Speak.h" #define THROTTLE_PIN 35 #define YAW_PIN 36 @@ -24,11 +25,21 @@ enum input_names { class Inputs { public: + bool invert_throttle = true; + bool invert_yaw = true; + bool invert_pitch = false; + bool invert_roll = false; + uint16_t throttle = 0; uint16_t yaw = 0; uint16_t pitch = 0; uint16_t roll = 0; + int16_t trim_throttle = 0; + int16_t trim_yaw = 0; + int16_t trim_pitch = 0; + int16_t trim_roll = 0; + Inputs(void); void begin(void); void read(void); diff --git a/src/Speak.cpp b/src/Speak.cpp index b915764..fd94906 100644 --- a/src/Speak.cpp +++ b/src/Speak.cpp @@ -17,11 +17,11 @@ void Speak::begin(void) { .mode = (i2s_mode_t)(I2S_MODE_MASTER), // Set the I2S operating mode. 设置I2S工作模式 .sample_rate = 22050, // Set the I2S sampling rate. 设置I2S采样率 .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // Fixed 12-bit stereo MSB. 固定为12位立体声MSB - .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, // Set the channel format. 设置频道格式 + .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, // Set the channel format. 设置频道格式 .communication_format = I2S_COMM_FORMAT_I2S, // Set the format of the communication. 设置通讯格式 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Set the interrupt flag. 设置中断的标志 .dma_buf_count = 2, // DMA buffer count. DMA缓冲区计数 - .dma_buf_len = 1024, // DMA buffer length. DMA缓冲区长度 + .dma_buf_len = 1024, // DMA buffer length. DMA缓冲区长度 }; i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX); i2s_config.use_apll = false; // I2S clock setup. I2S时钟设置 @@ -36,7 +36,7 @@ void Speak::begin(void) { tx_pin_config.ws_io_num = CONFIG_I2S_LRCK_PIN; tx_pin_config.data_out_num = CONFIG_I2S_DATA_PIN; tx_pin_config.data_in_num = CONFIG_I2S_DATA_IN_PIN; - i2s_set_pin(SPEAK_I2S_NUMBER, &tx_pin_config); // Set the I2S pin number. 设置I2S引脚编号 + i2s_set_pin(SPEAK_I2S_NUMBER, &tx_pin_config); // Set the I2S pin number. 设置I2S引脚编号 i2s_set_clk(SPEAK_I2S_NUMBER, 22050, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); // Set the clock and bitwidth used by I2S Rx and Tx. 设置I2S RX、Tx使用的时钟和位宽 } @@ -82,7 +82,31 @@ void Speak::speak_misc(uint8_t phrase) { } + +void Speak::speak_trims(uint8_t phrase) { + if(phrase == TRIM_THROTTLE_INCREASE) { + speak("throttleincrease.raw"); + } else if(phrase == TRIM_THROTTLE_DECREASE) { + speak("throttledecrease.raw"); + } else if(phrase == TRIM_YAW_INCREASE) { + speak("yawincrease.raw"); + } else if(phrase == TRIM_YAW_DECREASE) { + speak("yawdecrease.raw"); + } else if(phrase == TRIM_ROLL_INCREASE) { + speak("rollincrease.raw"); + } else if(phrase == TRIM_ROLL_DECREASE) { + speak("rolldecrease.raw"); + } else if(phrase == TRIM_PITCH_INCREASE) { + speak("pitchincrease.raw"); + } else if(phrase == TRIM_PITCH_DECREASE) { + speak("pitchdecrease.raw"); + } +} + + void Speak::speak(String filename) { + if(silent) return; + size_t bytes_written = 0; filename = "/" + filename; @@ -119,8 +143,13 @@ void Speak::speak(String filename) { data = data_low | data_high << 8; - // Lower sound ?? - data = data * 2.5; + // Either turn up volume or lower it + if(low_volume) { + data = data >> 2; + } else { + data = data * 2.5; + } + samples[samples_count * 2] = data; samples[samples_count * 2 + 1] = data; samples_read++; diff --git a/src/Speak.h b/src/Speak.h index d6f391f..1d057de 100644 --- a/src/Speak.h +++ b/src/Speak.h @@ -27,19 +27,27 @@ enum phrase { class Speak { public: + bool silent = false; + bool low_volume = false; + Speak(void); void begin(void); void dingdong(void); void welcome(void); void speak_menu(uint8_t); void speak_misc(uint8_t); + void speak_trims(uint8_t); void speak(String filename); virtual int16_t process_sample(int16_t sample) { return sample; } // virtual int8_t process_sample(int8_t sample) { return sample; } + bool speak_now = false; bool speak_now_other = false; + bool speak_now_trim = false; + int16_t speak_words = -1; int16_t speak_miscs = -1; + int16_t speak_trim_word = -1; private: }; diff --git a/src/main.cpp b/src/main.cpp index 9c5aad6..d65702a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ void setup() { customwifi.begin(); inputs.begin(); web.begin(); + speak.welcome(); @@ -109,7 +110,13 @@ void taskSpeak(void *pvParameters) { if(speak.speak_now_other == true) { speak.speak_misc(speak.speak_miscs); speak.speak_now_other = false; - } + } + + if(speak.speak_now_trim == true) { + speak.speak_trims(speak.speak_trim_word); + speak.speak_now_trim = false; + } + vTaskDelay(100); } }