Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simple, fast program for interacting with the Raspberry Pi Sense HAT's joystick with the Philips Hue REST API
# A simple program to use the joystick of the Sense HAT with the Philips Hue's REST API
from sense_hat import SenseHat
from evdev import InputDevice, ecodes,list_devices
from select import select
import random
import time
import os
# Setting min and max brightness settings
MIN_BRI=0
MAX_BRI=254
#Min and Max Hue Values
MIN_HUE=0
MAX_HUE=65535
sense = SenseHat()
devices = [InputDevice(fn) for fn in list_devices()]
for dev in devices:
if dev.name == "Raspberry Pi Sense HAT Joystick":
js = dev
# How you would work with the API with Linux
curl_string = 'curl -X PUT 192.168.1.100/api/iusiadahgohpesoopiaheephiehohnaebeinaenejait/lights/5/state -d \'{"on":true, "sat":254, "bri":%d,"hue":%d}\''
bri_val = 254
hue_val = 0
while True:
r, w, x = select([dev.fd], [], [],0.01)
for fd in r:
for event in dev.read():
if event.type == ecodes.EV_KEY and event.value == 1:
if event.code == ecodes.KEY_UP:
if bri_val > 234:
bri_val = MAX_BRI
else:
bri_val = bri_val + 20
os.system(curl_string % (bri_val, hue_val))
elif event.code == ecodes.KEY_DOWN:
if bri_val < 20:
bri_val = MIN_BRI
else:
bri_val = bri_val - 20
os.system(curl_string % (bri_val, hue_val))
elif event.code == ecodes.KEY_LEFT:
if hue_val < 2000:
hue_val = MIN_HUE
else:
hue_val = hue_val - 2000
os.system(curl_string % (bri_val, hue_val))
elif event.code == ecodes.KEY_RIGHT:
if hue_val > 63535:
hue_val = MAX_HUE
else:
hue_val = hue_val + 2000
os.system(curl_string % (bri_val, hue_val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment