I’m trying a perhaps ill-advised setup of running plain Python on my Model B+ Raspberry Pi to control some Qwiic buttons over i2c – and having some issues. New to this so maybe I’m missing something.
The button does not have an official python driver (yet, I might build it), and so am doing low-level i2c prodding. Here is code that polls the button watching for status changes. The problem is … that I sometimes get presses/noise even when nothing has been pressed. It runs fine for a few seconds (no presses) and then sees presses when there weren’t any. It DOES pick up real presses in addition to the noise! I also have no problem setting the LED brightness and even blinking.
Any advice would be appreciated. I’ve tried shorter qwiic wires, only having the one device, increasing the sleeps. I haven’t yet tried messing with the i2c bus clock speed, I think the default is 100 kHz.
import time
import qwiic
qwiic_i2c = qwiic._QwiicInternal.get_i2c_driver()
# Set brightness to 0x10
qwiic_i2c.writeByte(111, 0x19, 0x0)
def monitor_button():
print("Reset the current status")
qwiic_i2c.writeByte(111, 0x03, 0)
time.sleep(0.05)
# Wait for a click
while True:
time.sleep(0.05)
button_status = qwiic_i2c.readByte(111, 0x03)
# Bit 0 (eventAvailable) is set to 1 when a new event occurs user must write 0 to clear.
# Bit 1 (hasBeenClicked) defaults to 0 on POR, is set to one when the button is clicked, must be cleared by the user.
# Bit 2 (isPressed) is set to 1 when the button is pushed
event_available = button_status & 0b001
has_been_clicked = button_status & 0b010
is_pressed = button_status & 0b100
print(f"Status: {bin(button_status)} Event Available: {event_available} Has clicked: {has_been_clicked} Currently pressed: {is_pressed}")
if has_been_clicked:
print(f"CLICK DETECTED")
qwiic_i2c.writeByte(111, 0x03, 0)
# break
monitor_button()
Sample output:
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b1111111 Event Available: 1 Has clicked: 2 Currently pressed: 4
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED