using System.Threading; using Microsoft.SPOT.Hardware; namespace HWModules { //TODO P2 need a method to setup ADC registers public class ADC2485 : I2CPortBase { private static I2CDevice.I2CTransaction[] xActions2485 = new I2CDevice.I2CTransaction[2]; private static byte[] sampleBytes = new byte[4]; private static byte[] convertBytes = new byte[4]; private static byte[] mode0 = new byte[1] { 0x00 };//External input, 50 and 60 Hz filter, 1X speed private static byte[] mode4 = new byte[1] { 0x04 };//External input, 60 Hz filter, 1X speed private static int xActionReturn = 0; private static int value = int.MaxValue; public ADC2485(I2CDevice.Configuration config, int slotNumber) : base (config, slotNumber) {} public int readValue() { // create write buffer, send 2485 mode command xActions2485[0] = CreateWriteTransaction(mode4); // create read buffer to read the returned data xActions2485[1] = CreateReadTransaction(sampleBytes); Thread.Sleep(200); xActionReturn = Execute(xActions2485, 1000); if (xActionReturn == 0) { EngrLogger.writeToColumns("Error executing 2485 Transaction = " + xActionReturn.ToString()); return (int.MaxValue); } else { //Debug.Print(sampleBytes[0].ToString() + " " + sampleBytes[1].ToString() + " " + // sampleBytes[2].ToString() + " " + sampleBytes[3].ToString()); if ((sampleBytes[0] >= (byte)0xC0) || (sampleBytes[0] < (byte)0x40)) { EngrLogger.writeToColumns("2485 Sample out of range"); return (int.MaxValue); } else { convertBytes[0] = sampleBytes[3]; convertBytes[1] = sampleBytes[2]; convertBytes[2] = sampleBytes[1]; convertBytes[3] = sampleBytes[0]; value = GHI.Utilities.Arrays.ExtractInt32(convertBytes, 0); //Debug.Print("Reversed bytes value = " + value.ToString()); return (value); } } } } }