package net.sf.jaer.hardwareinterface.usb.cypressfx3libusb;

import javax.swing.JOptionPane;

import org.usb4java.Device;

import eu.seebetter.ini.chips.davis.DavisConfig;
import net.sf.jaer.biasgen.Biasgen;
import net.sf.jaer.biasgen.BiasgenHardwareInterface;
import net.sf.jaer.hardwareinterface.HardwareInterfaceException;

/**
 * Adds biasgen functionality to base interface via Cypress FX2.
 *
 * @author tobi
 */
public class CypressFX3Biasgen extends CypressFX3 implements BiasgenHardwareInterface {

	/**
	 * max number of bytes used for each bias. For 24-bit biasgen, only 3 bytes are used, but we oversize considerably
	 * for the future.
	 */
	public static final int MAX_BYTES_PER_BIAS = 8;

	/**
	 * Creates a new instance of CypressFX3Biasgen. Note that it is possible to construct several instances
	 * and use each of them to open and read from the same device.
	 *
	 * @param devNumber
	 *            the desired device number, in range returned by CypressFX3Factory.getNumInterfacesAvailable
	 * @see CypressFX3TmpdiffRetinaFactory
	 */
	protected CypressFX3Biasgen(final Device device) {
		super(device);
	}

	/*
	 * sets the powerdown input pin to the biasgenerator.
	 * Chip may have been plugged in without being
	 * powered up. To ensure the biasgen is powered up, a negative transition is necessary. This transistion is
	 * necessary to ensure the startup circuit starts up the masterbias again.
	 *
	 * if this method is called from a GUI is may be desireable to actually toggle the powerdown pin high and then low
	 * to ensure the chip is powered up.
	 * otherwise it doesn't make sense to always toggle this pin because it will perturb the chip operation
	 * significantly.
	 * For instance, it should not be called very time new bias values are sent.
	 *
	 * @param powerDown true to power OFF the biasgen, false to power on
	 */
	@Override
	synchronized public void setPowerDown(final boolean powerDown) throws HardwareInterfaceException {
		// DO NOTHING.
	}

	/**
	 * sends the ipot values.
	 *
	 * @param biasgen
	 *            the biasgen which has the values to send
	 */
	@Override
	synchronized public void sendConfiguration(final net.sf.jaer.biasgen.Biasgen biasgen)
		throws HardwareInterfaceException {
		if ((biasgen != null) && (biasgen instanceof DavisConfig)) {
			((DavisConfig) biasgen).sendConfiguration();
		}
	}


	@Override
	synchronized public void flashConfiguration(final Biasgen biasgen) throws HardwareInterfaceException {
		JOptionPane.showMessageDialog(null, "Flashing biases not yet supported on CypressFX3");
	}

	/**
	 * This implementation delegates the job of getting the bytes to send to the Biasgen object.
	 * Depending on the hardware interface, however, it may be that a particular subclass of this
	 * should override formatConfigurationBytes to return a different set of data.
	 *
	 * @param biasgen
	 *            the source of configuration information.
	 * @return the bytes to send
	 */
	@Override
	public byte[] formatConfigurationBytes(final Biasgen biasgen) {
		final byte[] b = biasgen.formatConfigurationBytes(biasgen);
		return b;
	}
}

@@ -112,24 +112,65 @@
 va.Device;%0A%0A
+import eu.seebetter.ini.chips.DavisChip;%0A
 import eu.se
@@ -2238,19 +2238,17 @@
 biasgen)
-%0A%09%09
+ 
 throws H
@@ -2394,16 +2394,408 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@Override%0A%09synchronized public void open() throws HardwareInterfaceException %7B%0A%09%09super.open();%0A%0A%09%09// And now send the current bias values to the device to enable it.%0A%09%09if ((getChip() != null) && (getChip() instanceof DavisChip) && (getChip().getBiasgen() != null)%0A%09%09%09&& (getChip().getBiasgen() instanceof DavisConfig)) %7B%0A%09%09%09((DavisConfig) getChip().getBiasgen()).sendConfiguration();%0A%09%09%7D%0A%09%7D%0A
 %0A%09@Overr

send full configuration on HWInterface open() too, this should automatically enable devices at startup.