Skip to content

Commit

Permalink
fixing the dislay to work with a controller using an ESP32 rather tha…
Browse files Browse the repository at this point in the history
…n a 33 IoT where we have a real telnet client
  • Loading branch information
romanini committed Jul 16, 2024
1 parent 017edf8 commit 3047567
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 57 deletions.
43 changes: 31 additions & 12 deletions Arduino/display/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -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) {
Expand Down
89 changes: 45 additions & 44 deletions Arduino/display/command.ino
Original file line number Diff line number Diff line change
@@ -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 <WiFiNINA.h>
#include <WiFiNINA.h>
#elif defined(ARDUINO_ARCH_ESP32) // Check if the board is based on the ESP32 architecture (like Arduino Nano ESP32)
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#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);
}
Expand All @@ -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();
}
}

2 changes: 1 addition & 1 deletion Arduino/display/screen.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down

0 comments on commit 3047567

Please sign in to comment.