Skip to content

Commit 5619274

Browse files
committed
Added MP readme
1 parent 862806f commit 5619274

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed
Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
# Encoder Wheel Breakout (Micropython) <!-- omit in toc -->
2+
3+
This is the Micropython 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+
- [Button Constants](#button-constants)
33+
- [GPIO Constants](#gpio-constants)
34+
- [Count Constants](#count-constants)
35+
36+
37+
## Getting Started
38+
39+
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:
40+
41+
```python
42+
from pimoroni_i2c import PimoroniI2C
43+
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
44+
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
45+
```
46+
47+
This creates a `i2c` variable that can be passed into the Encoder Wheel's class as part of its creation:
48+
```python
49+
from breakout_encoder_wheel import BreakoutEncoderWheel
50+
wheel = BreakoutEncoderWheel(i2c)
51+
```
52+
53+
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.
54+
55+
56+
## Reading the Buttons
57+
58+
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:
59+
60+
* `UP` = `0`
61+
* `DOWN` = `1`
62+
* `LEFT` = `2`
63+
* `RIGHT` = `3`
64+
* `CENTRE` = `4`
65+
66+
For example, to read the centre button you would write:
67+
68+
```python
69+
centre_state = wheel.pressed(CENTRE)
70+
```
71+
72+
You can also get the number of buttons using the `NUM_BUTTONS` constant.
73+
74+
75+
## Reading the Encoder
76+
77+
Within the directional buttons of the Encoder Wheel breakout is a rotary encoder with 24 counts per revolution.
78+
79+
### Count and Angle
80+
81+
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()`.
82+
83+
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.
84+
85+
86+
### Count Delta
87+
88+
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.
89+
90+
91+
### Step and Turn
92+
93+
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()`.
94+
95+
These functions differ from reading the `.count()` or `.revolutions()` by using separate counters, and so avoid the wrapping issue that these functions experience.
96+
97+
98+
### Changing the Direction
99+
100+
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 the `pimoroni` module. There is also a `NORMAL_DIR` constant, though this is the default.
101+
102+
103+
### Resetting to Zero
104+
105+
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()`.
106+
107+
108+
## LEDs
109+
110+
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.
111+
112+
113+
### Setting an LED
114+
115+
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.
116+
117+
#### RGB
118+
119+
Set the first LED - `0` - to Purple `255, 0, 255`:
120+
121+
```python
122+
wheel.set_rgb(0, 255, 0, 255)
123+
```
124+
125+
#### HSV
126+
127+
Set the first LED - `0` - to Red `0.0`:
128+
129+
```python
130+
wheel.set_hsv(0, 0.0, 1.0, 1.0)
131+
```
132+
133+
134+
### Clear all LEDs
135+
136+
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.
137+
138+
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.
139+
140+
141+
### Showing
142+
143+
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.
144+
145+
146+
## GPIOs
147+
148+
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.
149+
150+
151+
### Setup
152+
153+
To start using a GPIO pin, first import one of the handy constants used to reference them (see [GPIO Constants](#gpio-constants)). For example, to use the first GPIO pin:
154+
155+
```python
156+
from breakout_encoder_wheel import GP7
157+
```
158+
159+
Then you need to import the constants for the pin mode to use. These are on the `breakout_ioexpander` module that Encoder Wheel is based on.
160+
161+
```python
162+
# For input
163+
from breakout_ioexpander import IN # or IN_PU of a pull-up is wanted
164+
165+
# For output
166+
from breakout_ioexpander import OUT
167+
168+
# For PWM
169+
from breakout_ioexpander import PWM
170+
171+
# For ADC
172+
from breakout_ioexpander import ADC
173+
```
174+
175+
176+
### Mode
177+
178+
With the intended constants imported, the mode of a GPIO pin can be set by calling `.gpio_pin_mode(gpio, mode)`:
179+
180+
```python
181+
wheel.gpio_pin_mode(GP7, <IN or IN_PU or OUT or PWM or ADC>)
182+
```
183+
184+
It is also possible to read the current mode of a GPIO pin by calling `.gpio_pin_mode(gpio)`:
185+
186+
```python
187+
mode = wheel.gpio_pin_mode(GP7)
188+
```
189+
190+
191+
### As Input or ADC
192+
193+
The current value of an GPIO pin in input or ADC mode can be read by calling `.gpio_pin_value(gpio)`:
194+
195+
```python
196+
value = wheel.gpio_pin_value(GP7)
197+
```
198+
199+
If the mode is digital, the value will either be `0` or `1`.
200+
If the mode is analog, the value will be a voltage from `0.0` to `3.3`.
201+
202+
203+
### As Output
204+
205+
The current value of a GPIO pin in output mode can be set by calling `.gpio_pin_value(gpio, value)`:
206+
207+
```python
208+
wheel.gpio_pin_value(GP7, value)
209+
```
210+
211+
The expected value is either `0` or `1`, or `True` or `False`.
212+
213+
214+
### As PWM
215+
216+
The GPIO pins can also be set as PWM outputs. The `PWM` constant can be imported from the `breakout_ioexpander` module, and passed into the `.gpio_pin_mode()` function.
217+
218+
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.
219+
220+
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.
221+
222+
Below is an example of setting a gpio pin to output a 25KHz signal with a 50% duty cycle:
223+
224+
```python
225+
from pimoroni_i2c import PimoroniI2C
226+
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
227+
from breakout_ioexpander import PWM
228+
from breakout_encoderwheel import BreakoutEncoderWheel, GP7
229+
230+
# Initialise EncoderWheel
231+
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
232+
wheel = BreakoutEncoderWheel(i2c)
233+
234+
# Setup the gpio pin as a PWM output
235+
wheel.gpio_pin_mode(GP7, PWM)
236+
237+
# Set the gpio pin's frequency to 25KHz, and record the cycle period
238+
period = wheel.gpio_pwm_frequency(25000)
239+
240+
# Output a 50% duty cycle square wave
241+
wheel.gpio_pin_value(GP7, int(period * 0.5))
242+
```
243+
244+
245+
#### Delayed Loading
246+
247+
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.
248+
249+
For this purpose, `.gpio_pwm_frequency()` and `.gpio_pin_value()` include an optional parameter `load`, which by default is `True`. To avoid this "loading" include `load=False` in the relevant function calls. Then either the last call can include `load=True`, or a specific call to `.gpio_pwm_load()` can be made.
250+
251+
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 `wait_for_load=True` in the relevant function calls.
252+
253+
254+
#### Limitations
255+
256+
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.
257+
258+
259+
## Function Reference
260+
261+
Here is the complete list of functions available on the `BreakoutEncoderWheel` class:
262+
```python
263+
BreakoutEncoderWheel(ioe_address=0x13, led_address=0x77, interrupt=PIN_UNUSED)
264+
set_ioe_address(address)
265+
pressed(button)
266+
count()
267+
delta()
268+
step()
269+
turn()
270+
zero()
271+
revolutions()
272+
degrees()
273+
radians()
274+
direction()
275+
direction(direction)
276+
set_rgb(index, r, g, b)
277+
set_hsv(index, h, s=1.0, v=1.0)
278+
clear()
279+
show()
280+
gpio_pin_mode(gpio)
281+
gpio_pin_mode(gpio, mode)
282+
gpio_pin_value(gpio)
283+
gpio_pin_value(gpio, value)
284+
gpio_pwm_load(wait_for_load=True)
285+
gpio_pwm_frequency(frequency, load=True, wait_for_load=True)
286+
```
287+
288+
## Constants Reference
289+
290+
Here is the complete list of constants on the `breakoutencoderwheel` module:
291+
292+
### Address Constants
293+
294+
* `DEFAULT_IOE_I2C_ADDR` = `0x13`
295+
* `DEFAULT_LED_I2C_ADDR` = `0x77`
296+
* `ALTERNATE_LED_I2C_ADDR` = `0x74`
297+
298+
299+
### Button Constants
300+
301+
* `UP` = `0`
302+
* `DOWN` = `1`
303+
* `LEFT` = `2`
304+
* `RIGHT` = `3`
305+
* `CENTRE` = `4`
306+
307+
308+
### GPIO Constants
309+
310+
* `GP7` = `7`
311+
* `GP8` = `8`
312+
* `GP9` = `9`
313+
* `GPIOS` = (`7`, `8`, `9`)
314+
315+
316+
### Count Constants
317+
318+
* `NUM_LEDS` = `24`
319+
* `NUM_BUTTONS` = `5`
320+
* `NUM_GPIOS` = `5`

micropython/modules/breakout_ioexpander/breakout_ioexpander.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ const mp_obj_type_t breakout_ioexpander_BreakoutIOExpander_type = {
8888
STATIC const mp_map_elem_t breakout_ioexpander_globals_table[] = {
8989
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_breakout_ioexpander) },
9090
{ MP_OBJ_NEW_QSTR(MP_QSTR_BreakoutIOExpander), (mp_obj_t)&breakout_ioexpander_BreakoutIOExpander_type },
91+
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(IOE_PIN_IN) },
92+
{ MP_ROM_QSTR(MP_QSTR_IN_PU), MP_ROM_INT(IOE_PIN_IN_PU) },
93+
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(IOE_PIN_OUT) },
94+
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_INT(IOE_PIN_PWM) },
95+
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_INT(IOE_PIN_ADC) },
9196
};
9297
STATIC MP_DEFINE_CONST_DICT(mp_module_breakout_ioexpander_globals, breakout_ioexpander_globals_table);
9398

0 commit comments

Comments
 (0)