Skip to content

Commit 1c9a3dc

Browse files
committed
ballle98#21: unpredictable waitForPDAMessageType added 45 second delay
1 parent 9aa1aa7 commit 1c9a3dc

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

pda_aq_programmer.c

+50-26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#include "init_buttons.h"
1818

1919
bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, int numMessageReceived);
20-
bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, int numMessageReceived);
20+
static bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
21+
unsigned long sec, unsigned long msec);
2122
bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu);
2223
bool wait_pda_selected_item(struct aqualinkdata *aq_data);
2324
bool waitForPDAnextMenu(struct aqualinkdata *aq_data);
@@ -30,7 +31,7 @@ static pda_type _PDA_Type;
3031
bool wait_pda_selected_item(struct aqualinkdata *aq_data)
3132
{
3233
while (pda_m_hlightindex() == -1){
33-
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,10);
34+
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2,0);
3435
}
3536

3637
if (pda_m_hlightindex() == -1)
@@ -40,8 +41,8 @@ bool wait_pda_selected_item(struct aqualinkdata *aq_data)
4041
}
4142

4243
bool waitForPDAnextMenu(struct aqualinkdata *aq_data) {
43-
waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10);
44-
return waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,15);
44+
waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,2,0);
45+
return waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,3,0);
4546
}
4647

4748
bool loopover_devices(struct aqualinkdata *aq_data) {
@@ -80,7 +81,7 @@ bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charli
8081
send_cmd(KEY_PDA_DOWN);
8182
//delay(500);
8283
//wait_for_empty_cmd_buffer();
83-
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2);
84+
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,0,500);
8485
//waitForMessage(aq_data, NULL, 1);
8586
index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit);
8687
if (index >= 0) {
@@ -150,7 +151,7 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
150151
//logMessage(LOG_DEBUG, "******************PDA Device programmer selected back button\n",menu);
151152
waitForPDAnextMenu(aq_data);
152153
} else {
153-
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,15);
154+
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,3,0);
154155
}
155156
//logMessage(LOG_DEBUG, "******************PDA Device programmer menu type %d\n",pda_m_type());
156157
//if (!wait_for_empty_cmd_buffer() || i++ > 6)
@@ -436,32 +437,55 @@ bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex,
436437
}
437438

438439

439-
bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, int numMessageReceived)
440+
bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
441+
unsigned long sec, unsigned long msec)
440442
{
441-
logMessage(LOG_DEBUG, "waitForPDAMessageType 0x%02hhx\n",mtype);
442-
443443
int i=0;
444+
444445
pthread_mutex_init(&aq_data->active_thread.thread_mutex, NULL);
445-
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
446446

447-
while( ++i <= numMessageReceived)
448-
{
449-
logMessage(LOG_DEBUG, "waitForPDAMessageType 0x%02hhx, last message type was 0x%02hhx (%d of %d)\n",mtype,aq_data->last_packet_type,i,numMessageReceived);
447+
struct timespec max_wait;
448+
int ret = 0;
450449

451-
if (aq_data->last_packet_type == mtype) break;
450+
logMessage(LOG_DEBUG, "waitForPDAMessageType 0x%02hhx, %lu.%03lu sec\n",
451+
mtype, sec, msec);
452+
if (msec > 999)
453+
{
454+
logMessage(LOG_ERR, "waitForPDAMessageType INVALID msec value %lu\n", msec);
455+
}
456+
clock_gettime(CLOCK_REALTIME, &max_wait);
457+
max_wait.tv_sec += sec;
458+
max_wait.tv_nsec += msec*1000000;
459+
if (max_wait.tv_nsec > 999999999L)
460+
{
461+
max_wait.tv_nsec -= 1000000000L;
462+
max_wait.tv_sec++;
463+
}
452464

453-
pthread_cond_init(&aq_data->active_thread.thread_cond, NULL);
454-
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
455-
}
465+
pthread_cond_init(&aq_data->active_thread.thread_cond, NULL);
466+
467+
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
468+
469+
do {
470+
i++;
471+
logMessage(LOG_DEBUG, "waitForPDAMessageType 0x%02hhx, last message type was 0x%02hhx (%d)\n",
472+
mtype,aq_data->last_packet_type,i);
473+
474+
if ((ret = pthread_cond_timedwait(&aq_data->active_thread.thread_cond,
475+
&aq_data->active_thread.thread_mutex, &max_wait)))
476+
{
477+
logMessage(LOG_ERR, "waitForPDAMessageType: did not receive 0x%02hhx %s\n",
478+
mtype, strerror(ret));
479+
break;
480+
}
481+
} while(aq_data->last_packet_type != mtype);
456482

457483
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
458-
459-
if (aq_data->last_packet_type != mtype) {
460-
//logMessage(LOG_ERR, "Could not select MENU of Aqualink control panel\n");
461-
logMessage(LOG_DEBUG, "waitForPDAMessageType: did not receive 0x%02hhx\n",mtype);
462-
return false;
463-
} else
464-
logMessage(LOG_DEBUG, "waitForPDAMessageType: received 0x%02hhx\n",mtype);
484+
if (ret)
485+
{
486+
return false;
487+
}
488+
logMessage(LOG_DEBUG, "waitForPDAMessageType: received 0x%02hhx\n",mtype);
465489

466490
return true;
467491
}
@@ -474,7 +498,7 @@ bool set_PDA_numeric_field_value(struct aqualinkdata *aq_data, int val, int *cur
474498
while ( strncasecmp(pda_m_hlight(), select_label, 8) != 0 ) {
475499
send_cmd(KEY_PDA_DOWN);
476500
delay(500); // Last message probably was CMD_PDA_HIGHLIGHT, so wait before checking.
477-
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2);
501+
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,0,500);
478502
}
479503

480504
send_cmd(KEY_PDA_SELECT);
@@ -807,4 +831,4 @@ The SCALE setting is fixed to RPM for the Jandy ePumpTM DC, Jandy ePumpTM AC, an
807831
The SCALE setting is fixed to GPM for the IntelliFlo® VF
808832
809833
There are eight (8) default speed presets for each variable speed pump.
810-
*/
834+
*/

0 commit comments

Comments
 (0)