Skip to content

Commit

Permalink
v0.1.5: minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed Dec 10, 2022
1 parent 2d58e68 commit bce3b6f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pn532.js",
"version": "0.1.4",
"version": "0.1.5",
"author": "taichunmin <[email protected]>",
"browser": "dist/pn532.min.js",
"description": "pn532.js is a JavaScript library for PN532 base on Web Bluetooth and Web Serial.",
Expand Down
40 changes: 20 additions & 20 deletions src/plugin/WebbleAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ export default class Pn532WebbleAdapter {

const deviceOptionalServices = _.uniq(_.map(BLESERIAL_UUID, 'serv'))

// register TransformStream
pn532.tx = new TransformStream({
flush: async controller => {
await disconnect()
controller.terminate()
},
transform: async (pack, controller) => {
// https://stackoverflow.com/questions/38913743/maximum-packet-length-for-bluetooth-le
// 20 bytes are left for the attribute data
for (const chunk of pack.chunk(20)) controller.enqueue(chunk)
},
})
pn532.tx.readable.pipeTo(new WritableStream({ // no wait
write: async chunk => {
if (!me.charWrite) throw new Error('me.charWrite can not be null')
await me.charWrite.writeValueWithResponse(chunk.buffer)
},
}, new CountQueuingStrategy({ highWaterMark: 1 })))

/**
* Determines whether Web Bluetooth API is supported.
* @memberof Pn532WebbleAdapter
Expand Down Expand Up @@ -74,7 +93,7 @@ export default class Pn532WebbleAdapter {
const pack = new Packet(event?.target?.value?.buffer)
if (debug) utils.logTime(`gattOnNotify = ${pack.inspect}`)
const writer = pn532.rx.writable.getWriter()
writer.write(pack)
await writer.write(pack)
writer.releaseLock()
}

Expand Down Expand Up @@ -149,25 +168,6 @@ export default class Pn532WebbleAdapter {
await me.charNotify.startNotifications()
utils.logTime(`gatt connected, serv = 0x${uuid.serv.toString(16)}, notify = 0x${uuid.notify.toString(16)}, write = 0x${uuid.write.toString(16)}`)

// register TransformStream
pn532.tx = new TransformStream({
flush: async controller => {
await disconnect()
controller.terminate()
},
transform: async (pack, controller) => {
// https://stackoverflow.com/questions/38913743/maximum-packet-length-for-bluetooth-le
// 20 bytes are left for the attribute data
for (const chunk of pack.chunk(20)) controller.enqueue(chunk)
},
})
pn532.tx.readable.pipeTo(new WritableStream({ // no wait
write: async chunk => {
// console.log(`me.charWrite.writeValueWithResponse('${chunk.inspect}')`)
await me.charWrite.writeValueWithResponse(chunk.buffer)
},
}, new CountQueuingStrategy({ highWaterMark: 1 })))

me._isOpen = true
break
}
Expand Down
41 changes: 25 additions & 16 deletions src/plugin/WebserialAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ export default class Pn532WebserialAdapter {

if (pn532.$adapter) throw new Error('adapter already exists')

// register TransformStream
pn532.tx = new TransformStream({
flush: async controller => {
await disconnect()
controller.terminate()
},
transform: async (pack, controller) => {
controller.enqueue(pack)
},
})
pn532.tx.readable.pipeTo(new WritableStream({ // no wait
write: async chunk => {
if (!me.port) throw new Error('me.port can not be null')
const writer = me.port.writable.getWriter()
await writer.write(chunk)
writer.releaseLock()
},
}, new CountQueuingStrategy({ highWaterMark: 1 })))

function getSerial () {
return navigator?.serial ?? serialPolyfill
}
Expand Down Expand Up @@ -100,25 +119,15 @@ export default class Pn532WebserialAdapter {
await me.port.open({ baudRate: 115200 })
me.port.addEventListener('disconnect', disconnect)

for (let i = 0; !isOpen() && i < 100; i++) {
if (!me.port?.readable || !me.port?.writable) {
await utils.sleep(10)
continue
for (let i = 0; i < 100; i++) { // wait for serial open
if (me.port?.readable && me.port?.writable) {
me._isOpen = true
break
}
pn532.tx = new TransformStream({
flush: async controller => {
await disconnect()
controller.terminate()
},
transform: async (pack, controller) => {
controller.enqueue(pack)
},
})
pn532.tx.readable.pipeTo(me.port.writable) // no wait
me.port.readable.pipeTo(pn532.rx.writable) // no wait
me._isOpen = true
await utils.sleep(10)
}
if (isOpen()) {
me.port.readable.pipeTo(pn532.rx.writable) // no wait
await pn532.sendCommandWakeup()
await pn532.resetSettings()
} else {
Expand Down

0 comments on commit bce3b6f

Please sign in to comment.