-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement reading from a serial port
- Loading branch information
1 parent
58e6685
commit ae18256
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import gleam/javascript/array.{Array} | ||
import gleam/javascript/promise.{Promise} | ||
pub type SerialPort | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/Serial/requestPort | ||
@external(javascript, "../../serial_ffi.mjs", "requestPort") | ||
pub fn request_port() -> Promise(Result(SerialPort, Nil)) | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/Serial/getPorts | ||
@external(javascript, "../../serial_ffi.mjs", "getPorts") | ||
pub fn get_ports() -> Promise(Result(List(SerialPort), Nil)) | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/SerialPort/getInfo | ||
@external(javascript, "../../serial_ffi.mjs", "getInfo") | ||
pub fn get_info(port: SerialPort) -> Result(#(Int, Int), Nil) | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/SerialPort/open | ||
@external(javascript, "../../serial_ffi.mjs", "open") | ||
pub fn open(port: SerialPort, baud_rate: Int) -> Promise(Result(Nil, Nil)) | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/SerialPort#reading_data_from_a_port | ||
@external(javascript, "../../serial_ffi.mjs", "read") | ||
pub fn read(port: SerialPort, callback: fn(BitString) -> Nil) -> Promise(Result(Nil, Nil)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Ok, Error, BitString } from "./gleam.mjs"; | ||
|
||
export async function requestPort() { | ||
try { | ||
const port = await globalThis.navigator.serial.requestPort({ filters: [] }); | ||
return new Ok(port); | ||
} catch (error) { | ||
console.warn(error); | ||
return new Error(); | ||
} | ||
} | ||
|
||
export async function getPorts() { | ||
try { | ||
const ports = await globalThis.navigator.serial.getPorts(); | ||
return new Ok(ports); | ||
} catch (error) { | ||
console.warn(error); | ||
return new Error(); | ||
} | ||
} | ||
|
||
export function getInfo(port) { | ||
try { | ||
const { usbVendorId, usbProductId } = port.getInfo(); | ||
return new Ok([usbVendorId, usbProductId]); | ||
} catch (error) { | ||
console.warn(error); | ||
return new Error(); | ||
} | ||
} | ||
|
||
export async function open(port, baudRate) { | ||
try { | ||
await port.open({ baudRate }); | ||
return new Ok(); | ||
} catch (error) { | ||
console.warn(error); | ||
return new Error(); | ||
} | ||
} | ||
|
||
export async function read(port, callback) { | ||
while (port.readable) { | ||
const reader = port.readable.getReader(); | ||
try { | ||
while (true) { | ||
const { value, done } = await reader.read(); | ||
if (done) { | ||
// |reader| has been canceled. | ||
break; | ||
} | ||
callback(new BitString(value)); | ||
// Do something with |value|… | ||
} | ||
return new Ok(); | ||
} catch (error) { | ||
return new Error(); | ||
} finally { | ||
reader.releaseLock(); | ||
} | ||
} | ||
} |