/**
 * Instances of this class are matricial keyboard with a Mux and a DeMux.
 * <BR>	- Pin 1 to 3 are inputs : demux entry -> col.
 * <BR>	- Pin 4 to 6 are outputs : mux output -> ~line.
 * <BR>	- Pin 7 is output : if true -> some keys are pressed.
 * <BR>	- Pin 8 is output : 8th line of the keyboard.
 * <BR>You can read the source of this class <A HREF="./MatricialKeyboard.java.html"> here</A>.
 */
public class MatricialKeyboard extends DigitalComponent_std implements DigitalComponent {

	public MatricialKeyboard(DigitalComponentUI dui) {
		super(8, dui);
		Matrice = new boolean[8][];
		int i;
		for(i = 0 ; i < 8 ; i++)
			Matrice[i] = new boolean[9];

		Matrice[1][0] = true; //"3"
	}

	public MatricialKeyboard() {
		super(8);
		Matrice = new boolean[8][];
		int i;
		for(i = 0 ; i < 8 ; i++)
			Matrice[i] = new boolean[9];

		Matrice[1][0] = true; //"3"
	}

	public void setPinStateDontNotify(int p, boolean state) {
		super.setPinStateDontNotify(p, state);

		col = readNumberOnPins(1, 3);

		writeNumberOnPins(4, 6, ~keyPressed(col));

		if((keyPressed(col) != -1) && (keyPressed(col) <= 7))
			setPinState(7, true);
		else
			setPinState(7, false);

		if(keyPressed(col) == 8)
			setPinState(8, false);
		else
			setPinState(8, true);

		refreshDisplay();
	}

	protected int keyPressed(int col) {
		int i;

		for(i = 0 ; i <= 8 ; i++)
			if(Matrice[col][i])
				return i;

		return -1;
	}

	public String getState() {
		return "Col asked : " + col + "\n";
	}

	protected int col;
	protected boolean Matrice[][];
}