10000 Implemented binary transfer to handle emoji and other non-ascii chara… · arduino/micropython.js@ec191df · GitHub
[go: up one dir, main page]

Skip to content

Commit ec191df

Browse files
committed
Implemented binary transfer to handle emoji and other non-ascii characters.
Signed-off-by: ubi de feo <me@ubidefeo.com>
1 parent 5d42cfd commit ec191df

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

examples/07_save_file.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let content = `
44
"""
55
Test
66
"""
7+
# Emoji TEST 💩🤯🫶🏼
78
89
from time import sleep
910
from machine import Pin

examples/big_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# When the following number is sampled at four consecutive
3737
# even-numbered bits it will have two bits set, but sampling at four
3838
# consecutive odd-numbered bits will only yield one bit set.
39-
39+
# 😌🥲🙏🏼💩
4040
_WAVE_MAGIC = 0b0000011100000111
4141

4242
class Stepper:

examples/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Test
33
"""
4-
4+
# 🙏🏼
55
from time import sleep
66

77
print("start OK \r\n")

micropython.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,17 @@ class MicroPythonBoard {
266266
async fs_put(src, dest, data_consumer) {
267267
data_consumer = data_consumer || function() {}
268268
if (src && dest) {
269-
const contentBuffer = fs.readFileSync(path.resolve(src))
270-
let contentString = contentBuffer.toString()
271-
contentString = fixLineBreak(contentString)
272-
const hexArray = contentString.split('').map(
273-
c => c.charCodeAt(0).toString(16).padStart(2, '0')
274-
)
269+
const fileContent = fs.readFileSync(path.resolve(src), 'binary')
270+
const contentBuffer = Buffer.from(fileContent, 'binary')
275271
let out = ''
276272
out += await this.enter_raw_repl()
277-
out += await this.exec_raw(`f=open('${dest}','w')\nw=f.write`)
273+
out += await this.exec_raw(`f=open('${dest}','wb')\nw=f.write`)
278274
const chunkSize = 48
279-
for (let i = 0; i < hexArray.length; i+= chunkSize) {
280-
let slice = hexArray.slice(i, i+chunkSize)
281-
let bytes = slice.map(h => `0x${h}`)
282-
let line = `w(bytes([${bytes.join(',')}]))`
275+
for (let i = 0; i < contentBuffer.length; i+= chunkSize) {
276+
let slice = Uint8Array.from(contentBuffer.subarray(i, i+chunkSize))
277+
let line = `w(bytes([${slice}]))`
283278
out += await this.exec_raw(line)
284-
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
279+
data_consumer( parseInt((i / contentBuffer.length) * 100) + '%')
285280
}
286281
out += await this.exec_raw(`f.close()`)
287282
out += await this.exit_raw_repl()
@@ -293,20 +288,16 @@ class MicroPythonBoard {
293288
async fs_save(content, dest, data_consumer) {
294289
data_consumer = data_consumer || function() {}
295290
if (content && dest) {
296-
let contentString = fixLineBreak(content)
297-
const hexArray = contentString.split('').map(
298-
c => c.charCodeAt(0).toString(16).padStart(2, '0')
299-
)
291+
const contentBuffer = Buffer.from(content, 'utf-8')
300292
let out = ''
301293
out += await this.enter_raw_repl()
302-
out += await this.exec_raw(`f=open('${dest}','w')\nw=f.write`)
294+
out += await this.exec_raw(`f=open('${dest}','wb')\nw=f.write`)
303295
const chunkSize = 48
304-
for (let i = 0; i < hexArray.length; i+= chunkSize) {
305-
let slice = hexArray.slice(i, i+chunkSize)
306-
let bytes = slice.map(h => `0x${h}`)
307-
let line = `w(bytes([${bytes.join(',')}]))`
296+
for (let i = 0; i < contentBuffer.length; i+= chunkSize) {
297+
let slice = Uint8Array.from(contentBuffer.subarray(i, i+chunkSize))
298+
let line = `w(bytes([${slice}]))`
308299
out += await this.exec_raw(line)
309-
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
300+
data_consumer( parseInt((i / contentBuffer.length) * 100) + '%')
310301
}
311302
out += await this.exec_raw(`f.close()`)
312303
out += await this.exit_raw_repl()

0 commit comments

Comments
 (0)
1559
0