Embedded Toolbox: Programmer's Calculator
Like any craftsman, I have accumulated quite a few tools during my embedded software development career. Some of them proved to me more useful than others. And these generally useful tools ended up in my Embedded Toolbox. In this blog, I'd like to share some of my tools with you. Today, I'd like to start with my cross-platform Programmer's Calculator called QCalc.
I'm sure that you already have your favorite calculator online or on your smartphone. But can your calculator accept complete expressions in the C-syntax, which you can cut-and-paste directly to and from your embedded code? How many buttons do you need to push to see your result in decimal, hex and binary? Well, QCalc can do this with less hassle than anything else I've seen out there. I begin with describing QCalc features and then I tell you how to download and launch it.
Expressions in C-Syntax
The most important feature of QCalc is that it accepts expressions in the C-syntax – with the same operands and precedence rules as in the C or C++ source code. Among others, the expressions can contain all bit-wise operators (<<, >>, |, &, ^, ~) as well as mixed decimal, hexadecimal and even binary constants. QCalc is also a powerful floating-point scientific calculator and supports all mathematical functions (sin(), cos(), tan(), exp(), ln(), ...). Some examples of acceptable expressions are:
((0xBEEF << 16) | 1280) & ~0xFF – binary operators, mixed hex and decimal numbers
($1011 << 24) | (1280 >> 8) ^ 0xFFF0 – mixed binary, dec and hex numbers
(1234 % 55) + 4321/33 – remainder, integer division
pow(sin($pi),2) + pow(cos($pi),2) – scientific floating-point calculations, pi-constant
($0111 & $GPIO_EXTIPINSELL_EXTIPINSEL0_MASK) << ($GPIO_EXTIPINSELL_EXTIPINSEL1_SHIFT * 12) - (see user-defined variables)
NOTE: QCalc internally uses the Tcl command expr to evaluate the expressions. Please refer to the documentation of the Tcl expr command for more details of supported syntax and features.
Automatic conversion to hexadecimal and binary
If the result of expression evaluation is integer (as opposed to floating point), QCalc automatically displays the result in hexadecimal and binary formats (see QCalc GUI). For better readability the hex display shows a comma between the two 16-bit half-words (e.g.,
0xDEAD,BEEF). Similarly, the binary output shows a comma between the four 8-bit bytes (e.g.,
As the extension to the C-syntax, QCalc supports binary numbers in the range from 0-15 (0b0000-0b1111). These binary constants are represented as
$1111 and can be mixed into expressions. Here are a few examples of such expressions:
($0110 << 14) & 0xDEADBEEF
($0010 | $1000) * 123
History of inputs
QCalc remembers the history of up to 8 most recently entered expressions. You can recall and navigate the history of previously entered expressions by pressing the Up / Down keys.
The $ans variable
QCalc stores the result of the last computation in the $ans variable (note the dollar sign
$ in front of the variable name). Here are some examples of expressions with the
1/$ans – find the inverse of the last computation
log($ans)/log(2) – find log-base-2 of the last computation
QCalc allows you also to define any number of your own user variables. To set a variable, you simply type the expression
=alpha in the user input field. This will define the variable
alpha and assign it the value of the last computation (
$ans). Subsequently, you can use your
alpha variable in expressions by typing $
alpha (note the dollar sign
$ in front of the variable name). Here is example of defining and using variable
0xE000E000 – set some value into
=GPIO_BASE – define user variable
GPIO_BASE and set it to
$GPIO_BASE + 0x400 – use the variable
$GPIO_BASE in an expression
Note: The names of user variables are case-sensitive.
Expressions that you enter into QCalc might have all kinds of errors: syntax errors, computation errors (e.g., division by zero), undefined variable errors, etc. In all these cases, QCalc responds with the
Error message and the explanation of the error:
Downloading and Launching QCalc
QCalc is included in the open source QTools Collection, which you cad freely download from SourceForge or GitHub. Once you install QTools, QCalc is located in the sub-directory
qtools/bin/ and consists of a single file
qcalc.tcl. To launch QCalc, you need to open this file with the wish Tk interpreter.
NOTE: The wish Tk interpreter is included in the QTools Collection for Windows and is also pre-installed in most Linux distributions.
You use QCalc by typing (or pasting) an expression in the user input field and pressing the Enter key to evaluate the expression. You can conveniently edit any expression already inside the user input field, and you can recall the previous expressions by means of the Up/Down keys. You can also resize the QCalc window to see more or less of the input field.
QCalc on Windows
The wish Tk interpreter is conveniently provided in the same
qtools/bin/ directory as the
qcalc.tcl script. The directory contains also a shortcut
qcalc, which you can copy to your desktop.
QCalc on Linux
Most Linux distributions contain the Tk interpreter, which you can use to launch QCalc. You can do this either from a terminal, by typin
wish $QTOOLS/qcalc.tcl & or by creating a shortcut to
wish with the command-line argument
Previous post by Miro Samek:
Modern Embedded Systems Programming: Beyond the RTOS
Next post by Miro Samek:
Embedded Toolbox: Windows GUI Prototyping Toolkit
Just cross-posted this blog on DSPRelated as well as I believe it could be of interest to DSP engineers. Thanks for writing this Miro and publishing it here.
Lots of comments about this article on Reddit: https://www.reddit.com/r/programming/comments/6kba...
Thank you for pointing out the Reddit comments.
As anticipated in the blog post, people mostly offer their own favorite tools. However, the challenge still stands, because none of the suggested tools allows a developer to enter complete expressions in the C-syntax and obtain automatic conversion to hex and binary with less hassle than QCalc.
I cant find the qcalc tool in the referenced locations on Github or Sourceforge, QPC_5.9.3 is what I downloaded from sourceforge. Could you check the links and update if nessecary?
Thanks for your interest.
You are apparently downloading QPC_5.9.3, whereas QCalc is included in the QTools collection. Please use the web links provided in the blog post and not the default download component offered by SourceForge. Here are the links again:
Thanks, I have got the correct package from your Sourceforge link. The Qtools link on Github dosent seem to have Qcalc though.
The calculator looks good.
Looks like a wonderful tool. Wish I could access it. Went to both links you gave PikHal and both contain the same images Neither of which contains QCalc or anything link wish in any directory.
Great tool. A must have for every embedded engineer. The UI is very minimal, yet offers maximum benefits. Thanks Miro Samek.
To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.
Registering will allow you to participate to the forums on ALL the related sites and give you access to all pdf downloads.