Skip to content

Commit 5aee374

Browse files
committed
Move serial logic to separate file
1 parent 8c13b4c commit 5aee374

File tree

3 files changed

+108
-106
lines changed

3 files changed

+108
-106
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

56
module.exports = function registerMenu(win, state = {}) {
@@ -91,10 +92,8 @@ module.exports = function registerMenu(win, state = {}) {
9192
accelerator: '',
9293
click: async () => {
9394
try {
94-
win.webContents.send('cleanup-before-reload')
95-
setTimeout(() => {
96-
win.reload()
97-
}, 500)
95+
await Serial.disconnect()
96+
win.reload()
9897
} catch(e) {
9998
console.error('Reload from menu failed:', e)
10099
}

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: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,9 @@
11
console.log('preload')
22
const { contextBridge, ipcRenderer } = require('electron')
33
const path = require('path')
4+
const Serial = require('./backend/serial.js')
45

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

1078
const Disk = {
1089
openFolder: async () => {
@@ -202,4 +103,4 @@ const Window = {
202103

203104
contextBridge.exposeInMainWorld('BridgeSerial', Serial)
204105
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
205-
contextBridge.exposeInMainWorld('BridgeWindow', Window)
106+
contextBridge.exposeInMainWorld('BridgeWindow', Window)

0 commit comments

Comments
 (0)