Go to the documentation of this file.00001 #include <stdio.h>
00002
00003 #include <pigpio.h>
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define GPIO 25
00023
00024 #define MAX_READING 1000
00025
00026 static uint32_t rechargeTick = 0;
00027
00028 void callback(int gpio, int level, uint32_t tick)
00029 {
00030 static uint32_t smooth = 0;
00031 static int reading = 0;
00032
00033 uint32_t raw;
00034
00035 if (level == 1)
00036 {
00037 ++reading;
00038
00039 if (rechargeTick)
00040 {
00041 raw = tick - rechargeTick;
00042
00043 if (raw < MAX_READING)
00044 {
00045
00046
00047 smooth = (raw + (4 * smooth)) / 5;
00048
00049 printf("%d %d %d\n", reading, raw, smooth);
00050 }
00051 else
00052 {
00053
00054 printf("%d %d %d\n", reading, 40, smooth);
00055 }
00056 }
00057 else
00058 {
00059
00060 printf("%d %d %d\n", reading, 20, smooth);
00061 }
00062 }
00063 }
00064
00065 int main (int argc, char *argv[])
00066 {
00067 uint32_t t1, t2;
00068 int tDiff;
00069
00070 if (gpioInitialise()<0) return 1;
00071
00072 gpioSetAlertFunc(GPIO, callback);
00073
00074 while (1)
00075 {
00076 gpioWrite(GPIO, PI_OFF);
00077
00078 gpioDelay(200);
00079
00080 t1 = gpioTick();
00081
00082 gpioSetMode(GPIO, PI_INPUT);
00083
00084 t2 = gpioTick();
00085
00086
00087
00088 if ((t2 - t1) < 3) rechargeTick = t1; else rechargeTick = 0;
00089
00090 gpioDelay(5000);
00091 }
00092
00093 gpioTerminate();
00094 }