Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <stdlib.h>
00003
00004 #include <pigpio.h>
00005
00006 #include "rotary_encoder.h"
00007
00008 struct _Pi_Renc_s
00009 {
00010 int gpioA;
00011 int gpioB;
00012 Pi_Renc_CB_t callback;
00013 int levA;
00014 int levB;
00015 int lastGpio;
00016 };
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 static void _cb(int gpio, int level, uint32_t tick, void *user)
00035 {
00036 Pi_Renc_t *renc;
00037
00038 renc = user;
00039
00040 if (gpio == renc->gpioA) renc->levA = level; else renc->levB = level;
00041
00042 if (gpio != renc->lastGpio)
00043 {
00044 renc->lastGpio = gpio;
00045
00046 if ((gpio == renc->gpioA) && (level == 1))
00047 {
00048 if (renc->levB) (renc->callback)(1);
00049 }
00050 else if ((gpio == renc->gpioB) && (level == 1))
00051 {
00052 if (renc->levA) (renc->callback)(-1);
00053 }
00054 }
00055 }
00056
00057 Pi_Renc_t * Pi_Renc(int gpioA, int gpioB, Pi_Renc_CB_t callback)
00058 {
00059 Pi_Renc_t *renc;
00060
00061 renc = malloc(sizeof(Pi_Renc_t));
00062
00063 renc->gpioA = gpioA;
00064 renc->gpioB = gpioB;
00065 renc->callback = callback;
00066 renc->levA=0;
00067 renc->levB=0;
00068 renc->lastGpio = -1;
00069
00070 gpioSetMode(gpioA, PI_INPUT);
00071 gpioSetMode(gpioB, PI_INPUT);
00072
00073
00074
00075 gpioSetPullUpDown(gpioA, PI_PUD_UP);
00076 gpioSetPullUpDown(gpioB, PI_PUD_UP);
00077
00078
00079
00080 gpioSetAlertFuncEx(gpioA, _cb, renc);
00081 gpioSetAlertFuncEx(gpioB, _cb, renc);
00082 }
00083
00084 void Pi_Renc_cancel(Pi_Renc_t *renc)
00085 {
00086 if (renc)
00087 {
00088 gpioSetAlertFunc(renc->gpioA, 0);
00089 gpioSetAlertFunc(renc->gpioB, 0);
00090
00091 free(renc);
00092 }
00093 }
00094