8000 Move serial logic to separate file · arduino/lab-micropython-editor@42f8f9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 42f8f9c

Browse files
committed
Move serial logic to separate file
1 parent 48881ce commit 42f8f9c

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

backend/menu.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { app, Menu } = require('electron')
22
const path = require('path')
3+
const Serial = require('./serial.js')
34
const openAboutWindow = require('about-window').default
45
const shortcuts = require('./shortcuts.js')
56
const { type } = require('os')
@@ -127,10 +128,8 @@ module.exports = function registerMenu(win, state = {}) {
127128
accelerator: '',
128129
click: async () => {
129130
try {
130-
win.webContents.send('cleanup-before-reload')
131-
setTimeout(() => {
132-
win.reload()
133-
}, 500)
131+
await Serial.disconnect()
132+
win.reload()
134133
} catch(e) {
135134
console.error('Reload from menu failed:', e)
136135
}

backend/serial.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const MicroPython = require('micropython.js')
2+
3+
const board = new MicroPython()
4+
board.chunk_size = 192
5+
board.chunk_sleep = 200
6+
7+
const Serial = {
8+
loadPorts: async () => {
9+
let ports = await board.list_ports()
10+
return ports.filter(p => p.vendorId && p.productId)
11+
},
12+
connect: async (path) => {
13+
return board.open(path)
14+
},
15+
disconnect: async () => {
16+
return board.close()
17+
},
18+
run: async (code) => {
19+
return board.run(code)
20+
},
21+
execFile: async (path) => {
22+
return board.execfile(path)
23+
},
24+
getPrompt: async () => {
25+
return board.get_prompt()
26+
},
27+
keyboardInterrupt: async () => {
28+
await board.stop()
29+
return Promise.resolve()
30+
},
31+
reset: async () => {
32+
await board.stop()
33+
await board.exit_raw_repl()
34+
await board.reset()
35+
return Promise.resolve()
36+
},
37+
eval: (d) => {
38+
return board.eval(d)
39+
},
40+
onData: (fn) => {
41+
board.serial.on('data', fn)
42+
},
43+
listFiles: async (folder) => {
44+
return board.fs_ls(folder)
45+
},
46+
ilistFiles: async (folder) => {
47+
return board.fs_ils(folder)
48+
},
49+
loadFile: async (file) => {
50+
const output = await board.fs_cat_binary(file)
51+
return output || ''
52+
},
53+
removeFile: async (file) => {
54+
return board.fs_rm(file)
55+
},
56+
saveFileContent: async (filename, content, dataConsumer) => {
57+
return board.fs_save(content || ' ', filename, dataConsumer)
58+
},
59+
uploadFile: async (src, dest, dataConsumer) => {
60+
return board.fs_put(src, dest.replaceAll(path.win32.sep, path.posix.sep), dataConsumer)
61+
},
62+
downloadFile: async (src, dest) => {
63+
let contents = await Serial.loadFile(src)
64+
return ipcRenderer.invoke('save-file', dest, contents)
65+
},
66+
renameFile: async (oldName, newName) => {
67+
return board.fs_rename(oldName, newName)
68+
},
69+
onConnectionLost: async (fn) => {
70+
board.serial.on('close', fn)
71+
},
72+
createFolder: async (folder) => {
73+
return await board.fs_mkdir(folder)
74+
},
75+
removeFolder: async (folder) => {
76+
return await board.fs_rmdir(folder)
77+
},
78+
getNavigationPath: (navigation, target) => {
79+
return path.posix.join(navigation, target)
80+
},
81+
getFullPath: (root, navigation, file) => {
82+
return path.posix.join(root, navigation, file)
83+
},
84+
getParentPath: (navigation) => {
85+
return path.posix.dirname(navigation)
86+
},
87+
fileExists: async (filePath) => {
88+
// !!!: Fix this on micropython.js level
89+
// ???: Check if file exists is not part of mpremote specs
90+
const output = await board.run(`
91+
import os
92+
try:
93+
os.stat("${filePath}")
94+
print(0)
95+
except OSError:
96+
print(1)
97+
`)
98+
return output[2] === '0'
99+
}
100+
}
101+
102+
module.exports = Serial

preload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
console.log('preload')
22
const { contextBridge, ipcRenderer } = require('electron')
33
const path = require('path')
4+
const Serial = require('./backend/serial.js')
45
const shortcuts = require('./backend/shortcuts.js').global
56
const MicroPython = require('micropython.js')
67
const { emit, platform } = require('process')
@@ -191,4 +192,4 @@ const Window = {
191192

192193
contextBridge.exposeInMainWorld('BridgeSerial', Serial)
193194
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
194-
contextBridge.exposeInMainWorld('BridgeWindow', Window)
195+
contextBridge.exposeInMainWorld('BridgeWindow', Window)

0 commit comments

Comments
 (0)
0