first re-commit.
This commit is contained in:
4
pigpio-master/EXAMPLES/CPP/ROTARY_ENCODER/README
Normal file
4
pigpio-master/EXAMPLES/CPP/ROTARY_ENCODER/README
Normal file
@@ -0,0 +1,4 @@
|
||||
Class to decode a mechanical rotary encoder.
|
||||
|
||||
Follow the instructions in the test file to build and run.
|
||||
|
84
pigpio-master/EXAMPLES/CPP/ROTARY_ENCODER/rotary_encoder.cpp
Normal file
84
pigpio-master/EXAMPLES/CPP/ROTARY_ENCODER/rotary_encoder.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "rotary_encoder.hpp"
|
||||
|
||||
/*
|
||||
|
||||
+---------+ +---------+ 0
|
||||
| | | |
|
||||
A | | | |
|
||||
| | | |
|
||||
+---------+ +---------+ +----- 1
|
||||
|
||||
+---------+ +---------+ 0
|
||||
| | | |
|
||||
B | | | |
|
||||
| | | |
|
||||
----+ +---------+ +---------+ 1
|
||||
|
||||
*/
|
||||
|
||||
void re_decoder::_pulse(int gpio, int level, uint32_t tick)
|
||||
{
|
||||
if (gpio == mygpioA) levA = level; else levB = level;
|
||||
|
||||
if (gpio != lastGpio) /* debounce */
|
||||
{
|
||||
lastGpio = gpio;
|
||||
|
||||
if ((gpio == mygpioA) && (level == 1))
|
||||
{
|
||||
if (levB) (mycallback)(1);
|
||||
}
|
||||
else if ((gpio == mygpioB) && (level == 1))
|
||||
{
|
||||
if (levA) (mycallback)(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void re_decoder::_pulseEx(int gpio, int level, uint32_t tick, void *user)
|
||||
{
|
||||
/*
|
||||
Need a static callback to link with C.
|
||||
*/
|
||||
|
||||
re_decoder *mySelf = (re_decoder *) user;
|
||||
|
||||
mySelf->_pulse(gpio, level, tick); /* Call the instance callback. */
|
||||
}
|
||||
|
||||
re_decoder::re_decoder(int gpioA, int gpioB, re_decoderCB_t callback)
|
||||
{
|
||||
mygpioA = gpioA;
|
||||
mygpioB = gpioB;
|
||||
|
||||
mycallback = callback;
|
||||
|
||||
levA=0;
|
||||
levB=0;
|
||||
|
||||
lastGpio = -1;
|
||||
|
||||
gpioSetMode(gpioA, PI_INPUT);
|
||||
gpioSetMode(gpioB, PI_INPUT);
|
||||
|
||||
/* pull up is needed as encoder common is grounded */
|
||||
|
||||
gpioSetPullUpDown(gpioA, PI_PUD_UP);
|
||||
gpioSetPullUpDown(gpioB, PI_PUD_UP);
|
||||
|
||||
/* monitor encoder level changes */
|
||||
|
||||
gpioSetAlertFuncEx(gpioA, _pulseEx, this);
|
||||
gpioSetAlertFuncEx(gpioB, _pulseEx, this);
|
||||
}
|
||||
|
||||
void re_decoder::re_cancel(void)
|
||||
{
|
||||
gpioSetAlertFuncEx(mygpioA, 0, this);
|
||||
gpioSetAlertFuncEx(mygpioB, 0, this);
|
||||
}
|
||||
|
35
pigpio-master/EXAMPLES/CPP/ROTARY_ENCODER/rotary_encoder.hpp
Normal file
35
pigpio-master/EXAMPLES/CPP/ROTARY_ENCODER/rotary_encoder.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef ROTARY_ENCODER_HPP
|
||||
#define ROTARY_ENCODER_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*re_decoderCB_t)(int);
|
||||
|
||||
class re_decoder
|
||||
{
|
||||
int mygpioA, mygpioB, levA, levB, lastGpio;
|
||||
|
||||
re_decoderCB_t mycallback;
|
||||
|
||||
void _pulse(int gpio, int level, uint32_t tick);
|
||||
|
||||
/* Need a static callback to link with C. */
|
||||
static void _pulseEx(int gpio, int level, uint32_t tick, void *user);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
re_decoder(int gpioA, int gpioB, re_decoderCB_t callback);
|
||||
/*
|
||||
This function establishes a rotary encoder on gpioA and gpioB.
|
||||
|
||||
When the encoder is turned the callback function is called.
|
||||
*/
|
||||
|
||||
void re_cancel(void);
|
||||
/*
|
||||
This function releases the resources used by the decoder.
|
||||
*/
|
||||
};
|
||||
|
||||
#endif
|
@@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "rotary_encoder.hpp"
|
||||
|
||||
/*
|
||||
|
||||
REQUIRES
|
||||
|
||||
A rotary encoder contacts A and B connected to separate gpios and
|
||||
the common contact connected to Pi ground.
|
||||
|
||||
TO BUILD
|
||||
|
||||
g++ -o rot_enc_cpp test_rotary_encoder.cpp rotary_encoder.cpp -lpigpio -lrt
|
||||
|
||||
TO RUN
|
||||
|
||||
sudo ./rot_enc_cpp
|
||||
|
||||
*/
|
||||
|
||||
void callback(int way)
|
||||
{
|
||||
static int pos = 0;
|
||||
|
||||
pos += way;
|
||||
|
||||
std::cout << "pos=" << pos << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (gpioInitialise() < 0) return 1;
|
||||
|
||||
re_decoder dec(7, 8, callback);
|
||||
|
||||
sleep(3000);
|
||||
|
||||
dec.re_cancel();
|
||||
|
||||
gpioTerminate();
|
||||
}
|
||||
|
Reference in New Issue
Block a user