IODebounce.cs 1.8 KB
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);
            }
        }
    }
}