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 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 )s |=4 ; 33if (p2val )s |=8 ; 34state = (s >>2 ); 35 36switch (s ) { 37case 1 :case 7 :case 8 :case 14 : 38pos ++ ; 39return ; 40case 2 :case 4 :case 11 :case 13 : 41pos -- ; 42return ; 43case 3 :case 12 : 44pos += 2 ; 45return ; 46case 6 :case 9 : 47pos -= 2 ; 48return ; 49 } 50} 51 52int main (int argc ,const char * argv []) { 53 54// sets up the wiringPi library 55if (wiringPiSetup ()< 0 ) { 56perror ("wiringPiSetup" ); 57exit (EXIT_FAILURE ); 58 } 59 60pinMode (PIN1 ,INPUT ) ; 61pinMode (PIN2 ,INPUT ) ; 62pullUpDnControl (PIN1 ,PUD_UP ); 63pullUpDnControl (PIN2 ,PUD_UP ); 64 65if (wiringPiISR (PIN1 ,INT_EDGE_BOTH ,& pin_isr )< 0 ) { 66perror ("wiringPiISR" ); 67exit (EXIT_FAILURE ); 68 } 69 70if (wiringPiISR (PIN2 ,INT_EDGE_BOTH ,& pin_isr )< 0 ) { 71perror ("wiringPiISR" ); 72exit (EXIT_FAILURE ); 73 } 74 75// Show position every second 76while (1 ) { 77printf ("%ld\n" ,pos ); 78delay (1000 );// wait 1 second 79 } 80 81return 0 ; 82}