···11import { question, type PromptValidator } from "@topcli/prompts";
22import type { Applet } from "../Applet.js";
33-44-/*
55- * Information on the Sony IR protocol (SIRC):
66- * http://www.sbprojects.net/knowledge/ir/sirc.php
77- */
88-99-interface SonyProtocolVariation {
1010- commandLengthBits: 7;
1111- deviceTypeLengthBits: 5 | 8;
1212- extensionLengthBits: 0 | 8;
1313-}
1414-1515-const SONY_PROTOCOL_12_BITS: SonyProtocolVariation = {
1616- commandLengthBits: 7,
1717- deviceTypeLengthBits: 5,
1818- extensionLengthBits: 0,
1919-};
2020-const SONY_PROTOCOL_15_BITS: SonyProtocolVariation = {
2121- commandLengthBits: 7,
2222- deviceTypeLengthBits: 8,
2323- extensionLengthBits: 0,
2424-};
2525-const SONY_PROTOCOL_20_BITS: SonyProtocolVariation = {
2626- commandLengthBits: 7,
2727- deviceTypeLengthBits: 5,
2828- extensionLengthBits: 8,
2929-};
3030-const DATA_LENGTH_BITS_TO_SONY_PROTOCOL_VARIATION = new Map<
3131- number,
3232- SonyProtocolVariation
3333->([
3434- [12, SONY_PROTOCOL_12_BITS],
3535- [15, SONY_PROTOCOL_15_BITS],
3636- [20, SONY_PROTOCOL_20_BITS],
3737-]);
33+import { DATA_LENGTH_BITS_TO_SONY_PROTOCOL_VARIATION } from "../util/sony.ts";
44+import { extractNum } from "../util/bit-manipulation.ts";
55+import {
66+ parseUnfoldedHex,
77+ UNFOLDED_CIRCLE_PROTOCOL_SONY,
88+} from "../util/unfolded-circle.ts";
3893910// `<string>` is commented out and we have to check the type because of https://github.com/TopCli/prompts/issues/147
4011const sonyUnfoldedHexValidator: PromptValidator /*<string>*/ = {
···12293 console.log(`Extension: ${extension}`);
12394 },
12495};
125125-126126-/////////////////////////////
127127-128128-const UNFOLDED_CIRCLE_PROTOCOL_SONY = 4;
129129-130130-interface ParsedUnfoldedHex {
131131- protocolNum: number;
132132- data: number;
133133- dataLengthBits: number;
134134- repeatCount: number;
135135-}
136136-137137-function parseUnfoldedHex(unfoldedHex: string): ParsedUnfoldedHex | undefined {
138138- const match = unfoldedHex.match(
139139- /(?<protocolNum>\d+);(?<hexData>0x[0-9A-Fa-f]+);(?<dataLengthBits>\d+);(?<repeatCount>\d+)/,
140140- );
141141- if (match == null) {
142142- return undefined;
143143- }
144144-145145- return {
146146- protocolNum: Number.parseInt(match.groups!["protocolNum"]!),
147147- data: Number.parseInt(match.groups!["hexData"]!),
148148- dataLengthBits: Number.parseInt(match.groups!["dataLengthBits"]!),
149149- repeatCount: Number.parseInt(match.groups!["repeatCount"]!),
150150- };
151151-}
152152-153153-function getMaxUnsignedInt(lengthBits: number): number {
154154- return (1 << lengthBits) - 1;
155155-}
156156-157157-function extractNum(params: {
158158- input: number;
159159- extractedNumLengthBits: number;
160160- leastSignificantBit: number;
161161- reverseResultBits: boolean;
162162-}): number {
163163- const mask =
164164- getMaxUnsignedInt(params.extractedNumLengthBits) <<
165165- params.leastSignificantBit;
166166- let result = params.input & mask;
167167- result = result >> params.leastSignificantBit;
168168-169169- if (params.reverseResultBits) {
170170- result = reverseBits(result, params.extractedNumLengthBits);
171171- }
172172- return result;
173173-}
174174-175175-// Copied (with type annotations added and a parameter renamed) from https://stackoverflow.com/a/67064710
176176-// under the CC BY-SA 4.0 license (https://creativecommons.org/licenses/by-sa/4.0/)
177177-function reverseBits(integer: number, sizeBits: number): number {
178178- if (sizeBits > 32) {
179179- throw Error(
180180- "Bit manipulation is limited to <= 32 bit numbers in JavaScript.",
181181- );
182182- }
183183-184184- let result = 0;
185185- for (let i = 0; i < sizeBits; i++) {
186186- result |= ((integer >> i) & 1) << (sizeBits - 1 - i);
187187- }
188188-189189- return result >>> 0; // >>> 0 makes it unsigned even if bit 32 (the sign bit) was set
190190-}
+38
src/util/bit-manipulation.ts
···11+export function extractNum(params: {
22+ input: number;
33+ extractedNumLengthBits: number;
44+ leastSignificantBit: number;
55+ reverseResultBits: boolean;
66+}): number {
77+ const mask =
88+ getMaxUnsignedInt(params.extractedNumLengthBits) <<
99+ params.leastSignificantBit;
1010+ let result = params.input & mask;
1111+ result = result >> params.leastSignificantBit;
1212+1313+ if (params.reverseResultBits) {
1414+ result = reverseBits(result, params.extractedNumLengthBits);
1515+ }
1616+ return result;
1717+}
1818+1919+// Copied (with type annotations added and a parameter renamed) from https://stackoverflow.com/a/67064710
2020+// under the CC BY-SA 4.0 license (https://creativecommons.org/licenses/by-sa/4.0/)
2121+export function reverseBits(integer: number, sizeBits: number): number {
2222+ if (sizeBits > 32) {
2323+ throw Error(
2424+ "Bit manipulation is limited to <= 32 bit numbers in JavaScript.",
2525+ );
2626+ }
2727+2828+ let result = 0;
2929+ for (let i = 0; i < sizeBits; i++) {
3030+ result |= ((integer >> i) & 1) << (sizeBits - 1 - i);
3131+ }
3232+3333+ return result >>> 0; // >>> 0 makes it unsigned even if bit 32 (the sign bit) was set
3434+}
3535+3636+export function getMaxUnsignedInt(lengthBits: number): number {
3737+ return (1 << lengthBits) - 1;
3838+}