-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule1.vb
More file actions
96 lines (83 loc) · 4.26 KB
/
Module1.vb
File metadata and controls
96 lines (83 loc) · 4.26 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
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
Module Module1
Public FrmMain As frmCVJoy
Public SettingsMain As New clSettingsMain
Public toArduino1 As New SerialSend, fromArduino1 As New SerialRead
Public toArduino2 As New SerialSend2, fromArduino2 As New SerialRead2
Public Game As clGame
Public LogToFile As clLogToFile
Public graph As ucControlGraph, Ggraph As ucControlGGraph
'Public timeStart As DateTime, timeSent As DateTime, timeRead As DateTime
Private _KeysDict As Dictionary(Of UInteger, String)
Private Const KEYEVENTF_KEYDOWN As UInteger = &H0
Private Const KEYEVENTF_KEYUP As UInteger = &H2
Private Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)
Public Function ValidateNumber(pTextbox As Control, pMin As Single, pMax As Single, FieldDescr As String) As String
Dim res As Single, ok As Boolean
ok = Single.TryParse(pTextbox.Text.Replace("º", "").Replace(" ", "").Replace("G", ""), res)
If Not ok OrElse res < pMin OrElse res > pMax Then
pTextbox.BackColor = Color.Red
Return FieldDescr & " must be between " & pMin & " and " & pMax & vbCrLf
Else
pTextbox.BackColor = SystemColors.Window
Return ""
End If
End Function
Public Function CalculateOutput(pInput As Single, Range As Integer, MinInput As Integer, MinOuput As Integer, Gama As Integer, Factor As Single) As Integer
If pInput <= MinInput Then Return 0
Dim output As Integer
If MinInput > 0 Then pInput = (pInput - MinInput) / (Range - MinInput) * Range
Dim tmpFactor As Single = Factor
If MinOuput > 1 Then tmpFactor = Factor / Range * (Range - MinOuput)
If Gama <> 100 Then
output = MinOuput + (pInput / Range) ^ (Gama / 100) * Range * tmpFactor
Else
output = MinOuput + pInput * tmpFactor
End If
If output > Range Then output = Range
Return output
End Function
Public Function CalculateOutput2(pInput As Single, MidIn As Integer, MidOut As Single, MaxOut As Single) As Integer
If pInput <= MidIn Then
Return pInput / MidIn * MidOut
Else
Return MidOut + (pInput - MidIn) / (255 - MidIn) * (MaxOut - MidOut)
End If
End Function
Public Function ScaleValue(pValue As Single, pFromMin As Single, pFromMax As Single, pToMin As Single, pToMax As Single) As Single
If pValue = 0 Then Return 0
Return Math.Min(pToMax, Math.Max(pToMin, (pValue - pFromMin) * pToMax / (pFromMax - pFromMin) + pToMin))
End Function
Public Function ScaleValue(pValue As Single, pFromMin As Single, pFromMax As Single, pToMin As Single, pToMax As Single, pGama As Single) As Single
If pValue = 0 Then Return 0
Return (ScaleValue(pValue, pFromMin, pFromMax, pToMin, pToMax) / pToMax) ^ pGama * pToMax
End Function
Public Function KeysDict() As Dictionary(Of UInteger, String)
If _KeysDict IsNot Nothing Then Return _KeysDict
_KeysDict = New Dictionary(Of UInteger, String)
_KeysDict.Add(65535, "reset")
For Each k As Keys In [Enum].GetValues(GetType(Keys))
If k < 0 OrElse k > 255 Then Continue For
If Not _KeysDict.ContainsKey(k) Then _KeysDict.Add(k, If(k.ToString.ToUpper = "NONE", "", k.ToString))
Next
Return _KeysDict
End Function
Public Sub MySendKeys(pText As String, pPressed As Boolean)
Dim texts As String() = pText.Split("+"c)
If Not pPressed Then Array.Reverse(texts)
If pPressed Then FrmMain.ErrorAdd(vbCr, "")
For Each t As String In texts
If t.ToLower = "reset" Then
FrmMain.ResetArduino1()
' TODO: fazer um KeyDown das teclas todas, não vá algum KeyUp não ter tido KeyDown...
Else
Dim k As Keys
If [Enum].TryParse(t, k) Then
FrmMain.ErrorAdd($"{If(pPressed, "Key Down", "key up")} «{t}»", "")
keybd_event(CByte(k), 0, If(pPressed, KEYEVENTF_KEYDOWN, KEYEVENTF_KEYUP), 0)
Else
FrmMain.ErrorAdd($"UNEXPECTED KEY «{t}»", "")
End If
End If
Next
End Sub
End Module