Browse Source

Added multiple settings and storing functions

master
Englebert 3 years ago
parent
commit
cc280ff8ee
  1. 39
      src/Inputs.cpp
  2. 1
      src/Inputs.h
  3. 217
      src/RCController.cpp
  4. 9
      src/RCController.h
  5. 2
      src/Storage.cpp
  6. 2
      src/Storage.h
  7. 70
      src/Web.cpp
  8. 2
      src/Web.h

39
src/Inputs.cpp

@ -18,6 +18,42 @@ void Inputs::begin(void) {
roll_pool[i] = 0;
}
// Reading trims
String trim_str = "";
String filename = "";
filename = "/TRIM_THROTTLE";
if(!storage.exists(filename)) {
storage.writeFile(LITTLEFS, filename, String(trim_throttle));
} else {
trim_str = storage.readFile(LITTLEFS, filename);
trim_throttle = trim_str.toInt();
}
filename = "/TRIM_YAW";
if(!storage.exists(filename)) {
storage.writeFile(LITTLEFS, filename, String(trim_yaw));
} else {
trim_str = storage.readFile(LITTLEFS, filename);
trim_yaw = trim_str.toInt();
}
filename = "/TRIM_PITCH";
if(!storage.exists(filename)) {
storage.writeFile(LITTLEFS, filename, String(trim_pitch));
} else {
trim_str = storage.readFile(LITTLEFS, filename);
trim_pitch = trim_str.toInt();
}
filename = "/TRIM_ROLL";
if(!storage.exists(filename)) {
storage.writeFile(LITTLEFS, filename, String(trim_roll));
} else {
trim_str = storage.readFile(LITTLEFS, filename);
trim_roll = trim_str.toInt();
}
// Pre-read
read();
@ -26,9 +62,6 @@ void Inputs::begin(void) {
// Activate low volume mode....
if(pitch_raw > 3000) speak.low_volume = true;
// Temporary silent....
speak.silent = true;
}

1
src/Inputs.h

@ -4,6 +4,7 @@
#include <Arduino.h>
#include <M5Core2.h>
#include "Speak.h"
#include "Storage.h"
#define THROTTLE_PIN 35
#define YAW_PIN 36

217
src/RCController.cpp

@ -26,10 +26,12 @@ 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);
@ -50,6 +52,26 @@ void RCController::draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y) {
}
void RCController::draw_gimbal_huge(TFT_eSprite *m, uint16_t x, uint16_t y) {
// Top
m->drawFastHLine(x, y, 120, TFT_DARKGREEN);
// Bottom
m->drawFastHLine(x, y + 119, 120, TFT_DARKGREEN);
// Left
m->drawFastVLine(x, y, 120, TFT_DARKGREEN);
// Right
m->drawFastVLine(x + 119, y, 120, TFT_DARKGREEN);
// Middle dotted lines
for(uint16_t x1 = x; x1 < x + 120; x1 += 10) {
m->drawFastHLine(x1, y + 60, 5, TFT_DARKGREEN);
}
for(uint16_t y1 = y; y1 < y + 120; y1 += 10) {
m->drawFastVLine(x + 60, y1, 5, TFT_DARKGREEN);
}
}
void RCController::update(void) {
if(!nrf_begin) {
return;
@ -100,10 +122,33 @@ void RCController::show(TFT_eSprite *m) {
tx.begin();
tx.setAutoAck(false);
tx.setPayloadSize(sizeof(TxMessage));
tx.setChannel(8); ///// Temporary hard code first
// Reading from File...
String channel_str = "";
String filename = "/NRF_CHANNEL";
if(!storage.exists(filename)) {
// Default to channel 8. Lucky number?
channel = 8;
storage.writeFile(LITTLEFS, filename, String(channel));
} else {
channel_str = storage.readFile(LITTLEFS, filename);
channel = channel_str.toInt();
}
tx.setChannel(channel); // Load from settings.
tx.setDataRate(RF24_250KBPS); // Lowest.. at 250kbps
switch(txpower) { // Set the transmit power
String txpower_str = "";
filename = "/TXPOWER";
if(!storage.exists(filename)) {
storage.writeFile(LITTLEFS, filename, String(txpower));
} else {
txpower_str = storage.readFile(LITTLEFS, filename);
txpower = txpower_str.toInt();
}
switch(txpower) { // Set the transmit power
case 0:
tx.setPALevel(RF24_PA_MIN);
break;
@ -154,18 +199,52 @@ void RCController::show(TFT_eSprite *m) {
// Draw the gimbal box
if(rc_screens == RC_SCREEN_GIMBAL) {
// Showing the settings
draw_gimbal(m, 69, 150);
draw_gimbal(m, 161, 150);
// draw_gimbal(m, 69, 150);
// draw_gimbal(m, 161, 150);
draw_gimbal_huge(m, 39, 60);
draw_gimbal_huge(m, 161, 60);
// 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, stickcalibration.throttle_min, stickcalibration.throttle_max, 0, 90);
uint16_t left_gimbal_x = map(inputs.yaw, stickcalibration.yaw_min, stickcalibration.yaw_max, 0, 90);
m->fillCircle(left_gimbal_x + 69, left_gimbal_y + 150, 5, TFT_YELLOW);
uint16_t right_gimbal_y = map(inputs.pitch, stickcalibration.pitch_min, stickcalibration.pitch_max, 0, 90);
uint16_t right_gimbal_x = map(inputs.roll, stickcalibration.roll_min, stickcalibration.roll_max, 0, 90);
m->fillCircle(right_gimbal_x + 161, right_gimbal_y + 150, 5, TFT_YELLOW);
uint16_t left_gimbal_y = map(inputs.throttle, stickcalibration.throttle_min, stickcalibration.throttle_max, 0, 120);
uint16_t left_gimbal_x = map(inputs.yaw, stickcalibration.yaw_min, stickcalibration.yaw_max, 0, 120);
// Invert it
left_gimbal_y = 120-left_gimbal_y;
left_gimbal_x = 120-left_gimbal_x;
m->fillCircle(left_gimbal_x + 39, left_gimbal_y + 60, 5, TFT_YELLOW);
uint16_t right_gimbal_y = map(inputs.pitch, stickcalibration.pitch_min, stickcalibration.pitch_max, 0, 120);
uint16_t right_gimbal_x = map(inputs.roll, stickcalibration.roll_min, stickcalibration.roll_max, 0, 120);
m->fillCircle(right_gimbal_x + 161, right_gimbal_y + 60, 5, TFT_YELLOW);
} if(rc_screens == RC_SCREEN_RADIOSETTINGS) {
// Display and set the frequency channel and power level
m->setCursor(0, 40);
m->print("Channel: ");
m->print(String(channel));
m->setCursor(0, 90);
m->print("TX Power: ");
switch(txpower) {
case 0:
m->print("MIN");
break;
case 1:
m->print("LOW");
break;
case 2:
m->print("HIGH");
break;
case 3:
m->print("MAX");
break;
}
m->drawRect(220, 40, 40, 40, TFT_BLUE); // Channel Increase
m->drawRect(280, 40, 40, 40, TFT_BLUE); // Channel Decrease
m->drawRect(220, 90, 40, 40, TFT_BLUE); // POWER Increase
m->drawRect(280, 90, 40, 40, TFT_BLUE); // POWER Decrease
} if(rc_screens == RC_SCREEN_MAIN) {
@ -173,9 +252,9 @@ void RCController::show(TFT_eSprite *m) {
uint32_t last_millis = millis();
if((uint32_t)(last_millis - last_button1) > 50) {
if(!button1) {
m->drawRect(0, 40, 60, 125, TFT_BLUE); // Button 1
m->drawRect(0, 40, 60, 125, TFT_BLUE); // Button 1
} else {
m->drawRect(0, 40, 60, 125, TFT_BLUE); // Button 1
m->drawRect(0, 40, 60, 125, TFT_BLUE); // Button 1
m->fillRect(1, 41, 58, 123, TFT_BLUE);
}
}
@ -314,41 +393,125 @@ void RCController::show(TFT_eSprite *m) {
// - MaxX: 160
// - MaxY: 190
if(rc_screens == RC_SCREEN_GIMBAL) {
if(e.to.x >= 110 && e.to.x <= 120 && e.to.y >= 140 && e.to.y <= 180) {
if(e.to.x >= 60 && e.to.x <= 140 && e.to.y >= 60 && e.to.y <= 110) {
// Minus throttle
// Serial.printf("Left: TOP\n");
inputs.trim_throttle-=20;
inputs.trim_throttle+=10;
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) {
storage.writeFile(LITTLEFS, "/TRIM_THROTTLE", String(inputs.trim_throttle));
} else if(e.to.x >= 60 && e.to.x <= 140 && e.to.y >= 130 && e.to.y <= 180) {
// Add throttle
// Serial.printf("Left: BOTTOM\n");
inputs.trim_throttle+=20;
inputs.trim_throttle-=10;
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) {
storage.writeFile(LITTLEFS, "/TRIM_THROTTLE", String(inputs.trim_throttle));
} else if(e.to.x >= 40 && e.to.x <= 90 && e.to.y >= 80 && e.to.y <= 160) {
// Minus Yaw
// Serial.printf("Left: LEFT\n");
inputs.trim_yaw-=20;
inputs.trim_yaw+=10;
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) {
storage.writeFile(LITTLEFS, "/TRIM_YAW", String(inputs.trim_yaw));
} else if(e.to.x >= 110 && e.to.x <= 160 && e.to.y >= 80 && e.to.y <= 160) {
// Add Yaw
// Serial.printf("Left: RIGHT\n");
inputs.trim_yaw+=20;
inputs.trim_yaw-=10;
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;
storage.writeFile(LITTLEFS, "/TRIM_YAW", String(inputs.trim_yaw));
} else if(e.to.x >= 230 && e.to.x <= 280 && e.to.y >= 80 && e.to.y <= 160) {
// Add Roll
inputs.trim_roll+=10;
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;
storage.writeFile(LITTLEFS, "/TRIM_ROLL", String(inputs.trim_roll));
} else if(e.to.x >= 160 && e.to.x <= 210 && e.to.y >= 80 && e.to.y <= 160) {
// Minus Roll
inputs.trim_roll-=10;
speak.speak_trim_word = TRIM_ROLL_DECREASE;
speak.speak_now_trim = true;
storage.writeFile(LITTLEFS, "/TRIM_ROLL", String(inputs.trim_roll));
} else if(e.to.x >= 180 && e.to.x <= 260 && e.to.y >= 60 && e.to.y <= 110) {
// Add Pitch
inputs.trim_pitch-=10;
speak.speak_trim_word = TRIM_PITCH_INCREASE;
speak.speak_now_trim = true;
storage.writeFile(LITTLEFS, "/TRIM_PITCH", String(inputs.trim_pitch));
} else if(e.to.x >= 180 && e.to.x <= 260 && e.to.y >= 130 && e.to.y <= 180) {
// Minus Pitch
inputs.trim_pitch+=10;
speak.speak_trim_word = TRIM_PITCH_DECREASE;
speak.speak_now_trim = true;
storage.writeFile(LITTLEFS, "/TRIM_PITCH", String(inputs.trim_pitch));
}
} else if(rc_screens == RC_SCREEN_RADIOSETTINGS) {
if(e.to.x >= 220 && e.to.x <= 260 && e.to.y >= 40 && e.to.y <= 80) {
channel++;
if(channel > 125) channel = 125;
String filename = "/NRF_CHANNEL";
storage.writeFile(LITTLEFS, filename, String(channel));
tx.setChannel(channel);
} else if(e.to.x >= 280 && e.to.x <= 320 && e.to.y >= 40 && e.to.y <= 80) {
channel--;
if(channel == 0) channel++;
String filename = "/NRF_CHANNEL";
storage.writeFile(LITTLEFS, filename, String(channel));
tx.setChannel(channel);
} else if(e.to.x >= 220 && e.to.x <= 260 && e.to.y >= 90 && e.to.y <= 130) {
txpower++;
if(txpower == 4) {
txpower = 3;
} else {
String filename = "/TXPOWER";
storage.writeFile(LITTLEFS, filename, String(txpower));
switch(txpower) { // Set the transmit power
case 0:
tx.setPALevel(RF24_PA_MIN);
break;
case 1:
tx.setPALevel(RF24_PA_LOW);
break;
case 2:
tx.setPALevel(RF24_PA_HIGH);
break;
case 3:
tx.setPALevel(RF24_PA_MAX);
break;
}
}
} else if(e.to.x >= 280 && e.to.x <= 320 && e.to.y >= 90 && e.to.y <= 130) {
if(txpower != 0) {
txpower--;
String filename = "/TXPOWER";
storage.writeFile(LITTLEFS, filename, String(txpower));
switch(txpower) { // Set the transmit power
case 0:
tx.setPALevel(RF24_PA_MIN);
break;
case 1:
tx.setPALevel(RF24_PA_LOW);
break;
case 2:
tx.setPALevel(RF24_PA_HIGH);
break;
case 3:
tx.setPALevel(RF24_PA_MAX);
break;
}
}
}
} else if(rc_screens == RC_SCREEN_MAIN) {
if(e.to.x >= 0 && e.to.x <= 60 && e.to.y >= 40 && e.to.y <= 165) {
// Button 1

9
src/RCController.h

@ -13,6 +13,7 @@
enum rc_controller_screens {
RC_SCREEN_MAIN = 0,
RC_SCREEN_GIMBAL,
RC_SCREEN_RADIOSETTINGS,
RC_SCREEN_MAX
};
@ -30,6 +31,8 @@ class RCController {
uint16_t nrf_profiling_raw = 0;
uint16_t nrf_profiling = 0;
uint8_t channel = 0;
uint8_t txpower = 0;
uint32_t last_button1 = 0;
@ -42,6 +45,11 @@ class RCController {
uint32_t last_button8 = 0;
uint32_t last_button9 = 0;
uint32_t last_channel_up = 0;
uint32_t last_channel_down= 0;
uint32_t last_power_up = 0;
uint32_t last_power_down = 0;
bool show_settings = false;
uint8_t rc_screens = RC_SCREEN_MAIN;
@ -59,6 +67,7 @@ class RCController {
void begin(void);
void show(TFT_eSprite *m);
void draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y);
void draw_gimbal_huge(TFT_eSprite *m, uint16_t x, uint16_t y);
void update(void); // Sending NRF signals
private:

2
src/Storage.cpp

@ -17,7 +17,7 @@ bool Storage::format(void) {
}
bool Storage::exists(const char * path) {
bool Storage::exists(String path) {
if(LITTLEFS.exists(path)) {
return true;
} else {

2
src/Storage.h

@ -32,7 +32,7 @@ class Storage {
void read_block(void * data, const char * path, uint32_t len);
bool format(void);
bool exists(const char * path);
bool exists(String path);
bool load_defaults = false; // Default false. Only during reset this will auto set to true
private:

70
src/Web.cpp

@ -4,6 +4,7 @@ WebServer server(PORT_HTTP);
bool update_status = false;
bool opened = false;
File root;
String header_html_str = "<html><meta charset=\"UTF-8\"><title>M5Core2TX</title><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><body><pre>";
WEB::WEB(void){
}
@ -21,6 +22,75 @@ void WEB::begin(void) {
server.sendHeader("Connection", "close");
});
// Multiple configuration
server.on("/set", HTTP_GET, []() {
bool update_channel = false;
uint8_t channel_raw = 0;
bool update_sbus_type = false;
bool sbus_slow = false;
String msg = "";
// Server parameters
for(uint8_t i = 0; i < server.args(); i++) {
String value = server.arg(i);
if(server.argName(i) == "channel") {
channel_raw = value.toInt();
update_channel = true;
} else if(server.argName(i) == "sbus") {
sbus_slow = true;
update_sbus_type = true;
} else if(server.argName(i) == "sbusfast") {
sbus_slow = false;
update_sbus_type = true;
}
}
if(update_channel) {
// Making sure is channel 1 ~ 125.
if(channel_raw > 0 && channel_raw < 126) {
String filename = "/NRF_CHANNEL";
msg = "CHANNEL SET: " + String(channel_raw);
storage.writeFile(LITTLEFS, filename, String(channel_raw));
//// File file = LittleFS.open(filename, "w+");
//// file.println(channel_raw);
//// file.close();
rccontroller.channel = channel_raw;
tx.setChannel(channel_raw); // Instant Change
} else {
msg = "ERROR UPDATE CHANNEL";
}
}
String index_html_str = header_html_str + msg;
server.sendHeader("Connection", "close");
server.send(200, "text/html", index_html_str);
});
// Getting configuration
server.on("/get", HTTP_GET, []() {
bool update_channel = false;
uint8_t channel_raw = 0;
String msg = "";
// Server parameters
for(uint8_t i = 0; i < server.args(); i++) {
String value = server.arg(i);
if(server.argName(i) == "channel") {
String filename = "/NRF_CHANNEL";
msg = storage.readFile(LITTLEFS, filename);
msg = "Channel (From FILE): " + msg;
}
}
String index_html_str = header_html_str + msg;
server.sendHeader("Connection", "close");
server.send(200, "text/html", index_html_str);
});
// Update
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");

2
src/Web.h

@ -5,6 +5,8 @@
#include <LITTLEFS.h>
#include <WebServer.h>
#include <Update.h>
#include "nrf24l01.h"
#include "RCController.h"
#include "Storage.h"
#define PORT_HTTP 80

Loading…
Cancel
Save