Skip to content

Commit

Permalink
Hello GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
gigacycle committed Feb 26, 2018
0 parents commit e62868b
Show file tree
Hide file tree
Showing 15 changed files with 957 additions and 0 deletions.
28 changes: 28 additions & 0 deletions USB_Relay_Control.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "USB_Relay_Control", "USB_Relay_Control\USB_Relay_Control.csproj", "{42149B00-ABEA-4E10-9591-D4E6774C7860}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Debug|x86.ActiveCfg = Debug|x86
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Debug|x86.Build.0 = Debug|x86
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Release|Any CPU.Build.0 = Release|Any CPU
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Release|x86.ActiveCfg = Release|x86
{42149B00-ABEA-4E10-9591-D4E6774C7860}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions USB_Relay_Control/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
190 changes: 190 additions & 0 deletions USB_Relay_Control/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions USB_Relay_Control/Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace USB_Relay_Control
{

public partial class Form1 : Form
{
int _deviceHandle = 0;
List<Button> _buttonList;

public Form1()
{
InitializeComponent();

refreshList();

_buttonList = new List<Button>{
button1, button2, button3, button4,
button5, button6, button7, button8
};

if (listBox1.Items.Count > 0)
listBox1.SelectedIndex = 0;
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (_deviceHandle > 0)
{
RelayDeviceWrapper.usb_relay_device_close(_deviceHandle);
_deviceHandle = 0;
}

for (int i = 0; i < _buttonList.Count; i++){
_buttonList[i].BackColor = BackColor;
_buttonList[i].Enabled = false;
}

var lb = (ListBox)sender;
usb_relay_device_info device = (usb_relay_device_info)lb.SelectedItem;
_deviceHandle = RelayDeviceWrapper.usb_relay_device_open(ref device);
int numberOfRelays = (int)device.type;

uint status = 0;
RelayDeviceWrapper.usb_relay_device_get_status(_deviceHandle, ref status);

for (int i = 0; i < numberOfRelays; i++)
{
_buttonList[i].Enabled = true;
if (status > numberOfRelays)
_buttonList[i].BackColor = Color.Red;
else if (i + 1 == status)
_buttonList[i].BackColor = Color.Red;
}
}

private void refreshList()
{
listBox1.Items.Clear();
if (RelayDeviceWrapper.usb_relay_init() != 0)
{
Console.WriteLine("Couldn't initialize!");
MessageBox.Show("Couldn't initialize!");
return;
}
else { Console.WriteLine("Initialized successfully!"); }

List<usb_relay_device_info> devicesInfos = new List<usb_relay_device_info>();
usb_relay_device_info deviceInfo = RelayDeviceWrapper.usb_relay_device_enumerate();
devicesInfos.Add(deviceInfo);

while (deviceInfo.next.ToInt32() > 0)
{
deviceInfo = (usb_relay_device_info)Marshal.PtrToStructure(deviceInfo.next, typeof(usb_relay_device_info));
devicesInfos.Add(deviceInfo);
}

foreach (var device in devicesInfos)
{
listBox1.Items.Add(device);
}
}

private void button_Click(object sender, EventArgs e)
{
if (_deviceHandle <= 0)
return;

Button btn = (Button)sender;
string strNum = btn.Text.Substring(btn.Text.Length - 1, 1);
int num = int.Parse(strNum);

if (btn.BackColor == BackColor || btn.BackColor == Color.Transparent)
{
int openResult = RelayDeviceWrapper.usb_relay_device_open_one_relay_channel(_deviceHandle, num);
}
else
{
int closeResult = RelayDeviceWrapper.usb_relay_device_close_one_relay_channel(_deviceHandle, num);
}
btn.BackColor = (btn.BackColor == Color.Red) ? BackColor : Color.Red;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_deviceHandle > 0)
RelayDeviceWrapper.usb_relay_device_close(_deviceHandle);
}

}
}
Loading

0 comments on commit e62868b

Please sign in to comment.