-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOInput.cpp
111 lines (99 loc) · 1.91 KB
/
IOInput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <Arduino.h>
#include <stddef.h>
#include <stdint.h>
#include "IOInput.h"
#define IOINPUT_PORT Serial
#define IOINPUT_DBG(fmt, ...) //IOINPUT_PORT.printf("\r\n>IOINPUT< " fmt, ##__VA_ARGS__)
IOInput::IOInput(short _IO, uint8_t Lever, uint16_t S_To, uint16_t H_Cnt, uint16_t L_Cnt)
: ToE(100), _IOPIN(_IO), State(Lever), SampleTo(S_To), SampleHigh(H_Cnt), SampleLow(L_Cnt)
{
pinMode(_IOPIN, INPUT_PULLUP);
}
void IOInput::loop(void)
{
if (!ToE.ToEExpired())
return;
ToE.ToEUpdate(SampleTo);
if (digitalRead(_IOPIN) == LOW)
{
LowCount++;
if (LowCount == SampleLow)
{
if(State == HIGH) _Edge = IO_FALLING;
State = LOW;
LowTime = millis();
IOINPUT_DBG("%u: LOW",_IOPIN);
}
if (LowCount >= SampleLow)
{
HighCount = 0;
LowCount = SampleLow;
}
}
else
{
HighCount++;
if (HighCount == SampleHigh)
{
if(State == LOW) _Edge = IO_RISING;
State = HIGH;
HighTime = millis();
IOINPUT_DBG("%u: HIGH",_IOPIN);
}
if (HighCount >= SampleHigh)
{
LowCount = 0;
HighCount = SampleHigh;
}
}
}
uint32_t IOInput::GetTime(uint8_t Lever)
{
if (Lever == State)
{
if (State == HIGH)
return (millis() - HighTime);
else
return (millis() - LowTime);
}
else
return 0;
}
uint8_t IOInput::GetEdge(uint8_t Edge)
{
uint8_t Ed = 0;
if(_Edge == Edge){
Ed = 1;
_Edge = State;
}
return Ed;
}
uint8_t IOInput::GetStatus(void)
{
return State;
}
void IOInput::set_high_cnt_ms(uint16_t High)
{
SampleHigh = High;
}
void IOInput::set_low_cnt_ms(uint16_t Low)
{
SampleLow = Low;
}
void IOInput::set_status_init(uint8_t St)
{
State = St;
}
void IOInput::set_sample_ms(uint16_t Sp)
{
SampleTo = Sp;
ToE.ToEUpdate(SampleTo);
}
void IOInput::set_low_time_ms(uint32_t Low)
{
LowTime = millis() - Low;
}
void IOInput::set_high_time_ms(uint32_t High)
{
HighTime = millis() - High;
}