Go Down

Topic: Help Understanding Code: processInput (Read 457 times) previous topic - next topic

mech_eng

Code: [Select]
void processNumber (int x)
{

  switch (whichNumber)
  {

  case CASE:
    Casev = x;
    whichNumber = Speed;
    break;

  case Speed:
    Speedv = x;
    whichNumber = CASE;
    break;


  }



//-------------------------------------------------------------------

void processInput ()
{
  static int receivedNumber = 0;
  static boolean False = false;
  byte c = Serial.read ();   

  switch (c)
  {

  case EOP: 
    if (False)
      processNumber (- receivedNumber);
    else
      processNumber (receivedNumber);

    // fall through to start a new number
  case SOP:
    receivedNumber = 0;
    False = false;
    break;

  case '0' ... '9':
    receivedNumber *= 10;           // receivedNumber = recievedNumber*10 increments decimal places
    receivedNumber += c -'0';      //receivedNumber = recievedNumber + (c - '0') goes from char to decimal         
    break;

  case '-':           // What is this for?
    False = true;
    break;

  }
}


I have enough grasp on this code to use it but it bugs me that I do not fully understand it.
Is processInput a special function in the Arduino language?
What is the case '-' for? Does this just keep the received number positive?

robtillaart

this code has definitely style issues

False is the var that shows if a number is negative
CASE is also a bad name as it is a reserved word in C/C++

EOP is a EndOfPacket
SOP is a StartOfPacket

processInput reads an char from serial and builds up a number. If end of packet is received the number is processed.

And what that does is a mystery if we cannot see all the code.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

johncc

I remember seeing this code when you posted it before because the variable name just cracks me up :)


Code: [Select]
void processNumber (int x)
{

  switch (whichNumber)
  {

  case CASE:
    Casev = x;
    whichNumber = Speed;
    break;

  case Speed:
    Speedv = x;
    whichNumber = CASE;
    break;


  }



//-------------------------------------------------------------------

void processInput ()
{
  static int receivedNumber = 0;
  static boolean False = false;
  byte c = Serial.read ();   

  switch (c)
  {

  case EOP: 
    if (False)
      processNumber (- receivedNumber);
    else
      processNumber (receivedNumber);

    // fall through to start a new number
  case SOP:
    receivedNumber = 0;
    False = false;
    break;

  case '0' ... '9':
    receivedNumber *= 10;           // receivedNumber = recievedNumber*10 increments decimal places
    receivedNumber += c -'0';      //receivedNumber = recievedNumber + (c - '0') goes from char to decimal         
    break;

  case '-':           // What is this for?
    False = true;
    break;

  }
}


I have enough grasp on this code to use it but it bugs me that I do not fully understand it.
Is processInput a special function in the Arduino language?
What is the case '-' for? Does this just keep the received number positive?

processInput is not "Arduino language", it is defined right there in your code.
It examines the current character coming in, and if it is a '-', it takes note that the number that follows should be a negative number.  If the variable were named "isNegativeNumber", it would be clearer.

seealso http://arduino.cc/forum/index.php/topic,145379.msg1092845.html#msg1092845

PeterH


Is processInput a special function in the Arduino language?


No, this is just a piece of code that somebody has written.


What is the case '-' for? Does this just keep the received number positive?


The case '-' is executed when the byte returned by Serial.read() contains the ascii character '-' (a minus sign).

Google for "C++ switch case".
I only provide help via the forum - please do not contact me for private consultancy.

mech_eng

thanks all, cleared some things up for me

Go Up