Skip to content

Commit aee2b32

Browse files
authored
Major update
1 parent a78f3ea commit aee2b32

File tree

6 files changed

+383
-119
lines changed

6 files changed

+383
-119
lines changed

Nextion_HMI/Inc/Nextion_HMI.h

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern "C" {
4545
//DEFINES
4646
#define NEX_DEFAULT_SPEED (9600U)
4747

48-
#define NEX_VERBOSE_COMM
48+
//#define NEX_VERBOSE_COMM
4949

5050
#define NEX_RX_BUFF_SIZE (20) // UART RX buffer size
5151
#define NEX_MAX_OBJECTS (50) //maximum objects on the display
@@ -117,8 +117,8 @@ typedef enum {
117117

118118
typedef enum {
119119
COMP_IDLE = 0,
120-
COMP_BUSY,
121-
COMP_WAITANSW
120+
COMP_BUSY_TX,
121+
COMP_BUSY_RX
122122
} NxCompRetStatus_t;
123123

124124

@@ -127,7 +127,7 @@ typedef enum {
127127
STAT_TIMEOUT,
128128
STAT_FAILED,
129129
STAT_OK
130-
} FNC_Ret_Status_t;
130+
} Ret_Status_t;
131131

132132

133133
typedef struct Nextion_Object_t {
@@ -152,9 +152,12 @@ typedef struct Nextion_HMI_Handler_t {
152152
uint8_t rxPosition;
153153
uint16_t errorCnt;
154154
uint16_t cmdCnt;
155+
uint8_t ifaceVerbose;
155156
NxCompRetStatus_t hmiStatus;
156157

157158
///RTOS stuff
159+
TaskHandle_t xTaskToNotify;
160+
xTimerHandle blockTx;
158161
xTimerHandle rxTimerHandle;
159162
SemaphoreHandle_t hmiUartTxSem;
160163
osMessageQueueId_t rxCommandQHandle;
@@ -186,37 +189,51 @@ Nextion_HMI_Handler_t nextionHMI_h;
186189
char txBuf[30];
187190

188191
void HmiSendCommand(const char *cmd);
189-
FNC_Ret_Status_t waitForAnswer(Ret_Command_t *pRetCommand);
192+
Ret_Status_t waitForAnswer(Ret_Command_t *pRetCommand);
190193
void rxTimerCallback(void *argument);
194+
void txTimerCallback(void *argument);
191195
void prepareToSend(void);
192196

193197
///Public function prototypes
194198
void NxHmi_Init(UART_HandleTypeDef *huart);
195-
FNC_Ret_Status_t NxHmi_AddObject(Nextion_Object_t *pOb_handle);
196-
FNC_Ret_Status_t NxHmi_SetText(Nextion_Object_t *pOb_handle,const char *buffer);
197-
FNC_Ret_Status_t NxHmi_SetIntValue(Nextion_Object_t *pOb_handle, int16_t number);
198-
FNC_Ret_Status_t NxHmi_SetFloatValue(Nextion_Object_t *pOb_handle, float number);
199+
Ret_Status_t NxHmi_AddObject(Nextion_Object_t *pOb_handle);
200+
Ret_Status_t NxHmi_SetText(Nextion_Object_t *pOb_handle,const char *buffer);
201+
Ret_Status_t NxHmi_SetIntValue(Nextion_Object_t *pOb_handle, int16_t number);
202+
Ret_Status_t NxHmi_SetFloatValue(Nextion_Object_t *pOb_handle, float number);
199203

200204
//System commands
201205
void NxHmi_Verbosity(uint8_t vLevel);
202-
FNC_Ret_Status_t NxHmi_SetBacklight(uint8_t value, Cnf_permanence_t cnfSave);
203-
FNC_Ret_Status_t NxHmi_SendXYcoordinates(uint8_t status);
204-
FNC_Ret_Status_t NxHmi_Sleep(uint8_t status);
206+
Ret_Status_t NxHmi_SetBacklight(uint8_t value, Cnf_permanence_t cnfSave);
207+
Ret_Status_t NxHmi_SendXYcoordinates(uint8_t status);
208+
Ret_Status_t NxHmi_Sleep(uint8_t status);
209+
Ret_Status_t NxHmi_SetAutoSleep(uint16_t slNoSer, uint16_t slNoTouch, uint8_t wkpSer, uint8_t wkpTouch);
205210
//void NxHmi_ComSpeed(uint32_t baud, Cnf_permanence_t cnfSave);
206211

207212
//Operational commands
208-
FNC_Ret_Status_t NxHmi_ForceRedrawComponent(Nextion_Object_t *pOb_handle);
213+
Ret_Status_t NxHmi_ForceRedrawComponent(Nextion_Object_t *pOb_handle);
209214
void NxHmi_CalibrateTouchSensor(void);
210-
FNC_Ret_Status_t NxHmi_GotoPage(uint8_t pageId);
211-
FNC_Ret_Status_t NxHmi_SetObjectVisibility(Nextion_Object_t *pOb_handle, Ob_visibility_t visible);
212-
FNC_Ret_Status_t NxHmi_GetObjValue(Nextion_Object_t *pOb_handle, uint32_t *pValue);
213-
FNC_Ret_Status_t NxHmi_ResetDevice(void);
214-
FNC_Ret_Status_t NxHmi_GetCurrentPageId(uint8_t *pValue);
215+
Ret_Status_t NxHmi_GotoPage(uint8_t pageId);
216+
Ret_Status_t NxHmi_SetObjectVisibility(Nextion_Object_t *pOb_handle, Ob_visibility_t visible);
217+
Ret_Status_t NxHmi_GetObjValue(Nextion_Object_t *pOb_handle, uint32_t *pValue);
218+
Ret_Status_t NxHmi_ResetDevice(void);
219+
Ret_Status_t NxHmi_GetCurrentPageId(uint8_t *pValue);
220+
void NxHmi_WaveFormAddValue(Nextion_Object_t *pOb_handle, uint8_t channel, uint8_t value);
221+
Ret_Status_t NxHmi_WaveFormClearChannel(Nextion_Object_t *pOb_handle, uint8_t channel);
215222

216223
//GUI commands
217-
FNC_Ret_Status_t NxHmi_SetBcoColour(Nextion_Object_t *pOb_handle, uint16_t color);
218-
FNC_Ret_Status_t NxHmi_SetBcoColourRGB(Nextion_Object_t *pOb_handle, uint8_t red, uint8_t green, uint8_t blue);
224+
Ret_Status_t NxHmi_SetBcoColour(Nextion_Object_t *pOb_handle, uint16_t color);
225+
Ret_Status_t NxHmi_SetBcoColourRGB(Nextion_Object_t *pOb_handle, uint8_t red, uint8_t green, uint8_t blue);
226+
Ret_Status_t NxHmi_DrawImage(uint8_t picId, uint16_t xAxis, uint16_t yAxis);
219227

228+
Ret_Status_t NxHmi_DrawCropImage(uint8_t picId, uint16_t xPane, uint16_t yPane,
229+
uint16_t width, uint16_t height, uint16_t xImg, uint16_t yImg);
230+
231+
Ret_Status_t NxHmi_DrawLine(uint16_t startX, uint16_t startY, uint16_t endX, uint16_t endY, uint16_t color);
232+
233+
Ret_Status_t NxHmi_DrawRect(uint16_t startX, uint16_t startY, uint16_t endX,
234+
uint16_t endY, uint16_t color, uint8_t fMode);
235+
236+
Ret_Status_t NxHmi_DrawCircle(uint16_t centX, uint16_t centY, uint16_t radius, uint16_t color, uint8_t fMode);
220237

221238

222239
#ifdef __cplusplus

Nextion_HMI/Src/Nextion_HMI.c

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ static int8_t isItRawData(void);
4747
void ObjectHandlerTask(void *argument) {
4848

4949
Ret_Command_t objCommand;
50+
//Always perform a display reset
5051
NxHmi_ResetDevice();
51-
52-
#ifdef NEX_VERBOSE_COMM
53-
NxHmi_Verbosity(3);
54-
#endif
55-
52+
//NxHmi_Verbosity(3);
53+
nextionHMI_h.errorCnt = 0;
5654
for(;;) {
5755
// Block until an command arrives
5856
if(xQueueReceive(nextionHMI_h.objectQueueHandle, &objCommand, portMAX_DELAY) == pdPASS) {
@@ -100,25 +98,31 @@ void NxHmi_Init(UART_HandleTypeDef *huart) {
10098
nextionHMI_h.rxPosition = 0;
10199
nextionHMI_h.errorCnt = 0;
102100
nextionHMI_h.cmdCnt = 0;
101+
nextionHMI_h.ifaceVerbose = 2; // default is level 2, return data On Failure
103102
nextionHMI_h.hmiStatus = COMP_IDLE;
104-
103+
nextionHMI_h.xTaskToNotify = NULL; // no task is waiting
105104

106105
/* creation of hmiTasks */
107106
hmiObjectTaskHandle = osThreadNew(ObjectHandlerTask, NULL, &hmiObjectTask_attributes);
108107
hmiRxTaskHandle = osThreadNew(StartHmiRxTask, NULL, &hmiRxTask_attributes);
109108

110-
111109
/* creation of queues */
112-
113110
nextionHMI_h.rxCommandQHandle = osMessageQueueNew (4, sizeof(Ret_Command_t), &rxComQ_attributes);
114111
nextionHMI_h.objectQueueHandle = osMessageQueueNew (4, sizeof(Ret_Command_t), &txObjQ_attributes);
115112

116-
/* creation of RX timer */
113+
/* creation of timers */
117114
nextionHMI_h.rxTimerHandle = xTimerCreate("RxTimer", // Just a text name, not used by the kernel.
118115
TOUT_PERIOD_CALC(nextionHMI_h.pUart->Init.BaudRate) , // The timer period in ticks.
119116
pdFALSE, // The timers will auto-reload themselves when they expire.
120117
( 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.
122126
);
123127

124128
/* creation of semaphore */
@@ -135,7 +139,7 @@ void NxHmi_Init(UART_HandleTypeDef *huart) {
135139
* @param *pOb_handle Nextion object
136140
* @retval 0-failed, 1-success
137141
*/
138-
FNC_Ret_Status_t NxHmi_AddObject(Nextion_Object_t *pOb_handle) {
142+
Ret_Status_t NxHmi_AddObject(Nextion_Object_t *pOb_handle) {
139143
if (Nextion_Object_Count < NEX_MAX_OBJECTS) {
140144
Nextion_Object_List[Nextion_Object_Count] = pOb_handle;
141145
Nextion_Object_Count++;
@@ -156,7 +160,7 @@ FNC_Ret_Status_t NxHmi_AddObject(Nextion_Object_t *pOb_handle) {
156160
* @param *buffer = string pointer
157161
* @retval see @ref waitForAnswer() function for return value
158162
*/
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) {
160164

161165
prepareToSend();
162166
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)
173177
* @param number = integer
174178
* @retval see @ref waitForAnswer() function for return value
175179
*/
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) {
177181

178182
prepareToSend();
179183
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)
190194
* @param number = float number
191195
* @retval see @ref waitForAnswer() function for return value
192196
*/
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) {
194198

195199
prepareToSend();
196200
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
211215
* STAT_OK = command was successfully executed
212216
*
213217
*/
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;
216220
if(pRetCommand == NULL){
217-
Ret_Command_t tmpCommand;
221+
//Pass the address to pointer
218222
pRetCommand = &tmpCommand;
219-
} else {
220-
nextionHMI_h.hmiStatus = COMP_WAITANSW;
221223
}
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;
222229

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;
229230
} 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
241232
if(xQueueReceive(nextionHMI_h.rxCommandQHandle, pRetCommand, NEX_ANSW_TIMEOUT) == pdFALSE){
242233
//Timeout
243-
nextionHMI_h.hmiStatus = COMP_IDLE;
244234
return STAT_TIMEOUT;
235+
245236
} else {
237+
// Return value arrived
238+
return STAT_OK;
239+
} // end if data has been received
240+
}// end if NULL and Verbose is 0
246241

247-
if(pRetCommand->cmdCode == NEX_EVENT_SUCCESS) {
248-
return STAT_OK;
242+
} else {
249243

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
251250
return STAT_FAILED;
252-
253251
}
254252
}
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);
260256

257+
}
258+
return STAT_OK;
259+
}// end if verbose level is greater than 0
261260
}
262261

263262

@@ -490,8 +489,10 @@ void HmiSendCommand(const char *cmd) {
490489
while (HAL_UART_GetState(nextionHMI_h.pUart) == HAL_UART_STATE_BUSY) {
491490
osDelay(1);
492491
}
493-
492+
//Start data transmission
494493
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);
495496

496497
}
497498

@@ -504,17 +505,18 @@ void HmiSendCommand(const char *cmd) {
504505
*/
505506
static int8_t isItRawData(void) {
506507
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) ) {
508510
//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){
512514
//Raw data
513515
return 1;
514516
} else {
515517
//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;
518520
}
519521

520522
}
@@ -529,9 +531,15 @@ static int8_t isItRawData(void) {
529531
* @retval void
530532
*/
531533
void prepareToSend(void) {
534+
//Wait for the semaphore to get access to the UART
532535
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+
537544
}
545+

0 commit comments

Comments
 (0)