@@ -47,12 +47,10 @@ static int8_t isItRawData(void);
47
47
void ObjectHandlerTask (void * argument ) {
48
48
49
49
Ret_Command_t objCommand ;
50
+ //Always perform a display reset
50
51
NxHmi_ResetDevice ();
51
-
52
- #ifdef NEX_VERBOSE_COMM
53
- NxHmi_Verbosity (3 );
54
- #endif
55
-
52
+ //NxHmi_Verbosity(3);
53
+ nextionHMI_h .errorCnt = 0 ;
56
54
for (;;) {
57
55
// Block until an command arrives
58
56
if (xQueueReceive (nextionHMI_h .objectQueueHandle , & objCommand , portMAX_DELAY ) == pdPASS ) {
@@ -100,25 +98,31 @@ void NxHmi_Init(UART_HandleTypeDef *huart) {
100
98
nextionHMI_h .rxPosition = 0 ;
101
99
nextionHMI_h .errorCnt = 0 ;
102
100
nextionHMI_h .cmdCnt = 0 ;
101
+ nextionHMI_h .ifaceVerbose = 2 ; // default is level 2, return data On Failure
103
102
nextionHMI_h .hmiStatus = COMP_IDLE ;
104
-
103
+ nextionHMI_h . xTaskToNotify = NULL ; // no task is waiting
105
104
106
105
/* creation of hmiTasks */
107
106
hmiObjectTaskHandle = osThreadNew (ObjectHandlerTask , NULL , & hmiObjectTask_attributes );
108
107
hmiRxTaskHandle = osThreadNew (StartHmiRxTask , NULL , & hmiRxTask_attributes );
109
108
110
-
111
109
/* creation of queues */
112
-
113
110
nextionHMI_h .rxCommandQHandle = osMessageQueueNew (4 , sizeof (Ret_Command_t ), & rxComQ_attributes );
114
111
nextionHMI_h .objectQueueHandle = osMessageQueueNew (4 , sizeof (Ret_Command_t ), & txObjQ_attributes );
115
112
116
- /* creation of RX timer */
113
+ /* creation of timers */
117
114
nextionHMI_h .rxTimerHandle = xTimerCreate ("RxTimer" , // Just a text name, not used by the kernel.
118
115
TOUT_PERIOD_CALC (nextionHMI_h .pUart -> Init .BaudRate ) , // The timer period in ticks.
119
116
pdFALSE , // The timers will auto-reload themselves when they expire.
120
117
( void * )0 , // Assign each timer a unique id equal to its array index.
121
- (TimerCallbackFunction_t ) rxTimerCallback // Each timer calls the same callback when it expires.
118
+ (TimerCallbackFunction_t ) rxTimerCallback // Timer callback when it expires.
119
+ );
120
+ /* creation of TX timer */
121
+ nextionHMI_h .blockTx = xTimerCreate ("TxTimer" , // Just a text name, not used by the kernel.
122
+ TOUT_PERIOD_CALC (nextionHMI_h .pUart -> Init .BaudRate ) , // The minimum time between sending commands
123
+ pdFALSE , // The timers will auto-reload themselves when they expire.
124
+ ( void * )0 , // Assign each timer a unique id equal to its array index.
125
+ (TimerCallbackFunction_t ) txTimerCallback // Timer callback when it expires.
122
126
);
123
127
124
128
/* creation of semaphore */
@@ -135,7 +139,7 @@ void NxHmi_Init(UART_HandleTypeDef *huart) {
135
139
* @param *pOb_handle Nextion object
136
140
* @retval 0-failed, 1-success
137
141
*/
138
- FNC_Ret_Status_t NxHmi_AddObject (Nextion_Object_t * pOb_handle ) {
142
+ Ret_Status_t NxHmi_AddObject (Nextion_Object_t * pOb_handle ) {
139
143
if (Nextion_Object_Count < NEX_MAX_OBJECTS ) {
140
144
Nextion_Object_List [Nextion_Object_Count ] = pOb_handle ;
141
145
Nextion_Object_Count ++ ;
@@ -156,7 +160,7 @@ FNC_Ret_Status_t NxHmi_AddObject(Nextion_Object_t *pOb_handle) {
156
160
* @param *buffer = string pointer
157
161
* @retval see @ref waitForAnswer() function for return value
158
162
*/
159
- FNC_Ret_Status_t NxHmi_SetText (Nextion_Object_t * pOb_handle , const char * buffer ) {
163
+ Ret_Status_t NxHmi_SetText (Nextion_Object_t * pOb_handle , const char * buffer ) {
160
164
161
165
prepareToSend ();
162
166
sprintf (txBuf , "%s.txt=\"%s\"" , pOb_handle -> Name , buffer );
@@ -173,7 +177,7 @@ FNC_Ret_Status_t NxHmi_SetText(Nextion_Object_t *pOb_handle, const char *buffer)
173
177
* @param number = integer
174
178
* @retval see @ref waitForAnswer() function for return value
175
179
*/
176
- FNC_Ret_Status_t NxHmi_SetIntValue (Nextion_Object_t * pOb_handle , int16_t number ) {
180
+ Ret_Status_t NxHmi_SetIntValue (Nextion_Object_t * pOb_handle , int16_t number ) {
177
181
178
182
prepareToSend ();
179
183
sprintf (txBuf , "%s.val=%i" , pOb_handle -> Name , number );
@@ -190,7 +194,7 @@ FNC_Ret_Status_t NxHmi_SetIntValue(Nextion_Object_t *pOb_handle, int16_t number)
190
194
* @param number = float number
191
195
* @retval see @ref waitForAnswer() function for return value
192
196
*/
193
- FNC_Ret_Status_t NxHmi_SetFloatValue ( Nextion_Object_t * pOb_handle , float number ) {
197
+ Ret_Status_t NxHmi_SetFloatValue ( Nextion_Object_t * pOb_handle , float number ) {
194
198
195
199
prepareToSend ();
196
200
sprintf (txBuf , "%s.txt=\"%.2f\"" , pOb_handle -> Name , number );
@@ -211,53 +215,48 @@ FNC_Ret_Status_t NxHmi_SetFloatValue( Nextion_Object_t *pOb_handle, float number
211
215
* STAT_OK = command was successfully executed
212
216
*
213
217
*/
214
- FNC_Ret_Status_t waitForAnswer (Ret_Command_t * pRetCommand ) {
215
-
218
+ Ret_Status_t waitForAnswer (Ret_Command_t * pRetCommand ) {
219
+ Ret_Command_t tmpCommand ;
216
220
if (pRetCommand == NULL ){
217
- Ret_Command_t tmpCommand ;
221
+ //Pass the address to pointer
218
222
pRetCommand = & tmpCommand ;
219
- } else {
220
- nextionHMI_h .hmiStatus = COMP_WAITANSW ;
221
223
}
224
+ //This case the HMI is in silent mode, no command execution confirmation
225
+ if (nextionHMI_h .ifaceVerbose == 0 ) {
226
+ if (pRetCommand == & tmpCommand ){
227
+ //We not expecting any incoming data
228
+ return STAT_OK ;
222
229
223
- #ifdef NEX_VERBOSE_COMM
224
-
225
- if (xQueueReceive (nextionHMI_h .rxCommandQHandle , pRetCommand , NEX_ANSW_TIMEOUT ) == pdFALSE ){
226
- //Timeout
227
- nextionHMI_h .hmiStatus = COMP_IDLE ;
228
- return STAT_TIMEOUT ;
229
230
} else {
230
- nextionHMI_h .hmiStatus = COMP_IDLE ;
231
- if (pRetCommand -> cmdCode == NEX_RET_INVALID_CMD ) {
232
- return STAT_FAILED ;
233
- } else {
234
- return STAT_OK ;
235
- }
236
-
237
- }
238
- #else
239
- if (pRetCommand != NULL ){
240
-
231
+ //We expecting a return value
241
232
if (xQueueReceive (nextionHMI_h .rxCommandQHandle , pRetCommand , NEX_ANSW_TIMEOUT ) == pdFALSE ){
242
233
//Timeout
243
- nextionHMI_h .hmiStatus = COMP_IDLE ;
244
234
return STAT_TIMEOUT ;
235
+
245
236
} else {
237
+ // Return value arrived
238
+ return STAT_OK ;
239
+ } // end if data has been received
240
+ }// end if NULL and Verbose is 0
246
241
247
- if (pRetCommand -> cmdCode == NEX_EVENT_SUCCESS ) {
248
- return STAT_OK ;
242
+ } else {
249
243
250
- } else if (pRetCommand -> cmdCode == NEX_RET_INVALID_CMD ) {
244
+ if (pRetCommand == & tmpCommand ){
245
+ //We not expecting any incoming data, but a return value is possible
246
+ if ( xQueueReceive (nextionHMI_h .rxCommandQHandle , pRetCommand , pdMS_TO_TICKS (5 ) ) == pdTRUE ) {
247
+ //Return value arrived before timeout occurred
248
+ if (pRetCommand -> cmdCode != NEX_EVENT_SUCCESS ) {
249
+ //If the returned command code is anything other than SUCCESS
251
250
return STAT_FAILED ;
252
-
253
251
}
254
252
}
255
- }// if NOT NULL
256
- #endif
257
- //if its disabled, a value must to returns
258
- nextionHMI_h .hmiStatus = COMP_IDLE ;
259
- return STAT_OK ;
253
+ } else {
254
+ //We expecting a return value
255
+ xQueueReceive (nextionHMI_h .rxCommandQHandle , pRetCommand , NEX_ANSW_TIMEOUT );
260
256
257
+ }
258
+ return STAT_OK ;
259
+ }// end if verbose level is greater than 0
261
260
}
262
261
263
262
@@ -490,8 +489,10 @@ void HmiSendCommand(const char *cmd) {
490
489
while (HAL_UART_GetState (nextionHMI_h .pUart ) == HAL_UART_STATE_BUSY ) {
491
490
osDelay (1 );
492
491
}
493
-
492
+ //Start data transmission
494
493
HAL_UART_Transmit_IT (nextionHMI_h .pUart ,(uint8_t * )txBuf , strlen (txBuf ) );
494
+ //Block the task until data has been transmitted
495
+ ulTaskNotifyTake (pdTRUE , portMAX_DELAY );
495
496
496
497
}
497
498
@@ -504,17 +505,18 @@ void HmiSendCommand(const char *cmd) {
504
505
*/
505
506
static int8_t isItRawData (void ) {
506
507
uint32_t tempNr ;
507
- if ( ( (nextionHMI_h .rxCounter % 4 ) == 0 ) && (nextionHMI_h .hmiStatus == COMP_IDLE ) ) {
508
+
509
+ if ( ( nextionHMI_h .rxCounter == 4 ) && (nextionHMI_h .xTaskToNotify == NULL ) ) {
508
510
//Likely to be raw data
509
- tempNr = (uint32_t )& nextionHMI_h .rxBuff [nextionHMI_h .rxPosition ];
510
- tempNr = tempNr & 0xFFFFFF ;
511
- if (tempNr != 0xFFFFFF ){
511
+ tempNr = * (uint32_t * )& nextionHMI_h .rxBuff [nextionHMI_h .rxPosition ];
512
+ tempNr = tempNr & 0xFFFFFF00 ;
513
+ if (tempNr != 0xFFFFFF00 ){
512
514
//Raw data
513
515
return 1 ;
514
516
} else {
515
517
//Command or Raw data value is higher or equal than 16 777 215 (0xFFFFFF)
516
- //TODO: For now let's assume, the returned data is raw data
517
- return 1 ;
518
+ //TODO: For now let's assume, the returned data is not a raw data
519
+ return 0 ;
518
520
}
519
521
520
522
}
@@ -529,9 +531,15 @@ static int8_t isItRawData(void) {
529
531
* @retval void
530
532
*/
531
533
void prepareToSend (void ) {
534
+ //Wait for the semaphore to get access to the UART
532
535
xSemaphoreTake (nextionHMI_h .hmiUartTxSem , portMAX_DELAY );
533
- while (nextionHMI_h .hmiStatus != COMP_IDLE ) {
534
- osDelay (1 );
535
- }
536
- nextionHMI_h .hmiStatus = COMP_BUSY ;
536
+
537
+ /* At this point xTaskToNotify should be NULL as no transmission
538
+ is in progress. */
539
+ configASSERT ( nextionHMI_h .xTaskToNotify == NULL );
540
+
541
+ /* Store the handle of the calling task. */
542
+ nextionHMI_h .xTaskToNotify = xTaskGetCurrentTaskHandle ();
543
+
537
544
}
545
+
0 commit comments