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