# U8 smartwatch - Calculator App - Proof of Concept

**You can also be interested in:**

This is a proof of concept of how we can develop a simple calculator app (basic operations over two numbers) for the U8 smartwatch taking advantage from the **tasker+notification** method described in my prevoius post. Read the "How to get the best of your U8 smartwatch" entry before going on if you've not done it yet.

## Your Smartwatch Loves Tasker!

Since the whole post series about U8 smartwatch and tasker had a good following, I decided to write an entire e-book about the argument. In such book you'll find all the examples discussed here, plus something more. **All the concepts and examples are discussed more comprehensively (with guided steps and screenshots), and Tasker is presented in a better way, allowing also the most unexperienced users to benfit of the idea behind this post**.

**Special introductory price: 3.79 €**

## How can we build a calculator?

Well, the only possibility to work with numbers with the U8 smartwatch is to use the dialer. Here we miss some things obviously, for example the operation symbols! But don't break down, we can do something about it. My approach was the following:

- The called number shoud have the information of the operation type and the two numbers over which the operation is made
**I will code the operation type into a number**:

1: sum

2: difference

3: multiplication

4: division**I will use an informative schema**, which will be the first part of my final string of numbers, separated from the rest by an**asterisk**(which is present in the dialer)**In the informative schema I will describe the operation type****I will separate the two numbers with an hash**

Given the above rules the following operations are coded this way:

Operation | Dialed Number |
---|---|

345 * 4 | 3*345#4 |

255 / 9 | 4*255#9 |

## The tasker's task

Here comes the descriptive task code:

A1: If [ %CONUM ~R ^[1234]\* ] A2: End Call A3: JavaScriptlet [ Code:var oper, res; var num = global("CONUM"); var rexp = new RegExp("([1234])\\*([0-9]*?)#([0-9]*)"); var m = num.match(rexp); var fi = parseInt(m[2], 10); var si = parseInt(m[3], 10); if(m[1]=='1') { oper = m[2] + "+" + m[3]; res = fi + si; } else if (m[1]=='2') { oper = m[2] + "-" + m[3]; res = fi - si; } else if (m[1]=='3') { oper = m[2] + "*" + m[3]; res = fi * si; } else if (m[1]=='4') { oper = m[2] + "/" + m[3]; res = fi / si; } else { var oper = 'operation result'; var res = 'error'; } Libraries: Auto Exit:On Timeout (Seconds):45 ] A4: Notify [ Title:calculator Text:%oper = %res Icon:null Number:0 Permanent:Off Priority:3 ] A5: End If

Let's describe it a bit:

- in A1 we match called numbers starting with a number between 1 and 4 and followed by an asterisk
- in A2 we end the call. I must say that in this case, with a called number including "special chars" the call is not ended by tasker and I don't know why. I end it manually.
- in A3 we execute a js code which parses the called number, gets the operation type and the operation numbers and constructs two variables: the operation string and the operation result
- in A4 we notify to the watch the operation result

That's it, as I told you it is only a proof of concept, but the code could be extended to consider decimal numbers, parenthesis, or even more complex operations. In some cases it would be enough to represent different math symbols as sequences of special chars, in others you may consider the idea of using the first part of the called number as an informative schema for the symbols used in the operation, for example:

14#9*955#39#2

could be interpreted as (55 + 3)/2 with these rules

14 | operations order (the first is a sum, the second a division) |

# | Divides informations in the informative schema |

9 | Represents a parenthesis |

* | End of the informative schema |

9 | Parenthesis |

55 | The number 55 |

# | The first operation (a sum) |

3 | The second number, 3. It couldn't be 39 because 9 in this expression represents a parenthesis |

9 | Closed Parenthesis |

# | The second operation (division) |

2 | A number: 2 |

You'll just need to play a bit with regular expressions :)

Hope you enjoy, and write here your observations!