; Support code for the control board to talk to the MPR modules.
;
; ### license
;

        list p = 16f688
        include p16f688.inc

        include ../include/handy.inc
        include ../include/i2c.inc
        include ../include/mpr121.inc


; We need to examine this to determine how many MPRs are on the LOCAL bus.
; See main.asm for the defintion/detail.
 extern CONFIG_MASK


        ; Define this module's variables
.gpr0   udata

; Which MPR are we talking to right now?
WHICH_MPR res   1


; Time for the code ...
        code

 global mpr_initall
mpr_initall:
        movlw   I2C_WRITE_TO(I2C_MPR0)
        call    mpr121_init

        btfsc   CONFIG_MASK, 0
        ; Only LOCAL0 in this chain. No more MPRs to initialize.
        return

        movlw   I2C_WRITE_TO(I2C_MPR1)
        call    mpr121_init

        btfsc   CONFIG_MASK, 1
        ; Only LOCAL0/1 in this chain. No more MPRs to initialize.
        return

        movlw   I2C_WRITE_TO(I2C_MPR2)
        call    mpr121_init

        btfsc   CONFIG_MASK, 2
        ; Only LOCAL0/1/2 in this chain. No more MPRs to initialize.
        return

        movlw   I2C_WRITE_TO(I2C_MPR3)
        goto    mpr121_init     ; use its return


 global mpr_check
mpr_check:
        movwf   WHICH_MPR

        ; ### do something

        ;goto    found_mpr

        ; Tell caller that this MPR did NOT signal us, and keep checking.
        clrz
        return

found_mpr:
        ; ### do something
        ; ### tell I2C_MIDDLEMAN about the event
        ; ### clear the MPR event

        ; Tell caller that the MPR event has been handled.
        setz
        return


; stupid assemblers don't realize EOF means *end*
        end
