/// <summary> /// Read from PCA9685 register /// </summary> /// <param name="reg">PCA9685 register to read from</param> /// <param name="value">value read from register</param> /// <returns>StatusResults(FT260_STATUS, FT260_CONTROLLER_STATUS)</returns> public UsbI2C.StatusResults Read(Reg reg, out byte value) { UsbI2C.StatusResults statusResults; byte[] regValue = { (byte)reg }; byte[] readBytes = { 0 }; value = 0; lock (_usbI2C) { if (!(statusResults = _usbI2C.Write( _i2cAddress, UsbI2C.FT260_I2C_FLAG.FT260_I2C_START, regValue, (uint)regValue.Length)).HasErrors()) { if (!(statusResults = _usbI2C.Read( _i2cAddress, UsbI2C.FT260_I2C_FLAG.FT260_I2C_REPEATED_START_AND_STOP, readBytes, (uint)readBytes.Length)).HasErrors()) { value = readBytes[0]; } } } return(statusResults);; }
/// <summary> /// Read from PCF8574 port pins /// </summary> /// <param name="pins">value read from port pins</param> /// <returns>StatusResults(FT260_STATUS, FT260_CONTROLLER_STATUS)</returns> public UsbI2C.StatusResults Read(out byte pins) { UsbI2C.StatusResults statusResults; byte[] result = new byte[1]; lock (_usbI2C) { statusResults = _usbI2C.Read(_i2cAddress, UsbI2C.FT260_I2C_FLAG.FT260_I2C_START_AND_STOP, result, (uint)result.Length); } pins = result[0]; return(statusResults); }
/// <summary> /// Get a raw ADC reading /// </summary> /// <param name="channel">Channel0 - Channel3</param> /// <param name="size">Size12Bit, Size14Bit, Size16Bit, Size18Bit</param> /// <param name="pga">Gain1, Gain2, Gain4, Gain8</param> /// <param name="reading">value of ADC channel read at size & gain</param> /// <returns>StatusResults(FT260_STATUS, FT260_CONTROLLER_STATUS)</returns> public UsbI2C.StatusResults ReadAdc(AdcChannel channel, AdcSize size, AdcPga pga, out uint reading) { UsbI2C.StatusResults statusResults; reading = 0; byte[] control = { 0 }; // start one-shot conversion on channel @ size and gain... control[0] = (byte)(0x80 | (((int)channel & 3) << 5 | (((int)size & 3) << 2) | ((int)pga & 3))); lock (_usbI2C) { statusResults = _usbI2C.Write( _i2cAddress, UsbI2C.FT260_I2C_FLAG.FT260_I2C_START_AND_STOP, control, (uint)control.Length); } if (statusResults.HasErrors()) { return(statusResults); } // length of result depends upon size of conversion... byte[] result; if (size == AdcSize.Size18Bit) { result = new byte[4]; } else { result = new byte[3]; } // continuously read until conversion complete (/RDY bit = 1)... do { lock (_usbI2C) { statusResults = _usbI2C.Read( _i2cAddress, UsbI2C.FT260_I2C_FLAG.FT260_I2C_START_AND_STOP, result, (uint)result.Length); } if (statusResults.HasErrors()) { return(statusResults); } Thread.Sleep(1); } while ((result[result.Length - 1] & 0x80) == 0x00); // assemble result depending upon size of conversion... if (size == AdcSize.Size18Bit) { reading = ((uint)(result[0] & 3) << 16) | ((uint)result[1] << 8) | (uint)result[2]; } else { reading = ((uint)result[0] << 8) | (uint)result[1]; } return(statusResults); }