Browse Source

Added RC Controller with NRF drivers. Not yet running yet

master
Englebert 3 years ago
parent
commit
40865c99a8
  1. 4
      src/BLE.h
  2. 237
      src/RCController.cpp
  3. 36
      src/RCController.h
  4. 4
      src/Screen.cpp
  5. 4
      src/Screen.h
  6. 1318
      src/nrf24l01.cpp
  7. 1360
      src/nrf24l01.h

4
src/BLE.h

@ -8,9 +8,9 @@
class BLE {
public:
bool ble_begin = false;
bool nrf_begin = false;
bool updated = false;
bool ble_started = false;
bool nrf_started = false;
int16_t sent_counter_raw = 0;
int16_t sent_counter = 0;

237
src/RCController.cpp

@ -0,0 +1,237 @@
#include "RCController.h"
RCController::RCController(void) {
updated = true;
}
void RCController::begin(void) {
updated = true;
}
void RCController::draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y) {
// Top
m->drawFastHLine(x, y, 90, TFT_DARKGREEN);
// Bottom
m->drawFastHLine(x, y + 89, 90, TFT_DARKGREEN);
// Left
m->drawFastVLine(x, y, 90, TFT_DARKGREEN);
// Right
m->drawFastVLine(x + 89, y, 90, TFT_DARKGREEN);
// Middle dotted lines
for(uint16_t x1 = x; x1 < x + 90; x1 += 10) {
m->drawFastHLine(x1, y + 45, 5, TFT_DARKGREEN);
}
for(uint16_t y1 = y; y1 < y + 90; y1 += 10) {
m->drawFastVLine(x + 45, y1, 5, TFT_DARKGREEN);
}
}
void RCController::show(TFT_eSprite *m) {
if(!nrf_begin) {
nrf_begin = true;
////////////////////ble.begin();
}
if((uint32_t) (millis() - last_updated) > 50) {
updated = true;
}
if((uint32_t) (millis() - last_voltage_read) > 500) {
voltage = M5.Axp.GetBatVoltage();
current = M5.Axp.GetBatCurrent();
last_voltage_read = millis();
}
if(updated) {
m->fillRect(0, 0, 320, 240, BLACK);
m->setTextColor(WHITE, BLACK);
m->setTextSize(2);
m->setCursor(0, 0);
m->print("Bat: ");
m->print(String(voltage) + "V " + String(current) + "mA");
// Draw the gimbal box
draw_gimbal(m, 69, 150);
draw_gimbal(m, 161, 150);
// Show the position of the left and right gimbal
// Convert the throttle and raw to x and y
// Temporary assume 4096 = max
uint16_t left_gimbal_y = map(inputs.throttle, 0, 4096, 0, 90);
uint16_t left_gimbal_x = map(inputs.yaw, 0, 4096, 0, 90);
m->fillCircle(left_gimbal_x + 69, left_gimbal_y + 150, 5, TFT_YELLOW);
uint16_t right_gimbal_y = map(inputs.pitch, 0, 4096, 0, 90);
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("M5CORE2TX");
m->pushSprite(0,0);
updated = false;
last_updated = millis();
}
// Key Input Handler
if(M5.BtnC.wasPressed()) {
if(ble_begin) {
nrf_begin = false;
//////// ble.end();
}
screen.current_screen = SCREEN_MENU;
screen.updated = true;
}
// 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 = millis();; // 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 = millis(); // 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 = millis(); // 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 = millis(); // 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 = millis(); // 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);
***/
}
RCController rccontroller;

36
src/RCController.h

@ -0,0 +1,36 @@
#ifndef _RCCONTROLLER_H
#define _RCCONTROLLER_H
#include <Arduino.h>
#include <M5Core2.h>
#include "Inputs.h"
#include "Screen.h"
#include "Speak.h"
class RCController {
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;
RCController(void);
void begin(void);
void show(TFT_eSprite *m);
void draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y);
private:
uint32_t last_updated = 0;
uint32_t last_voltage_read = 0;
};
extern RCController rccontroller;
#endif

4
src/Screen.cpp

@ -45,6 +45,8 @@ void Screen::update(void) {
stickcalibration.mydebug(&mainscreen_buffer);
} else if(current_screen == SCREEN_MENU_STICK_POSITIONS) {
stickpositions.main(&mainscreen_buffer);
} else if(current_screen == SCREEN_MENU_RC_CONTROLLER) {
rccontroller.show(&mainscreen_buffer);
}
last_screen_update = millis();
@ -185,6 +187,8 @@ void Screen::menu(void) {
stickcalibration.updated = true;
} else if(current_menu == MENU_STICK_POSITIONS) {
current_screen = SCREEN_MENU_STICK_POSITIONS;
} else if(current_menu == MENU_RC_CONTROLLER) {
current_screen = SCREEN_MENU_RC_CONTROLLER;
}
updated = true;
}

4
src/Screen.h

@ -4,6 +4,7 @@
#include <Arduino.h>
#include <M5Core2.h>
#include "BLEGamepad.h"
#include "RCController.h"
#include "Speak.h"
#include "StickCalibration.h"
#include "StickPositions.h"
@ -24,7 +25,8 @@ enum screen_names {
SCREEN_MENU_POWEROFF,
SCREEN_MENU_BLUETOOTH,
SCREEN_MENU_STICK_CALIBRATION,
SCREEN_MENU_STICK_POSITIONS
SCREEN_MENU_STICK_POSITIONS,
SCREEN_MENU_RC_CONTROLLER
};
enum menu_items {

1318
src/nrf24l01.cpp
File diff suppressed because it is too large
View File

1360
src/nrf24l01.h
File diff suppressed because it is too large
View File

Loading…
Cancel
Save