led-matrix-controllers 0.1.0
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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/led-matrix-controllers.browser.mjs +2 -0
- package/dist/led-matrix-controllers.browser.mjs.map +1 -0
- package/package.json +23 -0
- package/src/hardware-constants.js +41 -0
- package/src/index.js +3 -0
- package/src/usb-hid/HIDControllerFactory.js +27 -0
- package/src/usb-hid/environments/web/HIDOperations.js +46 -0
- package/src/usb-hid/environments/web/device.js +36 -0
- package/src/usb-hid/environments/web/util.js +3 -0
- package/src/usb-hid/firmware/sparkle/SparkleController.js +132 -0
- package/src/usb-hid/firmware/sparkle/reports.js +37 -0
- package/src/usb-serial/SerialControllerFactory.js +57 -0
- package/src/usb-serial/environments/web/PortMutex.js +58 -0
- package/src/usb-serial/environments/web/PortOperations.js +66 -0
- package/src/usb-serial/environments/web/port.js +47 -0
- package/src/usb-serial/firmware/framework-official/DefaultController.js +89 -0
- package/src/usb-serial/firmware/framework-official/commands.js +36 -0
- package/src/usb-serial/firmware/sigroot/SigrootController.js +69 -0
- package/src/usb-serial/firmware/sigroot/commands.js +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jacob Padgett
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# LED Matrix Controllers
|
|
2
|
+
|
|
3
|
+
Cross-firmware controllers and tooling for the [Framework Laptop 16 LED matrix module](https://frame.work/products/16-led-matrix) using WebHID and Web Serial APIs
|
|
4
|
+
|
|
5
|
+
## Supported Firmwares
|
|
6
|
+
|
|
7
|
+
- https://github.com/FrameworkComputer/inputmodule-rs (Out-of-box default)
|
|
8
|
+
- https://github.com/vddCore/sparkle-fw16
|
|
9
|
+
- https://github.com/sigroot/FW_LED_Matrix_Firmware
|
|
10
|
+
|
|
11
|
+
If you maintain another firmware or a fork, please open an issue or submit a PR!
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### Installation
|
|
16
|
+
|
|
17
|
+
WIP
|
|
18
|
+
|
|
19
|
+
### Usage
|
|
20
|
+
|
|
21
|
+
WIP
|
|
22
|
+
|
|
23
|
+
## Development
|
|
24
|
+
|
|
25
|
+
### Prerequisites
|
|
26
|
+
|
|
27
|
+
* **Node.js** (v16.9.0 or later recommended for corepack)
|
|
28
|
+
|
|
29
|
+
### Installation
|
|
30
|
+
|
|
31
|
+
Setup development environment
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install -g corepack
|
|
35
|
+
corepack enable
|
|
36
|
+
yarn set version stable
|
|
37
|
+
yarn install
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Bundle (optional)
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
yarn build
|
|
44
|
+
```
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const t=9,e=34,r=12972,a=[50,172],i=32,n=[0,32],s=Object.freeze([0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,6,6,6,6,6,7,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13,14,14,14,15,15,16,16,17,17,17,18,18,19,19,20,20,21,22,22,23,23,24,24,25,26,26,27,27,28,29,29,30,31,32,32,33,34,34,35,36,37,38,38,39,40,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,66,67,68,69,70,71,73,74,75,76,78,79,80,82,83,84,86,87,88,90,91,93,94,96,97,99,100,102,103,105,106,108,110,111,113,115,116,118,120,121,123,125,127,128,130,132,134,136,138,140,141,143,145,147,149,151,153,155,157,159,161,164,166,168,170,172,174,177,179,181,183,186,188,190,193,195,197,200,202,205,207,210,212,215,217,220,222,225,228,230,233,236,238,241,244,247,249,252,255]),o=[],c=[{vendorId:12972,productId:32}];class l extends Error{constructor(){super("User cancelled device selection."),this.name=this.constructor.name,this.date=new Date}}class d{constructor(t){this.#t=t}async send(t,e){if(e.length<t.bytes)e=function(t,e,r=0){return t.concat(new Array(e-t.length).fill(r))}(e,t.bytes);else if(e.length>t.bytes)throw new Error("Unable to send report: too many bytes");const r=new Uint8Array(e).buffer;t.feature?await this.#t.sendFeatureReport(t.id,r):await this.#t.sendReport(t.buffer)}async receive(t){let e=[];if(!t.feature)throw new Error("Invalid operation");return e=await this.#t.receiveFeatureReport(t.id),e.byteLength!=t.bytes&&console.error(`reply length=${e.byteLength} (exp ${t.bytes})`),e}#t}const h={id:1,bytes:307,feature:!0},w={id:2,bytes:16,feature:!0},u={id:4,bytes:306,feature:!0},p=0,y=1,f=2,m=3,A=5;class E{constructor(t){this.#e=t}async info(){const t=await this.#e.receive(h);return{sleep_pin:t.getUint8(1),dip1_pin:t.getUint8(2),intb_pin:t.getUint8(3),state_flags:t.getUint8(4),id_reg:t.getUint8(5),config_reg:t.getUint8(6),global_brightness:t.getUint8(7),display_width:t.getUint8(8),display_height:t.getUint8(9),timeout_ms:t.getUint32(10)}}async wake(){await this.#e.send(w,[y,!1])}async sleep(){await this.#e.send(w,[y,!0])}async disableSleep(){await this.#e.send(w,[m,255,255,255,255])}async disableDeepSleep(){await this.#e.send(w,[f,1])}async disableSleepTimer(){await this.#e.send(w,[m,0,0,0,0])}async enableDeepSleep(){await this.#e.send(w,[f,0])}async enableSleepTimer(t){const e=new DataView(new ArrayBuffer(4));e.setInt32(0,t,!1),await this.#e.send(w,[m,e.getUint8(0),e.getUint8(1),e.getUint8(2),e.getUint8(3)])}async reboot(t){await this.#e.send(w,[p,t])}async drawPixel(t,e,r){await this.#e.send(w,[A,e,t,r])}async drawLine({r1:t,c1:e},{r2:r,c2:a},i){await this.#e.send(w,[A,t,e,r,a,i])}async verifyFirmware(){try{const t=await this.info();return 34==t.display_height&&9==t.display_width}catch{return!1}}async version(){return{major:1,minor:0}}async draw(t){return await this.#e.send(u,t.flat().map(t=>s[Math.floor(255*(t??0))]))}#e}class O{static async make(t){const e=await async function(){if(o&&o.length>0)return o.pop();if(o.push(...await navigator.hid.getDevices()),o&&o.length>0)return o.pop();if(o.push(...await navigator.hid.requestDevice({filters:c})),o&&o.length>0)return o.pop();throw new l}();if(e){if("sparkle"===t)return await O.makeSparkleController(e);throw new Error(`Unsupported firmware type: ${t}`)}return null}static async makeSparkleController(t){if(t)return await t.open(),new E(new d(t))}}class b{constructor(t){this.#r=`port-mutex-${Math.random().toString()}`,this.#a=new Map,this.#i=t}async acquire(t){const e=new Error("created bad cb").stack;await this.#n(async()=>{try{await t(this.#i)}catch(t){console.error("Error occured in anonymous callback."),console.error("Original error:",t),console.error("--- This callback was created at ---\n",e)}})}async acquireIdempotent(t,e){const r=new Error("created bad cb").stack;this.#a.has(t)&&console.info(`"${t}" request coalesced.`),this.#a.set(t,async()=>{try{await e(this.#i)}catch(t){console.error("Error occured in anonymous callback."),console.error("Original error:",t),console.error("--- This callback was created at ---\n",r)}}),await this.#n(()=>this.#s(t))}async#n(t){return navigator.locks.request(this.#r,t)}async#s(t){if(this.#a.has(t)){const e=this.#a.get(t);this.#a.delete(t),await e()}}#r;#a;#i}class _{constructor(t){this.#o=t}async rx(t,e=3e3){if(null===this.#o)throw new Error("attempted RX before port initialization.");if(this.#o.readable.locked)throw new Error("attempted RX while port locked.");const r=[],a=this.#o.readable.getReader(),i=setTimeout(()=>a.cancel(),e);try{for(;r.length<t;){const{value:t,done:e}=await a.read();if(r.push(...t??[]),e||!t)break}}finally{clearTimeout(i),a.releaseLock()}return r}async tx(t){if(null===this.#o)throw new Error("attempted TX before port initialization.");if(this.#o.writable.locked)throw new Error("attempted TX while port locked.");const e=this.#o.writable.getWriter();try{await e.write(new Uint8Array(t))}finally{await e.close()}}#o}class g extends Error{constructor(){super("User cancelled port selection."),this.name=this.constructor.name,this.date=new Date}}class T extends Error{constructor(){super("Selected port already in use."),this.name=this.constructor.name,this.date=new Date}}async function I(t){try{await t.close()}catch(t){if("InvalidStateError"!=t.name&&"Failed to execute 'close' on 'SerialPort': The port is already closed."!=t.message)throw t}}const S=Object.freeze({ANIMATE:4,BRIGHTNESS:0,BOOTLOADER:2,DRAW:6,DRAW_GREY_COL_BUFFER:8,GAME_CTRL:17,GAME_STATUS:18,PANIC:5,PATTERN:1,SLEEP:3,STAGE_GREY_COL:7,START_GAME:16,VERSION:32}),M=Object.freeze({GRAY_8BIT:"8-bit",MONO_1BIT:"1-bit"});class R{constructor(t){this.#c=M.MONO_1BIT,this.#l=t}async verifyFirmware(){let t=null;await this.#l.acquire(async e=>{await e.tx([...a,S.VERSION]),t=await e.rx(32)});const e=32==t.length,r=t.slice(3).every(t=>0==t);return e&&r}async version(){let t={};return await this.#l.acquire(async e=>{await e.tx([...a,S.VERSION]);const r=await e.rx(32);t.major=r[0],t.minor=r[1]>>4,t.patch=15&r[1],t.preRelease=1==r[2]}),t}async draw(t){if(this.#c==M.GRAY_8BIT){const e=Array.from({length:9},(e,r)=>new Array({length:34},(e,a)=>GAMMA[Math.floor(255*(t[a][r]??0))]));await this.#l.acquireIdempotent("drawMatrix",async t=>{for(let r=0;r<9;r++)await t.tx([S.STAGE_GREY_COL,r,...e[r]]);await t.tx([S.DRAW_GREY_COL_BUFFER])})}else if(this.#c==M.MONO_1BIT){let e=0,r=new Uint8Array(39).fill(0);for(let a=0;a<34;a++)for(let i=0;i<9;i++)t[a][i]&&(r[e>>3]|=1<<e%8),e++;await this.#l.acquireIdempotent("drawMatrix",async t=>{await t.tx([...a,S.DRAW,...r])})}}#c;#l}const x=Object.freeze({NOOP:0,ANIMATION_DIAMOND:100,ANIMATION_FIRE:98,ANIMATION_FIREPLACE:102,ANIMATION_GEAR:103,ANIMATION_RING:114,ANIMATION_STARTUP:97,ANIMATION_STARTUP_ONCE:65,BOOTLOADER:101,DRAW_PWM:109,DRAW_PWM_BLOCKING:77,DRAW_SCALE:110,DRAW_SCALE_BLOCKING:78,FLUSH_CMD_QUEUE:99,TEST_PATTERN:116,SET_CONST_PWM:119,SET_CONST_SCALE:115,SET_PX_PWM:112,SET_PX_SCALE:113,IDENT:127}),N=/^Sig\sFW\sLED\sMatrix\sFW\sV(\d+)\.(\d+)$/;class D{constructor(t){this.#l=t}async verifyFirmware(){let t=null;return await this.#l.acquire(async e=>{await e.tx([x.IDENT]),t=await e.rx(25)}),t=String.fromCharCode(...t),t.match(N)}async version(){let t=null;await this.#l.acquire(async e=>{await e.tx([x.IDENT]),t=await e.rx(25)}),t=String.fromCharCode(...t);const e=t.match(N);return e&&3==e.length?{major:e[1],minor:e[2]}:null}async draw(t){this.#d||(await this.#l.acquire(async t=>{await t.tx([x.SET_CONST_SCALE,32])}),this.#d=!0);const e=t.flat().map(t=>s[Math.floor(255*(t??0))]);await this.#l.acquireIdempotent("drawMatrix",async t=>{await t.tx([x.DRAW_PWM,...e])})}#l;#d}class k{static async make(t){const e=await async function(){try{const t=await navigator.serial.requestPort(),{usbProductId:e,usbVendorId:r}=t;return console.log("Selected port:",t),e&&r&&console.log(`VID:PID ${r}:${e}`),t}catch(t){throw"NotFoundError"==t.name?new g:"InvalidStateError"==t.name?new T:t}}();if(e){if("framework-official"===t)return await k.makeDefaultWebController(e);if("sigroot"===t)return await k.makeSigrootWebController(e);if("auto"===t)return await k.makeAutoWebController(e);throw new Error(`Unsupported firmware type: ${t}`)}}static async makeDefaultWebController(t){if(t?.connected)return await I(t),await t.open({baudRate:115200}),new R(new b(new _(t)))}static async makeSigrootWebController(t){if(t?.connected)return await I(t),await t.open({baudRate:115200}),new D(new b(new _(t)))}static async makeAutoWebController(t){const e=await this.makeDefaultWebController(t);if(await e.verifyFirmware())return e;const r=await this.makeSigrootWebController(t);return await r.verifyFirmware()?r:void 0}}export{s as GAMMA,e as HEIGHT,O as HIDControllerFactory,i as PID,n as PID_ARR,k as SerialControllerFactory,r as VID,a as VID_ARR,t as WIDTH};
|
|
2
|
+
//# sourceMappingURL=led-matrix-controllers.browser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"led-matrix-controllers.browser.mjs","sources":["../src/hardware-constants.js","../src/usb-hid/environments/web/device.js","../src/usb-hid/environments/web/HIDOperations.js","../src/usb-hid/environments/web/util.js","../src/usb-hid/firmware/sparkle/reports.js","../src/usb-hid/firmware/sparkle/SparkleController.js","../src/usb-hid/HIDControllerFactory.js","../src/usb-serial/environments/web/PortMutex.js","../src/usb-serial/environments/web/PortOperations.js","../src/usb-serial/environments/web/port.js","../src/usb-serial/firmware/framework-official/commands.js","../src/usb-serial/firmware/framework-official/DefaultController.js","../src/usb-serial/firmware/sigroot/commands.js","../src/usb-serial/firmware/sigroot/SigrootController.js","../src/usb-serial/SerialControllerFactory.js"],"sourcesContent":["export const WIDTH = 9;\r\nexport const HEIGHT = 34;\r\nexport const VID = 0x32AC;\r\nexport const VID_ARR = [0x32, 0xAC];\r\nexport const PID = 0x0020;\r\nexport const PID_ARR = [0x00, 0x20];\r\n\r\nexport const GAMMA = Object.freeze([\r\n 0, 0, 0, 0, 0, 0, 0, 1, \r\n 1, 1, 1, 1, 1, 1, 1, 1, \r\n 1, 1, 1, 1, 1, 1, 1, 1, \r\n 1, 1, 1, 1, 1, 1, 1, 1, \r\n 1, 1, 1, 2, 2, 2, 2, 2, \r\n 2, 2, 2, 2, 3, 3, 3, 3, \r\n 3, 3, 3, 4, 4, 4, 4, 4, \r\n 4, 5, 5, 5, 5, 6, 6, 6, \r\n 6, 6, 7, 7, 7, 7, 8, 8, \r\n 8, 9, 9, 9, 10, 10, 10, 11, \r\n 11, 11, 12, 12, 12, 13, 13, 14, \r\n 14, 14, 15, 15, 16, 16, 17, 17, \r\n 17, 18, 18, 19, 19, 20, 20, 21, \r\n 22, 22, 23, 23, 24, 24, 25, 26, \r\n 26, 27, 27, 28, 29, 29, 30, 31, \r\n 32, 32, 33, 34, 34, 35, 36, 37, \r\n 38, 38, 39, 40, 41, 42, 42, 43, \r\n 44, 45, 46, 47, 48, 49, 50, 51, \r\n 52, 53, 54, 55, 56, 57, 58, 59, \r\n 60, 61, 62, 63, 64, 66, 67, 68, \r\n 69, 70, 71, 73, 74, 75, 76, 78, \r\n 79, 80, 82, 83, 84, 86, 87, 88, \r\n 90, 91, 93, 94, 96, 97, 99, 100, \r\n 102, 103, 105, 106, 108, 110, 111, 113, \r\n 115, 116, 118, 120, 121, 123, 125, 127, \r\n 128, 130, 132, 134, 136, 138, 140, 141, \r\n 143, 145, 147, 149, 151, 153, 155, 157, \r\n 159, 161, 164, 166, 168, 170, 172, 174, \r\n 177, 179, 181, 183, 186, 188, 190, 193, \r\n 195, 197, 200, 202, 205, 207, 210, 212, \r\n 215, 217, 220, 222, 225, 228, 230, 233, \r\n 236, 238, 241, 244, 247, 249, 252, 255\r\n]);\r\n","import { PID, VID } from '../../../hardware-constants.js';\r\n\r\nconst extraDevices = [];\r\n\r\nconst filters = [\r\n {\r\n vendorId: VID,\r\n productId: PID,\r\n }\r\n]\r\n\r\nexport class DeviceSelectionCancelled extends Error {\r\n constructor() {\r\n super('User cancelled device selection.');\r\n this.name = this.constructor.name;\r\n this.date = new Date();\r\n }\r\n}\r\n\r\nexport async function getDevice() {\r\n if (extraDevices && extraDevices.length > 0) {\r\n return extraDevices.pop();\r\n }\r\n\r\n extraDevices.push(...(await navigator.hid.getDevices()));\r\n if (extraDevices && extraDevices.length > 0) {\r\n return extraDevices.pop();\r\n }\r\n\r\n extraDevices.push(...(await navigator.hid.requestDevice({ filters })));\r\n if (extraDevices && extraDevices.length > 0) {\r\n return extraDevices.pop();\r\n }\r\n\r\n throw new DeviceSelectionCancelled();\r\n}\r\n","import { pad } from './util.js';\r\n\r\nexport class HIDOperations {\r\n constructor(device) {\r\n this.#device = device;\r\n }\r\n\r\n async send(report, data) {\r\n if (data.length < report.bytes) {\r\n data = pad(data, report.bytes);\r\n } else if (data.length > report.bytes) {\r\n throw new Error('Unable to send report: too many bytes');\r\n }\r\n\r\n const buffer = new Uint8Array(data).buffer;\r\n\r\n if (report.feature) {\r\n await this.#device.sendFeatureReport(report.id, buffer);\r\n } else {\r\n await this.#device.sendReport(report.buffer);\r\n }\r\n }\r\n\r\n async receive(report) {\r\n let reply = [];\r\n\r\n if (report.feature) {\r\n reply = await this.#device.receiveFeatureReport(report.id);\r\n } else {\r\n /* \r\n * HID input reports are used for unprompted data.\r\n * An event system must be used.\r\n * See WebHID `HIDInputReportEvent`\r\n */\r\n throw new Error('Invalid operation');\r\n }\r\n\r\n if (reply.byteLength != report.bytes) {\r\n console.error(`reply length=${reply.byteLength} (exp ${report.bytes})`);\r\n }\r\n\r\n return reply;\r\n }\r\n\r\n #device;\r\n}\r\n","export function pad(arr, len, val=0x00) {\r\n return arr.concat(new Array(len - arr.length).fill(val));\r\n}\r\n","export const Reports = {\r\n GLITTER_DEVICE_INFO: {\r\n id: 0x01,\r\n bytes: 307,\r\n feature: true,\r\n },\r\n GLITTER_BASIC_CMD: {\r\n id: 0x02,\r\n bytes: 16,\r\n feature: true,\r\n },\r\n GLITTER_GRID_PWM_CNTL: {\r\n id: 0x03,\r\n bytes: 306,\r\n feature: true,\r\n },\r\n GLITTER_GRID_PWM_CNTL: {\r\n id: 0x04,\r\n bytes: 306,\r\n feature: true,\r\n }\r\n}\r\n\r\nexport const Commands = {\r\n GLITTER_CMD_REBOOT: 0x00,\r\n GLITTER_CMD_SLEEP: 0x01,\r\n GLITTER_CMD_WAKE_ON_COMMAND: 0x02,\r\n GLITTER_CMD_SET_SLEEP_TIMEOUT: 0x03,\r\n GLITTER_CMD_SET_GLOBAL_BRIGHTNESS: 0x04,\r\n GLITTER_CMD_DRAW_PIXEL: 0x05,\r\n GLITTER_CMD_DRAW_LINE: 0x06\r\n}\r\n\r\nexport const BootMode = {\r\n BOOTSEL: 0x00,\r\n NORMAL: 0x01,\r\n}\r\n","import { GAMMA, HEIGHT, WIDTH } from '../../../hardware-constants.js';\r\nimport { Commands, Reports } from './reports.js';\r\n\r\nexport class SparkleController {\r\n constructor(hidOps) {\r\n this.#hidOps = hidOps;\r\n }\r\n\r\n async info() {\r\n const infoRaw = await this.#hidOps.receive(Reports.GLITTER_DEVICE_INFO);\r\n\r\n return {\r\n sleep_pin: infoRaw.getUint8(1),\r\n dip1_pin: infoRaw.getUint8(2),\r\n intb_pin: infoRaw.getUint8(3),\r\n state_flags: infoRaw.getUint8(4),\r\n id_reg: infoRaw.getUint8(5),\r\n config_reg: infoRaw.getUint8(6),\r\n global_brightness: infoRaw.getUint8(7),\r\n display_width: infoRaw.getUint8(8),\r\n display_height: infoRaw.getUint8(9),\r\n timeout_ms: infoRaw.getUint32(10),\r\n };\r\n }\r\n\r\n async wake() {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD,\r\n [Commands.GLITTER_CMD_SLEEP, false]\r\n );\r\n }\r\n\r\n async sleep() {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD,\r\n [Commands.GLITTER_CMD_SLEEP, true]\r\n );\r\n }\r\n\r\n async disableSleep() {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD, \r\n [Commands.GLITTER_CMD_SET_SLEEP_TIMEOUT, 0xff, 0xff, 0xff, 0xff]\r\n );\r\n }\r\n\r\n async disableDeepSleep() {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD, \r\n [Commands.GLITTER_CMD_WAKE_ON_COMMAND, 0x01]\r\n );\r\n }\r\n\r\n async disableSleepTimer() {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD, \r\n [Commands.GLITTER_CMD_SET_SLEEP_TIMEOUT, 0x00, 0x00, 0x00, 0x00]\r\n );\r\n }\r\n \r\n async enableDeepSleep() {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD, \r\n [Commands.GLITTER_CMD_WAKE_ON_COMMAND, 0x00]\r\n );\r\n }\r\n\r\n async enableSleepTimer(milliseconds) {\r\n const view = new DataView(new ArrayBuffer(4));\r\n const littleEndian = false;\r\n view.setInt32(0, milliseconds, littleEndian);\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD, \r\n [\r\n Commands.GLITTER_CMD_SET_SLEEP_TIMEOUT, \r\n view.getUint8(0), \r\n view.getUint8(1), \r\n view.getUint8(2), \r\n view.getUint8(3),\r\n ]\r\n );\r\n }\r\n\r\n async reboot(mode) {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD, \r\n [Commands.GLITTER_CMD_REBOOT, mode]\r\n );\r\n }\r\n\r\n async drawPixel(r, c, brightness) {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD,\r\n [Commands.GLITTER_CMD_DRAW_PIXEL, c, r, brightness]\r\n );\r\n }\r\n\r\n async drawLine({r1, c1}, {r2, c2}, brightness) {\r\n await this.#hidOps.send(\r\n Reports.GLITTER_BASIC_CMD,\r\n [Commands.GLITTER_CMD_DRAW_PIXEL, r1, c1, r2, c2, brightness]\r\n );\r\n }\r\n\r\n // --- generic methods ---\r\n\r\n async verifyFirmware() {\r\n try {\r\n const info = await this.info();\r\n return (\r\n info.display_height == HEIGHT &&\r\n info.display_width == WIDTH\r\n );\r\n } catch {\r\n return false;\r\n }\r\n }\r\n\r\n async version() {\r\n // Not Available\r\n return { major: 1, minor: 0 };\r\n }\r\n\r\n async draw(matrix) {\r\n return await this.#hidOps.send(\r\n Reports.GLITTER_GRID_PWM_CNTL,\r\n matrix.flat().map(v => GAMMA[Math.floor((v ?? 0) * 255)])\r\n );\r\n }\r\n\r\n #hidOps;\r\n}\r\n","import { getDevice } from './environments/web/device.js';\r\nimport { HIDOperations } from './environments/web/HIDOperations.js';\r\nimport { SparkleController } from './firmware/sparkle/SparkleController.js';\r\n\r\nexport class HIDControllerFactory {\r\n static async make(firmware) {\r\n const device = await getDevice();\r\n if (device) {\r\n if (firmware === 'sparkle') {\r\n return await HIDControllerFactory.makeSparkleController(device);\r\n } else {\r\n throw new Error(`Unsupported firmware type: ${firmware}`);\r\n }\r\n } else {\r\n return null;\r\n }\r\n }\r\n\r\n static async makeSparkleController(device) {\r\n if (device) {\r\n await device.open();\r\n return new SparkleController(\r\n new HIDOperations(device)\r\n );\r\n }\r\n }\r\n}\r\n","export class PortMutex {\r\n constructor(portOperations) {\r\n this.#lockName = `port-mutex-${Math.random().toString()}`;\r\n this.#deduper = new Map();\r\n this.#portOps = portOperations;\r\n }\r\n\r\n async acquire(fn) {\r\n const trace = new Error('created bad cb').stack;\r\n\r\n await this.#enqueue(async () => {\r\n try {\r\n await fn(this.#portOps);\r\n } catch (e) {\r\n console.error('Error occured in anonymous callback.');\r\n console.error('Original error:', e);\r\n console.error('--- This callback was created at ---\\n', trace);\r\n }\r\n });\r\n }\r\n\r\n async acquireIdempotent(key, fn) {\r\n const trace = new Error('created bad cb').stack;\r\n\r\n if (this.#deduper.has(key)) {\r\n console.info(`\"${key}\" request coalesced.`);\r\n }\r\n\r\n this.#deduper.set(key, async () => {\r\n try {\r\n await fn(this.#portOps);\r\n } catch (e) {\r\n console.error('Error occured in anonymous callback.');\r\n console.error('Original error:', e);\r\n console.error('--- This callback was created at ---\\n', trace);\r\n }\r\n });\r\n\r\n await this.#enqueue(() => this.#execDedupedOp(key));\r\n }\r\n\r\n async #enqueue(fn) {\r\n // `navigator.locks` maintains queue and provides mutual exclusion.\r\n return navigator.locks.request(this.#lockName, fn);\r\n }\r\n\r\n async #execDedupedOp(key) {\r\n if (this.#deduper.has(key)) {\r\n const fn = this.#deduper.get(key);\r\n this.#deduper.delete(key);\r\n await fn();\r\n }\r\n }\r\n\r\n #lockName;\r\n #deduper;\r\n #portOps;\r\n}\r\n","export class PortOperations {\r\n constructor(port) {\r\n this.#port = port;\r\n }\r\n\r\n async rx(length, timeout=3000) {\r\n if (this.#port === null) {\r\n throw new Error('attempted RX before port initialization.');\r\n }\r\n\r\n /*\r\n * ReadableStream's built-in locking mechanism cannot be awaited or otherwise\r\n * asynchronously acquired. A PortMutex must be used.\r\n */\r\n if (this.#port.readable.locked) {\r\n throw new Error('attempted RX while port locked.');\r\n }\r\n\r\n const response = [];\r\n const reader = this.#port.readable.getReader();\r\n const timeoutHandle = setTimeout(() => reader.cancel(), timeout);\r\n\r\n try {\r\n // Response may be divided across multiple reads\r\n while (response.length < length) {\r\n const { value, done } = await reader.read();\r\n response.push(...(value ?? []));\r\n if (done || !value) break;\r\n }\r\n } finally {\r\n clearTimeout(timeoutHandle);\r\n reader.releaseLock();\r\n }\r\n\r\n return response;\r\n }\r\n\r\n async tx(buffer) {\r\n if (this.#port === null) {\r\n throw new Error('attempted TX before port initialization.');\r\n }\r\n \r\n /*\r\n * WritableStream's built-in locking mechanism cannot be awaited or otherwise\r\n * asynchronously acquired. A PortMutex must be used.\r\n */\r\n if (this.#port.writable.locked) {\r\n throw new Error('attempted TX while port locked.');\r\n }\r\n\r\n const writer = this.#port.writable.getWriter();\r\n\r\n try {\r\n await writer.write(new Uint8Array(buffer));\r\n } finally {\r\n /* \r\n * The writer must be completely torn down between every single write.\r\n * Many parsers can't handle delayed flushing and write coalescing.\r\n * Command sequences will fail if `releaseLock()` is used here.\r\n */\r\n await writer.close();\r\n }\r\n }\r\n\r\n #port;\r\n}\r\n","export class PortSelectionCancelled extends Error {\r\n constructor() {\r\n super('User cancelled port selection.');\r\n this.name = this.constructor.name;\r\n this.date = new Date();\r\n }\r\n}\r\n\r\nexport class PortUnavailable extends Error {\r\n constructor() {\r\n super('Selected port already in use.');\r\n this.name = this.constructor.name;\r\n this.date = new Date();\r\n }\r\n}\r\n\r\nexport async function getPort() {\r\n try {\r\n const port = await navigator.serial.requestPort();\r\n const { usbProductId, usbVendorId } = port;\r\n console.log('Selected port:', port);\r\n if (usbProductId && usbVendorId) {\r\n console.log(`VID:PID ${usbVendorId}:${usbProductId}`);\r\n }\r\n return port;\r\n } catch (e) {\r\n if (e.name == 'NotFoundError') {\r\n throw new PortSelectionCancelled();\r\n } else if (e.name == 'InvalidStateError') {\r\n throw new PortUnavailable();\r\n } else {\r\n throw e;\r\n }\r\n }\r\n}\r\n\r\nexport async function close(port) {\r\n try {\r\n await port.close();\r\n } catch(e) {\r\n if (e.name != 'InvalidStateError') {\r\n if (e.message != \"Failed to execute 'close' on 'SerialPort': The port is already closed.\") {\r\n throw e;\r\n }\r\n }\r\n }\r\n}\r\n","// All replies to all commands are 32 bytes\r\nexport const RX_PACKET_SZ = 32;\r\n\r\nexport const Command = Object.freeze({\r\n ANIMATE: 0x04,\r\n BRIGHTNESS: 0x00,\r\n BOOTLOADER: 0x02,\r\n DRAW: 0x06,\r\n DRAW_GREY_COL_BUFFER: 0x08,\r\n GAME_CTRL: 0x11,\r\n GAME_STATUS: 0x12,\r\n PANIC: 0x05,\r\n PATTERN: 0x01,\r\n SLEEP: 0x03,\r\n STAGE_GREY_COL: 0x07,\r\n START_GAME: 0x10,\r\n VERSION: 0x20,\r\n});\r\n\r\n// Used with Command.PATTERN\r\nexport const Pattern = Object.freeze({\r\n DISPLAY_LOTUS_HORIZONTAL: 0x03,\r\n DISPLAY_LOTUS_VERTICAL: 0x07,\r\n DISPLAY_PANIC: 0x06,\r\n DOUBLE_GRADIENT: 0x02,\r\n FULL_BRIGHTNESS: 0x05,\r\n GRADIENT: 0x01,\r\n PERCENTAGE: 0x00,\r\n ZIG_ZAG: 0x04,\r\n});\r\n\r\n// DRAW or DRAW_GREY_COL_BUFFER\r\nexport const BitDepth = Object.freeze({\r\n GRAY_8BIT: '8-bit',\r\n MONO_1BIT: '1-bit',\r\n})\r\n","import { HEIGHT, VID_ARR, WIDTH } from '../../../hardware-constants.js';\r\nimport { BitDepth, Command, RX_PACKET_SZ } from './commands.js';\r\n\r\nexport class DefaultController {\r\n constructor(portMutex) {\r\n this.#bitDepth = BitDepth.MONO_1BIT;\r\n this.#portMutex = portMutex;\r\n }\r\n\r\n async verifyFirmware() {\r\n let packet = null;\r\n\r\n await this.#portMutex.acquire(async p => {\r\n await p.tx([...VID_ARR, Command.VERSION]);\r\n packet = await p.rx(RX_PACKET_SZ);\r\n });\r\n\r\n const isPacket = packet.length == RX_PACKET_SZ;\r\n const isPadded = packet.slice(3).every(e => e == 0x00);\r\n\r\n return isPacket && isPadded;\r\n }\r\n\r\n async version() {\r\n let ver = {};\r\n\r\n await this.#portMutex.acquire(async p => {\r\n await p.tx([...VID_ARR, Command.VERSION]);\r\n const response = await p.rx(RX_PACKET_SZ);\r\n\r\n // MMMMMMMM mmmmPPPP 0000000p\r\n ver.major = response[0];\r\n ver.minor = response[1] >> 4;\r\n ver.patch = response[1] & 0x0F;\r\n ver.preRelease = response[2] == 1;\r\n });\r\n\r\n return ver;\r\n }\r\n\r\n async draw(matrix) {\r\n if (this.#bitDepth == BitDepth.GRAY_8BIT) {\r\n // Transpose & Gamma Correction\r\n const payloads = Array.from(\r\n { length: WIDTH }, \r\n (_, c) => new Array(\r\n { length: HEIGHT }, \r\n (_, r) => GAMMA[Math.floor((matrix[r][c] ?? 0) * 255)]\r\n )\r\n );\r\n\r\n // Only execute the most recent call \r\n await this.#portMutex.acquireIdempotent(\r\n 'drawMatrix', \r\n async p => {\r\n for (let i = 0; i < WIDTH; i++) {\r\n await p.tx([Command.STAGE_GREY_COL, i, ...payloads[i]]);\r\n }\r\n await p.tx([Command.DRAW_GREY_COL_BUFFER]);\r\n }\r\n );\r\n }\r\n\r\n else if (this.#bitDepth == BitDepth.MONO_1BIT) {\r\n let index = 0;\r\n let output = new Uint8Array(39).fill(0);\r\n\r\n // Pack cells into bits\r\n for (let r = 0; r < HEIGHT; r++) {\r\n for (let c = 0; c < WIDTH; c++) {\r\n if (matrix[r][c]) {\r\n output[index >> 3] |= 1 << index % 8;\r\n }\r\n index++;\r\n }\r\n }\r\n\r\n await this.#portMutex.acquireIdempotent(\r\n 'drawMatrix', \r\n async p => {\r\n await p.tx([...VID_ARR, Command.DRAW, ...output]);\r\n }\r\n );\r\n }\r\n }\r\n\r\n #bitDepth;\r\n #portMutex;\r\n}\r\n","export const Command = Object.freeze({\r\n /* 000 */ NOOP: 0x00,\r\n /* 'd' */ ANIMATION_DIAMOND: 0x64,\r\n /* 'b' */ ANIMATION_FIRE: 0x62,\r\n /* 'f' */ ANIMATION_FIREPLACE: 0x66,\r\n /* 'g' */ ANIMATION_GEAR: 0x67,\r\n /* 'r' */ ANIMATION_RING: 0x72,\r\n /* 'a' */ ANIMATION_STARTUP: 0x61,\r\n /* 'A' */ ANIMATION_STARTUP_ONCE: 0x41,\r\n /* 'e' */ BOOTLOADER: 0x65,\r\n /* 'm' */ DRAW_PWM: 0x6D,\r\n /* 'M' */ DRAW_PWM_BLOCKING: 0x4D,\r\n /* 'n' */ DRAW_SCALE: 0x6E,\r\n /* 'N' */ DRAW_SCALE_BLOCKING: 0x4E,\r\n /* 'c' */ FLUSH_CMD_QUEUE: 0x63,\r\n /* 't' */ TEST_PATTERN: 0x74,\r\n /* 'w' */ SET_CONST_PWM: 0x77,\r\n /* 's' */ SET_CONST_SCALE: 0x73,\r\n /* 'p' */ SET_PX_PWM: 0x70,\r\n /* 'q' */ SET_PX_SCALE: 0x71,\r\n /* 127 */ IDENT: 0x7F,\r\n});\r\n\r\nexport const IDENT_STR_LEN = 25;\r\n\r\nexport const IDENT_STR_REGEX = /^Sig\\sFW\\sLED\\sMatrix\\sFW\\sV(\\d+)\\.(\\d+)$/;\r\n\r\nexport const IDENT_STR_PREFIX = 'Sig FW LED Matrix FW';\r\n","import { GAMMA } from '../../../hardware-constants.js';\r\nimport { Command, IDENT_STR_LEN, IDENT_STR_REGEX } from './commands.js';\r\n\r\nexport class SigrootController {\r\n constructor(portMutex) {\r\n this.#portMutex = portMutex;\r\n }\r\n\r\n async verifyFirmware() {\r\n let ident = null;\r\n\r\n await this.#portMutex.acquire(async p => {\r\n await p.tx([Command.IDENT]);\r\n ident = await p.rx(IDENT_STR_LEN);\r\n });\r\n\r\n ident = String.fromCharCode(...ident);\r\n\r\n return ident.match(IDENT_STR_REGEX);\r\n }\r\n\r\n async version() {\r\n let ident = null;\r\n\r\n await this.#portMutex.acquire(async p => {\r\n await p.tx([Command.IDENT]);\r\n ident = await p.rx(IDENT_STR_LEN);\r\n });\r\n\r\n ident = String.fromCharCode(...ident);\r\n\r\n const match = ident.match(IDENT_STR_REGEX);\r\n\r\n if (match && match.length == 3) {\r\n return {\r\n major: match[1],\r\n minor: match[2]\r\n }\r\n } else {\r\n return null;\r\n }\r\n }\r\n\r\n async draw(matrix) {\r\n if (!this.#scaleInitialized) {\r\n await this.#portMutex.acquire(\r\n async p => {\r\n await p.tx([Command.SET_CONST_SCALE, 0x20]);\r\n }\r\n );\r\n this.#scaleInitialized = true;\r\n }\r\n\r\n const bytes = matrix.flat().map(v => \r\n GAMMA[Math.floor((v ?? 0) * 255)]\r\n );\r\n\r\n // Only execute the most recent call \r\n await this.#portMutex.acquireIdempotent(\r\n 'drawMatrix', \r\n async p => {\r\n await p.tx([Command.DRAW_PWM, ...bytes]);\r\n }\r\n );\r\n }\r\n\r\n #portMutex;\r\n #scaleInitialized;\r\n}\r\n","import { PortMutex } from './environments/web/PortMutex.js';\r\nimport { PortOperations } from './environments/web/PortOperations.js';\r\nimport { close, getPort } from './environments/web/port.js';\r\nimport { DefaultController } from './firmware/framework-official/DefaultController.js';\r\nimport { SigrootController } from './firmware/sigroot/SigrootController.js';\r\n\r\nexport class SerialControllerFactory {\r\n static async make(firmware) {\r\n const port = await getPort();\r\n if (port) {\r\n if (firmware === 'framework-official') {\r\n return await SerialControllerFactory.makeDefaultWebController(port);\r\n } else if (firmware === 'sigroot') {\r\n return await SerialControllerFactory.makeSigrootWebController(port);\r\n } else if (firmware === 'auto') {\r\n return await SerialControllerFactory.makeAutoWebController(port);\r\n } else {\r\n throw new Error(`Unsupported firmware type: ${firmware}`);\r\n }\r\n }\r\n }\r\n\r\n static async makeDefaultWebController(port) {\r\n if (port?.connected) {\r\n await close(port);\r\n await port.open({ baudRate: 115200 });\r\n return new DefaultController(\r\n new PortMutex(\r\n new PortOperations(port)\r\n )\r\n );\r\n }\r\n }\r\n\r\n static async makeSigrootWebController(port) {\r\n if (port?.connected) {\r\n await close(port);\r\n await port.open({ baudRate: 115200 });\r\n return new SigrootController(\r\n new PortMutex(\r\n new PortOperations(port)\r\n )\r\n );\r\n }\r\n }\r\n\r\n static async makeAutoWebController(port) {\r\n const ctrl1 = await this.makeDefaultWebController(port);\r\n if (await ctrl1.verifyFirmware()) {\r\n return ctrl1;\r\n }\r\n const ctrl2 = await this.makeSigrootWebController(port);\r\n if (await ctrl2.verifyFirmware()) {\r\n return ctrl2;\r\n }\r\n }\r\n}\r\n"],"names":["WIDTH","HEIGHT","VID","VID_ARR","PID","PID_ARR","GAMMA","Object","freeze","extraDevices","filters","vendorId","productId","DeviceSelectionCancelled","Error","constructor","super","this","name","date","Date","HIDOperations","device","send","report","data","length","bytes","arr","len","val","concat","Array","fill","pad","buffer","Uint8Array","feature","sendFeatureReport","id","sendReport","receive","reply","receiveFeatureReport","byteLength","console","error","Reports","Commands","SparkleController","hidOps","info","infoRaw","sleep_pin","getUint8","dip1_pin","intb_pin","state_flags","id_reg","config_reg","global_brightness","display_width","display_height","timeout_ms","getUint32","wake","sleep","disableSleep","disableDeepSleep","disableSleepTimer","enableDeepSleep","enableSleepTimer","milliseconds","view","DataView","ArrayBuffer","setInt32","reboot","mode","drawPixel","r","c","brightness","drawLine","r1","c1","r2","c2","verifyFirmware","version","major","minor","draw","matrix","flat","map","v","Math","floor","HIDControllerFactory","make","firmware","async","pop","push","navigator","hid","getDevices","requestDevice","getDevice","makeSparkleController","open","PortMutex","portOperations","lockName","random","toString","deduper","Map","portOps","acquire","fn","trace","stack","enqueue","e","acquireIdempotent","key","has","set","execDedupedOp","locks","request","get","delete","PortOperations","port","rx","timeout","readable","locked","response","reader","getReader","timeoutHandle","setTimeout","cancel","value","done","read","clearTimeout","releaseLock","tx","writable","writer","getWriter","write","close","PortSelectionCancelled","PortUnavailable","message","Command","ANIMATE","BRIGHTNESS","BOOTLOADER","DRAW","DRAW_GREY_COL_BUFFER","GAME_CTRL","GAME_STATUS","PANIC","PATTERN","SLEEP","STAGE_GREY_COL","START_GAME","VERSION","BitDepth","GRAY_8BIT","MONO_1BIT","DefaultController","portMutex","bitDepth","packet","p","isPacket","isPadded","slice","every","ver","patch","preRelease","payloads","from","_","i","index","output","NOOP","ANIMATION_DIAMOND","ANIMATION_FIRE","ANIMATION_FIREPLACE","ANIMATION_GEAR","ANIMATION_RING","ANIMATION_STARTUP","ANIMATION_STARTUP_ONCE","DRAW_PWM","DRAW_PWM_BLOCKING","DRAW_SCALE","DRAW_SCALE_BLOCKING","FLUSH_CMD_QUEUE","TEST_PATTERN","SET_CONST_PWM","SET_CONST_SCALE","SET_PX_PWM","SET_PX_SCALE","IDENT","IDENT_STR_REGEX","SigrootController","ident","String","fromCharCode","match","scaleInitialized","SerialControllerFactory","serial","requestPort","usbProductId","usbVendorId","log","getPort","makeDefaultWebController","makeSigrootWebController","makeAutoWebController","connected","baudRate","ctrl1","ctrl2"],"mappings":"AAAY,MAACA,EAAQ,EACRC,EAAS,GACTC,EAAM,MACNC,EAAU,CAAC,GAAM,KACjBC,EAAM,GACNC,EAAU,CAAC,EAAM,IAEjBC,EAAQC,OAAOC,OAAO,CACjC,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EACrB,EAAG,EAAG,EAAG,EAAG,GAAI,GAAI,GAAI,GACxB,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAC5B,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GAAI,GA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qBAAVjC,EAAE3G,KACL,IAAI6I,EAEJlC,CAEV,CACF,CK1BuBuG,GACnB,GAAI5F,EAAM,CACR,GAAiB,uBAAbnC,EACF,aAAayH,EAAwBO,yBAAyB7F,GACzD,GAAiB,YAAbnC,EACT,aAAayH,EAAwBQ,yBAAyB9F,GACzD,GAAiB,SAAbnC,EACT,aAAayH,EAAwBS,sBAAsB/F,GAE3D,MAAM,IAAI1H,MAAM,8BAA8BuF,IAElD,CACF,CAEA,qCAAagI,CAAyB7F,GACpC,GAAIA,GAAMgG,UAGR,aAFM3E,EAAMrB,SACNA,EAAKzB,KAAK,CAAE0H,SAAU,SACrB,IAAIvD,EACT,IAAIlE,EACF,IAAIuB,EAAeC,IAI3B,CAEA,qCAAa8F,CAAyB9F,GACpC,GAAIA,GAAMgG,UAGR,aAFM3E,EAAMrB,SACNA,EAAKzB,KAAK,CAAE0H,SAAU,SACrB,IAAIjB,EACT,IAAIxG,EACF,IAAIuB,EAAeC,IAI3B,CAEA,kCAAa+F,CAAsB/F,GACjC,MAAMkG,QAAczN,KAAKoN,yBAAyB7F,GAClD,SAAUkG,EAAMlJ,iBACd,OAAOkJ,EAET,MAAMC,QAAc1N,KAAKqN,yBAAyB9F,GAClD,aAAUmG,EAAMnJ,iBACPmJ,OADT,CAGF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "led-matrix-controllers",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"module": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"packageManager": "yarn@4.12.0",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
17
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
18
|
+
"rollup": "^4.53.3"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rollup -c"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const WIDTH = 9;
|
|
2
|
+
export const HEIGHT = 34;
|
|
3
|
+
export const VID = 0x32AC;
|
|
4
|
+
export const VID_ARR = [0x32, 0xAC];
|
|
5
|
+
export const PID = 0x0020;
|
|
6
|
+
export const PID_ARR = [0x00, 0x20];
|
|
7
|
+
|
|
8
|
+
export const GAMMA = Object.freeze([
|
|
9
|
+
0, 0, 0, 0, 0, 0, 0, 1,
|
|
10
|
+
1, 1, 1, 1, 1, 1, 1, 1,
|
|
11
|
+
1, 1, 1, 1, 1, 1, 1, 1,
|
|
12
|
+
1, 1, 1, 1, 1, 1, 1, 1,
|
|
13
|
+
1, 1, 1, 2, 2, 2, 2, 2,
|
|
14
|
+
2, 2, 2, 2, 3, 3, 3, 3,
|
|
15
|
+
3, 3, 3, 4, 4, 4, 4, 4,
|
|
16
|
+
4, 5, 5, 5, 5, 6, 6, 6,
|
|
17
|
+
6, 6, 7, 7, 7, 7, 8, 8,
|
|
18
|
+
8, 9, 9, 9, 10, 10, 10, 11,
|
|
19
|
+
11, 11, 12, 12, 12, 13, 13, 14,
|
|
20
|
+
14, 14, 15, 15, 16, 16, 17, 17,
|
|
21
|
+
17, 18, 18, 19, 19, 20, 20, 21,
|
|
22
|
+
22, 22, 23, 23, 24, 24, 25, 26,
|
|
23
|
+
26, 27, 27, 28, 29, 29, 30, 31,
|
|
24
|
+
32, 32, 33, 34, 34, 35, 36, 37,
|
|
25
|
+
38, 38, 39, 40, 41, 42, 42, 43,
|
|
26
|
+
44, 45, 46, 47, 48, 49, 50, 51,
|
|
27
|
+
52, 53, 54, 55, 56, 57, 58, 59,
|
|
28
|
+
60, 61, 62, 63, 64, 66, 67, 68,
|
|
29
|
+
69, 70, 71, 73, 74, 75, 76, 78,
|
|
30
|
+
79, 80, 82, 83, 84, 86, 87, 88,
|
|
31
|
+
90, 91, 93, 94, 96, 97, 99, 100,
|
|
32
|
+
102, 103, 105, 106, 108, 110, 111, 113,
|
|
33
|
+
115, 116, 118, 120, 121, 123, 125, 127,
|
|
34
|
+
128, 130, 132, 134, 136, 138, 140, 141,
|
|
35
|
+
143, 145, 147, 149, 151, 153, 155, 157,
|
|
36
|
+
159, 161, 164, 166, 168, 170, 172, 174,
|
|
37
|
+
177, 179, 181, 183, 186, 188, 190, 193,
|
|
38
|
+
195, 197, 200, 202, 205, 207, 210, 212,
|
|
39
|
+
215, 217, 220, 222, 225, 228, 230, 233,
|
|
40
|
+
236, 238, 241, 244, 247, 249, 252, 255
|
|
41
|
+
]);
|
package/src/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getDevice } from './environments/web/device.js';
|
|
2
|
+
import { HIDOperations } from './environments/web/HIDOperations.js';
|
|
3
|
+
import { SparkleController } from './firmware/sparkle/SparkleController.js';
|
|
4
|
+
|
|
5
|
+
export class HIDControllerFactory {
|
|
6
|
+
static async make(firmware) {
|
|
7
|
+
const device = await getDevice();
|
|
8
|
+
if (device) {
|
|
9
|
+
if (firmware === 'sparkle') {
|
|
10
|
+
return await HIDControllerFactory.makeSparkleController(device);
|
|
11
|
+
} else {
|
|
12
|
+
throw new Error(`Unsupported firmware type: ${firmware}`);
|
|
13
|
+
}
|
|
14
|
+
} else {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static async makeSparkleController(device) {
|
|
20
|
+
if (device) {
|
|
21
|
+
await device.open();
|
|
22
|
+
return new SparkleController(
|
|
23
|
+
new HIDOperations(device)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { pad } from './util.js';
|
|
2
|
+
|
|
3
|
+
export class HIDOperations {
|
|
4
|
+
constructor(device) {
|
|
5
|
+
this.#device = device;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async send(report, data) {
|
|
9
|
+
if (data.length < report.bytes) {
|
|
10
|
+
data = pad(data, report.bytes);
|
|
11
|
+
} else if (data.length > report.bytes) {
|
|
12
|
+
throw new Error('Unable to send report: too many bytes');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const buffer = new Uint8Array(data).buffer;
|
|
16
|
+
|
|
17
|
+
if (report.feature) {
|
|
18
|
+
await this.#device.sendFeatureReport(report.id, buffer);
|
|
19
|
+
} else {
|
|
20
|
+
await this.#device.sendReport(report.buffer);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async receive(report) {
|
|
25
|
+
let reply = [];
|
|
26
|
+
|
|
27
|
+
if (report.feature) {
|
|
28
|
+
reply = await this.#device.receiveFeatureReport(report.id);
|
|
29
|
+
} else {
|
|
30
|
+
/*
|
|
31
|
+
* HID input reports are used for unprompted data.
|
|
32
|
+
* An event system must be used.
|
|
33
|
+
* See WebHID `HIDInputReportEvent`
|
|
34
|
+
*/
|
|
35
|
+
throw new Error('Invalid operation');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (reply.byteLength != report.bytes) {
|
|
39
|
+
console.error(`reply length=${reply.byteLength} (exp ${report.bytes})`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return reply;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#device;
|
|
46
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { PID, VID } from '../../../hardware-constants.js';
|
|
2
|
+
|
|
3
|
+
const extraDevices = [];
|
|
4
|
+
|
|
5
|
+
const filters = [
|
|
6
|
+
{
|
|
7
|
+
vendorId: VID,
|
|
8
|
+
productId: PID,
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
export class DeviceSelectionCancelled extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super('User cancelled device selection.');
|
|
15
|
+
this.name = this.constructor.name;
|
|
16
|
+
this.date = new Date();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function getDevice() {
|
|
21
|
+
if (extraDevices && extraDevices.length > 0) {
|
|
22
|
+
return extraDevices.pop();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
extraDevices.push(...(await navigator.hid.getDevices()));
|
|
26
|
+
if (extraDevices && extraDevices.length > 0) {
|
|
27
|
+
return extraDevices.pop();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
extraDevices.push(...(await navigator.hid.requestDevice({ filters })));
|
|
31
|
+
if (extraDevices && extraDevices.length > 0) {
|
|
32
|
+
return extraDevices.pop();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw new DeviceSelectionCancelled();
|
|
36
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { GAMMA, HEIGHT, WIDTH } from '../../../hardware-constants.js';
|
|
2
|
+
import { Commands, Reports } from './reports.js';
|
|
3
|
+
|
|
4
|
+
export class SparkleController {
|
|
5
|
+
constructor(hidOps) {
|
|
6
|
+
this.#hidOps = hidOps;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async info() {
|
|
10
|
+
const infoRaw = await this.#hidOps.receive(Reports.GLITTER_DEVICE_INFO);
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
sleep_pin: infoRaw.getUint8(1),
|
|
14
|
+
dip1_pin: infoRaw.getUint8(2),
|
|
15
|
+
intb_pin: infoRaw.getUint8(3),
|
|
16
|
+
state_flags: infoRaw.getUint8(4),
|
|
17
|
+
id_reg: infoRaw.getUint8(5),
|
|
18
|
+
config_reg: infoRaw.getUint8(6),
|
|
19
|
+
global_brightness: infoRaw.getUint8(7),
|
|
20
|
+
display_width: infoRaw.getUint8(8),
|
|
21
|
+
display_height: infoRaw.getUint8(9),
|
|
22
|
+
timeout_ms: infoRaw.getUint32(10),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async wake() {
|
|
27
|
+
await this.#hidOps.send(
|
|
28
|
+
Reports.GLITTER_BASIC_CMD,
|
|
29
|
+
[Commands.GLITTER_CMD_SLEEP, false]
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async sleep() {
|
|
34
|
+
await this.#hidOps.send(
|
|
35
|
+
Reports.GLITTER_BASIC_CMD,
|
|
36
|
+
[Commands.GLITTER_CMD_SLEEP, true]
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async disableSleep() {
|
|
41
|
+
await this.#hidOps.send(
|
|
42
|
+
Reports.GLITTER_BASIC_CMD,
|
|
43
|
+
[Commands.GLITTER_CMD_SET_SLEEP_TIMEOUT, 0xff, 0xff, 0xff, 0xff]
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async disableDeepSleep() {
|
|
48
|
+
await this.#hidOps.send(
|
|
49
|
+
Reports.GLITTER_BASIC_CMD,
|
|
50
|
+
[Commands.GLITTER_CMD_WAKE_ON_COMMAND, 0x01]
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async disableSleepTimer() {
|
|
55
|
+
await this.#hidOps.send(
|
|
56
|
+
Reports.GLITTER_BASIC_CMD,
|
|
57
|
+
[Commands.GLITTER_CMD_SET_SLEEP_TIMEOUT, 0x00, 0x00, 0x00, 0x00]
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async enableDeepSleep() {
|
|
62
|
+
await this.#hidOps.send(
|
|
63
|
+
Reports.GLITTER_BASIC_CMD,
|
|
64
|
+
[Commands.GLITTER_CMD_WAKE_ON_COMMAND, 0x00]
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async enableSleepTimer(milliseconds) {
|
|
69
|
+
const view = new DataView(new ArrayBuffer(4));
|
|
70
|
+
const littleEndian = false;
|
|
71
|
+
view.setInt32(0, milliseconds, littleEndian);
|
|
72
|
+
await this.#hidOps.send(
|
|
73
|
+
Reports.GLITTER_BASIC_CMD,
|
|
74
|
+
[
|
|
75
|
+
Commands.GLITTER_CMD_SET_SLEEP_TIMEOUT,
|
|
76
|
+
view.getUint8(0),
|
|
77
|
+
view.getUint8(1),
|
|
78
|
+
view.getUint8(2),
|
|
79
|
+
view.getUint8(3),
|
|
80
|
+
]
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async reboot(mode) {
|
|
85
|
+
await this.#hidOps.send(
|
|
86
|
+
Reports.GLITTER_BASIC_CMD,
|
|
87
|
+
[Commands.GLITTER_CMD_REBOOT, mode]
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async drawPixel(r, c, brightness) {
|
|
92
|
+
await this.#hidOps.send(
|
|
93
|
+
Reports.GLITTER_BASIC_CMD,
|
|
94
|
+
[Commands.GLITTER_CMD_DRAW_PIXEL, c, r, brightness]
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async drawLine({r1, c1}, {r2, c2}, brightness) {
|
|
99
|
+
await this.#hidOps.send(
|
|
100
|
+
Reports.GLITTER_BASIC_CMD,
|
|
101
|
+
[Commands.GLITTER_CMD_DRAW_PIXEL, r1, c1, r2, c2, brightness]
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// --- generic methods ---
|
|
106
|
+
|
|
107
|
+
async verifyFirmware() {
|
|
108
|
+
try {
|
|
109
|
+
const info = await this.info();
|
|
110
|
+
return (
|
|
111
|
+
info.display_height == HEIGHT &&
|
|
112
|
+
info.display_width == WIDTH
|
|
113
|
+
);
|
|
114
|
+
} catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async version() {
|
|
120
|
+
// Not Available
|
|
121
|
+
return { major: 1, minor: 0 };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async draw(matrix) {
|
|
125
|
+
return await this.#hidOps.send(
|
|
126
|
+
Reports.GLITTER_GRID_PWM_CNTL,
|
|
127
|
+
matrix.flat().map(v => GAMMA[Math.floor((v ?? 0) * 255)])
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#hidOps;
|
|
132
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const Reports = {
|
|
2
|
+
GLITTER_DEVICE_INFO: {
|
|
3
|
+
id: 0x01,
|
|
4
|
+
bytes: 307,
|
|
5
|
+
feature: true,
|
|
6
|
+
},
|
|
7
|
+
GLITTER_BASIC_CMD: {
|
|
8
|
+
id: 0x02,
|
|
9
|
+
bytes: 16,
|
|
10
|
+
feature: true,
|
|
11
|
+
},
|
|
12
|
+
GLITTER_GRID_PWM_CNTL: {
|
|
13
|
+
id: 0x03,
|
|
14
|
+
bytes: 306,
|
|
15
|
+
feature: true,
|
|
16
|
+
},
|
|
17
|
+
GLITTER_GRID_PWM_CNTL: {
|
|
18
|
+
id: 0x04,
|
|
19
|
+
bytes: 306,
|
|
20
|
+
feature: true,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const Commands = {
|
|
25
|
+
GLITTER_CMD_REBOOT: 0x00,
|
|
26
|
+
GLITTER_CMD_SLEEP: 0x01,
|
|
27
|
+
GLITTER_CMD_WAKE_ON_COMMAND: 0x02,
|
|
28
|
+
GLITTER_CMD_SET_SLEEP_TIMEOUT: 0x03,
|
|
29
|
+
GLITTER_CMD_SET_GLOBAL_BRIGHTNESS: 0x04,
|
|
30
|
+
GLITTER_CMD_DRAW_PIXEL: 0x05,
|
|
31
|
+
GLITTER_CMD_DRAW_LINE: 0x06
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const BootMode = {
|
|
35
|
+
BOOTSEL: 0x00,
|
|
36
|
+
NORMAL: 0x01,
|
|
37
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { PortMutex } from './environments/web/PortMutex.js';
|
|
2
|
+
import { PortOperations } from './environments/web/PortOperations.js';
|
|
3
|
+
import { close, getPort } from './environments/web/port.js';
|
|
4
|
+
import { DefaultController } from './firmware/framework-official/DefaultController.js';
|
|
5
|
+
import { SigrootController } from './firmware/sigroot/SigrootController.js';
|
|
6
|
+
|
|
7
|
+
export class SerialControllerFactory {
|
|
8
|
+
static async make(firmware) {
|
|
9
|
+
const port = await getPort();
|
|
10
|
+
if (port) {
|
|
11
|
+
if (firmware === 'framework-official') {
|
|
12
|
+
return await SerialControllerFactory.makeDefaultWebController(port);
|
|
13
|
+
} else if (firmware === 'sigroot') {
|
|
14
|
+
return await SerialControllerFactory.makeSigrootWebController(port);
|
|
15
|
+
} else if (firmware === 'auto') {
|
|
16
|
+
return await SerialControllerFactory.makeAutoWebController(port);
|
|
17
|
+
} else {
|
|
18
|
+
throw new Error(`Unsupported firmware type: ${firmware}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static async makeDefaultWebController(port) {
|
|
24
|
+
if (port?.connected) {
|
|
25
|
+
await close(port);
|
|
26
|
+
await port.open({ baudRate: 115200 });
|
|
27
|
+
return new DefaultController(
|
|
28
|
+
new PortMutex(
|
|
29
|
+
new PortOperations(port)
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static async makeSigrootWebController(port) {
|
|
36
|
+
if (port?.connected) {
|
|
37
|
+
await close(port);
|
|
38
|
+
await port.open({ baudRate: 115200 });
|
|
39
|
+
return new SigrootController(
|
|
40
|
+
new PortMutex(
|
|
41
|
+
new PortOperations(port)
|
|
42
|
+
)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static async makeAutoWebController(port) {
|
|
48
|
+
const ctrl1 = await this.makeDefaultWebController(port);
|
|
49
|
+
if (await ctrl1.verifyFirmware()) {
|
|
50
|
+
return ctrl1;
|
|
51
|
+
}
|
|
52
|
+
const ctrl2 = await this.makeSigrootWebController(port);
|
|
53
|
+
if (await ctrl2.verifyFirmware()) {
|
|
54
|
+
return ctrl2;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export class PortMutex {
|
|
2
|
+
constructor(portOperations) {
|
|
3
|
+
this.#lockName = `port-mutex-${Math.random().toString()}`;
|
|
4
|
+
this.#deduper = new Map();
|
|
5
|
+
this.#portOps = portOperations;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async acquire(fn) {
|
|
9
|
+
const trace = new Error('created bad cb').stack;
|
|
10
|
+
|
|
11
|
+
await this.#enqueue(async () => {
|
|
12
|
+
try {
|
|
13
|
+
await fn(this.#portOps);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
console.error('Error occured in anonymous callback.');
|
|
16
|
+
console.error('Original error:', e);
|
|
17
|
+
console.error('--- This callback was created at ---\n', trace);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async acquireIdempotent(key, fn) {
|
|
23
|
+
const trace = new Error('created bad cb').stack;
|
|
24
|
+
|
|
25
|
+
if (this.#deduper.has(key)) {
|
|
26
|
+
console.info(`"${key}" request coalesced.`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.#deduper.set(key, async () => {
|
|
30
|
+
try {
|
|
31
|
+
await fn(this.#portOps);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
console.error('Error occured in anonymous callback.');
|
|
34
|
+
console.error('Original error:', e);
|
|
35
|
+
console.error('--- This callback was created at ---\n', trace);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await this.#enqueue(() => this.#execDedupedOp(key));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async #enqueue(fn) {
|
|
43
|
+
// `navigator.locks` maintains queue and provides mutual exclusion.
|
|
44
|
+
return navigator.locks.request(this.#lockName, fn);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async #execDedupedOp(key) {
|
|
48
|
+
if (this.#deduper.has(key)) {
|
|
49
|
+
const fn = this.#deduper.get(key);
|
|
50
|
+
this.#deduper.delete(key);
|
|
51
|
+
await fn();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#lockName;
|
|
56
|
+
#deduper;
|
|
57
|
+
#portOps;
|
|
58
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export class PortOperations {
|
|
2
|
+
constructor(port) {
|
|
3
|
+
this.#port = port;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
async rx(length, timeout=3000) {
|
|
7
|
+
if (this.#port === null) {
|
|
8
|
+
throw new Error('attempted RX before port initialization.');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* ReadableStream's built-in locking mechanism cannot be awaited or otherwise
|
|
13
|
+
* asynchronously acquired. A PortMutex must be used.
|
|
14
|
+
*/
|
|
15
|
+
if (this.#port.readable.locked) {
|
|
16
|
+
throw new Error('attempted RX while port locked.');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const response = [];
|
|
20
|
+
const reader = this.#port.readable.getReader();
|
|
21
|
+
const timeoutHandle = setTimeout(() => reader.cancel(), timeout);
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
// Response may be divided across multiple reads
|
|
25
|
+
while (response.length < length) {
|
|
26
|
+
const { value, done } = await reader.read();
|
|
27
|
+
response.push(...(value ?? []));
|
|
28
|
+
if (done || !value) break;
|
|
29
|
+
}
|
|
30
|
+
} finally {
|
|
31
|
+
clearTimeout(timeoutHandle);
|
|
32
|
+
reader.releaseLock();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async tx(buffer) {
|
|
39
|
+
if (this.#port === null) {
|
|
40
|
+
throw new Error('attempted TX before port initialization.');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* WritableStream's built-in locking mechanism cannot be awaited or otherwise
|
|
45
|
+
* asynchronously acquired. A PortMutex must be used.
|
|
46
|
+
*/
|
|
47
|
+
if (this.#port.writable.locked) {
|
|
48
|
+
throw new Error('attempted TX while port locked.');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const writer = this.#port.writable.getWriter();
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
await writer.write(new Uint8Array(buffer));
|
|
55
|
+
} finally {
|
|
56
|
+
/*
|
|
57
|
+
* The writer must be completely torn down between every single write.
|
|
58
|
+
* Many parsers can't handle delayed flushing and write coalescing.
|
|
59
|
+
* Command sequences will fail if `releaseLock()` is used here.
|
|
60
|
+
*/
|
|
61
|
+
await writer.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#port;
|
|
66
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export class PortSelectionCancelled extends Error {
|
|
2
|
+
constructor() {
|
|
3
|
+
super('User cancelled port selection.');
|
|
4
|
+
this.name = this.constructor.name;
|
|
5
|
+
this.date = new Date();
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class PortUnavailable extends Error {
|
|
10
|
+
constructor() {
|
|
11
|
+
super('Selected port already in use.');
|
|
12
|
+
this.name = this.constructor.name;
|
|
13
|
+
this.date = new Date();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function getPort() {
|
|
18
|
+
try {
|
|
19
|
+
const port = await navigator.serial.requestPort();
|
|
20
|
+
const { usbProductId, usbVendorId } = port;
|
|
21
|
+
console.log('Selected port:', port);
|
|
22
|
+
if (usbProductId && usbVendorId) {
|
|
23
|
+
console.log(`VID:PID ${usbVendorId}:${usbProductId}`);
|
|
24
|
+
}
|
|
25
|
+
return port;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
if (e.name == 'NotFoundError') {
|
|
28
|
+
throw new PortSelectionCancelled();
|
|
29
|
+
} else if (e.name == 'InvalidStateError') {
|
|
30
|
+
throw new PortUnavailable();
|
|
31
|
+
} else {
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function close(port) {
|
|
38
|
+
try {
|
|
39
|
+
await port.close();
|
|
40
|
+
} catch(e) {
|
|
41
|
+
if (e.name != 'InvalidStateError') {
|
|
42
|
+
if (e.message != "Failed to execute 'close' on 'SerialPort': The port is already closed.") {
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { HEIGHT, VID_ARR, WIDTH } from '../../../hardware-constants.js';
|
|
2
|
+
import { BitDepth, Command, RX_PACKET_SZ } from './commands.js';
|
|
3
|
+
|
|
4
|
+
export class DefaultController {
|
|
5
|
+
constructor(portMutex) {
|
|
6
|
+
this.#bitDepth = BitDepth.MONO_1BIT;
|
|
7
|
+
this.#portMutex = portMutex;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async verifyFirmware() {
|
|
11
|
+
let packet = null;
|
|
12
|
+
|
|
13
|
+
await this.#portMutex.acquire(async p => {
|
|
14
|
+
await p.tx([...VID_ARR, Command.VERSION]);
|
|
15
|
+
packet = await p.rx(RX_PACKET_SZ);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const isPacket = packet.length == RX_PACKET_SZ;
|
|
19
|
+
const isPadded = packet.slice(3).every(e => e == 0x00);
|
|
20
|
+
|
|
21
|
+
return isPacket && isPadded;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async version() {
|
|
25
|
+
let ver = {};
|
|
26
|
+
|
|
27
|
+
await this.#portMutex.acquire(async p => {
|
|
28
|
+
await p.tx([...VID_ARR, Command.VERSION]);
|
|
29
|
+
const response = await p.rx(RX_PACKET_SZ);
|
|
30
|
+
|
|
31
|
+
// MMMMMMMM mmmmPPPP 0000000p
|
|
32
|
+
ver.major = response[0];
|
|
33
|
+
ver.minor = response[1] >> 4;
|
|
34
|
+
ver.patch = response[1] & 0x0F;
|
|
35
|
+
ver.preRelease = response[2] == 1;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return ver;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async draw(matrix) {
|
|
42
|
+
if (this.#bitDepth == BitDepth.GRAY_8BIT) {
|
|
43
|
+
// Transpose & Gamma Correction
|
|
44
|
+
const payloads = Array.from(
|
|
45
|
+
{ length: WIDTH },
|
|
46
|
+
(_, c) => new Array(
|
|
47
|
+
{ length: HEIGHT },
|
|
48
|
+
(_, r) => GAMMA[Math.floor((matrix[r][c] ?? 0) * 255)]
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Only execute the most recent call
|
|
53
|
+
await this.#portMutex.acquireIdempotent(
|
|
54
|
+
'drawMatrix',
|
|
55
|
+
async p => {
|
|
56
|
+
for (let i = 0; i < WIDTH; i++) {
|
|
57
|
+
await p.tx([Command.STAGE_GREY_COL, i, ...payloads[i]]);
|
|
58
|
+
}
|
|
59
|
+
await p.tx([Command.DRAW_GREY_COL_BUFFER]);
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
else if (this.#bitDepth == BitDepth.MONO_1BIT) {
|
|
65
|
+
let index = 0;
|
|
66
|
+
let output = new Uint8Array(39).fill(0);
|
|
67
|
+
|
|
68
|
+
// Pack cells into bits
|
|
69
|
+
for (let r = 0; r < HEIGHT; r++) {
|
|
70
|
+
for (let c = 0; c < WIDTH; c++) {
|
|
71
|
+
if (matrix[r][c]) {
|
|
72
|
+
output[index >> 3] |= 1 << index % 8;
|
|
73
|
+
}
|
|
74
|
+
index++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await this.#portMutex.acquireIdempotent(
|
|
79
|
+
'drawMatrix',
|
|
80
|
+
async p => {
|
|
81
|
+
await p.tx([...VID_ARR, Command.DRAW, ...output]);
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#bitDepth;
|
|
88
|
+
#portMutex;
|
|
89
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// All replies to all commands are 32 bytes
|
|
2
|
+
export const RX_PACKET_SZ = 32;
|
|
3
|
+
|
|
4
|
+
export const Command = Object.freeze({
|
|
5
|
+
ANIMATE: 0x04,
|
|
6
|
+
BRIGHTNESS: 0x00,
|
|
7
|
+
BOOTLOADER: 0x02,
|
|
8
|
+
DRAW: 0x06,
|
|
9
|
+
DRAW_GREY_COL_BUFFER: 0x08,
|
|
10
|
+
GAME_CTRL: 0x11,
|
|
11
|
+
GAME_STATUS: 0x12,
|
|
12
|
+
PANIC: 0x05,
|
|
13
|
+
PATTERN: 0x01,
|
|
14
|
+
SLEEP: 0x03,
|
|
15
|
+
STAGE_GREY_COL: 0x07,
|
|
16
|
+
START_GAME: 0x10,
|
|
17
|
+
VERSION: 0x20,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Used with Command.PATTERN
|
|
21
|
+
export const Pattern = Object.freeze({
|
|
22
|
+
DISPLAY_LOTUS_HORIZONTAL: 0x03,
|
|
23
|
+
DISPLAY_LOTUS_VERTICAL: 0x07,
|
|
24
|
+
DISPLAY_PANIC: 0x06,
|
|
25
|
+
DOUBLE_GRADIENT: 0x02,
|
|
26
|
+
FULL_BRIGHTNESS: 0x05,
|
|
27
|
+
GRADIENT: 0x01,
|
|
28
|
+
PERCENTAGE: 0x00,
|
|
29
|
+
ZIG_ZAG: 0x04,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// DRAW or DRAW_GREY_COL_BUFFER
|
|
33
|
+
export const BitDepth = Object.freeze({
|
|
34
|
+
GRAY_8BIT: '8-bit',
|
|
35
|
+
MONO_1BIT: '1-bit',
|
|
36
|
+
})
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { GAMMA } from '../../../hardware-constants.js';
|
|
2
|
+
import { Command, IDENT_STR_LEN, IDENT_STR_REGEX } from './commands.js';
|
|
3
|
+
|
|
4
|
+
export class SigrootController {
|
|
5
|
+
constructor(portMutex) {
|
|
6
|
+
this.#portMutex = portMutex;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async verifyFirmware() {
|
|
10
|
+
let ident = null;
|
|
11
|
+
|
|
12
|
+
await this.#portMutex.acquire(async p => {
|
|
13
|
+
await p.tx([Command.IDENT]);
|
|
14
|
+
ident = await p.rx(IDENT_STR_LEN);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
ident = String.fromCharCode(...ident);
|
|
18
|
+
|
|
19
|
+
return ident.match(IDENT_STR_REGEX);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async version() {
|
|
23
|
+
let ident = null;
|
|
24
|
+
|
|
25
|
+
await this.#portMutex.acquire(async p => {
|
|
26
|
+
await p.tx([Command.IDENT]);
|
|
27
|
+
ident = await p.rx(IDENT_STR_LEN);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
ident = String.fromCharCode(...ident);
|
|
31
|
+
|
|
32
|
+
const match = ident.match(IDENT_STR_REGEX);
|
|
33
|
+
|
|
34
|
+
if (match && match.length == 3) {
|
|
35
|
+
return {
|
|
36
|
+
major: match[1],
|
|
37
|
+
minor: match[2]
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async draw(matrix) {
|
|
45
|
+
if (!this.#scaleInitialized) {
|
|
46
|
+
await this.#portMutex.acquire(
|
|
47
|
+
async p => {
|
|
48
|
+
await p.tx([Command.SET_CONST_SCALE, 0x20]);
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
this.#scaleInitialized = true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const bytes = matrix.flat().map(v =>
|
|
55
|
+
GAMMA[Math.floor((v ?? 0) * 255)]
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// Only execute the most recent call
|
|
59
|
+
await this.#portMutex.acquireIdempotent(
|
|
60
|
+
'drawMatrix',
|
|
61
|
+
async p => {
|
|
62
|
+
await p.tx([Command.DRAW_PWM, ...bytes]);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#portMutex;
|
|
68
|
+
#scaleInitialized;
|
|
69
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const Command = Object.freeze({
|
|
2
|
+
/* 000 */ NOOP: 0x00,
|
|
3
|
+
/* 'd' */ ANIMATION_DIAMOND: 0x64,
|
|
4
|
+
/* 'b' */ ANIMATION_FIRE: 0x62,
|
|
5
|
+
/* 'f' */ ANIMATION_FIREPLACE: 0x66,
|
|
6
|
+
/* 'g' */ ANIMATION_GEAR: 0x67,
|
|
7
|
+
/* 'r' */ ANIMATION_RING: 0x72,
|
|
8
|
+
/* 'a' */ ANIMATION_STARTUP: 0x61,
|
|
9
|
+
/* 'A' */ ANIMATION_STARTUP_ONCE: 0x41,
|
|
10
|
+
/* 'e' */ BOOTLOADER: 0x65,
|
|
11
|
+
/* 'm' */ DRAW_PWM: 0x6D,
|
|
12
|
+
/* 'M' */ DRAW_PWM_BLOCKING: 0x4D,
|
|
13
|
+
/* 'n' */ DRAW_SCALE: 0x6E,
|
|
14
|
+
/* 'N' */ DRAW_SCALE_BLOCKING: 0x4E,
|
|
15
|
+
/* 'c' */ FLUSH_CMD_QUEUE: 0x63,
|
|
16
|
+
/* 't' */ TEST_PATTERN: 0x74,
|
|
17
|
+
/* 'w' */ SET_CONST_PWM: 0x77,
|
|
18
|
+
/* 's' */ SET_CONST_SCALE: 0x73,
|
|
19
|
+
/* 'p' */ SET_PX_PWM: 0x70,
|
|
20
|
+
/* 'q' */ SET_PX_SCALE: 0x71,
|
|
21
|
+
/* 127 */ IDENT: 0x7F,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const IDENT_STR_LEN = 25;
|
|
25
|
+
|
|
26
|
+
export const IDENT_STR_REGEX = /^Sig\sFW\sLED\sMatrix\sFW\sV(\d+)\.(\d+)$/;
|
|
27
|
+
|
|
28
|
+
export const IDENT_STR_PREFIX = 'Sig FW LED Matrix FW';
|