Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- touching = False
- x = 0
- y = 0
- start_touch = [0, 0]
- end_touch = [0, 0]
- light = False
- power = False
- def convert_pos(orientation, pos):
- """The Kobo Glo's touch screen is read in landscape mode, so for apps using
- portrait mode we need to convert the x and y coordinates.
- orientation: either "landscape" or "portrait"
- pos: the x and y values you want to convert
- returns: a tuple, (x, y)"""
- if orientation.lower() == "landscape":
- return pos
- else:
- return 758-pos[1], pos[0]
- try:
- touchscreen = open("/dev/input/event1", "r")
- buttons = open("/dev/input/event0", "r")
- except IOError as error:
- print(error)
- def get_touch_input(orientation="landscape"):
- '''Read the Kobo Glo's touchscreen.
- orientation: either "portrait" or "landscape"'''
- format = "iihhi"
- event = touchscreen.read(16)
- global touching, x, y, start_touch, end_touch
- (time1, time2, type, code, value) = struct.unpack(format, event)
- #print type, code, value
- if type == 3:
- if code == 0:
- x = value
- elif code == 1:
- y = value
- elif code == 24 and value != 0:
- touching = True
- # Touch
- pos = convert_pos(orientation, (x, y))
- print(pos)
- elif type == 1 and code == 330:
- if value == 0:
- # End touch
- touching = False
- end_touch = x, y
- pos = convert_pos(orientation, end_touch)
- print("End touch", end_touch)
- elif value == 1:
- # Start Touch
- touching = True
- start_touch = x, y
- pos = convert_pos(orientation, start_touch)
- print("Start touch", start_touch)
- #print "Touching:", touching
- #print "Position:", x, y
- #print "Type:", type
- #print "___________"
- def get_button_input():
- format = "iihhi"
- event = buttons.read(16)
- global light, power
- (time1, time2, type, code, value) = struct.unpack(format, event)
- if code == 90:
- # Light button
- if value == 1:
- light = True
- print("Light button pressed")
- else:
- light = False
- print("Light button released")
- elif code == 116:
- # Power switch
- if value == 1:
- power = True
- print("Power slider pulled")
- else:
- power = False
- print("Power slider released")
- #print(code, value)
- #print("Power:", power)
- #print("Light:", light)
- def test_touch():
- while 1:
- get_touch_input("landscape")
- def test_buttons():
- while 1:
- get_button_input()
- if __name__ == "__main__":
- import thread
- print("Starting Glo Input Test")
- thread.start_new_thread(test_touch, ())
- test_buttons()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement