Skip to content

Commit ba88ed3

Browse files
authored
Adds override for incorrect target detection (ExpressLRS#924)
* init * Update BFinitPassthrough.py
1 parent 887e6df commit ba88ed3

8 files changed

Lines changed: 195 additions & 3 deletions

File tree

src/python/BFinitPassthrough.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import serials_find
44
import SerialHelper
55
import bootloader
6+
from query_yes_no import query_yes_no
67

78
SCRIPT_DEBUG = 0
89

@@ -132,7 +133,10 @@ def reset_to_bootloader(args):
132133
if rx_target == "":
133134
dbg_print("Cannot detect RX target, blindly flashing!")
134135
elif rx_target != flash_target:
135-
raise WrongTargetSelected("Wrong target selected your RX is '%s', trying to flash '%s'" % (rx_target, flash_target))
136+
if query_yes_no("\n\n\nWrong target selected! your RX is '%s', trying to flash '%s', continue? Y/N\n" % (rx_target, flash_target)):
137+
dbg_print("Ok, flashing anyway!")
138+
else:
139+
raise WrongTargetSelected("Wrong target selected your RX is '%s', trying to flash '%s'" % (rx_target, flash_target))
136140
elif flash_target != "":
137141
dbg_print("Verified RX target '%s'" % (flash_target))
138142
time.sleep(.5)

src/python/UARTupload.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import SerialHelper
1010
import re
1111
import bootloader
12+
from query_yes_no import query_yes_no
1213

1314
SCRIPT_DEBUG = 0
1415
BAUDRATE_DEFAULT = 420000
@@ -75,6 +76,7 @@ def uart_upload(port, filename, baudrate, ghst=False, key=None, target=""):
7576
gotBootloader = 'CCC' in rl.read_line()
7677

7778
# Init bootloader
79+
ignore_incorrect_target = False
7880
if not gotBootloader:
7981
# legacy bootloader requires a 500ms delay
8082
delay_seq2 = .5
@@ -141,8 +143,12 @@ def uart_upload(port, filename, baudrate, ghst=False, key=None, target=""):
141143

142144
elif "_RX_" in line:
143145
flash_target = re.sub("_VIA_.*", "", target.upper())
144-
if line != flash_target:
145-
raise Exception("Wrong target selected your RX is '%s', trying to flash '%s'" % (line, flash_target))
146+
if line != flash_target and not ignore_incorrect_target:
147+
if query_yes_no("\n\n\nWrong target selected! your RX is '%s', trying to flash '%s', continue? Y/N\n" % (line, flash_target)):
148+
ignore_incorrect_target = True
149+
continue
150+
else:
151+
raise Exception("Wrong target selected your RX is '%s', trying to flash '%s'" % (line, flash_target))
146152
elif flash_target != "":
147153
dbg_print("Verified RX target '%s'" % flash_target)
148154

src/python/inputimeout/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mitsuo Heijo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/python/inputimeout/README.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
inputimeout
2+
===========
3+
4+
.. image:: https://travis-ci.org/johejo/inputimeout.svg?branch=master
5+
:target: https://travis-ci.org/johejo/inputimeout
6+
7+
.. image:: https://ci.appveyor.com/api/projects/status/2g1fbnrcoj64g8t9?svg=true
8+
:target: https://ci.appveyor.com/project/johejo/inputimeout
9+
10+
.. image:: https://img.shields.io/pypi/v/inputimeout.svg
11+
:target: https://pypi.python.org/pypi/inputimeout
12+
13+
.. image:: https://img.shields.io/github/license/mashape/apistatus.svg
14+
:target: https://raw.githubusercontent.com/johejo/inputimeout/master/LICENSE
15+
16+
.. image:: https://api.codeclimate.com/v1/badges/3d51d0efbd7b86f0b7f1/maintainability
17+
:target: https://codeclimate.com/github/johejo/inputimeout/maintainability
18+
:alt: Maintainability
19+
20+
.. image:: https://api.codeclimate.com/v1/badges/3d51d0efbd7b86f0b7f1/test_coverage
21+
:target: https://codeclimate.com/github/johejo/inputimeout/test_coverage
22+
:alt: Test Coverage
23+
24+
.. image:: https://codecov.io/gh/johejo/inputimeout/branch/master/graph/badge.svg
25+
:target: https://codecov.io/gh/johejo/inputimeout
26+
27+
Description
28+
-----------
29+
30+
Multi platform standard input with timeout
31+
32+
Install
33+
-------
34+
35+
.. code:: bash
36+
37+
$ pip install inputimeout
38+
39+
Usage
40+
-----
41+
42+
.. code:: python
43+
44+
from inputimeout import inputimeout, TimeoutOccurred
45+
try:
46+
something = inputimeout(prompt='>>', timeout=5)
47+
except TimeoutOccurred:
48+
something = 'something'
49+
print(something)
50+
51+
License
52+
-------
53+
54+
MIT

src/python/inputimeout/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .inputimeout import inputimeout, TimeoutOccurred # noqa
2+
from .__version__ import ( # noqa
3+
__version__, __author__, __author_email__, __copyright__, __license__,
4+
__description__, __title__, __url__,
5+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
__title__ = 'inputimeout'
2+
__description__ = 'Multi platform standard input with timeout'
3+
__url__ = 'http://github.com/johejo/inutimeout'
4+
__version__ = '1.0.4'
5+
__author__ = 'Mitsuo Heijo'
6+
__author_email__ = '[email protected]'
7+
__license__ = 'MIT'
8+
__copyright__ = 'Copyright 2018 Mitsuo Heijo'
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import sys
2+
3+
DEFAULT_TIMEOUT = 30.0
4+
INTERVAL = 0.05
5+
6+
SP = ' '
7+
CR = '\r'
8+
LF = '\n'
9+
CRLF = CR + LF
10+
11+
12+
class TimeoutOccurred(Exception):
13+
pass
14+
15+
16+
def echo(string):
17+
sys.stdout.write(string)
18+
sys.stdout.flush()
19+
20+
21+
def posix_inputimeout(prompt='', timeout=DEFAULT_TIMEOUT):
22+
echo(prompt)
23+
sel = selectors.DefaultSelector()
24+
sel.register(sys.stdin, selectors.EVENT_READ)
25+
events = sel.select(timeout)
26+
27+
if events:
28+
key, _ = events[0]
29+
return key.fileobj.readline().rstrip(LF)
30+
else:
31+
echo(LF)
32+
termios.tcflush(sys.stdin, termios.TCIFLUSH)
33+
raise TimeoutOccurred
34+
35+
36+
def win_inputimeout(prompt='', timeout=DEFAULT_TIMEOUT):
37+
echo(prompt)
38+
begin = time.monotonic()
39+
end = begin + timeout
40+
line = ''
41+
42+
while time.monotonic() < end:
43+
if msvcrt.kbhit():
44+
c = msvcrt.getwche()
45+
if c in (CR, LF):
46+
echo(CRLF)
47+
return line
48+
if c == '\003':
49+
raise KeyboardInterrupt
50+
if c == '\b':
51+
line = line[:-1]
52+
cover = SP * len(prompt + line + SP)
53+
echo(''.join([CR, cover, CR, prompt, line]))
54+
else:
55+
line += c
56+
time.sleep(INTERVAL)
57+
58+
echo(CRLF)
59+
raise TimeoutOccurred
60+
61+
62+
try:
63+
import msvcrt
64+
65+
except ImportError:
66+
import selectors
67+
import termios
68+
69+
inputimeout = posix_inputimeout
70+
71+
else:
72+
import time
73+
74+
inputimeout = win_inputimeout

src/python/query_yes_no.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
from inputimeout import inputimeout, TimeoutOccurred
3+
4+
def query_yes_no(question=''): #https://code.activestate.com/recipes/577058/
5+
"""Ask a yes/no question via raw_input() and return their answer.
6+
"question" is a string that is presented to the user.
7+
The "answer" return value is True for "yes" or False for "no".
8+
"""
9+
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
10+
11+
while True:
12+
choice = ''
13+
try:
14+
choice = inputimeout(prompt=question, timeout=5)
15+
except TimeoutOccurred:
16+
sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n')")
17+
sys.stdout.flush()
18+
19+
if choice in valid:
20+
return valid[choice]

0 commit comments

Comments
 (0)