PiFace CAD - a different request

Actually, could be it would work,,,,at least the library import works, so worth trying:

  1. Install node-red-contrib-python-function (it seems to be newer than the python3 you mentioned)

  2. Import and deploy the flow below (NR must in this case run in the same Pi as the piFaceCAD is mounted)

If it works, you should be able to see messages written on the lcd and in the debug window

The simple flow:
image

[{"id":"13305b2.f5872a5","type":"python-function","z":"ae89c6f4.025a58","name":"","func":"import pifacecad\n\ndef update_pin_text(event):\n    event.chip.lcd.set_cursor(13, 0)\n    event.chip.lcd.write(str(event.pin_num))\n    node.send(\"You pressed button:\"+str(event.pin_num))\n\ncad = pifacecad.PiFaceCAD()\ncad.lcd.write(\"Just a test\")\nlistener = pifacecad.SwitchEventListener(chip=cad)\nfor i in range(8):\n    listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text)\nlistener.activate()\n\nif msg['payload'] == 'abort':\n    node.warn('aborting listener')\n    listener.deactivate()","outputs":1,"x":740,"y":480,"wires":[["b2d81da9.0f60a"]]},{"id":"28c4c4cf.eb5c6c","type":"inject","z":"ae89c6f4.025a58","name":"","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":580,"y":480,"wires":[["13305b2.f5872a5"]]},{"id":"b2d81da9.0f60a","type":"debug","z":"ae89c6f4.025a58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":920,"y":480,"wires":[]}]

The python code in the python function node:

import pifacecad

def update_pin_text(event):
    event.chip.lcd.set_cursor(13, 0)
    event.chip.lcd.write(str(event.pin_num))
    node.send("You pressed button:"+str(event.pin_num))

cad = pifacecad.PiFaceCAD()
cad.lcd.write("Just a test")
listener = pifacecad.SwitchEventListener(chip=cad)
for i in range(8):
    listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text)
listener.activate()

if msg['payload'] == 'abort':
    node.warn('aborting listener')
    listener.deactivate()

Thanks very much for that.

That post of mine was more rhetorical to you. I was just thinking aloud of the possibility.
But I have imported it and it does seem to work.

I'll now sit down and teak bits of the code.

I'll say I know notihing about python.

When I read the code I try to simply break it down into blocks which I try to not look into/at.

(This is again me just thinking aloud, so please don't take it as I am unhappy with what you did)

The original bit of code which was an external file:
That was a lot bigger.

The new code is (therefore) a lot smaller than the original.
It doesn't have MQTT. No problem.
It only seems to work once.
The other code kept running, and although the node says running I'm not sure it is.

I implement MQTT, I put in the line:
import paho.mqtt.client as mqtt
and then added:
send_mqtt_message("PIFACE_BUTTON", str(event.pin_num))
in the bit which shows what button is pressed. But noting is sent.
(Probably because I haven't really connected to MQTT.)

Which I guess are there lines:

def on_connect(client, obj, flags, rc):
    client.subscribe("PIFACE_CMDS", 0)


def on_subscribe(client, userdata, mid, granted_qos):
    print ('Subscribed:', userdata, mid, granted_qos)
    print ('We are here, waiting for commands...')

I'll keep plodding away at it.

But - going way off topic:
Are you good at understanding how node-code works (I guess that means JS) also?
Someone else wrote some code for the LCD which works, but it is missing what I think is a really basic feature which is being faked in how you send it messages.

I won't go on at this stage. I think I have enough to deal with just now getting the MQTT working in the new code you posted.

Andrew, don't waste time on the mqtt stuff in the code while using the python function node. It is not needed in that case, we are in NR, we will just send out what we need from the node and then to a mqtt out node. One reason this code is shorter than the external script

Another reason, I just wanted to get a confirmation that you could communicate with the piface from the python function node. Just to clarify, you wrote it worked once. Did you see the text "Just a test" on the lcd? Did any button work but just once? If so there is hope, then it is worth adding more code to make it work as we want

I think the text "Running" is just indicating that the python code syntax is correctly written

Regarding coding nodes, no, that is not on my agenda. I made some changes to some nodes but in general, I have no plans. If a node author lost interest or appetite there must be a reason behind. Limited time, technical problems, lost faith

Of course! Silly me.

I have got the original code working great. It accepts multiple button presses.

On the last part about node coding.
3 hours of head bashing I found and fixed the problem.
An erroneous = in the code made something not work as expected.

A bit unclear to me what you use, the original python script I wrote, the 3 hours head bashing node (JS) fix. Or what?

You never confirmed if you saw that message on the lcd ("Just a test") when you tried the python code in the python node ???

Sorry.

Your code works.

The original works fine. The newer bit works also, I was just messing around trying to make it a complete replacement for the older external file. (Keeping it all in NR)

one of my other (many) problems is the node which does the PiFaceCAD LCD.
It works. It requires a prefix of 1,1: (or something like that) to position where on the display the message is printed.

Alas out of the box it only works with 1,1:
Putting anything else, like 2,2: it is printed at 1,1:
I mentioned this to the author but it isn't on their priority list.
Which is strange because if you don't put the 1,1: the node throws an error.
My suggestion was if there isn't a valid prefix it defaults to 1,1: Again: Little interest.

So, I was messing around with that - which is why I am needing/wanting to get the buttons to work.
Button presses put messages at different places on the screen.

I decided today to put my effort to trying to work out why this other node isn't/wasn't working out of the box as claimed.

3 hours. Painful hours. I found the problem and fixed it. Now I can send messages to be printed anywhere on the screen with a working prefix.