diff --git a/NexConfig.h b/NexConfig.h index 090fbc6..aa095d3 100644 --- a/NexConfig.h +++ b/NexConfig.h @@ -1,54 +1,54 @@ -/** - * @file NexConfig.h - * - * Options for user can be found here. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#ifndef __NEXCONFIG_H__ -#define __NEXCONFIG_H__ - -/** - * @addtogroup Configuration - * @{ - */ - -/** - * Define DEBUG_SERIAL_ENABLE to enable debug serial. - * Comment it to disable debug serial. - */ -#define DEBUG_SERIAL_ENABLE - -/** - * Define dbSerial for the output of debug messages. - */ -#define dbSerial Serial - -/** - * Define nexSerial for communicate with Nextion touch panel. - */ -#define nexSerial Serial2 - - -#ifdef DEBUG_SERIAL_ENABLE -#define dbSerialPrint(a) dbSerial.print(a) -#define dbSerialPrintln(a) dbSerial.println(a) -#define dbSerialBegin(a) dbSerial.begin(a) -#else -#define dbSerialPrint(a) do{}while(0) -#define dbSerialPrintln(a) do{}while(0) -#define dbSerialBegin(a) do{}while(0) -#endif - -/** - * @} - */ - -#endif /* #ifndef __NEXCONFIG_H__ */ +/** + * @file NexConfig.h + * + * Options for user can be found here. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#ifndef __NEXCONFIG_H__ +#define __NEXCONFIG_H__ + +/** + * @addtogroup Configuration + * @{ + */ + +/** + * Define DEBUG_SERIAL_ENABLE to enable debug serial. + * Comment it to disable debug serial. + */ +#define DEBUG_SERIAL_ENABLE + +/** + * Define dbSerial for the output of debug messages. + */ +#define dbSerial Serial + +/** + * Define nexSerial for communicate with Nextion touch panel. + */ +#define nexSerial Serial2 + + +#ifdef DEBUG_SERIAL_ENABLE +#define dbSerialPrint(a) dbSerial.print(a) +#define dbSerialPrintln(a) dbSerial.println(a) +#define dbSerialBegin(a) dbSerial.begin(a) +#else +#define dbSerialPrint(a) do{}while(0) +#define dbSerialPrintln(a) do{}while(0) +#define dbSerialBegin(a) do{}while(0) +#endif + +/** + * @} + */ + +#endif /* #ifndef __NEXCONFIG_H__ */ diff --git a/NexCrop.cpp b/NexCrop.cpp index 46949c8..90b6a24 100644 --- a/NexCrop.cpp +++ b/NexCrop.cpp @@ -1,45 +1,45 @@ -/** - * @file NexCrop.cpp - * - * The implementation of class NexCrop. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ - -#include "NexCrop.h" - -NexCrop::NexCrop(uint8_t pid, uint8_t cid, const char *name) - :NexTouch(pid, cid, name) -{ -} - -bool NexCrop::getPic(uint32_t *number) -{ - String cmd = String("get "); - cmd += getObjName(); - cmd += ".picc"; - sendCommand(cmd.c_str()); - return recvRetNumber(number); -} - -bool NexCrop::setPic(uint32_t number) -{ - char buf[10] = {0}; - String cmd; - - utoa(number, buf, 10); - cmd += getObjName(); - cmd += ".picc="; - cmd += buf; - - sendCommand(cmd.c_str()); - return recvRetCommandFinished(); -} - +/** + * @file NexCrop.cpp + * + * The implementation of class NexCrop. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +#include "NexCrop.h" + +NexCrop::NexCrop(uint8_t pid, uint8_t cid, const char *name) + :NexTouch(pid, cid, name) +{ +} + +bool NexCrop::getPic(uint32_t *number) +{ + String cmd = String("get "); + cmd += getObjName(); + cmd += ".picc"; + sendCommand(cmd.c_str()); + return recvRetNumber(number); +} + +bool NexCrop::setPic(uint32_t number) +{ + char buf[10] = {0}; + String cmd; + + utoa(number, buf, 10); + cmd += getObjName(); + cmd += ".picc="; + cmd += buf; + + sendCommand(cmd.c_str()); + return recvRetCommandFinished(); +} + diff --git a/NexCrop.h b/NexCrop.h index e32244a..4b1d1ea 100644 --- a/NexCrop.h +++ b/NexCrop.h @@ -1,64 +1,64 @@ -/** - * @file NexCrop.h - * - * The definition of class NexCrop. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ - -#ifndef __NEXCROP_H__ -#define __NEXCROP_H__ - -#include "NexTouch.h" -#include "NexHardware.h" -/** - * @addtogroup Component - * @{ - */ - -/** - * NexCrop component. - */ -class NexCrop: public NexTouch -{ -public: /* methods */ - - /** - * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); - */ - NexCrop(uint8_t pid, uint8_t cid, const char *name); - - /** - * Get the number of picture. - * - * @param number - an output parameter to save the number of picture. - * - * @retval true - success. - * @retval false - failed. - */ - bool getPic(uint32_t *number); - - /** - * Set the number of picture. - * - * @param number - the number of picture. - * - * @retval true - success. - * @retval false - failed. - */ - bool setPic(uint32_t number); -}; - -/** - * @} - */ - -#endif /* #ifndef __NEXCROP_H__ */ +/** + * @file NexCrop.h + * + * The definition of class NexCrop. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +#ifndef __NEXCROP_H__ +#define __NEXCROP_H__ + +#include "NexTouch.h" +#include "NexHardware.h" +/** + * @addtogroup Component + * @{ + */ + +/** + * NexCrop component. + */ +class NexCrop: public NexTouch +{ +public: /* methods */ + + /** + * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); + */ + NexCrop(uint8_t pid, uint8_t cid, const char *name); + + /** + * Get the number of picture. + * + * @param number - an output parameter to save the number of picture. + * + * @retval true - success. + * @retval false - failed. + */ + bool getPic(uint32_t *number); + + /** + * Set the number of picture. + * + * @param number - the number of picture. + * + * @retval true - success. + * @retval false - failed. + */ + bool setPic(uint32_t number); +}; + +/** + * @} + */ + +#endif /* #ifndef __NEXCROP_H__ */ diff --git a/NexGauge.cpp b/NexGauge.cpp index d3451bb..906f9fe 100644 --- a/NexGauge.cpp +++ b/NexGauge.cpp @@ -1,45 +1,45 @@ -/** - * @file NexGauge.cpp - * - * The implementation of class NexGauge. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ - -#include "NexGauge.h" - -NexGauge::NexGauge(uint8_t pid, uint8_t cid, const char *name) - :NexObject(pid, cid, name) -{ -} - -bool NexGauge::getValue(uint32_t *number) -{ - String cmd = String("get "); - cmd += getObjName(); - cmd += ".val"; - sendCommand(cmd.c_str()); - return recvRetNumber(number); -} - -bool NexGauge::setValue(uint32_t number) -{ - char buf[10] = {0}; - String cmd; - - utoa(number, buf, 10); - cmd += getObjName(); - cmd += ".val="; - cmd += buf; - - sendCommand(cmd.c_str()); - return recvRetCommandFinished(); -} - +/** + * @file NexGauge.cpp + * + * The implementation of class NexGauge. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +#include "NexGauge.h" + +NexGauge::NexGauge(uint8_t pid, uint8_t cid, const char *name) + :NexObject(pid, cid, name) +{ +} + +bool NexGauge::getValue(uint32_t *number) +{ + String cmd = String("get "); + cmd += getObjName(); + cmd += ".val"; + sendCommand(cmd.c_str()); + return recvRetNumber(number); +} + +bool NexGauge::setValue(uint32_t number) +{ + char buf[10] = {0}; + String cmd; + + utoa(number, buf, 10); + cmd += getObjName(); + cmd += ".val="; + cmd += buf; + + sendCommand(cmd.c_str()); + return recvRetCommandFinished(); +} + diff --git a/NexGauge.h b/NexGauge.h index a6a086f..5abf7f7 100644 --- a/NexGauge.h +++ b/NexGauge.h @@ -1,63 +1,63 @@ -/** - * @file NexGauge.h - * - * The definition of class NexGauge. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ - -#ifndef __NEXGAUGE_H__ -#define __NEXGAUGE_H__ - -#include "NexTouch.h" -#include "NexHardware.h" -/** - * @addtogroup Component - * @{ - */ - -/** - * NexGauge component. - */ -class NexGauge: public NexObject -{ -public: /* methods */ - /** - * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); - */ - NexGauge(uint8_t pid, uint8_t cid, const char *name); - - /** - * Get the value of gauge. - * - * @param number - an output parameter to save gauge's value. - * - * @retval true - success. - * @retval false - failed. - */ - bool getValue(uint32_t *number); - - /** - * Set the value of gauge. - * - * @param number - the value of gauge. - * - * @retval true - success. - * @retval false - failed. - */ - bool setValue(uint32_t number); -}; - -/** - * @} - */ - -#endif /* #ifndef __NEXGAUGE_H__ */ +/** + * @file NexGauge.h + * + * The definition of class NexGauge. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +#ifndef __NEXGAUGE_H__ +#define __NEXGAUGE_H__ + +#include "NexTouch.h" +#include "NexHardware.h" +/** + * @addtogroup Component + * @{ + */ + +/** + * NexGauge component. + */ +class NexGauge: public NexObject +{ +public: /* methods */ + /** + * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); + */ + NexGauge(uint8_t pid, uint8_t cid, const char *name); + + /** + * Get the value of gauge. + * + * @param number - an output parameter to save gauge's value. + * + * @retval true - success. + * @retval false - failed. + */ + bool getValue(uint32_t *number); + + /** + * Set the value of gauge. + * + * @param number - the value of gauge. + * + * @retval true - success. + * @retval false - failed. + */ + bool setValue(uint32_t number); +}; + +/** + * @} + */ + +#endif /* #ifndef __NEXGAUGE_H__ */ diff --git a/NexHardware.cpp b/NexHardware.cpp index 2ae6c9e..17d4ec9 100644 --- a/NexHardware.cpp +++ b/NexHardware.cpp @@ -1,267 +1,267 @@ -/** - * @file NexHardware.cpp - * - * The implementation of base API for using Nextion. - * - * @author Wu Pengfei (email:) - * @date 2015/8/11 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#include "NexHardware.h" - -#define NEX_RET_CMD_FINISHED (0x01) -#define NEX_RET_EVENT_LAUNCHED (0x88) -#define NEX_RET_EVENT_UPGRADED (0x89) -#define NEX_RET_EVENT_TOUCH_HEAD (0x65) -#define NEX_RET_EVENT_POSITION_HEAD (0x67) -#define NEX_RET_EVENT_SLEEP_POSITION_HEAD (0x68) -#define NEX_RET_CURRENT_PAGE_ID_HEAD (0x66) -#define NEX_RET_STRING_HEAD (0x70) -#define NEX_RET_NUMBER_HEAD (0x71) -#define NEX_RET_INVALID_CMD (0x00) -#define NEX_RET_INVALID_COMPONENT_ID (0x02) -#define NEX_RET_INVALID_PAGE_ID (0x03) -#define NEX_RET_INVALID_PICTURE_ID (0x04) -#define NEX_RET_INVALID_FONT_ID (0x05) -#define NEX_RET_INVALID_BAUD (0x11) -#define NEX_RET_INVALID_VARIABLE (0x1A) -#define NEX_RET_INVALID_OPERATION (0x1B) - -/* - * Receive uint32_t data. - * - * @param number - save uint32_t data. - * @param timeout - set timeout time. - * - * @retval true - success. - * @retval false - failed. - * - */ -bool recvRetNumber(uint32_t *number, uint32_t timeout) -{ - bool ret = false; - uint8_t temp[8] = {0}; - - if (!number) - { - goto __return; - } - - nexSerial.setTimeout(timeout); - if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp))) - { - goto __return; - } - - if (temp[0] == NEX_RET_NUMBER_HEAD - && temp[5] == 0xFF - && temp[6] == 0xFF - && temp[7] == 0xFF - ) - { - *number = (temp[4] << 24) | (temp[3] << 16) | (temp[2] << 8) | (temp[1]); - ret = true; - } - -__return: - - if (ret) - { - dbSerialPrint("recvRetNumber :"); - dbSerialPrintln(*number); - } - else - { - dbSerialPrintln("recvRetNumber err"); - } - - return ret; -} - - -/* - * Receive string data. - * - * @param buffer - save string data. - * @param len - string buffer length. - * @param timeout - set timeout time. - * - * @return the length of string buffer. - * - */ -uint16_t recvRetString(char *buffer, uint16_t len, uint32_t timeout) -{ - uint16_t ret = 0; - bool str_start_flag = false; - uint8_t cnt_0xff = 0; - String temp = String(""); - uint8_t c = 0; - long start; - - if (!buffer || len == 0) - { - goto __return; - } - - start = millis(); - while (millis() - start <= timeout) - { - while (nexSerial.available()) - { - c = nexSerial.read(); - if (str_start_flag) - { - if (0xFF == c) - { - cnt_0xff++; - if (cnt_0xff >= 3) - { - break; - } - } - else - { - temp += (char)c; - } - } - else if (NEX_RET_STRING_HEAD == c) - { - str_start_flag = true; - } - } - - if (cnt_0xff >= 3) - { - break; - } - } - - ret = temp.length(); - ret = ret > len ? len : ret; - strncpy(buffer, temp.c_str(), ret); - -__return: - - dbSerialPrint("recvRetString["); - dbSerialPrint(temp.length()); - dbSerialPrint(","); - dbSerialPrint(temp); - dbSerialPrintln("]"); - - return ret; -} - -/* - * Send command to Nextion. - * - * @param cmd - the string of command. - */ -void sendCommand(const char* cmd) -{ - while (nexSerial.available()) - { - nexSerial.read(); - } - - nexSerial.print(cmd); - nexSerial.write(0xFF); - nexSerial.write(0xFF); - nexSerial.write(0xFF); -} - - -/* - * Command is executed successfully. - * - * @param timeout - set timeout time. - * - * @retval true - success. - * @retval false - failed. - * - */ -bool recvRetCommandFinished(uint32_t timeout) -{ - bool ret = false; - uint8_t temp[4] = {0}; - - nexSerial.setTimeout(timeout); - if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp))) - { - ret = false; - } - - if (temp[0] == NEX_RET_CMD_FINISHED - && temp[1] == 0xFF - && temp[2] == 0xFF - && temp[3] == 0xFF - ) - { - ret = true; - } - - if (ret) - { - dbSerialPrintln("recvRetCommandFinished ok"); - } - else - { - dbSerialPrintln("recvRetCommandFinished err"); - } - - return ret; -} - - -bool nexInit(void) -{ - bool ret1 = false; - bool ret2 = false; - - dbSerialBegin(9600); - nexSerial.begin(9600); - sendCommand(""); - sendCommand("bkcmd=1"); - ret1 = recvRetCommandFinished(); - sendCommand("page 0"); - ret2 = recvRetCommandFinished(); - return ret1 && ret2; -} - -void nexLoop(NexTouch *nex_listen_list[]) -{ - static uint8_t __buffer[10]; - - uint16_t i; - uint8_t c; - - while (nexSerial.available() > 0) - { - delay(10); - c = nexSerial.read(); - - if (NEX_RET_EVENT_TOUCH_HEAD == c) - { - if (nexSerial.available() >= 6) - { - __buffer[0] = c; - for (i = 1; i < 7; i++) - { - __buffer[i] = nexSerial.read(); - } - __buffer[i] = 0x00; - - if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6]) - { - NexTouch::iterate(nex_listen_list, __buffer[1], __buffer[2], (int32_t)__buffer[3]); - } - - } - } - } -} - +/** + * @file NexHardware.cpp + * + * The implementation of base API for using Nextion. + * + * @author Wu Pengfei (email:) + * @date 2015/8/11 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#include "NexHardware.h" + +#define NEX_RET_CMD_FINISHED (0x01) +#define NEX_RET_EVENT_LAUNCHED (0x88) +#define NEX_RET_EVENT_UPGRADED (0x89) +#define NEX_RET_EVENT_TOUCH_HEAD (0x65) +#define NEX_RET_EVENT_POSITION_HEAD (0x67) +#define NEX_RET_EVENT_SLEEP_POSITION_HEAD (0x68) +#define NEX_RET_CURRENT_PAGE_ID_HEAD (0x66) +#define NEX_RET_STRING_HEAD (0x70) +#define NEX_RET_NUMBER_HEAD (0x71) +#define NEX_RET_INVALID_CMD (0x00) +#define NEX_RET_INVALID_COMPONENT_ID (0x02) +#define NEX_RET_INVALID_PAGE_ID (0x03) +#define NEX_RET_INVALID_PICTURE_ID (0x04) +#define NEX_RET_INVALID_FONT_ID (0x05) +#define NEX_RET_INVALID_BAUD (0x11) +#define NEX_RET_INVALID_VARIABLE (0x1A) +#define NEX_RET_INVALID_OPERATION (0x1B) + +/* + * Receive uint32_t data. + * + * @param number - save uint32_t data. + * @param timeout - set timeout time. + * + * @retval true - success. + * @retval false - failed. + * + */ +bool recvRetNumber(uint32_t *number, uint32_t timeout) +{ + bool ret = false; + uint8_t temp[8] = {0}; + + if (!number) + { + goto __return; + } + + nexSerial.setTimeout(timeout); + if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp))) + { + goto __return; + } + + if (temp[0] == NEX_RET_NUMBER_HEAD + && temp[5] == 0xFF + && temp[6] == 0xFF + && temp[7] == 0xFF + ) + { + *number = (temp[4] << 24) | (temp[3] << 16) | (temp[2] << 8) | (temp[1]); + ret = true; + } + +__return: + + if (ret) + { + dbSerialPrint("recvRetNumber :"); + dbSerialPrintln(*number); + } + else + { + dbSerialPrintln("recvRetNumber err"); + } + + return ret; +} + + +/* + * Receive string data. + * + * @param buffer - save string data. + * @param len - string buffer length. + * @param timeout - set timeout time. + * + * @return the length of string buffer. + * + */ +uint16_t recvRetString(char *buffer, uint16_t len, uint32_t timeout) +{ + uint16_t ret = 0; + bool str_start_flag = false; + uint8_t cnt_0xff = 0; + String temp = String(""); + uint8_t c = 0; + long start; + + if (!buffer || len == 0) + { + goto __return; + } + + start = millis(); + while (millis() - start <= timeout) + { + while (nexSerial.available()) + { + c = nexSerial.read(); + if (str_start_flag) + { + if (0xFF == c) + { + cnt_0xff++; + if (cnt_0xff >= 3) + { + break; + } + } + else + { + temp += (char)c; + } + } + else if (NEX_RET_STRING_HEAD == c) + { + str_start_flag = true; + } + } + + if (cnt_0xff >= 3) + { + break; + } + } + + ret = temp.length(); + ret = ret > len ? len : ret; + strncpy(buffer, temp.c_str(), ret); + +__return: + + dbSerialPrint("recvRetString["); + dbSerialPrint(temp.length()); + dbSerialPrint(","); + dbSerialPrint(temp); + dbSerialPrintln("]"); + + return ret; +} + +/* + * Send command to Nextion. + * + * @param cmd - the string of command. + */ +void sendCommand(const char* cmd) +{ + while (nexSerial.available()) + { + nexSerial.read(); + } + + nexSerial.print(cmd); + nexSerial.write(0xFF); + nexSerial.write(0xFF); + nexSerial.write(0xFF); +} + + +/* + * Command is executed successfully. + * + * @param timeout - set timeout time. + * + * @retval true - success. + * @retval false - failed. + * + */ +bool recvRetCommandFinished(uint32_t timeout) +{ + bool ret = false; + uint8_t temp[4] = {0}; + + nexSerial.setTimeout(timeout); + if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp))) + { + ret = false; + } + + if (temp[0] == NEX_RET_CMD_FINISHED + && temp[1] == 0xFF + && temp[2] == 0xFF + && temp[3] == 0xFF + ) + { + ret = true; + } + + if (ret) + { + dbSerialPrintln("recvRetCommandFinished ok"); + } + else + { + dbSerialPrintln("recvRetCommandFinished err"); + } + + return ret; +} + + +bool nexInit(void) +{ + bool ret1 = false; + bool ret2 = false; + + dbSerialBegin(9600); + nexSerial.begin(9600); + sendCommand(""); + sendCommand("bkcmd=1"); + ret1 = recvRetCommandFinished(); + sendCommand("page 0"); + ret2 = recvRetCommandFinished(); + return ret1 && ret2; +} + +void nexLoop(NexTouch *nex_listen_list[]) +{ + static uint8_t __buffer[10]; + + uint16_t i; + uint8_t c; + + while (nexSerial.available() > 0) + { + delay(10); + c = nexSerial.read(); + + if (NEX_RET_EVENT_TOUCH_HEAD == c) + { + if (nexSerial.available() >= 6) + { + __buffer[0] = c; + for (i = 1; i < 7; i++) + { + __buffer[i] = nexSerial.read(); + } + __buffer[i] = 0x00; + + if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6]) + { + NexTouch::iterate(nex_listen_list, __buffer[1], __buffer[2], (int32_t)__buffer[3]); + } + + } + } + } +} + diff --git a/NexHardware.h b/NexHardware.h index a263c3d..474dfef 100644 --- a/NexHardware.h +++ b/NexHardware.h @@ -1,55 +1,55 @@ -/** - * @file NexHardware.h - * - * The definition of base API for using Nextion. - * - * @author Wu Pengfei (email:) - * @date 2015/8/11 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#ifndef __NEXHARDWARE_H__ -#define __NEXHARDWARE_H__ -#include -#include "NexConfig.h" -#include "NexTouch.h" - -/** - * @addtogroup CoreAPI - * @{ - */ - -/** - * Init Nextion. - * - * @return true if success, false for failure. - */ -bool nexInit(void); - -/** - * Listen touch event and calling callbacks attached before. - * - * Supports push and pop at present. - * - * @param nex_listen_list - index to Nextion Components list. - * @return none. - * - * @warning This function must be called repeatedly to response touch events - * from Nextion touch panel. Actually, you should place it in your loop function. - */ -void nexLoop(NexTouch *nex_listen_list[]); - -/** - * @} - */ - -bool recvRetNumber(uint32_t *number, uint32_t timeout = 100); -uint16_t recvRetString(char *buffer, uint16_t len, uint32_t timeout = 100); -void sendCommand(const char* cmd); -bool recvRetCommandFinished(uint32_t timeout = 100); - -#endif /* #ifndef __NEXHARDWARE_H__ */ +/** + * @file NexHardware.h + * + * The definition of base API for using Nextion. + * + * @author Wu Pengfei (email:) + * @date 2015/8/11 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#ifndef __NEXHARDWARE_H__ +#define __NEXHARDWARE_H__ +#include +#include "NexConfig.h" +#include "NexTouch.h" + +/** + * @addtogroup CoreAPI + * @{ + */ + +/** + * Init Nextion. + * + * @return true if success, false for failure. + */ +bool nexInit(void); + +/** + * Listen touch event and calling callbacks attached before. + * + * Supports push and pop at present. + * + * @param nex_listen_list - index to Nextion Components list. + * @return none. + * + * @warning This function must be called repeatedly to response touch events + * from Nextion touch panel. Actually, you should place it in your loop function. + */ +void nexLoop(NexTouch *nex_listen_list[]); + +/** + * @} + */ + +bool recvRetNumber(uint32_t *number, uint32_t timeout = 100); +uint16_t recvRetString(char *buffer, uint16_t len, uint32_t timeout = 100); +void sendCommand(const char* cmd); +bool recvRetCommandFinished(uint32_t timeout = 100); + +#endif /* #ifndef __NEXHARDWARE_H__ */ diff --git a/NexObject.cpp b/NexObject.cpp index a247d72..081ad73 100644 --- a/NexObject.cpp +++ b/NexObject.cpp @@ -1,58 +1,58 @@ -/** - * @file NexObject.cpp - * - * The implementation of class NexObject. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#include "NexObject.h" - -NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name) -{ - this->__pid = pid; - this->__cid = cid; - this->__name = name; -} - -uint8_t NexObject::getObjPid(void) -{ - return __pid; -} - -uint8_t NexObject::getObjCid(void) -{ - return __cid; -} - -const char* NexObject::getObjName(void) -{ - return __name; -} - -void NexObject::printObjInfo(void) -{ - dbSerialPrint("["); - dbSerialPrint((uint32_t)this); - dbSerialPrint(":"); - dbSerialPrint(__pid); - dbSerialPrint(","); - dbSerialPrint(__cid); - dbSerialPrint(","); - if (__name) - { - dbSerialPrint(__name); - } - else - { - dbSerialPrint("(null)"); - } - dbSerialPrintln("]"); -} - +/** + * @file NexObject.cpp + * + * The implementation of class NexObject. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#include "NexObject.h" + +NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name) +{ + this->__pid = pid; + this->__cid = cid; + this->__name = name; +} + +uint8_t NexObject::getObjPid(void) +{ + return __pid; +} + +uint8_t NexObject::getObjCid(void) +{ + return __cid; +} + +const char* NexObject::getObjName(void) +{ + return __name; +} + +void NexObject::printObjInfo(void) +{ + dbSerialPrint("["); + dbSerialPrint((uint32_t)this); + dbSerialPrint(":"); + dbSerialPrint(__pid); + dbSerialPrint(","); + dbSerialPrint(__cid); + dbSerialPrint(","); + if (__name) + { + dbSerialPrint(__name); + } + else + { + dbSerialPrint("(null)"); + } + dbSerialPrintln("]"); +} + diff --git a/NexObject.h b/NexObject.h index cfc55b5..d967826 100644 --- a/NexObject.h +++ b/NexObject.h @@ -1,84 +1,84 @@ -/** - * @file NexObject.h - * - * The definition of class NexObject. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#ifndef __NEXOBJECT_H__ -#define __NEXOBJECT_H__ -#include -#include "NexConfig.h" -/** - * @addtogroup CoreAPI - * @{ - */ - -/** - * Root class of all Nextion components. - * - * Provides the essential attributes of a Nextion component and the methods accessing - * them. At least, Page ID(pid), Component ID(pid) and an unique name are needed for - * creating a component in Nexiton library. - */ -class NexObject -{ -public: /* methods */ - - /** - * Constructor. - * - * @param pid - page id. - * @param cid - component id. - * @param name - pointer to an unique name in range of all components. - */ - NexObject(uint8_t pid, uint8_t cid, const char *name); - - /** - * Print current object'address, page id, component id and name. - * - * @warning this method does nothing, unless debug message enabled. - */ - void printObjInfo(void); - -protected: /* methods */ - - /* - * Get page id. - * - * @return the id of page. - */ - uint8_t getObjPid(void); - - /* - * Get component id. - * - * @return the id of component. - */ - uint8_t getObjCid(void); - - /* - * Get component name. - * - * @return the name of component. - */ - const char *getObjName(void); - -private: /* data */ - uint8_t __pid; /* Page ID */ - uint8_t __cid; /* Component ID */ - const char *__name; /* An unique name */ -}; -/** - * @} - */ - -#endif /* #ifndef __NEXOBJECT_H__ */ +/** + * @file NexObject.h + * + * The definition of class NexObject. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#ifndef __NEXOBJECT_H__ +#define __NEXOBJECT_H__ +#include +#include "NexConfig.h" +/** + * @addtogroup CoreAPI + * @{ + */ + +/** + * Root class of all Nextion components. + * + * Provides the essential attributes of a Nextion component and the methods accessing + * them. At least, Page ID(pid), Component ID(pid) and an unique name are needed for + * creating a component in Nexiton library. + */ +class NexObject +{ +public: /* methods */ + + /** + * Constructor. + * + * @param pid - page id. + * @param cid - component id. + * @param name - pointer to an unique name in range of all components. + */ + NexObject(uint8_t pid, uint8_t cid, const char *name); + + /** + * Print current object'address, page id, component id and name. + * + * @warning this method does nothing, unless debug message enabled. + */ + void printObjInfo(void); + +protected: /* methods */ + + /* + * Get page id. + * + * @return the id of page. + */ + uint8_t getObjPid(void); + + /* + * Get component id. + * + * @return the id of component. + */ + uint8_t getObjCid(void); + + /* + * Get component name. + * + * @return the name of component. + */ + const char *getObjName(void); + +private: /* data */ + uint8_t __pid; /* Page ID */ + uint8_t __cid; /* Component ID */ + const char *__name; /* An unique name */ +}; +/** + * @} + */ + +#endif /* #ifndef __NEXOBJECT_H__ */ diff --git a/NexSlider.cpp b/NexSlider.cpp index 371bfd7..e714662 100644 --- a/NexSlider.cpp +++ b/NexSlider.cpp @@ -1,44 +1,44 @@ -/** - * @file NexSlider.cpp - * - * The implementation of class NexSlider. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#include "NexSlider.h" - -NexSlider::NexSlider(uint8_t pid, uint8_t cid, const char *name) - :NexTouch(pid, cid, name) -{ -} - -bool NexSlider::getValue(uint32_t *number) -{ - String cmd = String("get "); - cmd += getObjName(); - cmd += ".val"; - sendCommand(cmd.c_str()); - return recvRetNumber(number); -} - -bool NexSlider::setValue(uint32_t number) -{ - char buf[10] = {0}; - String cmd; - - utoa(number, buf, 10); - cmd += getObjName(); - cmd += ".val="; - cmd += buf; - - sendCommand(cmd.c_str()); - return recvRetCommandFinished(); -} - +/** + * @file NexSlider.cpp + * + * The implementation of class NexSlider. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#include "NexSlider.h" + +NexSlider::NexSlider(uint8_t pid, uint8_t cid, const char *name) + :NexTouch(pid, cid, name) +{ +} + +bool NexSlider::getValue(uint32_t *number) +{ + String cmd = String("get "); + cmd += getObjName(); + cmd += ".val"; + sendCommand(cmd.c_str()); + return recvRetNumber(number); +} + +bool NexSlider::setValue(uint32_t number) +{ + char buf[10] = {0}; + String cmd; + + utoa(number, buf, 10); + cmd += getObjName(); + cmd += ".val="; + cmd += buf; + + sendCommand(cmd.c_str()); + return recvRetCommandFinished(); +} + diff --git a/NexSlider.h b/NexSlider.h index 2d84b0b..7b22b6a 100644 --- a/NexSlider.h +++ b/NexSlider.h @@ -1,63 +1,63 @@ -/** - * @file NexSlider.h - * - * The definition of class NexSlider. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ - -#ifndef __NEXSLIDER_H__ -#define __NEXSLIDER_H__ - -#include "NexTouch.h" -#include "NexHardware.h" -/** - * @addtogroup Component - * @{ - */ - -/** - * NexSlider component. - */ -class NexSlider: public NexTouch -{ -public: /* methods */ - /** - * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); - */ - NexSlider(uint8_t pid, uint8_t cid, const char *name); - - /** - * Get the value of slider. - * - * @param number - an output parameter to save the value of slider. - * - * @retval true - success. - * @retval false - failed. - */ - bool getValue(uint32_t *number); - - /** - * Set the value of slider. - * - * @param number - the value of slider. - * - * @retval true - success. - * @retval false - failed. - */ - bool setValue(uint32_t number); -}; -/** - * @} - */ - - -#endif /* #ifndef __NEXSLIDER_H__ */ +/** + * @file NexSlider.h + * + * The definition of class NexSlider. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +#ifndef __NEXSLIDER_H__ +#define __NEXSLIDER_H__ + +#include "NexTouch.h" +#include "NexHardware.h" +/** + * @addtogroup Component + * @{ + */ + +/** + * NexSlider component. + */ +class NexSlider: public NexTouch +{ +public: /* methods */ + /** + * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); + */ + NexSlider(uint8_t pid, uint8_t cid, const char *name); + + /** + * Get the value of slider. + * + * @param number - an output parameter to save the value of slider. + * + * @retval true - success. + * @retval false - failed. + */ + bool getValue(uint32_t *number); + + /** + * Set the value of slider. + * + * @param number - the value of slider. + * + * @retval true - success. + * @retval false - failed. + */ + bool setValue(uint32_t number); +}; +/** + * @} + */ + + +#endif /* #ifndef __NEXSLIDER_H__ */ diff --git a/NexWaveform.cpp b/NexWaveform.cpp index bfdaac2..ac868d6 100644 --- a/NexWaveform.cpp +++ b/NexWaveform.cpp @@ -1,36 +1,36 @@ -/** - * @file NexWaveform.cpp - * - * The implementation of class NexWaveform. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#include "NexWaveform.h" - -NexWaveform::NexWaveform(uint8_t pid, uint8_t cid, const char *name) - :NexObject(pid, cid, name) -{ -} - -bool NexWaveform::addValue(uint8_t ch, uint8_t number) -{ - char buf[15] = {0}; - - if (ch > 3) - { - return false; - } - - sprintf(buf, "add %u,%u,%u", getObjCid(), ch, number); - - sendCommand(buf); - return true; -} - +/** + * @file NexWaveform.cpp + * + * The implementation of class NexWaveform. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#include "NexWaveform.h" + +NexWaveform::NexWaveform(uint8_t pid, uint8_t cid, const char *name) + :NexObject(pid, cid, name) +{ +} + +bool NexWaveform::addValue(uint8_t ch, uint8_t number) +{ + char buf[15] = {0}; + + if (ch > 3) + { + return false; + } + + sprintf(buf, "add %u,%u,%u", getObjCid(), ch, number); + + sendCommand(buf); + return true; +} + diff --git a/NexWaveform.h b/NexWaveform.h index 0e8dd08..02f3a78 100644 --- a/NexWaveform.h +++ b/NexWaveform.h @@ -1,53 +1,53 @@ -/** - * @file NexWaveform.h - * - * The definition of class NexWaveform. - * - * @author Wu Pengfei (email:) - * @date 2015/8/13 - * - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#ifndef __NEXWAVEFORM_H__ -#define __NEXWAVEFORM_H__ - -#include "NexTouch.h" -#include "NexHardware.h" -/** - * @addtogroup Component - * @{ - */ - -/** - * NexWaveform component. - */ -class NexWaveform: public NexObject -{ -public: /* methods */ - /** - * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); - */ - NexWaveform(uint8_t pid, uint8_t cid, const char *name); - - /** - * Add value to show. - * - * @param ch - channel of waveform(0-3). - * @param number - the value of waveform. - * - * @retval true - success. - * @retval false - failed. - */ - bool addValue(uint8_t ch, uint8_t number); -}; - -/** - * @} - */ - -#endif /* #ifndef __NEXWAVEFORM_H__ */ +/** + * @file NexWaveform.h + * + * The definition of class NexWaveform. + * + * @author Wu Pengfei (email:) + * @date 2015/8/13 + * + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#ifndef __NEXWAVEFORM_H__ +#define __NEXWAVEFORM_H__ + +#include "NexTouch.h" +#include "NexHardware.h" +/** + * @addtogroup Component + * @{ + */ + +/** + * NexWaveform component. + */ +class NexWaveform: public NexObject +{ +public: /* methods */ + /** + * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); + */ + NexWaveform(uint8_t pid, uint8_t cid, const char *name); + + /** + * Add value to show. + * + * @param ch - channel of waveform(0-3). + * @param number - the value of waveform. + * + * @retval true - success. + * @retval false - failed. + */ + bool addValue(uint8_t ch, uint8_t number); +}; + +/** + * @} + */ + +#endif /* #ifndef __NEXWAVEFORM_H__ */ diff --git a/Nextion.h b/Nextion.h index 4ae784a..d5125c8 100644 --- a/Nextion.h +++ b/Nextion.h @@ -1,36 +1,36 @@ -/** - * @file Nextion.h - * - * The header file including all other header files provided by this library. - * - * Every example sketch should include this file. - * - * @author Wu Pengfei (email:) - * @date 2015/8/12 - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ -#ifndef __NEXTION_H__ -#define __NEXTION_H__ - -#include "Arduino.h" -#include "NexConfig.h" -#include "NexTouch.h" -#include "NexHardware.h" - -#include "NexButton.h" -#include "NexCrop.h" -#include "NexGauge.h" -#include "NexHotspot.h" -#include "NexPage.h" -#include "NexPicture.h" -#include "NexProgressBar.h" -#include "NexSlider.h" -#include "NexText.h" -#include "NexWaveform.h" - -#endif /* #ifndef __NEXTION_H__ */ +/** + * @file Nextion.h + * + * The header file including all other header files provided by this library. + * + * Every example sketch should include this file. + * + * @author Wu Pengfei (email:) + * @date 2015/8/12 + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ +#ifndef __NEXTION_H__ +#define __NEXTION_H__ + +#include "Arduino.h" +#include "NexConfig.h" +#include "NexTouch.h" +#include "NexHardware.h" + +#include "NexButton.h" +#include "NexCrop.h" +#include "NexGauge.h" +#include "NexHotspot.h" +#include "NexPage.h" +#include "NexPicture.h" +#include "NexProgressBar.h" +#include "NexSlider.h" +#include "NexText.h" +#include "NexWaveform.h" + +#endif /* #ifndef __NEXTION_H__ */ diff --git a/doc/Documentation/_comp_button_8ino-example.html b/doc/Documentation/_comp_button_8ino-example.html index ef1f695..5ba0f82 100644 --- a/doc/Documentation/_comp_button_8ino-example.html +++ b/doc/Documentation/_comp_button_8ino-example.html @@ -65,7 +65,7 @@ $(document).ready(function(){initNavTree('_comp_button_8ino-example.html','');})
CompButton.ino
-
How to Use
Show how to use API of class NexButton.
+
How to Use
This example shows that when the button component on the Nextion screen is released, the text of this button will plus one every time.
Author
Wu Pengfei (email:pengf.nosp@m.ei.w.nosp@m.u@ite.nosp@m.ad.c.nosp@m.c)
Date
2015/7/10
1 
-
17 #include "Nextion.h"
-
18 
-
19 NexButton b0 = NexButton(0, 1, "b0");
-
20 
-
21 char buffer[100] = {0};
-
22 
-
23 NexTouch *nex_listen_list[] =
-
24 {
-
25  &b0,
-
26  NULL
-
27 };
-
28 
-
29 void b0PopCallback(void *ptr)
-
30 {
-
31  uint16_t len;
-
32  uint16_t number;
-
33  NexButton *btn = (NexButton *)ptr;
-
34  dbSerialPrintln("b0PopCallback");
-
35  dbSerialPrint("ptr=");
-
36  dbSerialPrintln((uint32_t)ptr);
-
37  memset(buffer, 0, sizeof(buffer));
-
38  btn->getText(buffer, sizeof(buffer));
-
39 
-
40  number = atoi(buffer);
-
41  number += 1;
-
42 
-
43  memset(buffer, 0, sizeof(buffer));
-
44  itoa(number, buffer, 10);
-
45 
-
46  btn->setText(buffer);
-
47 }
-
48 
-
49 void setup(void)
-
50 {
-
51  nexInit();
-
52  b0.attachPop(b0PopCallback, &b0);
-
53  dbSerialPrintln("setup done");
-
54 }
+
18 #include "Nextion.h"
+
19 
+
20 /*
+
21  * Declare a button object [page id:0,component id:1, component name: "b0"].
+
22  */
+
23 NexButton b0 = NexButton(0, 1, "b0");
+
24 
+
25 char buffer[100] = {0};
+
26 
+
27 /*
+
28  * Register a button object to the touch event list.
+
29  */
+
30 NexTouch *nex_listen_list[] =
+
31 {
+
32  &b0,
+
33  NULL
+
34 };
+
35 
+
36 /*
+
37  * Button component pop callback function.
+
38  * In this example,the button's text value will plus one every time when it is released.
+
39  */
+
40 void b0PopCallback(void *ptr)
+
41 {
+
42  uint16_t len;
+
43  uint16_t number;
+
44  NexButton *btn = (NexButton *)ptr;
+
45  dbSerialPrintln("b0PopCallback");
+
46  dbSerialPrint("ptr=");
+
47  dbSerialPrintln((uint32_t)ptr);
+
48  memset(buffer, 0, sizeof(buffer));
+
49 
+
50  /* Get the text value of button component [the value is string type]. */
+
51  btn->getText(buffer, sizeof(buffer));
+
52 
+
53  number = atoi(buffer);
+
54  number += 1;
55 
-
56 void loop(void)
-
57 {
-
58  nexLoop(nex_listen_list);
-
59 }
+
56  memset(buffer, 0, sizeof(buffer));
+
57  itoa(number, buffer, 10);
+
58 
+
59  /* Set the text value of button component [the value is string type]. */
+
60  btn->setText(buffer);
+
61 }
+
62 
+
63 void setup(void)
+
64 {
+
65  /* Set the baudrate which is for debug and communicate with Nextion screen. */
+
66  nexInit();
+
67 
+
68  /* Register the pop event callback function of the current button component. */
+
69  b0.attachPop(b0PopCallback, &b0);
+
70 
+
71  dbSerialPrintln("setup done");
+
72 }
+
73 
+
74 void loop(void)
+
75 {
+
76  /*
+
77  * When a pop or push event occured every time,
+
78  * the corresponding component[right page id and component id] in touch event list will be asked.
+
79  */
+
80  nexLoop(nex_listen_list);
+
81 }
+
82 
+
83 
+
84 
+
85 
+
86 
+
87 
+
88 
+
89 
+
90 
+
91 
+
92 
+
93 
void nexLoop(NexTouch *nex_listen_list[])
Listen touch event and calling callbacks attached before.
uint16_t getText(char *buffer, uint16_t len)
Get text attribute of component.
Definition: NexButton.cpp:23
void attachPop(NexTouchEventCb pop, void *ptr=NULL)
Attach an callback function of pop touch event.
Definition: NexTouch.cpp:39
@@ -129,7 +162,7 @@ $(document).ready(function(){initNavTree('_comp_button_8ino_source.html','');});
-
How to Use
Show how to use API of class NexCrop.
+
How to Use
This example shows that when the crop component on the Nextion screen is released, the image of this component will be changed.
Author
Wu Pengfei (email:pengf.nosp@m.ei.w.nosp@m.u@ite.nosp@m.ad.c.nosp@m.c)
Date
2015/7/10