GUI demo: fixes, added VS2013 config, x64
[usb-relay-hid.git] / windows-vs / VS_GUI_demo / GUIapp.cpp
CommitLineData
00a4b8f3 1// GUIapp.cpp : main project file
2
3//#include "stdafx.h"
4
5namespace GUIapp {
6 const int MaxRelaysNum = 8;
7}
8
9#include "Form1.h"
10
11#include "../../lib/usb_relay_device.h"
12#include <cstring>
13
14using namespace GUIapp;
15
16static intptr_t g_dev = 0;
17static int g_chanNum = 0;
18static pusb_relay_device_info_t g_enumlist = nullptr;
19
20[STAThreadAttribute]
21int 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();
83b98758 28 Application::SetCompatibleTextRenderingDefault(false);
00a4b8f3 29
30 // Create the main window and run it
31 Application::Run(gcnew Form1());
32 return 0;
33}
34
35
36System::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
64System::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
83b98758 80 intptr_t dh = usb_relay_device_open_with_serial_number(id, (int)strlen(id));
00a4b8f3 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;
8a7c0a63
P
88 int numChannels = (unsigned char)di->type;
89 if ( numChannels > MaxRelaysNum || numChannels == 0) {
90 // oops?
91 numChannels = MaxRelaysNum;
92 }
00a4b8f3 93 g_chanNum = numChannels;
94 unsigned st = 0;
95 int r = usb_relay_device_get_status( g_dev, &st);
96 if ( r != 0 ) {
97 setMsg("Error reading device state", true);
98 } else {
99 for (int i = 0; i < MaxRelaysNum; i++ ) {
100 System::Windows::Forms::TextBox^ t;
101 t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i));
102 Drawing::Color c = st & (1 << i) ? Drawing::Color::LimeGreen : Drawing::Color::Red;
103 if ( ((i + 1) > numChannels) && (c == Drawing::Color::Red) )
104 c = Drawing::Color::Gray;
105 t->BackColor = c;
106 }
107 }
108
109 } else {
110 setMsg("Error opening the device", true);
111 }
112}
113
114// Close device
115System::Void Form1::buttonClose_Click(System::Object^ sender, System::EventArgs^ e)
116{
117 if (g_dev) {
118 usb_relay_device_close(g_dev);
119 IndOpen->BackColor = Drawing::Color::Red;
120
121 // All indicators -> gray, but leave relays as is.
122 System::Windows::Forms::TextBox^ t;
123 usb_relay_device_close(g_dev);
124 {
125 for (int i = 0; i < MaxRelaysNum; i++ ) {
126 t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i));
127 t->BackColor = Drawing::Color::Gray;
128 }
129 }
130
131 g_dev = 0;
132 }
133}
134
135// Open all channels
136System::Void Form1::RopenAll_Click(System::Object^ sender, System::EventArgs^ e)
137{
138 if (!g_dev) {
139 setMsg("No relay device is open!", true);
140 return;
141 }
142
143 System::Windows::Forms::TextBox^ t;
144 int r = usb_relay_device_open_all_relay_channel(g_dev);
145 if ( r == 0 ) {
146 for (int i=0; i < MaxRelaysNum; i++ ) {
147 t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i));
148 t->BackColor = (i <= g_chanNum - 1) ? Drawing::Color::LimeGreen : Drawing::Color::Gray;
149 }
150 }
151}
152
153// Close all channels
154System::Void Form1::RcloseAll_Click(System::Object^ sender, System::EventArgs^ e)
155{
156 if (!g_dev) {
157 setMsg("No relay device is open!", true);
158 return;
159 }
160
161 System::Windows::Forms::TextBox^ t;
162 int r = usb_relay_device_close_all_relay_channel(g_dev);
163 if (r == 0) {
164 for (int i=0; i < MaxRelaysNum; i++ ) {
165 t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i));
166 t->BackColor = (i <= g_chanNum - 1) ? Drawing::Color::Red : Drawing::Color::Gray;
167 }
168 }
169}
170
171// Open one channel
172System::Void Form1::Ropen1_Click(System::Object^ sender, System::EventArgs^ e)
173{
174 if (!g_dev) {
175 setMsg("No relay device is open!", true);
176 return;
177 }
178
179 int i;
180 System::Windows::Forms::Button^ b = safe_cast<System::Windows::Forms::Button^>(sender);
181 System::Int16^ n = safe_cast<System::Int16^>(b->Tag);
182 i = *n;
183 System::Windows::Forms::TextBox^ t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i));
184
185 int r = usb_relay_device_open_one_relay_channel(g_dev, i+1);
186 if (r == 0) {
187 t->BackColor = Drawing::Color::LimeGreen;
188 }
189}
190
191// Close one channel
192System::Void Form1::Rclose1_Click(System::Object^ sender, System::EventArgs^ e)
193{
194 if (!g_dev) {
195 setMsg("No relay device is open!", true);
196 return;
197 }
198
199 int i;
200 System::Windows::Forms::Button^ b = safe_cast<System::Windows::Forms::Button^>(sender);
201 System::Int16^ n = safe_cast<System::Int16^>(b->Tag);
202 i = *n;
203 System::Windows::Forms::TextBox^ t = static_cast<System::Windows::Forms::TextBox^>(aIndicators->GetValue(i));
204
205 int r = usb_relay_device_close_one_relay_channel(g_dev, i+1);
206 if (r == 0) {
207 t->BackColor = Drawing::Color::Red;
208 }
209}
210