158 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.Threading.Tasks;
|
|
|
6 |
using System.Timers;
|
|
|
7 |
using MCP2221;
|
|
|
8 |
|
|
|
9 |
namespace nitdcscore {
|
|
|
10 |
public class Bridge_mcp2221 {
|
|
|
11 |
public MchpUsbI2c usbi2c = new MchpUsbI2c();
|
|
|
12 |
public DcsBios bios = new DcsBios();
|
|
|
13 |
public uint speed { get; set; }
|
|
|
14 |
public int deviceid { get; set; }
|
|
|
15 |
|
|
|
16 |
Timer timer = new Timer();
|
|
|
17 |
|
|
|
18 |
NITAAPDevice aap;
|
|
|
19 |
NITAHFSDevice ahfs;
|
|
|
20 |
|
|
|
21 |
public Bridge_mcp2221() {
|
|
|
22 |
this.speed = 400000;
|
|
|
23 |
|
|
|
24 |
timer.Enabled = false;
|
|
|
25 |
timer.AutoReset = true;
|
|
|
26 |
timer.Interval = 50;
|
|
|
27 |
timer.Elapsed += timer_Elapsed;
|
|
|
28 |
|
|
|
29 |
bios.InitUDP();
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
public void init() {
|
|
|
35 |
aap = new NITAAPDevice(ref this.bios, this);
|
|
|
36 |
ahfs = new NITAHFSDevice(ref this.bios, this);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
void timer_Elapsed(object sender, ElapsedEventArgs e) {
|
|
|
40 |
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public int SelectDevice(String devName) {
|
|
|
44 |
if (usbi2c.Settings.GetConnectionStatus() == false)
|
|
|
45 |
return -2;
|
|
|
46 |
int rslt = 0;
|
|
|
47 |
|
|
|
48 |
int count = usbi2c.Management.GetDevCount();
|
|
|
49 |
for (int i = 0; i < count; i++) {
|
|
|
50 |
rslt = usbi2c.Management.SelectDev(i);
|
|
|
51 |
usbi2c.Settings.GetConnectionStatus();
|
|
|
52 |
string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
|
|
|
53 |
if (usbDescriptor == devName) {
|
|
|
54 |
rslt = usbi2c.Management.SelectDev(i);
|
|
|
55 |
Console.WriteLine(devName + ":" + usbDescriptor + ":" + i.ToString());
|
|
|
56 |
this.deviceid = i;
|
|
|
57 |
return rslt;
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
return -5;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
public byte ReadGpio(byte pinNum) {
|
|
|
65 |
return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public ushort ReadADC(byte pinNum) {
|
|
|
69 |
ushort[] adcData = { 0,0,0,0,0,0 };
|
|
|
70 |
|
|
|
71 |
int rslt = usbi2c.Functions.GetAdcData(adcData);
|
|
|
72 |
|
|
|
73 |
//foreach (ushort adc in adcData) {
|
|
|
74 |
// Console.Write((adc).ToString() + " ");
|
|
|
75 |
//}
|
|
|
76 |
//Console.WriteLine();
|
|
|
77 |
|
|
|
78 |
return adcData[pinNum];
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public void WriteGpio(byte pinNum, byte value) {
|
|
|
82 |
this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public int WriteI2cData(byte address, byte[] data, uint count) {
|
|
|
86 |
int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
|
|
|
87 |
return rslt;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
public int ReadI2CData(byte address, ref byte[] data, uint count) {
|
|
|
91 |
int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
|
|
|
92 |
return rslt;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|