You can- you just must approach the problem properly. See the MQTT server in progress that I linked above as an example.
You need one Python program to interface with Blinkt!, and then your other programs talk via that. This could be achieved via MQTT, sockets, maybe even SIGUSR signals, HTTP and maybe even using 8 unused GPIO pins as “registers”.
Here’s a really quick and dirty example of the latter (possibly the simplest) way to accomplish this.
It uses GPIO pins 5, 6, 7, 8, 9, 10, 11, 12 as 8-bits of shared memory, that’s 8 distinct on/off states which can be shared between multiple Python applications.
To provide this functionality, we’ll start with pins.py
:
import RPi.GPIO as GPIO
PINS = [5,6,7,8,9,10,11,12]
GPIO.setmode(GPIO.BCM)
GPIO.setup(PINS, GPIO.OUT, pull_up_down=GPIO.PUD_OFF)
def write_single(index, value):
GPIO.output(PINS[index], value)
def read_single(index):
return GPIO.input(PINS[index])
def read():
value = 0
for pin in PINS:
value <<= 1
value |= GPIO.input(pin)
return value
def write(value):
value &= 0xff
for pin in PINS:
GPIO.output(pin, value & 0b10000000)
value <<= 1
Now we need to read these 8-bits and do something meaningful with them, here’s server.py
. This server runs constantly and updates Blinkt! by lighting the pixels that correspond to each set bit:
import time
import blinkt
import pins
FPS = 1.0 / 60
while True:
blinkt.clear()
value = pins.read() # Read BCM 5, 6, 7 etc
for x in range(8):
if value & 1:
blinkt.set_pixel(x,128,0,0)
value >>= 1
blinkt.show()
time.sleep(FPS)
And finally we want some clients to set these bits so the server has something to do, here’s client.py
setting just one bit:
import pins
import time
while True:
pins.write_single(0, 1) # Write bit 0 high, this sets BCM5 high
time.sleep(0.5)
pins.write_single(0, 0) # Write bit 0 low, this sets BCM5 low
time.sleep(0.5)
If you run server.py in one Terminal session, and client.py in another the result should be one blinking red LED on your Blinkt!