package devices.LEDMax7219;

import com.sun.jna.Pointer;

import anywheresoftware.b4a.BA;

@BA.ShortName("LEDBlock")
public class LEDBlock {
	private Pointer[] rows = new Pointer[8];
	
	@BA.Hide
	public void setpointer(int row, Pointer value) {
		rows[row] = value;
	}
	
	public void SetPixel(int rownumber, int cellnumber, boolean value) {
		Pointer vv = rows[rownumber];
		
		int mask = (1 << cellnumber);
		int prev = vv.getByte(0);
		
		if (value) {
			prev = prev | mask;
		} else {
			prev = prev & ~(mask);
		}
		
		vv.setByte(0, (byte) prev);
	}
	
	public short GetRowValue(int rownumber) {
		Pointer vv = rows[rownumber];
		short result = (short)(get_mask(rownumber) | (vv.getByte(0) & 0xFF));
		return result;
	}
	
	private short get_mask(int baris) {
		switch(baris) {
		case 1:		// baris 1
			return 0x200;
		case 2:
			return 0x300;
		case 3:
			return 0x400;
		case 4:
			return 0x500;
		case 5:
			return 0x600;
		case 6:
			return 0x700;
		case 7:
			return 0x800;
		default :	// baris 0 
			return 0x100;
	}
}
	
}
