-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// TODO: Be able to read bytes from server in background/task | ||
// so there is no loss of data when inputing | ||
#include "modules/wifi/tcp_utils.h" | ||
#include "core/wifi_common.h" | ||
|
||
bool inputMode; | ||
|
||
void listenTcpPort() { | ||
if (!wifiConnected) wifiConnectMenu(); | ||
|
||
WiFiClient tcpClient; | ||
tft.fillScreen(TFT_BLACK); | ||
tft.setTextSize(1); | ||
tft.setTextColor(TFT_WHITE, TFT_BLACK); | ||
|
||
String portNumber = keyboard("", 5, "TCP port to listen"); | ||
int portNumberInt = atoi(portNumber.c_str()); | ||
|
||
WiFiServer server(portNumberInt); | ||
server.begin(); | ||
|
||
tft.println("Listening..."); | ||
tft.print(WiFi.localIP().toString().c_str()); | ||
tft.println(":" + portNumber); | ||
|
||
|
||
for (;;) { | ||
WiFiClient client = server.available(); // Wait for a client to connect | ||
|
||
if (client) { | ||
Serial.println("Client connected"); | ||
tft.println("Client connected"); | ||
|
||
while (client.connected()) { | ||
if (inputMode) { | ||
String keyString = keyboard("", 16, "send input data"); | ||
delay(300); | ||
inputMode = false; | ||
tft.fillScreen(TFT_BLACK); | ||
tft.setCursor(0,0); | ||
if (keyString.length() > 0) { | ||
client.print(keyString); // Send the entire string to the client | ||
Serial.print(keyString); | ||
} | ||
} else { | ||
if (client.available()) { | ||
char incomingChar = client.read(); // Read one byte at time from the client | ||
tft.print(incomingChar); | ||
Serial.print(incomingChar); | ||
} | ||
if (check(SelPress)) { | ||
delay(300); | ||
inputMode = true; | ||
} | ||
} | ||
} | ||
client.stop(); | ||
Serial.println("Client disconnected"); | ||
displayError("Client disconnected"); | ||
|
||
} | ||
if (check(EscPress)) { | ||
displayError("Exiting Listener"); | ||
server.stop(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void clientTCP() { | ||
if (!wifiConnected) wifiConnectMenu(); | ||
|
||
String serverIP = keyboard("", 15, "Enter server IP"); | ||
String portString = keyboard("", 5, "Enter server Port"); | ||
int portNumber = atoi(portString.c_str()); | ||
|
||
if (serverIP.length() == 0 || portNumber == 0) { | ||
displayError("Invalid IP or Port"); | ||
return; | ||
} | ||
|
||
WiFiClient client; | ||
if (!client.connect(serverIP.c_str(), portNumber)) { | ||
displayError("Connection failed"); | ||
return; | ||
} | ||
|
||
tft.fillScreen(TFT_BLACK); | ||
tft.setTextSize(1); | ||
tft.setTextColor(TFT_WHITE, TFT_BLACK); | ||
tft.println("Connected to:"); | ||
tft.println(serverIP + ":" + portString); | ||
Serial.println("Connected to server"); | ||
|
||
while (client.connected()) { | ||
if (inputMode) { | ||
String keyString = keyboard("", 16, "send input data"); | ||
inputMode = false; | ||
tft.fillScreen(TFT_BLACK); | ||
tft.setCursor(0,0); | ||
delay(300); | ||
if (keyString.length() > 0) { | ||
client.print(keyString); | ||
Serial.print(keyString); | ||
} | ||
} else { | ||
if (client.available()) { | ||
char incomingChar = client.read(); | ||
tft.print(incomingChar); | ||
Serial.print(incomingChar); | ||
} | ||
if (check(SelPress)) { | ||
delay(300); | ||
inputMode = true; | ||
} | ||
} | ||
if (check(EscPress)) { | ||
displayError("Exiting Client"); | ||
client.stop(); | ||
break; | ||
} | ||
} | ||
|
||
displayError("Connection closed."); | ||
Serial.println("Connection closed."); | ||
client.stop(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include "core/mykeyboard.h" | ||
|
||
void listenTcpPort(); | ||
void clientTCP(); |