diff --git a/NexButton.cpp b/NexButton.cpp index 1447fb5..f673427 100644 --- a/NexButton.cpp +++ b/NexButton.cpp @@ -19,8 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexButton::NexButton(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop, void *pop_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr) +NexButton::NexButton(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } @@ -61,24 +61,3 @@ bool NexButton::setText(const char *buffer) return recvRetCommandFinished(); } -/** - * Register button pop callback function. - * - * @param pop - the pointer to button pop callback function. - * @param ptr - the parameter to be transmitted to button pop callback function. - */ -void NexButton::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload button pop callback function. - * - */ -void NexButton::detachPop(void) -{ - NexTouch::detachPop(); -} - - diff --git a/NexButton.h b/NexButton.h index 790fced..69efa1d 100644 --- a/NexButton.h +++ b/NexButton.h @@ -26,10 +26,7 @@ class NexButton: public NexTouch { public: /* methods */ - NexButton(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop = NULL, void *pop_ptr = NULL); - - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); + NexButton(NexPid pid, NexCid cid, const char *name); uint16_t getText(char *buffer, uint16_t len); bool setText(const char *buffer); diff --git a/NexCrop.cpp b/NexCrop.cpp index 7d181bc..98ebd69 100644 --- a/NexCrop.cpp +++ b/NexCrop.cpp @@ -19,8 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexCrop::NexCrop(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop, void *pop_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr) +NexCrop::NexCrop(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } @@ -63,23 +63,3 @@ bool NexCrop::setPic(uint32_t number) return recvRetCommandFinished(); } -/** - * Register crop pop callback function. - * - * @param pop - the pointer to crop pop callback function. - * @param ptr - the parameter to be transmitted to crop pop callback function. - */ -void NexCrop::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload crop pop callback function. - * - */ -void NexCrop::detachPop(void) -{ - NexTouch::detachPop(); -} - diff --git a/NexCrop.h b/NexCrop.h index 7137162..5200335 100644 --- a/NexCrop.h +++ b/NexCrop.h @@ -26,11 +26,8 @@ class NexCrop: public NexTouch { public: /* methods */ - NexCrop(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop = NULL, void *pop_ptr = NULL); + NexCrop(NexPid pid, NexCid cid, const char *name); - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); - bool getPic(uint32_t *number); bool setPic(uint32_t number); }; diff --git a/NexGauge.cpp b/NexGauge.cpp index 649aacc..c6043d5 100644 --- a/NexGauge.cpp +++ b/NexGauge.cpp @@ -20,7 +20,7 @@ * */ NexGauge::NexGauge(NexPid pid, NexCid cid, const char *name) - :NexTouch(pid, cid, name) + :NexObject(pid, cid, name) { } diff --git a/NexGauge.h b/NexGauge.h index 38f155c..e7685c8 100644 --- a/NexGauge.h +++ b/NexGauge.h @@ -23,7 +23,7 @@ * NexGauge,subclass of NexTouch,provides simple methods to control gauge component. * */ -class NexGauge: public NexTouch +class NexGauge: public NexObject { public: /* methods */ NexGauge(NexPid pid, NexCid cid, const char *name); diff --git a/NexHotspot.cpp b/NexHotspot.cpp index aba96fe..f35ab56 100644 --- a/NexHotspot.cpp +++ b/NexHotspot.cpp @@ -19,49 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexHotspot::NexHotspot(NexPid pid, NexCid cid, const char *name, - NexTouchEventCb pop, void *pop_ptr, - NexTouchEventCb push, void *push_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr, push, push_ptr) +NexHotspot::NexHotspot(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } -/** - * Register hotspot push callback function. - * - * @param push - the pointer to hotspot push callback function. - * @param ptr - the parameter to be transmitted to hotspot push callback function. - */ -void NexHotspot::attachPush(NexTouchEventCb push, void *ptr) -{ - NexTouch::attachPush(push, ptr); -} - -/** - * Unload hotsopt push callback function. - * - */ -void NexHotspot::detachPush(void) -{ - NexTouch::detachPush(); -} - -/** - * Register hotspot pop callback function. - * - * @param pop - the pointer to hotspot pot callback function. - * @param ptr - the parameter to be transmitted to hotspot pop callback function. - */ -void NexHotspot::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload hotsopt pop callback function. - * - */ -void NexHotspot::detachPop(void) -{ - NexTouch::detachPop(); -} diff --git a/NexHotspot.h b/NexHotspot.h index f739904..09f9528 100644 --- a/NexHotspot.h +++ b/NexHotspot.h @@ -26,15 +26,7 @@ class NexHotspot: public NexTouch { public: /* methods */ - NexHotspot(NexPid pid, NexCid cid, const char *name, - NexTouchEventCb pop = NULL, void *pop_ptr = NULL, - NexTouchEventCb push = NULL, void *push_ptr = NULL); - - void attachPush(NexTouchEventCb push, void *ptr = NULL); - void detachPush(void); - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); - + NexHotspot(NexPid pid, NexCid cid, const char *name); }; #endif /* #ifdef __cplusplus */ diff --git a/NexObject.cpp b/NexObject.cpp new file mode 100644 index 00000000..5c3a51a --- /dev/null +++ b/NexObject.cpp @@ -0,0 +1,87 @@ +/** + * @file NexObject.cpp + * + * The root of all classes in Nextion library. + * + * @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" + +/** + * Constructor + * + * @param pid - page id. + * @param cid - component id. + * @param name - component name. + */ +NexObject::NexObject(NexPid pid, NexCid cid, const char *name) +{ + this->pid = pid; + this->cid = cid; + this->name = name; + dbSerialPrintln("NexObject called"); +} + +/** + * Get page id. + * + * @return the id of page. + */ +NexPid NexObject::getObjPid(void) +{ + return pid; +} + +/** + * Get component id. + * + * @return the id of component. + */ +NexCid NexObject::getObjCid(void) +{ + return cid; +} + +/** + * Get component name. + * + * @return the name of component. + */ +const char* NexObject::getObjName(void) +{ + return name; +} + +/** + * Print current object address,page id,component id, + * component name,pop event function address,push event function address. + * + */ +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 new file mode 100644 index 00000000..11402f8 --- /dev/null +++ b/NexObject.h @@ -0,0 +1,45 @@ +/** + * @file NexObject.h + * + * The root of all classes in Nextion library. + * + * @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__ +#ifdef __cplusplus +#include +#include "NexSerialConfig.h" + +typedef uint8_t NexPid; +typedef uint8_t NexCid; + +/** + * Root Class of Nextion Components. + */ +class NexObject +{ +public: /* methods */ + NexObject(NexPid pid, NexCid cid, const char *name); + + NexPid getObjPid(void); + NexCid getObjCid(void); + const char *getObjName(void); + void printObjInfo(void); + +private: /* data */ + NexPid pid; /* Page ID */ + NexCid cid; /* Component ID */ + const char *name; /* An unique name */ +}; + +#endif /* #ifdef __cplusplus */ +#endif /* #ifndef __NEXOBJECT_H__ */ diff --git a/NexPage.cpp b/NexPage.cpp index e44958e..c14d40e 100644 --- a/NexPage.cpp +++ b/NexPage.cpp @@ -19,8 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexPage::NexPage(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop, void *pop_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr) +NexPage::NexPage(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } @@ -46,22 +46,3 @@ bool NexPage::show(void) return recvRetCommandFinished(); } -/** - * Register page pop callback function. - * - * @param pop - the pointer to page pop callback function. - * @param ptr - the parameter to be transmitted to page pop callback function. - */ -void NexPage::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload page pop callback function. - * - */ -void NexPage::detachPop(void) -{ - NexTouch::detachPop(); -} diff --git a/NexPage.h b/NexPage.h index 1ca2bae..5a17d9e 100644 --- a/NexPage.h +++ b/NexPage.h @@ -26,12 +26,8 @@ class NexPage: public NexTouch { public: /* methods */ - NexPage(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop = NULL, void *pop_ptr = NULL); + NexPage(NexPid pid, NexCid cid, const char *name); bool show(void); - - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); - }; #endif /* #ifdef __cplusplus */ diff --git a/NexPicture.cpp b/NexPicture.cpp index 4902688..2933c10 100644 --- a/NexPicture.cpp +++ b/NexPicture.cpp @@ -19,8 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexPicture::NexPicture(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop, void *pop_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr) +NexPicture::NexPicture(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } @@ -62,24 +62,4 @@ bool NexPicture::setPic(uint32_t number) sendCommand(cmd.c_str()); return recvRetCommandFinished(); } - -/** - * Register picture pop callback function. - * - * @param pop - the pointer to picture pop callback function. - * @param ptr - the parameter to be transmitted to picture pop callback function. - */ -void NexPicture::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload picture pop callback function. - * - */ -void NexPicture::detachPop(void) -{ - NexTouch::detachPop(); -} diff --git a/NexPicture.h b/NexPicture.h index 887f2e8..8816e6a 100644 --- a/NexPicture.h +++ b/NexPicture.h @@ -26,11 +26,8 @@ class NexPicture: public NexTouch { public: /* methods */ - NexPicture(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop = NULL, void *pop_ptr = NULL); + NexPicture(NexPid pid, NexCid cid, const char *name); - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); - bool getPic(uint32_t *number); bool setPic(uint32_t number); }; diff --git a/NexProgressBar.cpp b/NexProgressBar.cpp index b33d02c..6f06333 100644 --- a/NexProgressBar.cpp +++ b/NexProgressBar.cpp @@ -20,7 +20,7 @@ * */ NexProgressBar::NexProgressBar(NexPid pid, NexCid cid, const char *name) - :NexTouch(pid, cid, name) + :NexObject(pid, cid, name) { } diff --git a/NexProgressBar.h b/NexProgressBar.h index 90740c0..d0af8a9 100644 --- a/NexProgressBar.h +++ b/NexProgressBar.h @@ -23,7 +23,7 @@ * NexProgressBar,subclass of NexTouch,provides simple methods to control progress bar component. * */ -class NexProgressBar: public NexTouch +class NexProgressBar: public NexObject { public: /* methods */ NexProgressBar(NexPid pid, NexCid cid, const char *name); diff --git a/NexSlider.cpp b/NexSlider.cpp index a529802..bae2707 100644 --- a/NexSlider.cpp +++ b/NexSlider.cpp @@ -19,9 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexSlider::NexSlider(NexPid pid, NexCid cid, const char *name, - NexTouchEventCb pop, void *pop_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr) +NexSlider::NexSlider(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } @@ -64,23 +63,3 @@ bool NexSlider::setValue(uint32_t number) return recvRetCommandFinished(); } -/** - * Register slider pop callback function. - * - * @param pop - the pointer to picture pop callback function. - * @param ptr - the parameter to be transmitted to picture pop callback function. - */ -void NexSlider::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload slider pop callback function. - * - */ -void NexSlider::detachPop(void) -{ - NexTouch::detachPop(); -} - diff --git a/NexSlider.h b/NexSlider.h index 6e7b509..4150150 100644 --- a/NexSlider.h +++ b/NexSlider.h @@ -26,11 +26,8 @@ class NexSlider: public NexTouch { public: /* methods */ - NexSlider(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop = NULL, void *pop_ptr = NULL); - - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); - + NexSlider(NexPid pid, NexCid cid, const char *name); + bool getValue(uint32_t *number); bool setValue(uint32_t number); }; diff --git a/NexText.cpp b/NexText.cpp index cde4dc6..9fee731 100644 --- a/NexText.cpp +++ b/NexText.cpp @@ -19,8 +19,8 @@ * Constructor,inherited NexTouch's constructor function. * */ -NexText::NexText(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop, void *pop_ptr) - :NexTouch(pid, cid, name, pop, pop_ptr) +NexText::NexText(NexPid pid, NexCid cid, const char *name) + :NexTouch(pid, cid, name) { } @@ -61,23 +61,3 @@ bool NexText::setText(const char *buffer) return recvRetCommandFinished(); } -/** - * Register text pop callback function. - * - * @param pop - the pointer to text pop callback function. - * @param ptr - the parameter to be transmitted to text pop callback function. - */ -void NexText::attachPop(NexTouchEventCb pop, void *ptr) -{ - NexTouch::attachPop(pop, ptr); -} - -/** - * Unload text pop callback function. - * - */ -void NexText::detachPop(void) -{ - NexTouch::detachPop(); -} - diff --git a/NexText.h b/NexText.h index 37e74ab..3c0f167 100644 --- a/NexText.h +++ b/NexText.h @@ -26,11 +26,8 @@ class NexText: public NexTouch { public: /* methods */ - NexText(NexPid pid, NexCid cid, const char *name, NexTouchEventCb pop = NULL, void *pop_ptr = NULL); + NexText(NexPid pid, NexCid cid, const char *name); - void attachPop(NexTouchEventCb pop, void *ptr = NULL); - void detachPop(void); - uint16_t getText(char *buffer, uint16_t len); bool setText(const char *buffer); }; diff --git a/NexTouch.cpp b/NexTouch.cpp index 05dfc3f..ee4ed99 100644 --- a/NexTouch.cpp +++ b/NexTouch.cpp @@ -19,85 +19,19 @@ /** * Constructor of Nextouch. * - * @param pid - page id. - * @param cid - component id. - * @param name - component name. * @param pop - pop event function pointer. * @param pop_ptr - the parameter was transmitted to pop event function pointer. * @param push - push event function pointer. * @param push_ptr - the parameter was transmitted to push event function pointer. * */ -NexTouch::NexTouch(NexPid pid, NexCid cid, const char *name, - NexTouchEventCb pop, void *pop_ptr, - NexTouchEventCb push, void *push_ptr) +NexTouch::NexTouch(NexPid pid, NexCid cid, const char *name) + :NexObject(pid, cid, name) { - this->pid = pid; - this->cid = cid; - this->name = name; - this->cbPush = push; - this->cbPop = pop; - this->__cbpop_ptr = pop_ptr; - this->__cbpush_ptr = push_ptr; -} - -/** - * Get page id. - * - * @return the id of page. - */ -NexPid NexTouch::getPid(void) -{ - return pid; -} - -/** - * Get component id. - * - * @return the id of component. - */ -NexCid NexTouch::getCid(void) -{ - return cid; -} - -/** - * Get component name. - * - * @return the name of component. - */ -const char* NexTouch::getObjName(void) -{ - return name; -} - -/** - * Print current object address,page id,component id, - * component name,pop event function address,push event function address. - * - */ -void NexTouch::print(void) -{ - dbSerialPrint("["); - dbSerialPrint((uint32_t)this); - dbSerialPrint(":"); - dbSerialPrint(pid); - dbSerialPrint(","); - dbSerialPrint(cid); - dbSerialPrint(","); - if (name) - { - dbSerialPrint(name); - } - else - { - dbSerialPrint("(null)"); - } - dbSerialPrint(","); - dbSerialPrint((uint32_t)cbPush); - dbSerialPrint(","); - dbSerialPrint((uint32_t)cbPop); - dbSerialPrintln("]"); + this->cbPush = NULL; + this->cbPop = NULL; + this->__cbpop_ptr = NULL; + this->__cbpush_ptr = NULL; } void NexTouch::attachPush(NexTouchEventCb push, void *ptr) @@ -152,9 +86,9 @@ void NexTouch::iterate(NexTouch **list, NexPid pid, NexCid cid, NexEventType eve for(i = 0; (e = list[i]) != NULL; i++) { - if (e->getPid() == pid && e->getCid() == cid) + if (e->getObjPid() == pid && e->getObjCid() == cid) { - e->print(); + e->printObjInfo(); if (NEX_EVENT_PUSH == event) { e->push(); diff --git a/NexTouch.h b/NexTouch.h index e8fbf8c..bfa127b 100644 --- a/NexTouch.h +++ b/NexTouch.h @@ -18,9 +18,7 @@ #ifdef __cplusplus #include #include "NexSerialConfig.h" - -typedef uint8_t NexPid; -typedef uint8_t NexCid; +#include "NexObject.h" typedef void (*NexTouchEventCb)(void *ptr); @@ -34,35 +32,24 @@ typedef enum { * Root Class of Nextion Components. * */ -class NexTouch +class NexTouch: public NexObject { public: /* static methods */ static void iterate(NexTouch **list, NexPid pid, NexCid cid, NexEventType event); public: /* methods */ - NexTouch(NexPid pid, NexCid cid, const char *name, - NexTouchEventCb pop = NULL, void *pop_ptr = NULL, - NexTouchEventCb push = NULL, void *push_ptr = NULL); + NexTouch(NexPid pid, NexCid cid, const char *name); - NexPid getPid(void); - NexCid getCid(void); - const char *getObjName(void); - -protected: /* methods */ void attachPush(NexTouchEventCb push, void *ptr = NULL); void detachPush(void); void attachPop(NexTouchEventCb pop, void *ptr = NULL); void detachPop(void); private: /* methods */ - void print(void); void push(void); void pop(void); private: /* data */ - NexPid pid; /* Page ID */ - NexCid cid; /* Component ID */ - const char *name; /* An unique name */ NexTouchEventCb cbPush; void *__cbpush_ptr; NexTouchEventCb cbPop; diff --git a/NexWaveform.cpp b/NexWaveform.cpp index 3d38243..355f45c 100644 --- a/NexWaveform.cpp +++ b/NexWaveform.cpp @@ -20,7 +20,7 @@ * */ NexWaveform::NexWaveform(NexPid pid, NexCid cid, const char *name) - :NexTouch(pid, cid, name) + :NexObject(pid, cid, name) { } @@ -43,7 +43,7 @@ bool NexWaveform::addValue(uint8_t ch, uint8_t number) return false; } - sprintf(buf, "add %u,%u,%u", getCid(), ch, number); + sprintf(buf, "add %u,%u,%u", getObjCid(), ch, number); sendCommand(buf); return true; diff --git a/NexWaveform.h b/NexWaveform.h index b6fa2aa..ba6dbec 100644 --- a/NexWaveform.h +++ b/NexWaveform.h @@ -22,7 +22,7 @@ * NexWaveform,subclass of NexTouch,provides simple methods to control NexWaveform component. * */ -class NexWaveform: public NexTouch +class NexWaveform: public NexObject { public: /* methods */ NexWaveform(NexPid pid, NexCid cid, const char *name); diff --git a/examples/CompText/CompText.ino b/examples/CompText/CompText.ino index 75d21b0..465e2cf 100644 --- a/examples/CompText/CompText.ino +++ b/examples/CompText/CompText.ino @@ -19,9 +19,9 @@ void t0PopCallback(void *ptr); void b0PopCallback(void *ptr); void b1PopCallback(void *ptr); -NexText t0 = NexText(0, 1, "t0", t0PopCallback); -NexButton b0 = NexButton(0, 2, "b0", b0PopCallback); -NexButton b1 = NexButton(0, 3, "b1", b1PopCallback); +NexText t0 = NexText(0, 1, "t0"); +NexButton b0 = NexButton(0, 2, "b0"); +NexButton b1 = NexButton(0, 3, "b1"); char buffer[100] = {0}; @@ -80,6 +80,9 @@ void b1PopCallback(void *ptr) void setup(void) { nexInit(); + t0.attachPop(t0PopCallback); + b0.attachPop(b0PopCallback); + b1.attachPop(b1PopCallback); dbSerialPrintln("setup done"); }