Skip to content

Commit ddbc8c2

Browse files
committed
p2p upd
1 parent 91bc8a5 commit ddbc8c2

File tree

2 files changed

+150
-46
lines changed

2 files changed

+150
-46
lines changed

TurtleBook/TurtleBook.ino

+45-17
Original file line numberDiff line numberDiff line change
@@ -2107,16 +2107,20 @@ void p2pSenderMenuButtonHandler(int dir) {
21072107
void p2pSenderApplyButtonHandler(int dir) {
21082108
Serial3.print("<select>");
21092109
Serial3.print("\r");
2110-
2110+
Serial3.flush();
2111+
Serial3.readStringUntil('\r'); //ack wait
21112112
Serial3.print(exfiles[p2pSenderMenuIdx]);
21122113
//oled progress show?
21132114
Serial3.print("\r");
2115+
Serial3.flush();
21142116

2117+
Serial3.readStringUntil('\r'); //ack wait
21152118
while (true) {
21162119
auto v_str = Serial3.readStringUntil('\r');
2120+
v_str.trim();
21172121
if (v_str == "end")
21182122
break;
2119-
v_str.trim();
2123+
21202124
drawOledString(v_str + "%");
21212125
}
21222126
drawOledString("100%");
@@ -2135,60 +2139,84 @@ String readLine() {
21352139
return teststr111;
21362140
}
21372141

2138-
21392142
void p2pApplyButtonHandler(int dir) {
21402143
if (!serial3started) {
21412144
Serial3.begin(115200);
21422145
serial3started = true;
21432146
}
21442147
if (p2pMenuIdx == 0) {
2145-
Serial3.println("web");
2148+
Serial3.print("web");
2149+
Serial3.print("\r");
21462150
clearOled();
21472151
Serial3.end();
21482152
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // PowerDown - самый экономный режим
21492153
sleep_mode(); // Переводим МК в сон
21502154
} else if (p2pMenuIdx == 1) {
2151-
Serial3.println("p2p.reciever");
2155+
Serial3.print("p2p.reciever");
2156+
Serial3.print("\r");
2157+
Serial3.setTimeout(10000);
2158+
drawOledString("waiting..");
2159+
2160+
while (true) {
2161+
auto v_str = Serial3.readStringUntil('\r');
2162+
v_str.trim();
2163+
if (v_str == "")
2164+
continue;
2165+
if (v_str == "end")
2166+
break;
2167+
v_str.trim();
2168+
drawOledString(v_str + "%");
2169+
}
2170+
drawOledString("100%");
21522171

2172+
clearOled();
2173+
Serial3.end();
2174+
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // PowerDown - самый экономный режим
2175+
sleep_mode();
21532176
//
21542177
} else if (p2pMenuIdx == 2) {
21552178
Serial3.setTimeout(10000);
2156-
Serial3.println("p2p.sender");
2157-
2179+
Serial3.print("p2p.sender");
2180+
Serial3.print("\r");
2181+
drawOledString("waiting..");
21582182
int fcount = 0;
21592183
int cntr = 0;
21602184
int cntr2 = 0;
21612185
bool was1 = false;
21622186
bool was2 = false;
21632187

2164-
Serial3.setTimeout(10000);
2165-
String teststr11 = Serial3.readStringUntil('\n');
2188+
Serial3.setTimeout(100000);
2189+
String teststr11 = Serial3.readStringUntil('\r');
21662190

21672191
drawOledString(teststr11);
21682192
delay(500);
21692193

21702194
while (true) {
21712195

2172-
teststr11 = Serial3.readStringUntil('\n');
2196+
teststr11 = Serial3.readStringUntil('\r');
21732197
teststr11.trim();
2174-
if (teststr11 == "<finish>") {
2198+
Serial3.print("ok");
2199+
Serial3.print("\r");
2200+
Serial3.flush();
2201+
//drawOledString(teststr11);
2202+
2203+
if (teststr11 == "<finish>")
21752204
break;
2176-
}
21772205

21782206
//read until timeout
2179-
drawOledString(teststr11);
2180-
delay(100);
2207+
21812208
if (exfiles_top < EXT_FILES_MAX)
2182-
exfiles[exfiles_top++] = (teststr3);
2209+
exfiles[exfiles_top++] = teststr11;
21832210
}
21842211

2185-
drawOledString("finish!");
2212+
/*drawOledString("finish!");
21862213
delay(1000);
21872214
for (int i = 0; i < exfiles_top; i++) {
21882215
drawOledString(exfiles[i]);
21892216
delay(1000);
21902217
}
2191-
2218+
drawOledString("finish2!");
2219+
delay(1000);*/
21922220
applyButton = p2pSenderApplyButtonHandler;
21932221
menuButton = p2pSenderMenuButtonHandler;
21942222
drawP2PSenderMenu();

TurtleBook_wemos/TurtleBook_wemos.ino

+105-29
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,11 @@ void p2p_enumDir(FsFile* tdir, String path) {
662662
file.getName(f_name, max_characters);
663663
//filename = String(f_name);
664664
String filename = String(f_name);
665-
Serial.println(filename);
665+
Serial.print(filename);
666+
Serial.print("\r");
667+
668+
Serial.flush();
669+
Serial.readStringUntil('\r');
666670

667671
if (file.isDir()) {
668672

@@ -764,12 +768,12 @@ WiFiServer p2p_server(888);
764768
bool webMode = true;
765769

766770
IPAddress ip(192, 168, 1, 1);
767-
IPAddress gateway(192, 168, 4, 1);
771+
IPAddress gateway(192, 168, 1, 1);
768772
IPAddress subnet(255, 255, 255, 0);
769773

770774
void p2pRecieverMode() {
771-
772-
775+
Serial.print("cp1");
776+
Serial.print('\r');
773777
WiFi.mode(WIFI_AP);
774778
WiFi.setOutputPower(0);
775779
WiFi.softAPConfig(ip, gateway, subnet);
@@ -785,50 +789,88 @@ void p2pRecieverMode() {
785789
}*/
786790

787791
p2p_server.begin();
788-
792+
Serial.print("cp2");
793+
Serial.print('\r');
789794
WiFiClient client = p2p_server.available(); // Проверка подключения клиента
790795
while (!client) {
791796
delay(100);
792797
}
793798
while (!client.available()) { // Ожидание запроса клиента
794799
delay(1);
795800
}
801+
Serial.print("cp3");
802+
Serial.print('\r');
796803
client.setTimeout(100000);
797804
String filename = client.readStringUntil('\r'); // filename
798805
filename.trim();
799-
if (!filename.startsWith("/")) filename = "/" + filename;
806+
if (!filename.startsWith("/"))
807+
filename = "/" + filename;
808+
Serial.print("cp4");
809+
Serial.print('\r');
800810

811+
Serial.print(filename);
812+
Serial.print('\r');
801813
String request2 = client.readStringUntil('\r'); // size
802814
request2.trim();
803815
auto size = request2.toInt();
804-
client.flush();
816+
Serial.print(size);
817+
Serial.print('\r');
818+
//client.flush();
819+
// Initialize the SD.
820+
my_DEV_Module_Init();
821+
bool goodInited = true;
822+
if (!sd.begin(SD_CONFIG)) {
823+
sd.initErrorHalt(&Serial);
824+
goodInited = false;
825+
}
826+
Serial.print("cp5");
827+
Serial.print('\r');
828+
805829
file = sd.open(filename, MFILE_WRITE);
806-
Serial.println("max");
807-
int crntsz = 0;
808-
Serial.println(size);
809-
//while (client.available()) {
830+
Serial.print("cp6");
831+
Serial.print('\r');
810832
for (long ii = 0; ii < size; ii++) {
811833
file.write(client.read());
812-
813-
if (ii % 1000 == 0)
814-
Serial.println(crntsz);
834+
if (ii % 1000 == 0) {
835+
float vv = ii / size;
836+
Serial.print((int)(100 * vv));
837+
Serial.print('\r');
838+
}
815839
}
840+
816841
file.close();
842+
Serial.print("end");
843+
Serial.print('\r');
817844
WiFi.mode(WIFI_OFF); // TURN OFF WIFI
818845
WiFi.forceSleepBegin();
819846
ESP.deepSleep(0);
820847
}
821848

822849
void p2pSenderMode() {
823850
webMode = false;
824-
Serial.println("files");
851+
Serial.print("files");
852+
Serial.print("\r");
853+
825854
Serial.flush();
855+
my_DEV_Module_Init();
856+
// Initialize the SD.
857+
bool goodInited = true;
858+
if (!sd.begin(SD_CONFIG)) {
859+
sd.initErrorHalt(&Serial);
860+
goodInited = false;
861+
}
862+
826863
if (!dir.open("/")) {
827864
error("dir.open failed");
828865
}
829866
p2p_enumDir(&dir, "/");
830-
Serial.println("<finish>");
867+
868+
869+
Serial.print("<finish>");
870+
Serial.print("\r");
871+
831872
Serial.flush();
873+
Serial.readStringUntil('\r');
832874
String dPath = "/1.cb";
833875
Serial.setTimeout(100000);
834876
while (true) {
@@ -837,23 +879,37 @@ void p2pSenderMode() {
837879
}
838880

839881
String teststr = Serial.readStringUntil('\r'); //read until timeout
840-
teststr.trim(); // remove any \r \n whitespace at the end of the String
882+
883+
Serial.print("ack");
884+
Serial.print("\r");
885+
Serial.flush();
886+
teststr.trim(); // remove any \r \n whitespace at the end of the String
841887
if (teststr == "<select>") {
842888

843889
dPath = Serial.readStringUntil('\r'); //read until timeout
890+
Serial.print("ack");
891+
Serial.print("\r");
892+
Serial.flush();
844893
dPath.trim();
845894
break;
846895
} else {
847896
}
848897
}
849-
898+
delay(500);
899+
Serial.print("cp1");
900+
Serial.print("\r");
901+
Serial.flush();
902+
delay(500);
850903
WiFi.setOutputPower(0);
851904
WiFi.mode(WIFI_STA);
852905
WiFi.begin(p2p_ssid, p2p_password);
853906
while (WiFi.status() != WL_CONNECTED) {
854907
delay(500);
855908
}
856-
909+
Serial.print("cp2");
910+
Serial.print("\r");
911+
Serial.flush();
912+
delay(500);
857913
WiFiClient client;
858914
if (!client.connect(ip, 888)) {
859915
//return client;sleep?
@@ -864,25 +920,46 @@ void p2pSenderMode() {
864920
}
865921
//send file name
866922
//send data
923+
Serial.print("cp3");
924+
Serial.print("\r");
925+
Serial.flush();
926+
delay(500);
927+
928+
929+
if (!dPath.startsWith("/"))
930+
dPath = "/" + dPath;
867931

868932
client.print(dPath);
869933
client.print('\r');
934+
client.flush();
935+
936+
Serial.print(dPath);
937+
Serial.print("\r");
938+
Serial.flush();
939+
delay(500);
870940
file = sd.open(dPath, MFILE_READ);
871941
//Serial.println("max");
872942

873-
auto sz = file.size();
943+
auto sz = file.size();
944+
client.print(sz);
945+
client.print('\r');
946+
client.flush();
874947
for (long i = 0; i < sz; i++) {
875948
client.write(file.read());
876-
if (i % 1000 == 0) {
877-
float vv = i / sz;
949+
if (i % 10000 == 0) {
950+
float vv = i / (float)sz;
878951
Serial.print((int)(100 * vv));
879-
client.print('\r');
952+
Serial.print('\r');
953+
Serial.flush();
880954
}
881955
}
882-
client.print("end");
883-
client.print('\r');
884-
file.close();
956+
file.close();
957+
958+
885959
WiFi.mode(WIFI_OFF); // TURN OFF WIFI
960+
Serial.print("end");
961+
Serial.print('\r');
962+
Serial.flush();
886963
WiFi.forceSleepBegin();
887964
ESP.deepSleep(0);
888965
}
@@ -944,9 +1021,8 @@ void setup() {
9441021
yield();
9451022
}
9461023

947-
while (Serial.available() == 0) {}
948-
String teststr = Serial.readString(); //read until timeout
949-
teststr.trim(); // remove any \r \n whitespace at the end of the String
1024+
String teststr = Serial.readStringUntil('\r'); //read until timeout
1025+
teststr.trim(); // remove any \r \n whitespace at the end of the String
9501026
if (teststr == "web") {
9511027
break;
9521028
}

0 commit comments

Comments
 (0)