This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Kobo Glo Input

By: KevinShort on Nov 24th, 2012  |  syntax: Python  |  size: 2.90 KB  |  views: 212  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import struct
  2.  
  3. touching = False
  4. x = 0
  5. y = 0
  6. start_touch = [0, 0]
  7. end_touch = [0, 0]
  8. light = False
  9. power = False
  10.  
  11. def convert_pos(orientation, pos):
  12.     """The Kobo Glo's touch screen is read in landscape mode, so for apps using
  13.    portrait mode we need to convert the x and y coordinates.
  14.    orientation: either "landscape" or "portrait"
  15.    pos: the x and y values you want to convert
  16.  
  17.    returns: a tuple, (x, y)"""
  18.     if orientation.lower() == "landscape":
  19.         return pos
  20.     else:
  21.         return 758-pos[1], pos[0]
  22.  
  23. try:
  24.     touchscreen = open("/dev/input/event1", "r")
  25.     buttons = open("/dev/input/event0", "r")
  26. except IOError as error:
  27.     print(error)
  28.    
  29. def get_touch_input(orientation="landscape"):
  30.     '''Read the Kobo Glo's touchscreen.
  31.            orientation: either "portrait" or "landscape"'''
  32.     format = "iihhi"
  33.     event = touchscreen.read(16)
  34.     global touching, x, y, start_touch, end_touch
  35.    
  36.     (time1, time2, type, code, value) = struct.unpack(format, event)
  37.     #print type, code, value
  38.    
  39.     if type == 3:
  40.         if code == 0:
  41.             x = value
  42.         elif code == 1:
  43.             y = value
  44.            
  45.         elif code == 24 and value != 0:
  46.             touching = True
  47.             # Touch
  48.             pos = convert_pos(orientation, (x, y))
  49.             print(pos)
  50.            
  51.     elif type == 1 and code == 330:
  52.         if value == 0:
  53.                         # End touch
  54.             touching = False
  55.             end_touch = x, y
  56.             pos = convert_pos(orientation, end_touch)
  57.             print("End touch", end_touch)
  58.         elif value == 1:
  59.             # Start Touch
  60.             touching = True
  61.             start_touch = x, y
  62.             pos = convert_pos(orientation, start_touch)
  63.             print("Start touch", start_touch)
  64.    
  65.     #print "Touching:", touching
  66.     #print "Position:", x, y
  67.     #print "Type:", type
  68.     #print "___________"
  69.        
  70. def get_button_input():
  71.     format = "iihhi"
  72.     event = buttons.read(16)
  73.     global light, power
  74.  
  75.     (time1, time2, type, code, value) = struct.unpack(format, event)
  76.     if code == 90:
  77.         # Light button
  78.         if value == 1:
  79.             light = True
  80.             print("Light button pressed")
  81.         else:
  82.             light = False
  83.             print("Light button released")
  84.     elif code == 116:
  85.         # Power switch
  86.         if value == 1:
  87.             power = True
  88.             print("Power slider pulled")
  89.         else:
  90.             power = False
  91.             print("Power slider released")
  92.    
  93.     #print(code, value)
  94.     #print("Power:", power)
  95.     #print("Light:", light)
  96.    
  97. def test_touch():
  98.     while 1:
  99.         get_touch_input("landscape")
  100.                
  101. def test_buttons():
  102.     while 1:
  103.         get_button_input()
  104.        
  105.    
  106. if __name__ == "__main__":
  107.     import thread
  108.     print("Starting Glo Input Test")
  109.     thread.start_new_thread(test_touch, ())
  110.     test_buttons()
clone this paste RAW Paste Data