collab/pb-hal

git clone https://git.julia.blue/collab/pb-hal

Rahul ButaniRestructuringce16264

imu
1.7 KiB82 linesraw
1//
2//  qe.c
3//  hidtest
4//
5//  Created by Albin Stigö on 09/09/2017.
6//  Copyright © 2017 Albin Stigo. All rights reserved.
7//
8//  License BSD
9//  https://opensource.org/licenses/BSD-2-Clause
10
11
12#include <stdio.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <stdbool.h>
16#include <errno.h>
17#include <string.h>
18#include <wiringPi.h>
19
20#define PIN1 4
21#define PIN2 5
22
23volatile long pos;
24volatile uint8_t state;
25
26// Inspired by Paul Stoffregen's excellent Arduino library Encoder:
27// https://github.com/PaulStoffregen/Encoder
28void pin_isr(void) {
29    uint8_t p1val = digitalRead(PIN1);
30    uint8_t p2val = digitalRead(PIN2);
31    uint8_t s = state & 3;
32    if (p1val) s |= 4;
33    if (p2val) s |= 8;
34    state = (s >> 2);
35
36    switch (s) {
37        case 1: case 7: case 8: case 14:
38            pos++;
39            return;
40        case 2: case 4: case 11: case 13:
41            pos--;
42            return;
43        case 3: case 12:
44            pos += 2;
45            return;
46        case 6: case 9:
47            pos -= 2;
48            return;
49    }
50}
51
52int main(int argc, const char * argv[]) {
53
54    // sets up the wiringPi library
55    if (wiringPiSetup() < 0) {
56        perror("wiringPiSetup");
57        exit(EXIT_FAILURE);
58    }
59
60    pinMode (PIN1,  INPUT) ;
61    pinMode (PIN2,  INPUT) ;
62    pullUpDnControl(PIN1, PUD_UP);
63    pullUpDnControl(PIN2, PUD_UP);
64
65    if ( wiringPiISR (PIN1, INT_EDGE_BOTH, &pin_isr) < 0 ) {
66        perror("wiringPiISR");
67        exit(EXIT_FAILURE);
68    }
69
70    if ( wiringPiISR (PIN2, INT_EDGE_BOTH, &pin_isr) < 0 ) {
71        perror("wiringPiISR");
72        exit(EXIT_FAILURE);
73    }
74
75    // Show position every second
76    while ( 1 ) {
77        printf( "%ld\n", pos);
78        delay( 1000 ); // wait 1 second
79    }
80
81    return 0;
82}