Re: GUI for display management
Posted: Sun Nov 08, 2015 1:00 pm
I followed this guide https://github.com/mrichardson23/rpi-kivy-screen
A small, affordable computer with free resources to help people learn, make things, and have fun
https://www.raspberrypi.org/forums/
https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=124277
So, try moving the config import and config.set lines to the very top of your script.Applying configurations
-----------------------
Configuration options control the initialization of the :class:`~kivy.app.App`.
In order to avoid situations where the config settings do not work or are not
applied before window creation (like setting an initial window size),
... Config.set ... should be used before
importing any other Kivy modules. Ideally, this means setting them right at
the start of your main.py script.
In "~/.kivy/" directory, i have only the following items:
Seems like the Config.set isn't registering. The solution is a bit of a hack I'm afraid, you just need to edit the config.ini file directly.
The file should be at ~/.kivy/config.ini
There will be height and width lines in there.
I've tried to move he config import and config.set lines to the very top of my script, but it still doesn't work!So, try moving the config import and config.set lines to the very top of your script.
Code: Select all
extensions/
icon/
logs/
mods/
config.iniCode: Select all
[kivy]
keyboard_repeat_delay = 300
keyboard_repeat_rate = 30
log_dir = logs
log_enable = 1
log_level = info
log_name = kivy_%y-%m-%d_%_.txt
window_icon =
keyboard_mode =
keyboard_layout = qwerty
desktop = 1
exit_on_escape = 1
pause_on_minimize = 0
config_version = 14
[graphics]
display = -1
fullscreen = 1 <<<==I've triedeto set this parameter
height = 200 <<<==I've triedeto set this parameter
left = 0
maxfps = 60
multisamples = 2
position = auto
rotation = 0
show_cursor = 1
top = 0
width = 100 <<<==I've triedeto set this parameter
resizable = 0 <<<==I've triedeto set this parameter
borderless = 0
window_state = visible
minimum_width = 0
minimum_height = 0
[input]
mouse = mouse
%(name)s = probesysfs,provider=hidinput
[postproc]
double_tap_distance = 20
double_tap_time = 250
ignore = []
jitter_distance = 0
jitter_ignore_devices = mouse,mactouch,
retain_distance = 50
jitter_ignore_devices = mouse,mactouch,
retain_distance = 50
retain_time = 0
triple_tap_distance = 20
triple_tap_time = 375
[widgets]
scroll_timeout = 250
scroll_distance = 20
scroll_friction = 1.
scroll_stoptime = 300
scroll_moves = 5
[modules]
Code: Select all
print Config.get("graphics", "height")
print Config.get("graphics", "width")Code: Select all
#!/usr/bin/env python
import kivy
kivy.require('1.9.0')
from kivy.config import Config
Config.set('graphics', 'width', '250')
Config.set('graphics', 'height', '150')
# le righe di config e relativi import,devono essere in testa allo script
# se l'impostazioni di dimensione dello scherma non ha effetto,
# impostarlo dal file config.ini in /home/pi/.kivy/config.ini
# MainApp.py
from threading import Thread
from time import sleep
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from glob import glob
from os.path import join, dirname
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from random import randint
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.image import Image
import RPi.GPIO as gpio
# This is a worker class that will work in a separate thread.
class TempWorker(Thread):
def __init__(self, callback):
super(TempWorker, self).__init__()
# Save reference to our callback function
self.callback = callback
# Initialise GPIO here
gpio.setmode(gpio.BCM)
gpio.setup(4, gpio.IN, pull_up_down=gpio.PUD_DOWN)
def run(self):
# The "run" method is called when a thread is started
# Initialise your variables
numero=0
pulse_cnt_high=0
pulse_cnt_low=0
max_pulse_cnt=1
pulse_status=pulse_status_old=0
# Start your loop
while True:
if gpio.input(4):
pulse_cnt_low=0
if (pulse_cnt_high<max_pulse_cnt):
pulse_cnt_high=pulse_cnt_high+1
else:
pulse_status=1
pulse_cnt_high=0
else:
pulse_cnt_high=0
if (pulse_cnt_low<max_pulse_cnt):
pulse_cnt_low=pulse_cnt_low+1
else:
pulse_status=0
pulse_cnt_low=0
# Send the value of pulse_status to our callback
self.callback(pulse_status)
# Sleep for 100ms
sleep(0.1)
class Screen(FloatLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
layout = BoxLayout(orientation='horizontal', spacing=3)
#il parametro spacing determina la distanza tra i due box
tx = '[b][size=45][color=ff3333]The current value of pulse_status is:[/color][/size][/b]'
self.lbl = Label(
text=tx,
markup=True,
size_hint=(.4, 1),
pos_hint={"center_x":.5, "center_y":.7})
carousel = Carousel(direction='right', size_hint=(.6, 1))
carousel.loop = True
curdir = dirname(__file__)
for filename in glob(join(curdir, '/home/pi/myfolder/img', '*')):
picture = Image(source=filename,allow_stretch=True)
carousel.add_widget(picture)
Clock.schedule_interval(carousel.load_next, 5)
layout.add_widget(carousel)
layout.add_widget(self.lbl)
self.add_widget(layout)
# Create the thread object and pass the reference to our callback
# function
worker = TempWorker(callback=self.update_label_text)
# Daemonising the thread means that when you exit Kivy, this thread
# will also be killed
worker.daemon = True
# Start the thread
worker.start()
def update_label_text(self, val):
# Update our label text
self.lbl.text = "Current value: {}".format(val)
class MainApp(App):
def build(self):
#imposto il colore dello sfondo
Window.clearcolor = (255, 255, 255, 1)
#Window.size=(500, 500)
return Screen()
MainApp().run()
Code: Select all
worker = TempWorker(callback=self.update_label_text)Code: Select all
self.callback(pulse_status)Code: Select all
def update_label_text(self, val):
# Update our label text
self.lbl.text = "Current value: {}".format(val)Code: Select all
self.lbl.text = "Current value: {}".format(val)Code: Select all
# MainApp.py
from threading import Thread
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from glob import glob
from os.path import join, dirname
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from random import randint
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.image import Image
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
n = 0
def my_callback(channel):
global n
print "rising detected on GPIO 4"
n=n+1
print "n=",n
return n
GPIO.add_event_detect(4, GPIO.RISING, callback=my_callback, bouncetime=500)
class TempWorker(Thread):
def __init__(self, callback):
super(TempWorker, self).__init__()
# Save reference to our callback function
self.callback = callback
def run(self):
# The "run" method is called when a thread is started
# Send the value of global n to our callback
self.callback(n)
class Screen(FloatLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
layout = BoxLayout(orientation='horizontal', spacing=3)
#il parametro spacing determina la distanza tra i due box
tx ='[b][size=45][color=ff3333]The value of n is \n\[/color][/size][/b]'
self.lbl = Label(
text=tx,
markup=True,
size_hint=(.4, 1),
pos_hint={"center_x":.5, "center_y":.7})
carousel = Carousel(direction='right', size_hint=(.6, 1))
carousel.loop = True
curdir = dirname(__file__)
for filename in glob(join(curdir, '/home/pi/mufolder/img', '*')):
picture = Image(source=filename,allow_stretch=True)
carousel.add_widget(picture)
Clock.schedule_interval(carousel.load_next, 5)
layout.add_widget(carousel)
layout.add_widget(self.lbl)
self.add_widget(layout)
# Create the thread object and pass the reference to our callback
# function
worker = TempWorker(callback=self.update_label_text)
# Daemonising the thread means that when you exit Kivy, this thread
# will also be killed
worker.daemon = True
# Start the thread
worker.start()
def update_label_text(self, val):
print "update_label_text called and n=", n
# Update our label text
self.lbl.text = "Current ue: {}".format(val)
class MainApp(App):
def build(self):
#imposto il colore dello sfondo
Window.clearcolor = (255, 255, 255, 1)
#Window.size=(500, 500)
return Screen()
MainApp().run()
yes, it can be. In this moment I want only do the following:If it can be handled via the add_event_detect method then we can probably do this without threading. You would then just get your callback to add a value to n and update the label.
[/quote]ll be home in a few hours and will post some updated code for you.
Perfect - no need for my separate worker thread.paolojo wrote:yes, it can be. In this moment I want only do the following:
every time that GPIO 4 rises (positive edge), i increase the counter "n" (n=n+1) and I update the label with this value.
Code: Select all
# MainApp.py
from random import randint
from glob import glob
from os.path import join, dirname
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
import RPi.GPIO as GPIO
class Screen(FloatLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
# Here's your GPIO stuff
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self.n = 0
# Add a call function
GPIO.add_event_detect(4,
GPIO.RISING,
callback=self.my_callback,
bouncetime=500)
layout = BoxLayout(orientation='horizontal', spacing=3)
#il parametro spacing determina la distanza tra i due box
# Create a template for your text
self.tx ='[b][size=45][color=ff3333]The value of n is {n}[/color][/size][/b]'
self.lbl = Label(text=self.tx.format(n=self.n),
markup=True,
size_hint=(.4, 1),
pos_hint={"center_x":.5, "center_y":.7})
carousel = Carousel(direction='right', size_hint=(.6, 1))
carousel.loop = True
curdir = dirname(__file__)
for filename in glob(join(curdir, '/home/pi/mufolder/img', '*')):
picture = Image(source=filename,allow_stretch=True)
carousel.add_widget(picture)
Clock.schedule_interval(carousel.load_next, 5)
layout.add_widget(carousel)
layout.add_widget(self.lbl)
self.add_widget(layout)
def my_callback(self, channel):
print "rising detected on GPIO 4"
self.n += 1
# Update our template with the current value of n
tx = self.tx.format(n=self.n)
# Print it
print tx
# Update the text on the label.
self.lbl.text = tx
class MainApp(App):
def build(self):
#imposto il colore dello sfondo
Window.clearcolor = (255, 255, 255, 1)
#Window.size=(500, 500)
return Screen()
MainApp().run()
Code: Select all
def my_callback(self, channel):
print "rising detected on GPIO 4"
self.n += 1
# Update our template with the current value of n
tx = self.tx.format(n=self.n)
# Print it
print tx
# Update the text on the label.
self.lbl.text = tx
. #CALL NEW FUNCTION
. new_script.new_function()