;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;; C O N F I G ;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    include "p12f683.inc"

    __config _MCLRE_OFF & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF & _PWRTE_OFF & _CP_OFF & _CPD_OFF & _WDTE_OFF & _MCLRE_OFF & _BOREN_OFF & _FOSC_INTOSCIO
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;; C O N S T A N T S ;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#define SERVO_DVIRKA	GPIO, GP5		;dvirka
#define SERVO_PACKA		GPIO, GP4		;packa
#define SPINAC 			GPIO, GP3		;prepinac

#define TRUE				.1
#define	FALSE				.0

#define DVIRKA_FULL_ANGLE	.15		;dvirka naplno
#define DVIRKA_OCI_ANGLE	.40		;jen koukaji oci
#define DVIRKA_INIT_ANGLE	.90		;zakladni

#define PACKA_INIT_ANGLE	.175	;zakladni
#define PACKA_END_ANGLE		.30		;vypni
#define PACKA_PRED_ANGLE	.50		;pred vypinac
#define PACKA_TESNE_ANGLE	.30		;tesne pred vypinac
#define PACKA_HALF_ANGLE	.90		;na pul cesty
#define PACKA_MEZI_ANGLE	.70		

#define MOTOR_DVIRKA		.0
#define MOTOR_PACKA			.1

#define DIRECTION_FORWARD	.0
#define DIRECTION_BACKWARD	.1

;WT588D   
#define S_SDA		GPIO, GP0;
#define S_CS		GPIO, GP1;
#define S_SCK		GPIO, GP2;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;; K O N S T A N T Y ;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;WT588D
SOUND_00_SNORE		equ .0
SOUND_01_YAWN		equ .1
SOUND_02_SIREN		equ .2
SOUND_03_PILA		equ .3
SOUND_04_LAUGH		equ .4		;laugh maniacal
SOUND_05_KNOCK		equ .5		;knock knock
SOUND_06_CHILDREN	equ .6		;children hustle
SOUND_07_CLARINET	equ .7
SOUND_08_ROBOT		equ .8		;robot alarm
SOUND_09_PARTY		equ .9		;party horn
SOUND_0A_FACTORY	equ .10		;factory whistle
SOUND_0B_CAR		equ .11		;car horn
SOUND_0C_ROOSTER	equ .12	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;; V A R I A B L E S ;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    cblock 0x20
	d1			;registry na countery pro 1s delay
	d2
	d3
	w_temp
	status_temp
	pulseCount
	numMilis	;WT588D
	numSecs		;WT588D
	tmp			;WT588D
	finished
	canExit			;Flag pro ukonceni behu interruptu
	numHalfSecsWait	;Kolik 0.5s cekat
	i
	j
	scenar			;Ktery scenar?
	hiByte
	loByte
	sweepFrom
	sweepTo
	sweepStep	
	sweepDirection	;false=ascending,true=descending
	sweepNotNeeded  ;Nekdy staci drzet jen dvirka
	whichHold		;ktery motor drzi pozici? (0=1., 1=2.)
	holdAngle		;drzime uhel
	tmpStep			;pro uchovani stepu v PackaTamZpet
    endc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;; M A C R O S ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BANK0 macro
;{

    bcf STATUS, RP0 ;Select memory bank 0
	errorlevel    	-302
    endm
    
BANK1 macro
    bsf STATUS, RP0 ;Select memory bank 1
	errorlevel    	+302
    endm    
;}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;; R E S E T  V E C T O R S ;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	org 0x00
	goto MAIN
	
	org 0x04
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

INTERRUPT
;{
	movwf w_temp 		;copy W to temp register, could be in either bank
	swapf STATUS,w 		;swap status to be saved into W
	movwf status_temp 	;save status to bank 0 register

	bcf PIR1, TMR1IF	;vycisti Interrupt flag pro Timer1

	;orig
	movlw 0xB1
	movwf TMR1H
	movlw 0xEA
	movwf TMR1L

HandleFirstServo
	btfsc sweepNotNeeded, 0
	goto HandleSecondServo
		
;Obsluha 1. motoru
Sw0	btfsc whichHold, 0	;Ktery motor drzim? 5cycles ->servosSet
	goto setServo1
	bsf SERVO_PACKA
	goto servosSet
setServo1
	bsf SERVO_DVIRKA
	nop					;Vypln, at mam konstantni cas pro obe if vetve
servosSet
	call Wait0.490ms		;prictej postupne 11us (0.011ms)
	movf sweepFrom, w
	movf sweepFrom, f		;=0?
	btfsc STATUS, Z
	goto SwZ	
	movwf i
Sw1 nop					;11us
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	decfsz i, f
	goto Sw1
	
SwZ	btfsc whichHold, 0	;Ktery motor drzim? 5cycles ->servosUnset
	goto unsetServo1
	bcf SERVO_PACKA
	goto servoUnset
unsetServo1
	bcf SERVO_DVIRKA
	nop					;Vypln, at mam konstantni cas pro obe if vetve
servoUnset

	movlw .50				
	addwf sweepFrom, f
	addwf sweepTo, f

	movf sweepStep, w
	btfss sweepDirection, 0	;pricitej/odecitej stupne
	goto Swi
Swd	subwf sweepFrom, f		;dekrement
	movf sweepTo, w
	subwf sweepFrom, w		;sweepFrom - sweepTo < 0?
	goto Swc				
Swi	addwf sweepFrom, f		;inkrement
	movf sweepFrom, w
	subwf sweepTo, w		;sweepTo - sweepFrom < 0?
	goto Swc				;filler, takty
Swc	movlw FALSE
	btfsc STATUS, C
	goto Swf
	movlw TRUE	
Swf movwf finished



HandleSecondServo

;Obsluha 2. motoru (hold)
Hw0	btfsc whichHold, 0		;Ktery motor drzim? 5cycles ->servosSet
	goto holdServo2
	bsf SERVO_DVIRKA
	goto servoHeld
holdServo2
	bsf SERVO_PACKA
	nop						;Vypln, at mam konstantni cas pro obe if vetve
servoHeld
	call Wait0.490ms		;prictej postupne 11us (0.011ms)
	movf holdAngle, w
	movf holdAngle, f		;=0?
	btfsc STATUS, Z
	goto HwZ	
Hwc	movwf i
Hw1 nop					;11us
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	decfsz i, f
	goto Hw1
	
HwZ	btfsc whichHold, 0	;Ktery motor drzim? 5cycles ->servosUnset
	goto unholdServo2
	bcf SERVO_DVIRKA
	goto servoUnHeld
unholdServo2
	bcf SERVO_PACKA
	nop					;Vypln, at mam konstantni cas pro obe if vetve
servoUnHeld

	movlw .50
	subwf sweepFrom, f
	subwf sweepTo, f

	btfss canExit, 0	;"Vypni" interrupt zvenku
	goto skip
	movlw TRUE
	movwf finished

skip
	swapf status_temp,w	;swap status_temp register into W, sets bank to original state
	movwf STATUS 		;move W into STATUS register
	swapf w_temp,f 		;swap w_temp
	swapf w_temp,w 		;swap w_temp into W	
	retfie

;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;; M A I N ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MAIN
;{
	BANK1
	movlw b'01100100'
	movwf OSCCON		;4MHZ internal
	BANK0

	;Inicializace vystupnich GPIO	
	BANK1	
	clrf ANSEL ;digital I/O
	movlw b'11001000'   ;Vse output, GP3 input
	movwf TRISIO    
	BANK0 	
	movlw 0x07 
	movwf CMCON0 ;digital I/O
	clrf GPIO 	
	bcf SERVO_DVIRKA
	BANK1
	movlw b'11001000'   ;Interni oscilator, prescaler 1:1 (PSA = 1)
	movwf OPTION_REG 
	BANK0

	movlw SOUND_00_SNORE
	call DoSoundFX
	call SetLoopSound

  	call InterruptOff
	call DvirkaInitPos
	call PackaInitPos

	call GetNumStartScenar	;Pockej na spinac a init scenar
;}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;; M A I N   L O O P ;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LOOP	
;{		
	call RunScenar

	movlw SOUND_00_SNORE
	call DoSoundFX
	call SetLoopSound

waitKey	
	btfss SPINAC			;Pockej na spinac
	goto waitKey
	call GetNextScenar
	
	goto LOOP

;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;; S C E N A R E ;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Pust scenar podle promenne 'scenar'
RunScenar	
;{
		movlw HIGH jmpTbl
		movwf PCLATH
		movf scenar, w
		addwf PCL, f

jmpTbl	goto Scenar0
		goto Scenar1
		goto Scenar2
		goto Scenar3
		goto Scenar4
		goto Scenar5
		goto Scenar6
		goto Scenar7
		goto Scenar8
		goto Scenar9
		goto Scenar10

;}
;Vyber nahodny scenar z TMR1L, se kterym budeme zacinat.
;Pote se posunujeme +1 a na konci seznamu zpet na 0ty scenar.
GetNumStartScenar
;{
GetL
	btfss SPINAC
	goto GetL
	movf TMR0, w
	andlw b'00000111'	;0-7
	movwf scenar
	return
;}

GetNextScenar
;{
	movf scenar, w
	addlw .246		;>10?
	btfss STATUS, C
	goto lessNine
	clrf scenar
	return
lessNine
	incf scenar, f
	return
;}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar0
;{
;1. Dvirka rychle nahoru
;2. Pockej 1s, packa tam 
;3. Packa rychle zpet
;4. Dvirka rychle dolu
		
		movlw SOUND_02_SIREN
		call DoSoundFX
		movlw .2
		call WaitSecs
		
		;1
		call DvirkaFullFast

		;Pockej 1s
		movlw .2
		call DvirkaHold
		
		;2, 3
		movlw .3					;Rychle	
		call PackaTamZpet
		
		;Pockej 0.5secs
		movlw .1
		call DvirkaHold

		;4
		call DvirkaInitPos			;Zavri dvirka
		return
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar1
;{
;1. Klapani 3x nahoru a dolu
;2. Dvirka pomalu nahoru, pak pockej 1s
;3. Packa rychle prepne
;4. Packa rychle zpet
;5. Dvirka rychle dolu

		movlw SOUND_03_PILA
		call DoSoundFX
		movlw .2
		call WaitSecs
	
		;1
		call DvirkaKlapRychle

		call Wait1s
	
		;2
		movlw .1
		call DvirkaFullOpen
		
		;Pockej 1sec
		movlw .2		
		call DvirkaHold

		;3, 4
		movlw .3		
		call PackaTamZpet
		
		;Pockej 1secs
		movlw .2
		call DvirkaHold

		;5
		movlw .3
		call DvirkaDown

		return
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar2
;{
;1. Stredne rychla dvirka nahoru
;2. Packa stredne rychly vyjezd tesne pred prepinac
;3. Packa dojede a vypne
;4. Packa zajede stredne rychle zpet
;5. Stredne rychla dvirka dolu

		movlw SOUND_04_LAUGH
		call DoSoundFX
		movlw .2
		call WaitSecs	

		;1
		movlw .2
		call DvirkaFullOpen
	
		;Pockej 1.5secs
		movlw .3
		call DvirkaHold

		;2
		movlw .2
		call PackaPredPrepinac
		
		;cekej 2sec
		movlw .4
		call DvirkaHold

		;3
		call PackaInitVars

		movlw PACKA_PRED_ANGLE
		movwf sweepFrom
		movlw PACKA_END_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa prepina rychle
		call WaitServo

		;4
		movlw PACKA_END_ANGLE
		movwf sweepFrom
		movlw PACKA_INIT_ANGLE
		movwf sweepTo
		movlw .2
		movwf sweepStep
		call SweepForward			;Packa zpet, stredne rychle

		;Pockej 0.5secs
		movlw .1
		call DvirkaHold

		;5
		movlw .2
		call DvirkaDown	
		
		return
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar3
;{
;1. Rychle otevreni dvirek full
;2. Packa rychle tesne pred prepinac
;3. Pockej 2s
;4. Packa stredne rychle prepne
;5. Pockej 2s
;6. Packa stredne rychle vrati zpet
;7. Dvirka se stredne rychle zavrou
;8.	Pockej 1s
;9. Dvirka se rychle otevrou
;A. Pockej 3s
;B. Dvirka se rychle zavrou

		movlw SOUND_05_KNOCK
		call DoSoundFX
		movlw .2
		call WaitSecs	


		;1
		call DvirkaFullFast
		
		;2
		movlw .4
		call PackaTesnePredPrepinac

		;3
		movlw .4
		call DvirkaHold	
		
		;4
		call PackaInitVars
		
		movlw PACKA_TESNE_ANGLE
		movwf sweepFrom
		movlw PACKA_END_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa prepne prepinac		
		call WaitServo
		
		;5
		movlw .4
		call DvirkaHold
		
		;6
		movlw .2
		movwf tmpStep
		call PackaZpet
		
		;7
		movlw .2
		call DvirkaDown
		
		;8
		call Wait1s
		
		;9 
		call DvirkaFullFast
		
		;A
		;Pockej 1s
		movlw .6
		call DvirkaHold
		
		;B
		call DvirkaInitPos			;Zavri dvirka
		return
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar4
;{
;1. Rychle otevreni dvirek full
;2. 2s pauza, rychle zavreni dvirek
;3. 2s pauza; rychle otevreni dvirek full
;4. Packa rychle tam
;5. Packa rychle zpet
;6. Dvirka rychle dolu

		movlw SOUND_06_CHILDREN
		call DoSoundFX
		movlw .2
		call WaitSecs	

		;1
		call DvirkaFullFast

		;2
		;Pockej 2s
		movlw .4
		call DvirkaHold
		call DvirkaInitPos			;Zavri dvirka
		
		;3
		call Wait1s
		call Wait1s				
		call DvirkaFullFast
		
		;4, 5
		movlw .4
		call PackaTamZpet

		;5
		movlw .1
		call DvirkaDown
		return
		
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar5
;{
;1. Rychle otevreni dvirek full
;2. Packa rychle tam
;3. Packa rychle zpet
;4. Rychle zavreni dvirek
;5. Pauza 2secs
;6. Rychle otevreni dvirek full
;7. Pauza 3secs
;8. Rychle zavreni dvirek

		movlw SOUND_07_CLARINET
		call DoSoundFX
		movlw .2
		call WaitSecs	

		;1
		call DvirkaFullFast

		;2, 3
		movlw .4
		call PackaTamZpet

		;4
		call DvirkaInitPos			;Zavri dvirka
		
		;5
		call Wait1s
		call Wait1s
		
		;6
		call DvirkaFullFast
		
		;7
		movlw .6
		call DvirkaHold
		
		;8
		movlw .2
		call DvirkaDown
		return
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar6
;{
;1. Rychle otevreni dvirek full
;2. Packa vyjede rychle a vypne a drzi 2s
;3. Packa 3x zajede trochu zpet a zase kousek tam
;4. Packa zajede rychle zpet, pauza 2secs
;5. Dvirka rychle zavreni, pauza 2secs
;6. Dvirka rychle otevreni full
;7. Pauza 2secs (plysak kouka)
;8. Dvirka rychle zavreni


		movlw SOUND_08_ROBOT
		call DoSoundFX
		movlw .2
		call WaitSecs

		;1
		call DvirkaFullFast

		;2
		movlw .4
		call PackaTam

		
		movlw .4
		call DvirkaHold				;Drz packu na miste 2s
		
		;3
		call PackaInitVars

		movlw .3
		movwf j
Sc6l		
		movlw PACKA_END_ANGLE
		movwf sweepFrom
		movlw PACKA_MEZI_ANGLE
		movwf sweepTo
		movlw .2
		movwf sweepStep
		call SweepForward			;Packa zpet
		
		movlw PACKA_MEZI_ANGLE
		movwf sweepFrom
		movlw PACKA_INIT_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa rychle zpet nahoru
		call WaitServo
		decfsz j, f
		goto Sc6l

		;4
		movlw .4
		movwf tmpStep
		call PackaZpet		
		
		movlw .4
		call DvirkaHold				;Drz packu na miste 2s

		;5
		call DvirkaInitPos			;Zavri dvirka
		
		call Wait1s					;Cekej 2s
		call Wait1s
		
		;6
		call DvirkaFullFast
		
		;7
		movlw .4					;Cekej otevrene dvirka 2s
		call DvirkaHold
		
		;8
		call DvirkaInitPos			;Zavri dvirka rychle
		return
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar7
;{
;1. Dvirka pomale otevreni full
;2. Packa pomalu vyjede tesne pred prepinac
;3. 1secs, packa prepne prepinac
;4. Packa zajede rychle zpet
;5. Dvirka se rychle zavrou.


		movlw SOUND_09_PARTY
		call DoSoundFX
		movlw .2
		call WaitSecs

		;1
		movlw .1
		call DvirkaFullOpen
		
		;Pockej 1.5secs
		movlw .3
		call DvirkaHold

		;2
		movlw .2
		call PackaPredPrepinac
		
		;3
		;Pockej 1sec
		movlw .2
		call DvirkaHold		
		
		call PackaInitVars
		
		movlw PACKA_PRED_ANGLE
		movwf sweepFrom
		movlw PACKA_END_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa prepne prepinac		
		
		;4
		movlw .4
		movwf tmpStep
		call PackaZpet
		
		;5
		call DvirkaInitPos			;Zavri dvirka rychle
		return
		
		
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar8
;{
;1. Dvirka rychle otevreni full
;2. Packa stredne rychle do poloviny
;3. Packa rychle tesne pred prepinac
;4. Packa do poloviny a opakuj 3. celkem 3x
;5. Pockej 2s, packa rychle prepne prepinac.
;6. Packa rychle zajede zpet.
;7. Dvirka se rychle zavrou.


		movlw SOUND_0A_FACTORY
		call DoSoundFX
		movlw .2
		call WaitSecs


		;1
		call DvirkaFullFast
		
		;2
		call PackaInitVars

		movlw PACKA_INIT_ANGLE
		movwf sweepFrom
		movlw PACKA_HALF_ANGLE
		movwf sweepTo
		movlw .2
		movwf sweepStep
		call SweepBackward			;Packa do poloviny cesty
		
		movlw .2					;Pocka 1s
		call DvirkaHold
		
		;3
		call PackaInitVars

		;4
		movlw .3
		movwf j
		
Sc8l
		movlw PACKA_HALF_ANGLE
		movwf sweepFrom
		movlw PACKA_PRED_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa tesne pred prepinac
		
		movlw PACKA_PRED_ANGLE
		movwf sweepFrom
		movlw PACKA_HALF_ANGLE
		movwf sweepTo
		movlw .2
		movwf sweepStep
		call SweepForward			;Packa do poloviny zpet
		decfsz j, f
		goto Sc8l
		
		;5
		movlw .4					;Pocka 2s
		call DvirkaHold

		call PackaInitVars

		movlw PACKA_HALF_ANGLE
		movwf sweepFrom
		movlw PACKA_END_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa vypne prepinac
		
		;6
		movlw .4
		movwf tmpStep
		call PackaZpet
			
		;7
		call Wait1s
		
		movlw .3
		call DvirkaDown
		return
		
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar9
;{
;1. Dvirka se pomalu otevrou full
;2. Packa rychle prepne prepinac
;3. Packa rychle zpet.
;4. Dvirka pomalu zpet do vysky oci, pauza 2s
;5. Dvirka rychle otevrou full
;6. Pauza 3s
;7. Dvirka rychle zavrou.

		movlw SOUND_0B_CAR
		call DoSoundFX
		movlw .2
		call WaitSecs

		;1
		movlw .1
		call DvirkaFullOpen
		
		movlw .3					;Pockej 1.5s
		call DvirkaHold
		
		;2, 3
		movlw .4
		call PackaTamZpet
			
		movlw .4					;Pockej 2s
		call DvirkaHold
		
		;4
		call DvirkaInitVars

		movlw DVIRKA_FULL_ANGLE
		movwf sweepFrom
		movlw DVIRKA_OCI_ANGLE
		movwf sweepTo
		movlw .2
		movwf sweepStep
		call SweepBackward			;Dvirka do vysky oci
		
		movlw .4					;2secs ve vysce oci
		movwf numHalfSecsWait
		movlw DVIRKA_OCI_ANGLE
		call DvirkaHoldOci
		
		;5
		call DvirkaFullFast			;Otevri dvirka rychle full
		
		;6
		movlw .6					;Pockej 3secs
		call DvirkaHold
		
		;7
		call DvirkaInitPos			;Zavri dvirka rychle
		return
		
;}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Scenar10
;{
;1. Dvirka se pomalu otevrou na vysku oci
;2. Pauza 2s
;3. Dvirka se rychle otevrou full
;4. Packa rychle tesne pred prepinac
;5. Pauza 3s
;6. Packa prepne rychle prepinac
;7. Packa stredne rychle zpet
;8. Dvirka rychle zavrou
;9. Pauza 2s
;A. Dvirka rychle otevreni full
;B. Packa rychle prepnuti a drzi prepnute 2s
;C. Packa rychle zpet
;D. Dvirka stredne rychle dolu 

		movlw SOUND_0C_ROOSTER
		call DoSoundFX
		movlw .2
		call WaitSecs

		;1
		movlw .1
		call DvirkaOciOpen
			
		;2
		movlw .4					;Pockej 2secs
		movwf numHalfSecsWait
		movlw DVIRKA_OCI_ANGLE
		call DvirkaHoldOci
		
		;3
		call DvirkaFullFast			;Otevri dvirka rychle full
		
		;4
		movlw .4
		call PackaPredPrepinac
					
		;5
		movlw .6					;Pauza 3secs
		call DvirkaHold				
		
		;6
		call PackaInitVars

		movlw PACKA_PRED_ANGLE
		movwf sweepFrom
		movlw PACKA_END_ANGLE
		movwf sweepTo
		movlw .4
		movwf sweepStep
		call SweepBackward			;Packa rychle prepne prepinac
		
		;7
		movlw .2
		movwf tmpStep
		call PackaZpet
			
		;8
		call DvirkaInitPos			;Zavri dvirka rychle		

		;9
		call Wait1s					;Pauza 2secs
		call Wait1s	
		
		;A
		call DvirkaFullFast	
		
		;B
		movlw .4
		call PackaTam
		
		movlw .4					;Pockej 2secs, u prepinace
		call DvirkaHold		
		
		;C
		movlw .4
		movwf tmpStep
		call PackaZpet
		
		call Wait1s	
		
		;D
		movlw .2
		call DvirkaDown			;Zavri dvirka rychle
		return		
					
;}



;*************************************************************
; ***************** H E L P E R   R U T I N Y ****************
;*************************************************************

InterruptOn
;{
		BANK1
		bsf PIE1, TMR1IE	;Enables the TMR1 overflow interrupt

		bsf INTCON, PEIE	;Peripheral interrupt pro Timer1
		bsf INTCON, GIE	    ;Povol vsechna preruseni	

		BANK0	
		movlw FALSE
		movwf finished
		movwf canExit	

		movlw 0xFF			;Par cyklu pred turnoverem, aby hned nastalo preruseni
		movwf TMR1H
		movlw 0xE0
		movwf TMR1L
		nop
		nop
		movlw b'00000001'	;TMR1ON = zapni
		movwf T1CON		
		return
;}
InterruptOff
;{
		BANK0	
		clrf T1CON			;TMR1ON = vypni

		BANK1
		bcf PIE1, TMR1IE	;Enables the TMR1 overflow interrupt

		bcf INTCON, PEIE	;Peripheral interrupt pro Timer1
		bcf INTCON, GIE	    ;Povol vsechna preruseni	
		BANK0
		return

;}
;*************************************************************
; ***************** S E R V O   R U T I N Y ******************
;*************************************************************
ServoRutiny
;{
SweepForward
		movlw DIRECTION_FORWARD
		goto SweepXtoY

SweepBackward
		movlw DIRECTION_BACKWARD
		goto SweepXtoY

SweepXtoY
		movwf sweepDirection

		call InterruptOn

SwWait	btfss finished, 0
		goto SwWait

		call InterruptOff
		return
;}
		

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; D V I R K A   R U T I N Y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;		

DvirkaKlapRychle
;{
		movlw .5
		movwf j
DvirkaK	
		call DvirkaOciFast
		call DvirkaInitPos2		
		decfsz j, f
		goto DvirkaK
		return
;}

DvirkaInitPos
;{
	movlw .10
	movwf pulseCount
DvInit
	bsf SERVO_DVIRKA
	call Wait1.5ms
	bcf SERVO_DVIRKA
	call Wait18.5ms
	decfsz pulseCount, f
	goto DvInit
	return
;}

DvirkaInitPos2
;{
	movlw .10
	movwf pulseCount
S65
	bsf SERVO_DVIRKA
	call Wait1.275ms
	bcf SERVO_DVIRKA
	call Wait18.725ms
	decfsz pulseCount, f
	goto S65
	return
;}

DvirkaInitVars
;{
	movlw PACKA_INIT_ANGLE		;Drz uhel pro packu
	movwf holdAngle
	movlw MOTOR_PACKA
	movwf whichHold				;Drz packu, hybej dvirky
	movlw FALSE
	movwf sweepNotNeeded
	return
;}

DvirkaOciFast
;{
	movlw .10
	movwf pulseCount
S50
	bsf SERVO_DVIRKA
	call Wait1.055ms
	bcf SERVO_DVIRKA
	call Wait18.945ms
	decfsz pulseCount, f
	goto S50
	return
;}

DvirkaOciOpen
;{
	movwf sweepStep
	call DvirkaInitVars

	movlw DVIRKA_INIT_ANGLE
	movwf sweepFrom
	movlw DVIRKA_OCI_ANGLE
	movwf sweepTo
	call SweepBackward			;Dvirka pomalu nahoru do oci
	return
;}

DvirkaFullFast
;{
	movlw .30
	movwf pulseCount
S15
	bsf SERVO_DVIRKA
	call Wait0.665ms
	bcf SERVO_DVIRKA
	call Wait19.335ms
	decfsz pulseCount, f
	goto S15
	return
;}	
	
DvirkaFullOpen
;{
	movwf sweepStep
	call DvirkaInitVars

	movlw DVIRKA_INIT_ANGLE
	movwf sweepFrom
	movlw DVIRKA_FULL_ANGLE
	movwf sweepTo
	call SweepBackward			;Dvirka pomalu nahoru	
	return
;}
	
DvirkaDown
;{
	movwf sweepStep
	call DvirkaInitVars

	movlw DVIRKA_FULL_ANGLE
	movwf sweepFrom
	movlw DVIRKA_INIT_ANGLE
	movwf sweepTo
	call SweepForward			;Dvirka pomalu dolu
	return	
;}

DvirkaHold
;{
		movwf numHalfSecsWait
		;Drz dvirka, packu nechej byt (noSweep)
		movlw DVIRKA_FULL_ANGLE		;Drz uhel pro dvirka
DvirkaHoldOci		
		movwf holdAngle
		movlw MOTOR_DVIRKA
		movwf whichHold				;Drz dvirka
		movlw TRUE
		movwf sweepNotNeeded		;Jen drzime dvirka
		call InterruptOn		
		movf numHalfSecsWait, w
		movwf j
DvHl	call Wait0.5s
		decfsz j, f
		goto DvHl
		movlw TRUE					;Cekani hotovo
		movwf canExit
DvHw	btfss finished, 0			;Pockej na dobeh preruseni
		goto DvHw
		call InterruptOff
		movlw FALSE
		movwf canExit
		return
;}		

	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P A C K A   R U T I N Y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

PackaInitPos
;{
	movlw .40
	movwf pulseCount
S175
	bsf SERVO_PACKA
	call Wait2.44ms
	bcf SERVO_PACKA
	call Wait17.56ms
	decfsz pulseCount, f
	goto S175
	return
;}

PackaInitVars
;{
	movlw DVIRKA_FULL_ANGLE		;Drz uhel pro dvirka
	movwf holdAngle
	movlw MOTOR_DVIRKA
	movwf whichHold				;Drz dvirka, hybej packou
	movlw FALSE
	movwf sweepNotNeeded
	return
;}
	
PackaTamZpet
;{
	call PackaTam
	call PackaZpet
	return
;}

PackaTam
;{
	movwf tmpStep
	call PackaInitVars

	movlw PACKA_INIT_ANGLE
	movwf sweepFrom
	movlw PACKA_END_ANGLE
	movwf sweepTo
	movf tmpStep, w
	movwf sweepStep
	call SweepBackward			;Packa ven
	call WaitServo
	return
;}

PackaZpet
;{
	call PackaInitVars

	movlw PACKA_END_ANGLE
	movwf sweepFrom
	movlw PACKA_INIT_ANGLE
	movwf sweepTo
	movf tmpStep, w
	movwf sweepStep
	call SweepForward			;Packa zpet
	call WaitServo
	return
;}
		

PackaPredPrepinac
;{
	movwf sweepStep
	call PackaInitVars

	movlw PACKA_INIT_ANGLE
	movwf sweepFrom
	movlw PACKA_PRED_ANGLE
	movwf sweepTo
	call SweepBackward			;Packa ven pred prepinac
	call WaitServo
	return		
;}

PackaTesnePredPrepinac
;{
	movwf sweepStep
	call PackaInitVars

	movlw PACKA_INIT_ANGLE
	movwf sweepFrom
	movlw PACKA_TESNE_ANGLE
	movwf sweepTo
	call SweepBackward			;Packa ven tesne pred prepinac
	call WaitServo
	return		
;}

;*************************************************************
; ***************** C E K A C I   S M Y C K Y ****************
;*************************************************************

Wait1s		;999990 cycles
;{
	movlw	0x07
	movwf	d1
	movlw	0x2F
	movwf	d2
	movlw	0x03
	movwf	d3
wait1_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	wait1_0			
	goto	$+1		;6 cycles
	goto	$+1
	goto	$+1
	return	;4 cycles (including call)
;}

Wait0.5s	;499994 cycles
;{
	movlw	0x03
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Wait0.5s_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Wait0.5s_0	;2 cycles
	goto	$+1			;4 cycles (including call)
	return
;}

Wait0.490ms ;484 cycles
;{
			
	movlw	0xA1
	movwf	d1
Wait0.490ms_0
	decfsz	d1, f
	goto	Wait0.490ms_0 ;2 cycles
	goto	$+1	;4 cycles (including call)
	return
;}


Wait0.5ms	;496 cycles
;{
	movlw	0xA5
	movwf	d1
Wait0.5ms_0
	decfsz	d1, f
	goto	Wait0.5ms_0	;4 cycles (including call)
	return
;}

Wait0.665ms	;661 cycles
;{
	movlw	0xDC
	movwf	d1
Wait0.665ms_0
	decfsz	d1, f
	goto	Wait0.665ms_0;4 cycles (including call)
	return
;}

Wait1ms		;993 cycles
;{
	movlw	0xC6
	movwf	d1
	movlw	0x01
	movwf	d2
	goto 	WaitCommon
;}

Wait1.055ms	;1048 cycles (50°)
;{
	movlw	0xD1
	movwf	d1
	movlw	0x01
	movwf	d2
	goto	WaitCommon
;}


Wait1.275ms	;1268 cycles
;{
	movlw	0xFD
	movwf	d1
	movlw	0x01
	movwf	d2
	goto	WaitCommon
;}

Wait1.5ms	;1493 cycles
;{
	movlw	0x2A
	movwf	d1
	movlw	0x02
	movwf	d2
	goto 	WaitCommon
;}

Wait2.44ms	;2433 cycles
;{
	movlw	0xE6
	movwf	d1
	movlw	0x02
	movwf	d2
	goto	WaitCommon
;}

Wait17.56ms	;17553 cycles
;{
	movlw	0xB6
	movwf	d1
	movlw	0x0E
	movwf	d2
	goto	WaitCommon
;}

Wait18.5ms	;18493 cycles
;{
	movlw	0x72
	movwf	d1
	movlw	0x0F
	movwf	d2
	goto	WaitCommon
;}

Wait18.725ms;18718 cycles
;{
	movlw	0x9F
	movwf	d1
	movlw	0x0F
	movwf	d2
	goto	WaitCommon
;}

Wait18.945ms ;18938 cycles
;{
	movlw	0xCB
	movwf	d1
	movlw	0x0F
	movwf	d2
	goto 	WaitCommon
;}

Wait19.335ms;19328 cycles
;{
	movlw	0x19
	movwf	d1
	movlw	0x10
	movwf	d2
	goto	WaitCommon
;}
WaitServo	;Prodleva pro dojezd motoru na misto
;{
	goto Wait19.335ms
;}

WaitCommon
;{
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	WaitCommon		
	nop
	return	;4 cycles (including call)
;}



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;; W T 5 8 8 D   P L A Y E R ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DoSoundFX
;{
		movwf tmp

		movlw .5
		call WaitMilis

		;Nastav SCK a CS na log. 1
		bsf S_SCK			;Pro WT588D
		bsf S_CS

		movlw .17
		call WaitMilis

		bcf	S_CS		;Slave Select = LOW (Initiating transmission)

		movlw .2
		call WaitMilis

		;Ted projdu postupne bit 0, bit 1, bit 2, bit 3 a nastavuju podle toho log 1 nebo log 0

		movlw .8
		movwf i
		
cycle	bcf S_SCK		;Clock na log. 0, zaciname prenos data			

		btfss tmp, 0;  bit 0 nastaven?
		goto btNotSet
		bsf S_SDA		 ; ano, posli log 1
		goto tsted
btNotSet bcf S_SDA		; ne, posli log 0 na vystup
tsted	call Wait30us

		bsf S_SCK		;Clock na log. 1, skoncili jsme prenos 1 bitu	
		call Wait30us

		rrf tmp, f
		decfsz i, f
		goto cycle
		
		bsf S_CS		;Slave Select = HI

		return
;}

SetLoopSound
;{
	movlw .30
	call WaitMilis
	movlw 0xF2			;loop aktualne prehravaneho zvuku
	call DoSoundFX
	return
;}

WaitMilis
;{
		movwf numMilis
WaitM	call Wait1ms
		decfsz numMilis, f
		goto WaitM
		return
;}


WaitSecs
;{
		movwf numSecs
WaitS	call Wait1s
		decfsz numSecs, f
		goto WaitS
		return
;}


Wait30us	;25 cycles
;{
	movlw	0x08
	movwf	d1
Wait30us_0
	decfsz	d1, f
	goto	Wait30us_0	;1 cycle
	nop		;4 cycles (including call)
	return
;}

;*** KONEC SOUBORU ********************************************
	end