diff --git a/NexTimer.cpp b/NexTimer.cpp new file mode 100644 index 00000000..2e2f166 --- /dev/null +++ b/NexTimer.cpp @@ -0,0 +1,86 @@ +/** + * @file NexButton.cpp + * + * The implementation of class NexTimer. + * + * @author huang xianming (email:) + * @date 2015/8/26 + * @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 "NexTimer.h" + +NexTimer::NexTimer(uint8_t pid, uint8_t cid, const char *name) + :NexTouch(pid, cid, name) +{ +} + +void NexTimer::attachTimer(NexTouchEventCb timer, void *ptr) +{ + NexTouch::attachPop(timer, ptr); +} + +void NexTimer::detachTimer(void) +{ + NexTouch::detachPop(); +} + + +bool NexTimer::getCycle(uint32_t *number) +{ + String cmd = String("get "); + cmd += getObjName(); + cmd += ".tim"; + sendCommand(cmd.c_str()); + return recvRetNumber(number); +} + +bool NexTimer::setCycle(uint32_t number) +{ + char buf[10] = {0}; + String cmd; + + utoa(number, buf, 10); + cmd += getObjName(); + cmd += ".tim="; + cmd += buf; + + sendCommand(cmd.c_str()); + return recvRetCommandFinished(); +} + + +bool NexTimer::enable(void) +{ + char buf[10] = {0}; + String cmd; + + utoa(1, buf, 10); + cmd += getObjName(); + cmd += ".en="; + cmd += buf; + + sendCommand(cmd.c_str()); + return recvRetCommandFinished(); +} + +bool NexTimer::disable(void) +{ + char buf[10] = {0}; + String cmd; + + utoa(0, buf, 10); + cmd += getObjName(); + cmd += ".en="; + cmd += buf; + + sendCommand(cmd.c_str()); + return recvRetCommandFinished(); +} + + diff --git a/NexTimer.h b/NexTimer.h new file mode 100644 index 00000000..2800c22 --- /dev/null +++ b/NexTimer.h @@ -0,0 +1,96 @@ +/** + * @file NexButton.h + * + * The definition of class NexButton. + * + * @author huang xianming (email:) + * @date 2015/8/26 + * + * @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 __NEXTIMER_H__ +#define __NEXTIMER_H__ + +#include "NexTouch.h" +#include "NexHardware.h" +/** + * @addtogroup Component + * @{ + */ + +/** + * NexTimer component. + * + * Commonly, you want to do something after set timer cycle and enable it. It is recommanded that only + * call @ref NexTimer::attachPop to satisfy your purpose. + * + */ +class NexTimer: public NexTouch +{ +public: /* methods */ + + /** + * @copydoc NexObject::NexObject(uint8_t pid, uint8_t cid, const char *name); + */ + NexTimer(uint8_t pid, uint8_t cid, const char *name); + + /** + * Attach an callback function of timer respond event. + * + * @param push - callback called with ptr when a timer respond event occurs. + * @param ptr - parameter passed into push[default:NULL]. + * @return none. + * + * @note If calling this method multiply, the last call is valid. + */ + void attachTimer(NexTouchEventCb timer, void *ptr = NULL); + + /** + * Detach an callback function. + * + * @return none. + */ + void detachTimer(void); + + /** + * Get the value of timer cycle val. + * + * @param number - an output parameter to save the value of timer cycle. + * + * @retval true - success. + * @retval false - failed. + */ + bool getCycle(uint32_t *number); + /** + * Set the value of timer cycle val. + * + * @param number - the value of timer cycle. + * + * @retval true - success. + * @retval false - failed. + */ + + bool setCycle(uint32_t number); + /** + *contorl timer enable. + */ + bool enable(void); + /** + *contorl timer disable. + */ + bool disable(void); + + +}; +/** + * @} + */ + + +#endif /* #ifndef __NEXBUTTON_H__ */ diff --git a/Nextion.h b/Nextion.h index d5125c8..6cd47cf 100644 --- a/Nextion.h +++ b/Nextion.h @@ -32,5 +32,7 @@ #include "NexSlider.h" #include "NexText.h" #include "NexWaveform.h" +#include "NexTimer.h" + #endif /* #ifndef __NEXTION_H__ */ diff --git a/examples/CompTimer/CompTimer.HMI b/examples/CompTimer/CompTimer.HMI new file mode 100644 index 00000000..b6b4477 Binary files /dev/null and b/examples/CompTimer/CompTimer.HMI differ diff --git a/examples/CompTimer/CompTimer.ino b/examples/CompTimer/CompTimer.ino new file mode 100644 index 00000000..88760d6 --- /dev/null +++ b/examples/CompTimer/CompTimer.ino @@ -0,0 +1,123 @@ +/** + * @example CompTimer.ino + * + * @par How to Use + * This example shows that ,when the OFF button component on the Nextion screen is released, + * the timer will opened,the text will show number changed and push the ADDTIME button timer + * cycle value will increase,when push the DECTIME button timer cycle value will reduce. + * + * @author huang xianming (email:) + * @date 2015/8/25 + * @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 "Nextion.h" + +NexButton b0 = NexButton(0, 2, "b0"); +NexButton b1 = NexButton(0, 5, "b1"); +NexButton b2 = NexButton(0, 6, "b2"); +NexText t0 = NexText(0, 3, "t0"); +NexText t1 = NexText(0, 4, "t1"); +NexTimer tm0 = NexTimer(0, 1, "tm0"); + + +char buffer[100] = {0}; +uint32_t number_timer = 0; +uint32_t number_enable = 0; +uint32_t number_cycle = 100; + + + +NexTouch *nex_listen_list[] = +{ + &b0, + &b1, + &b2, + &t0, + &t1, + &tm0, + NULL +}; +/* + * Button component pop callback function. + * In this example,the button can open the timer when it is released. + */ +void b0PopCallback(void *ptr) +{ + if(number_enable == 1) + { + tm0.enable(); + number_enable = 0; + b0.setText("ON"); + } + else if (number_enable ==0) + { + tm0.disable(); + number_enable =1; + b0.setText("OFF"); + } +} +/* + * Button component pop callback function. + * In this example,the timer's cycle value will increase when it is released. + */ +void b1PopCallback(void *ptr) +{ + tm0.getCycle(&number_cycle); + number_cycle = number_cycle + 100; + tm0.setCycle(number_cycle); + memset(buffer, 0, sizeof(buffer)); + itoa(number_cycle, buffer, 10); + t1.setText(buffer); +} + +/* + * Button component pop callback function. + * In this example,the timer's cycle value will reduce when it is released. + */ + +void b2PopCallback(void *ptr) +{ + tm0.getCycle(&number_cycle); + if (number_cycle >100) + { + number_cycle = number_cycle - 100; + } + tm0.setCycle(number_cycle); + memset(buffer, 0, sizeof(buffer)); + itoa(number_cycle, buffer, 10); + t1.setText(buffer); +} + +/* + * The timer respond function + * In this example,the timer will respond when set cycle time done and puls one for a variable. + */ + +void tm0TimerCallback(void *ptr) +{ + number_timer++; + memset(buffer, 0, sizeof(buffer)); + itoa(number_timer, buffer, 10); + t0.setText(buffer); +} +void setup(void) +{ + nexInit(); + b0.attachPop(b0PopCallback); + tm0.attachTimer(tm0TimerCallback); + b1.attachPop(b1PopCallback); + b2.attachPop(b2PopCallback); + dbSerialPrintln("setup done"); +} + +void loop(void) +{ + nexLoop(nex_listen_list); +} + diff --git a/examples/CompTimer/CompTimer.tft b/examples/CompTimer/CompTimer.tft new file mode 100644 index 00000000..dd47c3f Binary files /dev/null and b/examples/CompTimer/CompTimer.tft differ