77#include < netinet/tcp.h>
88#include < unistd.h>
99#endif
10+ #include < sstream>
1011
1112#include " komihash/komihash.h"
1213#include " miniz/miniz.h"
@@ -52,71 +53,139 @@ bool ZeDMDWiFi::DoConnect(const char* ip)
5253 m_httpServer.sin_port = htons (80 );
5354 m_httpServer.sin_addr .s_addr = inet_addr (ip);
5455
55- if (SendGetRequest (" /get_version" ))
56- {
57- strncpy (m_firmwareVersion, ReceiveStringPayload (), sizeof (m_firmwareVersion) - 1 );
58- }
59- else
56+ int port = 3333 ;
57+ m_udpDelay = 5 ;
58+ uint8_t tries;
59+ for (tries = 0 ; tries < 2 ; tries++)
6060 {
61- Log ( " ZeDMD version could not be detected " );
62- return false ;
63- }
61+ if ( SendGetRequest ( " /handshake " ))
62+ {
63+ tries = 3 ;
6464
65- if (SendGetRequest (" /get_width" ))
66- {
67- m_width = (uint16_t )ReceiveIntegerPayload ();
68- }
69- else
70- {
71- Log (" ZeDMD width could not be detected" );
72- return false ;
73- }
65+ std::string handshake = ReceiveStringPayload ();
66+ std::stringstream ss (handshake);
67+ std::string item;
7468
75- if (SendGetRequest (" /get_height" ))
76- {
77- m_height = (uint16_t )ReceiveIntegerPayload ();
78- }
79- else
80- {
81- Log (" ZeDMD height could not be detected" );
82- return false ;
69+ for (uint8_t pos = 0 ; pos <= 6 ; pos++)
70+ {
71+ if (std::getline (ss, item, ' |' ))
72+ {
73+ switch (pos)
74+ {
75+ case 0 :
76+ {
77+ m_width = std::stoi (item);
78+ break ;
79+ }
80+ case 1 :
81+ {
82+ m_height = std::stoi (item);
83+ break ;
84+ }
85+ case 2 :
86+ {
87+ strncpy (m_firmwareVersion, item.c_str (), sizeof (m_firmwareVersion) - 1 );
88+ break ;
89+ }
90+ case 3 :
91+ {
92+ m_s3 = (std::stoi (item) == 1 );
93+ break ;
94+ }
95+ case 4 :
96+ {
97+ if (strcmp (" TCP" , item.c_str ()) == 0 )
98+ {
99+ m_tcp = true ;
100+ }
101+ break ;
102+ }
103+ case 5 :
104+ {
105+ port = std::stoi (item);
106+ break ;
107+ }
108+ case 6 :
109+ {
110+ m_udpDelay = std::stoi (item);
111+ break ;
112+ }
113+ }
114+ }
115+ }
116+ }
83117 }
84118
85- if (SendGetRequest ( " /get_s3 " ) )
119+ if (tries < 3 )
86120 {
87- m_s3 = (ReceiveIntegerPayload () == 1 );
88- }
89- else
90- {
91- Log (" ZeDMD ESP32 generation could not be detected" );
92- return false ;
93- }
121+ Log (" ZeDMD fast handshake failed ... fallback to single requests" );
94122
95- if (SendGetRequest (" /get_protocol" ))
96- {
97- if (strcmp (" TCP" , ReceiveStringPayload ()) == 0 )
123+ if (SendGetRequest (" /get_version" ))
98124 {
99- m_tcp = true ;
125+ strncpy (m_firmwareVersion, ReceiveCStringPayload (), sizeof (m_firmwareVersion) - 1 );
126+ }
127+ else
128+ {
129+ Log (" ZeDMD version could not be detected" );
130+ return false ;
100131 }
101- }
102- else
103- {
104- Log (" ZeDMD port could not be detected" );
105- return false ;
106- }
107132
108- int port = 3333 ;
109- if (SendGetRequest (" /get_port" ))
110- {
111- port = (int )ReceiveIntegerPayload ();
112- }
113- else
114- {
115- Log (" ZeDMD port could not be detected" );
116- return false ;
133+ if (SendGetRequest (" /get_width" ))
134+ {
135+ m_width = (uint16_t )ReceiveIntegerPayload ();
136+ }
137+ else
138+ {
139+ Log (" ZeDMD width could not be detected" );
140+ return false ;
141+ }
142+
143+ if (SendGetRequest (" /get_height" ))
144+ {
145+ m_height = (uint16_t )ReceiveIntegerPayload ();
146+ }
147+ else
148+ {
149+ Log (" ZeDMD height could not be detected" );
150+ return false ;
151+ }
152+
153+ if (SendGetRequest (" /get_s3" ))
154+ {
155+ m_s3 = (ReceiveIntegerPayload () == 1 );
156+ }
157+ else
158+ {
159+ Log (" ZeDMD ESP32 generation could not be detected" );
160+ return false ;
161+ }
162+
163+ if (SendGetRequest (" /get_protocol" ))
164+ {
165+ if (strcmp (" TCP" , ReceiveCStringPayload ()) == 0 )
166+ {
167+ m_tcp = true ;
168+ }
169+ }
170+ else
171+ {
172+ Log (" ZeDMD port could not be detected" );
173+ return false ;
174+ }
175+
176+ int port = 3333 ;
177+ if (SendGetRequest (" /get_port" ))
178+ {
179+ port = (int )ReceiveIntegerPayload ();
180+ }
181+ else
182+ {
183+ Log (" ZeDMD port could not be detected" );
184+ return false ;
185+ }
117186 }
118187
119- if ((m_width != 128 && m_width != 256 ) || (m_height != 32 && m_height != 64 ))
188+ if (!( 128 == m_width && 32 == m_height) && !( 256 == m_width && 64 == m_height ))
120189 {
121190 Log (" Invalid dimensions reported from ZeDMD: %dx%d" , m_width, m_height);
122191 return false ;
@@ -330,7 +399,7 @@ int ZeDMDWiFi::ReceiveIntegerPayload()
330399 }
331400}
332401
333- const char * ZeDMDWiFi::ReceiveStringPayload ()
402+ std::string ZeDMDWiFi::ReceiveStringPayload ()
334403{
335404 std::string response = ReceiveResponse ();
336405
@@ -342,11 +411,11 @@ const char* ZeDMDWiFi::ReceiveStringPayload()
342411 }
343412
344413 // The payload starts after "\r\n\r\n"
345- std::string payload = response.substr (headerEnd + 4 );
346-
347- return payload.c_str ();
414+ return response.substr (headerEnd + 4 );
348415}
349416
417+ const char * ZeDMDWiFi::ReceiveCStringPayload () { return ReceiveStringPayload ().c_str (); }
418+
350419bool ZeDMDWiFi::IsConnected () { return m_connected; }
351420
352421void ZeDMDWiFi::Reset () {}
@@ -381,7 +450,7 @@ bool ZeDMDWiFi::SendChunks(uint8_t* pData, uint16_t size)
381450 // ESP32 crashes if too many packages arrive in a short time, mainly in FlexDMD tables.
382451 // Even if the firmware has some protections and should ignore some packages, it crashes.
383452 // It seems like a bug in AsyncUDP. At the moment, only that delay seems to avoid the crash.
384- std::this_thread::sleep_for (std::chrono::milliseconds (5 ));
453+ std::this_thread::sleep_for (std::chrono::milliseconds (m_udpDelay ));
385454 }
386455 }
387456
0 commit comments