Variables on touch phat

Hi,
I use touch phat with scroll phat. Touch send command to scroll for display differents datas…
Everything is fine, and so beautiful, with a low consumption… I love it.

I would like to display data wich is saving in a file from a serial connection.
But I don’t success. The “event” seems not able to use a variable from a file that I have to open in the script…
I have tried to use a threading for initiate the variable out of the button_action… without success…

If somebody can tell me if what I want is possible…
And maybe a solution…
Thanks.
PS: my script:

Blockquote
#!/usr/bin/env python

import signal
import time
import sys
import subprocess
from sys import exit
from gpiozero import CPUTemperature
import touchphat
import scrollphat
import threading
import pickle
import numpy as np

scrollphat.set_brightness(2)
scrollphat.set_rotate(True)

def flag():
#time.sleep(3)
event.set()
#print(“event is clear”)
#event.clear()
for pad in [‘Back’,‘A’,‘B’,‘C’,‘D’,‘Enter’]:
touchphat.set_led(pad, True)
time.sleep(0.1)
touchphat.set_led(pad, False)
time.sleep(0.1)

def start_scroll():
event.wait()
while event.is_set():

#print('starting random integer task')
#x = np.random.randint(1,30)
#time.sleep(1)
  def string_to_bcd(digit):

    bcd_digit = bin(int(digit))[2:]
    return ('00000' + bcd_digit)[-5:]


  def plot_digit(digit, position):

    bcd_digit = string_to_bcd(digit)
    for y in range(0, 5, 1):
        scrollphat.set_pixel(position, y, int(bcd_digit[y]) == 1)

  try:
      current = time.strftime('%H0%M0%S')
      for x in range(0, 8):
          plot_digit(current[x], x)
      for i in range(0, 5):
          scrollphat.set_pixel(10, i, (5 - i) <= ((int(current[3:5])) / 10))
      scrollphat.update()
      time.sleep(0.5)
  except KeyboardInterrupt:
      scrollphat.clear()
      sys.exit(-1)

def start_touch():
event.wait()
while event.is_set():
###touche A pour affichage heure
@touchphat.on_release(‘A’)
def handle_touch(event):
scrollphat.clear()
dati_now = time.strftime(" %H:%M ")
scrollphat.write_string(dati_now, 11)
length = scrollphat.buffer_len()
for i in range(length):
try:
scrollphat.scroll()
time.sleep(0.1)
except KeyboardInterrupt:
scrollphat.clear()
sys.exit(-1)

###touche B pour lancer affichage date
@touchphat.on_release('B')
def handle_touch(event):
  scrollphat.clear()
  date_now = time.strftime("  %m/%d  ")
  scrollphat.write_string(date_now, 11)
  length = scrollphat.buffer_len()
  for i in range(length):
    try:
        scrollphat.scroll()
        time.sleep(0.1)
    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

###touche C pour lancer affichage temperature cpu
@touchphat.on_release('C')
def handle_touch(event):
  scrollphat.clear()
  cpu = CPUTemperature()
  tempe = str(cpu)
  cpuu = tempe[44:48]
  cpuuu = float(cpuu)
  message = "   " + cpuu + "C   "
  scrollphat.write_string(message, 11)
  length = scrollphat.buffer_len()
  for i in range(length):
    try:
        scrollphat.scroll()
        time.sleep(0.1)
    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

###touche D 
#@touchphat.on_release('D')
#def handle_touch(event):
    
###touche return pour arreter l'ordi
@touchphat.on_release('Back')
def handle_touch(event):
  subprocess.check_call(["sudo shutdown +1"], shell=True)

###touche return pour sortir de la boucle ou du script
#@touchphat.on_release('Enter')
#def handle_touch(event):

signal.pause()

event = threading.Event()
t1 = threading.Thread(target = flag)
t2 = threading.Thread(target = start_touch)
t3 = threading.Thread(target = start_scroll)

t1.start()
t2.start()
t3.start()

#print(“event has been cleared, random operation stop”)

Blockquote

Hi,
I have found the solution…
I didn’t known that it produce that when I write “text”… Sorry for presentation of my first post…
Below the solution with a normal presentation ( I hope)…
indent preformatted text by 4 spaces`#!/usr/bin/env python

import signal
import time
import sys
import subprocess
from sys import exit
from gpiozero import CPUTemperature
import touchphat
import scrollphat
import threading
import pickle
import numpy as np

scrollphat.set_brightness(2)
scrollphat.set_rotate(True)

def ouverture_donnees():
with open("/home/pi/scripts/scripts-demarrage/double/liste-capteurs", ‘rb’) as fichier:
liste_pre = pickle.Unpickler(fichier)
liste = liste_pre.load()
listar = np.array(liste[2], dtype=“i”)
listour = listar[-1]
listour = str(listour)
global listour

def flag():
#time.sleep(3)
event.set()
#print(“event is clear”)
#event.clear()
for pad in [‘Back’,‘A’,‘B’,‘C’,‘D’,‘Enter’]:
touchphat.set_led(pad, True)
time.sleep(0.1)
touchphat.set_led(pad, False)
time.sleep(0.1)

def start_scroll():
event.wait()
while event.is_set():

#print('starting random integer task')
#x = np.random.randint(1,30)
#time.sleep(1)
  def string_to_bcd(digit):

    bcd_digit = bin(int(digit))[2:]
    return ('00000' + bcd_digit)[-5:]


  def plot_digit(digit, position):

    bcd_digit = string_to_bcd(digit)
    for y in range(0, 5, 1):
        scrollphat.set_pixel(position, y, int(bcd_digit[y]) == 1)

  try:
      current = time.strftime('%H0%M0%S')
      for x in range(0, 8):
          plot_digit(current[x], x)
      for i in range(0, 5):
          scrollphat.set_pixel(10, i, (5 - i) <= ((int(current[3:5])) / 10))
      scrollphat.update()
      time.sleep(0.5)
  except KeyboardInterrupt:
      scrollphat.clear()
      sys.exit(-1)

def start_touch():
event.wait()
while event.is_set():
###touche A pour affichage heure
@touchphat.on_release(‘A’)
def handle_touch(event):
scrollphat.clear()
dati_now = time.strftime(" %H:%M ")
scrollphat.write_string(dati_now, 11)
length = scrollphat.buffer_len()
for i in range(length):
try:
scrollphat.scroll()
time.sleep(0.1)
except KeyboardInterrupt:
scrollphat.clear()
sys.exit(-1)

###touche B pour lancer affichage date
@touchphat.on_release('B')
def handle_touch(event):
  scrollphat.clear()
  date_now = time.strftime("  %m/%d  ")
  scrollphat.write_string(date_now, 11)
  length = scrollphat.buffer_len()
  for i in range(length):
    try:
        scrollphat.scroll()
        time.sleep(0.1)
    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

###touche C pour lancer affichage temperature cpu
@touchphat.on_release('C')
def handle_touch(event):
  scrollphat.clear()
  cpu = CPUTemperature()
  tempe = str(cpu)
  cpuu = tempe[44:48]
  cpuuu = float(cpuu)
  message = "   " + cpuu + "C   "
  scrollphat.write_string(message, 11)
  length = scrollphat.buffer_len()
  for i in range(length):
    try:
        scrollphat.scroll()
        time.sleep(0.1)
    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

###touche D 
@touchphat.on_release('D')
def handle_touch(event):
  ouverture_donnees()
  scrollphat.write_string(listour, 11)
  length = scrollphat.buffer_len()
  for i in range(length):
    try:
        scrollphat.scroll()
        time.sleep(0.1)
    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)


###touche return pour arreter l'ordi
@touchphat.on_release('Back')
def handle_touch(event):
  subprocess.check_call(["sudo shutdown +1"], shell=True)

###touche enter pour sortir de la boucle ou du script
@touchphat.on_release('Enter')
def handle_touch(event):
  sys.exit(-1)

signal.pause()

event = threading.Event()
t1 = threading.Thread(target = flag)
t2 = threading.Thread(target = start_scroll)
t3 = threading.Thread(target = start_touch)

t1.start()
t2.start()
t3.start()`

It works for display variables coming from other serial port on scroll phat and commanded by touch phat.

The result is beautifull!!!
Maybe I can make a project with a video if somoene ask…
Thanks for your attention,
Have a fun.