; tick_tock_timer.EZ
; Foxonix / Metre Ideas and Design
; www.foxonix.com

; Program: Will Sakran
; Created: 04.02.14
; Public - Modify and share this program as you wish.

; Program description
;	Plays a tick-tock sound effect until the specified timeout is reached,
;	then plays a "times up" sound effect. There are three different "times up"
;	sound effects. The effect that plays is determined by which button is
;	pressed.

; Hardware INPUTS
	; Pushbuttons attached to pins P1.0, P1.1, P1.2

; Hardware OUTPUTS
	; Speaker attached to PWM output

; Set the device size. A larger device number allows more audio space, but
; will take longer to upload and program.
[device]
SNC21030


; Specify the number of pins to be used as inputs
[key]
direct=3


; List WAV files to be included in the program
[files]
tick_128ms_12k.wav
tock_144ms_12k.wav
bell_desk_12k.wav
game_show_buzzer_12k.wav
cartoon_horn_12k.wav


; Define the various input states for the program.
; This describes which pins will have buttons, switches, or
; other inputs attached to them, and defines when those
; inputs will be active and inactive.
[input_states]
; PIN		P1.0	P1.1	P1.2
locked_out:	x	x	x		; locked_out state means no buttons are active
enabled:	timer1	timer2	timer3		; normal input state - P1.0 calls timer1, P1.1 calls timer2, etc.


; Define the routines (paths) that the program will use.
; The init path is always called first on power up and reset.
[paths]
init:
	& locked_out		; Disable all inputs during init
	& freq=12k		; Set the audio playback rate to 12 kHz
	& x0=0			; Initialize memory register x0 to zero
	& x1=10			; Set the timer timeout length (in seconds)
	& enabled		; Enable normal input state - makes buttons active
	& end			; Stop program and put chip to sleep (low power mode)


; when P1.0 is triggered, the timer1 label is called
timer1:
	& x0=0					; reset the timer counter
	& timer1_loop

timer1_loop:
	& tick_128ms_12k.wav delay(0.372)	; Play tick sound effect
	& tock_144ms_12k.wav delay(0.356)	; Play tock sound effect
	& x0=x0+1
	& x0?x1:timeout1			; if x0=x1, then we've reached the end of the timeout period
	& timer1_loop				; if not, jump to timer1 and keep ticking


; when P1.1 is triggered, the timer2 label is called
timer2:
	& x0=0
	& timer2_loop

timer2_loop:
	& tick_128ms_12k.wav delay(0.372)	; Play tick sound effect
	& tock_144ms_12k.wav delay(0.356)	; Play tock sound effect
	& x0=x0+1
	& x0?x1:timeout2			; if x0=x1, then we've reached the end of the timeout period
	& timer2_loop				; if not, jump to timer2 and keep ticking


; when P1.2 is triggered, the timer3 label is called
timer3:
	& x0=0
	& timer3_loop

timer3_loop:
	& tick_128ms_12k.wav delay(0.372)	; Play tick sound effect
	& tock_144ms_12k.wav delay(0.356)	; Play tock sound effect
	& x0=x0+1
	& x0?x1:timeout3			; if x0=x1, then we've reached the end of the timeout period
	& timer3_loop				; if not, jump to timer3 and keep ticking


timeout1:
	& game_show_buzzer_12k.wav
	& end					; Stop program and put chip to sleep (low power mode)

timeout2:
	& bell_desk_12k.wav
	& end

timeout3:
	& cartoon_horn_12k.wav
	& end