;---------------------------------------------------------------------------
;-- midilŒdan - version 060208	(updated 060503)
;-- ATtiny26 assembler code for 4 button midi pedal with 62 presets
;-- fuse 4mhz internal oscillator
;-- (c) fredrikolofsson.com
;---------------------------------------------------------------------------

.nolist
.include "tn26def.inc"
.list

.def temp= r16					;general working register
.def bank= r17					;0= no, 1= A, 2= B, 3= C
.def velo= r18					;velocity or controller value
.def chan= r19					;midichannel
.def butt= r20					;upper 4 are events, lower 4 are states
.def cont= r21					;used in midiSend
.def midi= r22					;used in midiSend
.def dely= r23					;used in mainDelay
.def not0= r24					;default for butt0
.def not1= r25					;default for butt1
.def not2= r26					;default for butt2
.def not3= r27					;default for butt3
.def last= r28					;lower 4 keeps previous states for toggle
.def las1= r13					;previous states for toggle bank A
.def las2= r14					;previous states for toggle bank B
.def las3= r15					;previous states for toggle bank C

.cseg
.org $0000

;---------------------------------------------------------------------------
;-- start
;-- settings
;---------------------------------------------------------------------------
start:
	ldi temp, RAMEND			;init stack
	out SP, temp
	ldi temp, 0b10000000		;porta/pin7 output midi
	out DDRA, temp
	ldi temp, 0b01110000		;portb/pins4-6 output leds
	out DDRB, temp
	ldi temp, 0b11111111		;porta/pins0-7 pull high presets and midi
	out portA, temp
	ldi temp, 0b00001111		;portb/pins0-3 pull high for buttons
	out portB, temp
	ldi last, 0					;clear flags for toggle
	ldi bank, 0
	
;---------------------------------------------------------------------------
;-- main
;-- inf loop. uses dely for short delay (update rate)
;---------------------------------------------------------------------------
main:
	rcall checkButt
	rcall checkPset
	rcall lightLeds
	rcall mainDelay
	rjmp main
	
mainDelay:
	ldi dely, 255
mainDelay_1:
	rcall midiDelay
	dec dely
	brne mainDelay_1
	ret
	
;---------------------------------------------------------------------------
;-- checkButt
;-- detect change in buttons. uses temp and butt
;-- called from main
;-- (afterwards butt upper 4= events, and lower 4= states)
;---------------------------------------------------------------------------
checkButt:
	in temp, pinB				;read current button states. lower 4
	com temp					;invert all bits
	andi temp, 0b00001111		;filter out non-button bits. safety
	andi butt, 0b00001111		;clear last events in upper 4
	eor butt, temp				;check if any change
	swap butt					;move and store events in upper 4
	or butt, temp				;store states
	ret
	
;---------------------------------------------------------------------------
;-- checkPset
;-- detect change in presets. reads pinA and jump to corresponding pset sub
;-- called from main
;---------------------------------------------------------------------------
checkPset:
	ldi not0, 60				;default note numbers
	ldi not1, 61
	ldi not2, 62
	ldi not3, 63
	ldi velo, 127				;default velocity
	ldi chan, 0					;default midi channel
	in temp, pinA				;read from all pins on portA
	com temp					;invert all bits
	andi temp, 0b01111111		;ignore bit 7
	cpi temp, 62				;make sure pset only 0-61
	brsh psetTooHigh
	ldi ZH, HIGH(table)
	ldi ZL, LOW(table)
	add ZL, temp
	brcc noOverFlow
	inc ZH
noOverFlow:
	ijmp						;jump to pset sub
	
psetTooHigh:
	ldi bank, 255				;force bank to light all leds
	
checkPset_:						;back from pset subs
	ret							;back to main
	
table:							;address table
	rjmp pset00
	rjmp pset01
	rjmp pset02
	rjmp pset03
	rjmp pset04
	rjmp pset05
	rjmp pset06
	rjmp pset07
	rjmp pset08
	rjmp pset09
	rjmp pset10
	rjmp pset11
	rjmp pset12
	rjmp pset13
	rjmp pset14
	rjmp pset15
	rjmp pset16
	rjmp pset17
	rjmp pset18
	rjmp pset19
	rjmp pset20
	rjmp pset21
	rjmp pset22
	rjmp pset23
	rjmp pset24
	rjmp pset25
	rjmp pset26
	rjmp pset27
	rjmp pset28
	rjmp pset29
	rjmp pset30
	rjmp pset31
	rjmp pset32
	rjmp pset33
	rjmp pset34
	rjmp pset35
	rjmp pset36
	rjmp pset37
	rjmp pset38
	rjmp pset39
	rjmp pset40
	rjmp pset41
	rjmp pset42
	rjmp pset43
	rjmp pset44
	rjmp pset45
	rjmp pset46
	rjmp pset47
	rjmp pset48
	rjmp pset49
	rjmp pset50
	rjmp pset51
	rjmp pset52
	rjmp pset53
	rjmp pset54
	rjmp pset55
	rjmp pset56
	rjmp pset57
	rjmp pset58
	rjmp pset59
	rjmp pset60
	rjmp pset61
	
;---------------------------------------------------------------------------
;-- pset subs
;-- called from checkPset
;---------------------------------------------------------------------------	
pset00:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rcall checkButt3Toggle
	rjmp checkPset_
	
pset01:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset01_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset01_B:
	cpi bank, 2					;check bank
	brne pset01_C
	rcall noteAdd3
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset01_C:
	;cpi bank, 3				;check bank
	;brne pset01_
	rcall noteAdd6
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
	
pset02:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset02_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset02_B:
	;cpi bank, 2				;check bank
	;brne pset02_
	rcall noteAdd3
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
	
pset03:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rcall checkButt3ToggleChan3
	rjmp checkPset_
	
pset04:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset04_B
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
pset04_B:
	cpi bank, 2					;check bank
	brne pset04_C
	rcall noteAdd3
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
pset04_C:
	;cpi bank, 3				;check bank
	;brne pset04_
	rcall noteAdd6
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
	
pset05:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset05_B
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
pset05_B:
	;cpi bank, 2				;check bank
	;brne pset05_
	rcall noteAdd3
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
	
pset06:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rcall checkButt3Switch
	rjmp checkPset_
	
pset07:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset07_B
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset07_B:
	cpi bank, 2					;check bank
	brne pset07_C
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset07_C:
	;cpi bank, 3				;check bank
	;brne pset07_
	rcall noteAdd6
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
	
pset08:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset08_B
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset08_B:
	;cpi bank, 2				;check bank
	;brne pset08_
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
	
pset09:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rcall checkButt3SwitchChan3
	rjmp checkPset_
	
pset10:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset10_B
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
pset10_B:
	cpi bank, 2					;check bank
	brne pset10_C
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
pset10_C:
	;cpi bank, 3				;check bank
	;brne pset10_
	rcall noteAdd6
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
	
pset11:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset11_B
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
pset11_B:
	;cpi bank, 2				;check bank
	;brne pset11_
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
	
pset12:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rcall checkButt3Ctrl
	rjmp checkPset_
	
pset13:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset13_B
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
pset13_B:
	cpi bank, 2					;check bank
	brne pset13_C
	rcall noteSub47
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
pset13_C:
	;cpi bank, 3				;check bank
	;brne pset13_
	rcall noteSub44
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset14:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset14_B
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
pset14_B:
	;cpi bank, 2				;check bank
	;brne pset14_
	rcall noteSub47
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset15:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rcall checkButt3CtrlChan3
	rjmp checkPset_
	
pset16:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset16_B
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
pset16_B:
	cpi bank, 2					;check bank
	brne pset16_C
	rcall noteSub47
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
pset16_C:
	;cpi bank, 3				;check bank
	;brne pset16_
	rcall noteSub44
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
	
pset17:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset17_B
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
pset17_B:
	;cpi bank, 2				;check bank
	;brne pset17_
	rcall noteSub47
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
	
pset18:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset18_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset18_B:
	cpi bank, 2					;check bank
	brne pset18_C
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset18_C:
	;cpi bank, 3				;check bank
	;brne pset18_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset19:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset19_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset19_B:
	;cpi bank, 2				;check bank
	;brne pset19_
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
	
pset20:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset20_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset20_B:
	;cpi bank, 2				;check bank
	;brne pset20_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset21:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset21_B
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset21_B:
	;cpi bank, 2				;check bank
	;brne pset21_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset22:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset22_B
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
pset22_B:
	cpi bank, 2					;check bank
	brne pset22_C
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
pset22_C:
	;cpi bank, 3				;check bank
	;brne pset22_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
	
pset23:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset23_B
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
pset23_B:
	;cpi bank, 2				;check bank
	;brne pset23_
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
	
pset24:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset24_B
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rjmp checkPset_
pset24_B:
	;cpi bank, 2				;check bank
	;brne pset24_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
	
pset25:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset25_B
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rjmp checkPset_
pset25_B:
	;cpi bank, 2				;check bank
	;brne pset25_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	rjmp checkPset_
	
pset26:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Switch
	rcall checkButt3Switch
	rjmp checkPset_
	
pset27:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	ldi not2, 10
	rcall checkButt2Ctrl
	ldi not3, 11
	rcall checkButt3Ctrl
	rjmp checkPset_
	
pset28:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1Switch
	ldi not2, 10
	rcall checkButt2Ctrl
	ldi not3, 11
	rcall checkButt3Ctrl
	rjmp checkPset_
	
pset29:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rcall checkButt3Switch
	rjmp checkPset_
	
pset30:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	ldi not3, 10
	rcall checkButt3Ctrl
	rjmp checkPset_
	
pset31:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rcall checkButt3Toggle
	rjmp checkPset_
	
pset32:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	ldi not3, 10
	rcall checkButt3Ctrl
	rjmp checkPset_
	
pset33:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	ldi not3, 63
	rcall checkButt3Toggle
	rjmp checkPset_
	
pset34:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	ldi not3, 63
	rcall checkButt3Switch
	rjmp checkPset_
	
pset35:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2SwitchChan2
	rcall checkButt3SwitchChan3
	rjmp checkPset_
	
pset36:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	ldi not2, 10
	rcall checkButt2CtrlChan2
	ldi not3, 11
	rcall checkButt3CtrlChan3
	rjmp checkPset_
	
pset37:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	ldi not2, 10
	rcall checkButt2CtrlChan2
	ldi not3, 11
	rcall checkButt3CtrlChan3
	rjmp checkPset_
	
pset38:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	rcall checkButt3SwitchChan3
	rjmp checkPset_
	
pset39:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1ToggleChan1
	rcall checkButt2ToggleChan2
	ldi not3, 10
	rcall checkButt3CtrlChan3
	rjmp checkPset_
	
pset40:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	rcall checkButt3ToggleChan3
	rjmp checkPset_
	
pset41:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1SwitchChan1
	rcall checkButt2SwitchChan2
	ldi not3, 10
	rcall checkButt3CtrlChan3
	rjmp checkPset_
	
pset42:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	ldi not3, 63
	rcall checkButt3ToggleChan3
	rjmp checkPset_
	
pset43:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1CtrlChan1
	rcall checkButt2CtrlChan2
	ldi not3, 63
	rcall checkButt3SwitchChan3
	rjmp checkPset_
	
pset44:
	ldi bank, 0					;force bank
	rcall checkButt0Button
	rcall checkButt1Button
	ldi not2, 10
	rcall checkButt2Ctrl
	ldi not3, 11
	rcall checkButt3Ctrl
	rjmp checkPset_
	
pset45:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Button
	rcall checkButt3Button
	rjmp checkPset_
	
pset46:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Button
	rcall checkButt3Button
	rjmp checkPset_
	
pset47:
	ldi bank, 0					;force bank
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rcall checkButt3Button
	rjmp checkPset_
	
pset48:
	ldi bank, 0					;force bank
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rcall checkButt3Button
	rjmp checkPset_
	
pset49:
	ldi bank, 0					;force bank
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rcall checkButt3Toggle
	rjmp checkPset_
	
pset50:
	ldi bank, 0					;force bank
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	ldi not3, 63
	rcall checkButt3Button
	rjmp checkPset_
	
pset51:
	ldi bank, 0					;force bank
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rcall checkButt3Button
	rjmp checkPset_	
	
pset52:
	ldi bank, 0					;force bank
	rcall checkButt0Button
	rcall checkButt1ButtonChan1
	rcall checkButt2ButtonChan2
	rcall checkButt3ButtonChan3
	rjmp checkPset_	
	
pset53:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset53_B
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
pset53_B:
	cpi bank, 2					;check bank
	brne pset53_C
	rcall noteAdd3
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
pset53_C:
	;cpi bank, 3				;check bank
	;brne pset53_
	rcall noteAdd6
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
	
pset54:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset54_B
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
pset54_B:
	;cpi bank, 2				;check bank
	;brne pset54_
	rcall noteAdd3
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
	
pset55:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset55_B
	rcall checkButt0Button
	rcall checkButt1ButtonChan1
	rcall checkButt2ButtonChan2
	rjmp checkPset_
pset55_B:
	cpi bank, 2					;check bank
	brne pset55_C
	rcall noteAdd3
	rcall checkButt0Button
	rcall checkButt1ButtonChan1
	rcall checkButt2ButtonChan2
	rjmp checkPset_
pset55_C:
	;cpi bank, 3				;check bank
	;brne pset55_
	rcall noteAdd6
	rcall checkButt0Button
	rcall checkButt1ButtonChan1
	rcall checkButt2ButtonChan2
	rjmp checkPset_
	
pset56:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset56_B
	rcall checkButt0Button
	rcall checkButt1ButtonChan1
	rcall checkButt2ButtonChan2
	rjmp checkPset_
pset56_B:
	;cpi bank, 2				;check bank
	;brne pset56_
	rcall noteAdd3
	rcall checkButt0Button
	rcall checkButt1ButtonChan1
	rcall checkButt2ButtonChan2
	rjmp checkPset_
	
pset57:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset57_B
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
pset57_B:
	cpi bank, 2					;check bank
	brne pset57_C
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset57_C:
	;cpi bank, 3				;check bank
	;brne pset57_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset58:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset58_B
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
pset58_B:
	;cpi bank, 2				;check bank
	;brne pset58_
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
	
pset59:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset59_B
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
pset59_B:
	;cpi bank, 2				;check bank
	;brne pset59_
	rcall noteSub50
	rcall checkButt0Ctrl
	rcall checkButt1Ctrl
	rcall checkButt2Ctrl
	rjmp checkPset_
	
pset60:
	rcall bankButtonAB			;update bank
	cpi bank, 1					;check bank
	brne pset60_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset60_B:
	;cpi bank, 2				;check bank
	;brne pset60_
	rcall noteAdd3
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
	
pset61:
	rcall bankButtonABC			;update bank
	cpi bank, 1					;check bank
	brne pset61_B
	rcall checkButt0Toggle
	rcall checkButt1Toggle
	rcall checkButt2Toggle
	rjmp checkPset_
pset61_B:
	cpi bank, 2					;check bank
	brne pset61_C
	rcall noteAdd3
	rcall checkButt0Switch
	rcall checkButt1Switch
	rcall checkButt2Switch
	rjmp checkPset_
pset61_C:
	;cpi bank, 3				;check bank
	;brne pset61_
	rcall noteAdd6
	rcall checkButt0Button
	rcall checkButt1Button
	rcall checkButt2Button
	rjmp checkPset_
	
;---------------------------------------------------------------------------
;-- noteAddX, noteSubXX
;-- adjust not0-4 registers
;-- called from psets
;---------------------------------------------------------------------------
noteAdd3:
	ldi temp, 3
	rjmp noteAdd
noteAdd6:
	ldi temp, 6
noteAdd:
	add not0, temp
	add not1, temp
	add not2, temp
	add not3, temp
	ret
	
noteSub50:
	ldi temp, 50
	rjmp noteSub
noteSub47:
	ldi temp, 47
	rjmp noteSub
noteSub44:
	ldi temp, 44
noteSub:
	sub not0, temp
	sub not1, temp
	sub not2, temp
	sub not3, temp
	ret
	
;---------------------------------------------------------------------------
;-- bankButtonAB, bankButtonABC
;-- set bank. afterwards bank is updated. maybe increased, maybe wrapped
;-- called from psets
;---------------------------------------------------------------------------
bankButtonAB:					;cycle bank A>B>A...
	rcall bankCheckBank0		;make sure no bank 0 here
	rcall bankCheckBank3		;and no bank 3 either
	cpi butt, 0b10001000		;check if event at button3 and pressed
	brne bankCheckABDone
	cpi bank, 1					;if bank= 1(A) then 2(B)
	brne bankCheckAB_2
	ldi bank, 2
	mov las1, last				;save button states for toggle
	mov last, las2				;recall button states for toggle
	ret
bankCheckAB_2:
	cpi bank, 2					;if bank= 2(B) then 1(A)
	brne bankCheckABDone
	ldi bank, 1
	mov las2, last				;save button states for toggle
	mov last, las1				;recall button states for toggle
bankCheckABDone:
	ret
	
bankButtonABC:					;cycle bank A>B>C>A...
	rcall bankCheckBank0		;make sure no bank 0 here
	cpi butt, 0b10001000		;check if event at button3 and pressed
	brne bankCheckABCDone
	cpi bank, 1					;if bank= 1(A) then 2(B)
	brne bankCheckABC_2
	ldi bank, 2
	mov las1, last				;save button states for toggle
	mov last, las2				;recall button states for toggle
	ret
bankCheckABC_2:
	cpi bank, 2					;if bank= 2(B) then 3(C)
	brne bankCheckABC_3
	ldi bank, 3
	mov las2, last				;save button states for toggle
	mov last, las3				;recall button states for toggle
	ret
bankCheckABC_3:
	cpi bank, 3					;if bank= 3(C) then 1(A)
	brne bankCheckABCDone
	ldi bank, 1
	mov las3, last				;save button states for toggle
	mov last, las1				;recall button states for toggle
bankCheckABCDone:
	ret
	
bankCheckBank0:
	cpi bank, 0					;if bank= 0 then 1(A). zero not allowed
	brne bankCheckBank0Done
	ldi bank, 1
bankCheckBank0Done:
	ret
	
bankCheckBank3:
	cpi bank, 3					;if bank= 3 then 1(A). no bank 3 if AB only
	brne bankCheckBank3Done
	ldi bank, 1
bankCheckBank3Done:
	ret
	
;---------------------------------------------------------------------------
;-- checkButtXToggle, checkButtXSwitch, checkButtXButton, checkButtXCtrl
;-- check button events and pressed, set note and call routine to send midi
;-- called from psets
;---------------------------------------------------------------------------
checkButt0Toggle:
	sbrc butt, 0				;check button 0 event
	sbrs butt, 4				;check button 0 pressed
	ret
	sbrc last, 0				;skip if previous was noteOff
	rjmp checkButt0Toggle_2
	ori last, 0b00000001		;set toggle flag
	rjmp checkButt0Toggle_3
checkButt0Toggle_2:
	andi last, 0b11111110		;clear toggle flag
	ldi velo, 0
checkButt0Toggle_3:
	rcall sendNote0
	ret
	
checkButt1ToggleChan1:			;also set midi channel
	ldi chan, 1
checkButt1Toggle:
	sbrc butt, 5				;check button 1 event
	sbrs butt, 1				;check button 1 pressed
	ret
	sbrc last, 1				;skip if previous was noteOff
	rjmp checkButt1Toggle_2
	ori last, 0b00000010		;set toggle flag
	rjmp checkButt1Toggle_3
checkButt1Toggle_2:
	andi last, 0b11111101		;clear toggle flag
	ldi velo, 0
checkButt1Toggle_3:
	rcall sendNote1
	ret
	
checkButt2ToggleChan2:			;also set midi channel
	ldi chan, 2
checkButt2Toggle:
	sbrc butt, 6				;check button 2 event
	sbrs butt, 2				;check button 2 pressed
	ret
	sbrc last, 2				;skip if previous was noteOff
	rjmp checkButt2Toggle_2
	ori last, 0b00000100		;set toggle flag
	rjmp checkButt2Toggle_3
checkButt2Toggle_2:
	andi last, 0b11111011		;clear toggle flag
	ldi velo, 0
checkButt2Toggle_3:
	rcall sendNote2
	ret
	
checkButt3ToggleChan3:			;also set midi channel
	ldi chan, 3
checkButt3Toggle:
	sbrc butt, 7				;check button 3 event
	sbrs butt, 3				;check button 3 pressed
	ret
	sbrc last, 3				;skip if previous was noteOff
	rjmp checkButt3Toggle_2
	ori last, 0b00001000		;set toggle flag
	rjmp checkButt3Toggle_3
checkButt3Toggle_2:
	andi last, 0b11110111		;clear toggle flag
	ldi velo, 0
checkButt3Toggle_3:
	rcall sendNote3
	ret
	
checkButt0Switch:
	sbrs butt, 4				;check button 0 event
	ret
	sbrs butt, 0				;skip if button pressed
	ldi velo, 0
	rcall sendNote0
	ret
	
checkButt1SwitchChan1:			;also set midi channel
	ldi chan, 1
checkButt1Switch:
	sbrs butt, 5				;check button 1 event
	ret
	sbrs butt, 1				;skip if button pressed
	ldi velo, 0
	rcall sendNote1
	ret
	
checkButt2SwitchChan2:			;also set midi channel
	ldi chan, 2
checkButt2Switch:
	sbrs butt, 6				;check button 2 event
	ret
	sbrs butt, 2				;skip if button pressed
	ldi velo, 0
	rcall sendNote2
	ret
	
checkButt3SwitchChan3:			;also set midi channel
	ldi chan, 3
checkButt3Switch:
	sbrs butt, 7				;check button 3 event
	ret
	sbrs butt, 3				;skip if button pressed
	ldi velo, 0
	rcall sendNote3
	ret
	
checkButt0Button:
	sbrc butt, 4				;check button 0 event
	sbrs butt, 0				;check button 0 pressed
	ret
	rcall sendNote0
	ret
	
checkButt1ButtonChan1:			;also set midi channel
	ldi chan, 1
checkButt1Button:
	sbrc butt, 5				;check button 1 event
	sbrs butt, 1				;check button 1 pressed
	ret
	rcall sendNote1
	ret
	
checkButt2ButtonChan2:			;also set midi channel
	ldi chan, 2
checkButt2Button:
	sbrc butt, 6				;check button 2 event
	sbrs butt, 2				;check button 2 pressed
	ret
	rcall sendNote2
	ret
	
checkButt3ButtonChan3:			;also set midi channel
	ldi chan, 3
checkButt3Button:
	sbrc butt, 7				;check button 3 event
	sbrs butt, 3				;check button 3 pressed
	ret
	rcall sendNote3
	ret
	
checkButt0Ctrl:
	sbrc butt, 4				;check button 0 event
	sbrs butt, 0				;check button 0 pressed
	ret
	rcall sendCtrl0
	ret
	
checkButt1CtrlChan1:			;also set midi channel
	ldi chan, 1
checkButt1Ctrl:
	sbrc butt, 5				;check button 1 event
	sbrs butt, 1				;check button 1 pressed
	ret
	rcall sendCtrl1
	ret
	
checkButt2CtrlChan2:			;also set midi channel
	ldi chan, 2
checkButt2Ctrl:
	sbrc butt, 6				;check button 2 event
	sbrs butt, 2				;check button 2 pressed
	ret
	rcall sendCtrl2
	ret
	
checkButt3CtrlChan3:			;also set midi channel
	ldi chan, 3
checkButt3Ctrl:
	sbrc butt, 7				;check button 3 event
	sbrs butt, 3				;check button 3 pressed
	ret
	rcall sendCtrl3
	ret
	
;---------------------------------------------------------------------------
;-- sendNoteX, sendCtrlX
;-- uses midi as shift out register, cont for shifting and temp for delay
;-- called from checkButtX...
;-- (not0 and velo is ctrlnumber and ctrlvalue when calling sendCtrl)
;---------------------------------------------------------------------------
sendNote1:
	mov not0, not1
	rjmp sendNote0
sendNote2:
	mov not0, not2
	rjmp sendNote0
sendNote3:
	mov not0, not3
sendNote0:
	ldi midi, 144				;note
	rjmp sendMidi
	
sendCtrl1:
	mov not0, not1
	rjmp sendCtrl0
sendCtrl2:
	mov not0, not2
	rjmp sendCtrl0
sendCtrl3:
	mov not0, not3
sendCtrl0:
	ldi midi, 176				;control
	ldi velo, 127				;values always 127
	
sendMidi:
	or midi, chan				;add channel
	rcall midiSend
	rcall midiDelay
	mov midi, not0				;for ctrl this is controller number
	rcall midiSend
	rcall midiDelay
	mov midi, velo				;for ctrl this is controller value
	rcall midiSend
	rcall midiDelay
	ret
	
midiSend:
	ldi cont, 10
	com midi
	sec
midiSend_2:
	brcc midiSend_3
	cbi portA, 7				;clear out pin7
	rjmp midiSend_4
midiSend_3:
	sbi portA, 7				;set out pin7
	nop
	nop
midiSend_4:
	rcall midiDelay
	rcall midiDelay
	lsr midi
	dec cont
	brne midiSend_2
	ret
	
midiDelay:						;to match midi baudrate (31250)
	ldi temp, 18				;short delay for 4mhz oscillator
midiDelay_1:
	dec temp
	brne midiDelay_1
	ret
	
;---------------------------------------------------------------------------
;-- lightLeds
;-- set and clear leds according to bank
;-- called from main
;---------------------------------------------------------------------------
lightLeds:
	cpi bank, 0					;no bank - no leds
	brne ll_1
	cbi portB, 4
	cbi portB, 5
	cbi portB, 6
ll_1:
	cpi bank, 1					;bank A - red led
	brne ll_2
	sbi portB, 4
	cbi portB, 5
	cbi portB, 6
ll_2:
	cpi bank, 2					;bank B - green led
	brne ll_3
	cbi portB, 4
	sbi portB, 5
	cbi portB, 6
ll_3:
	cpi bank, 3					;bank C - yellow led
	brne ll_4
	cbi portB, 4
	cbi portB, 5
	sbi portB, 6
ll_4:
	cpi bank, 255				;too high pset - all leds
	brne ll_5
	sbi portB, 4
	sbi portB, 5
	sbi portB, 6
ll_5:
	ret							;back to main
