collab/pb-hal

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

Rahul ButaniRestructuringce16264

imu
1.8 KiB96 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#include <Python.h>
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) {
33        s |= 4;
34    }
35    if (p2val) {
36        s |= 8;
37    }
38    state = (s >> 2);
39
40    switch (s) {
41    case 1:
42    case 7:
43    case 8:
44    case 14:
45        pos++;
46        return;
47    case 2:
48    case 4:
49    case 11:
50    case 13:
51        pos--;
52        return;
53    case 3:
54    case 12:
55        pos += 2;
56        return;
57    case 6:
58    case 9:
59        pos -= 2;
60        return;
61    }
62}
63
64static PyObject* init_stuff(PyObject* self, PyObject* args) {
65    if (wiringPiSetup() < 0) {
66        return Py_BuildValue("i", -1);
67    }
68
69    pinMode (PIN1,  INPUT) ;
70    pinMode (PIN2,  INPUT) ;
71    pullUpDnControl(PIN1, PUD_UP);
72    pullUpDnControl(PIN2, PUD_UP);
73
74    if ( wiringPiISR (PIN1, INT_EDGE_BOTH, &pin_isr) < 0 ) {
75        return Py_BuildValue("i", -1);
76    }
77
78    if ( wiringPiISR (PIN2, INT_EDGE_BOTH, &pin_isr) < 0 ) {
79        return Py_BuildValue("i", -1);
80    }
81}
82
83static PyObject* getTicks(PyObject* self, PyObject* args) {
84    return Py_BuildValue("i", pos);
85}
86
87// int main(int argc, const char* argv[]) {
88//     // Show position every second
89//     while ( 1 ) {
90//         printf( "%ld\n", pos);
91//         delay( 1000 ); // wait 1 second
92//     }
93//
94//     return 0;
95// }
96//