def __init__(self, device_id): self.device_id = device_id # prepare for connection server = MNTServer(device_id) # real connection connection = MNTConnection(server.port) self.server = server self.connection = connection
class MNTDevice(object): """ minitouch device object Sample:: device = MNTDevice(_DEVICE_ID) # It's also very important to note that the maximum X and Y coordinates may, but usually do not, match the display size. # so you need to calculate position by yourself, and you can get maximum X and Y by this way: print('max x: ', device.connection.max_x) print('max y: ', device.connection.max_y) # single-tap device.tap([(400, 600)]) # multi-tap device.tap([(400, 400), (600, 600)]) # set the pressure, default == 100 device.tap([(400, 600)], pressure=50) # long-time-tap # for long-click, you should control time delay by yourself # because minitouch return nothing when actions done # we will never know the time when it finished device.tap([(400, 600)], duration=1000) time.sleep(1) # swipe device.swipe([(100, 100), (500, 500)]) # of course, with duration and pressure device.swipe([(100, 100), (400, 400), (200, 400)], duration=500, pressure=50) # extra functions ( their names start with 'ext_' ) device.ext_smooth_swipe([(100, 100), (400, 400), (200, 400)], duration=500, pressure=50, part=20) # stop minitouch # when it was stopped, minitouch can do nothing for device, including release. device.stop() """ def __init__(self, device_id): self.device_id = device_id self.server = None self.connection = None self.start() def reset(self): self.stop() self.start() def start(self): # prepare for connection self.server = MNTServer(self.device_id) # real connection self.connection = MNTConnection(self.server.port) def stop(self): self.connection.disconnect() self.server.stop() def tap(self, points, pressure=100, duration=None, no_up=None): """ tap on screen, with pressure/duration :param points: list, looks like [(x1, y1), (x2, y2)] :param pressure: default == 100 :param duration: :param no_up: if true, do not append 'up' at the end :return: """ points = [list(map(int, each_point)) for each_point in points] _builder = CommandBuilder() for point_id, each_point in enumerate(points): x, y = each_point _builder.down(point_id, x, y, pressure) _builder.commit() # apply duration if duration: _builder.wait(duration) _builder.commit() # need release? if not no_up: for each_id in range(len(points)): _builder.up(each_id) _builder.publish(self.connection) def swipe(self, points, pressure=100, duration=None, no_down=None, no_up=None): """ swipe between points, one by one :param points: [(400, 500), (500, 500)] :param pressure: default == 100 :param duration: :param no_down: will not 'down' at the beginning :param no_up: will not 'up' at the end :return: """ points = [list(map(int, each_point)) for each_point in points] _builder = CommandBuilder() point_id = 0 # tap the first point if not no_down: x, y = points.pop(0) _builder.down(point_id, x, y, pressure) _builder.publish(self.connection) # start swiping for each_point in points: x, y = each_point _builder.move(point_id, x, y, pressure) # add delay between points if duration: _builder.wait(randint(0, int(0.1 * duration)) + duration) _builder.commit() _builder.publish(self.connection) # release if not no_up: _builder.up(point_id) _builder.publish(self.connection) # extra functions' name starts with 'ext_' def ext_smooth_swipe(self, points, pressure=100, duration=None, part=None, no_down=None, no_up=None): """ smoothly swipe between points, one by one it will split distance between points into pieces before:: points == [(100, 100), (500, 500)] part == 8 after:: points == [(100, 100), (150, 150), (200, 200), ... , (500, 500)] :param points: :param pressure: :param duration: :param part: default to 10 :param no_down: will not 'down' at the beginning :param no_up: will not 'up' at the end :return: """ if not part: part = 10 points = [list(map(int, each_point)) for each_point in points] for each_index in range(len(points) - 1): cur_point = points[each_index] next_point = points[each_index + 1] offset = (int((next_point[0] - cur_point[0]) / part), int((next_point[1] - cur_point[1]) / part)) new_points = [(cur_point[0] + i * offset[0], cur_point[1] + i * offset[1]) for i in range(part + 1)] self.swipe(new_points, pressure=pressure, duration=duration, no_down=no_down, no_up=no_up)
def start(self): # prepare for connection self.server = MNTServer(self.device_id) # real connection self.connection = MNTConnection(self.server.port)