-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlicButton.cs
100 lines (91 loc) · 3.63 KB
/
FlicButton.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlicSharp
{
public class FlicButton
{
public delegate void FlicButtonAction(FlicButton flic_button);
public string NAME { get; set; } //Name of the Flic button
public uint HANDLE { get; set; } //Handle number for managing multiple buttons
public BluetoothAddress BT_ADDRESS { get; set; }
public List<ClickType> PRESS_HISTORY { get; set; }
public event FlicButtonAction SinglePressed;
public event FlicButtonAction DoublePressed;
public event FlicButtonAction Hold;
public FlicButton(string flic_name, uint flic_handle, string bluetooth_address)
{
PRESS_HISTORY = new List<ClickType>();
NAME = flic_name;
HANDLE = flic_handle;
BT_ADDRESS = BluetoothAddress.Parse(bluetooth_address);
}
public void AddPressToHistory(ClickType click_type)
{
if ((PRESS_HISTORY.Count == 0 && click_type == ClickType.ButtonSingleClick) ||
(PRESS_HISTORY.Count == 0 && click_type == ClickType.ButtonUp))
{
return;
}
PRESS_HISTORY.Add(click_type);
//SingleClick
try
{
if (PRESS_HISTORY[PRESS_HISTORY.Count - 5] == ClickType.ButtonDown &&
PRESS_HISTORY[PRESS_HISTORY.Count - 4] == ClickType.ButtonUp &&
PRESS_HISTORY[PRESS_HISTORY.Count - 3] == ClickType.ButtonClick &&
PRESS_HISTORY[PRESS_HISTORY.Count - 2] == ClickType.ButtonSingleClick &&
PRESS_HISTORY[PRESS_HISTORY.Count - 1] == ClickType.ButtonSingleClick)
{
SinglePressed?.Invoke(this);
PRESS_HISTORY.Clear();
}
}
catch
{
//Do nothing
}
//Hold
try
{
if (PRESS_HISTORY[PRESS_HISTORY.Count - 3] == ClickType.ButtonDown &&
PRESS_HISTORY[PRESS_HISTORY.Count - 2] == ClickType.ButtonHold &&
PRESS_HISTORY[PRESS_HISTORY.Count - 1] == ClickType.ButtonHold)
{
Hold?.Invoke(this);
PRESS_HISTORY.Clear();
}
}
catch
{
//Do nothing
}
//DoublePress
try
{
if (PRESS_HISTORY[PRESS_HISTORY.Count - 8] == ClickType.ButtonDown &&
PRESS_HISTORY[PRESS_HISTORY.Count - 7] == ClickType.ButtonUp &&
PRESS_HISTORY[PRESS_HISTORY.Count - 6] == ClickType.ButtonClick &&
PRESS_HISTORY[PRESS_HISTORY.Count - 5] == ClickType.ButtonDown &&
PRESS_HISTORY[PRESS_HISTORY.Count - 4] == ClickType.ButtonUp &&
PRESS_HISTORY[PRESS_HISTORY.Count - 3] == ClickType.ButtonClick &&
PRESS_HISTORY[PRESS_HISTORY.Count - 2] == ClickType.ButtonDoubleClick &&
PRESS_HISTORY[PRESS_HISTORY.Count - 1] == ClickType.ButtonDoubleClick)
{
DoublePressed?.Invoke(this);
PRESS_HISTORY.Clear();
}
}
catch
{
//Do nothing
}
}
public override string ToString()
{
return "Name: " + NAME + " - Handle: " + HANDLE + " - BT-Address: " + BT_ADDRESS.ToString();
}
}
}