first re-commit.
This commit is contained in:
3
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/README
Normal file
3
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/README
Normal file
@@ -0,0 +1,3 @@
|
||||
Class to decode a Wiegand code.
|
||||
|
||||
Follow the instructions in the test file to build and run.
|
40
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/test_wiegand.cpp
Normal file
40
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/test_wiegand.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "wiegand.hpp"
|
||||
|
||||
/*
|
||||
|
||||
REQUIRES
|
||||
|
||||
Wiegand contacts 0 and 1 connected to separate gpios.
|
||||
|
||||
TO BUILD
|
||||
|
||||
g++ -o wiegand_cpp test_wiegand.cpp wiegand.cpp -lpigpio -lrt
|
||||
|
||||
TO RUN
|
||||
|
||||
sudo ./wiegand_cpp
|
||||
|
||||
*/
|
||||
|
||||
void callback(int bits, uint32_t value)
|
||||
{
|
||||
std::cout << "bits=" << bits << " value=" << value << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (gpioInitialise() < 0) return 1;
|
||||
|
||||
Wiegand dec(14, 15, callback);
|
||||
|
||||
sleep(300);
|
||||
|
||||
dec.cancel();
|
||||
|
||||
gpioTerminate();
|
||||
}
|
||||
|
116
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/wiegand.cpp
Normal file
116
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/wiegand.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "wiegand.hpp"
|
||||
|
||||
Wiegand::Wiegand(int gpio_0, int gpio_1, WiegandCB_t callback, int timeout)
|
||||
{
|
||||
/*
|
||||
Instantiate with the gpio for 0 (green wire), the gpio for 1
|
||||
(white wire), the callback function, and the bit timeout in
|
||||
milliseconds which indicates the end of a code.
|
||||
|
||||
The callback is passed the code length in bits and the value.
|
||||
*/
|
||||
|
||||
mygpio_0 = gpio_0;
|
||||
mygpio_1 = gpio_1;
|
||||
|
||||
mycallback = callback;
|
||||
|
||||
mytimeout = timeout;
|
||||
|
||||
in_code = 0;
|
||||
|
||||
gpioSetMode(gpio_0, PI_INPUT);
|
||||
gpioSetMode(gpio_1, PI_INPUT);
|
||||
|
||||
gpioSetPullUpDown(gpio_0, PI_PUD_UP);
|
||||
gpioSetPullUpDown(gpio_1, PI_PUD_UP);
|
||||
|
||||
gpioSetAlertFuncEx(gpio_0, _cbEx, this);
|
||||
gpioSetAlertFuncEx(gpio_1, _cbEx, this);
|
||||
}
|
||||
|
||||
void Wiegand::_cb(int gpio, int level, uint32_t tick)
|
||||
{
|
||||
/*
|
||||
Accumulate bits until both gpios 0 and 1 timeout.
|
||||
*/
|
||||
|
||||
if (level == 0) /* a falling edge indicates a new bit */
|
||||
{
|
||||
if (!in_code)
|
||||
{
|
||||
bits = 1;
|
||||
num = 0;
|
||||
|
||||
in_code = 1;
|
||||
code_timeout = 0;
|
||||
|
||||
gpioSetWatchdog(mygpio_0, mytimeout);
|
||||
gpioSetWatchdog(mygpio_1, mytimeout);
|
||||
}
|
||||
else
|
||||
{
|
||||
bits++;
|
||||
num <<= 1;
|
||||
}
|
||||
|
||||
if (gpio == mygpio_0)
|
||||
{
|
||||
code_timeout &= 2; /* clear gpio 0 timeout */
|
||||
}
|
||||
else
|
||||
{
|
||||
code_timeout &= 1; /* clear gpio 1 timeout */
|
||||
num |= 1;
|
||||
}
|
||||
}
|
||||
else if (level == PI_TIMEOUT)
|
||||
{
|
||||
if (in_code)
|
||||
{
|
||||
if (gpio == mygpio_0)
|
||||
{
|
||||
code_timeout |= 1; /* timeout gpio 0 */
|
||||
}
|
||||
else
|
||||
{
|
||||
code_timeout |= 2; /* timeout gpio 1 */
|
||||
}
|
||||
|
||||
if (code_timeout == 3) /* both gpios timed out */
|
||||
{
|
||||
gpioSetWatchdog(mygpio_0, 0);
|
||||
gpioSetWatchdog(mygpio_1, 0);
|
||||
|
||||
in_code = 0;
|
||||
|
||||
(mycallback)(bits, num);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Wiegand::_cbEx(int gpio, int level, uint32_t tick, void *user)
|
||||
{
|
||||
/*
|
||||
Need a static callback to link with C.
|
||||
*/
|
||||
|
||||
Wiegand *mySelf = (Wiegand *) user;
|
||||
|
||||
mySelf->_cb(gpio, level, tick); /* Call the instance callback. */
|
||||
}
|
||||
|
||||
|
||||
void Wiegand::cancel(void)
|
||||
{
|
||||
/*
|
||||
Cancel the Wiegand decoder.
|
||||
*/
|
||||
|
||||
gpioSetAlertFuncEx(mygpio_0, 0, this);
|
||||
gpioSetAlertFuncEx(mygpio_1, 0, this);
|
||||
}
|
||||
|
43
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/wiegand.hpp
Normal file
43
pigpio-master/EXAMPLES/CPP/WIEGAND_CODE/wiegand.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WIEGAND_HPP
|
||||
#define WIEGAND_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*WiegandCB_t)(int, uint32_t);
|
||||
|
||||
class Wiegand
|
||||
{
|
||||
int mygpio_0, mygpio_1, mytimeout, in_code, bits;
|
||||
|
||||
WiegandCB_t mycallback;
|
||||
|
||||
uint32_t num;
|
||||
|
||||
uint32_t code_timeout;
|
||||
|
||||
void _cb(int gpio, int level, uint32_t tick);
|
||||
|
||||
/* Need a static callback to link with C. */
|
||||
static void _cbEx(int gpio, int level, uint32_t tick, void *user);
|
||||
|
||||
public:
|
||||
|
||||
Wiegand(int gpio_0, int gpio_1, WiegandCB_t callback, int timeout=5);
|
||||
/*
|
||||
This function establishes a Wiegand decoder on gpio_0 and gpio_1.
|
||||
|
||||
A gap of timeout milliseconds without a new bit indicates
|
||||
the end of a code.
|
||||
|
||||
When the code is ended the callback function is called with the code
|
||||
bit length and value.
|
||||
*/
|
||||
|
||||
void cancel(void);
|
||||
/*
|
||||
This function releases the resources used by the decoder.
|
||||
*/
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user