8000 Import serialport to list available boards · arduino/lab-micropython-editor@75893c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 75893c2

Browse files
committed
Import serialport to list available boards
1 parent a242798 commit 75893c2

File tree

4 files changed

+242
-47
lines changed

4 files changed

+242
-47
lines changed

index.js

Lines changed: 15 additions & 19 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { app, BrowserWindow, Menu, ipcMain, dialog } = require('electron')
22
const Micropython = require('micropython-ctl-cont').MicroPythonDevice
3+
const { SerialPort } = require('serialport')
34
const path = require('path')
45
const fs = require('fs')
56
const openAboutWindow = require('about-window').default
@@ -42,7 +43,7 @@ function ilistFolder(folder) {
4243
}
4344

4445
// LOCAL FILE SYSTEM ACCESS
45-
ipcMain.handle('open-folder', async (event) => {
46+
ipcMain.handle('disk-open-folder', async (event) => {
4647
console.log('ipcMain', 'open-folder')
4748
const folder = await openFolderDialog()
4849
let files = []
@@ -52,31 +53,31 @@ ipcMain.handle('open-folder', async (event) => {
5253
return { folder, files }
5354
})
5455

55-
ipcMain.handle('list-files', async (event, folder) => {
56+
ipcMain.handle('disk-list-files', async (event, folder) => {
5657
console.log('ipcMain', 'list-files', folder)
5758
if (!folder) return []
5859
return listFolder(folder)
5960
})
6061

61-
ipcMain.handle('ilist-files', async (event, folder) => {
62+
ipcMain.handle('disk-ilist-files', async (event, folder) => {
6263
console.log('ipcMain', 'ilist-files', folder)
6364
if (!folder) return []
6465
return ilistFolder(folder)
6566
})
6667

67-
ipcMain.handle('load-file', (event, filePath) => {
68+
ipcMain.handle('disk-load-file', (event, filePath) => {
6869
console.log('ipcMain', 'load-file', filePath)
6970
let content = fs.readFileSync(filePath)
7071
return content
7172
})
7273

73-
ipcMain.handle('save-file', (event, filePath, content) => {
74+
ipcMain.handle('disk-save-file', (event, filePath, content) => {
7475
console.log('ipcMain', 'save-file', filePath, content)
7576
fs.writeFileSync(filePath, content, 'utf8')
7677
return true
7778
})
7879

79-
ipcMain.handle('update-folder', (event, folder) => {
80+
ipcMain.handle('disk-update-folder', (event, folder) => {
8081
console.log('ipcMain', 'update-folder', folder)
8182
let files = fs.readdirSync(path.resolve(folder))
8283
// Filter out directories
@@ -87,13 +88,13 @@ ipcMain.handle('update-folder', (event, folder) => {
8788
return { folder, files }
8889
})
8990

90-
ipcMain.handle('remove-file', (event, filePath) => {
91+
ipcMain.handle('disk-remove-file', (event, filePath) => {
9192
console.log('ipcMain', 'remove-file', filePath)
9293
fs.unlinkSync(filePath)
9394
return true
9495
})
9596

96-
ipcMain.handle('rename-file', (event, filePath, newFilePath) => {
97+
ipcMain.handle('disk-rename-file', (event, filePath, newFilePath) => {
9798
console.log('ipcMain', 'rename-file', filePath, newFilePath)
9899
fs.renameSync(filePath, newFilePath)
99100
return true
@@ -103,12 +104,7 @@ ipcMain.handle('rename-file', (event, filePath, newFilePath) => {
103104
const board = new Micropython()
104105
ipcMain.handle('serial-list-ports', (event) => {
105106
console.log('ipcMain', 'serial-list-ports')
106-
return [
107-
{ path: '/dev/ttyACM0' },
108-
{ path: '/dev/ttyACM1' },
109-
{ path: '/dev/ttyUSB0' },
110-
{ path: '/dev/ttyUSB1' },
111-
]
107+
return SerialPort.list()
112108
})
113109
ipcMain.handle('serial-connect', async (event, path) => {
114110
console.log('ipcMain', 'serial-connect', path)
@@ -134,11 +130,12 @@ ipcMain.handle('serial-run', async (event, code) => {
134130
disableDedent: true,
135131
broadcastOutputAsTerminalData: true
136132
})
133+
board.sendData('\r\n')
134+
return Promise.resolve(output)
137135
} catch(err) {
138136
console.log(err)
137+
return Promise.reject(err)
139138
}
140-
board.sendData('\r\n')
141-
return Promise.resolve(output)
142139
})
143140
ipcMain.handle('serial-stop', async (event) => {
144141
console.log('ipcMain', 'serial-stop')
@@ -170,10 +167,9 @@ ipcMain.handle('serial-ilist-files', async (event, folder) => {
170167
console.log('ipcMain', 'serial-ilist-files', folder)
171168
let files = await board.listFiles(folder)
172169
files = files.map(f => ({
173-
path: f.filename.slice(1),
170+
path: f.filename.split('/').pop(),
174171
type: f.isDir ? 'folder' : 'file'
175172
}))
176-
// console.log('yolo', files)
177173
return files
178174
})
179175
ipcMain.handle('serial-load-file', async (event, filePath) => {
@@ -214,7 +210,7 @@ ipcMain.handle('serial-create-folder', async (event, folder) => {
214210

215211

216212
// WINDOW MANAGEMENT
217-
ipcMain.handle('set-window-size', (event, minWidth, minHeight) => {
213+
ipcMain.handle('window-set-minimum-size', (event, minWidth, minHeight) => {
218214
console.log('ipcMain', 'set-window-size', minWidth, minHeight)
219215
if (!win) {
220216
console.log('No window defined')

package-lock.json

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

0 commit comments

Comments
 (0)
0