I'm using RPi.GPIO and making a switch matrix which waits for rising or falling edges caused by switch toggles in the rows, triggering a scan of all the switches. I have one interrupt for each row, so any switch toggle can trigger the scan function in the threaded callback thread.
for i in RowList: GPIO.add_event_detect(i, GPIO.BOTH, callback=MatrixScan, bouncetime=100)
Within the MatrixScan function, each column is initially set to input. Then, each column is set low and each row scanned for switch states. Then, everything is set back to the default state of low output.
for i in ColumnList: GPIO.setup(i, GPIO.IN) for i in ColumnList: GPIO.setup(i, GPIO.OUT, initial=GPIO.LOW) for j in RowList: if not GPIO.input(j): #do something GPIO.setup(i, GPIO.IN) for i in ColumnList: GPIO.setup(i, GPIO.OUT, initial=GPIO.LOW)
Since the interrupt in the main function waits for any row to change state, does the fact that the MatrixScan function will cause state changes in rows mean that another MatrixScan run will be queued to run immediately after this one (ad infinitum)? Or are new operations not queued in this thread while something is running? If this is an issue, what practical solution is there?
EDIT: To be clear, when reading the rows, my rows are set with pull-up resistors, so they should be pulled low when a connection is made to a column (set to output LOW)
EDIT: A perhaps better option, as brought up by Pokebab, for the MatrixScan code is simply:
for i in ColumnList: GPIO.output(i, GPIO.HIGH) for i in ColumnList: GPIO.output(i, GPIO.LOW) for j in RowList: if not GPIO.input(j): #do something GPIO.output(i, GPIO.HIGH) for i in ColumnList: GPIO.output(i, GPIO.HIGH)
In this situation, to make sure I have my thinking correct, a row pin (with a pull-up resistor) pulled low by a low output column pin will read False on GPIO.input(), right?