parent
9f415e9b5d
commit
cacf637c4b
3 changed files with 315 additions and 0 deletions
@ -0,0 +1,20 @@ |
|||||||
|
#include "ovo.h" |
||||||
|
#include "digital-screen.h" |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void setup(){ |
||||||
|
digital_clock_ini(); |
||||||
|
} |
||||||
|
|
||||||
|
DigitalClock dig(22); |
||||||
|
|
||||||
|
void loop(){ |
||||||
|
|
||||||
|
dig.core(); |
||||||
|
dig.hide(); |
||||||
|
dig.show(); |
||||||
|
|
||||||
|
} |
||||||
|
|
@ -0,0 +1,171 @@ |
|||||||
|
|
||||||
|
/**
|
||||||
|
* digital-screen Functions |
||||||
|
* |
||||||
|
* @category digital-screen |
||||||
|
* @package digital-screen.h |
||||||
|
* @copyright Copyright (c) 2019 EEENeko (https://github.com/eeeneko)
|
||||||
|
* @license GNU General Public License 2.0 |
||||||
|
* @version 0.0.1 |
||||||
|
*/ |
||||||
|
|
||||||
|
|
||||||
|
#ifndef _DIGITAL_SCREEN_H |
||||||
|
#define _DIGITAL_SCREEN_H |
||||||
|
|
||||||
|
#include "ovo.h" |
||||||
|
|
||||||
|
|
||||||
|
#ifndef DIG_1_1 |
||||||
|
#define DIG_1_1 9 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_1_2 |
||||||
|
#define DIG_1_2 10 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_1_3 |
||||||
|
#define DIG_1_3 11 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_1_4 |
||||||
|
#define DIG_1_4 12 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_2_1 |
||||||
|
#define DIG_2_1 5 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_2_2 |
||||||
|
#define DIG_2_2 6 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_2_3 |
||||||
|
#define DIG_2_3 7 |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifndef DIG_2_4 |
||||||
|
#define DIG_2_4 8 |
||||||
|
#endif |
||||||
|
|
||||||
|
|
||||||
|
class DigitalClock{ |
||||||
|
|
||||||
|
public: |
||||||
|
|
||||||
|
DigitalClock(int group = 22){ |
||||||
|
|
||||||
|
this->_group = group; |
||||||
|
_ice = 0; |
||||||
|
_cnt = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* control digital screens |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param int num # 待显示数字 |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
inline void digital_show(int num){ |
||||||
|
|
||||||
|
if(num == -1 || _ice == 1){ |
||||||
|
digital_show_1(15); |
||||||
|
digital_show_2(15); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
digital_show_1(num % 10); |
||||||
|
digital_show_2((num / 10) % 10); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void core(){ |
||||||
|
|
||||||
|
setInterval([&](){ |
||||||
|
this->digital_show(this->_cnt % this->_group); |
||||||
|
this->_cnt++; |
||||||
|
}, 1000); |
||||||
|
} |
||||||
|
|
||||||
|
inline void hide(){ |
||||||
|
this->_ice = 1; |
||||||
|
} |
||||||
|
|
||||||
|
inline void show(){ |
||||||
|
this->_ice = 0; |
||||||
|
} |
||||||
|
|
||||||
|
inline void reset(){ |
||||||
|
this->_cnt = 0; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
|
||||||
|
int _group; |
||||||
|
int _ice; |
||||||
|
unsigned int _cnt; |
||||||
|
|
||||||
|
/**
|
||||||
|
* control digital screen 1 |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param int num # 待显示数字 |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
void digital_show_1(int num){ |
||||||
|
|
||||||
|
digitalWrite(DIG_1_1, !!(num & (0x01<<0))); |
||||||
|
digitalWrite(DIG_1_2, !!(num & (0x01<<1))); |
||||||
|
digitalWrite(DIG_1_3, !!(num & (0x01<<2))); |
||||||
|
digitalWrite(DIG_1_4, !!(num & (0x01<<3))); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* control digital screen 2 |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param int num # 待显示数字 |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
void digital_show_2(int num){ |
||||||
|
|
||||||
|
digitalWrite(DIG_2_1, !!(num & (0x01<<0))); |
||||||
|
digitalWrite(DIG_2_2, !!(num & (0x01<<1))); |
||||||
|
digitalWrite(DIG_2_3, !!(num & (0x01<<2))); |
||||||
|
digitalWrite(DIG_2_4, !!(num & (0x01<<3))); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* digital-clock setup |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param void |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
void digital_clock_ini(){ |
||||||
|
|
||||||
|
pinMode(DIG_1_1, OUTPUT); |
||||||
|
pinMode(DIG_1_2, OUTPUT); |
||||||
|
pinMode(DIG_1_3, OUTPUT); |
||||||
|
pinMode(DIG_1_4, OUTPUT); |
||||||
|
pinMode(DIG_2_1, OUTPUT); |
||||||
|
pinMode(DIG_2_2, OUTPUT); |
||||||
|
pinMode(DIG_2_3, OUTPUT); |
||||||
|
pinMode(DIG_2_4, OUTPUT); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,124 @@ |
|||||||
|
|
||||||
|
/**
|
||||||
|
* ovo-arduino Main File |
||||||
|
* |
||||||
|
* @category ovo-arduino |
||||||
|
* @package ovo.cpp |
||||||
|
* @copyright Copyright (c) 2019 EEENeko (https://github.com/eeeneko)
|
||||||
|
* @license GNU General Public License 2.0 |
||||||
|
* @version 0.0.1 |
||||||
|
*/ |
||||||
|
|
||||||
|
|
||||||
|
#ifndef _OVO_H |
||||||
|
#define _OVO_H |
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Like set Timeout in JS |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param auto function (allow Lambda) |
||||||
|
* @param int millisecond to dealy |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
void setTimeout(auto function,const int delay) |
||||||
|
{ |
||||||
|
static bool on = 1; |
||||||
|
if(on == 1){ |
||||||
|
static unsigned long startTime = millis();
|
||||||
|
|
||||||
|
if(millis() - startTime > delay){ |
||||||
|
function(); |
||||||
|
on == 0; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Like set Interval in JS |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param auto function (allow Lambda) |
||||||
|
* @param int millisecond of interval |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
void setInterval(auto function, const int delay) |
||||||
|
{ |
||||||
|
static unsigned long startTime = millis();
|
||||||
|
|
||||||
|
if(millis() - startTime > delay){ |
||||||
|
function(); |
||||||
|
startTime = millis(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Switch between function1 and function2 for delay1 and delay2 |
||||||
|
* |
||||||
|
* @Author yimian |
||||||
|
* @param auto function1 (allow Lambda) |
||||||
|
* @param auto function2 (allow Lambda) |
||||||
|
* @param int millisecond of interval |
||||||
|
* @param int millisecond of interval |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
void setSwitch(auto function1, auto function2, const int delay1, const int delay2) |
||||||
|
{ |
||||||
|
static unsigned long startTime = millis();
|
||||||
|
|
||||||
|
if(millis() - startTime < delay1){ |
||||||
|
function1(); |
||||||
|
}else if(millis() - startTime >= delay1 && millis() - startTime < delay1 + delay2){ |
||||||
|
function2(); |
||||||
|
}else if(millis() - startTime >= delay1 + delay2){ |
||||||
|
startTime = millis(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make direct voltage change to gradually change |
||||||
|
* |
||||||
|
* @author yimian |
||||||
|
* @category ovo |
||||||
|
* @package ovo |
||||||
|
*/ |
||||||
|
class slowWrite |
||||||
|
{ |
||||||
|
public: |
||||||
|
inline void set(int t_pin, unsigned int t_delay){ |
||||||
|
pin = t_pin; |
||||||
|
delay = t_delay; |
||||||
|
}; |
||||||
|
inline void high(){ |
||||||
|
startTime = millis(); |
||||||
|
state = 1; |
||||||
|
}; |
||||||
|
inline void low(){ |
||||||
|
startTime = millis(); |
||||||
|
state = 0; |
||||||
|
}; |
||||||
|
inline void run(){ |
||||||
|
if(state == 1 && millis() - startTime < delay){ |
||||||
|
analogWrite(pin, ((millis() - startTime) * 255 / delay));Serial.println(((millis() - startTime) * 255 / delay)); |
||||||
|
}else if(state == 0 && millis() - startTime < delay){ |
||||||
|
analogWrite(pin, 255-((millis() - startTime) * 255 / delay));Serial.println(255-((millis() - startTime) * 255 / delay)); |
||||||
|
}else{ |
||||||
|
state = -1; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
private: |
||||||
|
unsigned long startTime; |
||||||
|
int state; |
||||||
|
int pin; |
||||||
|
unsigned int delay;
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue