--[[ ======================================== UINT16 TO 8 DOUBLE POINT OUTPUTS ======================================== PURPOSE: Converts a single 16-bit unsigned integer input into 8 output tags. Each output represents 2 bits (Double Point value). LOGIC: The 16-bit input is split into 8 groups of 2 bits: Bits mapping: Output 1 → bits 0-1 Output 2 → bits 2-3 Output 3 → bits 4-5 Output 4 → bits 6-7 Output 5 → bits 8-9 Output 6 → bits 10-11 Output 7 → bits 12-13 Output 8 → bits 14-15 DOUBLE POINT MAPPING: 00 → 0 01 → 1 10 → 2 11 → 3 INPUT REQUIREMENT: Only input["1"] is used (UINT16 value) OUTPUT: output["1"] to output["8"] will be generated EXAMPLE: Input value = 3 (binary: 0000 0000 0000 0011) Output: Tag1 → 3 Tag2 → 0 Tag3 → 0 ... Tag8 → 0 IMPORTANT: - Ensure input value is within 0–65535 - Each output will contain one vector element ======================================== ]] function Uint16ToDoublePoint(input) -- Handle nil input if input == nil then return {} end local output = {} -- Read only first input tag local inVec = input["1"] if type(inVec) ~= "table" then return {} end -- Process each element in input vector for i, v in ipairs(inVec) do -- Get UINT16 value local value = v.value or 0 -- Ensure value is within 16-bit range value = value % 65536 -- Loop through 8 outputs (2 bits each) for bitPair = 0, 7 do -- Extract 2 bits local shift = bitPair * 2 local twoBits = math.floor(value / (2 ^ shift)) % 4 -- Assign to corresponding output tag local outKey = tostring(bitPair + 1) -- Initialize output vector if not exists if output[outKey] == nil then output[outKey] = {} end table.insert(output[outKey], { value = twoBits, -- Double point value (0–3) quality = v.quality or 0, timestamp = v.timestamp or 0 }) end end return output end