|
1 | 1 | const { app, BrowserWindow, Menu, ipcMain, dialog } = require('electron')
|
| 2 | +const Micropython = require('micropython-ctl-cont').MicroPythonDevice |
2 | 3 | const path = require('path')
|
3 | 4 | const fs = require('fs')
|
4 | 5 | const openAboutWindow = require('about-window').default
|
@@ -98,8 +99,121 @@ ipcMain.handle('rename-file', (event, filePath, newFilePath) => {
|
98 | 99 | return true
|
99 | 100 | })
|
100 | 101 |
|
101 |
| -// WINDOW MANAGEMENT |
| 102 | +// MICROPYTHON |
| 103 | +const board = new Micropython() |
| 104 | +ipcMain.handle('serial-list-ports', (event) => { |
| 105 | + 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 | + ] |
| 112 | +}) |
| 113 | +ipcMain.handle('serial-connect', async (event, path) => { |
| 114 | + console.log('ipcMain', 'serial-connect', path) |
| 115 | + await board.disconnect() |
| 116 | + await board.connectSerial(path) |
| 117 | + board.sendData(Buffer.from('\x03\x03')) |
| 118 | + board.onTerminalData = (d) => { |
| 119 | + win.webContents.send('terminal-data', d) |
| 120 | + } |
| 121 | + board.onclose = () => { |
| 122 | + win.webContents.send('disconnect') |
| 123 | + } |
| 124 | +}) |
| 125 | +ipcMain.handle('serial-disconnect', async (event) => { |
| 126 | + console.log('ipcMain', 'serial-disconnect') |
| 127 | + await board.disconnect() |
| 128 | +}) |
| 129 | +ipcMain.handle('serial-run', async (event, code) => { |
| 130 | + console.log('ipcMain', 'serial-run', code) |
| 131 | + win.webContents.send('terminal-data', '\r\n') |
| 132 | + try { |
| 133 | + const output = await board.runScript(code , { |
| 134 | + disableDedent: true, |
| 135 | + broadcastOutputAsTerminalData: true |
| 136 | + }) |
| 137 | + } catch(err) { |
| 138 | + console.log(err) |
| 139 | + } |
| 140 | + board.sendData('\r\n') |
| 141 | + return Promise.resolve(output) |
| 142 | +}) |
| 143 | +ipcMain.handle('serial-stop', async (event) => { |
| 144 | + console.log('ipcMain', 'serial-stop') |
| 145 | + board.sendData(Buffer.from('\r\x03\x03')) |
| 146 | + try { |
| 147 | + await board.readUntil('>>>', 5) |
| 148 | + } catch { |
| 149 | + // Might be stuck in raw repl. Try to exit into friendly repl with Ctrl+B |
| 150 | + try { |
| 151 | + await board.sendData('\r\x03\x02\r') |
| 152 | + } catch { |
| 153 | + console.log('could not stop') |
| 154 | + } |
| 155 | + } |
| 156 | +}) |
| 157 | +ipcMain.handle('serial-reset', async (event) => { |
| 158 | + console.log('ipcMain', 'serial-reset') |
| 159 | + await board.reset() |
| 160 | +}) |
| 161 | +ipcMain.handle('serial-eval', async (event, data) => { |
| 162 | + // console.log('ipcMain', 'serial-eval', data) |
| 163 | + board.sendData(Buffer.from(data)) |
| 164 | +}) |
| 165 | +ipcMain.handle('serial-list-files', async (event, folder) => { |
| 166 | + console.log('ipcMain', 'serial-list-files', folder) |
| 167 | + return await board.listFiles(folder) |
| 168 | +}) |
| 169 | +ipcMain.handle('serial-ilist-files', async (event, folder) => { |
| 170 | + console.log('ipcMain', 'serial-ilist-files', folder) |
| 171 | + let files = await board.listFiles(folder) |
| 172 | + files = files.map(f => ({ |
| 173 | + path: f.filename.slice(1), |
| 174 | + type: f.isDir ? 'folder' : 'file' |
| 175 | + })) |
| 176 | + // console.log('yolo', files) |
| 177 | + return files |
| 178 | +}) |
| 179 | +ipcMain.handle('serial-load-file', async (event, filePath) => { |
| 180 | + console.log('ipcMain', 'serial-load-file', filePath) |
| 181 | + const output = await board.getFile(filePath) |
| 182 | + return output.toString() |
| 183 | +}) |
| 184 | +ipcMain.handle('serial-remove-file', async (event, filePath) => { |
| 185 | + console.log('ipcMain', 'serial-remove-file', filePath) |
| 186 | + await board.remove(filePath) <
341A
/td> |
| 187 | +}) |
| 188 | +ipcMain.handle('serial-save-file', async (event, filePath, content) => { |
| 189 | + console.log('ipcMain', 'serial-save-file', filePath, content) |
| 190 | + await board.putFile(filePath, Buffer.from(content)) |
| 191 | +}) |
| 192 | +ipcMain.handle('serial-upload', async (event, src, dest) => { |
| 193 | + console.log('ipcMain', 'serial-upload-file', src, dest) |
| 194 | + <
D449
span class=pl-k>const content = fs.readFileSync(src) |
| 195 | + await board.putFile(dest, content) |
| 196 | +}) |
| 197 | +ipcMain.handle('serial-download', async (event, src, dest) => { |
| 198 | + console.log('ipcMain', 'serial-download-file', src, dest) |
| 199 | + const content = await board.getFile(src) |
| 200 | + fs.writeFileSync(dest, content) |
| 201 | +}) |
| 202 | +ipcMain.handle('serial-rename-file', async (event, src, dest) => { |
| 203 | + console.log('ipcMain', 'serial-rename-file', src, dest) |
| 204 | + await board.rename(src, dest) |
| 205 | +}) |
| 206 | +ipcMain.handle('serial-create-folder', async (event, folder) => { |
| 207 | + console.log('ipcMain', 'serial-create-folder', folder) |
| 208 | + try { |
| 209 | + await board.mkdir(folder) |
| 210 | + } catch(err) { |
| 211 | + console.log('error', err) |
| 212 | + } |
| 213 | +}) |
| 214 | + |
102 | 215 |
|
| 216 | +// WINDOW MANAGEMENT |
103 | 217 | ipcMain.handle('set-window-size', (event, minWidth, minHeight) => {
|
104 | 218 | console.log('ipcMain', 'set-window-size', minWidth, minHeight)
|
105 | 219 | if (!win) {
|
|
0 commit comments