blob: 68967716e4dfaf421763bb6a8e67aaad21980d0d [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 Mooneya337d582015-04-29 10:37:45 -070087 elif test.name in tests.TWO_FINGER_TRACKING:
88 fn = lambda: _PerformTwoFingerLineTest(variation, robot, device_spec)
Charlie Mooney08661912015-04-16 09:20:34 -070089
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 Mooney61b4fbb2015-04-22 15:22:02 -070096
Charlie Mooney08661912015-04-16 09:20:34 -070097def _PerformStationaryNoiseTest(variation, robot, device_spec):
98 frequency, amplitude, waveform, location = variation
Charlie Mooney61b4fbb2015-04-22 15:22:02 -070099 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 Mooney1b708112015-04-28 14:46:21 -0700104def _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 Mooney61b4fbb2015-04-22 15:22:02 -0700111def _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 Mooneya337d582015-04-29 10:37:45 -0700117 robot.Line(device_spec, [fingertip], start, end, pause_time_s)
118 robot.PopSpeed()
119
120
121def _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 Mooney61b4fbb2015-04-22 15:22:02 -0700138 robot.PopSpeed()