Skip to content

Commit 46da6e6

Browse files
authored
Fix serial (#147)
- Serial console related enhancement and bug fix - will reconnect when re-plug in microcontroller - terminal will remain if connection lost - always dark theme option - option to soft reboot or not - will block sending code snippet if - code incomplete - repl not ready
1 parent d125189 commit 46da6e6

File tree

16 files changed

+520
-277
lines changed

16 files changed

+520
-277
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ REPL (Read-Evaluate-Print Loop) is a coding mode where you can interact with the
6262

6363
## About
6464

65-
Version: 2.2.0
65+
Version: 2.2.1
6666

6767
[CircuitPython](https://circuitpython.org/) is a version of Python that runs on microcontrollers and single-board computers. Its development is sponsored by [Adafruit](https://www.adafruit.com/).
6868

docs/index.html

Lines changed: 235 additions & 203 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ function App() {
7676
const {
7777
connectToSerialPort,
7878
sendDataToSerialPort,
79-
clearSerialOutput,
79+
addToSerialOutput,
8080
serialOutput,
81-
fullSerialHistory,
8281
serialReady,
8382
serial,
8483
} = useSerial();
@@ -147,9 +146,8 @@ function App() {
147146
// serial
148147
connectToSerialPort,
149148
sendDataToSerialPort,
150-
clearSerialOutput,
149+
addToSerialOutput,
151150
serialOutput,
152-
fullSerialHistory,
153151
serialReady,
154152
serial,
155153
sendCtrlC,

src/components/AppMenu.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import AppContext from "../AppContext";
66
import React, { useContext } from "react";
77

88
export default function AppMenu() {
9-
const { flexModel, helpTabSelection, openDirectory, connectToSerialPort } = useContext(AppContext);
9+
const { flexModel, helpTabSelection, openDirectory, connectToSerialPort, appConfig } = useContext(AppContext);
1010
const DARK_PURPLE = deepPurple[500];
1111
const DARK_GREY = grey[900];
1212

@@ -64,7 +64,7 @@ export default function AppMenu() {
6464
text: "Serial Port",
6565
handler: () => {
6666
console.log("clicked on menu item `Serial`");
67-
connectToSerialPort();
67+
connectToSerialPort(appConfig.config.serial_console.fresh_start_serial);
6868
},
6969
},
7070
],

src/components/Navigation.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const video_css = {
2626
};
2727

2828
export default function Navigation() {
29-
const { openDirectory, rootFolderDirectoryReady, serialReady, connectToSerialPort } = useContext(AppContext);
29+
const { openDirectory, rootFolderDirectoryReady, serialReady, connectToSerialPort, appConfig } =
30+
useContext(AppContext);
3031

3132
return (
3233
<Typography component="div" sx={{ margin: "20pt" }}>
@@ -51,7 +52,14 @@ export default function Navigation() {
5152
{rootFolderDirectoryReady ? "✅" : ""}
5253
</li>
5354
<li>
54-
Step 2. <Button onClick={connectToSerialPort}>Connect to Serial Port</Button>
55+
Step 2.{" "}
56+
<Button
57+
onClick={() => {
58+
connectToSerialPort(appConfig.config.serial_console.fresh_start_serial);
59+
}}
60+
>
61+
Connect to Serial Port
62+
</Button>
5563
{serialReady ? "✅" : ""}
5664
</li>
5765
{serialReady && rootFolderDirectoryReady ? (

src/components/RawConsole.jsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import MenuBar from "../utilComponents/MenuBar";
2828
import { selectTabById } from "../layout/layoutUtils";
2929
// Xterm
3030
import XtermConsole from "./XtermConsole";
31-
31+
// code snippet editor
3232
import SiblingWithBottomRightTab from "../utilComponents/SiblingWithBottomRightTab";
33+
import { isPythonIncomplete } from "../utilFunctions/serialHelpers";
3334

3435
const RawSerialWrite = ({
3536
text,
@@ -185,9 +186,7 @@ const RawSerialWrite = ({
185186

186187
const RawConsole = () => {
187188
const {
188-
fullSerialHistory,
189189
serialOutput,
190-
clearSerialOutput,
191190
serialReady,
192191
connectToSerialPort,
193192
sendCtrlC,
@@ -208,6 +207,9 @@ const RawConsole = () => {
208207
if (text.trim().length === 0) {
209208
return;
210209
}
210+
if (isPythonIncomplete(text)) {
211+
return;
212+
}
211213
sendCode(text);
212214
setCodeHistIndex(-1);
213215
setText("");
@@ -229,7 +231,9 @@ const RawConsole = () => {
229231
options: [
230232
{
231233
text: "Connect to Serial Port",
232-
handler: connectToSerialPort,
234+
handler: () => {
235+
connectToSerialPort(appConfig.config.serial_console.fresh_start_serial);
236+
},
233237
},
234238
{
235239
text: "Clear",
@@ -241,7 +245,7 @@ const RawConsole = () => {
241245
text: "Download Log",
242246
handler: () => {
243247
console.log("Download Log");
244-
downloadAsFile("serial log.txt", fullSerialHistory + serialOutput);
248+
downloadAsFile("serial log.txt", serialOutput);
245249
},
246250
},
247251
{
@@ -264,7 +268,7 @@ const RawConsole = () => {
264268
},
265269
];
266270

267-
return serialReady ? (
271+
return serialOutput.length > 0 ? (
268272
<TabTemplate title={serialReady ? serialTitle : "Not Connected"} menuStructure={menuStructure}>
269273
<Box sx={{ display: "flex", flexDirection: "column", height: "100%", overflowX: "hidden" }}>
270274
<Box sx={{ flex: 1, display: "flex", flexDirection: "column", overflow: "auto" }}>
@@ -327,7 +331,13 @@ const RawConsole = () => {
327331
</Box>
328332
</TabTemplate>
329333
) : (
330-
<Button onClick={connectToSerialPort}>Connect to Serial Port</Button>
334+
<Button
335+
onClick={() => {
336+
connectToSerialPort(appConfig.config.serial_console.fresh_start_serial);
337+
}}
338+
>
339+
Connect to Serial Port
340+
</Button>
331341
);
332342
};
333343

src/components/XtermConsole.jsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const invert_css = {
1212
};
1313

1414
const XtermConsole = ({ setSerialTitle, clearTrigger }) => {
15-
const { appConfig, sendDataToSerialPort, serial } = useContext(AppContext);
15+
const { appConfig, sendDataToSerialPort, serial, serialReady, addToSerialOutput } = useContext(AppContext);
1616

1717
const terminalOptions = {
1818
convertEol: true,
@@ -24,12 +24,11 @@ const XtermConsole = ({ setSerialTitle, clearTrigger }) => {
2424
const terminal = useRef(new Terminal(terminalOptions));
2525
const terminalRef = useRef(null);
2626
const fitAddon = new FitAddon();
27-
console.log(terminal.current);
2827

2928
useEffect(() => {
3029
/* terminal init */
3130
if (!terminalRef.current) {
32-
console.error("Error initializing terminal")
31+
console.error("Error initializing terminal");
3332
return;
3433
}
3534
if (!terminal.current.element) {
@@ -47,10 +46,10 @@ const XtermConsole = ({ setSerialTitle, clearTrigger }) => {
4746
// auto fit
4847
terminal.current.loadAddon(fitAddon);
4948
fitAddon.fit();
50-
const observer = new ResizeObserver(entries => {
49+
const observer = new ResizeObserver((entries) => {
5150
for (let entry of entries) {
5251
const { width, height } = entry.contentRect;
53-
console.log('Size changed:', width, height);
52+
console.log("Size changed:", width, height);
5453
fitAddon.fit();
5554
}
5655
});
@@ -75,6 +74,10 @@ const XtermConsole = ({ setSerialTitle, clearTrigger }) => {
7574
console.log("Clear terminal", clearTrigger);
7675
}, [clearTrigger]);
7776

77+
let isDarkTheme = JSON.parse(localStorage.getItem("isDarkTheme"));
78+
let always_dark = appConfig.config.serial_console.always_dark;
79+
let color_css = always_dark ? (isDarkTheme ? { ...invert_css } : {}) : invert_css;
80+
7881
return (
7982
<div
8083
ref={terminalRef}
@@ -83,7 +86,7 @@ const XtermConsole = ({ setSerialTitle, clearTrigger }) => {
8386
height: "100%",
8487
overflowY: "hidden",
8588
scrollbarColor: "#777 #000",
86-
...invert_css,
89+
...color_css,
8790
}}
8891
/>
8992
);

src/configs/serial_console.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"type": "number",
1010
"default": 12
1111
},
12+
"always_dark": {
13+
"title": "Serial console always use dark theme",
14+
"type": "boolean",
15+
"default": false
16+
},
1217
"show_snippet_editor": {
1318
"title": "Show code snippet editor",
1419
"description": "This is better for sending multiple lines of code to Python RELP",
@@ -21,11 +26,10 @@
2126
"type": "boolean",
2227
"default": false
2328
},
24-
"force_exec": {
25-
"title": "Enforce execution of sent code",
26-
"description": "When selected, this option ensures code executes instantly upon submission, interrupting any active processes. This configuration is applicable exclusively in code mode.",
29+
"fresh_start_serial": {
30+
"title": "Soft reboot after connected to serial.",
2731
"type": "boolean",
28-
"default": true
32+
"default": false
2933
}
3034
}
3135
}

src/docs/About.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## CircuitPython Online IDE
22

3-
Version: 2.2.0
3+
Version: 2.2.1
44

55
[CircuitPython](https://circuitpython.org/) is a version of Python that runs on microcontrollers and single-board computers. Its development is sponsored by [Adafruit](https://www.adafruit.com/).
66

0 commit comments

Comments
 (0)