Skip to content

Commit 8966cbf

Browse files
committed
Added C++ readme
1 parent 5619274 commit 8966cbf

File tree

11 files changed

+341
-3
lines changed

11 files changed

+341
-3
lines changed

examples/breakout_encoder_wheel/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Encoder Wheel Breakout C++ Examples <!-- omit in toc -->
1+
# RGB Encoder Wheel Breakout Examples (C++) <!-- omit in toc -->
22

33
- [Function Examples](#function-examples)
44
- [Buttons](#buttons)

examples/breakout_encoder_wheel/buttons/buttons.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56

examples/breakout_encoder_wheel/chase_game/chase_game.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56

examples/breakout_encoder_wheel/clock/clock.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56
#include "hardware/rtc.h"

examples/breakout_encoder_wheel/colour_picker/colour_picker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56

examples/breakout_encoder_wheel/encoder/encoder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56

examples/breakout_encoder_wheel/led_rainbow/led_rainbow.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56

examples/breakout_encoder_wheel/stop_watch/stop_watch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include <string>
3+
#include "pimoroni_i2c.hpp"
34
#include "breakout_encoder_wheel.hpp"
45
#include "time.h"
56

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
# RGB Encoder Wheel Breakout (C++) <!-- omit in toc -->
2+
3+
This is the C++ library reference for the [Pimoroni RGB Encoder Wheel Breakout](https://shop.pimoroni.com/products/rgb-encoder-wheel-breakout).
4+
5+
6+
## Table of Content <!-- omit in toc -->
7+
- [Getting Started](#getting-started)
8+
- [Reading the Buttons](#reading-the-buttons)
9+
- [Reading the Encoder](#reading-the-encoder)
10+
- [Count and Angle](#count-and-angle)
11+
- [Count Delta](#count-delta)
12+
- [Step and Turn](#step-and-turn)
13+
- [Changing the Direction](#changing-the-direction)
14+
- [Resetting to Zero](#resetting-to-zero)
15+
- [LEDs](#leds)
16+
- [Setting an LED](#setting-an-led)
17+
- [RGB](#rgb)
18+
- [HSV](#hsv)
19+
- [Clear all LEDs](#clear-all-leds)
20+
- [Showing](#showing)
21+
- [GPIOs](#gpios)
22+
- [Setup](#setup)
23+
- [Mode](#mode)
24+
- [As Input or ADC](#as-input-or-adc)
25+
- [As Output](#as-output)
26+
- [As PWM](#as-pwm)
27+
- [Delayed Loading](#delayed-loading)
28+
- [Limitations](#limitations)
29+
- [Function Reference](#function-reference)
30+
- [Constants Reference](#constants-reference)
31+
- [Address Constants](#address-constants)
32+
- [Value Constants](#value-constants)
33+
- [Button Constants](#button-constants)
34+
- [GPIO Constants](#gpio-constants)
35+
- [Count Constants](#count-constants)
36+
37+
38+
## Getting Started
39+
40+
To start coding for your Encoder Wheel breakout, you will first need to create an object for accessing the I2C bus that the breakout is connected to. The easiest way to do this is via the `PimoroniI2C` class, with one of the handy pin constants from `pimoroni`, like so:
41+
42+
```c++
43+
#include "pimoroni_i2c.hpp"
44+
using namespace pimoroni;
45+
46+
I2C i2c(BOARD::BREAKOUT_GARDEN);
47+
```
48+
49+
This creates a `i2c` object that can be passed into the Encoder Wheel's class as part of its creation:
50+
```c++
51+
#include "breakout_encoder_wheel.hpp"
52+
using namespace encoderwheel;
53+
54+
BreakoutEncoderWheel wheel(&i2c);
55+
```
56+
57+
The above lines of code import the `BreakoutEncoderWheel` class and create an instance of it, called `wheel`. This will be used in the rest of the examples going forward.
58+
59+
60+
## Reading the Buttons
61+
62+
EncoderWheel has five buttons, covering up, down, left, right, and centre. These can be read using the `.pressed(button)` function, which accepts a button number between `0` and `4`. For convenience, each button can be referred to using these constants:
63+
64+
* `UP` = `0`
65+
* `DOWN` = `1`
66+
* `LEFT` = `2`
67+
* `RIGHT` = `3`
68+
* `CENTRE` = `4`
69+
70+
For example, to read the centre button you would write:
71+
72+
```c++
73+
bool centre_state = wheel.pressed(CENTRE);
74+
```
75+
76+
You can also get the number of buttons using the `NUM_BUTTONS` constant.
77+
78+
79+
## Reading the Encoder
80+
81+
Within the directional buttons of the Encoder Wheel breakout is a rotary encoder with 24 counts per revolution.
82+
83+
### Count and Angle
84+
85+
The current count can be read by calling `.count()`. It can also be read back as either the number of `.revolutions()` of the encoder, or the angle in `.degrees()` or `.radians()`.
86+
87+
Be aware that the count is stored as an integer, if it is continually increased or decreased it will eventually wrap at `+32767` and `-32768`. This will cause a jump in the returned by `.revolutions()`, `degrees()` and `.radians()`, that will need handling by your code.
88+
89+
90+
### Count Delta
91+
92+
Often you are not interested in the exact count that the encoder is at, but rather if the count has changed since the last time you checked. This change can be read by calling `.delta()` at regular intervals. The returned value can then be used with a check in code, for the value being non-zero.
93+
94+
95+
### Step and Turn
96+
97+
Sometimes it can be useful to know the encoder's position in the form of which step it is at and how many turns have occurred. The current step can be read by calling `.step()`, which returns a value from `0` to `23`, and the number of turns can be read by calling `.turn()`.
98+
99+
These functions differ from reading the `.count()` or `.revolutions()` by using separate counters, and so avoid the wrapping issue that these functions experience.
100+
101+
102+
### Changing the Direction
103+
104+
The counting direction of an encoder can be changed by calling `.direction(REVERSED_DIR)` at any time in code. The `REVERSED_DIR` constant comes from `pimoroni_common.hpp`. There is also a `NORMAL_DIR` constant, though this is the default.
105+
106+
107+
### Resetting to Zero
108+
109+
There are times where an encoder's count (and related values) need to be reset back to zero. This can be done by calling `.zero()`.
110+
111+
112+
## LEDs
113+
114+
The Encoder Wheel breakout features 24 RGB LEDs arranged in a ring around the wheel. This is the same number as there are steps on the wheel, letting you use the LEDs to show the current step of the wheel.
115+
116+
117+
### Setting an LED
118+
119+
You can set the colour of a LED on the ring in either the RGB colourspace, or HSV (Hue, Saturation, Value). HSV is useful for creating rainbow patterns.
120+
121+
#### RGB
122+
123+
Set the first LED - `0` - to Purple `255, 0, 255`:
124+
125+
```c++
126+
wheel.set_rgb(0, 255, 0, 255);
127+
```
128+
129+
#### HSV
130+
131+
Set the first LED - `0` - to Red `0.0`:
132+
133+
```c++
134+
wheel.set_hsv(0, 0.0, 1.0, 1.0);
135+
```
136+
137+
138+
### Clear all LEDs
139+
140+
To turn off all the LEDs, the function `.clear()` can be called. This simply goes through each LED and sets its RGB colour to black, making them emit no light.
141+
142+
This function is useful to have at the end of your code to turn the lights off, otherwise they will continue to show the last colours they were given.
143+
144+
145+
### Showing
146+
147+
Changes to the LEDs do not get applied immediately, due to the amount of I2C communication involved. As such, to have the LEDs show what they have been set to after calling the `.set_rgb()`, `.set_hsv()`, and `.clear()` functions, a specific call to `.show()` needs to be made.
148+
149+
150+
## GPIOs
151+
152+
There are three spare GPIO pins on the edge of Encoder Wheel. These can be used as digital outputs, pwm outputs, digital inputs, and analog inputs.
153+
154+
155+
### Setup
156+
157+
To start using a GPIO pin, one of the handy constants from the `encoderwheel` namespace can be used to reference them (see [GPIO Constants](#gpio-constants)).
158+
159+
Then you need to import the constants for the pin mode to use. These are on the `IOExpander` class that Encoder Wheel is based on.
160+
161+
```c++
162+
#import "breakout_ioexpander.hpp"
163+
using namespace pimoroni;
164+
165+
// For input
166+
IOExpander::PIN_IN; // or PIN_IN_PU of a pull-up is wanted
167+
168+
// For output
169+
IOExpander::PIN_OUT;
170+
171+
// For PWM
172+
IOExpander::PIN_PWM;
173+
174+
// For ADC
175+
IOExpander::PIN_ADC;
176+
```
177+
178+
179+
### Mode
180+
181+
With the intended constants imported, the mode of a GPIO pin can be set by calling `.gpio_pin_mode(gpio, mode)`:
182+
183+
```c++
184+
wheel.gpio_pin_mode(GP7, IOExpander::PIN_<IN or IN_PU or OUT or PWM or ADC>);
185+
```
186+
187+
It is also possible to read the current mode of a GPIO pin by calling `.gpio_pin_mode(gpio)`:
188+
189+
```c++
190+
mode = wheel.gpio_pin_mode(GP7);
191+
```
192+
193+
194+
### As Input or ADC
195+
196+
The current value of an GPIO pin in input or ADC mode can be read by calling `.gpio_pin_value(gpio)`:
197+
198+
```c++
199+
value = wheel.gpio_pin_value(GP7);
200+
```
201+
202+
If the mode is digital, the value will either be `0` or `1`.
203+
If the mode is analog, the value will be a voltage from `0.0` to `3.3`.
204+
205+
206+
### As Output
207+
208+
The current value of a GPIO pin in output mode can be set by calling `.gpio_pin_value(gpio, value)`:
209+
210+
```c++
211+
wheel.gpio_pin_value(GP7, value);
212+
```
213+
214+
The expected value is either `0` or `1`, or `True` or `False`.
215+
216+
217+
### As PWM
218+
219+
The GPIO pins can also be set as PWM outputs. The `PIN_PWM` constant can be accessed from the `IOExpander` class, and passed into the `.gpio_pin_mode()` function.
220+
221+
The frequency of the PWM signal can then be configured by calling `.gpio_pwm_frequency()`, which accepts a frequency (in Hz). It returns the cycle period, which should be used to set duty cycles.
222+
223+
Finally, the duty cycle of the PWM signal can be set by calling `.gpio_pin_value()` and providing it with a value between `0` and the cycle period.
224+
225+
Below is an example of setting a gpio pin to output a 25KHz signal with a 50% duty cycle:
226+
227+
```c++
228+
#include "pimoroni_i2c.hpp"
229+
#include "breakout_encoder_wheel.hpp"
230+
231+
using namespace pimoroni;
232+
using namespace encoderwheel;
233+
234+
// Initialise EncoderWheel
235+
I2C i2c(BOARD::BREAKOUT_GARDEN);
236+
BreakoutEncoderWheel wheel(&i2c);
237+
238+
// Setup the gpio pin as a PWM output
239+
wheel.gpio_pin_mode(GP7, IOExpander::PIN_PWM);
240+
241+
// Set the gpio pin's frequency to 25KHz, and record the cycle period
242+
uint16_t period = wheel.gpio_pwm_frequency(25000);
243+
244+
// Output a 50% duty cycle square wave
245+
wheel.gpio_pin_value(GP7, (int)(period * 0.5f));
246+
```
247+
248+
249+
#### Delayed Loading
250+
251+
By default, changes to a gpio pin's frequency or value are applied immediately. However, sometimes this may not be wanted, and instead you want all pins to receive updated parameters at the same time, regardless of how long the code ran that calculated the update.
252+
253+
For this purpose, `.gpio_pwm_frequency()` and `.gpio_pin_value()` include an optional parameter `load`, which by default is `true`. To avoid this "loading" set the parameter to `false`. Then either the last call can include a `true`, or a specific call to `.gpio_pwm_load()` can be made.
254+
255+
In addition, any function that performs a load, including the `.gpio_pwm_load()` function, can be made to wait until the new PWM value has been sent out of the pins. By default this is disabled, but can be enabled by including setting the `wait_for_load` parameter to `true` in the relevant function calls.
256+
257+
258+
#### Limitations
259+
260+
All of Encoder Wheel's PWM outputs share the same timing parameters. This means that GP7, GP8, and GP9 share the same frequency. Keep this in mind if changing the frequency of one, as the others will not automatically know about the change, resulting in unexpected duty cycle outputs.
261+
262+
263+
## Function Reference
264+
265+
Here is the complete list of functions available on the `BreakoutEncoderWheel` class:
266+
```c++
267+
BreakoutEncoderWheel(ioe_address=0x13, led_address=0x77, interrupt=PIN_UNUSED)
268+
set_ioe_address(address)
269+
pressed(button)
270+
count()
271+
delta()
272+
step()
273+
turn()
274+
zero()
275+
revolutions()
276+
degrees()
277+
radians()
278+
direction()
279+
direction(direction)
280+
set_rgb(index, r, g, b)
281+
set_hsv(index, h, s=1.0, v=1.0)
282+
clear()
283+
show()
284+
gpio_pin_mode(gpio)
285+
gpio_pin_mode(gpio, mode)
286+
gpio_pin_value(gpio)
287+
gpio_pin_value(gpio, value)
288+
gpio_pwm_load(wait_for_load=True)
289+
gpio_pwm_frequency(frequency, load=True, wait_for_load=True)
290+
```
291+
292+
## Constants Reference
293+
294+
Here is the complete list of public constants on the `BreakoutEncoderWheel` class:
295+
296+
### Address Constants
297+
298+
* `DEFAULT_IOE_I2C_ADDR` = `0x13`
299+
* `DEFAULT_LED_I2C_ADDR` = `0x77`
300+
* `ALTERNATE_LED_I2C_ADDR` = `0x74`
301+
302+
### Value Constants
303+
304+
* `DEFAULT_DIRECTION` = `NORMAL_DIR`
305+
* `DEFAULT_TIMEOUT` = `1`
306+
307+
308+
Here is the complete list of public constants in the `encoderwheel` namespace:
309+
310+
### Button Constants
311+
312+
* `UP` = `0`
313+
* `DOWN` = `1`
314+
* `LEFT` = `2`
315+
* `RIGHT` = `3`
316+
* `CENTRE` = `4`
317+
318+
319+
### GPIO Constants
320+
321+
* `GP7` = `7`
322+
* `GP8` = `8`
323+
* `GP9` = `9`
324+
* `GPIOS` = (`7`, `8`, `9`)
325+
326+
327+
### Count Constants
328+
329+
* `NUM_LEDS` = `24`
330+
* `NUM_BUTTONS` = `5`
331+
* `NUM_GPIOS` = `5`

micropython/examples/breakout_encoder_wheel/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Encoder Wheel Breakout Micropython Examples <!-- omit in toc -->
1+
# RGB Encoder Wheel Breakout Examples (Micropython) <!-- omit in toc -->
22

33
- [Function Examples](#function-examples)
44
- [Buttons](#buttons)

0 commit comments

Comments
 (0)