Touch pHAT: auto_leds

Hi,
I got a script to to:

  • Control the leds of the Touch pHAT via mqtt as mqtt switches
  • send a payload via mqtt upon touch of the button

now the issue I have is that upon touching, the led turn on, and then off upon release, independently of the previous status of the led that was changed via mqtt.

how can i disable that? i would like the leds to be disconnected from the capacitive control.

thanks!

import threading
import paho.mqtt.client as mqtt
import time
import touchphat

touchphat.all_on()
back_status = 0
a_status = 0
b_status = 0
c_status = 0
d_status = 0
enter_status = 0

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe('touchphat/cmd/back')
    client.subscribe('touchphat/cmd/a')
    client.subscribe('touchphat/cmd/b')
    client.subscribe('touchphat/cmd/c')
    client.subscribe('touchphat/cmd/d')
    client.subscribe('touchphat/cmd/enter')


# Set up and connect to MQTT broker
client = mqtt.Client(protocol=mqtt.MQTTv31)
client.username_pw_set("*********", password="************")
client.on_connect = on_connect
client.on_message = on_message
client.connect("************", 1883, 60)
print("Running...")
client.publish("touchphat/MQTTclient/state", "ON")


@touchphat.on_touch(['Back', 'A', 'B', 'C', 'D', 'Enter'], handler=None)
def handle_touch(event):
    client.publish("touchphat/touch", event.name)
    print(event.name)

client.loop_forever()

I would think that function is in the init file. You’ll likely have to find it and edit it?
touch-phat/init.py at master · pimoroni/touch-phat (github.com)

Thank you alpha, does it mean to set them to false


auto_leds = False

And / or to comment as below

def _handle_press(event):
    global _on_press
    channel = event.channel
    event.name = NAMES[channel]
    event.pad = NUMMAP[channel]

#    if auto_leds:
#        captouch.set_led_state(LEDMAP[channel], True)

    if callable(_on_press[channel]):
        try:
            _on_press[channel](event)
        except TypeError:
            _on_press[channel]()


def _handle_release(event):
    global _on_release
    channel = event.channel
    event.name = NAMES[channel]
    event.pad = NUMMAP[channel]

#    if auto_leds:
#        captouch.set_led_state(LEDMAP[channel], False)

    if callable(_on_release[channel]):
        try:
            _on_release[channel](event)
        except TypeError:
            _on_release[channel]()

I did set auto_led = False on top of my script.

I also cloned into touch-phat, changed auto_leds to False, and python install it, but did not work: did this should have sorted any effect?

I did not try to comment the specific auto_leds lines yet.

Thank you for your support.

That I don’t know? It’s just a hunch, if I’m honest. It’s what I would look at if it was me. I don’t own one so your a bit on your own.

I edited the library in


/usr/local/lib/python/dist-packages/touchphat/__init__.py

with


auto_leds = False

and it worked. Not sure why would not work on top of my script

Might be how things are called up, order wise? What was in that file overrided what you did in your file.