.. 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.
C or C++ programmers curious about the differences between the Wiring
language and C++ may wish to skip to the
:ref:`arduino_c_for_c_hackers`.
.. contents:: Contents
:local:
Unique Maple Additions
----------------------
.. _language-assert:
``ASSERT(...)``
The ``ASSERT()`` function can be very useful for basic program
debugging. The function accepts a boolean; for example::
ASSERT(state == WAIT);
zero is false and any other number is true. If the boolean is true
the assertion passes and the program continues as usual. If it is
false, the assertion fails: the program is halted, debug
information is printed to USART2, and the status LED begins to
throb in intensity (it's noticeably different from blinking). The
debug information is printed at 9600 baud and consists of the
filename and line number where the particular assertion failed.
Including assertions in a program increases the program size. When
using libmaple **from the command line only**, they can be
disabled by making the definition ::
#define DEBUG_LEVEL DEBUG_NONE
before including either wirish or libmaple. In this case, all
assertions will pass without any lost clock cycles. Note that
this will **not work in the IDE**; even with this definition,
assertions will still be enabled.
.. _language-arduino-docs:
Maple Language Reference
------------------------
+-------------------------------------------------+----------------------------------------------+---------------------------------------------------+
| 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 ` | | |
| |* :ref:`true ` | | |
|* :ref:`if...else ` | :ref:`false ` |**Analog I/O** |
| | | |
|* :ref:`for ` |* :ref:`integer constants |* :ref:`analogRead() ` |
| | ` | |
|* :ref:`switch/case ` | |* :ref:`pwmWrite() ` |
| |* :ref:`floating point constants | (:ref:`analogWrite() ` is |
|* :ref:`while ` | ` | also available, though its use is discouraged) |
| | | |
|* :ref:`do...while ` | | |
| |**Data Types** |**Advanced I/O** |
|* :ref:`break ` | | |
| | The size of each datatype, in bytes, is |* tone(): TODO |
|* :ref:`continue ` | given in parentheses where appropriate. | |
| | |* noTone(): TODO |
|* :ref:`return ` | *Note*: The ``word`` type is (deliberately) | |
| | :ref:`not supported `. |* shiftOut(): TODO |
|* :ref:`goto ` | | |
| |* :ref:`void ` |* pulseIn(): TODO |
| | | |
|**Further syntax** |* :ref:`boolean ` (1 byte) | |
| | |**Time** |
|* :ref:`; ` (semicolon) |* :ref:`char ` (1 byte) | |
| | |* :ref:`millis() ` |
|* :ref:`{} ` (curly braces) |* :ref:`unsigned char | |
| | ` (1 byte) |* :ref:`micros() ` |
|* :ref:`// ` | | |
| (single line comment) |* :ref:`byte ` (1 byte) |* :ref:`delay() ` |
| | | |
|* :ref:`/\* \*/ |* :ref:`int ` (4 bytes) |* :ref:`delayMicroseconds() |
| ` | | ` |
| (multi-line comment) |* :ref:`unsigned int ` | |
| | (4 bytes) | |
|* :ref:`#define ` | |**Math** |
| |* :ref:`long ` (8 bytes) | |
|* :ref:`#include ` | |* :ref:`min() ` |
| |* :ref:`unsigned long ` | |
| | (8 bytes) |* :ref:`max() ` |
|**Arithmetic Operators** | | |
| |* :ref:`float ` (4 bytes) |* :ref:`abs() ` |
|* :ref:`= ` | | |
| (assignment operator) |* :ref:`double ` (8 bytes) |* :ref:`constrain() ` |
| | | |
|* :ref:`+ ` (addition) |* :ref:`string ` |* :ref:`map() ` |
| | | |
|* :ref:`- ` |* :ref:`array ` |* :ref:`pow() ` |
| (subtraction) | | |
| |* Also provided: ``int8``, ``int16``, |* :ref:`sqrt() ` |
|* :ref:`* ` | ``int32``, ``int64``, and their unsigned | |
| (multiplication) | counterparts ``uint8``, ``uint16``, | |
| | ``uint32``, ``uint64``. |**Trigonometry** |
|* :ref:`/ ` (division) | | |
| | |* :ref:`sin() ` |
|* :ref:`% ` (modulo) |**Conversion** | |
| | |* :ref:`cos() ` |
| |* :ref:`char() ` | |
|**Comparison Operators** | |* :ref:`tan() ` |
| |* :ref:`byte() ` | |
|* :ref:`== ` (equal to) | | |
| |* :ref:`int() ` |**Random Numbers** |
|* :ref:`\!= ` | | |
| (not equal to) |* :ref:`long() ` |* :ref:`randomSeed() ` |
| | | |
|* :ref:`< ` (less than) |* :ref:`float() ` |* :ref:`random() ` |
| | | |
|* :ref:`> ` |* :ref:`double() ` | |
| (greater than) | |**Bits and Bytes** |
| | | |
|* :ref:`<= ` |**Variable Scope & Qualifiers** |* :ref:`lowByte() ` |
| (less than or equal to) | | |
| |* :ref:`variables `, |* :ref:`highByte() ` is |
|* :ref:`>= ` | :ref:`scope ` | provided, though its use is discouraged. |
| (greater than or equal to) | | |
| |* :ref:`static ` |* :ref:`bitRead() ` |
| | | |
|**Boolean Operators** |* :ref:`volatile ` |* :ref:`bitWrite() ` |
| | | |
|* :ref:`&& ` (and) |* :ref:`const ` |* :ref:`bitSet() ` |
| | | |
|* :ref:`|| ` (or) | |* :ref:`bitClear() ` |
| |**Utilities** | |
|* :ref:`\! ` (not) | |* :ref:`bit() ` |
| |* :ref:`sizeof() ` | |
| | (``sizeof`` operator) | |
|**Pointer Access Operators** | |**External Interrupts** |
| | | |
|* :ref:`* dereference operator | |* :ref:`Reference Page ` |
| ` | | |
| | |* :ref:`attachInterrupt() |
|* :ref:`& reference operator | | ` |
| ` | | |
| | |* :ref:`detachInterrupt() |
| | | ` |
|**Bitwise Operators** | | |
| | | |
|* :ref:`& ` | |**Interrupts** |
| (bitwise and) | | |
| | |* interrupts(): TODO |
|* :ref:`| ` | | |
| (bitwise or) | |* noInterrupts(): TODO |
| | | |
|* :ref:`^ ` | | |
| (bitwise xor) | |**Communication** |
| | | |
|* :ref:`~ ` | |* :ref:`SerialUSB ` |
| (bitwise not) | | |
| | |* :ref:`Serial ` |
|* :ref:`\<\< ` | | |
| (shift left) | |**Looking for something else?** |
| | | |
|* :ref:`>> ` | | See the :ref:`libraries` page for interfacing with|
| (shift right) | | particular types of hardware. Try the list of |
| | | `community-contributed code` . Maple links |
| | | against `newlib` and allows the use of any of its |
|**Compound Operators** | | functions; see its documentation for more details.|
| | | |
|* :ref:`++ ` | | |
| (increment) | | |
| | | |
|* :ref:`- - ` | | |
| (decrement) | | |
| | | |
|* :ref:`+= ` | | |
| (compound add) | | |
| | | |
|* :ref:`-= | | |
| ` (compound | | |
| subtract) | | |
| | | |
|* :ref:`*= | | |
| ` (compound | | |
| multiply) | | |
| | | |
|* :ref:`/= | | |
| ` (compound | | |
| divide) | | |
| | | |
|* :ref:`&= | | |
| ` (compound | | |
| bitwise and) | | |
| | | |
|* :ref:`|= | | |
| ` (compound | | |
| bitwise or) | | |
| | | |
+-------------------------------------------------+----------------------------------------------+---------------------------------------------------+
.. _language-missing-features:
Missing Arduino Features
------------------------
Stub TODO: fill in other missing features
**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;
.. _our reference page: http://leaflabs.com/docs/external-interrupts/
.. _newlib: http://sourceware.org/newlib/
.. _arduino_c_for_c_hackers:
Note for C/C++ Programmers
--------------------------
This is a note for programmers comfortable with C or C++ (although,
you C programmers should remember that `C++ is not a superset of 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.
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, it forbids 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
`_. An
upcoming rewrite of the IDE performs this preprocessing step
correctly, using a real 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 a
``main()`` method. The final step of compilation provides this
method, which behaves roughly like::
int main(void) {
setup();
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() {
static int toggle = 0;
toggle ^= 1;
digitalWrite(the_pin, toggle);
}
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() {
static int toggle = 0;
toggle ^= 1;
digitalWrite(the_pin, toggle);
}
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() {
static int toggle = 0;
toggle ^= 1;
digitalWrite(the_pin, toggle);
}
int choose_a_pin(void);
int choose_a_pin() {
return random(5, 15);
}
int main() {
setup();
while (true) loop();
}
(Recall that it's legal C++ for a function to be declared multiple
times, as long as it's defined exactly once).
Recommended Reading
-------------------
* `newlib Documentation `_
* STMicro documentation for STM32F103RB microcontroller:
* `All documents `_
* `Datasheet `_ (pdf)
* `Reference Manual `_ (pdf)
* `Programming Manual `_ (assembly language and register reference)