~aleteoryx/muditaos

ref: af960b6fac2fdd9e42072c4632ca769fce76f95f muditaos/test/harness/utils.py -rw-r--r-- 3.3 KiB
af960b6f — Lucjan Bryndza [EGD-5146] Add read LFS block size from part 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import time
from enum import Enum

from harness.interface.CDCSerial import Keytype
from harness.interface.defs import key_codes


# assuming that the harness is actually in the menu
application_keypath = {
    "calllog": [
        "enter"
    ],
    "contacts": [
        "right",
        "enter"
    ],
    "messages": [
        "right",
        "right",
        "enter"
    ],
    "music": [
        "down",
        "enter"
    ],
    "meditation": [
        "down",
        "right",
        "enter"
    ],
    "settings": [
        "down",
        "right",
        "right",
        "enter"
    ],
    "tools": [
        "up",
        "enter"
    ],
    "alarm": [
        "up",
        "right",
        "enter"
    ],
    "calendar": [
        "up",
        "left",
        "left",
        "enter"
    ]
}

keymap = {
    "A": "2",
    "B": "22",
    "C": "222",
    "D": "3",
    "E": "33",
    "F": "333",
    "G": "4",
    "H": "44",
    "I": "444",
    "J": "5",
    "K": "55",
    "L": "555",
    "M": "6",
    "N": "66",
    "O": "666",
    "P": "7",
    "Q": "77",
    "R": "777",
    "S": "7777",
    "T": "8",
    "U": "88",
    "V": "888",
    "W": "9",
    "X": "99",
    "Y": "999",
    "Z": "9999",
    " ": "0",
    ".": "1",
    ",": "11",
    "_": "111",
    ":": "1111",
    ";": "11111",
    ")": "111111",
    "(": "1111111",
    "?": "11111111",
    "!": "111111111",
    "#": "1111111111",
    "/": "11111111111",
    "*": "111111111111",
    "+": "1111111111111",
    "del": key_codes["#"],
    "caps": key_codes["*"],
}


def send_keystoke(keypath, connection):
    for key in keypath:
        connection.send_key_code(key_codes[key])
        time.sleep(0.3)


last_char = '\0'


def send_char(char: str, connection):
    global last_char
    key_type = Keytype.short_press
    if char.isdigit():
        key_type = Keytype.long_press
        if last_char is char:
            print("repeated key!")
            connection.send_key_code(key_codes["right"])
        connection.send_key_code(int(char), key_type)
        connection.send_key_code(key_codes["right"])
        last_char = char

    else:
        if last_char is keymap[char][0]:
            print("repeated key!")
            connection.send_key_code(key_codes["right"], key_type)
        for key in keymap[char]:
            connection.send_key_code(int(key), key_type)
        last_char = keymap[char][0]


def send_number(number: str, connection):
    if number.isnumeric():
        for digit in number:
            connection.send_key_code(int(digit))
            time.sleep(0.3)


### timeout from https://stackoverflow.com/a/601168/5752094

from contextlib import contextmanager

import signal


class Timeout(Exception):
    '''
    usage:
        try:
            with Timeout.limit(10):
                long_function_call()
        except Timeout as e:
            print("Timed out!")
    '''

    @classmethod
    @contextmanager
    def limit(cls, seconds):
        def signal_handler(signum, frame):
            raise Timeout("Timed out!")

        signal.signal(signal.SIGALRM, signal_handler)
        signal.alarm(seconds)
        try:
            yield
        finally:
            signal.alarm(0)