Rev 159 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
using MCP2221;using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Timers;namespace nitdcscore {public struct devdata {public uint prev_input;public uint cur_input;public int id;public string name;public string function;}public class mcp2221 {DcsBios bios = new DcsBios();MchpUsbI2c usbi2c = new MchpUsbI2c();public Boolean hasAHFS { get; set; }public Boolean hasAAP { get; set; }int idx { get; set; }int devcount { get; set; }public uint speed { get; set; }Timer refresh = new Timer();private readonly Object lockobject = new object();public devdata[] devices = new devdata[5];public mcp2221() {speed = 400000;idx = 0;bios.InitUDP();usbi2c.Settings.GetConnectionStatus();devcount = usbi2c.Management.GetDevCount();Console.WriteLine(devcount.ToString() + " devices found");for (int i = 0; i < devcount; i++) {int rslt = usbi2c.Management.SelectDev(i);usbi2c.Settings.GetConnectionStatus();string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();if (usbDescriptor == "AHCP/FSCP Panel") {this.hasAHFS = true;devices[i].cur_input = 0;devices[i].prev_input = 0;devices[i].id = i;devices[i].name = "AHCP/FSCP Panel";devices[i].function = "refresh_ahfs";init_ahfs(ref devices[i]);} else if (usbDescriptor == "AAP Panel") {this.hasAAP = true;devices[i].cur_input = 0;devices[i].prev_input = 0;devices[i].id = i;devices[i].name = "AAP Panel";devices[i].function = "refresh_aap";init_aap(ref devices[i]);}}refresh.Interval = 500;refresh.AutoReset = true;refresh.Elapsed += refresh_Elapsed;refresh.Enabled = false;}private Boolean enabled;public Boolean Enabled {get {return enabled;}set {this.enabled = true;refresh.Enabled = true;}}void refresh_Elapsed(object sender, ElapsedEventArgs e) {if (!Enabled)return;devdata dev = devices[idx];usbi2c.Management.SelectDev(dev.id);Type type = this.GetType();MethodInfo callfunc = type.GetMethod(dev.function);ParameterInfo[] parameters = callfunc.GetParameters();object[] parms = { dev };callfunc.Invoke(this, parms);idx++;if (idx >= devcount)idx = 0;}public void init_aap(ref devdata dev) {// Enable the mcp23017WriteGpio(3, 1);// Set io dir, pullups and rev polaritybyte[] data;data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarityWriteI2cData(0x40, data, 5);data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = onWriteI2cData(0x40, data, 3);refresh_aap(ref dev);dev.prev_input = dev.cur_input;}public void refresh_aap(ref devdata dev) {Console.WriteLine("In aap " + usbi2c.Management.GetSelectedDevNum().ToString() + " : " + dev.name + " " + dev.cur_input.ToString("X"));}public void init_ahfs(ref devdata dev) {// Get the initial ADC value//this._prev_adc = this.adc = _mcp.ReadADC(1);// Enable the mcp23017WriteGpio(3, 1);// Set io dir, pullups and rev polaritybyte[] data;// Soldered a couple of inputs wrong, the f0 is to reverse them (fuel boost switches)data = new byte[] { 0x00, 0xff, 0xff, 0xf0, 0xff }; // All inputs & polarityWriteI2cData(0x40, data, 5);data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = onWriteI2cData(0x40, data, 3);data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarityWriteI2cData(0x42, data, 5);data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = onWriteI2cData(0x42, data, 3);refresh_ahfs(ref dev);dev.prev_input = dev.cur_input;}public void refresh_ahfs(ref devdata dev) {byte[] data;lock (lockobject) {data = new byte[] { 0x12 }; // Select GPIOA registerWriteI2cData(0x40, data, 1);data = new byte[] { 0x00, 0x00 }; // Read two bytesint rslt = ReadI2CData(0x41, ref data, 2);dev.cur_input = (uint)data[0] << 24;dev.cur_input |= (uint)data[1] << 16;data = new byte[] { 0x12 }; // Select GPIOA registerWriteI2cData(0x42, data, 1);data = new byte[] { 0x00, 0x00 }; // Read two bytesrslt = ReadI2CData(0x43, ref data, 2);dev.cur_input |= (uint)data[0] << 8;dev.cur_input |= (uint)data[1];}Console.WriteLine("In ahfs " + usbi2c.Management.GetSelectedDevNum().ToString() + " : " + dev.name + " " + dev.cur_input.ToString("X"));}public byte ReadGpio(byte pinNum) {return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));}public ushort ReadADC(byte pinNum) {ushort[] adcData = { 0, 0, 0, 0, 0, 0 };int rslt = usbi2c.Functions.GetAdcData(adcData);return adcData[pinNum];}public void WriteGpio(byte pinNum, byte value) {this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);}public int WriteI2cData(byte address, byte[] data, uint count) {int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);return rslt;}public int ReadI2CData(byte address, ref byte[] data, uint count) {int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);return rslt;}public uint Switch2Pos(int pin, String cmd, devdata data, Boolean invert = false) {uint chg = (uint)(data.prev_input >> pin) & 0x01;uint norm = (uint)(data.cur_input >> pin) & 0x01;uint value = 0;if ((uint)(norm) == 1) {value = (uint)(invert ? 0 : 1);}if (norm != chg) {bios.SendData(cmd + " " + value.ToString() + "\n");Console.WriteLine(cmd + ":" + value.ToString());}return value;}public uint Switch3Pos(int pin0, int pin1, String cmd, devdata data, Boolean invert = false) {uint value = 1;uint chg0 = (uint)(data.prev_input >> pin0) & 0x01;uint chg1 = (uint)(data.prev_input >> pin1) & 0x01;uint nrm0 = (uint)(data.cur_input >> pin0) & 0x01;uint nrm1 = (uint)(data.cur_input >> pin1) & 0x01;if ((uint)nrm0 == 1)value = (uint)(invert ? 2 : 0);else if ((uint)nrm1 == 1)value = (uint)(invert ? 0 : 2);if ((nrm0 != chg0) || (nrm1 != chg1)) {bios.SendData(cmd + " " + value.ToString() + "\n");Console.WriteLine(cmd + ":" + value.ToString());}return value;}public ushort map_ushort(ushort x, ushort in_min, ushort in_max, ushort out_min, ushort out_max) {return (ushort)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);}}}