This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
99 lines (81 loc) · 3.3 KB
/
Program.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
using System;
using System.Windows.Forms;
using igfxDHLib;
namespace PWMHelper
{
static class MessageString
{
public const string InvalidValue = "Invalid value. PMM frequency must be in range of {0}-{1}Hz";
public const string ErrorReadingData = "Failed to get current PWM: {0:X}";
public const string ErrorWritingData = "Failed to set PWM to {0}Hz (error {1:X})";
public const string CurrentFrequency = "Current frequency: {0}";
}
class Program
{
private const int MinFrequency = 200;
private const int MaxFrequency = 25000;
private static DataHandler _dh = new DataHandler();
[STAThread]
public static void Main(string[] args)
{
int targetPwmFrequency = ParseArguments(args);
if (targetPwmFrequency == -1)
return;
byte[] baseData = ReadDataFromDriver();
if (baseData == null)
return;
int currentPwmFrequency = BitConverter.ToInt32(baseData, 4);
if (targetPwmFrequency == 0)
{
ShowCurrentFrequency(currentPwmFrequency);
return;
}
if (currentPwmFrequency == targetPwmFrequency)
return;
SetNewFrequency(baseData, targetPwmFrequency);
}
private static int ParseArguments(string[] args)
{
if (args.Length == 0)
return 0;
int argValue;
if (!int.TryParse(args[0], out argValue))
return 0;
if (argValue < MinFrequency || argValue > MaxFrequency)
{
MessageBox.Show(string.Format(MessageString.InvalidValue, MinFrequency, MaxFrequency));
return -1;
}
return argValue;
}
private static byte[] ReadDataFromDriver()
{
uint error = 0;
byte[] baseData = new byte[8];
_dh.GetDataFromDriver(ESCAPEDATATYPE_ENUM.GET_SET_PWM_FREQUENCY, 4, ref error, ref baseData[0]);
if (error != 0)
{
MessageBox.Show(string.Format(MessageString.ErrorReadingData, error));
return null;
}
return baseData;
}
private static void SetNewFrequency(byte[] baseData, int targetPwmFrequency)
{
UpdateBaseDataWithNewFrequency(baseData, targetPwmFrequency);
uint error = 0;
_dh.SendDataToDriver(ESCAPEDATATYPE_ENUM.GET_SET_PWM_FREQUENCY, 4, ref error, ref baseData[0]);
if (error != 0)
MessageBox.Show(string.Format(MessageString.ErrorWritingData, targetPwmFrequency, error));
}
private static void UpdateBaseDataWithNewFrequency(byte[] baseData, int targetPwmFrequency)
{
byte[] b = BitConverter.GetBytes(targetPwmFrequency);
Array.Copy(b, 0, baseData, 4, 4);
}
private static void ShowCurrentFrequency(int currentFrequency)
{
MessageBox.Show(string.Format(MessageString.CurrentFrequency, currentFrequency));
}
}
}