Hi All
I can get the touch module working in Python mainly because I am working off the example.
However, without and example my Python is currently not strong enough to figure out how to get the slide and dial working.
I would like to pass the values from the dial to the brightness setting for the unicorn hat, and pass the slider value as colour. So basically I can control the unicorn hat colour and brines with the slider and dial.
On an related note is there anywhere with a list of the unicorn hat Python module commands. I want to turn all the pixels on at once and I thought the command was unicorn.set_all(r,g,b) but it doesnât work.
Thanks in advance.
set_all(r,g,b)
should work, but youâll need to follow it up with a unicorn.show()
.
As for using dial, could you post your existing code? Use backticks to keep it formatted like so:
```
Your code here
```
Reading Dial/Slider should be fairly straightforward, since they both have a position
method which returns their position, you can see it here: https://github.com/pimoroni/flotilla-python/blob/master/library/flotilla/dial.py
Depending on your code and setup, you might grab it like so:
client.first(flotilla.Dial).position()
Or use another approach.
Hi gadgetoid
I had a play last night and I have code that reads the dial and slider positions:
import flotilla
import unicornhat as unicorn
client = flotilla.Client(
requires={
'one': flotilla.Slider,
'two': flotilla.Dial
}
)
dial = client.first(flotilla.Dial)
slider = client.first(flotilla.Slider)
dial_val = int(dial.data[0])
print dial_val
slider_val = int(slider.data[0])
print slider_val
Obviously this only reads the values once, if I put it in a while True loop it keeps reading constantly and changes when I move the dial and slider.
I have two problems now:
- how to I pass these readings into the unicorn.brightness() function so I can set the brightness with the dial or slider
- unicorn.set_all(r,g,b) is still not working for me, it keep coming up with an AttributeError: âmoduleâ object has no attirbute set_all even in simple code such as:
import unicornhat as unicorn
unicorn.brightness (0.2)
unicorn.set_all (255,0,0)
unicorn.show()
I have tried to reinstall the unicornhat library with no success.
Thanks
1 Like
Looks like unicorn.set_all
hasnât made it into a release yet! Iâll need to fix that.
For the time being you can implement it yourself like so:
def set_all(r, g, b):
size_x, size_y, = unicorn.get_shape()
for x in range(size_x):
for y in range(size_y):
unicorn.set_pixel(x, y, r, g, b)
And then just call set_all(r, g, b)
To get the value from slider into unicorn.brightness()
youâll need to do some value scaling. brightness
ranges from 0.0
to 1.0
and (if I remember correctly) Dial returns 0
to 1023
. The scaling is then simply:
brightness_val = dial_val / 1023.0
In a loop you could plonk:
unicorn.brightness(client.first(flotilla.Dial).position / 1023.0)
Although itâs good practise to check and see client.first(flotilla.Dial)
actually returns a valid dial :D
Ok you will have to excuse the crudeness of the code here but I have at least something that works. When you move the flotilla slider it changes the colour of the unicornhat and when the change the dial the brightness goes up and down. The code is below.
If there is a better way of implementing the colour change then it would be great to learn about it.
import flotilla
import unicornhat as unicorn
client = flotilla.Client(
requires={
'one': flotilla.Slider,
'two': flotilla.Dial
}
)
def set_all(r, g, b):
size_x, size_y = unicorn.get_shape()
for x in range(size_x):
for y in range(size_y):
unicorn.set_pixel(x, y, r, g, b)
try:
while True:
unicorn.brightness(client.first(flotilla.Dial).position / 1023.0)
print client.first(flotilla.Dial).position / 1023.0
if client.first(flotilla.Slider).position >= 900:
set_all(255, 0, 0)
unicorn.show()
print client.first(flotilla.Slider).position
elif 600 <= client.first(flotilla.Slider).position <= 899:
set_all(255, 127, 0)
unicorn.show()
print client.first(flotilla.Slider).position
elif 400 <= client.first(flotilla.Slider).position <= 599:
set_all(255, 255, 0)
unicorn.show()
print client.first(flotilla.Slider).position
elif 200 <= client.first(flotilla.Slider).position <= 399:
set_all(0,255,0)
unicorn.show()
print client.first(flotilla.Slider).position
elif 50 <= client.first(flotilla.Slider).position <= 199:
set_all(0, 0, 255)
unicorn.show()
print client.first(flotilla.Slider).position
else:
client.first(flotilla.Slider).position == 0
set_all(0, 0, 0)
unicorn.show()
except KeyboardInterrupt:
client.stop()
print slider_val
Thanks
Great start. You should definitely now read my post here: Generating random colours (unicornhat)
And this: https://learn.pimoroni.com/tutorial/unicorn-hat/making-rainbows-with-unicorn-hat
To acquaint yourself with colour space conversion :D (It sounds harder than it is)
Brilliant thank you very much. Here is my new code using the colorsys module much cleaner.
import flotilla
import unicornhat as unicorn
import colorsys
client = flotilla.Client(
requires={
'one': flotilla.Slider,
'two': flotilla.Dial
}
)
def set_all(r, g, b):
size_x, size_y = unicorn.get_shape()
for x in range(size_x):
for y in range(size_y):
unicorn.set_pixel(x, y, r, g, b)
try:
while True:
unicorn.brightness(client.first(flotilla.Dial).position / 1023.0)
print client.first(flotilla.Dial).position / 1023.0
h = client.first(flotilla.Slider).position / 1023.0
r, g, b = [int(c*255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
print r, g, b
set_all(r, g, b)
unicorn.show()
except KeyboardInterrupt:
client.stop()
Just a quick question this work great, but when I remove the print functions from the while true loop it no longer works, why is this?
My best guess is that the âcostâ of writing to the terminal with print
is actually standing in as a delay, slowing down your code and preventing it from starving the Flotilla internals of resources.
You should import time
and replace the print
statements with time.sleep(0.01)
Yep time.sleep(0.01) worked a treat. Thanks.
Now to think of something else to add to the mix.
For anyone at all interested I have update my code above to work with the new unicornhathd.
import flotilla
import unicornhathd
import colorsys
import time
client = flotilla.Client(
requires={
'one': flotilla.Slider,
'two': flotilla.Dial
}
)
def set_all(r, g, b):
size_x, size_y = unicornhathd.get_shape()
for x in range(size_x):
for y in range(size_y):
unicornhathd.set_pixel(x, y, r, g, b)
try:
while True:
unicornhathd.brightness(client.first(flotilla.Dial).position / 1023.0)
time.sleep(0.01)
h = client.first(flotilla.Slider).position / 1023.0
r, g, b = [int(c*255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
time.sleep(0.01)
set_all(r, g, b)
unicornhathd.show()
except KeyboardInterrupt:
unicornhathd.clear()
unicornhathd.show()
client.stop()
Enjoy all the beautiful lightsâŚ
Pimoroni how did the month of Flotilla go any new bits of software or updates coming?