first re-commit.
This commit is contained in:
4
pigpio-master/EXAMPLES/C/WIEGAND_CODE/README
Normal file
4
pigpio-master/EXAMPLES/C/WIEGAND_CODE/README
Normal file
@@ -0,0 +1,4 @@
|
||||
Function to decode a Wiegand code.
|
||||
|
||||
Follow the instructions in the test file to build and run.
|
||||
|
42
pigpio-master/EXAMPLES/C/WIEGAND_CODE/test_wiegand.c
Normal file
42
pigpio-master/EXAMPLES/C/WIEGAND_CODE/test_wiegand.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "wiegand.h"
|
||||
|
||||
/*
|
||||
|
||||
REQUIRES
|
||||
|
||||
Wiegand contacts 0 and 1 connected to separate gpios.
|
||||
|
||||
TO BUILD
|
||||
|
||||
gcc -o wiegand_c test_wiegand.c wiegand.c -lpigpio -lrt
|
||||
|
||||
TO RUN
|
||||
|
||||
sudo ./wiegand_c
|
||||
|
||||
*/
|
||||
|
||||
void callback(int bits, uint32_t value)
|
||||
{
|
||||
printf("bits=%d value=%u\n", bits, value);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Pi_Wieg_t * w;
|
||||
|
||||
if (gpioInitialise() < 0) return 1;
|
||||
|
||||
w = Pi_Wieg(14, 15, callback, 5);
|
||||
|
||||
sleep(300);
|
||||
|
||||
Pi_Wieg_cancel(w);
|
||||
|
||||
gpioTerminate();
|
||||
}
|
||||
|
137
pigpio-master/EXAMPLES/C/WIEGAND_CODE/wiegand.c
Normal file
137
pigpio-master/EXAMPLES/C/WIEGAND_CODE/wiegand.c
Normal file
@@ -0,0 +1,137 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "wiegand.h"
|
||||
|
||||
struct _Pi_Wieg_s
|
||||
{
|
||||
int mygpio_0;
|
||||
int mygpio_1;
|
||||
int mytimeout;
|
||||
int in_code;
|
||||
int bits;
|
||||
Pi_Wieg_CB_t mycallback;
|
||||
uint32_t num;
|
||||
uint32_t code_timeout;
|
||||
};
|
||||
|
||||
void _cb(int gpio, int level, uint32_t tick, void *user)
|
||||
{
|
||||
/*
|
||||
Accumulate bits until both gpios 0 and 1 timeout.
|
||||
*/
|
||||
|
||||
Pi_Wieg_t *wieg;
|
||||
|
||||
wieg = user;
|
||||
|
||||
if (level == 0) /* a falling edge indicates a new bit */
|
||||
{
|
||||
if (!wieg->in_code)
|
||||
{
|
||||
wieg->bits = 1;
|
||||
wieg->num = 0;
|
||||
|
||||
wieg->in_code = 1;
|
||||
wieg->code_timeout = 0;
|
||||
|
||||
gpioSetWatchdog(wieg->mygpio_0, wieg->mytimeout);
|
||||
gpioSetWatchdog(wieg->mygpio_1, wieg->mytimeout);
|
||||
}
|
||||
else
|
||||
{
|
||||
wieg->bits++;
|
||||
wieg->num <<= 1;
|
||||
}
|
||||
|
||||
if (gpio == wieg->mygpio_0)
|
||||
{
|
||||
wieg->code_timeout &= 2; /* clear gpio 0 timeout */
|
||||
}
|
||||
else
|
||||
{
|
||||
wieg->code_timeout &= 1; /* clear gpio 1 timeout */
|
||||
wieg->num |= 1;
|
||||
}
|
||||
}
|
||||
else if (level == PI_TIMEOUT)
|
||||
{
|
||||
if (wieg->in_code)
|
||||
{
|
||||
if (gpio == wieg->mygpio_0)
|
||||
{
|
||||
wieg->code_timeout |= 1; /* timeout gpio 0 */
|
||||
}
|
||||
else
|
||||
{
|
||||
wieg->code_timeout |= 2; /* timeout gpio 1 */
|
||||
}
|
||||
|
||||
if (wieg->code_timeout == 3) /* both gpios timed out */
|
||||
{
|
||||
gpioSetWatchdog(wieg->mygpio_0, 0);
|
||||
gpioSetWatchdog(wieg->mygpio_1, 0);
|
||||
|
||||
wieg->in_code = 0;
|
||||
|
||||
(wieg->mycallback)(wieg->bits, wieg->num);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Pi_Wieg_t * Pi_Wieg(
|
||||
int gpio_0,
|
||||
int gpio_1,
|
||||
Pi_Wieg_CB_t callback,
|
||||
int timeout)
|
||||
{
|
||||
/*
|
||||
Instantiate with the gpio for 0 (green wire), the gpio for 1
|
||||
(white wire), the callback function, and the timeout in
|
||||
milliseconds which indicates the end of a code.
|
||||
|
||||
The callback is passed the code length in bits and the value.
|
||||
*/
|
||||
|
||||
Pi_Wieg_t *wieg;
|
||||
|
||||
wieg = malloc(sizeof(Pi_Wieg_t));
|
||||
|
||||
wieg->mygpio_0 = gpio_0;
|
||||
wieg->mygpio_1 = gpio_1;
|
||||
|
||||
wieg->mycallback = callback;
|
||||
|
||||
wieg->mytimeout = timeout;
|
||||
|
||||
wieg->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, _cb, wieg);
|
||||
gpioSetAlertFuncEx(gpio_1, _cb, wieg);
|
||||
|
||||
return wieg;
|
||||
}
|
||||
|
||||
void Pi_Wieg_cancel(Pi_Wieg_t *wieg)
|
||||
{
|
||||
/*
|
||||
Cancel the Wiegand decoder.
|
||||
*/
|
||||
|
||||
if (wieg)
|
||||
{
|
||||
gpioSetAlertFunc(wieg->mygpio_0, 0);
|
||||
gpioSetAlertFunc(wieg->mygpio_1, 0);
|
||||
|
||||
free(wieg);
|
||||
}
|
||||
}
|
||||
|
32
pigpio-master/EXAMPLES/C/WIEGAND_CODE/wiegand.h
Normal file
32
pigpio-master/EXAMPLES/C/WIEGAND_CODE/wiegand.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef WIEGAND_H
|
||||
#define WIEGAND_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*Pi_Wieg_CB_t)(int, uint32_t);
|
||||
|
||||
struct _Pi_Wieg_s;
|
||||
|
||||
typedef struct _Pi_Wieg_s Pi_Wieg_t;
|
||||
|
||||
Pi_Wieg_t *Pi_Wieg(int gpio_0, int gpio_1, Pi_Wieg_CB_t callback, int timeout);
|
||||
/*
|
||||
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.
|
||||
|
||||
A pointer to a private data type is returned. This should be passed
|
||||
to Pi_Wieg_cancel if the decoder is to be cancelled.
|
||||
*/
|
||||
|
||||
void Pi_Wieg_cancel(Pi_Wieg_t *wieg);
|
||||
/*
|
||||
This function releases the resources used by the decoder.
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user