IODebounce.cs
1.8 KB
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
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public class IODebounce<T>
{
static Dictionary<string, iovaluet> iolist = new Dictionary<string, iovaluet>();
public static bool Test(string ioType, IO_VALUE Current_VALUE, IO_VALUE Test_VALUE, int DebounceCount)
{
return GetDict(ioType).PushAndTest(Current_VALUE, Test_VALUE, DebounceCount);
}
static iovaluet GetDict(string ioType) {
if (!iolist.ContainsKey(ioType))
{
iolist.Add(ioType, new iovaluet());
}
return iolist[ioType];
}
class iovaluet {
IO_VALUE[] iO_VALUEs = new IO_VALUE[50];
int lastIndex=0;
void pushdata(IO_VALUE iO_VALUE) {
if (lastIndex == iO_VALUEs.Length)
lastIndex = 0;
iO_VALUEs[lastIndex] = iO_VALUE;
lastIndex++;
}
bool testdata(IO_VALUE Test_VALUE, int DebounceCount)
{
int currentIndex = lastIndex;
for (int i=0;i< DebounceCount;i++) {
currentIndex--;
if (currentIndex < 0) currentIndex = iO_VALUEs.Length-1;
if (iO_VALUEs[currentIndex] != Test_VALUE)
return false;
}
return true;
}
internal bool PushAndTest(IO_VALUE Current_VALUE, IO_VALUE Test_VALUE, int DebounceCount) {
pushdata(Current_VALUE);
return testdata(Test_VALUE, DebounceCount);
}
}
}
}