GPIO to I2C to OSC

Greetings to you all :slight_smile:

I’m trying to setup a GPIO via I2C to OSC supervised by a systemd service. The GPIO→I2C is assured by the 16 in|out PCF8575→I2C chip. It is directly connected to the expansion port right in the front of the zynthian V5.

Python seems a good way to go about this. The pcf8575 module is not available on a vanilla zynthian install so I tried : pip3 install pcf8575 and got a warning about using venv (which of course is a good idea) but then my question is : what would be the recommended way to go about this ? The goal I suppose would be for the service to be started in a venv ? Can any of you shed some light on good practice on this zynthian os ?

e.g. : Should this service be run by the zynthian user (or root (:scream:))?

Even before any modification on the zynthian, the command :

i2cdetect -y 1

returns the presence of two devices at address 0x20 and 0x21 respectively.

Q: Are there already some devices on the I2C bus ?

Anywho, I’ve changed my device to address 0x26 and is indeed displayed correctly :slight_smile:

Indeed, I believe the whole “control panel” is connected via I2C.

So, this works quite well :

#!/home/zynthian/gpio-i2c-osc/venv/bin/python3

import os
import socket
import pyliblo3 as liblo
from pcf8575 import PCF8575

os.nice(20)

hostname = socket.gethostname()
# osc_destination = "localhost"
osc_destination = "10.42.0.1"
osc_port = "57120"
osc_addr = liblo.Address(osc_destination, osc_port)

pcf_i2c_port = 1
pcf_address = 0x26
pcf = PCF8575(pcf_i2c_port, pcf_address)
pcf_last_state = list(reversed(pcf.port))
pcf_state = list(reversed(pcf.port))

def send_osc(gpio, value):
    path = "/{}/gpio".format(hostname)
    print(path, gpio, value)
    liblo.send(osc_addr, path, gpio, value)

while True:
    pcf_state = list(reversed(pcf.port))
    # print(pcf_state)
    for i in range(0,len(pcf_state)):
        if pcf_state[i] != pcf_last_state[i]:
            send_osc(i, not(pcf_state[i]))
    pcf_last_state = pcf_state

Now I need a systemd service.

This seems to work.
ln -s /home/zynthian/gpio-i2c-osc/gpio-i2c-osc.service /etc/systemd/system/
systemctl enable gpio-i2c-osc.service
rebooted
and everything works. The service file contains :

[Unit]
Description=PCF8575 GPIO to OSC

After=systemd-modules.load.service
Before=sysvinit.target
ConditionPathExists=/sys/class/i2c-adapter

[Service]
ExecStart=/usr/bin/nice -n 19 /home/zynthian/gpio-i2c-osc/gpio-i2c-osc.py
Restart=on-failure

[Install]
WantedBy=multi-user.target
1 Like

What have I done?! :scream:

Your branch and 'origin/vangelis' have diverged
https://termbin.com/a6yb

I (think I) never touched anything while adding this script service unit… or have I ?

How can I resolve this without breaking anything ?

Do you guys think I could just git reset --hard in these repository /zynthian/zynthian-{ui,sys,webconf,data} ? I swear, I haven’t done any shenanigans… no idea how I got there. Very weird. I do not know how the Zynthian updates itself and of course, it does its load of git incantations during that.

What does say “git diff”? :wink:

I re based and that sorted it out.

Nothing in all repositories.

But that would assume I do have diverging (div. from vangelis) commits that I want to keep and go forward like this but it is not my case. I (suppose I) want a vanilla (unaltered) vangelis repo.

Anywho, I did the very risky git reset --hard origin/vangelis on these repositories /zynthian/zynthian-{ui,sys,webconf,data} followed by an update, rebooted and… everything went fine, all is fine. :relieved:

2 Likes

git is SO in need of a replacement.

It get’s almost sentient in it’s obfuscation.