Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import pifacecad
  2. import transmissionrpc
  3.  
  4. # Transmission Client Settings
  5. ADDRESS = "127.0.0.1"
  6. PORT = 9091
  7. USER = "transmission"
  8. PASSWORD = "sean123456"
  9.  
  10. def shorten_string(str):
  11. str = (str[:14] + '..') if len(str) > 16 else str
  12. return str
  13.  
  14. if __name__ == "__main__":
  15. cad = pifacecad.PiFaceCAD()
  16. cad.lcd.backlight_on()
  17. cad.lcd.blink_off()
  18. cad.lcd.cursor_off()
  19. cad.lcd.write(shortenString(torrent.name))
  20. cad.lcd.write('\n')
  21. cad.lcd.write(str(int(round(torrent.progress))) + '% | ' + str(torrent.rateDownload / 1000) + 'KB/s')
  22.  
  23. class TransmissionDisplay(object):
  24. def __init__(self, cad, torrents, index=0):
  25. self.cad = cad
  26. self.cad.lcd.backlight_on()
  27. self.cad.lcd.blink_off()
  28. self.cad.lcd.cursor_off()
  29.  
  30. def update(self):
  31. self.cad.lcd.clear()
  32. self.display_torrent(0)
  33.  
  34. def display_torrent(index):
  35. torrent = torrents[index]
  36. cad.lcd.write(shorten_string(torrent.name))
  37. cad.lcd.write("\n")
  38. cad.lcd.write(str(int(round(torrent.progress))) + '% | ' + str(torrent.rateDownload / 1000) + 'KB/s')
  39.  
  40. def next_torrent(self):
  41. index = index + 1
  42. display_torrent(index)
  43.  
  44. def previous_torrent(self):
  45. index = index - 1
  46. display_torrent(index)
  47.  
  48. if __name__ == "__main__":
  49. transmission_client = transmissionrpc.Client(address=ADDRESS, port=PORT, user=USER, password=PASSWORD)
  50. torrents = transmission_client.get_torrents()
  51.  
  52. cad = pifacecad.PiFaceCAD()
  53.  
  54. global transmission_display
  55. transmission_display = TransmissionDisplay(cad, torrents)
  56. transmission_display.update()
  57.  
  58. # listener cannot deactivate itself so we have to wait until it has
  59. # finished using a barrier.
  60. global end_barrier
  61. end_barrier = threading.Barrier(2)
  62.  
  63. # wait for button presses
  64. global switchlistener
  65. switchlistener = pifacecad.SwitchEventListener(chip=cad)
  66. switchlistener.register(
  67. 0, pifacecad.IODIR_ON, traintimedisplay.previous_board)
  68. switchlistener.register(
  69. 1, pifacecad.IODIR_ON, traintimedisplay.next_board)
  70.  
  71. switchlistener.activate()
  72. end_barrier.wait() # wait unitl exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement