;#define	x16F690
#define	x12F683

#ifdef x16F690
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
LED_PORT			equ	PORTC
BTN_PORT			equ	PORTC
BTNM_PORT			equ	PORTA
BTN_PORT_CONTROL	equ	TRISC
BTNM_PORT_CONTROL	equ	TRISA
BTNL				equ	4
BTNM				equ	3
BTNR				equ	5
BTN_PORT_SETTING	equ	0x30
BTNM_PORT_SETTING	equ	0xFF
#endif

#ifdef x12F683
#include <p12F683.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _FCMEN_OFF)
LED_PORT			equ	GPIO
BTN_PORT			equ	GPIO
BTNM_PORT			equ	GPIO
BTN_PORT_CONTROL	equ	TRISIO
BTNM_PORT_CONTROL	equ	TRISIO
BTNL				equ	0
BTNM				equ	1
BTNR				equ	2
BTN_PORT_SETTING	equ	0x1F
BTNM_PORT_SETTING	equ	0x1F
#endif


DEFAULT_CUTOFF_LO	equ	0x06
DEFAULT_CUTOFF_HI	equ	0x06
BLINK_LENGTH_LO		equ	0x01
BLINK_LENGTH_HI		equ	0x04
BLINK_CYCLE_LEN		equ	0x04
INITIAL_BRIGHTNESS1	equ	0x10
INITIAL_BRIGHTNESS2	equ	0x20

		
	cblock 0x20
    	d1
		blinkLo
		blinkHi
		cutoffLo
		cutoffHi
		blinkCount
		lookupIndex
	    brightness1
		brightness2
		temp
	endc

	org 0x00
	goto	Main
	org	0x04
	goto	Main

	org	0x0c
Main

	; TESTING OF BRIGHTNESS2 LIMIT
	;movlw	0x00
	;movwf	brightness2
	;movlw	0x7F
	;movwf	brightness1
	;call	FixBrightness2

	call	Init
Loop
	call	CheckPressedButtons
	call	SetBlinkCutoff
	call	BlinkCycle
	goto 	Loop


CheckPressedButtons
	btfss	BTN_PORT,BTNL
	call	DecreaseBrightness
	btfss	BTN_PORT,BTNR
	call	IncreaseBrightness
	return


DecreaseBrightness
	btfss	BTNM_PORT,BTNM
	goto	DecreaseBrightness2
	movf	brightness1,w
	btfss	STATUS,Z
	decf	brightness1,f
	return
DecreaseBrightness2
	movf	brightness2,w
	btfss	STATUS,Z
	decf	brightness2,f
	goto	FixBrightness1
	

IncreaseBrightness
	btfss	BTNM_PORT,BTNM
	goto	IncreaseBrightness2
	movlw	0x7f
	subwf	brightness1,w
	btfss	STATUS,Z
	incf	brightness1,f
	goto	FixBrightness2
IncreaseBrightness2
	movlw	0x7f
	subwf	brightness2,w
	btfss	STATUS,Z
	incf	brightness2,f
	return


FixBrightness2		; bounds brightness2 at or above brightness1
	movf	brightness2,w
	subwf	brightness1,w	
	btfss	STATUS,C
	return		; No need to adjust
	movf	brightness1,w
	movwf	brightness2
	return


FixBrightness1		; bounds brightness1 at or below brightness1
	movf	brightness2,w
	subwf	brightness1,w	
	btfss	STATUS,C
	return		; No need to adjust
	movf	brightness2,w
	movwf	brightness1
	return


SetBlinkCutoff
	btfss	BTNM_PORT,BTNM
	goto	SetBlinkCutoff2
	movf	brightness1,w
	movwf	lookupIndex
	goto	SetBlinkCutoff_cont
SetBlinkCutoff2
	movf	brightness2,w
	movwf	lookupIndex
SetBlinkCutoff_cont
	movf	lookupIndex,w
	call	LookupCutoffLo
	movwf	cutoffLo
	movf	lookupIndex,w
	call	LookupCutoffHi
	movwf	cutoffHi
	return	

Init
	; The digital outputs

    BANKSEL GPIO ;
	CLRF GPIO ;Init GPIO
	MOVLW 0x07 ;Set GP<2:0> to
	MOVWF CMCON0 ;digital I/O
	BANKSEL ANSEL ;
	CLRF ANSEL ;digital I/O
	MOVLW 0x1F ;Set GP<3:2> as inputs
	MOVWF TRISIO

	banksel	LED_PORT	; Turn leds off
	movlw	0x00
	movwf	LED_PORT
	movlw	INITIAL_BRIGHTNESS1
	movwf	brightness1
	movlw	INITIAL_BRIGHTNESS2
	movwf	brightness2	
	return

BlinkCycle
	movlw	BLINK_CYCLE_LEN
	movwf	blinkCount
BlinkCycle_0
	call	BlinkImpulse
	decfsz	blinkCount,f
	goto	BlinkCycle_0
	return
	
BlinkImpulse
	call	LedsOn
	movlw	BLINK_LENGTH_LO
	movwf	blinkLo
	movlw	BLINK_LENGTH_HI
	movwf	blinkHi
BlinkImpulse_0
	movf	cutoffLo,w
	subwf	blinkLo,w
	btfss	STATUS,Z
	goto	BlinkImpulse_noCutoff
	movf	cutoffHi,w
	subwf	blinkHi,w
	btfss	STATUS,Z
	goto	BlinkImpulse_noCutoff
	call	LedsOff
BlinkImpulse_noCutoff
	decfsz	blinkLo, f
	goto	BlinkImpulse_0
	decfsz	blinkHi, f
	goto	BlinkImpulse_0
	return

LedsOn
	movlw	0xff
	goto	LedsSet
LedsOff
	movlw	0x00
LedsSet
	movwf	LED_PORT
	return

        org 0x101
LookupCutoffHi
        movwf   temp
        movlw   0x01
        movwf   PCLATH
        movf    temp,w
        addwf   PCL,f
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x03
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x02
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x01
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00
        retlw   0x00

        org 0x201
LookupCutoffLo
        movwf   temp
        movlw   0x02
        movwf   PCLATH
        movf    temp,w
        addwf   PCL,f
        retlw   0xff
        retlw   0xff
        retlw   0xfe
        retlw   0xfe
        retlw   0xfe
        retlw   0xfe
        retlw   0xfd
        retlw   0xfd
        retlw   0xfc
        retlw   0xfc
        retlw   0xfb
        retlw   0xfb
        retlw   0xfa
        retlw   0xfa
        retlw   0xf9
        retlw   0xf8
        retlw   0xf7
        retlw   0xf6
        retlw   0xf5
        retlw   0xf4
        retlw   0xf3
        retlw   0xf2
        retlw   0xf1
        retlw   0xef
        retlw   0xee
        retlw   0xec
        retlw   0xeb
        retlw   0xe9
        retlw   0xe7
        retlw   0xe5
        retlw   0xe3
        retlw   0xe1
        retlw   0xdf
        retlw   0xdd
        retlw   0xda
        retlw   0xd8
        retlw   0xd5
        retlw   0xd3
        retlw   0xd0
        retlw   0xcd
        retlw   0xca
        retlw   0xc7
        retlw   0xc3
        retlw   0xc0
        retlw   0xbc
        retlw   0xb9
        retlw   0xb5
        retlw   0xb1
        retlw   0xad
        retlw   0xa9
        retlw   0xa4
        retlw   0xa0
        retlw   0x9b
        retlw   0x97
        retlw   0x92
        retlw   0x8d
        retlw   0x87
        retlw   0x82
        retlw   0x7c
        retlw   0x77
        retlw   0x71
        retlw   0x6b
        retlw   0x64
        retlw   0x5e
        retlw   0x58
        retlw   0x51
        retlw   0x4a
        retlw   0x43
        retlw   0x3c
        retlw   0x34
        retlw   0x2c
        retlw   0x25
        retlw   0x1d
        retlw   0x14
        retlw   0x0c
        retlw   0x03
        retlw   0xfa
        retlw   0xf1
        retlw   0xe8
        retlw   0xdf
        retlw   0xd5
        retlw   0xcb
        retlw   0xc1
        retlw   0xb7
        retlw   0xac
        retlw   0xa2
        retlw   0x97
        retlw   0x8c
        retlw   0x80
        retlw   0x75
        retlw   0x69
        retlw   0x5d
        retlw   0x50
        retlw   0x44
        retlw   0x37
        retlw   0x2a
        retlw   0x1d
        retlw   0x0f
        retlw   0x01
        retlw   0xf3
        retlw   0xe5
        retlw   0xd6
        retlw   0xc7
        retlw   0xb8
        retlw   0xa9
        retlw   0x99
        retlw   0x89
        retlw   0x79
        retlw   0x69
        retlw   0x58
        retlw   0x47
        retlw   0x36
        retlw   0x24
        retlw   0x12
        retlw   0x00
        retlw   0xee
        retlw   0xdb
        retlw   0xc8
        retlw   0xb5
        retlw   0xa1
        retlw   0x8d
        retlw   0x79
        retlw   0x64
        retlw   0x4f
        retlw   0x3a
        retlw   0x25
        retlw   0x0f
        retlw   0x01
	
	END