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
+ }
0 commit comments