package devices.LCDSPI;

import anywheresoftware.b4a.BA;
import jGPIO.mycodes;

//@BA.ShortName("LCDSPI")
@BA.Events(values= {
		"log(msg as string)"
})

/**
 * LCD SPI Low level
 * Source : https://github.com/gitcnd/LCDWIKI_SPI
 * @author rdkartono
 *
 */
@SuppressWarnings("unused")
public class LcdSpi {
	private BA ba;
	private String event;
	private Object Me;
	private boolean need_log_event = false;
	
	public enum LCD_Mode_Identifiers{
		ILI9325("ILI9325",0),
		ILI9328("ILI9328 ",1),
		ILI9341("ILI9341 ",2),
		HX8357D("HX8357D ",3),
		HX8347G("HX8347G ",4),
		HX8347I("HX8347I ",5),
		ILI9486("ILI9486 ",6),
		ST7735S("ST7735S ",7),
		SSD1283A("SSD1283A ",8);
		public final String label;
		public final int mode_identifiers;
		
		private LCD_Mode_Identifiers(String label, int mode_identifier) {
			this.label = label;
			this.mode_identifiers = mode_identifier;
		}		
	}
	
	public enum LCD_Identifiers{
		ID_932X("ID_932X",0),
		ID_7575("ID_7575",1),
		ID_9341("ID_9341",2),
		ID_HX8357D("ID_HX8357D",3),
		ID_4535("ID_4535",4),
		ID_9486("ID_9486",5),
		ID_7735("ID_7735",6),
		ID_1283A("ID_1283A",7),
		ID_UNKNOWN("ID_UNKNOWN",0xFF);
		
		public final String label;
		public final int identifier;
		private LCD_Identifiers(String label, int identifier) {
			this.label = label;
			this.identifier = identifier;
		}
	}
	
	protected short WIDTH, HEIGHT, width, height, rotation, lcd_driver, lcd_model;
	protected boolean hw_spi=false;
	
	
	private short XC, YC, CC, RC, SC1, SC2, MD, VL, R24BIT;
	private volatile static byte spicsPort, spicdPort, spimisoPort, spimosiPort, spiclkPort;
	private volatile static byte spicsPinSet, spicdPinSet, spimisoPinSet, spimosiPinSet, spiclkPinSet;
	private volatile static byte spicsPinUnset, spicdPinUnset, spimisoPinUnset, spimosiPinUnset, spiclkPinUnset;
	private volatile byte _cs, _cd, _miso, _mosi, _clk, _reset, _led;
	
	
	/**
	 * Initialize SPI LCD
	 * @param event Event name
	 */
	public void Initialize(BA ba, String event) {
		this.ba = ba;
		this.event = event;
		Me = this;
		check_events();
	}
	
	void Init_LCD() {
		//TODO lanjutin
	}
	
	void reset() {
		//TODO lanjutin
	}
	
	void start(short ID) {
		//TODO lanjutin
	}
	
	void Draw_Pixel(short x, short y, int color) {
		//TODO lanjutin
	}
	
	void Spi_Write(byte data) {
		//TODO lanjutin
	}
	
	byte Spi_Read() {
		//TODO lanjutin
		return 0;
	}
	
	void Write_Cmd(int cmd) {
		//TODO lanjutin
	}
	
	void Write_Data(int data) {
		//TODO lanjutin
	}
	
	void Write_Cmd_Data(int cmd, int data) {
		//TODO lanjutin
	}
	
	void init_table8(cmd_data[] table, int size) {
		//TODO lanjutin
	}
	
	void init_table16(cmd_data[] table, int size) {
		//TODO lanjutin
	}
	
	void Push_Command(byte cmd, byte[] block, int size) {
		//TODO lanjutin
	}
	
	short Color_To_565(byte R, byte G, byte B) {
		//TODO lanjutin
		return 0;
	}
	
	short Read_ID() {
		//TODO lanjutin
		return 0;
	}
	
	void Fill_Rect(int x, int y, int w, int h, int color) {
		//TODO lanjutin
	}
	
	void Set_Rotation(byte r) {
		//TODO lanjutin
	}
	
	byte Get_Rotation() {
		//TODO lanjutin
		return 0;
	}
	
	void Invert_Display(boolean i) {
		//TODO lanjutin
	}
	
	int Read_Reg(int reg, byte index) {
		//TODO lanjutin
		return 0;
	}
	
	int Read_GRAM(int x, int y, byte[] block, int w, int h) {
		//TODO lanjutin
		return 0;
	}
	
	void Set_Addr_Window(int x1, int y1, int x2, int y2) {
		//TODO lanjutin
		
	}
	
	void Push_Any_Color(short[] block, int n, boolean first, byte flags) {
		//TODO lanjutin
	}
	
	void Vert_Scroll(short top, short scrolllines, short offset) {
		//TODO lanjutin
	}
	
	short Get_Height() {
		//TODO lanjutin
		return 0;
	}
	
	short Get_Width() {
		//TODO lanjutin
		return 0;
	}
	
	void Set_LR() {
		//TODO lanjutin
	}
	
	void Let_control(boolean i) {
		//TODO lanjutin
	}
	
	
	
	@BA.ShortName("SPI_CMD_DATA") 
	class cmd_data{
		public int cmd;
		public int data;
	}
	
	
	class lcd_info{
		short lcd_id;
		int lcd_wid;
		int lcd_heg;
	}
	
	static class mcu_spi_magic{
		public static void CD_COMMAND() {
			spicdPort &= spicdPinUnset;
		}
		public static void CD_DATA() {
			spicdPort |= spicdPinSet;
		}
		
		public static void CS_ACTIVE() {
			spicsPort &= spicsPinUnset;
		}
		
		public static void CS_IDLE() {
			spicsPort |= spicsPinSet;
		}
		public static byte MISO_STATE() {
			return (byte)(spimisoPort & spimisoPinSet);
		}
		public static void MOSI_LOW() {
			spimosiPort &= spimosiPinUnset;
		}
		public static void MOSI_HIGH() {
			spimosiPort |= spimosiPinSet;
		}
		
		public static void CLK_LOW() {
			spiclkPort &= spiclkPinUnset;
		}
		
		public static void CLK_HIGH() {
			spiclkPort |= spiclkPinSet;
		}
		
	}
	
	static class lcd_spi_register{
		public final static int ILI932X_START_OSC          = 0x00;
		public final static int ILI932X_DRIV_OUT_CTRL      = 0x01;
		public final static int ILI932X_DRIV_WAV_CTRL      = 0x02;
		public final static int ILI932X_ENTRY_MOD          = 0x03;
		public final static int ILI932X_RESIZE_CTRL        = 0x04;
		public final static int ILI932X_DISP_CTRL1         = 0x07;
		public final static int ILI932X_DISP_CTRL2         = 0x08;
		public final static int ILI932X_DISP_CTRL3         = 0x09;
		public final static int ILI932X_DISP_CTRL4         = 0x0A;
		public final static int ILI932X_RGB_DISP_IF_CTRL1  = 0x0C;
		public final static int ILI932X_FRM_MARKER_POS     = 0x0D;
		public final static int ILI932X_RGB_DISP_IF_CTRL2  = 0x0F;
		public final static int ILI932X_POW_CTRL1          = 0x10;
		public final static int ILI932X_POW_CTRL2          = 0x11;
		public final static int ILI932X_POW_CTRL3          = 0x12;
		public final static int ILI932X_POW_CTRL4          = 0x13;
		public final static int ILI932X_GRAM_HOR_AD        = 0x20;
		public final static int ILI932X_GRAM_VER_AD        = 0x21;
		public final static int ILI932X_RW_GRAM            = 0x22;
		public final static int ILI932X_POW_CTRL7          = 0x29;
		public final static int ILI932X_FRM_RATE_COL_CTRL  = 0x2B;
		public final static int ILI932X_GAMMA_CTRL1        = 0x30;
		public final static int ILI932X_GAMMA_CTRL2        = 0x31;
		public final static int ILI932X_GAMMA_CTRL3        = 0x32;
		public final static int ILI932X_GAMMA_CTRL4        = 0x35;
		public final static int ILI932X_GAMMA_CTRL5        = 0x36;
		public final static int ILI932X_GAMMA_CTRL6        = 0x37;
		public final static int ILI932X_GAMMA_CTRL7        = 0x38;
		public final static int ILI932X_GAMMA_CTRL8        = 0x39;
		public final static int ILI932X_GAMMA_CTRL9        = 0x3C;
		public final static int ILI932X_GAMMA_CTRL10       = 0x3D;
		public final static int ILI932X_HOR_START_AD       = 0x50;
		public final static int ILI932X_HOR_END_AD         = 0x51;
		public final static int ILI932X_VER_START_AD       = 0x52;
		public final static int ILI932X_VER_END_AD         = 0x53;
		public final static int ILI932X_GATE_SCAN_CTRL1    = 0x60;
		public final static int ILI932X_GATE_SCAN_CTRL2    = 0x61;
		public final static int ILI932X_GATE_SCAN_CTRL3    = 0x6A;
		public final static int ILI932X_PART_IMG1_DISP_POS = 0x80;
		public final static int ILI932X_PART_IMG1_START_AD = 0x81;
		public final static int ILI932X_PART_IMG1_END_AD   = 0x82;
		public final static int ILI932X_PART_IMG2_DISP_POS = 0x83;
		public final static int ILI932X_PART_IMG2_START_AD = 0x84;
		public final static int ILI932X_PART_IMG2_END_AD   = 0x85;
		public final static int ILI932X_PANEL_IF_CTRL1     = 0x90;
		public final static int ILI932X_PANEL_IF_CTRL2     = 0x92;
		public final static int ILI932X_PANEL_IF_CTRL3     = 0x93;
		public final static int ILI932X_PANEL_IF_CTRL4     = 0x95;
		public final static int ILI932X_PANEL_IF_CTRL5     = 0x97;
		public final static int ILI932X_PANEL_IF_CTRL6     = 0x98;
		
		public final static int HX8347G_COLADDRSTART_HI    = 0x02;
		public final static int HX8347G_COLADDRSTART_LO    = 0x03;
		public final static int HX8347G_COLADDREND_HI      = 0x04;
		public final static int HX8347G_COLADDREND_LO      = 0x05;
		public final static int HX8347G_ROWADDRSTART_HI    = 0x06;
		public final static int HX8347G_ROWADDRSTART_LO    = 0x07;
		public final static int HX8347G_ROWADDREND_HI      = 0x08;
		public final static int HX8347G_ROWADDREND_LO      = 0x09;
		public final static int HX8347G_MEMACCESS          = 0x16;



		public final static int ILI9341_SOFTRESET          = 0x01;
		public final static int ILI9341_SLEEPIN            = 0x10;
		public final static int ILI9341_SLEEPOUT           = 0x11;
		public final static int ILI9341_NORMALDISP         = 0x13;
		public final static int ILI9341_INVERTOFF          = 0x20;
		public final static int ILI9341_INVERTON           = 0x21;
		public final static int ILI9341_GAMMASET           = 0x26;
		public final static int ILI9341_DISPLAYOFF         = 0x28;
		public final static int ILI9341_DISPLAYON          = 0x29;
		public final static int ILI9341_COLADDRSET         = 0x2A;
		public final static int ILI9341_PAGEADDRSET        = 0x2B;
		public final static int ILI9341_MEMORYWRITE        = 0x2C;
		public final static int ILI9341_MEMORYACCESS       = 0x36;
		public final static int ILI9341_PIXELFORMAT        = 0x3A;
		public final static int ILI9341_RGBSIGNAL          = 0xB0;
		public final static int ILI9341_FRAMECONTROL       = 0xB1;
		public final static int ILI9341_INVERSIONCONRTOL   = 0xB4;
		public final static int ILI9341_DISPLAYFUNC        = 0xB6;
		public final static int ILI9341_ENTRYMODE          = 0xB7;
		public final static int ILI9341_POWERCONTROL1      = 0xC0;
		public final static int ILI9341_POWERCONTROL2      = 0xC1;
		public final static int ILI9341_VCOMCONTROL1       = 0xC5;
		public final static int ILI9341_VCOMCONTROL2       = 0xC7;
		public final static int ILI9341_POWERCONTROLB      = 0xCF;
		public final static int ILI9341_POWERCONTROLA      = 0xCB;
		public final static int ILI9341_UNDEFINE0          = 0xE0;
		public final static int ILI9341_UNDEFINE1          = 0xE1;
		public final static int ILI9341_DRIVERTIMINGA      = 0xE8;
		public final static int ILI9341_DRIVERTIMINGB      = 0xEA;
		public final static int ILI9341_POWERONSEQ         = 0xED;
		public final static int ILI9341_ENABLE3G           = 0xF2;
		public final static int ILI9341_INTERFACECONTROL   = 0xF6;
		public final static int ILI9341_RUMPRATIO          = 0xF7;
		public final static int ILI9341_MEMCONTROL         = 0x36;
		public final static int ILI9341_MADCTL             = 0x36;

		public final static int ILI9341_MADCTL_MY  = 0x80;
		public final static int ILI9341_MADCTL_MX  = 0x40;
		public final static int ILI9341_MADCTL_MV  = 0x20;
		public final static int ILI9341_MADCTL_ML  = 0x10;
		public final static int ILI9341_MADCTL_RGB = 0x00;
		public final static int ILI9341_MADCTL_BGR = 0x08;
		public final static int ILI9341_MADCTL_MH  = 0x04;



		public final static int HX8357_NOP     = 0x00;
		public final static int HX8357_SWRESET = 0x01;
		public final static int HX8357_RDDID   = 0x04;
		public final static int HX8357_RDDST   = 0x09;

		public final static int HX8357B_RDPOWMODE  = 0x0A;
		public final static int HX8357B_RDMADCTL  = 0x0B;
		public final static int HX8357B_RDCOLMOD  = 0x0C;
		public final static int HX8357B_RDDIM  = 0x0D;
		public final static int HX8357B_RDDSDR  = 0x0F;

		public final static int HX8357_SLPIN   = 0x10;
		public final static int HX8357_SLPOUT  = 0x11;
		public final static int HX8357B_PTLON   = 0x12;
		public final static int HX8357B_NORON   = 0x13;

		public final static int HX8357_INVOFF  = 0x20;
		public final static int HX8357_INVON   = 0x21;
		public final static int HX8357_DISPOFF = 0x28;
		public final static int HX8357_DISPON  = 0x29;

		public final static int HX8357_CASET   = 0x2A;
		public final static int HX8357_PASET   = 0x2B;
		public final static int HX8357_RAMWR   = 0x2C;
		public final static int HX8357_RAMRD   = 0x2E;

		public final static int HX8357B_PTLAR   = 0x30;
		public final static int HX8357_TEON  = 0x35;
		public final static int HX8357_TEARLINE  = 0x44;
		public final static int HX8357_MADCTL  = 0x36;
		public final static int HX8357_COLMOD  = 0x3A;

		public final static int HX8357_SETOSC = 0xB0;
		public final static int HX8357_SETPWR1 = 0xB1;
		public final static int HX8357B_SETDISPLAY = 0xB2;
		public final static int HX8357_SETRGB = 0xB3;
		public final static int HX8357D_SETCOM  = 0xB6;

		public final static int HX8357B_SETDISPMODE  = 0xB4;
		public final static int HX8357D_SETCYC  = 0xB4;
		public final static int HX8357B_SETOTP = 0xB7;
		public final static int HX8357D_SETC = 0xB9;

		public final static int HX8357B_SET_PANEL_DRIVING = 0xC0;
		public final static int HX8357D_SETSTBA = 0xC0;
		public final static int HX8357B_SETDGC  = 0xC1;
		public final static int HX8357B_SETID  = 0xC3;
		public final static int HX8357B_SETDDB  = 0xC4;
		public final static int HX8357B_SETDISPLAYFRAME = 0xC5;
		public final static int HX8357B_GAMMASET = 0xC8;
		public final static int HX8357B_SETCABC  = 0xC9;
		public final static int HX8357_SETPANEL  = 0xCC;


		public final static int HX8357B_SETPOWER = 0xD0;
		public final static int HX8357B_SETVCOM = 0xD1;
		public final static int HX8357B_SETPWRNORMAL = 0xD2;

		public final static int HX8357B_RDID1   = 0xDA;
		public final static int HX8357B_RDID2   = 0xDB;
		public final static int HX8357B_RDID3   = 0xDC;
		public final static int HX8357B_RDID4   = 0xDD;

		public final static int HX8357D_SETGAMMA = 0xE0;

		public final static int HX8357B_SETGAMMA = 0xC8;
		public final static int HX8357B_SETPANELRELATED  = 0xE9;

		public final static int HX8357B_MADCTL_MY  = 0x80;
		public final static int HX8357B_MADCTL_MX  = 0x40;
		public final static int HX8357B_MADCTL_MV  = 0x20;
		public final static int HX8357B_MADCTL_ML  = 0x10;
		public final static int HX8357B_MADCTL_RGB = 0x00;
		public final static int HX8357B_MADCTL_BGR = 0x08;
		public final static int HX8357B_MADCTL_MH  = 0x04;
	}
	
	private void check_events() {
		if (ba!=null) {
			if (mycodes.ValidString(event)) {
				need_log_event = ba.subExists(event+"_log");
			}
		}
	}
	
	private void raise_log(String msg) {
		if (need_log_event) ba.raiseEventFromDifferentThread(Me, null, 0, event+"_log", false, new Object[] {msg});
	}
	
	
}
