|
| 1 | +const USB = require('usb') |
| 2 | +const PUSH_VID = 0x2982 |
| 3 | +const PUSH_PID = 0x1967 |
| 4 | +const PUSH_FRAME_SIZE = 327680 |
| 5 | +const PUSH_LINE_SIZE = 2048 |
| 6 | +const PUSH_BUFFER_SIZE = PUSH_FRAME_SIZE |
| 7 | +const PUSH_XOR_PATTERN = 0xFFE7F3E7 |
| 8 | +const PUSH_BUFFER_COUNT = PUSH_FRAME_SIZE / PUSH_BUFFER_SIZE |
| 9 | +const PUSH_FRAME_HEADER = [0xFF, 0xCC, 0xAA, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] |
| 10 | + |
| 11 | +var endpoint; |
| 12 | +var interface; |
| 13 | + |
| 14 | +const frameData = new ArrayBuffer(327680) |
| 15 | + |
| 16 | +/* The actual conversion function */ |
| 17 | +function convertImage(image, frame) { |
| 18 | + var y,h,x,w; |
| 19 | + for(y=0;y<160;y++) { |
| 20 | + const frameLineStart = PUSH_LINE_SIZE * y |
| 21 | + const frameLine = new Uint16Array(frame, frameLineStart, PUSH_LINE_SIZE / 2) |
| 22 | + for(x=0;x<1024;x++) { |
| 23 | + var word = 0 |
| 24 | + if (x < 960) { |
| 25 | + const off = (y * 3840) + x * 4 |
| 26 | + const r = image[off + 0] >> 3 |
| 27 | + const g = image[off + 1] >> 2 |
| 28 | + const b = image[off + 2] >> 3 |
| 29 | + word = (r & 0b11111) | ((g & 0b111111) << 5) | ((b & 0b11111) << 11) |
| 30 | + } |
| 31 | + var mask = 0xF3E7 |
| 32 | + if (x & 1 > 0) { |
| 33 | + mask = 0xFFE7 |
| 34 | + } |
| 35 | + frameLine[x] = word ^ mask |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | +/* this is thi actual device init */ |
| 40 | +function _init(push) { |
| 41 | + if (push == null) { return; } |
| 42 | + push.open() |
| 43 | + const pushInterface = push.interface(0) |
| 44 | + pushInterface.claim() |
| 45 | + endpoint = pushInterface.endpoint(1) |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * initialize the push device. his will open the push device and claim the display interface. |
| 50 | + * This function also sets up callbacks for later device addition, so that the push gets |
| 51 | + * initialized if added to the system later |
| 52 | + * @param { function(error) } callback - callback will be called after init. Follows node's "error first" async convention |
| 53 | + */ |
| 54 | +function initPush(callback) { |
| 55 | + var push = USB.findByIds(PUSH_VID, PUSH_PID) |
| 56 | + USB.on('attach', function(device) { |
| 57 | + if (device.deviceDescriptor.idVendor === PUSH_VID && device.deviceDescriptor.idProduct === PUSH_PID) { |
| 58 | + _init(device) |
| 59 | + } |
| 60 | + }) |
| 61 | + USB.on('detach', function(device) { |
| 62 | + if (device.deviceDescriptor.idVendor === PUSH_VID && device.deviceDescriptor.idProduct === PUSH_PID) { |
| 63 | + endpoint = null |
| 64 | + } |
| 65 | + }) |
| 66 | + if (push == null) { |
| 67 | + callback(new Error("Push not found")) |
| 68 | + } |
| 69 | + _init(push) |
| 70 | + callback() |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * sends the current content of the given canvas context to the push. |
| 75 | + * The function assumes the canvas has the correct dimenstions. |
| 76 | + * It's fine to call this function even if initPush returned an error, as Push may have |
| 77 | + * been added to the system at a later time. |
| 78 | + * @param { Context2D } ctx - the canvas context. Both a native web canvas and node-canvas work. |
| 79 | + * @param { function(error) } callback - callback will be called after init. Follows node's "error first" async convention |
| 80 | + */ |
| 81 | +function sendFrame(ctx, callback) { |
| 82 | + if (!endpoint) { callback(new Error("No Push available")); return; } // push not available |
| 83 | + convertImage(ctx.getImageData(0,0,960,160).data, frameData) |
| 84 | + endpoint.transfer(Buffer.from(PUSH_FRAME_HEADER), function (error) { |
| 85 | + if (!error) { |
| 86 | + endpoint.transfer(Buffer.from(frameData), function (error) { |
| 87 | + if (!error) { |
| 88 | + callback() |
| 89 | + } else { |
| 90 | + callback(error) |
| 91 | + } |
| 92 | + }) |
| 93 | + } else { |
| 94 | + callback(error) |
| 95 | + } |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + initPush: initPush, |
| 101 | + sendFrame: sendFrame |
| 102 | +} |
0 commit comments