Connect a rotary encoder to Bela's digital inputs, detect the direction of the rotation, and keep track of how far you've gone.
If your encoder has a built-in switch, you can use that to reset the current count.
#include <libraries/Scope/Scope.h>
#include <libraries/Encoder/Encoder.h>
unsigned int kEncChA = 0;
unsigned int kEncChB = 1;
unsigned int kEncChBtn = 2;
unsigned int kDebouncingSamples = 15;
{
gEncoder.setup(kDebouncingSamples, polarity);
pinMode(context, 0, kEncChA, INPUT);
pinMode(context, 0, kEncChB, INPUT);
pinMode(context, 0, kEncChBtn, INPUT);
return true;
}
bool gButton;
unsigned int holdoff;
{
{
rt_printf(
"%s : %3d\n",
Encoder::CCW == ret ?
"ccw" :
"cw ", gEncoder.get());
}
if(gButton != button) {
gButton = button;
if(button) {
gEncoder.reset();
rt_printf("reset : %3d\n", gEncoder.get());
}
}
if(1)
{
button = 1;
else
button = 0;
}
gScope.log(
a * 0.5 + 0.333,
b * 0.5 - 0.333,
button * 0.5 - 0.88
);
}
}
{
}
Connect a quadrature rotary encoder.
Definition Encoder.h:16
Polarity
Definition Encoder.h:27
@ ANY
Trigger on any edge.
Definition Encoder.h:28
Rotation
Definition Encoder.h:18
@ CCW
The encoder rotated counter-clockwise.
Definition Encoder.h:19
@ NONE
The encoder did not rotate.
Definition Encoder.h:20
An oscilloscope which allows data to be visualised in a browser in real time.
Definition Scope.h:23
static int digitalRead(BelaContext *context, int frame, int channel)
Read a digital input, specifying the frame number (when to read) and the pin.
Definition Bela.h:1518
static void pinMode(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition Bela.h:1548
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition render.cpp:68
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition render.cpp:51
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition render.cpp:96
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const uint32_t digitalFrames
Number of digital frames per period.
Definition Bela.h:365
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328