Skip to content

Commit d00185d

Browse files
committed
Added C & MP interrupt example
1 parent 0120975 commit d00185d

File tree

9 files changed

+183
-4
lines changed

9 files changed

+183
-4
lines changed

examples/breakout_encoder_wheel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ add_subdirectory(clock)
44
add_subdirectory(colour_picker)
55
add_subdirectory(encoder)
66
add_subdirectory(gpio_pwm)
7+
add_subdirectory(interrupt)
78
add_subdirectory(led_rainbow)
89
add_subdirectory(stop_watch)

examples/breakout_encoder_wheel/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [Function Examples](#function-examples)
44
- [Buttons](#buttons)
55
- [Encoder](#encoder)
6+
- [Interrupt](#interrupt)
67
- [LED Examples](#led-examples)
78
- [LED Rainbow](#led-rainbow)
89
- [Clock](#clock)
@@ -28,6 +29,12 @@ A demonstration of reading the 5 buttons on Encoder Wheel.
2829
A demonstration of reading the rotary dial of the Encoder Wheel breakout.
2930

3031

32+
### Interrupt
33+
[interrupt/interrupt.cpp](interrupt/interrupt.cpp)
34+
35+
How to read the buttons and rotary dial of the Encoder Wheel breakout, only when an interrupt occurs.
36+
37+
3138
## LED Examples
3239

3340
### LED Rainbow
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set(OUTPUT_NAME encoderwheel_interrupt)
2+
add_executable(${OUTPUT_NAME} interrupt.cpp)
3+
4+
# Pull in pico libraries that we need
5+
target_link_libraries(${OUTPUT_NAME}
6+
pico_stdlib
7+
breakout_encoder_wheel
8+
)
9+
10+
# enable usb output
11+
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
12+
13+
pico_add_extra_outputs(${OUTPUT_NAME})
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <math.h>
2+
#include <string>
3+
#include "pimoroni_i2c.hpp"
4+
#include "breakout_encoder_wheel.hpp"
5+
#include "time.h"
6+
7+
using namespace pimoroni;
8+
using namespace encoderwheel;
9+
10+
/*
11+
How to read the buttons and rotary dial of the Encoder Wheel breakout, only when an interrupt occurs.
12+
*/
13+
14+
// Constants
15+
const std::string BUTTON_NAMES[] = {"Up", "Down", "Left", "Right", "Centre"};
16+
17+
// Create a new BreakoutEncoderWheel
18+
I2C i2c(BOARD::BREAKOUT_GARDEN);
19+
BreakoutEncoderWheel wheel(&i2c,
20+
BreakoutEncoderWheel::DEFAULT_IOE_I2C_ADDRESS,
21+
BreakoutEncoderWheel::DEFAULT_LED_I2C_ADDRESS,
22+
3); // 3 for BG_BASE, 22 for EXPLORER_BASE, or 19 for some RP2040 boards
23+
// If wiring the breakout via the qw/st connector, use the below line instead
24+
// BreakoutEncoderWheel wheel(&i2c);
25+
26+
// Variables
27+
bool last_pressed[NUM_BUTTONS] = {false, false, false, false, false};
28+
bool pressed[NUM_BUTTONS] = {false, false, false, false, false};
29+
int position = 0;
30+
float hue = 0.0f;
31+
32+
33+
int main() {
34+
stdio_init_all();
35+
36+
// Attempt to initialise the encoder wheel
37+
if(wheel.init()) {
38+
39+
// Set the first LED
40+
wheel.clear();
41+
wheel.set_hsv(position, hue, 1.0f, 1.0f);
42+
wheel.show();
43+
44+
// Clear any left over interrupt from previous code
45+
wheel.clear_interrupt_flag();
46+
47+
// Loop forever
48+
while(true) {
49+
50+
// Check if the interrupt has fired
51+
if(wheel.get_interrupt_flag()) {
52+
wheel.clear_interrupt_flag();
53+
54+
// Read all of the encoder wheel's buttons
55+
for(int b = 0 ; b < NUM_BUTTONS; b++) {
56+
pressed[b] = wheel.pressed(b);
57+
if(pressed[b] != last_pressed[b]) {
58+
printf("%s %s\n", BUTTON_NAMES[b].c_str(), pressed[b] ? "Pressed" : "Released");
59+
}
60+
last_pressed[b] = pressed[b];
61+
}
62+
63+
// The interrupt may have come from several sources,
64+
// so check if it was a position change
65+
int new_position = wheel.step();
66+
if(new_position != position) {
67+
// Record the new position (from 0 to 23)
68+
position = new_position;
69+
printf("Position = %d\n", position);
70+
71+
// Record a colour hue from 0.0 to 1.0
72+
hue = fmodf(wheel.revolutions(), 1.0f);
73+
74+
// Set the LED at the new position to the new hue
75+
wheel.clear();
76+
wheel.set_hsv(position, hue, 1.0f, 1.0f);
77+
wheel.show();
78+
}
79+
}
80+
}
81+
}
82+
83+
return 0;
84+
}

libraries/breakout_encoder_wheel/breakout_encoder_wheel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ namespace encoderwheel {
1717
ioe.set_mode(SW_RIGHT, IOExpander::PIN_IN_PU);
1818
ioe.set_mode(SW_CENTRE, IOExpander::PIN_IN_PU);
1919

20+
ioe.set_pin_interrupt(SW_UP, true);
21+
ioe.set_pin_interrupt(SW_DOWN, true);
22+
ioe.set_pin_interrupt(SW_LEFT, true);
23+
ioe.set_pin_interrupt(SW_RIGHT, true);
24+
ioe.set_pin_interrupt(SW_CENTRE, true);
25+
2026
led_ring.enable({
2127
0b00000000, 0b10111111,
2228
0b00111110, 0b00111110,

micropython/examples/breakout_encoder_wheel/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [Function Examples](#function-examples)
44
- [Buttons](#buttons)
55
- [Encoder](#encoder)
6+
- [Interrupt](#interrupt)
67
- [LED Examples](#led-examples)
78
- [LED Rainbow](#led-rainbow)
89
- [Clock](#clock)
@@ -28,6 +29,12 @@ A demonstration of reading the 5 buttons on Encoder Wheel.
2829
A demonstration of reading the rotary dial of the Encoder Wheel breakout.
2930

3031

32+
### Interrupt
33+
[interrupt.py](interrupt.py)
34+
35+
How to read the buttons and rotary dial of the Encoder Wheel breakout, only when an interrupt occurs.
36+
37+
3138
## LED Examples
3239

3340
### LED Rainbow

micropython/examples/breakout_encoder_wheel/encoder.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
Press Ctrl+C to stop the program.
99
"""
1010

11-
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
12-
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
13-
1411
# Create a new BreakoutEncoderWheel
1512
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
1613
wheel = BreakoutEncoderWheel(i2c)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from pimoroni_i2c import PimoroniI2C
2+
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
3+
from breakout_encoder_wheel import BreakoutEncoderWheel, NUM_BUTTONS
4+
5+
"""
6+
How to read the buttons and rotary dial of the Encoder Wheel breakout, only when an interrupt occurs.
7+
8+
Press Ctrl+C to stop the program.
9+
"""
10+
11+
# Constants
12+
BUTTON_NAMES = ["Up", "Down", "Left", "Right", "Centre"]
13+
14+
# Create a new BreakoutEncoderWheel with a pin on the Pico specified as an interrupt
15+
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
16+
wheel = BreakoutEncoderWheel(i2c, interrupt=3) # 3 for BG_BASE, 22 for EXPLORER_BASE, or 19 for some RP2040 boards
17+
# If wiring the breakout via the qw/st connector, use the below line instead
18+
# wheel = BreakoutEncoderWheel(i2c)
19+
20+
# Variables
21+
last_pressed = [False] * NUM_BUTTONS
22+
pressed = [False] * NUM_BUTTONS
23+
position = 0
24+
hue = 0.0
25+
26+
# Set the first LED
27+
wheel.clear()
28+
wheel.set_hsv(position, hue, 1.0, 1.0)
29+
wheel.show()
30+
31+
# Clear any left over interrupt from previous code
32+
wheel.clear_interrupt_flag()
33+
34+
# Loop forever
35+
while True:
36+
37+
# Check if the interrupt has fired
38+
if wheel.get_interrupt_flag():
39+
wheel.clear_interrupt_flag()
40+
41+
# Read all of the encoder wheel's buttons
42+
for b in range(NUM_BUTTONS):
43+
pressed[b] = wheel.pressed(b)
44+
if pressed[b] != last_pressed[b]:
45+
print(BUTTON_NAMES[b], "Pressed" if pressed[b] else "Released")
46+
last_pressed[b] = pressed[b]
47+
48+
# The interrupt may have come from several sources,
49+
# so check if it was a position change
50+
new_position = wheel.step()
51+
if new_position != position:
52+
# Record the new position (from 0 to 23)
53+
position = new_position
54+
print("Position = ", position)
55+
56+
# Record a colour hue from 0.0 to 1.0
57+
hue = wheel.revolutions() % 1.0
58+
59+
# Set the LED at the new position to the new hue
60+
wheel.clear()
61+
wheel.set_hsv(position, hue, 1.0, 1.0)
62+
wheel.show()

micropython/modules/breakout_encoder_wheel/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ Here is the complete list of functions available on the `BreakoutEncoderWheel` c
262262
```python
263263
BreakoutEncoderWheel(ioe_address=0x13, led_address=0x77, interrupt=PIN_UNUSED)
264264
set_ioe_address(address)
265+
get_interrupt_flag()
266+
clear_interrupt_flag()
265267
pressed(button)
266268
count()
267269
delta()
@@ -280,7 +282,7 @@ show()
280282
gpio_pin_mode(gpio)
281283
gpio_pin_mode(gpio, mode)
282284
gpio_pin_value(gpio)
283-
gpio_pin_value(gpio, value)
285+
gpio_pin_value(gpio, value, load=True, wait_for_load=False)
284286
gpio_pwm_load(wait_for_load=True)
285287
gpio_pwm_frequency(frequency, load=True, wait_for_load=True)
286288
```

0 commit comments

Comments
 (0)