Advertisement
TotteL

Sensor

Jan 26th, 2017
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
  2. import time
  3. from signal import pause
  4.  
  5. x = 3
  6. y = 3
  7.  
  8. sense = SenseHat()
  9. sense.set_rotation(180)
  10.  
  11. while True:
  12.     t = sense.get_temperature()
  13.     p = sense.get_pressure()
  14.     h = sense.get_humidity()
  15.  
  16.     t = round(t, 1)
  17.     p = round(p, 1)
  18.     h = round(h, 1)
  19.  
  20.     if t > 18.3 and t < 26.7:
  21.         bg = [0, 100, 0]  # green
  22.     else:
  23.         bg = [100, 0, 0]  # red
  24.  
  25.     msgt = "TMP: %sC" % (t)
  26.     msgp = "P: %sbar" % (p)
  27.     msgh = "HUM: %s%%rH" % (h)
  28.  
  29. def clamp(value, min_value=0, max_value=7):
  30.     return min(max_value, max(min_value, value))
  31.  
  32. def pushed_up(event):
  33.     global y
  34.     if event.action != ACTION_PRESSED:
  35.         sense.show_message(msgt, scroll_speed=0.08, back_colour=bg)
  36.         y = clamp(y + 1)
  37.  
  38. def pushed_down(event):
  39.     global y
  40.     if event.action != ACTION_PRESSED:
  41.         sense.show_message(msgp, scroll_speed=0.08, back_colour=bg)
  42.         y = clamp(y - 1)
  43.  
  44. def pushed_left(event):
  45.     global x
  46.     if event.action != ACTION_PRESSED:
  47.         sense.show_message(msgh, scroll_speed=0.08, back_colour=bg)
  48.         x = clamp(x + 1)
  49.  
  50. def pushed_right(event):
  51.     global x
  52.     x = clamp(x - 1)
  53.  
  54.     def refresh():
  55.         sense.clear()
  56.         sense.set_pixel(x, y, 255, 255, 255)
  57.  
  58. sense.stick.direction_up = pushed_up
  59. sense.stick.direction_down = pushed_down
  60. sense.stick.direction_left = pushed_left
  61. sense.stick.direction_right = pushed_right
  62. sense.stick.direction_any = refresh
  63. refresh()
  64. pause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement