-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotaryEncoder.c
72 lines (60 loc) · 1.31 KB
/
rotaryEncoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**********************************************************************
* Filename : rotaryEncoder.c
* Description : make a rotaryEncoder work.
* Author : Robot
* E-mail : [email protected]
* website : www.sunfounder.com
* Date : 2014/08/27
**********************************************************************/
#include <stdio.h>
#include "rotaryEncoder.h"
volatile globalCounter = 0;
void rotaryDeal()
{
Last_RoB_Status = digitalRead(RoBPin);
while(!digitalRead(RoAPin)){
Current_RoB_Status = digitalRead(RoBPin);
flag = 1;
}
if(flag == 1){
flag = 0;
if((Last_RoB_Status == 0)&&(Current_RoB_Status == 1)){
globalCounter++;
printf("%d", globalCounter);
}
if((Last_RoB_Status == 1)&&(Current_RoB_Status == 0)){
globalCounter--;
printf("%d", globalCounter);
}
}
}
void rotaryClear()
{
if(digitalRead(RoSPin) == 0)
{
globalCounter = 0;
delay(300);
}
}
int get()
{
return globalCounter;
}
void *rotary(void *param)
{
if(wiringPiSetup() < 0){
fprintf(stderr, "Unable to setup wiringPi:%s\n",strerror(errno));
return NULL;
}
int *counter = (int *) param;
pinMode(RoAPin, INPUT);
pinMode(RoBPin, INPUT);
pinMode(RoSPin, INPUT);
pullUpDnControl(RoSPin, PUD_UP);
while(1){
rotaryDeal(counter);
rotaryClear(counter);
}
printf("\nEND\n");
return NULL;
}