-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0120975
commit d00185d
Showing
9 changed files
with
183 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,13 @@ | ||
set(OUTPUT_NAME encoderwheel_interrupt) | ||
add_executable(${OUTPUT_NAME} interrupt.cpp) | ||
|
||
# Pull in pico libraries that we need | ||
target_link_libraries(${OUTPUT_NAME} | ||
pico_stdlib | ||
breakout_encoder_wheel | ||
) | ||
|
||
# enable usb output | ||
pico_enable_stdio_usb(${OUTPUT_NAME} 1) | ||
|
||
pico_add_extra_outputs(${OUTPUT_NAME}) |
This file contains 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,84 @@ | ||
#include <math.h> | ||
#include <string> | ||
#include "pimoroni_i2c.hpp" | ||
#include "breakout_encoder_wheel.hpp" | ||
#include "time.h" | ||
|
||
using namespace pimoroni; | ||
using namespace encoderwheel; | ||
|
||
/* | ||
How to read the buttons and rotary dial of the Encoder Wheel breakout, only when an interrupt occurs. | ||
*/ | ||
|
||
// Constants | ||
const std::string BUTTON_NAMES[] = {"Up", "Down", "Left", "Right", "Centre"}; | ||
|
||
// Create a new BreakoutEncoderWheel | ||
I2C i2c(BOARD::BREAKOUT_GARDEN); | ||
BreakoutEncoderWheel wheel(&i2c, | ||
BreakoutEncoderWheel::DEFAULT_IOE_I2C_ADDRESS, | ||
BreakoutEncoderWheel::DEFAULT_LED_I2C_ADDRESS, | ||
3); // 3 for BG_BASE, 22 for EXPLORER_BASE, or 19 for some RP2040 boards | ||
// If wiring the breakout via the qw/st connector, use the below line instead | ||
// BreakoutEncoderWheel wheel(&i2c); | ||
|
||
// Variables | ||
bool last_pressed[NUM_BUTTONS] = {false, false, false, false, false}; | ||
bool pressed[NUM_BUTTONS] = {false, false, false, false, false}; | ||
int position = 0; | ||
float hue = 0.0f; | ||
|
||
|
||
int main() { | ||
stdio_init_all(); | ||
|
||
// Attempt to initialise the encoder wheel | ||
if(wheel.init()) { | ||
|
||
// Set the first LED | ||
wheel.clear(); | ||
wheel.set_hsv(position, hue, 1.0f, 1.0f); | ||
wheel.show(); | ||
|
||
// Clear any left over interrupt from previous code | ||
wheel.clear_interrupt_flag(); | ||
|
||
// Loop forever | ||
while(true) { | ||
|
||
// Check if the interrupt has fired | ||
if(wheel.get_interrupt_flag()) { | ||
wheel.clear_interrupt_flag(); | ||
|
||
// Read all of the encoder wheel's buttons | ||
for(int b = 0 ; b < NUM_BUTTONS; b++) { | ||
pressed[b] = wheel.pressed(b); | ||
if(pressed[b] != last_pressed[b]) { | ||
printf("%s %s\n", BUTTON_NAMES[b].c_str(), pressed[b] ? "Pressed" : "Released"); | ||
} | ||
last_pressed[b] = pressed[b]; | ||
} | ||
|
||
// The interrupt may have come from several sources, | ||
// so check if it was a position change | ||
int new_position = wheel.step(); | ||
if(new_position != position) { | ||
// Record the new position (from 0 to 23) | ||
position = new_position; | ||
printf("Position = %d\n", position); | ||
|
||
// Record a colour hue from 0.0 to 1.0 | ||
hue = fmodf(wheel.revolutions(), 1.0f); | ||
|
||
// Set the LED at the new position to the new hue | ||
wheel.clear(); | ||
wheel.set_hsv(position, hue, 1.0f, 1.0f); | ||
wheel.show(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,62 @@ | ||
from pimoroni_i2c import PimoroniI2C | ||
from pimoroni import BREAKOUT_GARDEN_I2C_PINS # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS | ||
from breakout_encoder_wheel import BreakoutEncoderWheel, NUM_BUTTONS | ||
|
||
""" | ||
How to read the buttons and rotary dial of the Encoder Wheel breakout, only when an interrupt occurs. | ||
Press Ctrl+C to stop the program. | ||
""" | ||
|
||
# Constants | ||
BUTTON_NAMES = ["Up", "Down", "Left", "Right", "Centre"] | ||
|
||
# Create a new BreakoutEncoderWheel with a pin on the Pico specified as an interrupt | ||
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS) | ||
wheel = BreakoutEncoderWheel(i2c, interrupt=3) # 3 for BG_BASE, 22 for EXPLORER_BASE, or 19 for some RP2040 boards | ||
# If wiring the breakout via the qw/st connector, use the below line instead | ||
# wheel = BreakoutEncoderWheel(i2c) | ||
|
||
# Variables | ||
last_pressed = [False] * NUM_BUTTONS | ||
pressed = [False] * NUM_BUTTONS | ||
position = 0 | ||
hue = 0.0 | ||
|
||
# Set the first LED | ||
wheel.clear() | ||
wheel.set_hsv(position, hue, 1.0, 1.0) | ||
wheel.show() | ||
|
||
# Clear any left over interrupt from previous code | ||
wheel.clear_interrupt_flag() | ||
|
||
# Loop forever | ||
while True: | ||
|
||
# Check if the interrupt has fired | ||
if wheel.get_interrupt_flag(): | ||
wheel.clear_interrupt_flag() | ||
|
||
# Read all of the encoder wheel's buttons | ||
for b in range(NUM_BUTTONS): | ||
pressed[b] = wheel.pressed(b) | ||
if pressed[b] != last_pressed[b]: | ||
print(BUTTON_NAMES[b], "Pressed" if pressed[b] else "Released") | ||
last_pressed[b] = pressed[b] | ||
|
||
# The interrupt may have come from several sources, | ||
# so check if it was a position change | ||
new_position = wheel.step() | ||
if new_position != position: | ||
# Record the new position (from 0 to 23) | ||
position = new_position | ||
print("Position = ", position) | ||
|
||
# Record a colour hue from 0.0 to 1.0 | ||
hue = wheel.revolutions() % 1.0 | ||
|
||
# Set the LED at the new position to the new hue | ||
wheel.clear() | ||
wheel.set_hsv(position, hue, 1.0, 1.0) | ||
wheel.show() |
This file contains 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