UNPKG

pulseplot

Version:
94 lines (81 loc) 2.69 kB
/** @file BroadlinkRM JS. @author Christian W. Zuckschwerdt <zany@triq.net> @copyright Christian W. Zuckschwerdt, 2025 @license This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. */ export class BroadlinkRM { static isBroadlinkRM(line) { try { bin = atob(line) } catch (error) { return false // Not base64 } if (bin.length < 4) { return false // Invalid data header } const codeType = bin.charCodeAt(0) if (codeType == 0xb2 || codeType == 0xb1) { return true // 433 MHz } else if (codeType == 0xd7) { return true // 315 MHz } else if (codeType == 0x26) { return false // IR code } else { return false // Unknown code type } } static decodeData(line) { let data = { pulses: [], frequency: 0, repeats: 0, errors: "", } let bin = "" try { bin = atob(line) } catch (error) { data.errors = 'Invalid Base64' console.warn(data.errors) return data } if (bin.length < 4) { data.errors = 'Invalid data header' console.warn(data.errors) return data } const codeType = bin.charCodeAt(0) if (codeType == 0xb2 || codeType == 0xb1) { data.frequency = 433920000 // 433 MHz } else if (codeType == 0xd7) { data.frequency = 315000000 // 315 MHz } else if (codeType == 0x26 /* IR code */) { data.errors = 'Unsupported code type' console.warn(data.errors, codeType) return data } else { data.errors = `Invalid code type (${codeType.toString(16)})` console.warn(data.errors, codeType) return data } data.repeats = bin.charCodeAt(1) let dataLen = bin.charCodeAt(2) | (bin.charCodeAt(3) << 8) // big endian if (dataLen + 2 != bin.length) { data.errors = 'Code length mismatch' console.warn(data.errors) } for (let i = 4; i < bin.length; i++) { let pulse = bin.charCodeAt(i); if (pulse == 0) { pulse = bin.charCodeAt(++i) | (bin.charCodeAt(++i) << 8) // big endian } data.pulses.push(pulse) } return data } }