; recive_data_keyboard - pouziva CNT3,number
  ; neprimo i time1 (zpozdeni)

; proceed_keyboard_main_menu
; proceed_keyboard_Menu_mode - používá time3

; Wait_for_any_key
; *-----------------------------------------*
; Zde se definuje, kam se jednotlive informace o stisknutych tlacitkach
  ; ukladaji (v urcitem registru).
.EQU	plus_time_base = 0
.EQU	minus_time_base = 1
.EQU	menu = 2
.EQU	arrow_left = 3
.EQU	arrow_right = 4
.EQU	OK = 5
.EQU	X = 6
.EQU	run_stop = 7
  ; Dalsi "sada" tlacitek
.EQU	A_button = 0	; Tlacitko "A"
.EQU	B_button = 1	; "B"
.EQU	C_button = 2	; "C"
.EQU	D_button = 3	; ....

.EQU	E_button = 4
.EQU	F_button = 5
.EQU	G_button = 6
.EQU	H_button = 7
  ; tyto promenne musi byt  stejne nastaveny i v "keyboard_uC.asm", jinak
  ; dojde k "prehazeni" tlacitek

out_of_time:
	CBI control_port_ADC,main_ready_to_read_from_keyboard
	CLR tmp		; smaze data v registru tmp
	STS keyboard_L_SRAM,tmp		; a smazeme informace o stisklych
  ; klavesach, protoze vlastne jeste neni hotove vyhodnoceni
	STS keyboard_H_SRAM,tmp		; to same, ale pro 9. az 16.
  ; klavesu

	RET

recive_data_keyboard:
	SET	; nastavime T bit na "1" - pozdeji se tento bit bude kopirovat
  ; do number

	LDI data,0	; do "data" se bude ukladat informace o parite.
  ; v podstate rozhoduje pouze LSB. Musi byt shodne s nastavenim parity
  ; v "keyboard_uC"

	LDS CNT3,waiting_interval_keyboard_SRAM	; kontroluje, zda necekame
  ; na klavesnici prilis dlouho
  ; Napriklad kvuli tomu, ze "keyboard_uC" ceka na to, az uzivatel pusti
  ; stisknute tlacitko. Cislo udava pocet "pokusu". Teoreticky by stacilo
  ; 140 -> cca 35us -> doba za kterou se nactou tlacitka na klavesnici
  ; (pokud neni zadne stiskle). V podstate ale fungoue cokoli od 10 vys.
	CLR number	; sem se ukladaji veskere informace (postupne jsou
  ; ukladany do SRAM) prijate prez seriovou linku

main_keyboard_ready:
	SBI control_port_ADC,main_ready_to_read_from_keyboard	; nastavime priznak pripravenosti

wait_for_start_bit:
	DEC CNT3	; zapocteme jeden pokus cekani na klavesnici
	BREQ out_of_time	; pokud bude pocet pokusu vetsi nez
  ; je definovano vyse, pak CNT3 bude mit hodnotu 0000_0000 -> nastane skok
  ; na out_of_time

	SBIS control_port_ADC_in,keyboard_serial_data	; precteme si
  ; informace pinu keyboard_serial_data -> pokud bude na vstupu "0", pak se
  ; pokracuje na dalsi radek. Takze kdyz bude na vstupu "1" provede se skok
  ; (jedna instrukce se preskoci)  
	RJMP wait_for_start_bit	; pokud bude na vstupu "0"

  ; ok, dostali jsme se sem. Ted musime overit, ze impulz je opravdu
  ; "start bit" - ten trva cca 10us
	CALL wait_2us
	SBIS control_port_ADC_in,keyboard_serial_data	; kontrolujem, jestli
  ; je na vstupu stale log. "1"
	RJMP start_bit_error	; pokud je na vstupu uz "0", nejedna se o
  ; start bit -> vyhodnoceni
	; pokud je na vstupu stale "1", kontroluje se dal...

	CALL wait_2us
	SBIS control_port_ADC_in,keyboard_serial_data	; zase kontrola
	RJMP start_bit_error	; pokud je na vstupu "0" -> error

;	RCALL wait_2us
;	SBIS control_port_ADC_in,keyboard_serial_data
;	RJMP start_bit_error
  ; Je potreba brat v uvahu, ze prikazy SBIS zaberou 3 cykly! Pro 4Mhz je
  ; potreba toto brat v uvahu! (i pro 8Mhz). Na druhou stranu prenasime data
  ; celkem dost rychle, takze zase usetrime nejaky cas
  ; Toto by se dalo vyresit posilanim delsich impulzu, ale zase by se zvysil
  ; potrebny cas pro prenos dat

  ; Pokud se dostaneme sem, pak je vse v poradku -> data se uz vysilaji ->
  ; -> priznak "pripraveno" muzeme zmenit na "nepripraveno"
	CBI control_port_ADC,main_ready_to_read_from_keyboard	; "nepripraveno"

start_bit_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP start_bit_wait_for_end

bit_0:
	SBIS control_port_ADC_in,keyboard_serial_data	; cekame na zacatek
  ; impulzu
	RJMP bit_0	; pokud neni na vstupu "1", pak se dostaneme sem ->
  ; -> zase test jestli je na vstupu "1"
	CALL wait_3us	; pokud se dostaneme sem, znamena to, ze na vstupu
  ; se objevila "1" -> pockame 3us a potom se rozhodneme, zda se jedna o "0",
  ; nebo "1". Kratky impulz ("0") trva 2us, dlouhy ("1") pak 4us
	SBIS control_port_ADC_in,keyboard_serial_data	; opet test
  ; "1" skok, "0" - dalsi instrukce
	RJMP bit_1	; sem se dostaneme, pokud je na vstupu "0" -> neni
  ; potreba nic zapisovat, kontrola dalsiho bitu

	BLD number,0	; pokud je na vstupu stale "1", pak se program dostane
  ; sem a na LSB (0) se zapise "1"
	INC data	; a zmenime LSB u parity
bit_0_wait_for_end:	; a pockame na "mezeru"
	SBIC control_port_ADC_in,keyboard_serial_data	; kontrola, zda je na
  ; vstupu nula (mezera). Pokud je zde stale jednicka -> RJMP
	RJMP bit_0_wait_for_end

; a dalsi bit...
bit_1:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_1
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_2
	BLD number,1
	INC data
bit_1_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_1_wait_for_end
; a dalsi bit...
bit_2:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_2
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_3
	BLD number,2
	INC data
bit_2_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_2_wait_for_end
; a dalsi bit...
bit_3:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_3
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_4
	BLD number,3
	INC data
bit_3_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_3_wait_for_end
; a dalsi bit...
bit_4:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_4
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_5
	BLD number,4
	INC data
bit_4_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_4_wait_for_end
; a dalsi bit...
bit_5:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_5
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_6
	BLD number,5
	INC data
bit_5_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_5_wait_for_end
; a dalsi bit...
bit_6:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_6
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_7
	BLD number,6
	INC data
bit_6_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_6_wait_for_end
; a dalsi bit...
bit_7:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_7
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_8_pre
	BLD number,7
	INC data
bit_7_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_7_wait_for_end

bit_8_pre:
  ; Nyni mame 2us na to, abychom si zapsali data so SRAM. Pri taktu 4Mhz
  ; to znamena 8 cyklu, pri 8Mhz uz 16 cyklu. Nam staci pouze 3 cykly
	STS keyboard_L_SRAM,number	; nahraje data z number do SRAM na
  ; adresu "keyboard_L_SRAM". To trva 2 cykly
	CLR number	; toto trva 1 cykl
  ; Pokud bychom z nejakeho duvodu museli pracovat na 1Mhz, tak by se musela
  ; zvetsit mezera v programu "keyboard_uC"


; Nyni nacteme dalsi bity, ktere pak ulozime do "keyboard_H_SRAM"
bit_8:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_8
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_9
	BLD number,0
	INC data
bit_8_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_8_wait_for_end

bit_9:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_9
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_10
	BLD number,1
	INC data
bit_9_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_9_wait_for_end

bit_10:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_10
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_11
	BLD number,2
	INC data
bit_10_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_10_wait_for_end

bit_11:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_11
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_12
	BLD number,3
	INC data
bit_11_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_11_wait_for_end

bit_12:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_12
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_13
	BLD number,4
	INC data
bit_12_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_12_wait_for_end

bit_13:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_13
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_14
	BLD number,5
	INC data
bit_13_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_13_wait_for_end

bit_14:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_14
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_15
	BLD number,6
	INC data
bit_14_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_14_wait_for_end

bit_15:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_15
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_16_pre
	BLD number,7
	INC data
bit_15_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_15_wait_for_end


bit_16_pre:
  ; A zase zapiseme data do SRAM
	STS keyboard_H_SRAM,number
	CLR number


; dalsi data
  ; nyni info o rozsahu
bit_16:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_16
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_17
	BLD number,0
	INC data
bit_16_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_16_wait_for_end

bit_17:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_17
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_18
	BLD number,1
	INC data
bit_17_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_17_wait_for_end

bit_18:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_18
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_19_pre
	BLD number,2
	INC data
bit_18_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_18_wait_for_end

  ; Info o synchronizaci (int/ext).
  ; Ulozime data z "number" do range_SRAM
bit_19_pre:	; tato procedura se musi provest vzdy
	STS range_SRAM,number
	CLR number	; a smazeme obsah
bit_19:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_19
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_20_pre
	BLD number,0
	INC data
bit_19_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_19_wait_for_end



bit_20_pre:	; Analogie s informaci o synchronizaci
	STS trigger_INT_EXT_SRAM,number
	CLR number
bit_20:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_20
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_21_pre
	BLD number,0
	INC data
bit_20_wait_for_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_20_wait_for_end

bit_21_pre:
	STS AC_DC_coupling_SRAM,number
	CLR number

  ; Paritni bit
bit_parity_pre:
;	CLR number	; Jiz vykonanno

bit_parity:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_parity
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_parity_2
	BLD number,0	; zapiseme na LSB
bit_parity_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_parity_end

  ; Invertovany paritni bit
bit_parity_2:
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_parity_2
	CALL wait_3us
	SBIS control_port_ADC_in,keyboard_serial_data
	RJMP bit_end
	BLD number,1	; zapiseme na LSB+1
bit_parity_2_end:
	SBIC control_port_ADC_in,keyboard_serial_data
	RJMP bit_parity_2_end

bit_end:
  ; ted se musi provest kontrola parity

	SBRC number,0	; zkontrolujeme prvni paritni bit...
	RJMP parity_bit_is_1	; pokud bude "1"
  ; Pokud bude "0" pokracuje se dal
parity_bit_is_0:
  ; Nyni vime, ze prvni paritni bit je "0". Proto musi byt i nami vypocitany
  ; (ulozeny v "data") take "0", jinak se jedna o chybu
	SBRC data,0	; test na LSB v data. Skok kdyz LSB=0
	RJMP start_bit_error
	RJMP test_parity_bit_2

parity_bit_is_1:
  ; to same jako vyse, ale nyni musi byt v "1"
	SBRS data,0
	RJMP start_bit_error
  ;	RJMP test_parity_bit_2

test_parity_bit_2:
  ; tady musi byt ulozena hodnota naopak invertovana od vypocitane
	SBRC number,1	; zkontrolujeme tedy ulozeny paritni bit
	RJMP parity_bit2_is_1
parity_bit2_is_0:
	SBRS data,0
	RJMP start_bit_error
	RJMP parity_OK

parity_bit2_is_1:
	SBRC data,0
	RJMP start_bit_error
  ;	RJMP parity_OK


parity_OK:
  ; V pripade uspesneho prenosu dat si zalohujeme "range" a "trigger_INT_EXT"
	LDS tmp,range_SRAM	 ; stahneme si data do "tmp"
	STS range_backup_SRAM,tmp	; a zapiseme do SRAM (zalozni)

	LDS tmp,trigger_INT_EXT_SRAM
	STS trigger_INT_EXT_backup_SRAM,tmp	; to same, ale pro jinou
  ; promennou

	LDS tmp,AC_DC_coupling_SRAM
	STS AC_DC_coupling_backup_SRAM,tmp	; opet, to same, ale pro jinou
  ; promennou

	RET	; navrat z podprogramu

start_bit_error:	; teoreticky by tento stav ani nemel nastat, ale jenom
  ; pro jistotu...(treba pokud nechate kabel "v lufte" tak nastane tento stav)
	CBI control_port_ADC,main_ready_to_read_from_keyboard	; nastavime
  ; priznak pripravenosti na "nepripraven" -> "keyboard_uC" nebude vysilat
  ; dalsi sekvenci dat

  ; Dal je mozne, ze data jsou poskozena. Proto se prepisi samymi nulami
	CLR tmp	; smazeme registr tmp. Data z neho budeme kopirovat dale
	STS keyboard_L_SRAM,tmp
	STS keyboard_H_SRAM,tmp
  ; pripadne obnovime posledni zalohu
	LDS tmp,range_backup_SRAM
	STS range_backup_SRAM,tmp

	LDS tmp,trigger_INT_EXT_backup_SRAM
	STS trigger_INT_EXT_SRAM,tmp

	LDS tmp,AC_DC_coupling_backup_SRAM
	STS AC_DC_coupling_SRAM,tmp

  ; Nyni si do SRAM ulozime informaci o tom, ze nastala chyba prenosu
  ; nejdrive si zkontrolujeme, zda neni chyb vice nez 250. To by pak znamenalo
  ; spatny spoj mezi klavesnici a main_uc. V tom pripade se jiz dal pocitat
  ; nebude a ve statistikach se zobrazi "!!!" (kriticke)
	LDS tmp,start_bit_errors_SRAM	; nahrani hodnot z SRAM
	CPI tmp,250	; kontrola, zda neni rovno 250
	BREQ start_bit_error_end	; pokud ano, pak skok
	INC tmp			; V opacnem pripade zvyseni o 1 (plus dalsi
  ; chyba)
	STS start_bit_errors_SRAM,tmp	; a zapsani zpet do SRAM

start_bit_error_end:
	CALL wait_10ms	; pockame az se pripadne "spatna" sekvence impulzu
  ; prenese
	RJMP main_keyboard_ready	; a vse znova (kontrola)

; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*

proceed_keyboard_Osc_mode:


	LDS time3,keyboard_L_SRAM	; stahneme si data z SRAM z adresy
  ; keyboard_L_SRAM do registru "time3" - nebudeme pouzivat zadne zpozdeni,
  ; takze je "bezpecne" pouzivat tento registr (zadny podprogram tuto
  ; hodnotu neprepise - time3 kvůli tomu, že pro vykreslování znaků se používá
  ; registr time1 !


	SBRC time3,run_stop	; Kontrola RunStop - 0 -> skok
	RCALL pressed_run_stop
  ; Tento prikaz musi byt pred testovanim na stisklou klavesu. Tento
  ; podprogram "nevedomky" (diky spusteni dalsiho podprogramu
  ; "recive_data_keyboard" se bit T prepise!)

	SBRC time3,plus_time_base		; kontrola, zda nebylo stisknuto
  ; tlacitko [+Time base]
	RCALL pressed_plus_time_base	; pokud bylo stitsknuto, pak se spusti
  ; definovany podprogram, ktery provede nezbytne operace

	SBRC time3,minus_time_base	; tentokrat kontrola pro [-Time base]
	RCALL pressed_minus_time_base

	SBRC time3,arrow_left	; <--
	RCALL pressed_arrow_left

	SBRC time3,arrow_right	; -->
	RCALL pressed_arrow_right

	PUSH time3


  ; Dalsi sada tlacitek
	LDS time3,keyboard_H_SRAM
	SBRC time3,A_button	; "A"
	RCALL Single_mode_activate

	SBRC time3,B_button	; "B"
	RCALL HW_trigger_rise_edge_enabled

	SBRC time3,C_button	; "C"
	RCALL HW_trigger_fall_edge_enabled

	SBRC time3,D_button	; "D"
	RCALL HW_trigger_continuous


	POP time3

	CLT	; nastavime T bit na "0" - sem se uklada (ted) informace
	SBRC time3,menu		; MENU
	SET	; nastavi T bit do "1" -> znamena, ze chceme do rezimu "menu"


	RET

; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
pressed_run_stop:	; freeze
  ; Pozastavi vykreslovani na displeji. Musi se znovu pockat na stisknuti
  ; "run_stop" tlacitka
	CLT	; nastavime data v T registru - barva písma (protoze
  ; podprogram recive_data_keyboard si nastaví T bit "nahodne")
	RCALL Show_time_base	; Podprogram v "osc_info.asm" - casova zakl.
	RCALL Show_voltage_range	; take v "osc_info.asm" Ukazuje
  ; aktualni rozsah
	CALL show_X_sync	; Ukaze se i info o synchronizaci
  ; Vyse vypsane informace v tomto modu nepotrebujeme aktualizovat. Proto se
  ; vykresli jednou a pak uz ne (pokud nebudou smazany... viz. nize)
	CALL show_coupling
	CALL show_stop

  ; Namerene hodnoty
  ; Z nasbiranych vzorku vypocita Vpp, periodu atd...
;	CALL Samples_calc	; Provede vypocty potrebne pro zobrazeni Vpp,
  ; frekvence a dalsich	; Neni potreba, jiz se provedlo
	CALL Show_samples_results	; A zobrazi provedene vypocty


pressed_run_stop_1:
  ; Při pozastavení se "nedostaneme" dál, dokud nestiskneme znovu tlačítko R/S
  ; takže by se na displeji neobjevily tyto informace. Dal muzeme vyuzit
  ; funkce posuvu vlevo a vpravo. Viz. nize


	LDS tmp,range_SRAM
	STS range_backup_SRAM,tmp

	LDS tmp,trigger_INT_EXT_SRAM
	STS trigger_INT_EXT_backup_SRAM,tmp

	LDS tmp,AC_DC_coupling_SRAM
	STS AC_DC_coupling_backup_SRAM,tmp
  ; Pokud je osciloskop v modu Stop, pak nechceme, aby se aktualizoval stav
  ; rozsahu a informace o interni/externi synchronizaci -> proto se ulozi do
  ; extra promenne a pak se zapise zpatky

	RCALL recive_data_keyboard	; zjistime si stavy na klavesnici
  
  ; A obnovime ulozena data...
	LDS tmp,range_backup_SRAM
	STS range_SRAM,tmp

	LDS tmp,trigger_INT_EXT_backup_SRAM
	STS trigger_INT_EXT_SRAM,tmp

	LDS tmp,AC_DC_coupling_backup_SRAM
	STS AC_DC_coupling_SRAM,tmp
  ; Hotovo. Ted si zjistime co vlastne uzivatel chce

	LDS CNT,keyboard_L_SRAM		; nahrajeme je do registru "CNT"
	OR time3,CNT		; Pokud stitkneme behem pozastaveni nejake
  ; tlacitko, prida se informace o jeho stisknuti

	SBRC CNT,arrow_left	; kontrola, zda nebylo stisknuto "<-"
	RJMP pressed_run_stop_AL

	SBRC CNT,arrow_right	; "->"
	RJMP pressed_run_stop_AR

	SBRS CNT,run_stop	; a otestujeme, zda bylo stisknuto tlacitko
  ; R/S
	RJMP pressed_run_stop_1	; pokud ne, znova nacteme data z klavesnice
  ; a znova testujeme, zda bylo tlacitko stisknuto
	RET	; pokud bylo tlacitko stisknuto, ukoncime tento podprogram
  ; a bude se dal vykreslovat
; *-----------------------------------------*
pressed_run_stop_AL:
	RCALL pressed_arrow_left
	RJMP pressed_run_stop_A_end
pressed_run_stop_AR:
	RCALL pressed_arrow_right
pressed_run_stop_A_end:
	CALL Draw_points
  ; Musi se znovu vykreslit. Draw_points vse smaze
	RCALL Show_time_base
	RCALL Show_voltage_range
	CALL show_X_sync
	CALL show_coupling

  ; Namerene hodnoty
  ; Z nasbiranych vzorku vypocita Vpp, periodu atd...
;	CALL Samples_calc	; Provede vypocty potrebne pro zobrazeni Vpp,
  ; frekvence a dalsich	; Neni potreba, jiz se provedlo
	CALL Show_samples_results	; A zobrazi provedene vypocty

	CALL show_stop

	RJMP pressed_run_stop_1
; *-----------------------------------------*
pressed_plus_time_base:
	LDS tmp,TimeBaseSRAM	; stahneme si data z SRAM
	INC tmp		; zvysime hodnotu o 1
	STS TimeBaseSRAM,tmp	; a zapiseme zpet

  ; Bufferu posleme aktualni data - pokud se zmenila casova zakladna, pak je
  ; nejvyssi cas poslat bufferu aktualni nastaveni ;-)
	CALL reset_buffer_and_send_cfg

	RET	; navrat z podprogramu
; *-----------------------------------------*
pressed_minus_time_base:	; viz. vise. Je to stejne
	LDS tmp,TimeBaseSRAM
	DEC tmp		; snizi hodnotu o 1
	STS TimeBaseSRAM,tmp

  ; Bufferu posleme aktualni data - pokud se zmenila casova zakladna, pak je
  ; nejvyssi cas poslat bufferu aktualni nastaveni ;-)
	CALL reset_buffer_and_send_cfg

	RET
; *-----------------------------------------*
pressed_arrow_left:
	LDS tmp,Samples_offset_SRAM
  ; A kontrola, zda nejsme mimo rozsah
	LDS data,number_of_samples_SRAM	; load Samples_offset_SRAM2data
	SUBI data,100	; Nemuzeme se posunout dal, nez kolik mame dat v SRAM
  ; (max pocet vzorku - bodu na displeji)
	CP data,tmp	; porovname hodnoty aktualni offset a maximalni
  ; povoleny
	BRNE pressed_arrow_left_OK
	RET	; pokud si cisla budou rovna -> dal uz citat nemuzeme ->
  ; -> vyskocime z podprogramu (hodnota se nezvysi o 1)
pressed_arrow_left_OK:
	INC tmp	; +1
	STS Samples_offset_SRAM,tmp
	RET
; *-----------------------------------------*
pressed_arrow_right:
	LDS tmp,Samples_offset_SRAM
  ; Zde staci akoraz zkontrolovat, zda neni hodnota Samples_offset_SRAM nula,
  ; protoze pri odecteni jednicky by se $00 zmenilo na $FF -> problem
	CPI tmp,0
	BRNE pressed_arrow_right_OK	 ;pokud to nebude nula,pak skok
  ; na pressed_arrow_right_OK
	RET	; jinak vyskoci z podprogramu a nic se neodecte

pressed_arrow_right_OK:
	DEC tmp ; -1
	STS Samples_offset_SRAM,tmp

	RET
; *-----------------------------------------*
Single_mode_activate:	; Nastavi trigger na "single mode"

  ; Nastaveni LCD - je vhodne provest nyni, protoze se vykonava stejny kod pro
  ; obe moznosti
	CALL Clear_display

	LDI data,3
	CALL select_line_both	; Vyber radku
	CLT	; Nastaveni barvy pisma
	LDI data,5	; Xova souradnice



	LDS tmp,Trigger_single_mode_status_SRAM
	CPI tmp,0	; Porovnani s nulou
	BRNE S_m_a_deactivate	; Pokud neni nula, pak uz byl nastaven ->
  ; -> vypnout

  ; Ale pokud uz byl nastaven drive, pak ho vypne
	LDI tmp,1
	STS Trigger_single_mode_status_SRAM,tmp	; Zapise do pameti 1

  ; A zobrazi prislusny text
	LDI ZH,HIGH(2*OI_Single_mode_enabled)
	LDI ZL,LOW(2*OI_Single_mode_enabled)	; Nastaveni textu

	CALL write_text_from_DB	; Vypise text

	RET
S_m_a_deactivate:
	CLR tmp
	STS Trigger_single_mode_status_SRAM,tmp	; Ulozi hodnotu nula -> vypne
  ; single mod

  ; A zobrazi prislusny text
	LDI ZH,HIGH(2*OI_Single_mode_disabled)
	LDI ZL,LOW(2*OI_Single_mode_disabled)	; Nastaveni textu

	CALL write_text_from_DB	; Vypise text

	RET
; *-----------------------------------------*
HW_trigger_rise_edge_enabled:	; Nastavi trigger jako HW a na nabeznou hranu
	LDI tmp,0
	STS trigger_mode_SRAM,tmp	; Nastavi trigger softwarove

  ; Nastavni LCD
	LDI ZH,HIGH(2*OI_HW_trigger_raise_edge_enabled)
	LDI ZL,LOW(2*OI_HW_trigger_raise_edge_enabled)

	CALL Clear_display

	LDI data,3
	CALL select_line_both	; Vyber radku
	CLT	; Nastaveni barvy pisma
	LDI data,5	; Xova souradnice

	CALL write_text_from_DB

	CALL reset_buffer_and_send_cfg	; Restartuje buffer

	RET
; *-----------------------------------------*
HW_trigger_fall_edge_enabled:
	LDI tmp,1
	STS trigger_mode_SRAM,tmp	; Nastavi trigger softwarove

  ; Nastavni LCD
	LDI ZH,HIGH(2*OI_HW_trigger_fall_edge_enabled)
	LDI ZL,LOW(2*OI_HW_trigger_fall_edge_enabled)

	CALL Clear_display

	LDI data,3
	CALL select_line_both	; Vyber radku
	CLT	; Nastaveni barvy pisma
	LDI data,5	; Xova souradnice

	CALL write_text_from_DB

	CALL reset_buffer_and_send_cfg	; Restartuje buffer

	RET
; *-----------------------------------------*
HW_trigger_continuous:
	LDI tmp,2
	STS trigger_mode_SRAM,tmp	; Nastavi trigger softwarove

  ; Nastavni LCD
	LDI ZH,HIGH(2*OI_HW_trigger_continuous_enabled)
	LDI ZL,LOW(2*OI_HW_trigger_continuous_enabled)

	CALL Clear_display

	LDI data,3
	CALL select_line_both	; Vyber radku
	CLT	; Nastaveni barvy pisma
	LDI data,5	; Xova souradnice

	CALL write_text_from_DB

	CALL reset_buffer_and_send_cfg	; Restartuje buffer

	CALL wait_500ms	; Aby se vubec neco zobrazilo :) Trigger se totiz
  ; spusti prakticky ihned, takze main_uC prijme okamzite data a text se smaze

	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
Wait_for_any_key:

	CALL recive_data_keyboard

	LDS data,keyboard_L_SRAM
	LDS tmp,keyboard_H_SRAM
	OR tmp,data

	CPI tmp,0
	BREQ Wait_for_any_key

	RET
