Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 1 | # 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 |
| 6 | determine the specifics of the gesture and execute them on the touchbot. |
| 7 | In essence, you use this class to go from a Test object to the robot |
| 8 | actually performing the correct gesture on the pad. |
| 9 | """ |
| 10 | |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame^] | 11 | import math |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 12 | from threading import Thread |
| 13 | |
| 14 | import colorama as color |
| 15 | |
| 16 | import tests |
| 17 | from touchbot import Touchbot |
| 18 | |
| 19 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 20 | STANDARD_FINGERTIP = '1round_9mm' |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame^] | 21 | STANDARD_SECONDARY_FINGERTIP = '2round_9mm' |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 22 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 23 | BUFFER_SIZE = 0.1 |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 24 | OVERSHOOT_DISTANCE = 0.05 |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 25 | LEFT = BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 26 | OVER_LEFT = -OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 27 | RIGHT = 1.0 - BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 28 | OVER_RIGHT = 1.0 + OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 29 | TOP = BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 30 | OVER_TOP = -OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 31 | BOTTOM = 1.0 - BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 32 | OVER_BOTTOM = 1.0 + OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 33 | CENTER = 0.5 |
| 34 | |
| 35 | LOCATION_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 Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 47 | LINE_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 | |
| 65 | SPEEDS = { |
| 66 | tests.GV.NORMAL: Touchbot.SPEED_MEDIUM, |
| 67 | tests.GV.SLOW: Touchbot.SPEED_SLOW, |
| 68 | } |
| 69 | |
| 70 | SINGLE_FINGER_LINE_TESTS = [ |
| 71 | tests.ONE_FINGER_TO_EDGE, |
| 72 | tests.ONE_FINGER_TRACKING, |
| 73 | tests.ONE_FINGER_TRACKING_FROM_CENTER, |
| 74 | ] |
| 75 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 76 | |
| 77 | def 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 Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 82 | elif test.name == tests.ONE_FINGER_TAP: |
| 83 | fn = lambda: _PerformOneFingerTapTest(variation, robot, device_spec) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 84 | 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 Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame^] | 87 | elif test.name in tests.TWO_FINGER_TRACKING: |
| 88 | fn = lambda: _PerformTwoFingerLineTest(variation, robot, device_spec) |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 89 | |
| 90 | if fn is None: |
| 91 | print color.Fore.RED + 'Error: Robot unable to perform gesture!' |
| 92 | return None |
| 93 | |
| 94 | return Thread(target=fn) |
| 95 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 96 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 97 | def _PerformStationaryNoiseTest(variation, robot, device_spec): |
| 98 | frequency, amplitude, waveform, location = variation |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 99 | tap_position = LOCATION_COORDINATES[location] |
| 100 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 101 | robot.SingleTipTap(device_spec, fingertip, tap_position, touch_time_s=4) |
| 102 | |
| 103 | |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 104 | def _PerformOneFingerTapTest(variation, robot, device_spec): |
| 105 | location, = variation |
| 106 | tap_position = LOCATION_COORDINATES[location] |
| 107 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 108 | robot.SingleTipTap(device_spec, fingertip, tap_position) |
| 109 | |
| 110 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 111 | def _PerformOneFingerLineTest(variation, robot, device_spec, pause_time_s): |
| 112 | direction, speed = variation |
| 113 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 114 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 115 | |
| 116 | robot.PushSpeed(SPEEDS[speed]) |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame^] | 117 | robot.Line(device_spec, [fingertip], start, end, pause_time_s) |
| 118 | robot.PopSpeed() |
| 119 | |
| 120 | |
| 121 | def _PerformTwoFingerLineTest(variation, robot, device_spec): |
| 122 | direction, speed = variation |
| 123 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 124 | |
| 125 | # Compute the fingertip angle to be perpendicular to the |
| 126 | # movement direction |
| 127 | x1, y1 = start |
| 128 | x2, y2 = end |
| 129 | dy = y2 - y1 |
| 130 | dx = x2 - x1 |
| 131 | angle = -1 * (math.degrees(math.atan2(y2 - y1, x2 - x1)) + 90) |
| 132 | |
| 133 | fingertips = [robot.fingertips[STANDARD_FINGERTIP], |
| 134 | robot.fingertips[STANDARD_SECONDARY_FINGERTIP]] |
| 135 | robot.PushSpeed(SPEEDS[speed]) |
| 136 | robot.Line(device_spec, fingertips, start, end, |
| 137 | fingertip_spacing=5, fingertip_angle=angle) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 138 | robot.PopSpeed() |