Advertisement
Guest User

edoardo il nabbo

a guest
Mar 13th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. '''
  2.  
  3. Created by Edoardo La Greca
  4.  
  5. '''
  6.  
  7. from sense_emu import SenseHat
  8. from time import sleep
  9.  
  10. s = SenseHat()
  11.  
  12. # Colori
  13. red = (255, 0, 0)       # Da 22.5 in poi
  14. blue = (0, 0, 255)      # Fino a 21.5
  15. white = (255, 255, 255) # Da 21.5 a 22.5
  16.  
  17. # Temperatura iniziale ricavata dal sensore della SenseHAT
  18. initTemp = s.get_temperature()
  19.  
  20. # Stanza iniziale da mostrare (per non avere errori con
  21. # l'array, usa 0 anche se รจ la stanza 1)
  22. room = 0
  23.  
  24. # Array contenente la temperatura delle stanze
  25. roomsTemp = [initTemp, initTemp, initTemp]
  26.  
  27. # Fixa il bug per cui il metodo assegnato al movimento del joystick (sull'emulatore)
  28. # viene eseguito due volte
  29. emuButton = 0
  30.  
  31. # In base alla temperatura dice quale colore deve mostrare
  32. def whichColor(t):
  33.     if t < 21.5:
  34.         return "blue"
  35.     elif t >= 21.5 and t <= 22.5:
  36.         return "white"
  37.     elif t > 22.5:
  38.         return "red"
  39.  
  40. # Imposta tutti i pixel come blu
  41. def setAllBlue():
  42.     for x in range(0, 8):
  43.         for y in range(0, 8):
  44.             s.set_pixel(x, y, blue)
  45.  
  46. # Imposta tutti i pixel come bianchi
  47. def setAllWhite():
  48.     for x in range(0, 8):
  49.         for y in range(0, 8):
  50.             s.set_pixel(x, y, white)
  51.  
  52. # Imposta tutti i pixel come rossi
  53. def setAllRed():
  54.     for x in range(0, 8):
  55.         for y in range(0, 8):
  56.             s.set_pixel(x, y, red)
  57.  
  58. # Quando il joystick (js) punta verso destra
  59. def js_Right():
  60.  
  61.     if (emuButton == 1):
  62.         # UnboundLocalError: local variable referenced before assignment
  63.         global room
  64.         print(str(room))
  65.  
  66.         # Cambia stanza
  67.         if room == 0 or room == 1:
  68.             room += 1
  69.         else:
  70.             room = 0
  71.  
  72.         emuButton = 0
  73.  
  74.         # Mostra il numero della stanza
  75.         s.show_letter(str(room + 1))
  76.         sleep(1)
  77.     else:
  78.         emubutton += 1
  79.  
  80. # Quando il joystick (js) punta verso sinistra
  81. def js_Left():
  82.     if (emuButton == 1):
  83.         # UnboundLocalError: local variable referenced before assignment
  84.         global room
  85.  
  86.         # Cambia stanza
  87.         if room == 1 or room == 2:
  88.             room -= 1
  89.         else:
  90.             room = 2
  91.  
  92.         emuButton = 0
  93.  
  94.         # Mostra il numero della stanza
  95.         s.show_letter(str(room + 1))
  96.         sleep(1)
  97.     else:
  98.         emubutton += 1
  99.  
  100. # Quando il joystick (js) viene usato
  101. def js_Any():
  102.     global roomsTemp
  103.  
  104.     # Aggiorna la temperatura
  105.     roomsTemp[room] = s.get_temperature()
  106.  
  107.     # Mostra il colore in base alla temperatura
  108.     if whichColor(roomsTemp[room]) == "blue":
  109.         setAllBlue()
  110.     elif whichColor(roomsTemp[room]) == "white":
  111.         setAllWhite()
  112.     elif whichColor(roomsTemp[room]) == "red":
  113.         setAllRed()
  114.  
  115. s.clear()
  116.  
  117. # Mostra il numero della stanza iniziale
  118. s.show_letter(str(room + 1))
  119. sleep(1)
  120.  
  121. # Mostra il colore in base alla temperatura
  122. if whichColor(roomsTemp[room]) == "blue":
  123.     setAllBlue()
  124. elif whichColor(roomsTemp[room]) == "white":
  125.     setAllWhite()
  126. elif whichColor(roomsTemp[room]) == "red":
  127.     setAllRed()
  128.  
  129. # Eventi per il movimento del joystick
  130. s.stick.direction_right = js_Right
  131. s.stick.direction_left = js_Left
  132. s.stick.direction_any = js_Any
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement