Skip to content

Commit

Permalink
Support CircuitPython with micro:bit, thonny#2251
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Apr 23, 2022
1 parent 77ae1bd commit 78f03aa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Version history
* Don't propose pythonw.exe as initial backend, #2236
* Fix error when trying to open a file from file browser with ENTER, #1785
* Fix support for remote Python 3 over SSH (regression introduced in one of the 4.0.0 betas), #2249
* Support CircuitPython with micro:bit, #2251

4.0.0b2 (2022-04-09)
====================
Expand Down
4 changes: 4 additions & 0 deletions thonny/plugins/circuitpython/cirpy_front.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def _is_for_circuitpython(cls):

@classmethod
def _is_potential_port(cls, p):
# micro:bit's v2's CircuitPython does not have CircuitPython's port attributes
if (p.vid, p.pid) == (0x0D28, 0x0204):
return True

if "adafruit_board_toolkit" in sys.modules or sys.platform == "linux":
# can trust p.interface value
return "CircuitPython CDC " in (p.interface or "")
Expand Down
14 changes: 7 additions & 7 deletions thonny/plugins/micropython/bare_metal_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _check_prepare(self):
self._prepare_after_soft_reboot(False)

def _get_helper_code(self):
if self._connected_to_microbit():
if self._using_microbit_micropython():
return super()._get_helper_code()

result = super()._get_helper_code()
Expand Down Expand Up @@ -328,7 +328,7 @@ def _sync_time(self):

now = self._get_time_for_rtc()

if self._connected_to_microbit():
if self._using_microbit_micropython():
return
elif self._connected_to_circuitpython():
if "rtc" not in self._builtin_modules:
Expand Down Expand Up @@ -387,7 +387,7 @@ def _sync_time(self):
print("WARNING: Could not sync device's clock: " + val)

def _get_utc_timetuple_from_device(self) -> Union[tuple, str]:
if self._connected_to_microbit():
if self._using_microbit_micropython():
return "This device does not have a real-time clock"
elif self._connected_to_circuitpython():
specific_script = dedent(
Expand Down Expand Up @@ -449,7 +449,7 @@ def _get_actual_time_tuple_on_device(self):
return self._evaluate(script)

def _update_cwd(self):
if self._connected_to_microbit():
if self._using_microbit_micropython():
self._cwd = ""
else:
super()._update_cwd()
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def _cmd_execute_system_command(self, cmd):
return {"returncode": returncode}

def _cmd_get_fs_info(self, cmd):
if self._connected_to_microbit():
if self._using_microbit_micropython():
used = self._evaluate(
dedent(
"""
Expand Down Expand Up @@ -1328,7 +1328,7 @@ def __W(x):
"""
)
)
elif self._connected_to_microbit():
elif self._using_microbit_micropython():
# doesn't have neither BytesIO.flush, nor os.sync
self._execute_without_output(
dedent(
Expand Down Expand Up @@ -1613,7 +1613,7 @@ def _decode(self, data: bytes) -> str:
def _get_file_operation_block_size(self):
# don't forget that the size may be expanded up to 4x where converted to Python
# bytes literal
if self._connected_to_microbit():
if self._using_microbit_micropython():
return 512
else:
return 1024
Expand Down
11 changes: 6 additions & 5 deletions thonny/plugins/micropython/mp_back.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _prepare_rtc(self):
self._validate_time()

def _check_perform_just_in_case_gc(self):
if self._connected_to_microbit():
if self._using_microbit_micropython():
# May fail to allocate memory without this
self._perform_gc()

Expand Down Expand Up @@ -354,11 +354,12 @@ def _handle_normal_command(self, cmd: CommandToBackend) -> None:
def _check_for_connection_error(self) -> None:
self.get_connection().check_for_error()

def _connected_to_microbit(self):
def _using_microbit_micropython(self):
if not self._welcome_text:
return None

return "micro:bit" in self._welcome_text.lower()
# Don't confuse MicroPython and CircuitPython
return "micro:bit" in self._welcome_text.lower() and "MicroPython" in self._welcome_text

def _connected_to_pyboard(self):
if not self._welcome_text:
Expand Down Expand Up @@ -403,7 +404,7 @@ def _fetch_builtins_info(self):
return {}

def _fetch_epoch_year(self):
if self._connected_to_microbit():
if self._using_microbit_micropython():
return None

if self._connected_to_circuitpython() and "rtc" not in self._builtin_modules:
Expand Down Expand Up @@ -443,7 +444,7 @@ def _fetch_epoch_year(self):
return result

def _update_cwd(self):
if not self._connected_to_microbit():
if not self._using_microbit_micropython():
logger.debug("Updating cwd")
self._cwd = self._evaluate("__thonny_helper.getcwd()")

Expand Down

0 comments on commit 78f03aa

Please sign in to comment.