diff --git a/digital-clock.ino b/digital-clock.ino index 8321d19..9cecaf9 100644 --- a/digital-clock.ino +++ b/digital-clock.ino @@ -1,20 +1,91 @@ + +/** + * digital clock code for EEE116 + * + * @category digital-clock + * @package digital-clock.ino + * @copyright Copyright (c) 2019 IoTcat (https://iotcat.me) + * @license GNU General Public License 2.0 + * @version 0.0.1 + */ + + +/*** 引入关键库 ****/ #include "ovo.h" -#include "digital-screen.h" +/*** 定义初始参数 ****/ + +//小组组号 +#define GROUP_ID 22 + +//时钟变换时间间隔 (毫秒) +#define INTERVAL_TIME 1000 + +//pin口 - 接译码器1 (显示个位) +#define DIG_1_1 9 //A +#define DIG_1_2 10 //B +#define DIG_1_3 11 //C +#define DIG_1_4 12 //D + +//pin口 - 接译码器2 (显示十位) +#define DIG_2_1 5 //A +#define DIG_2_2 6 //B +#define DIG_2_3 7 //C +#define DIG_2_4 8 //D + +//pin口 - 接开关 +#define SWI_OFF 3 + +//pin口 - 接reset键 +#define SWI_RESET 4 + + + +/*** 引入自定义库 ****/ + +#include "digital-screen.h" +#include "switch.h" +/*** 运行初始化 ****/ + void setup(){ - digital_clock_ini(); + + //时钟显示屏初始化 + digital_clock_ini(); + //开关组件初始化 + switch_ini(); } -DigitalClock dig(22); + +/*** 实例化组件 ****/ + +//时钟显示屏实例化 +DigitalClock clock(GROUP_ID); +//开关组件实例化 +Switch swi(SWI_OFF), reset(SWI_RESET); + + + +/*** 主循环程序 ****/ void loop(){ - dig.core(); - dig.hide(); - dig.show(); + /*** 主要控制逻辑 ****/ + + //如果reset键被按下,重置时钟 + if(reset.isPressed() == true) clock.reset(); + + //如果开关打开,则显示时钟,否则不显示时钟 + if(swi.getStatus() == true) clock.show(); + else clock.hide(); + + + /*** 守护进程 ****/ + clock.core(); + swi.core(); + reset.core(); } diff --git a/digital-screen.h b/digital-screen.h index da408d4..bac9b15 100644 --- a/digital-screen.h +++ b/digital-screen.h @@ -2,9 +2,9 @@ /** * digital-screen Functions * - * @category digital-screen + * @category digital-clock * @package digital-screen.h - * @copyright Copyright (c) 2019 EEENeko (https://github.com/eeeneko) + * @copyright Copyright (c) 2019 iotcat (https://iotcat.me) * @license GNU General Public License 2.0 * @version 0.0.1 */ @@ -15,6 +15,10 @@ #include "ovo.h" +#ifndef INTERVAL_TIME +#define INTERVAL_TIME 1000 +#endif + #ifndef DIG_1_1 #define DIG_1_1 9 @@ -48,7 +52,6 @@ #define DIG_2_4 8 #endif - class DigitalClock{ public: @@ -88,7 +91,7 @@ public: setInterval([&](){ this->digital_show(this->_cnt % this->_group); this->_cnt++; - }, 1000); + }, INTERVAL_TIME); } inline void hide(){ diff --git a/switch.h b/switch.h new file mode 100644 index 0000000..6edcceb --- /dev/null +++ b/switch.h @@ -0,0 +1,100 @@ + +/** + * switch Functions + * + * @category digital-clock + * @package switch.h + * @copyright Copyright (c) 2019 IoTcat (https://iotcat.me) + * @license GNU General Public License 2.0 + * @version 0.0.1 + */ + + +#ifndef _SWITCH_H +#define _SWITCH_H + +#include "ovo.h" + +#ifndef SWI_OFF +#define SWI_OFF 3 +#endif + +#ifndef SWI_RESET +#define SWI_RESET 4 +#endif + + +class Switch{ + +public: + + Switch(unsigned short pin = 3, bool type = HIGH){ + this->_pin = pin; + this->_type = type; + this->_isPressed = false; + this->_status = true; + } + + void core(){ + check_status(); + } + + bool getStatus(){ + return _status; + } + + bool isPressed(){ + return _isPressed; + } + +private: + unsigned short _pin; + bool _type; + bool _isPressed; + + bool _status; + + void set_isPressed(){ + + if(digitalRead(this->_pin) == this->_type){ + + this->_isPressed = true; + }else{ + this->_isPressed = false; + } + } + + void check_status(){ + + static bool s_fIsPressed = false; + set_isPressed(); + + // 检测开关键松开动作 + if(this->_isPressed != s_fIsPressed && this->_isPressed != _type){ + + this->_status = !(this->_status); + } + + } + +}; + + + + +/** + * switch setup + * + * @Author yimian + * @param void + * @return void + */ +void switch_ini(){ + + pinMode(SWI_OFF, INPUT); + pinMode(SWI_RESET, INPUT); + +} + + +#endif