@danidoble/webserial 4.1.0 → 4.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1 +1,148 @@
1
- # Webserial API
1
+ # Webserial API (Wrapper)
2
+
3
+ Opensource wrapper to simplify connect to serial devices over [Webserial API](https://wicg.github.io/serial/)
4
+
5
+ # Install
6
+
7
+ Using npm
8
+
9
+ ```shell
10
+ npm install @danidoble/webserial
11
+ ```
12
+
13
+ Using pnpm
14
+
15
+ ```shell
16
+ pnpm install @danidoble/webserial
17
+ ```
18
+
19
+ Using bun
20
+
21
+ ```shell
22
+ bun install @danidoble/webserial
23
+ ```
24
+
25
+ # Docs
26
+
27
+ If you want to use a custom or device or the other inside this package a good starter point would be [Here](https://webserial-docs.danidoble.com)
28
+
29
+ # Example
30
+
31
+ In this example we'll use Arduino interface
32
+
33
+ ```html
34
+ <!-- index.html -->
35
+
36
+ <!doctype html>
37
+ <html lang="es">
38
+ <head>
39
+ <meta charset="UTF-8" />
40
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
41
+ <title>Webserial</title>
42
+ <script src="arduino.js" type="module"></script>
43
+ </head>
44
+ <body class="bg-neutral-950 text-white p-4 w-full">
45
+ <div class="webserial w-full max-w-xl mx-auto grid grid-cols-1 gap-y-4">
46
+ <div class="my-6"></div>
47
+ <button id="connect" class="hidden px-4 py-3 bg-gray-800 rounded-lg">Connect to serial</button>
48
+
49
+ <div id="need-permission" class="hidden p-4 bg-rose-900 rounded-lg">
50
+ Woooah! It seems that you need to give permission to access the serial port. Please, click the button 'Connect
51
+ to serial' to try again.
52
+ </div>
53
+
54
+ <div id="disconnected" class="hidden p-4 bg-neutral-900 w-full">
55
+ The arduino is disconnected. Please, check the connection.
56
+ </div>
57
+
58
+ <div id="unsupported" class="hidden p-4 bg-orange-700 w-full absolute bottom-0 left-0">
59
+ This browser does not support the WebSerial API. Please, use a compatible browser.
60
+ </div>
61
+
62
+ <div id="log" class="bg-neutral-800 p-4 rounded-lg">Log: <br /></div>
63
+ </div>
64
+
65
+ <script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,container-queries"></script>
66
+ </body>
67
+ </html>
68
+ ```
69
+
70
+ ```js
71
+ // arduino.js
72
+
73
+ import { Arduino } from '@danidoble/webserial';
74
+
75
+ const arduino = new Arduino();
76
+ arduino.on('serial:message', (data) => {
77
+ console.log(data.detail);
78
+ });
79
+
80
+ arduino.on('serial:timeout', (data) => {
81
+ console.log('serial:timeout', data.detail);
82
+ });
83
+
84
+ // arduino.on('serial:sent', data => {
85
+ // console.log('serial:sent',data.detail);
86
+ // });
87
+
88
+ arduino.on('serial:error', (event) => {
89
+ document.getElementById('log').innerText += event.detail.message + '\n\n';
90
+ });
91
+
92
+ // eslint-disable-next-line no-unused-vars
93
+ arduino.on('serial:disconnected', (event) => {
94
+ document.getElementById('log').innerText += 'Disconnected\n\n';
95
+
96
+ document.getElementById('disconnected').classList.remove('hidden');
97
+ document.getElementById('connect').classList.remove('hidden');
98
+ });
99
+
100
+ // eslint-disable-next-line no-unused-vars
101
+ arduino.on('serial:connecting', (event) => {
102
+ document.getElementById('log').innerText += 'Connecting\n\n';
103
+ });
104
+
105
+ // eslint-disable-next-line no-unused-vars
106
+ arduino.on('serial:connected', (event) => {
107
+ document.getElementById('log').innerText += 'Connected\n\n';
108
+
109
+ document.getElementById('disconnected').classList.add('hidden');
110
+ document.getElementById('need-permission').classList.add('hidden');
111
+ document.getElementById('connect').classList.add('hidden');
112
+ });
113
+
114
+ // eslint-disable-next-line no-unused-vars
115
+ arduino.on('serial:need-permission', (event) => {
116
+ document.getElementById('disconnected').classList.remove('hidden');
117
+ document.getElementById('need-permission').classList.remove('hidden');
118
+ document.getElementById('connect').classList.remove('hidden');
119
+ });
120
+
121
+ // eslint-disable-next-line no-unused-vars
122
+ arduino.on('serial:soft-reload', (event) => {
123
+ // reset your variables
124
+ });
125
+
126
+ // eslint-disable-next-line no-unused-vars
127
+ arduino.on('serial:unsupported', (event) => {
128
+ document.getElementById('unsupported').classList.remove('hidden');
129
+ });
130
+
131
+ function tryConnect() {
132
+ arduino
133
+ .connect()
134
+ .then(() => {})
135
+ .catch(console.error);
136
+ }
137
+
138
+ document.addEventListener('DOMContentLoaded', () => {
139
+ tryConnect();
140
+ document.getElementById('connect').addEventListener('click', tryConnect);
141
+ });
142
+
143
+ // get device instance after
144
+ // import {Devices} from '@danidoble/webserial'
145
+ // const myDevice = Devices.getArduino(1);
146
+
147
+ // window.arduino = arduino;
148
+ ```
package/dist/webserial.js CHANGED
@@ -23,10 +23,13 @@ function _a() {
23
23
  }
24
24
  const ga = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), qn = { randomUUID: ga };
25
25
  function ba(r, n, t) {
26
- if (qn.randomUUID && !n && !r)
26
+ var i;
27
+ if (qn.randomUUID && !r)
27
28
  return qn.randomUUID();
28
29
  r = r || {};
29
- const e = r.random || (r.rng || _a)();
30
+ const e = r.random ?? ((i = r.rng) == null ? void 0 : i.call(r)) ?? _a();
31
+ if (e.length < 16)
32
+ throw new Error("Random bytes length must be >= 16");
30
33
  return e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128, pa(e);
31
34
  }
32
35
  class fi extends EventTarget {
@@ -7222,7 +7225,7 @@ g = new WeakSet(), Zr = function() {
7222
7225
  return t.push("03"), t;
7223
7226
  }, es = async function() {
7224
7227
  if (this.__internal__.dispense.elevator.locking_interval) return;
7225
- this.__internal__.dispense.elevator.need_reset && (this.__internal__.dispense.elevator.need_reset = !1, await this.resetWaitingProductRemovedError(), await ut(500)), await this.collect();
7228
+ this.__internal__.dispense.elevator.need_reset && (this.__internal__.dispense.elevator.need_reset = !1, await this.resetWaitingProductRemovedError(), await ut(500));
7226
7229
  const t = this;
7227
7230
  return this.__internal__.dispense.status = "elevator-locked", this.__internal__.dispense.elevator.locking_time = 60, new Promise((e) => {
7228
7231
  t.__internal__.dispense.elevator.locking_interval = setInterval(() => {
@@ -7510,8 +7513,9 @@ g = new WeakSet(), Zr = function() {
7510
7513
  type_degrees: null,
7511
7514
  formatted: null,
7512
7515
  decimal_point: t[7] === "2e" ? "." : null,
7513
- degrees: t[9] === "7f" ? "°" : null
7514
- }, t[4] === "2b" ? e.additional.sign = t[4] = "+" : ["2e", "2d"].includes(t[4]) && (e.additional.sign = t[4] = "-"), this.hexToDec(t[5]) >= 48 && this.hexToDec(t[5]) <= 57 && (e.additional.tens = this.hexToDec(t[5]) - 48), this.hexToDec(t[6]) >= 48 && this.hexToDec(t[6]) <= 57 && (e.additional.units = this.hexToDec(t[6]) - 48), this.hexToDec(t[8]) >= 48 && this.hexToDec(t[8]) <= 57 && (e.additional.decimals = this.hexToDec(t[8]) - 48), t[10] === "43" ? e.additional.type_degrees = "C" : t[10] === "46" && (e.additional.type_degrees = "F"), e.additional.formatted = (e.additional.sign ?? "") + (e.additional.tens ?? "") + (e.additional.units ?? "") + (e.additional.decimal_point ?? "") + (e.additional.decimals ?? "") + (e.additional.degrees ?? "") + (e.additional.type_degrees ?? ""), e.description = `The current temperature is ${e.additional.formatted}`, this.dispatch("temperature:current", e.additional), e;
7516
+ degrees: t[9] === "7f" ? "°" : null,
7517
+ error: null
7518
+ }, t[4] === "2b" ? e.additional.sign = t[4] = "+" : ["2e", "2d"].includes(t[4]) ? e.additional.sign = t[4] = "-" : t[4] === "20" && (e.additional.error = "Error in thermometer"), this.hexToDec(t[5]) >= 48 && this.hexToDec(t[5]) <= 57 ? e.additional.tens = this.hexToDec(t[5]) - 48 : t[5] === "2a" && (e.additional.error = "Error in thermometer"), this.hexToDec(t[6]) >= 48 && this.hexToDec(t[6]) <= 57 ? e.additional.units = this.hexToDec(t[6]) - 48 : t[6] === "2a" && (e.additional.error = "Error in thermometer"), this.hexToDec(t[8]) >= 48 && this.hexToDec(t[8]) <= 57 ? e.additional.decimals = this.hexToDec(t[8]) - 48 : t[8] === "2a" && (e.additional.error = "Error in thermometer"), t[10] === "43" ? e.additional.type_degrees = "C" : t[10] === "46" && (e.additional.type_degrees = "F"), e.additional.error === "Error in thermometer" ? (e.additional.formatted = "Error in thermometer", e.description = "The current temperature cannot be read because there is an error in the thermometer") : (e.additional.formatted = (e.additional.sign ?? "") + (e.additional.tens ?? "") + (e.additional.units ?? "") + (e.additional.decimal_point ?? "") + (e.additional.decimals ?? "") + (e.additional.degrees ?? "") + (e.additional.type_degrees ?? ""), e.description = `The current temperature is ${e.additional.formatted}`), this.dispatch("temperature:current", e.additional), e;
7515
7519
  }, Ss = function(t, e, i = 128) {
7516
7520
  if (t[1] && (e.additional.machine.hex = t[1], e.additional.machine.dec = this.hexToDec(t[1]) - i), !(t[1] && t[2]))
7517
7521
  e = o(this, g, ns).call(this, t, e);
@@ -8552,7 +8556,7 @@ const Sc = {
8552
8556
  wait: ut,
8553
8557
  getSeconds: tn,
8554
8558
  supportWebSerial: _i
8555
- }, kc = "4.1.0";
8559
+ }, kc = "4.1.1";
8556
8560
  export {
8557
8561
  Cc as Arduino,
8558
8562
  Tc as Boardroid,