## Tuesday, February 17, 2015

### Arduino Retro Computer: Calculator

The most basic function of a computer is to be... a calculator. (For an interesting story, learn about the history of VisiCalc and the Apple 2.)

I have to do calculations for work frequently throughout the day, and I usually have several Windows Calculators up at one time. This retro computer can be perfect for that role.

For my initial implementation, I'm supporting the basic math operators: +, -, *, and /. The user will be able to set the initial value and/or perform those math operations. Each screen of the OS will keep track of its last calculated math operation. That way users can continuously operate on running results. (And if they switch to a different screen, they can have a different running calculation going.)

The following code is a follow-up to the previous post "Arduino Retro Computer: Command Input". I highly recommend reading that post to understand this one better.

The basic premise is if the command string parsed by submitCommand() starts with the character "=", then the command string gets sent to commandMath() for processing.

The following commands are available:
=###   (Set the previousMathResult to the given ###.)
=+###   (Increment the previousMathResult by the given ###.)
=-###   (Decrement the previousMathResult by the given ###.)
=*### (Multiply the previousMathResult by the given ###.)
=/### (Divide the previousMathResult by the given ###.)
=###+### (Add two numbers, store the result in previousMathResult.)
=###-### (Subtract two numbers, store the result in previousMathResult.)
=###*### (Multiply two numbers, store the result in previousMathResult.)
=###/### (Divide two numbers, store the result in previousMathResult.)

Math commands can also be compounded:
=###+###-### (Perform addition followed by subtraction.)

### What's Bad / Future Improvements

• The calculator does not adhere to proper "Operator Precedence". http://en.wikipedia.org/wiki/Order_of_operations Calculations are always performed from the left to the right, regardless of the operation. (And no ability to use parenthesis to denote the order.)
• More advanced functions such as sin/cos/tan, power, and sqrt aren't (yet) supported.
• The calculator does not (yet) report hex and binary values. This is important to my work so I'll eventually get that added.
• Doing =-### subtracts the previous result by ### rather than sets the current value to -###. (It thinks the user wants to perform a subtraction operation rather than set a negative value.)

(I have a strong feeling a "Calculator Part 2" will be posted in the near future.)

### Screen Shot

At the start you can see I set the previousMathResult to 5. I then perform several additions on the running value.

I am also able to perform a calculation on 2 numbers regardless of the last result. (2+2=4)

### The Code

class ScreenModel
{
public:
..
double previousMathResult;

bool init(byte newIndex)
{
..
previousMathResult = 0.0;
..
}

bool submitCommand()
{
..
// Command Formatted is what the user entered with a null terminator.
// The commandFormatted is incremented by one when it is passed into
// commandMath so it doesn't have to reparse the leading "=".
else if (strncmp(commandFormatted, "=", 1) == 0)
{
commandMath(commandFormatted+1);
}
..
}

double commandMath(char *commandString)
{
// Determine the starting number and store in the result variable.
// If an operator, we'll begin with the result of the previous math calculation.
double result;
switch (*commandString)
{
case 0:
case '+':
case '-':
case '*':
case '/':
// First character is terminator or operator.
result = previousMathResult;
break;
default:
// to the end of the number / location of the operator.
result = strtod(commandString, &commandString);
break;
}

// Ensure the operator and the next digit are not a terminator.
while ((*commandString != 0) && (*(commandString+1) != 0))
{
// Next character should be the operatot.
char *operatorChar = commandString;
// Increment the commandString pointer passed the operator and read the number;
double number = strtod(++commandString, &commandString);
switch (*operatorChar)
{
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
// Divide by 0 protection.
if (number != 0.0)
{
result /= number;
}
break;
default:
// Invalid operator - possibly a space. Continue processing to the next
// character. Even if we have multiple spaces between the numbers and the
// operators, the while loop will eventually increment the commandString
// pointer to the next valid operation.
break;
}
}

// Completed processing the string. Format the result and display!
char mathString[sizeOfOutputColumnArray];
// Can't use snprintf - Arduino does not support floats in sprintf.
//snprintf(mathString, sizeOfOutputColumnArray, "%lf", result);
dtostrf(result, sizeOfOutputColumnArray-1, 8, mathString);

previousMathResult = result;

return result;
}

};