]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Projects/TempDataLogger/TempLogHostApp/DataLoggerSettings.cs
Merge commit '71381457fa1311dfa0b58ba882a96db740640871'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Projects / TempDataLogger / TempLogHostApp / DataLoggerSettings.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Hid;
10
11 namespace Project1HostApp
12 {
13     public partial class frmDataloggerSettings : Form
14     {
15         private const int DEVICE_VID = 0x03EB;
16         private const int DEVICE_PID = 0x2063;
17
18         private struct Device_Report_t
19         {
20             public Byte Day;
21             public Byte Month;
22             public Byte Year;
23
24             public Byte Hour;
25             public Byte Minute;
26             public Byte Second;
27
28             public Byte LogInterval500MS;
29
30             public Byte[] ToReport()
31             {
32                 Byte[] Report = new Byte[7];
33
34                 Report[0] = this.Hour;
35                 Report[1] = this.Minute;
36                 Report[2] = this.Second;
37                 Report[3] = this.Day;
38                 Report[4] = this.Month;
39                 Report[5] = this.Year;
40                 Report[6] = this.LogInterval500MS;
41
42                 return Report;
43             }
44
45             public void FromReport(Byte[] Report)
46             {
47                 this.Hour = Report[0];
48                 this.Minute = Report[1];
49                 this.Second = Report[2];
50                 this.Day = Report[3];
51                 this.Month = Report[4];
52                 this.Year = Report[5];
53                 this.LogInterval500MS = Report[6];
54             }
55         };
56
57         private IDevice GetDeviceConnection()
58         {
59             IDevice[] ConnectedDevices = DeviceFactory.Enumerate(DEVICE_VID, DEVICE_PID);
60             IDevice ConnectionHandle = null;
61
62             if (ConnectedDevices.Count() > 0)
63                 ConnectionHandle = ConnectedDevices[0];
64             else
65                 return null;
66
67             // Fix report handle under Windows
68             if (ConnectionHandle is Hid.Win32.Win32DeviceSet)
69             {
70                 ((Hid.Win32.Win32DeviceSet)ConnectionHandle).AddDevice(0x00,
71                     ((Hid.Win32.Win32DeviceSet)ConnectionHandle).UnallocatedDevices[0]);
72             }
73
74             return ConnectionHandle;
75         }
76
77         public frmDataloggerSettings()
78         {
79             InitializeComponent();
80         }
81
82         private void btnSetValues_Click(object sender, EventArgs e)
83         {
84             IDevice ConnectionHandle = GetDeviceConnection();
85
86             if (ConnectionHandle == null)
87             {
88                 MessageBox.Show("Error: Cannot connect to Datalogger device.");
89                 return;
90             }
91
92             Device_Report_t DeviceReport = new Device_Report_t();
93             DeviceReport.Day = (byte)dtpDate.Value.Day;
94             DeviceReport.Month = (byte)dtpDate.Value.Month;
95             DeviceReport.Year = (byte)((dtpDate.Value.Year < 2000) ? 0 : (dtpDate.Value.Year - 2000));
96             DeviceReport.Hour = (byte)dtpTime.Value.Hour;
97             DeviceReport.Minute = (byte)dtpTime.Value.Minute;
98             DeviceReport.Second = (byte)dtpTime.Value.Second;
99             DeviceReport.LogInterval500MS = (byte)(nudLogInterval.Value * 2);
100
101             try
102             {
103                 ConnectionHandle.Write(0x00, DeviceReport.ToReport());
104                 MessageBox.Show("Device parameters updated successfully.");
105             }
106             catch (Exception ex)
107             {
108                 MessageBox.Show("Error: " + ex.Message);
109             }
110         }
111
112         private void btnGetValues_Click(object sender, EventArgs e)
113         {
114             IDevice ConnectionHandle = GetDeviceConnection();
115
116             if (ConnectionHandle == null)
117             {
118                 MessageBox.Show("Error: Cannot connect to Datalogger device.");
119                 return;
120             }
121
122             Device_Report_t DeviceReport = new Device_Report_t();
123
124             try
125             {
126                 Byte[] Report = new Byte[7];
127
128                 ConnectionHandle.Read(0x00, Report);
129                 DeviceReport.FromReport(Report);
130                 String msgText = "Device parameters retrieved successfully.";
131
132                 try
133                 {
134                     dtpDate.Value = new DateTime(
135                         (2000 + DeviceReport.Year),
136                         DeviceReport.Month,
137                         DeviceReport.Day);
138
139                     dtpTime.Value = new DateTime(
140                         DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
141                         DeviceReport.Hour,
142                         DeviceReport.Minute,
143                         DeviceReport.Second);
144                 }
145                 catch (Exception ex)
146                 {
147                     msgText = "Problem reading device:\n" +
148                         ex.Message +
149                         "\nY:" + DeviceReport.Year.ToString() +
150                         " M:" + DeviceReport.Month.ToString() +
151                         " D:" + DeviceReport.Day.ToString() +
152                         "\n\nUsing current date and time.";
153                     dtpDate.Value = DateTime.Now;
154                     dtpTime.Value = DateTime.Now;
155                 }
156
157                 try
158                 {
159                     nudLogInterval.Value = (DeviceReport.LogInterval500MS / 2);
160                 }
161                 catch (Exception ex)
162                 {
163                     nudLogInterval.Value = nudLogInterval.Minimum;
164                 }
165
166                 MessageBox.Show(msgText);
167             }
168             catch (Exception ex)
169             {
170                 MessageBox.Show("Error: " + ex.Message);
171             }
172         }
173
174         private void frmDataloggerSettings_Load(object sender, EventArgs e)
175         {
176
177         }
178     }
179 }