@ -6331,5 +6331,67 @@ Due to size constraints, and with the goal of being easier for the novice user t
force a function to not be inlined using the GCC extension void __attribute__ ((noinline)) foo() "
force a function to not be inlined using the GCC extension void __attribute__ ((noinline)) foo() "
<pre>
<pre>
<h1>The following content is the Arduino library API style guide. In places where it does not contradict the style guide above, you should follow it.</h1>
<p>We should work to integrate this content into the main style guide</p>
<h1>Arduino Style Guide for Writing Libraries</h1>
<p>This is a style guide to writing library <spanclass='wikiword'>APIs</span> in an Arduino style. Some of these run counter to professional programming practice. We’re aware of that, but it’s what’s made it possible for so many beginners to get started with Arduino easily. So please code with these principles in mind. If you have suggestions on how to make Arduino libraries clearer for that core audience, please jump in the discussion. This is a work in progress.
</p>
<p><strong>Be kind to the end user.</strong>
Assume you are writing an API for an intelligent person who has not programmed before. Come up with a clear mental model of the concept you’re working with, and the terms and functions you will use.
</p>
<p><strong>Match your API to the underlying capabilities.</strong> You don’t want to expose implementation details to the user but you also don’t want an API that suggests an inaccurate mental model of the possibilities. For example, if there are only a few possible options for a particular setting, don’t use a function that takes an int, as it implies you can use any value you want.
</p>
<p><strong>Organize your public functions around the data and functionality that the user wants.</strong> Quite often, the command set for a particular electronic module is overly complicated for the most common uses, or can be re-organized around higher level functionality. Think about what the average person thinks the thing does, and try to organise your API functions around that. Adafruit's <aclass='urllink'href='https://github.com/adafruit/Adafruit-BMP085-Library'rel='nofollow'>BMP085 library</a> is a good example. The readPressure() command performs all the necessary steps to get the final pressure. The library wraps this commonly executed series of functions into a high-level single command which returns the value the user's looking for in a format she expects. It abstracts away not only the low-level <spanclass='wikiword'>I2C</span> commands, but also the mid-level temperature and pressure calculations, while still offering those mid-level functions as public functions for those who want them.
</p>
<p><strong>Use full, everyday words.</strong> Don’t be terse with your function names or variables. Use everyday terms instead of technical ones. Pick terms that correspond to popular perception of the concept at hand. Don’t assume specialized knowledge. For example, this is why we used analogWrite() rather than pwm(). Abbreviations are acceptable, though, if they’re in common use or are the primary name for something.For example, “HTML” is relatively common and “SPI” is effectively the name of that protocol (“serial-peripheral interface” is probably too long). (“Wire” was probably a mistake, as the protocol it uses is typically called “TWI” or “<spanclass='wikiword'>I2C</span>”.)
</p>
<p><strong>Avoid words that have different meanings to the general public.</strong> For example, to programmers, an error is a notification that something happened. To the general public, errors are bad things.
</p>
<p><strong>When you have to use a domain-specific term, write a sentence or two describing it to the general public FIRST.</strong> You’ll likely come across a better term, and if not, you’ll have started the documentation on your library.
</p>
<p><strong>Document and comment as you go.</strong> When writing examples and documentation, follow this style guide: <aclass='urllink'href='http://arduino.cc/en/Reference/StyleGuide'rel='nofollow'>http://arduino.cc/en/Reference/StyleGuide</a>
</p>
<p><strong>Use the established core libraries and styles.</strong>
</p><ul><li>Use read() to read inputs, and write() to write to outputs, e.g. digitalRead(), analogWrite(), etc.
</li><li>Use the Stream.h and Print.h libraries when dealing with byte streams. If it’s not appropriate, at least try to use its API as a model. For more on this, see below
</li><li>For network applications, use the Client and Server libraries as the basis.
</li><li>Use begin() to initialize a library instance, usually with some settings. Use end() to stop it.
</li></ul><p><strong>Use camel case function names, not underscore.</strong> For example, analogRead, not analog_read. Or myNewFunction, not my_new_function. We've adopted this from Processing.org for readability's sake.
</p>
<p><strong>LONG_CONSTANT_NAMES_FULL_OF_CAPS are hard to read.</strong> Try to simplify when possible, without being terse.
</p>
<p><strong>Try to avoid boolean arguments.</strong> Instead, consider providing two different functions with names the describe the differences between them.
</p>
<p><strong>Don’t assume knowledge of pointers.</strong> Beginning users of C find this the biggest roadblock, and get very confused by & and *, so whenever you can avoid having them hanging out in the API, do so. One way is to pass by reference using array notation rather than * notation, for example.
</p>
<pre> void printArray( char* array);
</pre>
<p>can be replaced by
</p>
<pre> void printArray(char[] array);
</pre>
<p>Though there are some libraries where we pass pointers by using structures like const chars, avoid anything that requires the user to pass them. For example,rather than:
</p>
<pre> foo.readAccel(&x, &y, &z);
</pre>
<p>use something like this:
</p>
<pre> xAxis = adxl.readX();
yAxis = adxl.readY();
zAxis = adxl.readZ();
</pre>
<p>When using serial communication, allow the user to specify any Stream object, rather than hard-coding "Serial". This will make your library compatible all serial ports on Mega and the Due, and can also use alternate interfaces like <spanclass='wikiword'>SoftwareSerial</span>. The Stream object can be passed to your library's constructor or to a begin() function (as a reference, not a pointer). See <aclass='urllink'href='http://www.firmata.org/wiki/Main_Page'rel='nofollow'>Firmata 2.3</a> or <aclass='urllink'href='https://code.google.com/p/xbee-arduino/'rel='nofollow'>XBee 0.4</a> for examples of each approach.
</p>
<p>When writing a library that provides byte-stream communication, inherit Arduino's Stream class, so your library can be used with all other libraries that accept Stream objects. If possible, buffer incoming data, so that read() immediately accesses data the buffer but does not wait for more data to arrive. If possible, your write() method should store data to a transmit buffer, but write() must wait if the buffer does not have enough space to immediately store all outgoing data. The yield() function should be called while waiting.
</p>
<p>Here are a few libraries that are exemplary from Adafruit. She breaks the functions of the devices down into their high-level activities really well. <aclass='urllink'href='https://github.com/adafruit/Adafruit-BMP085-Library'rel='nofollow'>https://github.com/adafruit/Adafruit-BMP085-Library</a><aclass='urllink'href='https://github.com/adafruit/DHT-sensor-library'rel='nofollow'>https://github.com/adafruit/DHT-sensor-library </a>
</p>
<p>This does a nice job of abstracting from the Wire (<spanclass='wikiword'>I2C</span>) library: