public Mcu(ConfigWrapper config, ClockSync clocksync) { this._printer = config.get_printer(); this._clocksync = clocksync; this._reactor = _printer.get_reactor(); this._name = config.get_name(); if (this._name.StartsWith("mcu ")) { this._name = this._name.Substring(4); } this._printer.register_event_handler("klippy:connect", this._connect); this._printer.register_event_handler("klippy:shutdown", this._shutdown); this._printer.register_event_handler("klippy:disconnect", this._disconnect); // Serial port this._serialport = config.get("serial", "/dev/ttyS0"); var baud = 0; if (!(this._serialport.StartsWith("/dev/rpmsg_") || this._serialport.StartsWith("/tmp/klipper_host_"))) { baud = (int)config.getint("baud", 250000, minval: 2400); } this._serial = new SerialReader(this._reactor, this._serialport, baud); // Restarts this._restart_method = RestartMethod.Command; if (baud != 0) { this._restart_method = config.getEnum <RestartMethod>("restart_method", RestartMethod.None); } this._reset_cmd = null; this._emergency_stop_cmd = null; this._is_shutdown = false; this._shutdown_msg = ""; // Config building this._printer.lookup_object <PrinterPins>("pins").register_chip(this._name, this); this._oid_count = 0; this._config_callbacks = new List <Action>(); this._init_cmds = new List <string>(); this._config_cmds = new List <string>(); this._pin_map = config.get("pin_map", null); this._custom = config.get("custom", ""); this._mcu_freq = 0.0; // Move command queuing this._max_stepper_error = config.getfloat("max_stepper_error", 2.5E-05, minval: 0.0); this._move_count = 0; this._stepqueues = new List <stepcompress>(); this._steppersync = null; // Stats this._stats_sumsq_base = 0.0; this._mcu_tick_avg = 0.0; this._mcu_tick_stddev = 0.0; this._mcu_tick_awake = 0.0; }
void _connect() { if (this.is_fileoutput()) { this._connect_file(); } else { if (this._restart_method == RestartMethod.Rpi_usb && !System.IO.File.Exists(this._serialport)) { // Try toggling usb power this._check_restart("enable power"); } this._serial.Connect(); this._clocksync.connect(this._serial); } var msgparser = this._serial.msgparser; var name = this._name; var log_info = new List <string> { $"Loaded MCU '{name}' {msgparser.messages_by_id.Count} commands ({msgparser.version} / {msgparser.build_versions})", $"MCU '{name}' config: {string.Join(" ", msgparser.config.Select((a) => $"{a.Key}={a.Value}"))}" }; logging.Info(log_info[0]); logging.Info(log_info[1]); this._mcu_freq = this.get_constant_float("CLOCK_FREQ"); this._stats_sumsq_base = this.get_constant_float("STATS_SUMSQ_BASE"); this._emergency_stop_cmd = this.lookup_command("emergency_stop"); this._reset_cmd = this.try_lookup_command("reset"); this._config_reset_cmd = this.try_lookup_command("config_reset"); if (this._restart_method == RestartMethod.None && (this._reset_cmd != null || this._config_reset_cmd != null) && msgparser.get_constant("SERIAL_BAUD", null) == null) { this._restart_method = RestartMethod.Command; } this.register_msg(this._handle_shutdown, "shutdown"); this.register_msg(this._handle_shutdown, "is_shutdown"); this.register_msg(this._handle_mcu_stats, "stats"); this._check_config(); var move_msg = $"Configured MCU '{name}' ({this._move_count} moves)"; logging.Info(move_msg); log_info.Add(move_msg); this._printer.set_rollover_info(name, string.Join("\n", log_info), log: false); }