@@ -100,7 +100,7 @@ static void state_thc_pid (void);
100100static void state_thc_adjust (void );
101101static void state_vad_lock (void );
102102
103- static bool set_feed_override = false, updown_enabled = false;
103+ static bool set_feed_override = false, updown_enabled = false, init_ok = false ;
104104static uint8_t n_ain , n_din ;
105105static uint8_t port_arc_ok , port_arc_voltage , port_cutter_down , port_cutter_up ;
106106static uint_fast8_t feed_override , segment_id = 0 ;
@@ -479,9 +479,17 @@ static bool is_setting_available (const setting_detail_t *setting)
479479 ok = n_din >= 3 ;
480480 break ;
481481
482- default :
482+ case Setting_Arc_VoltagePort :
483483 ok = n_ain >= 1 ;
484484 break ;
485+
486+ case Setting_THC_VADThreshold :
487+ ok = init_ok ;
488+ break ;
489+
490+ default :
491+ ok = init_ok && plasma .mode == Plasma_ModeVoltage ;
492+ break ;
485493 }
486494
487495 return ok ;
@@ -546,9 +554,38 @@ static void plasma_settings_save (void)
546554 hal .nvs .memcpy_to_nvs (nvs_address , (uint8_t * )& plasma , sizeof (plasma_settings_t ), true);
547555}
548556
557+ static uint8_t ioport_find_free (io_port_type_t type , io_port_direction_t dir , char * description )
558+ {
559+ uint8_t port ;
560+ bool found = false;
561+ xbar_t * pin ;
562+
563+ if (description ) {
564+ port = ioports_available (type , dir );
565+ do {
566+ if ((pin = hal .port .get_pin_info (type , dir , -- port ))) {
567+ if ((found = pin -> description && !strcmp (pin -> description , description )))
568+ port = pin -> id ;
569+ }
570+ } while (port && !found );
571+ }
572+
573+ if (!found ) {
574+ port = ioports_available (type , dir );
575+ do {
576+ if ((pin = hal .port .get_pin_info (type , dir , -- port ))) {
577+ if ((found = !pin -> mode .claimed ))
578+ port = pin -> id ;
579+ }
580+ } while (port && !found );
581+ }
582+
583+ return found ? port : 255 ;
584+ }
585+
549586static void plasma_settings_restore (void )
550587{
551- plasma .mode = updown_enabled ? Plasma_ModeUpDown : Plasma_ModeVoltage ;
588+ plasma .mode = updown_enabled ? Plasma_ModeUpDown : Plasma_ModeVoltage ;
552589 plasma .thc_delay = 3.0f ;
553590 plasma .thc_threshold = 1.0f ;
554591 plasma .thc_override = 100 ;
@@ -570,25 +607,23 @@ static void plasma_settings_restore (void)
570607 plasma .pid .d_gain = 0.0f ;
571608
572609 if (ioport_can_claim_explicit ()) {
573-
574- n_ain = ioports_available (Port_Analog , Port_Input );
575- n_din = ioports_available (Port_Digital , Port_Input );
576-
577- plasma .port_arc_voltage = n_ain - 1 ;
578- plasma .port_arc_ok = n_din - 1 ;
579- plasma .port_cutter_down = updown_enabled ? n_din - 2 : 255 ;
580- plasma .port_cutter_up = updown_enabled ? n_din - 3 : 255 ;
610+ plasma .port_arc_voltage = ioport_find_free (Port_Analog , Port_Input , "Arc voltage" );
611+ plasma .port_arc_ok = ioport_find_free (Port_Digital , Port_Input , "Arc ok" );
612+ plasma .port_cutter_down = updown_enabled && plasma .port_arc_ok >= 1 ? plasma .port_arc_ok - 1 : 255 ;
613+ plasma .port_cutter_up = updown_enabled && plasma .port_arc_ok >= 2 ? plasma .port_arc_ok - 2 : 255 ;
581614 }
582615
583616 hal .nvs .memcpy_to_nvs (nvs_address , (uint8_t * )& plasma , sizeof (plasma_settings_t ), true);
584617}
585618
586619static void plasma_settings_load (void )
587620{
588- bool ok = true;
621+ init_ok = true;
589622
590- if (hal .nvs .memcpy_from_nvs ((uint8_t * )& plasma , nvs_address , sizeof (plasma_settings_t ), true) != NVS_TransferResult_OK )
623+ if (hal .nvs .memcpy_from_nvs ((uint8_t * )& plasma , nvs_address , sizeof (plasma_settings_t ), true) != NVS_TransferResult_OK ) {
624+ plasma .port_arc_ok = plasma .port_cutter_down = plasma .port_cutter_up = 255 ;
591625 plasma_settings_restore ();
626+ }
592627
593628 port_arc_voltage = plasma .port_arc_voltage ;
594629 port_arc_ok = plasma .port_arc_ok ;
@@ -599,16 +634,16 @@ static void plasma_settings_load (void)
599634 if (n_ain >= 1 ) {
600635 if (port_arc_voltage == 255 )
601636 plasma .port_arc_voltage = port_arc_voltage = 0 ;
602- ok = ioport_claim (Port_Analog , Port_Input , & port_arc_voltage , "Arc voltage" );
637+ init_ok = ioport_claim (Port_Analog , Port_Input , & port_arc_voltage , "Arc voltage" );
603638 }
604- ok = ok && ioport_claim (Port_Digital , Port_Input , & port_arc_ok , "Arc ok" );
605- if (ok && n_din > 1 ) {
606- ok = ioport_claim (Port_Digital , Port_Input , & port_cutter_down , "Cutter down" );
607- ok = ok && ioport_claim (Port_Digital , Port_Input , & port_cutter_up , "Cutter up" );
639+ init_ok = init_ok && ioport_claim (Port_Digital , Port_Input , & port_arc_ok , "Arc ok" );
640+ if (init_ok && n_din > 2 && port_cutter_down != 255 ) {
641+ init_ok = ioport_claim (Port_Digital , Port_Input , & port_cutter_down , "Cutter down" );
642+ init_ok = init_ok && ioport_claim (Port_Digital , Port_Input , & port_cutter_up , "Cutter up" );
608643 }
609644 }
610645
611- if (ok ) {
646+ if (init_ok ) {
612647
613648 if (n_ain == 0 && plasma .mode == Plasma_ModeVoltage )
614649 plasma .mode = Plasma_ModeUpDown ;
@@ -627,10 +662,8 @@ static void plasma_settings_load (void)
627662 settings_changed = hal .settings_changed ;
628663 hal .settings_changed = plasma_setup ;
629664
630- } else {
631- n_ain = n_din = 0 ;
665+ } else
632666 protocol_enqueue_foreground_task (report_warning , "Plasma mode failed to initialize!" );
633- }
634667}
635668
636669static void on_settings_changed (settings_t * settings , settings_changed_flags_t changed )
@@ -692,7 +725,7 @@ static void plasma_report_options (bool newopt)
692725 on_report_options (newopt );
693726
694727 if (!newopt )
695- hal .stream .write ("[PLUGIN:PLASMA v0.12 ]" ASCII_EOL );
728+ hal .stream .write ("[PLUGIN:PLASMA v0.13 ]" ASCII_EOL );
696729 else if (driver_reset ) // non-null when successfully enabled
697730 hal .stream .write (",THC" );
698731}
0 commit comments