trying to understand the gammon's Serial State machine code

i am trying to understand the nick gammon's Serial State machine code. http://www.gammon.com.au/forum/?id=11425 the code is

// Example state machine reading serial input
// Author: Nick Gammon
// Date: 17 December 2011

// the possible states of the state-machine
typedef enum {  NONE, GOT_R, GOT_S, GOT_G } states;

// current state-machine state
states state = NONE;
// current partial number
unsigned int currentValue;

void setup ()
{
  Serial.begin (115200);
  state = NONE;
}  // end of setup

void processRPM (const unsigned int value)
{
  // do something with RPM 
  Serial.print ("RPM = ");
  Serial.println (value);
} // end of processRPM

void processSpeed (const unsigned int value)
{
  // do something with speed 
  Serial.print ("Speed = ");
  Serial.println (value);
} // end of processSpeed

void processGear (const unsigned int value)
{
  // do something with gear 
  Serial.print ("Gear = ");
  Serial.println (value);  
} // end of processGear

void handlePreviousState ()
{
  switch (state)
  {
  case GOT_R:
    processRPM (currentValue);
    break;
  case GOT_S:
    processSpeed (currentValue);
    break;
  case GOT_G:
    processGear (currentValue);
    break;
  }  // end of switch  

  currentValue = 0; 
}  // end of handlePreviousState

void processIncomingByte (const byte c)
{
  if (isdigit (c))
  {
    currentValue *= 10;
    currentValue += c - '0';
  }  // end of digit
  else 
  {

    // The end of the number signals a state change
    handlePreviousState ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'R':
      state = GOT_R;
      break;
    case 'S':
      state = GOT_S;
      break;
    case 'G':
      state = GOT_G;
      break;
    default:
      state = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit  
  
} // end of processIncomingByte

void loop ()
{
  while (Serial.available ())
    processIncomingByte (Serial.read ());

  // do other stuff in loop as required
  
}  // end of loop

as a newbie.. i am having hard time understanding it. can someone explain / simplify the following

typedef enum { NONE, GOT_R, GOT_S, GOT_G } states;

does enum is used to define states?

what's the use of typedef and states?

 processRPM (currentValue);
    break;
  case GOT_S:
    processSpeed (currentValue);
    break;
  case GOT_G:
    processGear (currentValue);
    break;
  }

what is the currentValue passing to the function?

i barely understand the flow but dont understand how it is getting the data into variable Value. can Someone explain it..

typedef enum {  NONE, GOT_R, GOT_S, GOT_G } states; 
states state;

these two lines essentially allow you to create a new variable state where the only four possible values for state are in the braces.

try compiling this:

typedef enum {  NONE, GOT_R, GOT_S, GOT_G } states; 

states mystate;

void setup()
{
  mystate = GOT_R;
}

void loop()
{
  
}

and then try compiling this:

typedef enum {  NONE, GOT_R, GOT_S, GOT_G } states; 

states mystate;

void setup()
{
  mystate = 0;
}

void loop()
{
  
}

you can see that the compiler allows you to only assign the set of values in the braces to variables of type states

currentValue is built with this code

void processIncomingByte (const byte c)
{
  if (isdigit (c))
  {
    currentValue *= 10;
    currentValue += c - '0';
  } 
  ...

That means: if the received character is a digit, then multiply currentValue by 10 and add the character's digit value to it (In ASCII, '0' == 48, '1' == 49, etc, that's why '0' is substracted from the result)

For example, you sent "234"

  • currentValue = 0
  • First character is '2', so currentValue = (currentValue * 10) + ( '2' - '0' ) = 2
  • Second character is '3' so currentValue = (currentValue * 10) + ( '3' - '0' ) = 23
  • Third character is '4' so currentValue = (currentValue * 10) + ( '4' - '0' ) = 234

Thank you ....

anyone who came to this searching i try to write something I understand(not much) in this post....https://anilarduino.wordpress.com/2014/11/29/parsing-commands-nick-gammons-state-machine/

I hope It may be helpful..