blob: d63a4a2492c0745f9335fc376f3d21da042f0260 [file] [log] [blame]
Charlie Mooney08661912015-04-16 09:20:34 -07001# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5""" This is a module containing functions that allow the test suite to
6determine the specifics of the gesture and execute them on the touchbot.
7In essence, you use this class to go from a Test object to the robot
8actually performing the correct gesture on the pad.
9"""
10
Charlie Mooneya337d582015-04-29 10:37:45 -070011import math
Charlie Mooney08661912015-04-16 09:20:34 -070012from threading import Thread
13
14import colorama as color
15
16import tests
17from touchbot import Touchbot
18
19
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070020STANDARD_FINGERTIP = '1round_9mm'
Charlie Mooneya337d582015-04-29 10:37:45 -070021STANDARD_SECONDARY_FINGERTIP = '2round_9mm'
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070022
Charlie Mooney08661912015-04-16 09:20:34 -070023BUFFER_SIZE = 0.1
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070024OVERSHOOT_DISTANCE = 0.05
Charlie Mooney08661912015-04-16 09:20:34 -070025LEFT = BUFFER_SIZE
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070026OVER_LEFT = -OVERSHOOT_DISTANCE
Charlie Mooney08661912015-04-16 09:20:34 -070027RIGHT = 1.0 - BUFFER_SIZE
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070028OVER_RIGHT = 1.0 + OVERSHOOT_DISTANCE
Charlie Mooney08661912015-04-16 09:20:34 -070029TOP = BUFFER_SIZE
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070030OVER_TOP = -OVERSHOOT_DISTANCE
Charlie Mooney08661912015-04-16 09:20:34 -070031BOTTOM = 1.0 - BUFFER_SIZE
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070032OVER_BOTTOM = 1.0 + OVERSHOOT_DISTANCE
Charlie Mooney08661912015-04-16 09:20:34 -070033CENTER = 0.5
34
35LOCATION_COORDINATES = {
36 tests.GV.TL: (LEFT, TOP),
37 tests.GV.TR: (RIGHT, TOP),
38 tests.GV.BL: (LEFT, BOTTOM),
39 tests.GV.BR: (RIGHT, BOTTOM),
40 tests.GV.TS: (CENTER, TOP),
41 tests.GV.BS: (CENTER, BOTTOM),
42 tests.GV.LS: (LEFT, CENTER),
43 tests.GV.RS: (RIGHT, CENTER),
44 tests.GV.CENTER: (CENTER, CENTER),
45}
46
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070047LINE_DIRECTION_COORDINATES = {
48 tests.GV.LR: ((LEFT, CENTER), (RIGHT, CENTER)),
49 tests.GV.RL: ((RIGHT, CENTER), (LEFT, CENTER)),
50 tests.GV.TB: ((CENTER, TOP), (CENTER, BOTTOM)),
51 tests.GV.BT: ((CENTER, BOTTOM), (CENTER, TOP)),
52 tests.GV.BLTR: ((LEFT, BOTTOM), (RIGHT, TOP)),
53 tests.GV.BRTL: ((RIGHT, BOTTOM), (LEFT, TOP)),
54 tests.GV.TRBL: ((RIGHT, TOP), (LEFT, BOTTOM)),
55 tests.GV.TLBR: ((LEFT, TOP), (RIGHT, BOTTOM)),
56 tests.GV.CL: ((CENTER, CENTER), (OVER_LEFT, CENTER)),
57 tests.GV.CR: ((CENTER, CENTER), (OVER_RIGHT, CENTER)),
58 tests.GV.CT: ((CENTER, CENTER), (CENTER, OVER_TOP)),
59 tests.GV.CB: ((CENTER, CENTER), (CENTER, OVER_BOTTOM)),
60 tests.GV.CUL: ((CENTER, CENTER), (OVER_LEFT, OVER_TOP)),
61 tests.GV.CLL: ((CENTER, CENTER), (OVER_LEFT, OVER_BOTTOM)),
62 tests.GV.CLR: ((CENTER, CENTER), (OVER_RIGHT, OVER_BOTTOM)),
63}
64
65SPEEDS = {
66 tests.GV.NORMAL: Touchbot.SPEED_MEDIUM,
67 tests.GV.SLOW: Touchbot.SPEED_SLOW,
68}
69
70SINGLE_FINGER_LINE_TESTS = [
71 tests.ONE_FINGER_TO_EDGE,
72 tests.ONE_FINGER_TRACKING,
73 tests.ONE_FINGER_TRACKING_FROM_CENTER,
74]
75
Charlie Mooney08661912015-04-16 09:20:34 -070076
77def PerformCorrespondingGesture(test, variation_number, robot, device_spec):
78 variation = test.variations[variation_number]
79 fn = None
80 if test.name == tests.NOISE_STATIONARY:
81 fn = lambda: _PerformStationaryNoiseTest(variation, robot, device_spec)
Charlie Mooney1b708112015-04-28 14:46:21 -070082 elif test.name == tests.ONE_FINGER_TAP:
83 fn = lambda: _PerformOneFingerTapTest(variation, robot, device_spec)
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070084 elif test.name in SINGLE_FINGER_LINE_TESTS:
85 pause = 1 if test.name == tests.ONE_FINGER_TRACKING_FROM_CENTER else 0
86 fn = lambda: _PerformOneFingerLineTest(variation, robot, device_spec, pause)
Charlie Mooneye9fd6562015-04-30 13:45:19 -070087 elif test.name == tests.TWO_FINGER_TRACKING:
Charlie Mooneya337d582015-04-29 10:37:45 -070088 fn = lambda: _PerformTwoFingerLineTest(variation, robot, device_spec)
Charlie Mooneye9fd6562015-04-30 13:45:19 -070089 elif test.name == tests.RESTING_FINGER_PLUS_2ND_FINGER_MOVE:
90 fn = lambda: _PerformRestingFingerTest(variation, robot, device_spec)
Charlie Mooney08661912015-04-16 09:20:34 -070091
92 if fn is None:
93 print color.Fore.RED + 'Error: Robot unable to perform gesture!'
94 return None
95
96 return Thread(target=fn)
97
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070098
Charlie Mooney08661912015-04-16 09:20:34 -070099def _PerformStationaryNoiseTest(variation, robot, device_spec):
100 frequency, amplitude, waveform, location = variation
Charlie Mooney61b4fbb2015-04-22 15:22:02 -0700101 tap_position = LOCATION_COORDINATES[location]
102 fingertip = robot.fingertips[STANDARD_FINGERTIP]
103 robot.SingleTipTap(device_spec, fingertip, tap_position, touch_time_s=4)
104
105
Charlie Mooney1b708112015-04-28 14:46:21 -0700106def _PerformOneFingerTapTest(variation, robot, device_spec):
107 location, = variation
108 tap_position = LOCATION_COORDINATES[location]
109 fingertip = robot.fingertips[STANDARD_FINGERTIP]
110 robot.SingleTipTap(device_spec, fingertip, tap_position)
111
112
Charlie Mooney61b4fbb2015-04-22 15:22:02 -0700113def _PerformOneFingerLineTest(variation, robot, device_spec, pause_time_s):
114 direction, speed = variation
115 start, end = LINE_DIRECTION_COORDINATES[direction]
116 fingertip = robot.fingertips[STANDARD_FINGERTIP]
117
118 robot.PushSpeed(SPEEDS[speed])
Charlie Mooneya337d582015-04-29 10:37:45 -0700119 robot.Line(device_spec, [fingertip], start, end, pause_time_s)
120 robot.PopSpeed()
121
122
123def _PerformTwoFingerLineTest(variation, robot, device_spec):
124 direction, speed = variation
125 start, end = LINE_DIRECTION_COORDINATES[direction]
126
127 # Compute the fingertip angle to be perpendicular to the
128 # movement direction
129 x1, y1 = start
130 x2, y2 = end
131 dy = y2 - y1
132 dx = x2 - x1
133 angle = -1 * (math.degrees(math.atan2(y2 - y1, x2 - x1)) + 90)
134
135 fingertips = [robot.fingertips[STANDARD_FINGERTIP],
136 robot.fingertips[STANDARD_SECONDARY_FINGERTIP]]
137 robot.PushSpeed(SPEEDS[speed])
138 robot.Line(device_spec, fingertips, start, end,
139 fingertip_spacing=5, fingertip_angle=angle)
Charlie Mooney61b4fbb2015-04-22 15:22:02 -0700140 robot.PopSpeed()
Charlie Mooneye9fd6562015-04-30 13:45:19 -0700141
142
143def _PerformRestingFingerTest(variation, robot, device_spec):
144 direction, speed = variation
145 start, end = LINE_DIRECTION_COORDINATES[direction]
146 stationary_location = LOCATION_COORDINATES[tests.GV.BL]
147
148 stationary_fingertip = robot.fingertips[STANDARD_FINGERTIP]
149 moving_fingertip = robot.fingertips[STANDARD_SECONDARY_FINGERTIP]
150
151 robot.PushSpeed(SPEEDS[speed])
152 robot.LineWithStationaryFinger(device_spec, stationary_fingertip,
153 moving_fingertip, start, end,
154 stationary_location)
155 robot.PopSpeed()