def init(init_board=True): """Initialises the PiFace Digital board""" pfcom.init(SPI_BUS, SPI_CHIP_SELECT) if init_board: # set up each board ioconfig = pfcom.BANK_OFF | \ pfcom.INT_MIRROR_OFF | pfcom.SEQOP_ON | pfcom.DISSLW_OFF | \ pfcom.HAEN_ON | pfcom.ODR_OFF | pfcom.INTPOL_LOW pfd_detected = False for board_index in range(MAX_BOARDS): pfcom.write(ioconfig, pfcom.IOCON, board_index) # configure if not pfd_detected: if pfcom.read(pfcom.IOCON, board_index) == ioconfig: pfd_detected = True pfcom.write(0, pfcom.GPIOA, board_index) # clear port A pfcom.write(0, pfcom.IODIRA, board_index) # set port A as outputs pfcom.write(0xff, pfcom.IODIRB, board_index) # set port B as inputs pfcom.write(0xff, pfcom.GPPUB, board_index) # set port B pullups on if not pfd_detected: raise NoPiFaceDigitalDetectedError( "There was no PiFace Digital board detected!" )
def call_mapped_input_functions(input_func_map): for board_i in range(MAX_BOARDS): this_board_ifm = [m for m in input_func_map if m['board'] == board_i] # read the interupt status of this PiFace board int_bit = pfcom.read(pfcom.INTFB, board_i) if int_bit == 0: continue # The interupt has not been flagged on this board int_bit_num = pfcom.get_bit_num(int_bit) int_byte = pfcom.read(pfcom.INTCAPB, board_i) into = (int_bit & int_byte) >> int_bit_num # bit changed into (0/1) # for each mapping (on this board) see if we have a callback for mapping in this_board_ifm: if int_bit_num == mapping['index'] and \ (mapping['into'] is None or into == mapping['into']): mapping['callback'](int_bit, int_byte) return # one at a time
def _call_mapped_input_functions(input_func_map): """Finds which board caused the interrupt and calls the mapped function. Returns whether the wait_for_input function should keep waiting for input """ for board_i in range(MAX_BOARDS): this_board_ifm = [m for m in input_func_map if m['board'] == board_i] # read the interrupt status of this PiFace board # interrupt bit (int_bit) - bit map showing what caused the interrupt int_bit = pfcom.read(pfcom.INTFB, board_i) if int_bit == 0: continue # The interrupt has not been flagged on this board int_bit_num = pfcom.get_bit_num(int_bit) # interrupt byte (int_byte) - snapshot of in port when int occured int_byte = pfcom.read(pfcom.INTCAPB, board_i) # direction - whether the bit changed into a 1 or a 0 direction = (int_bit & int_byte) >> int_bit_num # for each mapping (on this board) see if we have a callback for mapping in this_board_ifm: if int_bit_num == mapping['index'] and \ (mapping['direction'] is None or direction == mapping['direction']): # run the callback keep_waiting = mapping['callback'](int_bit, int_byte) # stop waiting for interrupts, by default if keep_waiting is None: keep_waiting = False return keep_waiting # This event does not have a mapped function, keep waiting return True
def clear_interrupts(): """Clears the interrupt flags by 'pfcom.read'ing the capture register on all boards """ for i in range(MAX_BOARDS): pfcom.read(pfcom.INTCAPB, i)