@@ -571,6 +571,39 @@ static status_code_t read_operation_unary (char *line, uint_fast8_t *pos, ngc_un
571571 return status ;
572572}
573573
574+ /*! \brief Reads the name of a parameter out of the line
575+ starting at the index given by the pos offset.
576+
577+ \param line pointer to RS274/NGC code (block).
578+ \param pos offset into line where expression starts.
579+ \param buffer pointer to a character buffer for the name.
580+ \returns #Status_OK enum value if processed without error, appropriate \ref status_code_t enum value if not.
581+ */
582+ status_code_t ngc_read_name (char * line , uint_fast8_t * pos , char * buffer )
583+ {
584+ char * s ;
585+ uint_fast8_t len = 0 ;
586+ status_code_t status = Status_BadNumberFormat ;
587+
588+ if (* (s = line + (* pos )++ ) == '<' ) {
589+
590+ s ++ ;
591+
592+ while (* s && * s != '>' && len <= NGC_MAX_PARAM_LENGTH ) {
593+ * buffer ++ = * s ++ ;
594+ (* pos )++ ;
595+ len ++ ;
596+ }
597+
598+ if ((status = * s == '>' ? Status_OK : Status_FlowControlSyntaxError ) == Status_OK ) {
599+ * buffer = '\0' ;
600+ (* pos )++ ;
601+ }
602+ }
603+
604+ return status ;
605+ }
606+
574607/*! \brief Reads the value out of a parameter of the line, starting at the
575608index given by the pos offset.
576609
@@ -593,6 +626,7 @@ sequentially, the value of #2 would be 10 after the line was executed.
593626*/
594627status_code_t ngc_read_parameter (char * line , uint_fast8_t * pos , float * value , bool check )
595628{
629+ int32_t param ;
596630 status_code_t status = Status_BadNumberFormat ;
597631
598632 if (* (line + * pos ) == '#' ) {
@@ -601,34 +635,18 @@ status_code_t ngc_read_parameter (char *line, uint_fast8_t *pos, float *value, b
601635
602636 if (* (line + * pos ) == '<' ) {
603637
604- (* pos )++ ;
605- char * param = line + * pos , * arg = line + * pos ;
606-
607- while (* arg && * arg != '>' )
608- arg ++ ;
638+ char name [NGC_MAX_PARAM_LENGTH + 1 ];
609639
610- * pos += arg - param + 1 ;
611-
612- if (* arg == '>' ) {
613- * arg = '\0' ;
614- if (ngc_named_param_get (param , value ))
615- status = Status_OK ;
616- * arg = '>' ;
640+ if ((status = ngc_read_name (line , pos , name )) == Status_OK ) {
641+ if (!ngc_named_param_get (name , value ))
642+ status = Status_BadNumberFormat ;
617643 }
644+ } else if ((status = ngc_read_integer_value (line , pos , & param )) == Status_OK ) {
618645
619- } else if ((status = ngc_read_real_value (line , pos , value )) == Status_OK ) {
620-
621- uint32_t param = (uint32_t )floorf (* value );
622-
623- if ((* value - (float )param ) > 0.9999f ) {
624- param = (uint32_t )ceilf (* value );
625- } else if ((* value - (float )param ) > 0.0001f )
626- status = Status_BadNumberFormat ; // not integer
627-
628- if (check && !ngc_param_exists ((ngc_param_id_t )param ))
646+ if (param < 0 || (check && !ngc_param_exists ((ngc_param_id_t )param )))
647+ status = Status_GcodeValueOutOfRange ;
648+ else if (!ngc_param_get ((ngc_param_id_t )param , value ))
629649 status = Status_GcodeValueOutOfRange ;
630- else if (ngc_param_get ((ngc_param_id_t )param , value ))
631- status = Status_OK ;
632650 }
633651 }
634652
@@ -761,6 +779,52 @@ status_code_t ngc_read_real_value (char *line, uint_fast8_t *pos, float *value)
761779 return status ;
762780}
763781
782+ /*! \brief Reads explicit unsigned (positive) integer out of the line,
783+ starting at the index given by the pos offset. It expects to find one
784+ or more digits. Any character other than a digit terminates reading
785+ the integer. Note that if the first character is a sign (+ or -),
786+ an error will be reported (since a sign is not a digit).
787+
788+ \param line pointer to RS274/NGC code (block).
789+ \param pos offset into line where expression starts.
790+ \param value pointer to integer where result is to be stored.
791+ \returns #Status_OK enum value if processed without error, appropriate \ref status_code_t enum value if not.
792+ */
793+ status_code_t ngc_read_integer_unsigned (char * line , uint_fast8_t * pos , uint32_t * value )
794+ {
795+ return line [* pos ] == '+' ? Status_GcodeCommandValueNotInteger : read_uint (line , pos , value );
796+ }
797+
798+ /*! \brief Reads an integer (positive, negative or zero) out of the line,
799+ starting at the index given by the pos offset. The value being
800+ read may be written with a decimal point or it may be an expression
801+ involving non-integers, as long as the result comes out within 0.0001
802+ of an integer.
803+
804+ This proceeds by calling read_real_value and checking that it is
805+ close to an integer, then returning the integer it is close to.
806+
807+ \param line pointer to RS274/NGC code (block).
808+ \param pos offset into line where expression starts.
809+ \param value pointer to integer where result is to be stored.
810+ \returns #Status_OK enum value if processed without error, appropriate \ref status_code_t enum value if not.
811+ */
812+ status_code_t ngc_read_integer_value (char * line , uint_fast8_t * pos , int32_t * value )
813+ {
814+ float fvalue ;
815+ status_code_t status ;
816+
817+ if ((status = ngc_read_real_value (line , pos , & fvalue )) == Status_OK ) {
818+ * value = (int32_t )floorf (fvalue );
819+ if ((fvalue - (float )* value ) > 0.9999f ) {
820+ * value = (uint32_t )ceilf (fvalue );
821+ } else if ((fvalue - (float )* value ) > 0.0001f )
822+ status = Status_GcodeCommandValueNotInteger ; // not integer
823+ }
824+
825+ return status ;
826+ }
827+
764828/*! \brief Evaluate expression and set result if successful.
765829
766830\param line pointer to RS274/NGC code (block).
0 commit comments