.. highlight:: c++
.. _language:
==========================
Maple Language Reference
==========================
The Maple can be programmed in the `Wiring
`_ language, which is the same
language used to program the `Arduino `_ boards.
.. TODO [0.2.0?] Wiring tutorial
C or C++ programmers may wish to skip to the
:ref:`arduino_c_for_c_hackers` or the :ref:`unix-toolchain`.
.. contents:: Contents
:local:
.. admonition:: **Looking for Something Else?**
- See the :ref:`libraries` for extra built-in libraries.
- If you want to use C or C++, see the :ref:`unix-toolchain`.
- If you're looking for something from the C standard library (like
``atoi()``, for instance): the :ref:`CodeSourcery GCC compiler
` used to compile your programs is configured to link
against `newlib `_, and allows the
use of any of its header files. However, dynamic memory allocation
(``malloc()``, etc.) is not available.
- If you're looking for pointers to low-level details, see
:ref:`libmaple` and this page's :ref:`Recommended Reading
`.
.. _language-lang-docs:
Maple Language Reference
------------------------
This table is a summary of the most important language features. See
the :ref:`language-index` for a complete listing.
+--------------------------------------------+----------------------------------------------+---------------------------------------------------+
| Structure | Variables | Functions |
| | | |
+============================================+==============================================+===================================================+
|* :ref:`setup() ` |**Constants** |**Digital I/O** |
| | | |
|* :ref:`loop() ` |* :ref:`HIGH ` | |* :ref:`pinMode() ` |
| | :ref:`LOW ` | |
| | |* :ref:`digitalWrite() ` |
|**Control Structures** |* :ref:`INPUT ` | | |
| | :ref:`OUTPUT ` |* :ref:`digitalRead() ` |
|* :ref:`if/else ` | | |
| |* :ref:`true ` | |* :ref:`togglePin() ` |
|* :ref:`for ` | :ref:`false ` | |
| | |* :ref:`toggleLED() ` |
|* :ref:`switch/case ` |* :ref:`Constants | |
| | ` (:ref:`integers |* :ref:`isButtonPressed() ` |
|* :ref:`while ` | `, :ref:`floating | |
| | point `) |* :ref:`waitForButtonPress() |
|* :ref:`do...while ` | | ` |
| |* :ref:`Board-specific values | |
|* :ref:`break ` | ` |**Analog I/O** |
| | | |
|* :ref:`continue ` |**Data Types** |* :ref:`analogRead() ` |
| | | |
|* :ref:`return ` | The size of each data type, in bytes, is |* :ref:`pwmWrite() ` |
| | given in parentheses where appropriate. | (:ref:`analogWrite() ` is |
|* :ref:`goto ` | | also available, though its use is discouraged) |
| | *Note*: The ``word`` type is (deliberately) | |
| | :ref:`not supported `. | |
|**Further syntax** | |**Advanced I/O** |
| |* :ref:`void ` | |
|* :ref:`; (semicolon) ` | |* tone(): TODO |
| |* :ref:`boolean ` (1 byte) | |
|* :ref:`{} (curly braces) | |* noTone(): TODO |
| ` |* :ref:`char ` (1 byte) | |
| | |* :ref:`shiftOut() ` |
|* :ref:`// (single-line comment) |* :ref:`unsigned char | |
| ` | ` (1 byte) |* pulseIn(): TODO |
| | | |
|* :ref:`/\* \*/ (multi-line comment) |* :ref:`byte ` (1 byte) | |
| ` | |**Time** |
| |* :ref:`int ` (4 bytes) | |
|* :ref:`#define ` | |* :ref:`millis() ` |
| |* :ref:`unsigned int ` | |
|* :ref:`#include ` | (4 bytes) |* :ref:`micros() ` |
| | | |
| |* ``long`` (4 bytes), synonym for :ref:`int |* :ref:`delay() ` |
|**Arithmetic Operators** | ` | |
| | |* :ref:`delayMicroseconds() |
|* :ref:`= ` |* ``unsigned long`` (4 bytes), synonym for | ` |
| (assignment) | :ref:`unsigned int ` | |
| | | |
|* :ref:`+ ` (addition) |* :ref:`long long ` (8 bytes) |**Math** |
| | | |
|* :ref:`- ` |* :ref:`unsigned long |* :ref:`min() ` |
| (subtraction) | long ` (8 bytes) | |
| | |* :ref:`max() ` |
|* :ref:`* ` |* :ref:`float ` (4 bytes) | |
| (multiplication) | |* :ref:`abs() ` |
| |* :ref:`double ` (8 bytes) | |
|* :ref:`/ ` (division) | |* :ref:`constrain() ` |
| |* :ref:`strings ` | |
|* :ref:`% ` (modulo) | |* :ref:`map() ` |
| |* :ref:`arrays ` | |
| | |* :ref:`pow() ` |
|**Comparison Operators** |* :ref:`enum ` | |
| | |* :ref:`sqrt() ` |
|* :ref:`== ` (equal to) |* :ref:`numeric types ` | |
| | | |
|* :ref:`\!= ` |**Conversion** |**Trigonometry** |
| (not equal to) | | |
| |* :ref:`char() ` |* :ref:`sin() ` |
|* :ref:`< ` (less than) | | |
| |* :ref:`byte() ` |* :ref:`cos() ` |
|* :ref:`> ` | | |
| (greater than) |* :ref:`int() ` |* :ref:`tan() ` |
| | | |
|* :ref:`<= ` |* :ref:`long() ` | |
| (less than or equal to) | |**Random Numbers** |
| |* :ref:`float() ` | |
|* :ref:`>= ` | |* :ref:`randomSeed() ` |
| (greater than or equal to) |* :ref:`double() ` | |
| | |* :ref:`random() ` |
| | | |
|**Boolean Operators** |**Variable Scope & Qualifiers** | |
| | |**Bits and Bytes** |
|* :ref:`&& ` (and) |* :ref:`variables `, | |
| | :ref:`scope ` |* :ref:`lowByte() ` |
|* :ref:`|| ` (or) | | |
| |* :ref:`static ` |* :ref:`highByte() ` is |
|* :ref:`\! ` (not) | | provided, though its use is discouraged. |
| |* :ref:`volatile ` | |
| | |* :ref:`bitRead() ` |
|**Pointer Operators** |* :ref:`const ` | |
| | |* :ref:`bitWrite() ` |
|* :ref:`* dereference operator | | |
| ` |**Utilities** |* :ref:`bitSet() ` |
| | | |
|* :ref:`& reference operator |* :ref:`sizeof() ` |* :ref:`bitClear() ` |
| ` | | |
| |* :ref:`ASSERT() ` |* :ref:`bit() ` |
| | | |
|**Bitwise Operators** | | |
| | |**External Interrupts** |
|* :ref:`& ` | | |
| (bitwise and) | |* :ref:`Reference Page ` |
| | | |
|* :ref:`| ` | |* :ref:`attachInterrupt() |
| (bitwise or) | | ` |
| | | |
|* :ref:`^ ` | |* :ref:`detachInterrupt() |
| (bitwise xor) | | ` |
| | | |
|* :ref:`~ ` | | |
| (bitwise not) | |**Interrupts** |
| | | |
|* :ref:`\<\< ` | |* :ref:`interrupts() ` |
| (shift left) | | |
| | |* :ref:`noInterrupts() ` |
|* :ref:`>> ` | | |
| (shift right) | | |
| | |**Communication** |
| | | |
|**Compound Operators** | |* :ref:`SerialUSB ` |
| | | |
|* :ref:`++ ` | |* :ref:`Serial ` |
| (increment) | | |
| | | |
|* :ref:`- - ` | | |
| (decrement) | | |
| | | |
|* :ref:`+= ` | | |
| (compound add) | | |
| | | |
|* :ref:`-= | | |
| ` (compound | | |
| subtract) | | |
| | | |
|* :ref:`*= | | |
| ` (compound | | |
| multiply) | | |
| | | |
|* :ref:`/= | | |
| ` (compound | | |
| divide) | | |
| | | |
|* :ref:`&= | | |
| ` (compound | | |
| bitwise and) | | |
| | | |
|* :ref:`|= | | |
| ` (compound | | |
| bitwise or) | | |
| | | |
|**Keywords** | | |
| | | |
|* :ref:`C++ Keywords ` | | |
| | | |
| | | |
+--------------------------------------------+----------------------------------------------+---------------------------------------------------+
.. _language-missing-features:
Missing Arduino Features
------------------------
.. _langage-missing-analogreference:
**analogReference()**
It is not possible to implement this function on the Maple
hardware. It will be possible on the upcoming Maple Native.
.. _language-no-word:
**word**
Readers familiar with the Arduino environment may notice that the
``word`` datatype is missing from the above table's list of data
types. We chose **not to provide** the ``word`` data type on the
Maple. If you want a 16-bit unsigned integer, use the ``uint16``
type instead.
While the Maple has 32-bit words, the word size on an Arduino
board is only 16 bits, and code that uses the ``word`` type is
likely to rely on that fact.
By not supporting ``word``, you'll get a compile error when
porting Arduino code to the Maple instead of potentially weird,
hard-to-debug runtime behavior.
If you really must have ``word``, you can include the following
``typedef`` in your program::
typedef uint16 word;
Unimplemented Arduino Features
------------------------------
The following Wiring/Arduino features are currently unimplemented on
the Maple.
- `tone() `_
- `noTone() `_
- `pulseIn() `_
- `String `_
.. _our reference page: http://leaflabs.com/docs/external-interrupts/
.. _newlib: http://sourceware.org/newlib/
.. _cpp-for-maple:
C++ for Maple
--------------
If you haven't programmed in C++, or if you just need to jog your
memory, you may want to check out our :ref:`Language Index
`. It provides some introductory coverage of
programming ideas and C++.
.. _arduino_c_for_c_hackers:
Note for C/C++ Hackers
----------------------
This is a note for programmers comfortable with C or C++ who want a
better understanding of the differences between C++ and the Wiring
language.
The good news is that the differences are relatively few; Wiring is
just a thin wrapper around C++. Some potentially better news is that
the Maple can be programmed using a :ref:`standard Unix toolchain
`, so if you'd rather stick with :command:`gcc`,
:command:`make`, and friends, you can. If you're using the Unix
toolchain and want to skip past the Wiring conveniences and get
straight to registers, you are encouraged to move on to the
:ref:`libmaple` documentation.
A *sketch* is the IDE's notion of a project; it consists of one or
more files written in the Wiring language, which is mostly the same as
C++. The major difference between the two is that in Wiring, it's not
necessary to declare global functions before they are used. That is,
the following is valid Wiring, and ``f()`` returns ``5``::
int f() {
return g();
}
int g() {
return 5;
}
All of the files in a sketch share the same (global) namespace. That
is, the behavior is as if all of a sketch's files were part of the
same translation unit, so they don't have to include one another in
order to access each other's definitions. The only other major
difference between Wiring and C++ is that Wiring doesn't support
dynamically allocated memory -- that is, ``new`` and ``delete`` won't
work. As of |today|, Maple only has 20 KB RAM, anyway, so it's
doubtful that static allocation is not what you want.
The Wiring language also does not require you to define your own
``main`` method (in fact, we currently forbid you from doing so).
Instead, you are required to define two functions, ``setup`` and
``loop``, with type signatures ::
void setup(void);
void loop(void);
Once a sketch is uploaded to a Maple and begins to run, ``setup()`` is
called once, and then ``loop()`` is called repeatedly. The IDE
compilation process proceeds via a source-to-source translation from
the files in a sketch to C++.
This translation process first concatenates the sketch files, then
parses the result to produce a list of all functions defined in the
global scope. (We borrow this stage from the Arduino IDE, which in
turn borrows it from Wiring. It uses regular expressions to parse
C++, which is, of course, `Bad and Wrong
`_. In the
future, we'll do this correctly, using a better parser. Until then,
you have our apologies.) The order in which the individual sketch
files are concatenated is not defined; it is unwise to write code that
depends on a particular ordering.
The concatenated sketch files are then appended onto a file which
includes `WProgram.h
`_
(which includes the wirish and libmaple libraries, and declares
``setup()`` and ``loop()``), and then provides declarations for all
the function definitions found in the previous step. At this point,
we have a file that is a valid C++ translation unit, but lacks
``main()``. The final step of compilation provides ``main()``, which
behaves roughly like::
int main(void) {
// Call libmaple's built-in initialization routines
init();
// Perform the user's initialization
setup();
// Call user loop() forever.
while (true) {
loop();
}
}
(The truth is a little bit more complicated, but not by much).
As an example, consider a sketch with two files. The first file
contains ``setup()`` and ``loop()``::
int the_pin;
void setup() {
the_pin = choose_a_pin();
pinMode(the_pin, OUTPUT);
}
void loop() {
togglePin(the_pin);
}
The second file contains the (not very useful) implementation for
``choose_a_pin()``::
int choose_a_pin() {
return random(5, 15);
}
Then the results of the concatenation process might be ::
int the_pin;
void setup() {
the_pin = choose_a_pin();
pinMode(the_pin, OUTPUT);
}
void loop() {
togglePin(the_pin);
}
int choose_a_pin(void);
int choose_a_pin() {
return random(5, 15);
}
Which could plausibly be turned into the final source file ::
#include "WProgram.h"
void setup(void);
void loop(void);
int choose_a_pin(void);
int the_pin;
void setup() {
the_pin = choose_a_pin();
pinMode(the_pin, OUTPUT);
}
void loop() {
togglePin(the_pin);
}
int choose_a_pin(void);
int choose_a_pin() {
return random(5, 15);
}
int main() {
init();
setup();
while (true) loop();
}
.. _language-recommended-reading:
Recommended Reading
-------------------
* :ref:`libmaple Documentation `
* Your board's :ref:`Board Hardware Documentation ` page
* ST Documentation:
* Reference Manual `RM0008
`_
(PDF). This is the most important reference work on the STM32
line, and covers the low-level hardware capabilities and
interfaces in great detail.
* `Programming Manual
`_
(PDF). This is an assembly language and register reference for
the STM32 line.
* ARM Documentation:
* `Cortex-M3 Technical Reference Manual, Revision r1p1
`_
(PDF). This ARM manual specifies much of the the Cortex M3
Architecture, including instruction timings.
* `newlib Documentation `_