diff --git a/Arduino/display/button.ino b/Arduino/display/button.ino index 29a24e3..0941896 100644 --- a/Arduino/display/button.ino +++ b/Arduino/display/button.ino @@ -119,10 +119,14 @@ void update_beep() { } } void button_pressed(int pin) { - button_press_times[pin] = millis(); - button_pressed_states[pin] = true; - // light the LED - pcf.digitalWrite(led_pins[pin], LOW); + if ((autoPilot.getMode() == 0 && (pin == COMPASS_MODE_BUTTON_PIN || (pin == NAVIGATE_MODE_BUTTON_PIN && autoPilot.isWaypointSet() && autoPilot.hasFix()))) || + ((autoPilot.getMode() != 0 && (pin == PORT_ADJUST_BUTTON_PIN || pin == STARBORD_ADJUST_BUTTON_PIN || + pin == COMPASS_MODE_BUTTON_PIN || (pin == NAVIGATE_MODE_BUTTON_PIN && autoPilot.isWaypointSet() && autoPilot.hasFix()))))) { + button_press_times[pin] = millis(); + button_pressed_states[pin] = true; + // light the LED + pcf.digitalWrite(led_pins[pin], LOW); + } } void button_release(int pin) { @@ -145,7 +149,10 @@ void button_release(int pin) { DEBUG_PRINTLN(" ms"); if (press_duration < 25) { - if (autoPilot.getMode() != 0 || (pin != PORT_ADJUST_BUTTON_PIN && pin != STARBORD_ADJUST_BUTTON_PIN)) { + // if (autoPilot.getMode() != 0 || (pin != PORT_ADJUST_BUTTON_PIN && pin != STARBORD_ADJUST_BUTTON_PIN)) { + if ((autoPilot.getMode() == 0 && (pin == COMPASS_MODE_BUTTON_PIN || (pin == NAVIGATE_MODE_BUTTON_PIN && autoPilot.isWaypointSet() && autoPilot.hasFix()))) || + ((autoPilot.getMode() > 0 && (pin == PORT_ADJUST_BUTTON_PIN || pin == STARBORD_ADJUST_BUTTON_PIN || + pin == COMPASS_MODE_BUTTON_PIN || (pin == NAVIGATE_MODE_BUTTON_PIN && autoPilot.isWaypointSet() && autoPilot.hasFix()))))) { DEBUG_PRINT("Button "); DEBUG_PRINT(pin); DEBUG_PRINTLN(" Clicked"); @@ -168,15 +175,27 @@ void button_release(int pin) { switch (pin) { case PORT_ADJUST_BUTTON_PIN: - adjustment *= -1.0; - adjust_heading(adjustment); - DEBUG_PRINT("Port Adjust "); - DEBUG_PRINTLN(adjustment); + if (autoPilot.getMode() > 0) { + if (autoPilot.getMode() == 2) { + set_mode(1); + delay(1000); + } + adjustment *= -1.0; + adjust_heading(adjustment); + DEBUG_PRINT("Port Adjust "); + DEBUG_PRINTLN(adjustment); + } break; case STARBORD_ADJUST_BUTTON_PIN: - adjust_heading(adjustment); - DEBUG_PRINT("Port Adjust "); - DEBUG_PRINTLN(adjustment); + if (autoPilot.getMode() > 0) { + if (autoPilot.getMode() == 2) { + set_mode(1); + delay(1000); + } + adjust_heading(adjustment); + DEBUG_PRINT("Port Adjust "); + DEBUG_PRINTLN(adjustment); + } break; case COMPASS_MODE_BUTTON_PIN: if (autoPilot.getMode() != 1) { diff --git a/Arduino/display/command.ino b/Arduino/display/command.ino index 2d35995..cbedb88 100644 --- a/Arduino/display/command.ino +++ b/Arduino/display/command.ino @@ -1,20 +1,45 @@ #if defined(ARDUINO_ARCH_SAMD) // Check if the board is based on the SAMD architecture (like Arduino Nano 33 IoT) - #include +#include #elif defined(ARDUINO_ARCH_ESP32) // Check if the board is based on the ESP32 architecture (like Arduino Nano ESP32) - #include - #include +#include +#include #else - #error "Unsupported board type. Please use Arduino Nano 33 IoT or Arduino Nano ESP32." +#error "Unsupported board type. Please use Arduino Nano 33 IoT or Arduino Nano ESP32." #endif //#define MOCK_SEND false -#define TIMEOUT_MS 1000 // Set your desired timeout value in milliseconds +#define COMMAND_LEN 12 +#define TIMEOUT_MS 1000 // Set your desired timeout value in milliseconds #define COMMAND_PORT 8023 +#define COMMAND_DELAY 200 IPAddress processor_ip(10, 20, 1, 2); WiFiClient client; -void setup_command() { +char command[COMMAND_LEN]; + +boolean connect() { + if (client.connected()) { + client.stop(); + } + if (!client.connect(processor_ip, COMMAND_PORT)) { + Serial.println("Could not connect to processor."); + return false; + } + return true; +} + +void send_command(const char* command) { + if (connect()) { + client.write('\0'); + client.print(command); + client.write('\n'); + client.flush(); + delay(COMMAND_DELAY); + } +} + +void setup_command() { // Set the timeout for WiFiClient client.setTimeout(TIMEOUT_MS); } @@ -26,48 +51,24 @@ void check_command() { } void adjust_heading(float change) { -#ifndef MOCK_SEND - if (connect()) { - client.print("a"); - client.println(change); - client.flush(); - DEBUG_PRINT("adjusting heading "); - DEBUG_PRINTLN(change); - disconnect(); - } else { - DEBUG_PRINTLN("Could not adjust heading."); - } +#ifndef MOCK_SEND + command[0] = '/0'; + sprintf(command, "a%.2f", change); + send_command(command); + DEBUG_PRINT("adjusting heading "); + DEBUG_PRINTLN(change); #endif } void set_mode(int mode) { -#ifndef MOCK_SEND - if (connect()) { - client.print("m"); - client.println(mode); - client.flush(); - DEBUG_PRINT("set mode "); - DEBUG_PRINTLN(mode); - disconnect(); - } else { - DEBUG_PRINTLN("Could not set mode."); - } -#endif -} +#ifndef MOCK_SEND + command[0] = '/0'; + sprintf(command, "m%d", mode); + send_command(command); + DEBUG_PRINT("set mode "); + DEBUG_PRINTLN(mode); -boolean connect() { - if (!client.connected()) { - if (!client.connect(processor_ip, COMMAND_PORT)) { - DEBUG_PRINTLN("Could not connect to processor."); - return false; - } - } - return true; +#endif } -boolean disconnect() { - if (client.connected()) { - client.println("q"); - client.stop(); - } -} \ No newline at end of file + diff --git a/Arduino/display/screen.ino b/Arduino/display/screen.ino index 7c41d4c..f1f8e2d 100644 --- a/Arduino/display/screen.ino +++ b/Arduino/display/screen.ino @@ -377,7 +377,7 @@ void display_distance() { distance_value_canvas.setTextColor(HX8357_CYAN); distance_value_canvas.setFont(&FreeSansBold24pt7b); distance_value_canvas.setCursor(0, 37); - if (autoPilot.isWaypointSet()) { + if (autoPilot.isWaypointSet() && autoPilot.hasFix()) { distance_value_canvas.print(autoPilot.getDistance(), 2); } else { distance_value_canvas.print("");