Initial revision
[sensors.git] / src / pulsewidth.cpp
1 #include <iostream>
2 #include <iomanip>
3 #include <fstream>
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <pigpiod_if2.h>
8
9
10 #define CMP_IN      22    // The signal from the comparator
11 #define NR_SAMPLES  20
12
13 void gpio_callback(int pi, unsigned user_gpio, unsigned level, uint32_t tick, void * userdata);
14
15
16 class DVM
17 {
18    int      pi;          // Connection to pigpio daemon
19    unsigned comparator;  // The signal from the comparator
20
21
22    uint32_t last_tick;
23    uint32_t measure_start;
24
25    uint32_t samples[NR_SAMPLES];
26    int      sample_index;
27
28 public:
29
30    void input_event(unsigned gpio, unsigned level, uint32_t tick)
31    {
32       //printf("Input event: input %u = %u, %d\n", gpio, level, tick - last_tick);
33
34
35       if (gpio == comparator && level == 1)
36       {
37          measure_start = tick;
38       }
39       if (gpio == comparator && level == 0)
40       {
41          samples[sample_index] = tick - measure_start;
42          //printf("   Measured time = %d\n", tick - measure_start);
43          sample_index++;
44          if (sample_index == NR_SAMPLES)
45          {
46             uint32_t min = samples[0];
47             uint32_t max = samples[0];
48             uint32_t sum = 0;
49
50             for (int i = 0; i < NR_SAMPLES; i++)
51             {
52                sum += samples[i];
53                if (samples[i] > max) max = samples[i];
54                if (samples[i] < min) min = samples[i];
55             }
56             printf("Min = %d, Max = %d, Middle = %d, Avg = %d\n", min, max, (min + max) / 2, (sum / NR_SAMPLES) >> 5 << 5);
57
58             sample_index = 0;
59          }
60       }
61       last_tick = tick;
62    }
63
64    
65    DVM(int pi_connection, unsigned cmp)
66    {
67       int open_return;
68
69       pi         = pi_connection;
70       comparator = cmp;
71
72       last_tick = 0;
73       sample_index = 0;
74
75
76       set_mode(pi, comparator, PI_INPUT);
77       set_pull_up_down(pi, comparator, PI_PUD_OFF);
78       set_glitch_filter(pi, comparator, 200);
79
80       open_return = callback_ex(pi, comparator, EITHER_EDGE, gpio_callback, this);
81       fprintf(stderr, "Set callback returns %d.\n", open_return);
82    }
83
84 };
85
86 void gpio_callback(int pi, unsigned user_gpio, unsigned level, uint32_t tick, void * userdata)
87 {
88    DVM *sensor;
89
90    sensor = (DVM *)userdata;
91    sensor->input_event(user_gpio, level, tick);
92 }
93
94
95 int main()
96 {
97    int pi;
98    std::ofstream sensorfile;
99
100    pi = pigpio_start(NULL, NULL);
101    if (pi < 0)
102    {
103               // pigpio initialisation failed.
104       fprintf(stderr, "GPIO initialization failed.\n");
105
106    }
107    else
108    {
109               // pigpio initialised okay.
110       DVM sensor(pi, CMP_IN);
111
112       while (true)
113       {
114          sleep(1);
115          //printf("%.3f\r", sensor.Voltage());
116          //fflush(stdout);
117
118       }
119       pigpio_stop(pi);
120
121    }
122
123 }
124