Best Python code snippet using fMBT_python
touch.py
Source:touch.py
1# module to provide touch screen support2import os, sys, glob, fcntl, struct, select, time3from ctypes import *4# See linux/input.h for more information5# Returned by EVIOCGABS ioctl6class input_absinfo(Structure):7 _fields_ = [8 ("value", c_uint), # last value returned9 ("minimum", c_uint), # minimum value for the axis10 ("maximum", c_uint), # maximum value for the axis11 ("fuzz", c_uint), # fuzz value used to filter values from the input stream12 ("flat", c_uint), # indicates joystick discard13 ("resolution", c_uint), # units/mm14 ]15# ioctls to retrieve X and Y axis information16EVIOCGABS_X = 0x8018454017EVIOCGABS_Y = 0x8018454118# ioctl to retrieve supported keys19EVIOCGBIT_EVKEY_96 = 0x8060452120# Button events of interest21BTN_MOUSE = 0x11022BTN_TOUCH = 0x14a23BTN_STYLUS = 0x14b24# In order of preference25buttons = (BTN_TOUCH, BTN_STYLUS, BTN_MOUSE)26class Touch():27 def __init__(self, width=None, height=None, device=None):28 # Locate EV_ABS device with correct X and Y dimensions, return with29 # the device handle held open30 for td in glob.glob(device if device else "/dev/input/event*"):31 fd = open(td, 'rb')32 try:33 # get absinfo for x and y axis34 x = input_absinfo()35 fcntl.ioctl(fd, EVIOCGABS_X, x, True)36 y = input_absinfo()37 fcntl.ioctl(fd, EVIOCGABS_Y, y, True)38 # get array of supported keys39 keys = (c_ubyte * 96)()40 fcntl.ioctl(fd, EVIOCGBIT_EVKEY_96, keys, True)41 if x.minimum == 0 and (x.maximum == width if width else x.maximum >= 64):42 if y.minimum == 0 and (y.maximum == height if height else y.maximum >= 64):43 for button in buttons:44 if keys[button//8] & (1 << (button & 7)):45 # found a usable device46 self.fd = None47 self.device = td48 self.button = button49 self.width = x.maximum50 self.height = y.maximum51 self.scale_width = None # these can be set later to scale the results52 self.scale_height = None53 return54 except OSError:55 # ioctl not supported so advance to next56 pass57 fd.close()58 raise Exception("No touch device found")59 # Return (x, y) on touch or None on release60 # If timeout given, return False after timeout seconds61 # If reset given, close and reopen the device62 def touch(self, timeout=None, reset=False):63 if reset and self.fd is not None:64 self.fd.close()65 self.fd = None66 if timeout is not None:67 if timeout <= 0: return False68 timeout += time.monotonic()69 if self.fd is None:70 self.fd = open(self.device, 'rb')71 os.set_blocking(self.fd.fileno(), False)72 press = xabs = yabs = None73 while True:74 # It looks like some touch drivers don't support the poll method75 # correctly? So first try to read a packet in non-blocking mode and76 # select only if nothing is waiting.77 while True:78 packet = self.fd.read(16)79 if packet:80 if len(packet) == 16: break81 # Partial packet, maybe the rest is in the pipe?82 for x in range(10):83 os.sched_yield()84 packet += self.fd.read(16-len(packet))85 if len(packet) == 16: break86 # Ugh, ignore partial packet87 else:88 s = select.select([self.fd], [], [], max(0, timeout-time.monotonic()) if timeout else None)89 if not s[0]: return False # Timeout!90 # An event packet is 16 bytes:91 # struct input_event {92 # struct timeval time; // 8 bytes93 # __u16 type;94 # __u16 code;95 # __s32 value;96 # };97 # We ignore the time98 type, code, value = struct.unpack("8xHHi", packet)99 if type == 0: # EV_SYN100 if press == 1:101 if xabs is not None and yabs is not None:102 # scale if enabled103 if self.scale_width: xabs = int(xabs * (self.scale_width/self.width))104 if self.scale_height: yabs = int(yabs * (self.scale_height/self.height))105 return (xabs, yabs)106 elif press == 0:107 return None108 press = xabs = yabs = None109 elif type == 1 and code == self.button: # EV_KEY and our button110 press = value111 elif type == 3 and code == 0: # EV_ABS and ABS_X112 xabs = value113 elif type == 3 and code == 1: # EV_ABS and ABS_Y114 yabs = value115 # Return true when touch is released, or false on timeout116 def release(self, timeout=None):117 if timeout: timeout += time.monotonic()118 while True:119 xy = self.touch(timeout = max(0, timeout-time.monotonic()) if timeout else None)120 if xy is None: return True # release121 if xy is False: return False # timeout122 # Given dict of {box:value}, where 'box' defines an area in form [x1, y1,123 # x2, y2], look for a touch in one of the boxes, and return the124 # corresponding value. Boxes must not overlap.125 def select(self, boxes, timeout=None):126 if timeout: timeout += time.monotonic()127 while True:128 xy = self.touch(timeout = max(0, timeout-time.monotonic()) if timeout else None)129 if xy is False: return False # timeout130 if xy:131 for box, value in boxes.items():132 if xy[0] >= box[0] and xy[0] <= box[2] and xy[1] >= box[1] and xy[1] <= box[3]:133 return value134if __name__ == "__main__":135 t = Touch() # Find the first EV_ABS device with X and Y axis and a usable touch button136 print("Using device %s, %d x %d, key code %d" % (t.device, t.width, t.height, t.button))137 # scale it to virtual 250x250 screen138 t.scale_width=250139 t.scale_height=250140 while True:141 pos = t.touch(timeout=5)142 if pos : print("%d x %d" % pos)143 elif pos is None: print("Release")...
AbsAxisScaling.py
Source:AbsAxisScaling.py
1import array2import fcntl3import struct4from pi3d.event import ioctl5def EVIOCGABS(axis):6 return ioctl._IOR(ord('E'), 0x40 + axis, "ffffff") # get abs value/limits7class AbsAxisScaling(object):8 """9 Fetches and implements the EV_ABS axis scaling.10 The constructor fetches the scaling values from the given stream for the11 given axis using an ioctl.12 There is a scale method, which scales a given value to the range -1..+1.13 """14 def __init__(self, stream, axis):15 """16 Fetch the scale values for this stream and fill in the instance17 variables accordingly.18 """19 s = array.array("i", [1, 2, 3, 4, 5, 6])20 try:21 x = fcntl.ioctl(stream.filehandle, EVIOCGABS(axis), s)22 except IOError:23 self.value = self.minimum = self.maximum = self.fuzz = self.flat = self.resolution = 124 else:25 self.value, self.minimum, self.maximum, self.fuzz, self.flat, self.resolution = struct.unpack("iiiiii", s)26 def __str__(self):27 return "Value {0} Min {1}, Max {2}, Fuzz {3}, Flat {4}, Res {5}".format(self.value, self.minimum, self.maximum, self.fuzz, self.flat, self.resolution)28 def scale(self, value):29 """30 scales the given value into the range -1..+131 """...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!