collab/pb-hal
git clone https://git.julia.blue/collab/pb-hal
ce16264
imu
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 ) { 29uint8_t p1val = digitalRead (PIN1 ); 30uint8_t p2val = digitalRead (PIN2 ); 31uint8_t s = state & 3 ; 32if (p1val ) { 33s |=4 ; 34 } 35if (p2val ) { 36s |=8 ; 37 } 38state = (s >>2 ); 39 40switch (s ) { 41case 1 : 42case 7 : 43case 8 : 44case 14 : 45pos ++ ; 46return ; 47case 2 : 48case 4 : 49case 11 : 50case 13 : 51pos -- ; 52return ; 53case 3 : 54case 12 : 55pos += 2 ; 56return ; 57case 6 : 58case 9 : 59pos -= 2 ; 60return ; 61 } 62} 63 64static PyObject * init_stuff (PyObject * self ,PyObject * args ) { 65if (wiringPiSetup ()< 0 ) { 66return Py_BuildValue ("i" ,-1 ); 67 } 68 69pinMode (PIN1 ,INPUT ) ; 70pinMode (PIN2 ,INPUT ) ; 71pullUpDnControl (PIN1 ,PUD_UP ); 72pullUpDnControl (PIN2 ,PUD_UP ); 73 74if (wiringPiISR (PIN1 ,INT_EDGE_BOTH ,& pin_isr )< 0 ) { 75return Py_BuildValue ("i" ,-1 ); 76 } 77 78if (wiringPiISR (PIN2 ,INT_EDGE_BOTH ,& pin_isr )< 0 ) { 79return Py_BuildValue ("i" ,-1 ); 80 } 81} 82 83static PyObject * getTicks (PyObject * self ,PyObject * args ) { 84return 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//