|
| 1 | +const io = require("./socket.io.min.js"); |
| 2 | + |
| 3 | +class ArduinoUnoQ { |
| 4 | + constructor(host, port) { |
| 5 | + this.serverURL = `wss://${host}:${port}`; |
| 6 | + |
| 7 | + this.io = io(this.serverURL, { |
| 8 | + path: "/socket.io", |
| 9 | + transports: ["polling", "websocket"], |
| 10 | + autoConnect: true, |
| 11 | + }); |
| 12 | + this.isConnected = false; |
| 13 | + |
| 14 | + this._setupConnectionHandlers(); |
| 15 | + } |
| 16 | + |
| 17 | + _setupConnectionHandlers() { |
| 18 | + this.io.on("connect", () => { |
| 19 | + this.isConnected = true; |
| 20 | + console.log(`Connected to Arduino UNO Q at ${this.serverURL}`); |
| 21 | + }); |
| 22 | + |
| 23 | + this.io.on("disconnect", (reason) => { |
| 24 | + this.isConnected = false; |
| 25 | + console.log(`Disconnected from Arduino UNO Q: ${reason}`); |
| 26 | + }); |
| 27 | + |
| 28 | + this.io.on("connect_error", (error) => { |
| 29 | + console.error(`Connection error:`, error.message); |
| 30 | + }); |
| 31 | + |
| 32 | + this.io.on("reconnect", (attemptNumber) => { |
| 33 | + console.log(`Reconnected to Arduino UNO Q after ${attemptNumber} attempts`); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + connect() { |
| 38 | + if (!this.io.connected) { |
| 39 | + console.log("Attempting to connect to Arduino UNO Q..."); |
| 40 | + this.io.connect(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + disconnect() { |
| 45 | + if (this.io.connected) { |
| 46 | + console.log("Disconnecting from Arduino UNO Q..."); |
| 47 | + this.io.disconnect(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // ===== LED CONTROL METHODS ===== |
| 52 | + /** |
| 53 | + * Set RGB LED color |
| 54 | + * @param {string} led - LED identifier ("LED3" or "LED4") |
| 55 | + * @param {number} r - Red value (0-255) |
| 56 | + * @param {number} g - Green value (0-255) |
| 57 | + * @param {number} b - Blue value (0-255) |
| 58 | + */ |
| 59 | + setLedRGB(led, r, g, b) { |
| 60 | + this.io.emit("set_led_rgb", { |
| 61 | + led: led, |
| 62 | + r: Math.max(0, Math.min(255, r)), |
| 63 | + g: Math.max(0, Math.min(255, g)), |
| 64 | + b: Math.max(0, Math.min(255, b)), |
| 65 | + }); |
| 66 | + console.log(`Setting ${led} to RGB(${r}, ${g}, ${b})`); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Turn off LED |
| 71 | + * @param {string} led - LED identifier ("LED3" or "LED4") |
| 72 | + */ |
| 73 | + turnOffLed(led) { |
| 74 | + this.setLedRGB(led, 0, 0, 0); |
| 75 | + } |
| 76 | + |
| 77 | + // ===== MATRIX CONTROL METHODS ===== |
| 78 | + |
| 79 | + /** |
| 80 | + * Draw frame on LED matrix |
| 81 | + * @param {string} frame - 25-character string representing 5x5 matrix (0s and 1s) |
| 82 | + */ |
| 83 | + matrixDraw(frame) { |
| 84 | + if (typeof frame !== "string" || frame.length !== 25) { |
| 85 | + console.error("Invalid frame format. Expected 25-character string of 0s and 1s"); |
| 86 | + return; |
| 87 | + } |
| 88 | + // Validate frame contains only 0s and 1s |
| 89 | + if (!/^[01]+$/.test(frame)) { |
| 90 | + console.error("Frame must contain only 0s and 1s"); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + this.io.emit("matrix_draw", { frame: frame }); |
| 95 | + console.log(`Drawing matrix frame: ${frame}`); |
| 96 | + } |
| 97 | + |
| 98 | + matrixClear() { |
| 99 | + const clearFrame = "0".repeat(25); |
| 100 | + this.matrixDraw(clearFrame); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +module.exports = ArduinoUnoQ; |
0 commit comments