aboutsummaryrefslogtreecommitdiff
path: root/web/index.html
blob: 4374db026ded41765aa285109f0fde0cfd09135f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/pyodide/v0.19.1/full/pyodide.js"></script>
  </head>

  <body>
    <button onclick="runUSB()">USB</button>
    <button onclick="runSerial()">Serial</button>
    <br />
    <br />
    <div>Output:</div>
    <textarea id="output" style="width: 100%;" rows="30" disabled></textarea>

    <script>
        function bufferToHex(buffer) {
            return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('');
        }

        const output = document.getElementById("output");
        const code = document.getElementById("code");

        function addToOutput(s) {
            output.value += s + "\n";
        }

      output.value = "Initializing...\n";

      async function main() {
          let pyodide = await loadPyodide({
              indexURL: "https://cdn.jsdelivr.net/pyodide/v0.19.1/full/",
          })
          output.value += "Ready!\n"

          return pyodide;
      }

      let pyodideReadyPromise = main();

      async function readLoop(port, packet_source) {
        const reader = port.readable.getReader()
            try {
                while (true) {
                    console.log('@@@ Reading...')
                    const { done, value } = await reader.read()
                    if (done) {
                        console.log("--- DONE!")
                        break
                    }

                    console.log('@@@ Serial data:', bufferToHex(value))
                    if (packet_source.delegate !== undefined) {
                        packet_source.delegate.data_received(value)
                    } else {
                        console.warn('@@@ delegate not set yet, dropping data')
                    }
                }
            } catch (error) {
                console.error(error)
            } finally {
                reader.releaseLock()
            }
      }

      async function runUSB() {
        const device = await navigator.usb.requestDevice({
          filters: [
            {
                classCode: 0xE0,
                subclassCode: 0x01
            }
          ]
        });

        if (device.configuration === null) {
          await device.selectConfiguration(1);
        }
        await device.claimInterface(0)
      }

      async function runSerial() {
        const ports = await navigator.serial.getPorts()
          console.log('Paired ports:', ports)

          const port = await navigator.serial.requestPort()
          await port.open({ baudRate: 1000000 })
          const writer = port.writable.getWriter()
      }

      async function run() {

          let pyodide = await pyodideReadyPromise;
          try {
              const script = await(await fetch('scanner.py')).text()
              await pyodide.loadPackage('micropip')
              await pyodide.runPythonAsync(`
                  import micropip
                  await micropip.install('../dist/bumble-0.0.36.dev0+g3adbfe7.d20210807-py3-none-any.whl')
              `)
              let output = await pyodide.runPythonAsync(script)
              addToOutput(output)

              const pythonMain = pyodide.globals.get('main')
              const packet_source = {}
              const packet_sink = {
                  on_packet: (packet) => {
                    // Variant A, with the conversion done in Javascript
                      const buffer = packet.toJs()
                      console.log(`$$$ on_packet: ${bufferToHex(buffer)}`)
                      // TODO: create an sync queue here instead of blindly calling write without awaiting
                      /*await*/ writer.write(buffer)
                      packet.destroy()

                    // Variant B, with the conversion `to_js` done at the Python layer
                    // console.log(`$$$ on_packet: ${bufferToHex(packet)}`)
                    //   /*await*/ writer.write(packet)
                }
              }
              serialLooper = readLoop(port, packet_source)
              pythonResult = await pythonMain(packet_source, packet_sink)
              console.log(pythonResult)
              serialResult = await serialLooper
              writer.releaseLock()
              await port.close()
              console.log('### done')
        } catch (err) {
          addToOutput(err);
        }
      }
    </script>
  </body>
</html>