Commit | Line | Data |
---|---|---|
00a4b8f3 | 1 | // GUIapp.cpp : main project file |
2 | ||
00a4b8f3 | 3 | #include "Form1.h" |
4 | ||
5 | #include "../../lib/usb_relay_device.h" | |
6 | #include <cstring> | |
7 | ||
8 | using namespace GUIapp; | |
9 | ||
10 | static intptr_t g_dev = 0; | |
11 | static int g_chanNum = 0; | |
12 | static pusb_relay_device_info_t g_enumlist = nullptr; | |
13 | ||
0725bb21 | 14 | #define GREEN Drawing::Color::LimeGreen |
15 | #define RED Drawing::Color::Red | |
16 | #define GREY Drawing::Color::Gray | |
17 | ||
00a4b8f3 | 18 | [STAThreadAttribute] |
19 | int main(array<System::String ^> ^args) | |
20 | { | |
0725bb21 | 21 | g_dev = 0; |
22 | g_enumlist = nullptr; | |
00a4b8f3 | 23 | |
0725bb21 | 24 | // Enabling Windows XP visual effects before any controls are created |
25 | Application::EnableVisualStyles(); | |
26 | Application::SetCompatibleTextRenderingDefault(false); | |
00a4b8f3 | 27 | |
0725bb21 | 28 | // Create the main window and run it |
29 | Application::Run(gcnew Form1()); | |
30 | return 0; | |
00a4b8f3 | 31 | } |
32 | ||
0725bb21 | 33 | void Form1::buttonFind_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 34 | { |
0725bb21 | 35 | if (g_enumlist) { |
36 | usb_relay_device_free_enumerate(g_enumlist); | |
37 | g_enumlist = nullptr; | |
38 | } | |
39 | ||
40 | comboDevices->Items->Clear(); | |
41 | ||
42 | g_enumlist = usb_relay_device_enumerate(); | |
43 | pusb_relay_device_info_t q = g_enumlist; | |
44 | while(q) { | |
45 | System::String^ s = ""; | |
46 | for (char const *p = q->serial_number; *p; p++) | |
47 | s += (wchar_t)(*p); | |
48 | comboDevices->Items->Add(s); | |
49 | q = q->next; | |
50 | } | |
51 | ||
52 | if ( comboDevices->Items->Count != 0 ) { | |
53 | comboDevices->SelectedIndex = 0; | |
54 | setMsg( "Found:" + (comboDevices->Items->Count).ToString() + " device(s)", false ); | |
55 | } else { | |
56 | setMsg( "No devices found", true ); | |
57 | } | |
00a4b8f3 | 58 | } |
59 | ||
60 | // Open device | |
0725bb21 | 61 | void Form1::buttonOpen_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 62 | { |
0725bb21 | 63 | System::String^ s = comboDevices->Text; |
64 | if ( !s ) return; | |
65 | char id[16]; | |
66 | int i; | |
67 | for (i = 0; i < s->Length && i < (sizeof(id) - 1); i++ ) { | |
68 | id[i] = (char)s[i]; | |
69 | } | |
70 | id[i] = 0; | |
71 | // Only one device can be open. TODO: allow multiple devices open, save&restore state? | |
72 | if (g_dev) { | |
73 | setMsg( "Closing the opened device", false ); | |
74 | buttonClose_Click(sender, e); | |
75 | } | |
76 | ||
77 | intptr_t dh = usb_relay_device_open_with_serial_number(id, (int)strlen(id)); | |
78 | if ( dh ) { | |
79 | g_dev = dh; | |
80 | IndOpen->BackColor = GREEN; | |
81 | ||
82 | // Get and show state: | |
00a4b8f3 | 83 | //BUGBUG g_dev is not usb_relay_device_info* in the orig library! use my new f() ?? use only info in the list? |
0725bb21 | 84 | struct usb_relay_device_info* di = (struct usb_relay_device_info*)g_dev; |
85 | int numChannels = (unsigned char)di->type; | |
8a7c0a63 P |
86 | if ( numChannels > MaxRelaysNum || numChannels == 0) { |
87 | // oops? | |
88 | numChannels = MaxRelaysNum; | |
89 | } | |
0725bb21 | 90 | g_chanNum = numChannels; |
91 | unsigned st = 0; | |
92 | int r = usb_relay_device_get_status( g_dev, &st); | |
93 | if ( r != 0 ) { | |
94 | setMsg("Error reading device state", true); | |
95 | } else { | |
96 | for (int i = 0; i < MaxRelaysNum; i++ ) { | |
97 | System::Windows::Forms::TextBox^ t; | |
98 | t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i)); | |
99 | Drawing::Color c = st & (1 << i) ? GREEN : RED; | |
100 | if ( ((i + 1) > numChannels) && (c == RED) ) | |
101 | c = GREY; | |
102 | t->BackColor = c; | |
103 | } | |
104 | } | |
105 | ||
106 | } else { | |
107 | setMsg("Error opening the device", true); | |
108 | IndOpen->BackColor = RED; | |
109 | } | |
00a4b8f3 | 110 | } |
111 | ||
112 | // Close device | |
0725bb21 | 113 | void Form1::buttonClose_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 114 | { |
0725bb21 | 115 | if (g_dev) { |
116 | usb_relay_device_close(g_dev); | |
117 | IndOpen->BackColor = RED; | |
118 | ||
119 | // All indicators -> gray, but leave relays as is. | |
120 | System::Windows::Forms::TextBox^ t; | |
121 | usb_relay_device_close(g_dev); | |
122 | { | |
123 | for (int i = 0; i < MaxRelaysNum; i++ ) { | |
124 | t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i)); | |
125 | t->BackColor = GREY; | |
126 | } | |
127 | } | |
128 | ||
129 | g_dev = 0; | |
130 | } | |
00a4b8f3 | 131 | } |
132 | ||
133 | // Open all channels | |
0725bb21 | 134 | void Form1::RopenAll_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 135 | { |
0725bb21 | 136 | if (!g_dev) { |
137 | setMsg("No relay device is open!", true); | |
138 | return; | |
139 | } | |
140 | ||
141 | System::Windows::Forms::TextBox^ t; | |
142 | int r = usb_relay_device_open_all_relay_channel(g_dev); | |
143 | if ( r == 0 ) { | |
144 | for (int i=0; i < MaxRelaysNum; i++ ) { | |
145 | t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i)); | |
146 | t->BackColor = (i <= g_chanNum - 1) ? GREEN : GREY; | |
147 | } | |
148 | } | |
00a4b8f3 | 149 | } |
150 | ||
151 | // Close all channels | |
0725bb21 | 152 | void Form1::RcloseAll_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 153 | { |
0725bb21 | 154 | if (!g_dev) { |
155 | setMsg("No relay device is open!", true); | |
156 | return; | |
157 | } | |
158 | ||
159 | System::Windows::Forms::TextBox^ t; | |
160 | int r = usb_relay_device_close_all_relay_channel(g_dev); | |
161 | if (r == 0) { | |
162 | for (int i=0; i < MaxRelaysNum; i++ ) { | |
163 | t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i)); | |
164 | t->BackColor = (i <= g_chanNum - 1) ? RED : GREY; | |
165 | } | |
166 | } | |
00a4b8f3 | 167 | } |
168 | ||
169 | // Open one channel | |
0725bb21 | 170 | void Form1::Ropen1_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 171 | { |
0725bb21 | 172 | if (!g_dev) { |
173 | setMsg("No relay device is open!", true); | |
174 | return; | |
175 | } | |
176 | ||
177 | System::Windows::Forms::Control^ b = safe_cast<System::Windows::Forms::Control^>(sender); | |
178 | int i = *(safe_cast<System::Int16^>(b->Tag)); | |
179 | System::Windows::Forms::TextBox^ t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i)); | |
180 | ||
181 | int r = usb_relay_device_open_one_relay_channel(g_dev, i+1); | |
182 | t->BackColor = (r == 0) ? GREEN : GREY; | |
00a4b8f3 | 183 | } |
184 | ||
185 | // Close one channel | |
0725bb21 | 186 | void Form1::Rclose1_Click(System::Object^ sender, System::EventArgs^ e) |
00a4b8f3 | 187 | { |
0725bb21 | 188 | if (!g_dev) { |
189 | setMsg("No relay device is open!", true); | |
190 | return; | |
191 | } | |
192 | ||
193 | System::Windows::Forms::Control^ b = safe_cast<System::Windows::Forms::Control^>(sender); | |
194 | int i = *(safe_cast<System::Int16^>(b->Tag)); | |
195 | System::Windows::Forms::TextBox^ t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i)); | |
196 | ||
197 | int r = usb_relay_device_close_one_relay_channel(g_dev, i+1); | |
198 | t->BackColor = (r == 0) ? RED : GREY; | |
00a4b8f3 | 199 | } |
200 |