-
-
Notifications
You must be signed in to change notification settings - Fork 442
Style Guide Tutorials #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 26 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
b6345fa
adding and moving around style guides
jhansson-ard a2f1b82
formatted library creating guide
jhansson-ard 6b79f70
updated tags
jhansson-ard 17bc1d6
renamed folder and updated frontmatter
jhansson-ard a3331b4
renamed folder
jhansson-ard 746a4fb
moved style content
jhansson-ard ac77c2e
moved folders..again
jhansson-ard f70dd51
adding title to category
jhansson-ard eca5c31
adding image guide iamges
jhansson-ard 513f892
added images and how to take screenshots
jhansson-ard 507c076
updating hyperlinks to pages
jhansson-ard bce3541
reordering content and adding contributing page
jhansson-ard d5d0747
removed image style guide as it is not yet done
jhansson-ard 2cf1257
Update content/learn/08.contributions/04.arduino-creating-content-gui…
jhansson-ard 1436500
Update content/learn/08.contributions/04.arduino-creating-content-gui…
jhansson-ard 0c9d59d
Update content/learn/08.contributions/00.arduino-writing-style-guide/…
jhansson-ard c4518b8
Update content/learn/08.contributions/04.arduino-creating-content-gui…
jhansson-ard 606f9a0
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard 6d5de4f
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard 189a883
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard 9240e81
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard 429856e
Update content/learn/08.contributions/03.arduino-creating-library-gui…
jhansson-ard 2b136e0
Update content/learn/08.contributions/03.arduino-creating-library-gui…
jhansson-ard f5eed32
Update content/learn/08.contributions/04.arduino-creating-content-gui…
jhansson-ard f77a1f2
Update content/learn/08.contributions/03.arduino-creating-library-gui…
jhansson-ard 305b57f
Update content/learn/08.contributions/00.arduino-writing-style-guide/…
jhansson-ard 259888c
Update content/learn/08.contributions/04.arduino-creating-content-gui…
jhansson-ard e6aeace
Update content/learn/08.contributions/00.arduino-writing-style-guide/…
jhansson-ard d43e724
Update content/learn/08.contributions/04.arduino-creating-content-gui…
jhansson-ard 240c396
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard 0974342
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard f285408
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard a297aa5
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard a9a1255
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard a4c39ff
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard 2a43c12
Update content/learn/08.contributions/03.arduino-creating-library-gui…
jhansson-ard 6fa509f
Update content/learn/08.contributions/03.arduino-creating-library-gui…
jhansson-ard 90311c7
Update content/learn/08.contributions/03.arduino-creating-library-gui…
jhansson-ard 80dfeaf
Update content/learn/08.contributions/01.arduino-library-style-guide/…
jhansson-ard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 5 additions & 3 deletions
8
...re/ArduinoStyleGuide/ArduinoStyleGuide.md → ...tyle-guide/arduino-writing-style-guide.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.../08.contributions/01.arduino-library-style-guide/arduino-library-style-guide.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: 'Arduino Style Guide for Creating Libraries' | ||
description: 'Learn how to write library APIs in an Arduino style.' | ||
tags: | ||
- Styleguide | ||
- Guidelines | ||
--- | ||
|
||
This is a style guide to writing library APIs 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. | ||
|
||
**Be kind to the end user.** 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. | ||
|
||
**Match your API to the underlying capabilities.** 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. | ||
|
||
**Organize your public functions around the data and functionality that the user wants.** 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 [BMP085 library](https://github.com/adafruit/Adafruit-BMP085-Library) is a good example. The `readPressure()` function 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 I2C 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. | ||
|
||
**Use full, everyday words.** 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 “I2C”.) | ||
|
||
**Avoid words that have different meanings to the general public.** For example, to programmers, an error is a notification that something happened. To the general public, errors are bad things. | ||
|
||
**When you have to use a domain-specific term, write a sentence or two describing it to the general public FIRST.** You’ll likely come across a better term, and if not, you’ll have started the documentation on your library. | ||
|
||
**Document and comment as you go.** When writing examples and documentation, follow the [Writing Style Guide](/learn/contributions/arduino-writing-style-guide) | ||
|
||
**Use the established core libraries and styles.** | ||
|
||
* Use `read()` to read inputs, and `write()` to write to outputs, e.g. `digitalRead()`, `analogWrite()`, etc. | ||
* 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 | ||
* For network applications, use the Client and Server libraries as the basis. | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Use `begin()` to initialize a library instance, usually with some settings. Use `end()` to stop it. | ||
* Use camel case function names, not underscore. For example, **analogRead**, not **analog_read**. Or **myNewFunction**, not **my_new_function**. We've adopted this from Processing.org for readability's sake. | ||
|
||
**LONG_CONSTANT_NAMES_FULL_OF_CAPS are hard to read.** Try to simplify when possible, without being terse. | ||
|
||
**Try to avoid boolean arguments.** Instead, consider providing two different functions with names the describe the differences between them. | ||
|
||
**Don’t assume knowledge of pointers.** 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. | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```arduino | ||
void printArray( char* array); | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
can be replaced by | ||
|
||
```arduino | ||
void printArray(char[] array); | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
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: | ||
|
||
```arduino | ||
foo.readAccel(&x, &y, &z); | ||
``` | ||
|
||
use something like this: | ||
|
||
```arduino | ||
xAxis = adxl.readX(); | ||
yAxis = adxl.readY(); | ||
zAxis = adxl.readZ(); | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
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 SoftwareSerial. The Stream object can be passed to your library's constructor or to a `begin()` function (as a reference, not a pointer). See [Firmata 2.3](http://www.firmata.org/wiki/Main_Page) or [XBee 0.4](https://code.google.com/p/xbee-arduino/) for examples of each approach. | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
jhansson-ard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
|
||
* https://github.com/adafruit/Adafruit-BMP085-Library | ||
* https://github.com/adafruit/DHT-sensor-library | ||
|
||
This does a nice job of abstracting from the Wire (I2C) library: https://github.com/adafruit/RTClib | ||
|
||
The text of the Arduino reference is licensed under a [Creative Commons Attribution-ShareAlike 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/). Code samples in the reference are released into the public domain. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.