Pages: [1]   Go Down
Author Topic: Help Understanding Code: processInput  (Read 449 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 228
Posts: 14059
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Rob Tillaart

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks all, cleared some things up for me
Logged

Pages: [1]   Go Up
Jump to: