Browse Source

Added more buttons for BLE gamepad and with throttle and yaw trim. Todo: add pitch and roll trim and save the settings.

master
Englebert 3 years ago
parent
commit
c4527def29
  1. 4
      README.md
  2. BIN
      sounds/pitchdecrease.mp3
  3. BIN
      sounds/pitchdecrease.raw
  4. BIN
      sounds/pitchincrease.mp3
  5. BIN
      sounds/pitchincrease.raw
  6. BIN
      sounds/rolldecrease.mp3
  7. BIN
      sounds/rolldecrease.raw
  8. BIN
      sounds/rollincrease.mp3
  9. BIN
      sounds/rollincrease.raw
  10. BIN
      sounds/throttledecrease.mp3
  11. BIN
      sounds/throttledecrease.raw
  12. BIN
      sounds/throttleincrease.mp3
  13. BIN
      sounds/throttleincrease.raw
  14. BIN
      sounds/yawdecrease.mp3
  15. BIN
      sounds/yawdecrease.raw
  16. BIN
      sounds/yawincrease.mp3
  17. BIN
      sounds/yawincrease.raw
  18. 71
      src/BLE.cpp
  19. 17
      src/BLE.h
  20. 142
      src/BLEGamepad.cpp
  21. 39
      src/BLEGamepad.h
  22. 33
      src/Inputs.cpp
  23. 11
      src/Inputs.h
  24. 39
      src/Speak.cpp
  25. 8
      src/Speak.h
  26. 9
      src/main.cpp

4
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 ###

BIN
sounds/pitchdecrease.mp3

BIN
sounds/pitchdecrease.raw

BIN
sounds/pitchincrease.mp3

BIN
sounds/pitchincrease.raw

BIN
sounds/rolldecrease.mp3

BIN
sounds/rolldecrease.raw

BIN
sounds/rollincrease.mp3

BIN
sounds/rollincrease.raw

BIN
sounds/throttledecrease.mp3

BIN
sounds/throttledecrease.raw

BIN
sounds/throttleincrease.mp3

BIN
sounds/throttleincrease.raw

BIN
sounds/yawdecrease.mp3

BIN
sounds/yawdecrease.raw

BIN
sounds/yawincrease.mp3

BIN
sounds/yawincrease.raw

71
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();

17
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);

142
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;

39
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);

33
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;
}

11
src/Inputs.h

@ -3,6 +3,7 @@
#include <Arduino.h>
#include <M5Core2.h>
#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);

39
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++;

8
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:
};

9
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);
}
}

Loading…
Cancel
Save