;*********************************************************************************
;	gpcardisplay-lcd2-test1.bas	GpCarDisplay board test
;				Louise@RotaryRacer, Terry@RotaryRacer
;*********************************************************************************
;
; This tests writing information onto the LCD2 display.
; This assumes a GpCarDisplay LCD display
;
; For: PICAXE-20X2
;
; Pins are:
;	C.0	Switch0
;	C.1	Switch1
;	C.6	Switch2
;	C.5	Switch3
;	C.4	Switch4
;	C.3	GpSpeed1
;	C.2	GpSpeed0
;	B.4	Led0
;	B.3	Led1
;	B.2	Led2
;	B.1	Led3
;	B.0	Led4
;	B.7	I2c_Clk
;	B.5	I2c_Dat
;	C.7	LcdReset
;

; Symbols for pin names and variables
symbol	pinLed0		= B.4			; The LED pin
symbol	pinLed1		= B.3			; The LED pin
symbol	pinLed2		= B.2			; The LED pin
symbol	pinLed3		= B.1			; The LED pin

symbol	pinSwitch0	= pinC.4		; The Switch0 pin
symbol	pinSwitch1	= pinC.0		; The Switch1 pin
symbol	pinSwitch2	= pinC.5		; The Switch2 pin

symbol	pinLcdReset	= C.7			; The LCD reset pin

init:
;	setfreq M8
	let adcsetup = %000000000001111		; Initialises ADC inputs
	pause 1000
	sertxd ("Started: ok", 13, 10)

	;gosub ledLoop
	;gosub switchLoop
	;gosub lcdTest
	
	gosub lcdInit

	hi2cout ($00, $80)	; Set cursor to halfway along line 1
	hi2cout ($40, "Hello")
	
	b0 = "A";
	b2 = 64
	gosub lcdPutChar

	b0 = 123;
	b2 = 64 + 2
	gosub lcdPutDecimalByte

	w0 = 1234;
	b2 = 64 + 8
;	gosub lcdPutDecimalWord
	gosub lcdPutDecimalWord100

mainLoop:
	toggle pinLed0
	pause 1000
	toggle pinLed1
	pause 2000
	toggle pinLed2
	pause 3000
	goto mainLoop
	
ledLoop:
	toggle pinLed0
	toggle pinLed1
	toggle pinLed2
	toggle pinLed3
	pause 500
	goto ledLoop
	
switchLoop:
	if pinSwitch0 = 1 then high pinLed0 else low pinLed0 end if
	if pinSwitch1 = 1 then high pinLed1 else low pinLed1 end if
	pause 100
	goto switchLoop
	


lcdTest:
	; Reset the LCD	
	low pinLcdReset
	pause 10
	high pinLcdReset
	pause 100

;	hi2csetup i2cmaster,$3E,i2cslow,i2cbyte		; Setup I2C hardware with address of LCD nodule
	hi2csetup i2cmaster,$7C,i2cslow,i2cbyte		; Setup I2C hardware with address of LCD nodule

loop0:
;	goto loop0

loop1:
	hi2cout ($00, $38)	; Set function
	pause 10
	goto loop1


;******************************************************************************
;	LCD Subroutines
;******************************************************************************

; Initialise the LCD display
lcdInit:
	; Reset the LCD	
	low pinLcdReset
	pause 10
	high pinLcdReset
	pause 100

;	hi2csetup i2cmaster,$3E,i2cslow,i2cbyte		; Setup I2C hardware with address of LCD nodule
	hi2csetup i2cmaster,$7C,i2cslow,i2cbyte		; Setup I2C hardware with address of LCD nodule

	hi2cout ($00, $38)	; Set function
	hi2cout ($00, $39)	; Set function
	hi2cout ($00, $14)	; Set Oscilator frequency
	hi2cout ($00, $79)	; Set Contrast
	hi2cout ($00, $50)	; Set PowerOn/Contrast
	
	hi2cout ($00, $6C)	; Follower control
	hi2cout ($00, $0C)	; Set display on/off
	
	pause 100
;	hi2cout ($00, $01)	; Clear display
	
	gosub lcdClear
	return

; Set the print position to the position defined in b2. Line 1 = +64
lcdClear:
	hi2cout ($00, $01)
	return
	
; Set the print position to the position defined in b2. Line 1 = +64
lcdSetPos:
	b2 = 128 + b2
	hi2cout ($00, b2)
	return

; Print a single character that is in b0 at the position defined in b2. Line 1 = +64
lcdPutChar:
	b2 = 128 + b2
	hi2cout ($80,b2,$40,b0)
	pause 10
	return

;Print the value in b0 as a decimal at the position defined in b2. Line 1 = +64
lcdPutDecimalByte:
	b2 = 128 + b2
	b3 = b0 / 100 + 48
	b0 = b0 // 100
	b4 = b0 / 10 + 48
	b0 = b0 // 10;
	b5 = b0 + 48
	
	hi2cout ($80,b2,$40,b3,b4,b5)
	pause 10
	return

;Print the value in w0 as a decimal at the position defined in b2. Line 1 = +64
lcdPutDecimalWord:
	b2 = 128 + b2
	b3 = w0 / 10000 + 48
	w0 = w0 // 10000;
	b4 = w0 / 1000 + 48
	w0 = w0 // 1000;
	b5 = w0 / 100 + 48
	w0 = w0 // 100;
	b6 = w0 / 10 + 48
	w0 = w0 // 10;
	b7 = w0 + 48

	if b3 = $30 then
		b3 = $20
		if b4 = $30 then
			b4 = $20
			if b5 = $30 then
				b5 = $20
				if b6 = $30 then
					b6 = $20
				endif
			endif
		endif
	endif

	hi2cout	($80,b2,$40,b3,b4,b5,b6,b7)
;	pause 10

	return

;Print the value in w0 as a decimal scaled by 100 at the position defined in b2. Line 1 = +64
lcdPutDecimalWord100:
	b2 = 128 + b2
	b3 = w0 / 10000 + 48
	w0 = w0 // 10000;
	b4 = w0 / 1000 + 48
	w0 = w0 // 1000;
	b5 = w0 / 100 + 48
	w0 = w0 // 100;
	b6 = w0 / 10 + 48
	w0 = w0 // 10;
	b7 = w0 + 48

	if b3 = $30 then
		b3 = $20
		if b4 = $30 then
			b4 = $20
		endif
	endif
		
	hi2cout($80,b2,$40,b3,b4,b5,$2E,b6,b7)
	pause 10
	return

;Print the value in b0 as a decimal. Only two digits at current position
lcdPutDecimal2:
	b4 = b0 / 10 + 48
	b0 = b0 // 10;
	b5 = b0 + 48
	
	hi2cout ($40,b4,b5)
	pause 10
	return