Skip to content

Commit 0d7232c

Browse files
committed
Using timeouts instead of promises to write to serial
1 parent e76aab7 commit 0d7232c

File tree

1 file changed

+13
-57
lines changed

1 file changed

+13
-57
lines changed

micropython.js

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -190,39 +190,6 @@ class SerialConnection extends EventEmitter {
190190
_eventHandler(buffer) {
191191
const data = buffer.toString()
192192
this.emit('output', data)
193-
194-
// Getting data that should be sent to frontend
195-
// Loading file content, listing files, etc
196-
// if (data.indexOf('<REC>') !== -1) {
197-
// this.recordingData = true
198-
// }
199-
// if (this.recordingData) {
200-
// this.data += data
201-
// }
202-
// if (data.indexOf('<EOF>') !== -1) {
203-
// const iofREC = this.data.indexOf('<REC>')
204-
// const rec = this.data.indexOf('<REC>\r\n')+7
205-
// const eof = this.data.indexOf('<EOF>')
206-
// if (this.loadingFile) {
207-
// this.emit('file-loaded', this.data.slice(rec, eof))
208-
// this.loadingFile = false
209-
// }
210-
// if (this.loadingFileList) {
211-
// this.emit('file-list-loaded', this.data.slice(rec, eof))
212-
// this.loadingFileList = false
213-
// }
214-
// this.recordingData = false
215-
// }
216-
217-
if (this.rawRepl && data.indexOf('\n>>> ') != -1) {
218-
this.emit('execution-finished')
219-
this.rawRepl = false
220-
}
221-
222-
if (!this.rawRepl && data.indexOf('raw REPL;') != -1) {
223-
this.emit('execution-started')
224-
this.rawRepl = true
225-
}
226193
}
227194
/**
228195
* Put REPL in raw mode
@@ -241,32 +208,21 @@ class SerialConnection extends EventEmitter {
241208
* @param {String} command Command to be written on connected port
242209
*/
243210
_executeRaw(command) {
244-
const writePromise = (buffer) => {
245-
return new Promise((resolve, reject) => {
246-
setTimeout(() => {
247-
this.port.write(buffer, (err) => {
248-
if (err) return reject()
249-
resolve()
250-
})
251-
}, 1)
252-
})
253-
}
254-
const l = 1024
255-
let slices = []
256-
for(let i = 0; i < command.length; i+=l) {
257-
let slice = command.slice(i, i+l)
258-
slices.push(slice)
259-
}
211+
let p = 0
212+
const l = 256
260213
return new Promise((resolve, reject) => {
261-
slices.reduce((cur, next) => {
262-
return cur.then(() => {
263-
return writePromise(next)
264-
})
265-
}, Promise.resolve())
266-
.then()
267-
.then(() => {
214+
for(let i = 0; i < command.length; i+=l) {
215+
let slice = command.slice(i, i+l)
216+
setTimeout(() => {
217+
this.port.write(slice)
218+
}, p*10)
219+
p += 1
220+
}
221+
let finished = (command.length / l) + 1
222+
setTimeout(() => {
223+
this.port.write('\x04')
268224
resolve()
269-
})
225+
}, finished * 10)
270226
})
271227
}
272228
}

0 commit comments

Comments
 (0)