/// <summary> /// Set the bus pins and stores the state /// </summary> /// <param name="bus"></param> /// <param name="state"></param> /// <returns></returns> FTDI.FT_STATUS set_state(DIO_BUS bus, byte state) { byte direction = 0xFF; if (bus == DIO_BUS.AD_BUS) { _cha_state = state; direction = _cha_direction; } else if (bus == DIO_BUS.AC_BUS) { _chb_state = state; direction = _chb_direction; } byte addr = get_bus_write_address(bus); byte[] buffer = new byte[] { addr, state, direction }; // the last byte sets direction (0 = input, 1 = output) uint n = 0; FTDI.FT_STATUS status = _ftdi.Write(buffer, buffer.Length, ref n); return status; }
/// <summary> /// Gets the address for each "write" data bus /// </summary> /// <param name="bus"></param> /// <returns></returns> byte get_bus_write_address(DIO_BUS bus) { byte addr = 0x80; // DIO_BUS.AD_BUS if (bus == DIO_BUS.AC_BUS) addr = 0x82; return addr; }
/// <summary> /// Gets the stored state of a the pins for a particular bus /// </summary> /// <param name="bus"></param> /// <returns></returns> byte get_stored_state(DIO_BUS bus) { byte state = _cha_state; if (bus == DIO_BUS.AC_BUS) state = _chb_state; return state; }
/// <summary> /// Set the state of the pin /// </summary> /// <param name="bus"></param> /// <param name="pin"></param> /// <param name="value"></param> /// <returns></returns> public FTDI.FT_STATUS SetPin(DIO_BUS bus, PIN pin, bool value) { byte pin_num = Convert.ToByte(pin); FTDI.FT_STATUS status = SetPin(bus, pin_num, value); return status; }
/// <summary> /// Gets the address for each "read" data bus /// </summary> /// <param name="bus"></param> /// <returns></returns> byte get_bus_read_address(DIO_BUS bus) { byte addr = 0x81; if (bus == DIO_BUS.AC_BUS) addr = 0x83; return addr; }
/// <summary> /// Reads the state of the pin /// </summary> /// <param name="bus"></param> /// <param name="pin"></param> /// <returns></returns> public bool ReadPin(DIO_BUS bus, uint pin) { byte data = ReadBus(bus); byte pin_num = Convert.ToByte(pin); int b = (byte)(1 << pin_num); bool value = Convert.ToBoolean((data & b)); return value; }
/// <summary> /// Set the state of the pin /// </summary> /// <param name="bus"></param> /// <param name="pin"></param> /// <param name="value"></param> /// <returns></returns> public FTDI.FT_STATUS SetPin(DIO_BUS bus, uint pin, bool value) { Debug.Assert(pin < 8 && pin >= 0, "Pin number must be between 0 and 7"); byte pin_num = Convert.ToByte(pin); byte state_current = get_stored_state(bus); //byte state_current2 = GetBusData(bus); //if ((state_current & 0x0F) != (state_current2 & 0x0F)) //{ // // If we never see this it means we can get rid of get_bus_state and // // keeping track of the state // throw new Exception("Unexpected FT232DIO bus state. Was it close and re-opened?"); //} byte state_new = state_current; if (value) { state_new |= (byte)(1 << pin_num); } else { state_new &= (byte)(~(1 << pin_num) & 0xFF); } // Set the pin to output if (bus == DIO_BUS.AD_BUS) { _cha_direction |= (byte)(1 << pin_num); } else if (bus == DIO_BUS.AC_BUS) { _chb_direction |= (byte)(1 << pin_num); } FTDI.FT_STATUS status = set_state(bus, state_new); return status; }
/// <summary> /// Reads one byte from the specified bus /// </summary> /// <param name="bus"></param> /// <returns></returns> public byte ReadBus(DIO_BUS bus) { byte addr = get_bus_read_address(bus); byte[] data = new byte[] { addr }; uint n = 0; // Read command FTDI.FT_STATUS status = _ftdi.Write(data, data.Length, ref n); if (status != FTDI.FT_STATUS.FT_OK) throw new Exception(string.Format("Problem writing read command to bus {0}", bus.ToString())); // Get data status = _ftdi.Read(data, (uint)data.Length, ref n); if (status != FTDI.FT_STATUS.FT_OK) throw new Exception(string.Format("Problem writing read command to bus {0}", bus.ToString())); return data[0]; }
/// <summary> /// Reads the state of the pin /// </summary> /// <param name="bus"></param> /// <param name="pin"></param> /// <returns></returns> public bool ReadPin(DIO_BUS bus, PIN pin) { byte pin_num = Convert.ToByte(pin); bool value = ReadPin(bus, pin_num); return value; }
/// <summary> /// Returns the stored (may not be actual) state of the pin /// </summary> /// <param name="bus"></param> /// <param name="pin"></param> /// <returns></returns> public bool GetPin(DIO_BUS bus, uint pin) { byte data = get_stored_state(bus); byte pin_num = Convert.ToByte(pin); int b = (byte)(1 << pin_num); bool value = Convert.ToBoolean((data & b)); return value; }
void _set_bus_state(DIO_BUS bus, byte state) { if (bus == DIO_BUS.BUSA) _cha_state = state; else if (bus == DIO_BUS.BUSB) _chb_state = state; }
/// <summary> /// Gets the state of a the pins for a particular bus /// </summary> /// <param name="bus"></param> /// <returns></returns> byte _get_bus_state(DIO_BUS bus) { byte state = _cha_state; if (bus == DIO_BUS.BUSB) state = _chb_state; return state; }
/// <summary> /// Gets the address for each data bus /// </summary> /// <param name="bus"></param> /// <returns></returns> byte _get_bus_address(DIO_BUS bus) { byte addr = 0x80; if (bus == DIO_BUS.BUSB) addr = 0x82; return addr; }
/// <summary> /// Set the state of the pin /// </summary> /// <param name="bus"></param> /// <param name="pin"></param> /// <param name="value"></param> /// <returns></returns> public FTDI.FT_STATUS SetPin(DIO_BUS bus, int pin, bool value) { Debug.Assert(pin < 8 && pin >= 0, "Pin number must be between 0 and 7"); byte addr = _get_bus_address(bus); byte state_current = _get_bus_state(bus); byte pin_num = Convert.ToByte(pin); byte state_new = state_current; if (value) { state_new |= (byte)(1 << pin_num); } else { state_new &= (byte)(0 << pin_num); } FTDI.FT_STATUS status = FTDI.FT_STATUS.FT_OK; if (state_current != state_new) { _set_bus_state(bus, state_new); byte[] buffer = new byte[] { addr, state_new, 0xFF }; uint n = 0; status = _ftdi.Write(buffer, buffer.Length, ref n); } return status; }