#include <p16f648a.inc>

	radix dec
	
__config _CP_OFF & _DATA_CP_OFF & _HS_OSC & _PWRTE_ON & _WDT_OFF & _BOREN_OFF & _LVP_OFF & _MCLRE_ON

BorderMark	equ	0xC0

prodl	equ 0xA0
prodl1	equ	0xA1
prodl2	equ	0xA2


#define PB_BLUE_LED 7
#define PB_RED_LED 4
#define PB_GREEN_LED 6

Wait302us	macro	; T = 256 * 4 * T_inst
	clrf	Var_MicroCount
	nop
	decfsz	Var_MicroCount, f
    goto    $ - 2
endm

SendUARTByte2	macro
	btfss	PIR1, TXIF
	goto	$-1			; cekani na vyprazdneni TXREG
	movwf	TXREG		; odeslani
endm

SendUARTByte	macro	value
	movlw	value		; hodnotu dam do W a poslu
	SendUARTByte2		
endm

Command		macro	command, sum
	SendUARTByte	BorderMark
	SendUARTByte	0x01
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x01
	SendUARTByte	command
	SendUARTByte	0x00		; 2 bajty souctu
	SendUARTByte	sum
	SendUARTByte	BorderMark
endm

Command2	macro	command, buffer, sum
	SendUARTByte	BorderMark
	SendUARTByte	0x01
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x02
	SendUARTByte	command
	SendUARTByte	buffer
	SendUARTByte	0x00		; 2 bajty souctu
	SendUARTByte	sum
	SendUARTByte	BorderMark
endm

Command4	macro	command, buffer, PageID, sum
	SendUARTByte	BorderMark
	SendUARTByte	0x01
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x00
	SendUARTByte	0x04
	SendUARTByte	command
	SendUARTByte	buffer
	SendUARTByte	0x00
	SendUARTByte	PageID
	SendUARTByte	0x00		; 2 bajty souctu
	SendUARTByte	sum
	SendUARTByte	BorderMark
endm

RecieveByte		macro 	
	clrf	Var_MD_ByteCount
;	banksel	Var_MD_Buf		; bank 0
	bcf		STATUS, IRP		; bank 0, 1 for INDF
	movlw	Var_MD_Buf
	movwf	FSR			; pro ukladani jednotlivych bajtu pouziji VAR_MD_Buf, budu pocitat prisle bajty a inkrementovat FSR, ukladani do INDF
;	bcf		PORTB, PB_GREEN_LED
;	btfss	PIR1, RCIF	; testovani priznaku naplneni
;	goto	$-1			; cekani na na naplneni RCREG
;	movf	RCREG,0		; vybere RCREG a hodi do w
;	subwf	BorderMark, w			; testuju jestli uz je prijat 0xC0 jako !!!zacatek!!! ramce od ctecky, odectu W od Borderu
;	btfss	STATUS, Z				; a testuju na nulovy vysledek
;	goto	$-5	
	
; smycka
	btfss	PIR1, RCIF	; testovani priznaku naplneni
	goto	$-1			; cekani na na naplneni RCREG
	movf	RCREG,0		; vybere RCREG a hodi do w
	movwf	INDF		; hodim w na hodnotu FSR
	incf	FSR
	incf	Var_MD_ByteCount
;	bcf		PORTB, PB_RED_LED
	subwf	BorderMark, w			; testuju jestli uz je prijat 0xC0 jako konec ramce od ctecky, odectu W od Borderu
	btfss	STATUS, Z				; a testuju na nulovy vysledek
	goto	$-7				; dokud nedetekuju 0xC0, a vratim se							
	movlw	Var_MD_Buf + 8
	xorlw	0x00				; prenasobym FF, jestli bude vysledek nulovy, detekce probehla uspesne, mozna predelam akorat na testovani, btfsc prvniho bitu w
	btfsc	STATUS, Z
endm

cblock 0x20
    Var_MD_Buf:64	; buffer
	Var_MD_ByteCount	; pocet bitu
	Var_MicroCount	; promenna casu
endc


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Reset
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	org		0
Reset

	
	CLRF PORTA ;Initialize PORTA by
	MOVLW 0x07 ;Turn comparators off and
	MOVWF CMCON ;enable pins for I/O
	;functions
	BCF STATUS, RP1
	BSF STATUS, RP0 ;Select Bank1
	MOVLW 0x1F ;Value used to initialize
	;data direction
	MOVWF TRISA ;Set RA<4:0> as inputs
	;TRISA<5> always
	;read as ‘1’.
	;TRISA<7:6>
	;depend on oscillator
	;mode
		
	clrf	PORTB
	movlw	0x0E	
	movwf	TRISB

	bsf		STATUS, RP0	; bank 1
	movlw	0x27	
	movwf	TRISA
; for UART mode of RB1 and RB2 (assume the RS232 module is connected even)
; we should drive RB_1,2 as inputs
; the rest of PORTB is configured as output (possibly constant in L state)	
	movlw	0x0E	
	movwf	TRISB
		
; UART init

	banksel	SPBRG			; bank 1
	movlw	0x0E			; 13.56/ 16*15 ..................56500 kbps  
	movwf	SPBRG			; 
	bsf		TXSTA, BRGH		; high speed constant
	bcf		TXSTA, SYNC		; asynchronnni mod
	bcf		TXSTA, TX9		; 8 bits
	bcf		STATUS, RP0		; bank 0
	bsf		RCSTA, SPEN		; serial port enable

; .........................nastaveni prijmu..........................................
	bcf		STATUS, RP0		; prepnuti do banky 0 kde jsou registry 
	bcf		RCSTA, RX9		; prenasim 8 bitu
	bsf		RCSTA, CREN		; povolení kontinualniho prijmu RX

	bsf		STATUS, RP0		; bank 1
	bsf		TXSTA, TXEN		; povoleni vysilani TX
	Wait302us				; RB2 was an input tied to L, so we wait for its stabilization

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; obsluha ctecky ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;******************************************************************************************************
; zde jsme otestovali ze je pritomny vf signal ctecky, takze zjistime zda jsme 
; opravneni vyslat dane UID, prikaz ctecce zdali je prilozen prst, ............................................misto DTR dam tlacitko pro ukladani otisku
	bcf		PORTB, PB_BLUE_LED
	bsf		PORTB, PB_RED_LED
	bsf		PORTB, PB_GREEN_LED

	bcf		PORTB, PB_RED_LED
	bcf		PORTB, PB_GREEN_LED

prodleva:
		decfsz	prodl
		goto	$-1
		decfsz	prodl1
		goto	$-3
		decfsz	prodl2
		goto	$-5
	bsf		PORTB, PB_RED_LED
	bsf		PORTB, PB_GREEN_LED



DetectFinger:
;   btfsc		DTR
;   clrf nebo nejaky nastaveni bytu abych poznal, jestli bylo zmacknuty 	
	Command 	0x01, 0x03

; vyhodnoceni DetectFinger

; DetectFingerRes:
	RecieveByte
	goto	DetectFinger
	bcf		PORTB, PB_GREEN_LED

EnrollImag:
	Command		0x02, 0x04

; EnrollImagRes:
	RecieveByte
	goto	EnrollImag			; nebo navrat na detectFinger

GenTemplet:
	Command2	0x03, 0x01, 0x07

; GenTempletRes:
	RecieveByte
	goto	EnrollImag			; nebo navrat na detectFinger

LevelSearch:
	Command2	0x25, 0x01, 0x29
	

; LevelSearchRes:
	RecieveByte
	goto	OtiskNalezen
	;bcf		PORTB, PB_RED_LED	; otisk nenalezen
	; zpozdeni
	goto	DetectFinger

OtiskNalezen:	
	bcf		PORTB, PB_RED_LED
	; zpozdeni

;MergeTwoTemplate:
;	Command		0x06, 0x08
;
;	RecieveByte
;
;StoreTemplate:
;	Command		0x07, 0x03, 0x01, 0xA0		; prekontrolovat soucet a nejakou promenou kterou budu menit stranky pro ulozeni
;	
;	RecieveByte


end	