├── util ├── my_types.h ├── Utilities.c └── Utilities.h ├── nextion ├── NexPage.h ├── NexPage.c ├── NexObject.h ├── NexConfig.h ├── NexTouch.h ├── Serial.h ├── Nextion.h ├── NexHardware.h ├── NexProgressBar.h ├── NexTouch.c ├── Serial.c ├── NexProgressBar.c ├── NexSlider.h ├── NexText.h ├── NexNumber.h ├── NexSlider.c ├── NexText.c ├── NexButton.h ├── NexHardware.c ├── NexNumber.c └── NexButton.c ├── Example.c └── README.md /util/my_types.h: -------------------------------------------------------------------------------- 1 | #ifndef MY_TYPES_H 2 | #define MY_TYPES_H 3 | 4 | #endif -------------------------------------------------------------------------------- /nextion/NexPage.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXPAGE_H__ 2 | #define __NEXPAGE_H__ 3 | #include "NexTouch.h" 4 | #include "NexHardware.h" 5 | #include "../util/Utilities.h" 6 | 7 | uint8_t NexPage_show(struct NexObject *); 8 | 9 | #endif /* #ifndef __NEXPAGE_H__ */ 10 | -------------------------------------------------------------------------------- /nextion/NexPage.c: -------------------------------------------------------------------------------- 1 | #include "NexPage.h" 2 | #include 3 | #include "../util/Utilities.h" 4 | 5 | uint8_t buffer[4]; 6 | extern char cmd[64]; 7 | 8 | uint8_t NexPage_show(struct NexObject *page) 9 | { 10 | 11 | ClearString(buffer); 12 | if (!page->__name) 13 | { 14 | return 0; 15 | } 16 | 17 | ClearString(cmd); 18 | strcat(cmd, "page "); 19 | strcat(cmd, page->__name); 20 | sendCommand(cmd); 21 | return recvRetCommandFinished(); 22 | } -------------------------------------------------------------------------------- /nextion/NexObject.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __NEXOBJECT_H__ 3 | #define __NEXOBJECT_H__ 4 | #include "NexConfig.h" 5 | #include "../util/Utilities.h" 6 | 7 | typedef void (*NexTouchEventCb)(void *ptr); 8 | 9 | struct NexObject 10 | { 11 | uint8_t __pid; /* Page ID */ 12 | uint8_t __cid; /* Component ID */ 13 | char __name[16]; /* An unique name */ 14 | NexTouchEventCb __cb_push; 15 | void *__cbpush_ptr; 16 | NexTouchEventCb __cb_pop; 17 | void *__cbpop_ptr; 18 | }; 19 | 20 | #endif -------------------------------------------------------------------------------- /nextion/NexConfig.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __NEXCONFIG_H__ 3 | #define __NEXCONFIG_H__ 4 | #include "Serial.h" 5 | 6 | #define nexSerial_init(b) Serial_Init(b) 7 | #define nexSerial_available() Serial_Available() 8 | #define nexSerial_read() Serial_Read() 9 | #define nexSerial_write(d) Serial_Write(d) 10 | #define nexSerial_print(p) Serial_Print(p) 11 | #define nexSerial_readBytes(b,l) Serial_ReadBytes(b, l) 12 | #define nexDelay(d) delay_ms(d) 13 | 14 | #endif /* #ifndef __NEXCONFIG_H__ */ 15 | -------------------------------------------------------------------------------- /nextion/NexTouch.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __NEXTOUCH_H__ 3 | #define __NEXTOUCH_H__ 4 | 5 | #include "NexConfig.h" 6 | #include "NexObject.h" 7 | #include 8 | #include "../util/Utilities.h" 9 | 10 | #define NEX_EVENT_PUSH (0x01) 11 | #define NEX_EVENT_POP (0x00) 12 | 13 | void NexTouch_iterate(struct NexObject **list, uint8_t pid, uint8_t cid, int32_t event); 14 | 15 | void NexTouch_attachPush(struct NexObject *touch, NexTouchEventCb push, void *ptr); 16 | 17 | void NexTouch_detachPush(struct NexObject *touch); 18 | 19 | void NexTouch_attachPop(struct NexObject *touch, NexTouchEventCb pop, void *ptr); 20 | 21 | void NexTouch_detachPop(struct NexObject *touch); 22 | 23 | void NexTouch_push(struct NexObject *touch); 24 | void NexTouch_pop(struct NexObject *touch); 25 | 26 | #endif -------------------------------------------------------------------------------- /nextion/Serial.h: -------------------------------------------------------------------------------- 1 | #ifndef SERIAL_H 2 | #define SERIAL_H 3 | /************************************************************************* 4 | * MACROS 5 | *************************************************************************/ 6 | #define RX_BUFFER_SIZE 64 7 | #define TX_BUFFER_SIZE 64 8 | #define RX_Flush() _rx_buffer_head=0;_rx_buffer_tail=0 9 | #define TX_Flush() _tx_buffer_head=0;_tx_buffer_tail=0 10 | /************************************************************************* 11 | * FUNCTIONS 12 | *************************************************************************/ 13 | void Serial_Init(long baudrate); 14 | unsigned char Serial_Write(unsigned char c); 15 | unsigned char Serial_Read(); 16 | unsigned char Serial_Available(); 17 | unsigned char Serial_ReadBytes(char *buf, unsigned char len); 18 | void Serial_Print(unsigned char *txt); 19 | #endif -------------------------------------------------------------------------------- /util/Utilities.c: -------------------------------------------------------------------------------- 1 | #include "Utilities.h" 2 | #include 3 | #include 4 | 5 | uint8_t TestBit(uint8_t var, int8_t bi) 6 | { 7 | if (((var) & (0b00000001 << (bi)))) 8 | { 9 | return 1; 10 | } 11 | else 12 | { 13 | return 0; 14 | } 15 | } 16 | 17 | char *utoa(char *str, unsigned int value, int radix) 18 | { 19 | int size; 20 | const char *format = 0; 21 | switch (radix) 22 | { 23 | case 8: 24 | format = "%o"; 25 | break; 26 | case 10: 27 | format = "%u"; 28 | break; 29 | case 16: 30 | format = "%x"; 31 | break; 32 | } 33 | if (format == 0) 34 | return str; 35 | size = sprintf(str, format, value); 36 | return &str[size]; 37 | } 38 | 39 | uint8_t ArrayLength(char *arr) 40 | { 41 | return sizeof(arr) / sizeof(arr[0]); 42 | } -------------------------------------------------------------------------------- /nextion/Nextion.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file Nextion.h 3 | * 4 | * The header file including all other header files provided by this library. 5 | * 6 | * Every example sketch should include this file. 7 | * 8 | * @author Wu Pengfei (email:) 9 | * @date 2015/8/12 10 | * @copyright 11 | * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n 12 | * This program is free software; you can redistribute it and/or 13 | * modify it under the terms of the GNU General Public License as 14 | * published by the Free Software Foundation; either version 2 of 15 | * the License, or (at your option) any later version. 16 | */ 17 | #ifndef __NEXTION_H__ 18 | #define __NEXTION_H__ 19 | #include "NexObject.h" 20 | #include "NexConfig.h" 21 | #include "NexTouch.h" 22 | #include "NexHardware.h" 23 | #include "NexNumber.h" 24 | #include "NexButton.h" 25 | #include "NexPage.h" 26 | #include "NexProgressBar.h" 27 | #include "NexSlider.h" 28 | #include "NexText.h" 29 | 30 | #endif /* #ifndef __NEXTION_H__ */ -------------------------------------------------------------------------------- /Example.c: -------------------------------------------------------------------------------- 1 | #include "nextion/Nextion.h" 2 | #include "util/Utilities.h" 3 | #include 4 | #include 5 | 6 | struct NexObject button, slider; 7 | 8 | struct NexObject *nex_listen_list[] = { 9 | &button, 10 | &slider}; 11 | 12 | long value; 13 | 14 | #define buttonPageID 0 15 | #define sliderPageID 0 16 | #define buttonID 2 17 | #define sliderID 2 18 | 19 | void main() 20 | { 21 | CreateNexObject(button, buttonPageID, buttonID, "MyButton"); 22 | CreateNexObject(slider, sliderPageID, sliderID, "MySlider"); 23 | 24 | nexInit(); 25 | 26 | NexSlider_getValue(&slider, &value); 27 | NexSlider_setValue(&slider, value + 20); 28 | 29 | NexTouch_attachPop(&button, buttonCallback, 0); 30 | NexTouch_attachPop(&slider, sliderCallback, 0); 31 | 32 | while (1) 33 | { 34 | nexLoop(nex_listen_list); 35 | delay_ms(50); 36 | } 37 | } 38 | 39 | void buttonCallback(void *ptr) 40 | { 41 | //TODO 42 | } 43 | 44 | void sliderCallback(void *ptr) 45 | { 46 | //TODO 47 | } -------------------------------------------------------------------------------- /nextion/NexHardware.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXHARDWARE_H__ 2 | #define __NEXHARDWARE_H__ 3 | #include "NexConfig.h" 4 | #include "NexTouch.h" 5 | #include "NexObject.h" 6 | #include "../util/Utilities.h" 7 | /** 8 | * Init Nextion. 9 | * 10 | * @return true if success, false for failure. 11 | */ 12 | uint8_t nexInit(void); 13 | 14 | /** 15 | * Listen touch event and calling callbacks attached before. 16 | * 17 | * Supports push and pop at present. 18 | * 19 | * @param nex_listen_list - index to Nextion Components list. 20 | * @return none. 21 | * 22 | * @warning This function must be called repeatedly to response touch events 23 | * from Nextion touch panel. Actually, you should place it in your loop function. 24 | */ 25 | void nexLoop(struct NexObject *nex_listen_list[]); 26 | 27 | /** 28 | * @} 29 | */ 30 | //timeout=100 31 | uint8_t recvRetNumber(uint32_t *number); 32 | uint16_t recvRetString(char *buffer, uint16_t len); 33 | uint8_t recvRetCommandFinished(); 34 | void sendCommand(char *command); 35 | 36 | #define CreateNexObject(obj, pid, id, name) \ 37 | obj.__pid = pid; \ 38 | obj.__cid = id; \ 39 | StringCopy(obj.__name, name) 40 | 41 | #endif /* #ifndef __NEXHARDWARE_H__ */ -------------------------------------------------------------------------------- /util/Utilities.h: -------------------------------------------------------------------------------- 1 | #ifndef UTITLITIES_H 2 | #define UTITLITIES_H 3 | /******************************************************************************* 4 | * TYPES 5 | *******************************************************************************/ 6 | typedef short int8_t; 7 | typedef unsigned short uint8_t; 8 | typedef int int16_t; 9 | typedef unsigned int uint16_t; 10 | typedef long int32_t; 11 | typedef unsigned long uint32_t; 12 | /******************************************************************************* 13 | * MACROS 14 | *******************************************************************************/ 15 | #define ClearArray(arr) memset(arr, 0, sizeof(arr) / sizeof(arr[0])) 16 | #define ClearString(str) memset(str, 0, strlen(str)) 17 | #define ArrayCopy(d, s, l) \ 18 | ClearArray(d); \ 19 | memcpy(d, s, l) 20 | #define StringCopy(d, s) \ 21 | ClearString(d); \ 22 | memcpy(d, s, strlen(s)) 23 | #define StringAppend(d, s) strcat(d, s) 24 | /******************************************************************************* 25 | * FUNCTIONS 26 | *******************************************************************************/ 27 | uint8_t ArrayLength(char *arr); 28 | char *utoa(char *str, unsigned int value, int radix); 29 | #endif 30 | -------------------------------------------------------------------------------- /nextion/NexProgressBar.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXPROGRESSBAR_H__ 2 | #define __NEXPROGRESSBAR_H__ 3 | #include "NexTouch.h" 4 | #include "NexHardware.h" 5 | #include "../util/Utilities.h" 6 | 7 | /** 8 | * Get the value of progress bar. 9 | * 10 | * @param number - an output parameter to save the value of porgress bar. 11 | * 12 | * @retval true - success. 13 | * @retval false - failed. 14 | */ 15 | uint8_t NexProgressBar_getValue(struct NexObject *bar, uint32_t *number); 16 | 17 | /** 18 | * Set the value of progress bar. 19 | * 20 | * @param number - the value of progress bar. 21 | * 22 | * @retval true - success. 23 | * @retval false - failed. 24 | */ 25 | uint8_t NexProgressBar_setValue(struct NexObject *bar, uint32_t number); 26 | 27 | /** 28 | * Get bco attribute of component 29 | * 30 | * @param number - buffer storing data retur 31 | * @return the length of the data 32 | */ 33 | uint32_t NexProgressBar_Get_background_color_bco(struct NexObject *bar, uint32_t *number); 34 | 35 | /** 36 | * Set bco attribute of component 37 | * 38 | * @param number - To set up the data 39 | * @return true if success, false for failure 40 | */ 41 | uint8_t NexProgressBar_Set_background_color_bco(struct NexObject *bar, uint32_t number); 42 | 43 | /** 44 | * Get pco attribute of component 45 | * 46 | * @param number - buffer storing data retur 47 | * @return the length of the data 48 | */ 49 | uint32_t NexProgressBar_Get_font_color_pco(struct NexObject *bar, uint32_t *number); 50 | 51 | /** 52 | * Set pco attribute of component 53 | * 54 | * @param number - To set up the data 55 | * @return true if success, false for failure 56 | */ 57 | uint8_t NexProgressBar_Set_font_color_pco(struct NexObject *bar, uint32_t number); 58 | 59 | #endif /* #ifndef __NEXPROGRESSBAR_H__ */ -------------------------------------------------------------------------------- /nextion/NexTouch.c: -------------------------------------------------------------------------------- 1 | #include "NexTouch.h" 2 | 3 | NexTouchEventCb __cb_push; 4 | void *__cbpush_ptr; 5 | NexTouchEventCb __cb_pop; 6 | void *__cbpop_ptr; 7 | 8 | void NexTouch_attachPush(struct NexObject *touch, NexTouchEventCb push, void *ptr) 9 | { 10 | touch->__cb_push = push; 11 | touch->__cbpush_ptr = ptr; 12 | } 13 | 14 | void NexTouch_detachPush(struct NexObject *touch) 15 | { 16 | touch->__cb_push = 0; 17 | touch->__cbpush_ptr = 0; 18 | } 19 | 20 | void NexTouch_attachPop(struct NexObject *touch, NexTouchEventCb pop, void *ptr) 21 | { 22 | touch->__cb_pop = pop; 23 | touch->__cbpop_ptr = ptr; 24 | } 25 | 26 | void NexTouch_detachPop(struct NexObject *touch) 27 | { 28 | touch->__cb_pop = 0; 29 | touch->__cbpop_ptr = 0; 30 | } 31 | 32 | void NexTouch_push(struct NexObject *touch) 33 | { 34 | if (touch->__cb_push) 35 | { 36 | touch->__cb_push(__cbpush_ptr); 37 | } 38 | } 39 | 40 | void NexTouch_pop(struct NexObject *touch) 41 | { 42 | if (touch->__cb_pop) 43 | { 44 | touch->__cb_pop(__cbpop_ptr); 45 | } 46 | } 47 | 48 | void NexTouch_iterate(struct NexObject **list, uint8_t pid, uint8_t cid, int32_t event) 49 | { 50 | struct NexObject *e = 0; 51 | uint16_t i = 0; 52 | 53 | if (0 == list) 54 | { 55 | return; 56 | } 57 | 58 | for (i = 0; (e = list[i]) != 0; i++) 59 | { 60 | if (e->__pid == pid && e->__cid == cid) 61 | { 62 | //e->printObjInfo(); 63 | if (NEX_EVENT_PUSH == event) 64 | { 65 | if (e->__cb_push) 66 | { 67 | e->__cb_push(e->__cbpush_ptr); 68 | } 69 | } 70 | else if (NEX_EVENT_POP == event) 71 | { 72 | if (e->__cb_pop) 73 | { 74 | e->__cb_pop(e->__cbpop_ptr); 75 | } 76 | } 77 | 78 | break; 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /nextion/Serial.c: -------------------------------------------------------------------------------- 1 | #include "Serial.h" 2 | #include "../util/Utilities.h" 3 | 4 | unsigned char rxBuff[64]; 5 | char rxHead, rxTail; 6 | void UART1interrupt() iv IVT_UART_1 ilevel 6 ics ICS_AUTO 7 | { 8 | rxBuff[rxHead++] = UART1_Read(); // read the received data 9 | U1RXIF_bit = 0; 10 | } 11 | 12 | void Serial_Init(long baudrate) 13 | { 14 | AD1PCFG = 0xFFFF; // Configure AN pins as digital I/O 15 | UART1_Init(9600); 16 | rxHead = 0; 17 | rxTail = 0; 18 | 19 | U1IP0_bit = 0; // Set UART2 interrupt 20 | U1IP1_bit = 1; // Set interrupt priorities 21 | U1IP2_bit = 1; // Set UART2 interrupt to level 6 22 | 23 | U1RXIE_bit = 1; // Set UART Receive Interrupt 24 | U1RXIF_bit = 0; 25 | EnableInterrupts(); // Enable interruts as previously set 26 | } 27 | 28 | unsigned char Serial_Write(unsigned char c) 29 | { 30 | UART1_Write(c); 31 | return 1; 32 | } 33 | 34 | unsigned char Serial_Read() 35 | { 36 | unsigned char c; 37 | if (rxTail < rxHead) 38 | { 39 | c = rxBuff[rxTail++]; 40 | if (rxTail == rxHead) 41 | { 42 | rxHead = 0; 43 | rxTail = 0; 44 | } 45 | } 46 | else 47 | { 48 | rxHead = 0; 49 | rxTail = 0; 50 | c = -1; 51 | } 52 | return c; 53 | } 54 | 55 | unsigned char Serial_Available() 56 | { 57 | return rxHead - rxTail; 58 | } 59 | 60 | unsigned char Serial_ReadBytes(char *buf, unsigned char len) 61 | { 62 | unsigned char cnt = 0; 63 | if (len < rxHead - rxTail) 64 | { 65 | ArrayCopy(buf, &rxBuff[rxTail], len); 66 | rxTail += len; 67 | cnt = len; 68 | } 69 | else if (len == rxHead - rxTail) 70 | { 71 | ArrayCopy(buf, &rxBuff[rxTail], len); 72 | rxTail = 0; 73 | rxHead = 0; 74 | cnt = len; 75 | } 76 | else 77 | { 78 | ArrayCopy(buf, &rxBuff[rxTail], rxHead - rxTail); 79 | cnt = rxHead - rxTail; 80 | rxTail = 0; 81 | rxHead = 0; 82 | } 83 | return cnt; 84 | } 85 | 86 | void Serial_Print(unsigned char *txt) 87 | { 88 | short i = strlen(txt); 89 | while (1) 90 | { 91 | UART1_Write(*txt); 92 | ++txt; 93 | --i; 94 | if (i == 0) 95 | { 96 | break; 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /nextion/NexProgressBar.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file NexProgressBar.cpp 3 | * 4 | * The implementation of class NexProgressBar. 5 | * 6 | * @author Wu Pengfei (email:) 7 | * @date 2015/8/13 8 | * @copyright 9 | * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n 10 | * This program is free software); you can redistribute it and/or 11 | * modify it under the terms of the GNU General Public License as 12 | * published by the Free Software Foundation); either version 2 of 13 | * the License, or (at your option) any later version. 14 | */ 15 | 16 | #include "NexProgressBar.h" 17 | #include 18 | #include 19 | #include 20 | #include "../util/Utilities.h" 21 | 22 | extern char cmd[64]; 23 | extern char buf[12]; 24 | 25 | uint8_t NexProgressBar_getValue(struct NexObject *bar, uint32_t *number) 26 | { 27 | ClearString(cmd); 28 | strcat(cmd, "get "); 29 | strcat(cmd, bar->__name); 30 | strcat(cmd, ".val"); 31 | sendCommand(cmd); 32 | return recvRetNumber(number); 33 | } 34 | 35 | uint8_t NexProgressBar_setValue(struct NexObject *bar, uint32_t number) 36 | { 37 | ClearString(buf); 38 | ClearString(cmd); 39 | 40 | utoa(buf, number, 10); 41 | strcat(cmd, bar->__name); 42 | strcat(cmd, ".val="); 43 | strcat(cmd, buf); 44 | 45 | sendCommand(cmd); 46 | return recvRetCommandFinished(); 47 | } 48 | 49 | uint32_t NexProgressBar_Get_background_color_bco(struct NexObject *bar, uint32_t *number) 50 | { 51 | ClearString(cmd); 52 | strcat(cmd, "get "); 53 | strcat(cmd, bar->__name); 54 | strcat(cmd, ".bco"); 55 | sendCommand(cmd); 56 | return recvRetNumber(number); 57 | } 58 | 59 | uint8_t NexProgressBar_Set_background_color_bco(struct NexObject *bar, uint32_t number) 60 | { 61 | ClearString(buf); 62 | ClearString(cmd); 63 | 64 | utoa(buf, number, 10); 65 | strcat(cmd, bar->__name); 66 | strcat(cmd, ".bco="); 67 | strcat(cmd, buf); 68 | sendCommand(cmd); 69 | 70 | ClearString(cmd); 71 | strcat(cmd, "ref "); 72 | strcat(cmd, bar->__name); 73 | sendCommand(cmd); 74 | return recvRetCommandFinished(); 75 | } 76 | 77 | uint32_t NexProgressBar_Get_font_color_pco(struct NexObject *bar, uint32_t *number) 78 | { 79 | ClearString(cmd); 80 | strcat(cmd, "get "); 81 | strcat(cmd, bar->__name); 82 | strcat(cmd, ".pco"); 83 | sendCommand(cmd); 84 | return recvRetNumber(number); 85 | } 86 | 87 | uint8_t NexProgressBar_Set_font_color_pco(struct NexObject *bar, uint32_t number) 88 | { 89 | ClearString(buf); 90 | ClearString(cmd); 91 | 92 | utoa(buf, number, 10); 93 | strcat(cmd, bar->__name); 94 | strcat(cmd, ".pco="); 95 | strcat(cmd, buf); 96 | sendCommand(cmd); 97 | 98 | ClearString(cmd); 99 | strcat(cmd, "ref "); 100 | strcat(cmd, bar->__name); 101 | sendCommand(cmd); 102 | return recvRetCommandFinished(); 103 | } -------------------------------------------------------------------------------- /nextion/NexSlider.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXSLIDER_H__ 2 | #define __NEXSLIDER_H__ 3 | #include "../util/Utilities.h" 4 | #include "NexTouch.h" 5 | #include "NexHardware.h" 6 | 7 | /** 8 | * Get the value of slider. 9 | * 10 | * @param number - an output parameter to save the value of slider. 11 | * 12 | * @retval true - success. 13 | * @retval false - failed. 14 | */ 15 | uint8_t NexSlider_getValue(struct NexObject *slider, uint32_t *number); 16 | 17 | /** 18 | * Set the value of slider. 19 | * 20 | * @param number - the value of slider. 21 | * 22 | * @retval true - success. 23 | * @retval false - failed. 24 | */ 25 | uint8_t NexSlider_setValue(struct NexObject *slider, uint32_t number); 26 | 27 | /** 28 | * Get bco attribute of component 29 | * 30 | * @param number - buffer storing data retur 31 | * @return the length of the data 32 | */ 33 | uint32_t NexSlider_Get_background_color_bco(struct NexObject *slider, uint32_t *number); 34 | 35 | /** 36 | * Set bco attribute of component 37 | * 38 | * @param number - To set up the data 39 | * @return true if success, false for failure 40 | */ 41 | uint8_t NexSlider_Set_background_color_bco(struct NexObject *slider, uint32_t number); 42 | 43 | /** 44 | * Get pco attribute of component 45 | * 46 | * @param number - buffer storing data retur 47 | * @return the length of the data 48 | */ 49 | uint32_t NexSlider_Get_font_color_pco(struct NexObject *slider, uint32_t *number); 50 | 51 | /** 52 | * Set pco attribute of component 53 | * 54 | * @param number - To set up the data 55 | * @return true if success, false for failure 56 | */ 57 | uint8_t NexSlider_Set_font_color_pco(struct NexObject *slider, uint32_t number); 58 | 59 | /** 60 | * Get wid attribute of component 61 | * 62 | * @param number - buffer storing data retur 63 | * @return the length of the data 64 | */ 65 | uint32_t NexSlider_Get_pointer_thickness_wid(struct NexObject *slider, uint32_t *number); 66 | 67 | /** 68 | * Set wid attribute of component 69 | * 70 | * @param number - To set up the data 71 | * @return true if success, false for failure 72 | */ 73 | uint8_t NexSlider_Set_pointer_thickness_wid(struct NexObject *slider, uint32_t number); 74 | 75 | /** 76 | * Get hig attribute of component 77 | * 78 | * @param number - buffer storing data retur 79 | * @return the length of the data 80 | */ 81 | uint32_t NexSlider_Get_cursor_height_hig(struct NexObject *slider, uint32_t *number); 82 | 83 | /** 84 | * Set hig attribute of component 85 | * 86 | * @param number - To set up the data 87 | * @return true if success, false for failure 88 | */ 89 | uint8_t NexSlider_Set_cursor_height_hig(struct NexObject *slider, uint32_t number); 90 | 91 | /** 92 | * Get maxval attribute of component 93 | * 94 | * @param number - buffer storing data retur 95 | * @return the length of the data 96 | */ 97 | uint32_t NexSlider_getMaxval(struct NexObject *slider, uint32_t *number); 98 | 99 | /** 100 | * Set maxval attribute of component 101 | * 102 | * @param number - To set up the data 103 | * @return true if success, false for failure 104 | */ 105 | uint8_t NexSlider_setMaxval(struct NexObject *slider, uint32_t number); 106 | 107 | /** 108 | * Get minval attribute of component 109 | * 110 | * @param number - buffer storing data retur 111 | * @return the length of the data 112 | */ 113 | uint32_t NexSlider_getMinval(struct NexObject *slider, uint32_t *number); 114 | 115 | /** 116 | * Set minval attribute of component 117 | * 118 | * @param number - To set up the data 119 | * @return true if success, false for failure 120 | */ 121 | uint8_t NexSlider_setMinval(struct NexObject *slider, uint32_t number); 122 | 123 | #endif /* #ifndef __NEXSLIDER_H__ */ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nextion-C-Library 2 | The C Library version of the Nextion Arduino C++ Library 3 | 4 | # Introduction 5 | This is a C implementation of the [Nextion Arduino](https://github.com/itead/ITEADLIB_Arduino_Nextion) Library and was developed and tested on a PIC32MX340F512H microcontroller but the library can be adapted to work on any IDE with any microcontroller. 6 | 7 | # Usage 8 | All the functions that exist in the original library are available here hence I will only focus on how different the implemetnation is with this library. 9 | 10 | ## Initial setup 11 | For starters, you will need to define the following functions in the NexConfig.h file inside the nextion folder: 12 | ```` 13 | #define nexSerial_init(b) Serial_Init(b) 14 | #define nexSerial_available() Serial_Available() 15 | #define nexSerial_read() Serial_Read() 16 | #define nexSerial_write(d) Serial_Write(d) 17 | #define nexSerial_print(p) Serial_Print(p) 18 | #define nexSerial_readBytes(b,l) Serial_ReadBytes(b, l) 19 | #define nexDelay(d) delay_ms(d) 20 | ```` 21 | Most of the functions are self explanatory, a UART driver is included inside the nextion folder to show you how to implement a Serial driver that meets these requirements. 22 | 23 | ## Creating Nextion Objects 24 | In the original arduino Library, you would create an instance of a nextion object in such a manner: 25 | 26 | ```` 27 | NexButton b0 = NexButton(0, 1, "b0"); 28 | ```` 29 | 30 | which allows you to specify what type of component you want to create an instance of. With this library, to create a Nextion object, you would need to define the following structure then invoke the macro that follows to create the object. 31 | 32 | ````` 33 | struct NexObject b0; 34 | CreateNexObject(b0, 0, 1, "b0"); 35 | ````` 36 | 37 | The notable difference here is that all Nextion objects (buttons, sliders etc.) are created with the same instance of the Nextion Object and what will seperate them with the the object name, page id and component id. 38 | 39 | ## Invoking Nextion Objects 40 | As mentioned ealier, all the functions from the original library exist in this library, the invokation is slightly different. 41 | For example, to get the text on the previous button inside the arduino IDE, you would use: 42 | ```` 43 | char buffer[16]; 44 | char len; 45 | b0.getText(buffer,len) 46 | ```` 47 | but with this library, you would use: 48 | ```` 49 | NexButton_getText(&b0, buffer, len); 50 | ```` 51 | 52 | A good observation is that all functions and function parameters exist as they are in the original library. With this library, you don't invoke the functions using the class instance of the Nextion object. To get the text from a button you would use NexButton_getText, to set the text you would use NexButton_setTex(&buttonPointer, XXX). 53 | 54 | In short, ypu need to specify the instance of the nex object with every function, in this library the instance of the object will be the pointer to the NexObject structure. 55 | 56 | # Example Code 57 | ```` 58 | #include "nextion/Nextion.h" 59 | #include "util/Utilities.h" 60 | #include 61 | #include 62 | 63 | struct NexObject button, slider; 64 | 65 | struct NexObject *nex_listen_list[] = { 66 | &button, 67 | &slider}; 68 | 69 | long value; 70 | 71 | #define buttonPageID 0 72 | #define sliderPageID 0 73 | #define buttonID 2 74 | #define sliderID 2 75 | 76 | void main() 77 | { 78 | CreateNexObject(button, buttonPageID, buttonID, "MyButton"); 79 | CreateNexObject(slider, sliderPageID, sliderID, "MySlider"); 80 | 81 | nexInit(); 82 | 83 | NexSlider_getValue(&slider, &value); 84 | NexSlider_setValue(&slider, value + 20); 85 | 86 | NexTouch_attachPop(&button, buttonCallback, 0); 87 | NexTouch_attachPop(&slider, sliderCallback, 0); 88 | 89 | while (1) 90 | { 91 | nexLoop(nex_listen_list); 92 | delay_ms(50); 93 | } 94 | } 95 | 96 | void buttonCallback(void *ptr) 97 | { 98 | //TODO 99 | } 100 | 101 | void sliderCallback(void *ptr) 102 | { 103 | //TODO 104 | } 105 | ```` 106 | 107 | # Does it work? 108 | This library was tested on a confidential project hence details of the project will not be shared, but a compressed video clip of the working display will be shared with you. 109 | [Video Clip](https://youtu.be/4lPtvwyoER4) 110 | -------------------------------------------------------------------------------- /nextion/NexText.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXTEXT_H__ 2 | #define __NEXTEXT_H__ 3 | #include "NexTouch.h" 4 | #include "NexHardware.h" 5 | #include "../util/Utilities.h" 6 | /** 7 | * Get text attribute of component. 8 | * 9 | * @param buffer - buffer storing text returned. 10 | * @param len - length of buffer. 11 | * @return The real length of text returned. 12 | */ 13 | uint16_t NexText_getText(struct NexObject *text, char *buffer, uint16_t len); 14 | 15 | /** 16 | * Set text attribute of component. 17 | * 18 | * @param buffer - text buffer terminated with '\0'. 19 | * @return true if success, false for failure. 20 | */ 21 | uint8_t NexText_setText(struct NexObject *text, char *buffer); 22 | 23 | /** 24 | * Get bco attribute of component 25 | * 26 | * @param number - buffer storing data retur 27 | * @return the length of the data 28 | */ 29 | uint32_t NexText_Get_background_color_bco(struct NexObject *text, uint32_t *number); 30 | 31 | /** 32 | * Set bco attribute of component 33 | * 34 | * @param number - To set up the data 35 | * @return true if success, false for failure 36 | */ 37 | uint8_t NexText_Set_background_color_bco(struct NexObject *text, uint32_t number); 38 | 39 | /** 40 | * Get pco attribute of component 41 | * 42 | * @param number - buffer storing data retur 43 | * @return the length of the data 44 | */ 45 | uint32_t NexText_Get_font_color_pco(struct NexObject *text, uint32_t *number); 46 | 47 | /** 48 | * Set pco attribute of component 49 | * 50 | * @param number - To set up the data 51 | * @return true if success, false for failure 52 | */ 53 | uint8_t NexText_Set_font_color_pco(struct NexObject *text, uint32_t number); 54 | 55 | /** 56 | * Get xcen attribute of component 57 | * 58 | * @param number - buffer storing data retur 59 | * @return the length of the data 60 | */ 61 | uint32_t NexText_Get_place_xcen(struct NexObject *text, uint32_t *number); 62 | 63 | /** 64 | * Set xcen attribute of component 65 | * 66 | * @param number - To set up the data 67 | * @return true if success, false for failure 68 | */ 69 | uint8_t NexText_Set_place_xcen(struct NexObject *text, uint32_t number); 70 | 71 | /** 72 | * Get ycen attribute of component 73 | * 74 | * @param number - buffer storing data retur 75 | * @return the length of the data 76 | */ 77 | uint32_t NexText_Get_place_ycen(struct NexObject *text, uint32_t *number); 78 | 79 | /** 80 | * Set ycen attribute of component 81 | * 82 | * @param number - To set up the data 83 | * @return true if success, false for failure 84 | */ 85 | uint8_t NexText_Set_place_ycen(struct NexObject *text, uint32_t number); 86 | 87 | /** 88 | * Get font attribute of component 89 | * 90 | * @param number - buffer storing data retur 91 | * @return the length of the data 92 | */ 93 | uint32_t NexText_getFont(struct NexObject *text, uint32_t *number); 94 | 95 | /** 96 | * Set font attribute of component 97 | * 98 | * @param number - To set up the data 99 | * @return true if success, false for failure 100 | */ 101 | uint8_t NexText_setFont(struct NexObject *text, uint32_t number); 102 | 103 | /** 104 | * Get picc attribute of component 105 | * 106 | * @param number - buffer storing data retur 107 | * @return the length of the data 108 | */ 109 | uint32_t NexText_Get_background_crop_picc(struct NexObject *text, uint32_t *number); 110 | 111 | /** 112 | * Set picc attribute of component 113 | * 114 | * @param number - To set up the data 115 | * @return true if success, false for failure 116 | */ 117 | uint8_t NexText_Set_background_crop_picc(struct NexObject *text, uint32_t number); 118 | 119 | /** 120 | * Get pic attribute of component 121 | * 122 | * @param number - buffer storing data retur 123 | * @return the length of the data 124 | */ 125 | uint32_t NexText_Get_background_image_pic(struct NexObject *text, uint32_t *number); 126 | 127 | /** 128 | * Set pic attribute of component 129 | * 130 | * @param number - To set up the data 131 | * @return true if success, false for failure 132 | */ 133 | uint8_t NexText_Set_background_image_pic(struct NexObject *text, uint32_t number); 134 | 135 | #endif /* #ifndef __NEXTEXT_H__ */ -------------------------------------------------------------------------------- /nextion/NexNumber.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXNUMBER_H__ 2 | #define __NEXNUMBER_H__ 3 | 4 | #include "NexTouch.h" 5 | #include "NexHardware.h" 6 | 7 | /** 8 | * Get number attribute of component. 9 | * 10 | * @param number - buffer storing text returned. 11 | * @return The real length of text returned. 12 | */ 13 | uint8_t NexNumber_getValue(struct NexObject *number, uint32_t *num); 14 | 15 | /** 16 | * Set number attribute of component. 17 | * 18 | * @param number - number buffer. 19 | * @return true if success, false for failure. 20 | */ 21 | uint8_t NexNumber_setValue(struct NexObject *number, uint32_t num); 22 | 23 | /** 24 | * Get bco attribute of component 25 | * 26 | * @param number - buffer storing data retur 27 | * @return the length of the data 28 | */ 29 | uint32_t NexNumber_Get_background_color_bco(struct NexObject *number, uint32_t *num); 30 | 31 | /** 32 | * Set bco attribute of component 33 | * 34 | * @param number - To set up the data 35 | * @return true if success, false for failure 36 | */ 37 | uint8_t NexNumber_Set_background_color_bco(struct NexObject *number, uint32_t num); 38 | 39 | /** 40 | * Get pco attribute of component 41 | * 42 | * @param number - buffer storing data retur 43 | * @return the length of the data 44 | */ 45 | uint32_t NexNumber_Get_font_color_pco(struct NexObject *number, uint32_t *num); 46 | 47 | /** 48 | * Set pco attribute of component 49 | * 50 | * @param number - To set up the data 51 | * @return true if success, false for failure 52 | */ 53 | uint8_t NexNumber_Set_font_color_pco(struct NexObject *number, uint32_t num); 54 | 55 | /** 56 | * Get xcen attribute of component 57 | * 58 | * @param number - buffer storing data retur 59 | * @return the length of the data 60 | */ 61 | uint32_t NexNumber_Get_place_xcen(struct NexObject *number, uint32_t *num); 62 | 63 | /** 64 | * Set xcen attribute of component 65 | * 66 | * @param number - To set up the data 67 | * @return true if success, false for failure 68 | */ 69 | uint8_t NexNumber_Set_place_xcen(struct NexObject *number, uint32_t num); 70 | 71 | /** 72 | * Get ycen attribute of component 73 | * 74 | * @param number - buffer storing data retur 75 | * @return the length of the data 76 | */ 77 | uint32_t NexNumber_Get_place_ycen(struct NexObject *number, uint32_t *num); 78 | 79 | /** 80 | * Set ycen attribute of component 81 | * 82 | * @param number - To set up the data 83 | * @return true if success, false for failure 84 | */ 85 | uint8_t NexNumber_Set_place_ycen(struct NexObject *number, uint32_t num); 86 | 87 | /** 88 | * Get font attribute of component 89 | * 90 | * @param number - buffer storing data retur 91 | * @return the length of the data 92 | */ 93 | uint32_t NexNumber_getFont(struct NexObject *number, uint32_t *num); 94 | 95 | /** 96 | * Set font attribute of component 97 | * 98 | * @param number - To set up the data 99 | * @return true if success, false for failure 100 | */ 101 | uint8_t NexNumber_setFont(struct NexObject *number, uint32_t num); 102 | 103 | /** 104 | * Get lenth attribute of component 105 | * 106 | * @param number - buffer storing data retur 107 | * @return the length of the data 108 | */ 109 | uint32_t NexNumber_Get_number_lenth(struct NexObject *number, uint32_t *num); 110 | 111 | /** 112 | * Set lenth attribute of component 113 | * 114 | * @param number - To set up the data 115 | * @return true if success, false for failure 116 | */ 117 | uint8_t NexNumber_Set_number_lenth(struct NexObject *number, uint32_t num); 118 | 119 | /** 120 | * Get picc attribute of component 121 | * 122 | * @param number - buffer storing data retur 123 | * @return the length of the data 124 | */ 125 | uint32_t NexNumber_Get_background_crop_picc(struct NexObject *number, uint32_t *num); 126 | 127 | /** 128 | * Set picc attribute of component 129 | * 130 | * @param number - To set up the data 131 | * @return true if success, false for failure 132 | */ 133 | uint8_t NexNumber_Set_background_crop_picc(struct NexObject *number, uint32_t num); 134 | 135 | /** 136 | * Get pic attribute of component 137 | * 138 | * @param number - buffer storing data retur 139 | * @return the length of the data 140 | */ 141 | uint32_t NexNumber_Get_background_image_pic(struct NexObject *number, uint32_t *num); 142 | 143 | /** 144 | * Set pic attribute of component 145 | * 146 | * @param number - To set up the data 147 | * @return true if success, false for failure 148 | */ 149 | uint8_t NexNumber_Set_background_image_pic(struct NexObject *number, uint32_t num); 150 | 151 | #endif /* #ifndef __NEXNUMBER_H__ */ -------------------------------------------------------------------------------- /nextion/NexSlider.c: -------------------------------------------------------------------------------- 1 | #include "NexSlider.h" 2 | #include 3 | #include 4 | #include 5 | #include "../util/Utilities.h" 6 | 7 | extern char buf[12]; 8 | extern char cmd[64]; 9 | 10 | uint8_t NexSlider_getValue(struct NexObject *slider, uint32_t *number) 11 | { 12 | ClearString(cmd); 13 | strcat(cmd, "get "); 14 | strcat(cmd, slider->__name); 15 | strcat(cmd, ".val"); 16 | sendCommand(cmd); 17 | return recvRetNumber(number); 18 | } 19 | 20 | uint8_t NexSlider_setValue(struct NexObject *slider, uint32_t number) 21 | { 22 | ClearString(buf); 23 | ClearString(cmd); 24 | 25 | utoa(buf, number, 10); 26 | strcat(cmd, slider->__name); 27 | strcat(cmd, ".val="); 28 | strcat(cmd, buf); 29 | 30 | sendCommand(cmd); 31 | return recvRetCommandFinished(); 32 | } 33 | 34 | uint32_t NexSlider_Get_background_color_bco(struct NexObject *slider, uint32_t *number) 35 | { 36 | ClearString(cmd); 37 | strcat(cmd, "get "); 38 | strcat(cmd, slider->__name); 39 | strcat(cmd, ".bco"); 40 | sendCommand(cmd); 41 | return recvRetNumber(number); 42 | } 43 | 44 | uint8_t NexSlider_Set_background_color_bco(struct NexObject *slider, uint32_t number) 45 | { 46 | ClearString(buf); 47 | ClearString(cmd); 48 | 49 | utoa(buf, number, 10); 50 | strcat(cmd, slider->__name); 51 | strcat(cmd, ".bco="); 52 | strcat(cmd, buf); 53 | sendCommand(cmd); 54 | 55 | ClearString(cmd); 56 | strcat(cmd, "ref "); 57 | strcat(cmd, slider->__name); 58 | sendCommand(cmd); 59 | return recvRetCommandFinished(); 60 | } 61 | 62 | uint32_t NexSlider_Get_font_color_pco(struct NexObject *slider, uint32_t *number) 63 | { 64 | ClearString(cmd); 65 | strcat(cmd, "get "); 66 | strcat(cmd, slider->__name); 67 | strcat(cmd, ".pco"); 68 | sendCommand(cmd); 69 | return recvRetNumber(number); 70 | } 71 | 72 | uint8_t NexSlider_Set_font_color_pco(struct NexObject *slider, uint32_t number) 73 | { 74 | ClearString(buf); 75 | ClearString(cmd); 76 | 77 | utoa(buf, number, 10); 78 | strcat(cmd, slider->__name); 79 | strcat(cmd, ".pco="); 80 | strcat(cmd, buf); 81 | sendCommand(cmd); 82 | 83 | ClearString(cmd); 84 | strcat(cmd, "ref "); 85 | strcat(cmd, slider->__name); 86 | sendCommand(cmd); 87 | return recvRetCommandFinished(); 88 | } 89 | 90 | uint32_t NexSlider_Get_pointer_thickness_wid(struct NexObject *slider, uint32_t *number) 91 | { 92 | ClearString(cmd); 93 | strcat(cmd, "get "); 94 | strcat(cmd, slider->__name); 95 | strcat(cmd, ".wid"); 96 | sendCommand(cmd); 97 | return recvRetNumber(number); 98 | } 99 | 100 | uint8_t NexSlider_Set_pointer_thickness_wid(struct NexObject *slider, uint32_t number) 101 | { 102 | ClearString(buf); 103 | ClearString(cmd); 104 | 105 | utoa(buf, number, 10); 106 | strcat(cmd, slider->__name); 107 | strcat(cmd, ".wid="); 108 | strcat(cmd, buf); 109 | sendCommand(cmd); 110 | 111 | ClearString(cmd); 112 | strcat(cmd, "ref "); 113 | strcat(cmd, slider->__name); 114 | sendCommand(cmd); 115 | return recvRetCommandFinished(); 116 | } 117 | 118 | uint32_t NexSlider_Get_cursor_height_hig(struct NexObject *slider, uint32_t *number) 119 | { 120 | ClearString(cmd); 121 | strcat(cmd, "get "); 122 | strcat(cmd, slider->__name); 123 | strcat(cmd, ".hig"); 124 | sendCommand(cmd); 125 | return recvRetNumber(number); 126 | } 127 | 128 | uint8_t NexSlider_Set_cursor_height_hig(struct NexObject *slider, uint32_t number) 129 | { 130 | ClearString(buf); 131 | ClearString(cmd); 132 | 133 | utoa(buf, number, 10); 134 | strcat(cmd, slider->__name); 135 | strcat(cmd, ".hig="); 136 | strcat(cmd, buf); 137 | sendCommand(cmd); 138 | 139 | ClearString(cmd); 140 | strcat(cmd, "ref "); 141 | strcat(cmd, slider->__name); 142 | sendCommand(cmd); 143 | return recvRetCommandFinished(); 144 | } 145 | 146 | uint32_t NexSlider_getMaxval(struct NexObject *slider, uint32_t *number) 147 | { 148 | ClearString(cmd); 149 | strcat(cmd, "get "); 150 | strcat(cmd, slider->__name); 151 | strcat(cmd, ".maxval"); 152 | sendCommand(cmd); 153 | return recvRetNumber(number); 154 | } 155 | 156 | uint8_t NexSlider_setMaxval(struct NexObject *slider, uint32_t number) 157 | { 158 | ClearString(buf); 159 | ClearString(cmd); 160 | 161 | utoa(buf, number, 10); 162 | strcat(cmd, slider->__name); 163 | strcat(cmd, ".maxval="); 164 | strcat(cmd, buf); 165 | sendCommand(cmd); 166 | 167 | ClearString(cmd); 168 | strcat(cmd, "ref "); 169 | strcat(cmd, slider->__name); 170 | sendCommand(cmd); 171 | return recvRetCommandFinished(); 172 | } 173 | 174 | uint32_t NexSlider_getMinval(struct NexObject *slider, uint32_t *number) 175 | { 176 | ClearString(cmd); 177 | strcat(cmd, "get "); 178 | strcat(cmd, slider->__name); 179 | strcat(cmd, ".minval"); 180 | sendCommand(cmd); 181 | return recvRetNumber(number); 182 | } 183 | 184 | uint8_t NexSlider_setMinval(struct NexObject *slider, uint32_t number) 185 | { 186 | ClearString(buf); 187 | ClearString(cmd); 188 | 189 | utoa(buf, number, 10); 190 | strcat(cmd, slider->__name); 191 | strcat(cmd, ".minval="); 192 | strcat(cmd, buf); 193 | sendCommand(cmd); 194 | 195 | ClearString(cmd); 196 | strcat(cmd, "ref "); 197 | strcat(cmd, slider->__name); 198 | sendCommand(cmd); 199 | return recvRetCommandFinished(); 200 | } -------------------------------------------------------------------------------- /nextion/NexText.c: -------------------------------------------------------------------------------- 1 | #include "NexText.h" 2 | #include 3 | #include 4 | #include 5 | #include "../util/Utilities.h" 6 | 7 | extern char buf[12]; 8 | extern char cmd[64]; 9 | 10 | uint16_t NexText_getText(struct NexObject *text, char *buffer, uint16_t len) 11 | { 12 | ClearString(cmd); 13 | strcat(cmd, "get "); 14 | strcat(cmd, text->__name); 15 | strcat(cmd, ".txt"); 16 | sendCommand(cmd); 17 | return recvRetString(buffer, len); 18 | } 19 | 20 | uint8_t NexText_setText(struct NexObject *text, char *buffer) 21 | { 22 | ClearString(cmd); 23 | strcat(cmd, text->__name); 24 | strcat(cmd, ".txt=\""); 25 | strcat(cmd, buffer); 26 | strcat(cmd, "\""); 27 | sendCommand(cmd); 28 | return recvRetCommandFinished(); 29 | } 30 | 31 | uint32_t NexText_Get_background_color_bco(struct NexObject *text, uint32_t *number) 32 | { 33 | ClearString(cmd); 34 | strcat(cmd, "get "); 35 | strcat(cmd, text->__name); 36 | strcat(cmd, ".bco"); 37 | sendCommand(cmd); 38 | return recvRetNumber(number); 39 | } 40 | 41 | uint8_t NexText_Set_background_color_bco(struct NexObject *text, uint32_t number) 42 | { 43 | ClearString(buf); 44 | ClearString(cmd); 45 | 46 | utoa(buf, number, 10); 47 | strcat(cmd, text->__name); 48 | strcat(cmd, ".bco="); 49 | strcat(cmd, buf); 50 | sendCommand(cmd); 51 | 52 | ClearString(cmd); 53 | strcat(cmd, "ref "); 54 | strcat(cmd, text->__name); 55 | sendCommand(cmd); 56 | return recvRetCommandFinished(); 57 | } 58 | 59 | uint32_t NexText_Get_font_color_pco(struct NexObject *text, uint32_t *number) 60 | { 61 | ClearString(cmd); 62 | strcat(cmd, "get "); 63 | strcat(cmd, text->__name); 64 | strcat(cmd, ".pco"); 65 | sendCommand(cmd); 66 | return recvRetNumber(number); 67 | } 68 | 69 | uint8_t NexText_Set_font_color_pco(struct NexObject *text, uint32_t number) 70 | { 71 | ClearString(buf); 72 | ClearString(cmd); 73 | 74 | utoa(buf, number, 10); 75 | strcat(cmd, text->__name); 76 | strcat(cmd, ".pco="); 77 | strcat(cmd, buf); 78 | sendCommand(cmd); 79 | 80 | ClearString(cmd); 81 | strcat(cmd, "ref "); 82 | strcat(cmd, text->__name); 83 | sendCommand(cmd); 84 | return recvRetCommandFinished(); 85 | } 86 | 87 | uint32_t NexText_Get_place_xcen(struct NexObject *text, uint32_t *number) 88 | { 89 | ClearString(cmd); 90 | strcat(cmd, "get "); 91 | strcat(cmd, text->__name); 92 | strcat(cmd, ".xcen"); 93 | sendCommand(cmd); 94 | return recvRetNumber(number); 95 | } 96 | 97 | uint8_t NexText_Set_place_xcen(struct NexObject *text, uint32_t number) 98 | { 99 | ClearString(buf); 100 | ClearString(cmd); 101 | 102 | utoa(buf, number, 10); 103 | strcat(cmd, text->__name); 104 | strcat(cmd, ".xcen="); 105 | strcat(cmd, buf); 106 | sendCommand(cmd); 107 | 108 | ClearString(cmd); 109 | strcat(cmd, "ref "); 110 | strcat(cmd, text->__name); 111 | sendCommand(cmd); 112 | return recvRetCommandFinished(); 113 | } 114 | 115 | uint32_t NexText_Get_place_ycen(struct NexObject *text, uint32_t *number) 116 | { 117 | ClearString(cmd); 118 | strcat(cmd, "get "); 119 | strcat(cmd, text->__name); 120 | strcat(cmd, ".ycen"); 121 | sendCommand(cmd); 122 | return recvRetNumber(number); 123 | } 124 | 125 | uint8_t NexText_Set_place_ycen(struct NexObject *text, uint32_t number) 126 | { 127 | ClearString(buf); 128 | ClearString(cmd); 129 | 130 | utoa(buf, number, 10); 131 | strcat(cmd, text->__name); 132 | strcat(cmd, ".ycen="); 133 | strcat(cmd, buf); 134 | sendCommand(cmd); 135 | 136 | ClearString(cmd); 137 | strcat(cmd, "ref "); 138 | strcat(cmd, text->__name); 139 | sendCommand(cmd); 140 | return recvRetCommandFinished(); 141 | } 142 | 143 | uint32_t NexText_getFont(struct NexObject *text, uint32_t *number) 144 | { 145 | ClearString(cmd); 146 | strcat(cmd, "get "); 147 | strcat(cmd, text->__name); 148 | strcat(cmd, ".font"); 149 | sendCommand(cmd); 150 | return recvRetNumber(number); 151 | } 152 | 153 | uint8_t NexText_setFont(struct NexObject *text, uint32_t number) 154 | { 155 | ClearString(buf); 156 | ClearString(cmd); 157 | 158 | utoa(buf, number, 10); 159 | strcat(cmd, text->__name); 160 | strcat(cmd, ".font="); 161 | strcat(cmd, buf); 162 | sendCommand(cmd); 163 | 164 | ClearString(cmd); 165 | strcat(cmd, "ref "); 166 | strcat(cmd, text->__name); 167 | sendCommand(cmd); 168 | return recvRetCommandFinished(); 169 | } 170 | 171 | uint32_t NexText_Get_background_crop_picc(struct NexObject *text, uint32_t *number) 172 | { 173 | ClearString(cmd); 174 | strcat(cmd, "get "); 175 | strcat(cmd, text->__name); 176 | strcat(cmd, ".picc"); 177 | sendCommand(cmd); 178 | return recvRetNumber(number); 179 | } 180 | 181 | uint8_t NexText_Set_background_crop_picc(struct NexObject *text, uint32_t number) 182 | { 183 | ClearString(buf); 184 | ClearString(cmd); 185 | 186 | utoa(buf, number, 10); 187 | strcat(cmd, text->__name); 188 | strcat(cmd, ".picc="); 189 | strcat(cmd, buf); 190 | sendCommand(cmd); 191 | 192 | ClearString(cmd); 193 | strcat(cmd, "ref "); 194 | strcat(cmd, text->__name); 195 | sendCommand(cmd); 196 | return recvRetCommandFinished(); 197 | } 198 | 199 | uint32_t NexText_Get_background_image_pic(struct NexObject *text, uint32_t *number) 200 | { 201 | ClearString(cmd); 202 | strcat(cmd, "get "); 203 | strcat(cmd, text->__name); 204 | strcat(cmd, ".pic"); 205 | sendCommand(cmd); 206 | return recvRetNumber(number); 207 | } 208 | 209 | uint8_t NexText_Set_background_image_pic(struct NexObject *text, uint32_t number) 210 | { 211 | ClearString(buf); 212 | ClearString(cmd); 213 | 214 | utoa(buf, number, 10); 215 | strcat(cmd, text->__name); 216 | strcat(cmd, ".pic="); 217 | strcat(cmd, buf); 218 | 219 | sendCommand(cmd); 220 | return recvRetCommandFinished(); 221 | } -------------------------------------------------------------------------------- /nextion/NexButton.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEXBUTTON_H__ 2 | #define __NEXBUTTON_H__ 3 | 4 | #include "NexTouch.h" 5 | #include "NexHardware.h" 6 | #include "../util/my_types.h" 7 | 8 | uint16_t NexButton_getText(struct NexObject *button, char *buffer, uint16_t len); 9 | 10 | /** 11 | * Set text attribute of component. 12 | * 13 | * @param buffer - text buffer terminated with '\0'. 14 | * @return true if success, false for failure. 15 | */ 16 | uint8_t NexButton_setText(struct NexObject *button, const char *buffer); 17 | 18 | /** 19 | * Get bco attribute of component 20 | * 21 | * @param number - buffer storing data return 22 | * @return the length of the data 23 | */ 24 | uint32_t NexButton_Get_background_color_bco(struct NexObject *button, uint32_t *number); 25 | 26 | /** 27 | * Set bco attribute of component 28 | * 29 | * @param number - To set up the data 30 | * @return true if success, false for failure 31 | */ 32 | uint8_t NexButton_Set_background_color_bco(struct NexObject *button, uint32_t number); 33 | 34 | /** 35 | * Get bco2 attribute of component 36 | * 37 | * @param number - buffer storing data return 38 | * @return the length of the data 39 | */ 40 | uint32_t NexButton_Get_press_background_color_bco2(struct NexObject *button, uint32_t *number); 41 | 42 | /** 43 | * Set bco2 attribute of component 44 | * 45 | * @param number - To set up the data 46 | * @return true if success, false for failure 47 | */ 48 | uint8_t NexButton_Set_press_background_color_bco2(struct NexObject *button, uint32_t number); 49 | 50 | /** 51 | * Get pco attribute of component 52 | * 53 | * @param number - buffer storing data return 54 | * @return the length of the data 55 | */ 56 | uint32_t NexButton_Get_font_color_pco(struct NexObject *button, uint32_t *number); 57 | 58 | /** 59 | * Set pco attribute of component 60 | * 61 | * @param number - To set up the data 62 | * @return true if success, false for failure 63 | */ 64 | uint8_t NexButton_Set_font_color_pco(struct NexObject *button, uint32_t number); 65 | 66 | /** 67 | * Get pco2 attribute of component 68 | * 69 | * @param number - buffer storing data return 70 | * @return the length of the data 71 | */ 72 | uint32_t NexButton_Get_press_font_color_pco2(struct NexObject *button, uint32_t *number); 73 | 74 | /** 75 | * Set pco2 attribute of component 76 | * 77 | * @param number - To set up the data 78 | * @return true if success, false for failure 79 | */ 80 | uint8_t NexButton_Set_press_font_color_pco2(struct NexObject *button, uint32_t number); 81 | 82 | /** 83 | * Get xcen attribute of component 84 | * 85 | * @param number - buffer storing data return 86 | * @return the length of the data 87 | */ 88 | uint32_t NexButton_Get_place_xcen(struct NexObject *button, uint32_t *number); 89 | 90 | /** 91 | * Set xcen attribute of component 92 | * 93 | * @param number - To set up the data 94 | * @return true if success, false for failure 95 | */ 96 | uint8_t NexButton_Set_place_xcen(struct NexObject *button, uint32_t number); 97 | 98 | /** 99 | * Get ycen attribute of component 100 | * 101 | * @param number - buffer storing data return 102 | * @return the length of the data 103 | */ 104 | uint32_t NexButton_Get_place_ycen(struct NexObject *button, uint32_t *number); 105 | 106 | /** 107 | * Set ycen attribute of component 108 | * 109 | * @param number - To set up the data 110 | * @return true if success, false for failure 111 | */ 112 | uint8_t NexButton_Set_place_ycen(struct NexObject *button, uint32_t number); 113 | 114 | /** 115 | * Get font attribute of component 116 | * 117 | * @param number - buffer storing data return 118 | * @return the length of the data 119 | */ 120 | uint32_t NexButton_getFont(struct NexObject *button, uint32_t *number); 121 | 122 | /** 123 | * Set font attribute of component 124 | * 125 | * @param number - To set up the data 126 | * @return true if success, false for failure 127 | */ 128 | uint8_t NexButton_setFont(struct NexObject *button, uint32_t number); 129 | 130 | /** 131 | * Get picc attribute of component 132 | * 133 | * @param number - buffer storing data return 134 | * @return the length of the data 135 | */ 136 | uint32_t NexButton_Get_background_cropi_picc(struct NexObject *button, uint32_t *number); 137 | 138 | /** 139 | * Set picc attribute of component 140 | * 141 | * @param number - To set up the data 142 | * @return true if success, false for failure 143 | */ 144 | uint8_t NexButton_Set_background_crop_picc(struct NexObject *button, uint32_t number); 145 | 146 | /** 147 | * Get picc2 attribute of component 148 | * 149 | * @param number - buffer storing data return 150 | * @return the length of the data 151 | */ 152 | uint32_t NexButton_Get_press_background_crop_picc2(struct NexObject *button, uint32_t *number); 153 | 154 | /** 155 | * Set picc2 attribute of component 156 | * 157 | * @param number - To set up the data 158 | * @return true if success, false for failure 159 | */ 160 | uint8_t NexButton_Set_press_background_crop_picc2(struct NexObject *button, uint32_t number); 161 | 162 | /** 163 | * Get pic attribute of component 164 | * 165 | * @param number - buffer storing data return 166 | * @return the length of the data 167 | */ 168 | uint32_t NexButton_Get_background_image_pic(struct NexObject *button, uint32_t *number); 169 | 170 | /** 171 | * Set pic attribute of component 172 | * 173 | * @param number - To set up the data 174 | * @return true if success, false for failure 175 | */ 176 | uint8_t NexButton_Set_background_image_pic(struct NexObject *button, uint32_t number); 177 | 178 | /** 179 | * Get pic2 attribute of component 180 | * 181 | * @param number - buffer storing data return 182 | * @return the length of the data 183 | */ 184 | uint32_t NexButton_Get_press_background_image_pic2(struct NexObject *button, uint32_t *number); 185 | 186 | /** 187 | * Set pic2 attribute of component 188 | * 189 | * @param number - To set up the data 190 | * @return true if success, false for failure 191 | */ 192 | uint8_t NexButton_Set_press_background_image_pic2(struct NexObject *button, uint32_t number); 193 | 194 | #endif /* #ifndef __NEXBUTTON_H__ */ 195 | -------------------------------------------------------------------------------- /nextion/NexHardware.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file NexHardware.cpp 3 | * 4 | * The implementation of base API for using Nextion. 5 | * 6 | * @author Wu Pengfei (email:) 7 | * @date 2015/8/11 8 | * @copyright 9 | * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n 10 | * This program is free software; you can redistribute it and/or 11 | * modify it under the terms of the GNU General Public License as 12 | * published by the Free Software Foundation; either version 2 of 13 | * the License, or (at your option) any later version. 14 | */ 15 | #include "NexHardware.h" 16 | #include "../util/Utilities.h" 17 | 18 | #define NEX_RET_CMD_FINISHED (0x01) 19 | #define NEX_RET_EVENT_LAUNCHED (0x88) 20 | #define NEX_RET_EVENT_UPGRADED (0x89) 21 | #define NEX_RET_EVENT_TOUCH_HEAD (0x65) 22 | #define NEX_RET_EVENT_POSITION_HEAD (0x67) 23 | #define NEX_RET_EVENT_SLEEP_POSITION_HEAD (0x68) 24 | #define NEX_RET_CURRENT_PAGE_ID_HEAD (0x66) 25 | #define NEX_RET_STRING_HEAD (0x70) 26 | #define NEX_RET_NUMBER_HEAD (0x71) 27 | #define NEX_RET_INVALID_CMD (0x00) 28 | #define NEX_RET_INVALID_COMPONENT_ID (0x02) 29 | #define NEX_RET_INVALID_PAGE_ID (0x03) 30 | #define NEX_RET_INVALID_PICTURE_ID (0x04) 31 | #define NEX_RET_INVALID_FONT_ID (0x05) 32 | #define NEX_RET_INVALID_BAUD (0x11) 33 | #define NEX_RET_INVALID_VARIABLE (0x1A) 34 | #define NEX_RET_INVALID_OPERATION (0x1B) 35 | 36 | char txt[32]; 37 | char cmd[64]; 38 | char buf[12]; 39 | char msg[32]; 40 | unsigned char wrData; 41 | 42 | /* 43 | * Receive uint32_t data. 44 | * 45 | * @param number - save uint32_t data. 46 | * @param 100 - set 100 time. 47 | * 48 | * @retval 1 - success. 49 | * @retval 0 - failed. 50 | * 51 | */ 52 | uint8_t recvRetNumber(uint32_t *number) 53 | { 54 | uint8_t ret = 0; 55 | char temp[8]; 56 | 57 | if (!number) 58 | { 59 | goto __return; 60 | } 61 | 62 | nexDelay(100); 63 | if (8 != nexSerial_readBytes((char *)temp, 8)) 64 | { 65 | goto __return; 66 | } 67 | 68 | if (temp[0] == NEX_RET_NUMBER_HEAD && temp[5] == 0xFF && temp[6] == 0xFF && temp[7] == 0xFF) 69 | { 70 | *number = ((uint32_t)temp[4] << 24) | ((uint32_t)temp[3] << 16) | (temp[2] << 8) | (temp[1]); 71 | ret = 1; 72 | } 73 | 74 | __return: 75 | 76 | /*if (ret) 77 | { 78 | dbSerialPrint("recvRetNumber :"); 79 | dbSerialPrintln(*number); 80 | } 81 | else 82 | { 83 | dbSerialPrintln("recvRetNumber err"); 84 | }*/ 85 | 86 | return ret; 87 | } 88 | 89 | /* 90 | * Receive string data. 91 | * 92 | * @param buffer - save string data. 93 | * @param len - string buffer length. 94 | * @param 100 - set 100 time. 95 | * 96 | * @return the length of string buffer. 97 | * 98 | */ 99 | uint16_t recvRetString(char *buffer, uint16_t len) 100 | { 101 | uint16_t ret = 0; 102 | uint8_t str_start_flag = 0; 103 | uint8_t cnt_0xff = 0; 104 | char arr[32]; 105 | char *temp = arr; 106 | uint8_t c = 0; 107 | long start; 108 | 109 | if (!buffer || len == 0) 110 | { 111 | goto __return; 112 | } 113 | 114 | start = 500; 115 | while (start) 116 | { 117 | while (nexSerial_available()) 118 | { 119 | c = nexSerial_read(); 120 | if (str_start_flag) 121 | { 122 | if (0xFF == c) 123 | { 124 | cnt_0xff++; 125 | if (cnt_0xff >= 3) 126 | { 127 | break; 128 | } 129 | } 130 | else 131 | { 132 | temp += (char)c; 133 | } 134 | } 135 | else if (NEX_RET_STRING_HEAD == c) 136 | { 137 | str_start_flag = 1; 138 | } 139 | } 140 | 141 | if (cnt_0xff >= 3) 142 | { 143 | break; 144 | } 145 | --start; 146 | } 147 | 148 | ret = ArrayLength(temp); 149 | ret = ret > len ? len : ret; 150 | strncpy(buffer, temp, ret); 151 | 152 | __return: 153 | /* 154 | dbSerialPrint("recvRetString["); 155 | dbSerialPrint(temp.length()); 156 | dbSerialPrint(","); 157 | dbSerialPrint(temp); 158 | dbSerialPrintln("]"); 159 | */ 160 | return ret; 161 | } 162 | 163 | /* 164 | * Command is executed successfully. 165 | * 166 | * @param 100 - set 100 time. 167 | * 168 | * @retval 1 - success. 169 | * @retval 0 - failed. 170 | * 171 | */ 172 | uint8_t recvRetCommandFinished() 173 | { 174 | uint8_t ret = 0; 175 | uint8_t temp[4] = {0}; 176 | // nexSerial.set100(100); 177 | if (ArrayLength(temp) != nexSerial_readBytes((char *)temp, ArrayLength(temp))) 178 | { 179 | ret = 0; 180 | } 181 | 182 | if (temp[0] == NEX_RET_CMD_FINISHED && temp[1] == 0xFF && temp[2] == 0xFF && temp[3] == 0xFF) 183 | { 184 | ret = 1; 185 | } 186 | /* 187 | if (ret) 188 | { 189 | dbSerialPrintln("recvRetCommandFinished ok"); 190 | } 191 | else 192 | { 193 | dbSerialPrintln("recvRetCommandFinished err"); 194 | } 195 | */ 196 | return ret; 197 | } 198 | 199 | void sendCommand(char *command) 200 | { 201 | nexSerial_print(command); 202 | nexSerial_write(0xFF); 203 | nexSerial_write(0xFF); 204 | nexSerial_write(0xFF); 205 | } 206 | 207 | uint8_t nexInit(void) 208 | { 209 | uint8_t ret1 = 0; 210 | uint8_t ret2 = 0; 211 | 212 | //dbSerialBegin(9600); 213 | nexSerial_init(9600); 214 | sendCommand(""); 215 | sendCommand("bkcmd=1"); 216 | ret1 = recvRetCommandFinished(); 217 | sendCommand("page 0"); 218 | ret2 = recvRetCommandFinished(); 219 | return ret1 && ret2; 220 | } 221 | 222 | void nexLoop(struct NexObject *nex_listen_list[]) 223 | { 224 | static uint8_t __buffer[10]; 225 | 226 | uint16_t i; 227 | uint8_t limit = 20; 228 | uint8_t c; 229 | 230 | while (nexSerial_available() > 0) 231 | { 232 | nexDelay(10); 233 | c = nexSerial_read(); 234 | 235 | if (NEX_RET_EVENT_TOUCH_HEAD == c) 236 | { 237 | if (nexSerial_available() >= 6) 238 | { 239 | __buffer[0] = c; 240 | for (i = 1; i < 7; i++) 241 | { 242 | __buffer[i] = nexSerial_read(); 243 | } 244 | __buffer[i] = 0x00; 245 | 246 | if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6]) 247 | { 248 | NexTouch_iterate(nex_listen_list, __buffer[1], __buffer[2], (int32_t)__buffer[3]); 249 | } 250 | } 251 | } 252 | } 253 | } -------------------------------------------------------------------------------- /nextion/NexNumber.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file NexNumber.cpp 3 | * 4 | * The implementation of class NexNumber. 5 | * 6 | * @author huang xianming (email:) 7 | * @date 2015/8/13 8 | * @copyright 9 | * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n 10 | * This program is free software); you can redistribute it and/or 11 | * modify it under the terms of the GNU General Public License as 12 | * published by the Free Software Foundation); either version 2 of 13 | * the License, or (at your option) any later version. 14 | */ 15 | #include "NexNumber.h" 16 | #include 17 | #include 18 | #include 19 | #include "../util/Utilities.h" 20 | 21 | extern char cmd[64]; 22 | extern char buf[12]; 23 | 24 | uint8_t NexNumber_getValue(struct NexObject *number, uint32_t *num) 25 | { 26 | ClearString(cmd); 27 | strcat(cmd, "get "); 28 | strcat(cmd, number->__name); 29 | strcat(cmd, ".val"); 30 | sendCommand(cmd); 31 | return recvRetNumber(num); 32 | } 33 | 34 | uint8_t NexNumber_setValue(struct NexObject *number, uint32_t num) 35 | { 36 | ClearString(buf); 37 | ClearString(cmd); 38 | 39 | utoa(buf, num, 10); 40 | strcat(cmd, number->__name); 41 | strcat(cmd, ".val="); 42 | strcat(cmd, buf); 43 | 44 | sendCommand(cmd); 45 | return recvRetCommandFinished(); 46 | } 47 | 48 | uint32_t NexNumber_Get_background_color_bco(struct NexObject *number, uint32_t *num) 49 | { 50 | ClearString(cmd); 51 | strcat(cmd, "get "); 52 | strcat(cmd, number->__name); 53 | strcat(cmd, ".bco"); 54 | sendCommand(cmd); 55 | return recvRetNumber(num); 56 | } 57 | 58 | uint8_t NexNumber_Set_background_color_bco(struct NexObject *number, uint32_t num) 59 | { 60 | ClearString(buf); 61 | ClearString(cmd); 62 | 63 | utoa(buf, num, 10); 64 | strcat(cmd, number->__name); 65 | strcat(cmd, ".bco="); 66 | strcat(cmd, buf); 67 | sendCommand(cmd); 68 | 69 | ClearString(cmd); 70 | strcat(cmd, "ref "); 71 | strcat(cmd, number->__name); 72 | sendCommand(cmd); 73 | return recvRetCommandFinished(); 74 | } 75 | 76 | uint32_t NexNumber_Get_font_color_pco(struct NexObject *number, uint32_t *num) 77 | { 78 | ClearString(cmd); 79 | strcat(cmd, "get "); 80 | strcat(cmd, number->__name); 81 | strcat(cmd, ".pco"); 82 | sendCommand(cmd); 83 | return recvRetNumber(num); 84 | } 85 | 86 | uint8_t NexNumber_Set_font_color_pco(struct NexObject *number, uint32_t num) 87 | { 88 | ClearString(buf); 89 | ClearString(cmd); 90 | 91 | utoa(buf, num, 10); 92 | strcat(cmd, number->__name); 93 | strcat(cmd, ".pco="); 94 | strcat(cmd, buf); 95 | sendCommand(cmd); 96 | 97 | ClearString(cmd); 98 | strcat(cmd, "ref "); 99 | strcat(cmd, number->__name); 100 | sendCommand(cmd); 101 | return recvRetCommandFinished(); 102 | } 103 | 104 | uint32_t NexNumber_Get_place_xcen(struct NexObject *number, uint32_t *num) 105 | { 106 | ClearString(cmd); 107 | strcat(cmd, "get "); 108 | strcat(cmd, number->__name); 109 | strcat(cmd, ".xcen"); 110 | sendCommand(cmd); 111 | return recvRetNumber(num); 112 | } 113 | 114 | uint8_t NexNumber_Set_place_xcen(struct NexObject *number, uint32_t num) 115 | { 116 | ClearString(buf); 117 | ClearString(cmd); 118 | 119 | utoa(buf, num, 10); 120 | strcat(cmd, number->__name); 121 | strcat(cmd, ".xcen="); 122 | strcat(cmd, buf); 123 | sendCommand(cmd); 124 | 125 | ClearString(cmd); 126 | strcat(cmd, "ref "); 127 | strcat(cmd, number->__name); 128 | sendCommand(cmd); 129 | return recvRetCommandFinished(); 130 | } 131 | 132 | uint32_t NexNumber_Get_place_ycen(struct NexObject *number, uint32_t *num) 133 | { 134 | ClearString(cmd); 135 | strcat(cmd, "get "); 136 | strcat(cmd, number->__name); 137 | strcat(cmd, ".ycen"); 138 | sendCommand(cmd); 139 | return recvRetNumber(num); 140 | } 141 | 142 | uint8_t NexNumber_Set_place_ycen(struct NexObject *number, uint32_t num) 143 | { 144 | ClearString(buf); 145 | ClearString(cmd); 146 | 147 | utoa(buf, num, 10); 148 | strcat(cmd, number->__name); 149 | strcat(cmd, ".ycen="); 150 | strcat(cmd, buf); 151 | sendCommand(cmd); 152 | 153 | ClearString(cmd); 154 | strcat(cmd, "ref "); 155 | strcat(cmd, number->__name); 156 | sendCommand(cmd); 157 | return recvRetCommandFinished(); 158 | } 159 | 160 | uint32_t NexNumber_getFont(struct NexObject *number, uint32_t *num) 161 | { 162 | ClearString(cmd); 163 | strcat(cmd, "get "); 164 | strcat(cmd, number->__name); 165 | strcat(cmd, ".font"); 166 | sendCommand(cmd); 167 | return recvRetNumber(num); 168 | } 169 | 170 | uint8_t NexNumber_setFont(struct NexObject *number, uint32_t num) 171 | { 172 | ClearString(buf); 173 | ClearString(cmd); 174 | 175 | utoa(buf, num, 10); 176 | strcat(cmd, number->__name); 177 | strcat(cmd, ".font="); 178 | strcat(cmd, buf); 179 | sendCommand(cmd); 180 | 181 | ClearString(cmd); 182 | strcat(cmd, "ref "); 183 | strcat(cmd, number->__name); 184 | sendCommand(cmd); 185 | return recvRetCommandFinished(); 186 | } 187 | 188 | uint32_t NexNumber_Get_number_lenth(struct NexObject *number, uint32_t *num) 189 | { 190 | ClearString(cmd); 191 | strcat(cmd, "get "); 192 | strcat(cmd, number->__name); 193 | strcat(cmd, ".lenth"); 194 | sendCommand(cmd); 195 | return recvRetNumber(num); 196 | } 197 | 198 | uint8_t NexNumber_Set_number_lenth(struct NexObject *number, uint32_t num) 199 | { 200 | ClearString(buf); 201 | ClearString(cmd); 202 | 203 | utoa(buf, num, 10); 204 | strcat(cmd, number->__name); 205 | strcat(cmd, ".lenth="); 206 | strcat(cmd, buf); 207 | sendCommand(cmd); 208 | 209 | ClearString(cmd); 210 | strcat(cmd, "ref "); 211 | strcat(cmd, number->__name); 212 | sendCommand(cmd); 213 | return recvRetCommandFinished(); 214 | } 215 | 216 | uint32_t NexNumber_Get_background_crop_picc(struct NexObject *number, uint32_t *num) 217 | { 218 | ClearString(cmd); 219 | strcat(cmd, "get "); 220 | strcat(cmd, number->__name); 221 | strcat(cmd, ".picc"); 222 | sendCommand(cmd); 223 | return recvRetNumber(num); 224 | } 225 | 226 | uint8_t NexNumber_Set_background_crop_picc(struct NexObject *number, uint32_t num) 227 | { 228 | ClearString(buf); 229 | ClearString(cmd); 230 | 231 | utoa(buf, num, 10); 232 | strcat(cmd, number->__name); 233 | strcat(cmd, ".picc="); 234 | strcat(cmd, buf); 235 | sendCommand(cmd); 236 | 237 | ClearString(cmd); 238 | strcat(cmd, "ref "); 239 | strcat(cmd, number->__name); 240 | sendCommand(cmd); 241 | return recvRetCommandFinished(); 242 | } 243 | 244 | uint32_t NexNumber_Get_background_image_pic(struct NexObject *number, uint32_t *num) 245 | { 246 | char cmd[32] = "get "; 247 | strcat(cmd, number->__name); 248 | strcat(cmd, ".pic"); 249 | sendCommand(cmd); 250 | return recvRetNumber(num); 251 | } 252 | 253 | uint8_t NexNumber_Set_background_image_pic(struct NexObject *number, uint32_t num) 254 | { 255 | ClearString(buf); 256 | ClearString(cmd); 257 | 258 | utoa(buf, num, 10); 259 | strcat(cmd, number->__name); 260 | strcat(cmd, ".pic="); 261 | strcat(cmd, buf); 262 | sendCommand(cmd); 263 | 264 | ClearString(cmd); 265 | strcat(cmd, "ref "); 266 | strcat(cmd, number->__name); 267 | sendCommand(cmd); 268 | return recvRetCommandFinished(); 269 | } -------------------------------------------------------------------------------- /nextion/NexButton.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file NexButton.cpp 3 | * 4 | * The implementation of class NexButton. 5 | * 6 | * @author Wu Pengfei (email:) 7 | * @date 2015/8/13 8 | * @copyright 9 | * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n 10 | * This program is free software); you can redistribute it and/or 11 | * modify it under the terms of the GNU General Public License as 12 | * published by the Free Software Foundation); either version 2 of 13 | * the License, or (at your option) any later version. 14 | */ 15 | 16 | #include "NexButton.h" 17 | #include 18 | #include 19 | #include 20 | #include "../util/Utilities.h" 21 | 22 | extern char cmd[64]; 23 | extern char buf[12]; 24 | 25 | uint16_t NexButton_getText(struct NexObject *button, char *buffer, uint16_t len) 26 | { 27 | ClearString(cmd); 28 | strcat(cmd, "get "); 29 | strcat(cmd, button->__name); 30 | strcat(cmd, ".txt"); 31 | sendCommand(cmd); 32 | return recvRetString(buffer, len); 33 | } 34 | 35 | uint8_t NexButton_setText(struct NexObject *button, const char *buffer) 36 | { 37 | ClearString(cmd); 38 | strcat(cmd, button->__name); 39 | strcat(cmd, ".txt=\""); 40 | strcat(cmd, (char*)buffer); 41 | strcat(cmd, "\""); 42 | sendCommand(cmd); 43 | return recvRetCommandFinished(); 44 | } 45 | 46 | uint32_t NexButton_Get_background_color_bco(struct NexObject *button, uint32_t *number) 47 | { 48 | ClearString(cmd); 49 | strcat(cmd, "get "); 50 | strcat(cmd, button->__name); 51 | strcat(cmd, ".bco"); 52 | sendCommand(cmd); 53 | return recvRetNumber(number); 54 | } 55 | 56 | uint8_t NexButton_Set_background_color_bco(struct NexObject *button, uint32_t number) 57 | { 58 | ClearString(buf); 59 | ClearString(cmd); 60 | 61 | utoa(buf, number, 10); 62 | strcat(cmd, button->__name); 63 | strcat(cmd, ".bco="); 64 | strcat(cmd, buf); 65 | sendCommand(cmd); 66 | 67 | ClearString(cmd); 68 | strcat(cmd, "ref "); 69 | strcat(cmd, button->__name); 70 | sendCommand(cmd); 71 | return recvRetCommandFinished(); 72 | } 73 | 74 | uint32_t NexButton_Get_press_background_color_bco2(struct NexObject *button, uint32_t *number) 75 | { 76 | ClearString(cmd); 77 | strcat(cmd, "get "); 78 | strcat(cmd, button->__name); 79 | strcat(cmd, ".bco2"); 80 | sendCommand(cmd); 81 | return recvRetNumber(number); 82 | } 83 | 84 | uint8_t NexButton_Set_press_background_color_bco2(struct NexObject *button, uint32_t number) 85 | { 86 | ClearString(buf); 87 | ClearString(cmd); 88 | 89 | utoa(buf, number, 10); 90 | strcat(cmd, button->__name); 91 | strcat(cmd, ".bco2="); 92 | strcat(cmd, buf); 93 | sendCommand(cmd); 94 | 95 | ClearString(cmd); 96 | strcat(cmd, "ref "); 97 | strcat(cmd, button->__name); 98 | sendCommand(cmd); 99 | return recvRetCommandFinished(); 100 | } 101 | 102 | uint32_t NexButton_Get_font_color_pco(struct NexObject *button, uint32_t *number) 103 | { 104 | ClearString(cmd); 105 | strcat(cmd, "get "); 106 | strcat(cmd, button->__name); 107 | strcat(cmd, ".pco"); 108 | sendCommand(cmd); 109 | return recvRetNumber(number); 110 | } 111 | 112 | uint8_t NexButton_Set_font_color_pco(struct NexObject *button, uint32_t number) 113 | { 114 | ClearString(buf); 115 | ClearString(cmd); 116 | 117 | utoa(buf, number, 10); 118 | strcat(cmd, button->__name); 119 | strcat(cmd, ".pco="); 120 | strcat(cmd, buf); 121 | sendCommand(cmd); 122 | 123 | ClearString(cmd); 124 | strcat(cmd, "ref "); 125 | strcat(cmd, button->__name); 126 | sendCommand(cmd); 127 | return recvRetCommandFinished(); 128 | } 129 | 130 | uint32_t NexButton_Get_press_font_color_pco2(struct NexObject *button, uint32_t *number) 131 | { 132 | ClearString(cmd); 133 | strcat(cmd, "get "); 134 | strcat(cmd, button->__name); 135 | strcat(cmd, ".pco2"); 136 | sendCommand(cmd); 137 | return recvRetNumber(number); 138 | } 139 | 140 | uint8_t NexButton_Set_press_font_color_pco2(struct NexObject *button, uint32_t number) 141 | { 142 | ClearString(buf); 143 | ClearString(cmd); 144 | 145 | utoa(buf, number, 10); 146 | strcat(cmd, button->__name); 147 | strcat(cmd, ".pco2="); 148 | strcat(cmd, buf); 149 | sendCommand(cmd); 150 | 151 | ClearString(cmd); 152 | strcat(cmd, "ref "); 153 | strcat(cmd, button->__name); 154 | sendCommand(cmd); 155 | return recvRetCommandFinished(); 156 | } 157 | 158 | uint32_t NexButton_Get_place_xcen(struct NexObject *button, uint32_t *number) 159 | { 160 | ClearString(cmd); 161 | strcat(cmd, "get "); 162 | strcat(cmd, button->__name); 163 | strcat(cmd, ".xcen"); 164 | sendCommand(cmd); 165 | return recvRetNumber(number); 166 | } 167 | 168 | uint8_t NexButton_Set_place_xcen(struct NexObject *button, uint32_t number) 169 | { 170 | ClearString(buf); 171 | ClearString(cmd); 172 | 173 | utoa(buf, number, 10); 174 | strcat(cmd, button->__name); 175 | strcat(cmd, ".xcen="); 176 | strcat(cmd, buf); 177 | sendCommand(cmd); 178 | 179 | ClearString(cmd); 180 | strcat(cmd, "ref "); 181 | strcat(cmd, button->__name); 182 | sendCommand(cmd); 183 | return recvRetCommandFinished(); 184 | } 185 | 186 | uint32_t NexButton_Get_place_ycen(struct NexObject *button, uint32_t *number) 187 | { 188 | ClearString(cmd); 189 | strcat(cmd, "get "); 190 | strcat(cmd, button->__name); 191 | strcat(cmd, ".ycen"); 192 | sendCommand(cmd); 193 | return recvRetNumber(number); 194 | } 195 | 196 | uint8_t NexButton_Set_place_ycen(struct NexObject *button, uint32_t number) 197 | { 198 | ClearString(buf); 199 | ClearString(cmd); 200 | 201 | utoa(buf, number, 10); 202 | strcat(cmd, button->__name); 203 | strcat(cmd, ".ycen="); 204 | strcat(cmd, buf); 205 | sendCommand(cmd); 206 | 207 | ClearString(cmd); 208 | strcat(cmd, "ref "); 209 | strcat(cmd, button->__name); 210 | sendCommand(cmd); 211 | return recvRetCommandFinished(); 212 | } 213 | 214 | uint32_t NexButton_getFont(struct NexObject *button, uint32_t *number) 215 | { 216 | ClearString(cmd); 217 | strcat(cmd, "get "); 218 | strcat(cmd, button->__name); 219 | strcat(cmd, ".font"); 220 | sendCommand(cmd); 221 | return recvRetNumber(number); 222 | } 223 | 224 | uint8_t NexButton_setFont(struct NexObject *button, uint32_t number) 225 | { 226 | ClearString(buf); 227 | ClearString(cmd); 228 | 229 | utoa(buf, number, 10); 230 | strcat(cmd, button->__name); 231 | strcat(cmd, ".font="); 232 | strcat(cmd, buf); 233 | sendCommand(cmd); 234 | 235 | ClearString(cmd); 236 | strcat(cmd, "ref "); 237 | strcat(cmd, button->__name); 238 | sendCommand(cmd); 239 | return recvRetCommandFinished(); 240 | } 241 | 242 | uint32_t NexButton_Get_background_cropi_picc(struct NexObject *button, uint32_t *number) 243 | { 244 | ClearString(cmd); 245 | strcat(cmd, "get "); 246 | strcat(cmd, button->__name); 247 | strcat(cmd, ".picc"); 248 | sendCommand(cmd); 249 | return recvRetNumber(number); 250 | } 251 | 252 | uint8_t NexButton_Set_background_crop_picc(struct NexObject *button, uint32_t number) 253 | { 254 | ClearString(buf); 255 | ClearString(cmd); 256 | 257 | utoa(buf, number, 10); 258 | strcat(cmd, button->__name); 259 | strcat(cmd, ".picc="); 260 | strcat(cmd, buf); 261 | sendCommand(cmd); 262 | 263 | ClearString(cmd); 264 | strcat(cmd, "ref "); 265 | strcat(cmd, button->__name); 266 | sendCommand(cmd); 267 | return recvRetCommandFinished(); 268 | } 269 | 270 | uint32_t NexButton_Get_press_background_crop_picc2(struct NexObject *button, uint32_t *number) 271 | { 272 | ClearString(cmd); 273 | strcat(cmd, "get "); 274 | strcat(cmd, button->__name); 275 | strcat(cmd, ".picc2"); 276 | sendCommand(cmd); 277 | return recvRetNumber(number); 278 | } 279 | 280 | uint8_t NexButton_Set_press_background_crop_picc2(struct NexObject *button, uint32_t number) 281 | { 282 | ClearString(buf); 283 | ClearString(cmd); 284 | 285 | utoa(buf, number, 10); 286 | strcat(cmd, button->__name); 287 | strcat(cmd, ".picc2="); 288 | strcat(cmd, buf); 289 | sendCommand(cmd); 290 | 291 | ClearString(cmd); 292 | strcat(cmd, "ref "); 293 | strcat(cmd, button->__name); 294 | sendCommand(cmd); 295 | return recvRetCommandFinished(); 296 | } 297 | 298 | uint32_t NexButton_Get_background_image_pic(struct NexObject *button, uint32_t *number) 299 | { 300 | ClearString(cmd); 301 | strcat(cmd, "get "); 302 | strcat(cmd, button->__name); 303 | strcat(cmd, ".pic"); 304 | sendCommand(cmd); 305 | return recvRetNumber(number); 306 | } 307 | 308 | uint8_t NexButton_Set_background_image_pic(struct NexObject *button, uint32_t number) 309 | { 310 | ClearString(buf); 311 | ClearString(cmd); 312 | 313 | utoa(buf, number, 10); 314 | strcat(cmd, button->__name); 315 | strcat(cmd, ".pic="); 316 | strcat(cmd, buf); 317 | sendCommand(cmd); 318 | 319 | ClearString(cmd); 320 | strcat(cmd, "ref "); 321 | strcat(cmd, button->__name); 322 | sendCommand(cmd); 323 | return recvRetCommandFinished(); 324 | } 325 | 326 | uint32_t NexButton_Get_press_background_image_pic2(struct NexObject *button, uint32_t *number) 327 | { 328 | ClearString(cmd); 329 | strcat(cmd, "get "); 330 | strcat(cmd, button->__name); 331 | strcat(cmd, ".pic2"); 332 | sendCommand(cmd); 333 | return recvRetNumber(number); 334 | } 335 | 336 | uint8_t NexButton_Set_press_background_image_pic2(struct NexObject *button, uint32_t number) 337 | { 338 | ClearString(buf); 339 | ClearString(cmd); 340 | 341 | utoa(buf, number, 10); 342 | strcat(cmd, button->__name); 343 | strcat(cmd, ".pic2="); 344 | strcat(cmd, buf); 345 | sendCommand(cmd); 346 | 347 | ClearString(cmd); 348 | strcat(cmd, "ref "); 349 | strcat(cmd, button->__name); 350 | sendCommand(cmd); 351 | return recvRetCommandFinished(); 352 | } --------------------------------------------------------------------------------