95 lines
3.9 KiB
Python
95 lines
3.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
from .processor_utils import get_scl_representation, format_variable_name,get_target_scl_name
|
|
|
|
|
|
# TODO: Import necessary functions from processor_utils
|
|
# Example: from .processor_utils import get_scl_representation, format_variable_name
|
|
# Or: import processors.processor_utils as utils
|
|
|
|
# TODO: Define constants if needed (e.g., SCL_SUFFIX) or import them
|
|
SCL_SUFFIX = "_scl"
|
|
|
|
# --- Function code starts ---
|
|
def process_counter(instruction, network_id, scl_map, access_map, data):
|
|
"""
|
|
Genera SCL para Contadores (CTU, CTD, CTUD).
|
|
Requiere datos de instancia (DB o STAT).
|
|
"""
|
|
instr_uid = instruction["instruction_uid"]
|
|
instr_type = instruction["type"] # CTU, CTD, CTUD
|
|
if instr_type.endswith(SCL_SUFFIX) or "_error" in instr_type:
|
|
return False
|
|
|
|
# 1. Obtener Inputs (varía según tipo)
|
|
params = []
|
|
resolved = True
|
|
|
|
input_pins = []
|
|
if instr_type == "CTU": input_pins = ["CU", "R", "PV"]
|
|
elif instr_type == "CTD": input_pins = ["CD", "LD", "PV"]
|
|
elif instr_type == "CTUD": input_pins = ["CU", "CD", "R", "LD", "PV"]
|
|
else:
|
|
instruction["scl"] = f"// ERROR: Tipo de contador no soportado: {instr_type}"
|
|
instruction["type"] += "_error"
|
|
return True # Procesado con error
|
|
|
|
for pin in input_pins:
|
|
pin_info = instruction["inputs"].get(pin)
|
|
if pin_info is None and pin not in ["R", "LD"]: # R y LD pueden no estar conectados
|
|
print(f"Error: Falta entrada requerida '{pin}' para {instr_type} UID {instr_uid}.")
|
|
# Permitir continuar si solo faltan R o LD opcionales? Por ahora no.
|
|
instruction["scl"] = f"// ERROR: Falta entrada requerida '{pin}' para {instr_type} UID {instr_uid}."
|
|
instruction["type"] += "_error"
|
|
return True # Error
|
|
elif pin_info: # Si el pin existe en el JSON
|
|
scl_pin = get_scl_representation(pin_info, network_id, scl_map, access_map)
|
|
if scl_pin is None:
|
|
resolved = False
|
|
break # Salir si una dependencia no está lista
|
|
scl_pin_formatted = format_variable_name(scl_pin) if pin_info.get("type") == "variable" else scl_pin
|
|
params.append(f"{pin} := {scl_pin_formatted}")
|
|
|
|
if not resolved: return False
|
|
|
|
# 2. Obtener Nombre de Instancia (NECESITA MEJORA EN x1.py)
|
|
instance_name = instruction.get("instance_db")
|
|
if not instance_name:
|
|
instance_name = f"#COUNTER_INSTANCE_{instr_uid}" # Placeholder
|
|
print(f"Advertencia: No se encontró instancia para {instr_type} UID {instr_uid}. Usando placeholder '{instance_name}'. Ajustar x1.py y declarar en x3.py.")
|
|
else:
|
|
instance_name = format_variable_name(instance_name)
|
|
|
|
# 3. Generar la llamada SCL
|
|
param_string = ", ".join(params)
|
|
scl_call = f"{instance_name}({param_string}); // TODO: Declarar {instance_name} : {instr_type}; en VAR_STAT o VAR"
|
|
|
|
instruction["scl"] = scl_call
|
|
instruction["type"] = instr_type + SCL_SUFFIX
|
|
|
|
# 4. Actualizar scl_map para las salidas (QU, QD, CV)
|
|
output_pins = []
|
|
if instr_type == "CTU": output_pins = ["QU", "CV"]
|
|
elif instr_type == "CTD": output_pins = ["QD", "CV"]
|
|
elif instr_type == "CTUD": output_pins = ["QU", "QD", "CV"]
|
|
|
|
for pin in output_pins:
|
|
map_key = (network_id, instr_uid, pin)
|
|
scl_map[map_key] = f"{instance_name}.{pin}"
|
|
# Contadores no tienen ENO estándar en LAD/FBD
|
|
|
|
return True
|
|
|
|
# --- Procesador de Comparadores (EQ ya existe, añadir otros) ---
|
|
|
|
# --- Function code ends ---
|
|
|
|
# --- Processor Information Function ---
|
|
def get_processor_info():
|
|
"""Devuelve la información para los contadores CTU, CTD, CTUD."""
|
|
# Asumiendo que los tipos en el JSON son CTU, CTD, CTUD
|
|
return [
|
|
{'type_name': 'ctu', 'processor_func': process_counter, 'priority': 5},
|
|
{'type_name': 'ctd', 'processor_func': process_counter, 'priority': 5},
|
|
{'type_name': 'ctud', 'processor_func': process_counter, 'priority': 5}
|
|
]
|