Created
November 16, 2017 19:40
-
-
Save colesnicov/360b6c140ef0d54ac2de12c410880a79 to your computer and use it in GitHub Desktop.
Rotary encoder for RPi 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************** | |
* Description : Rotacni enkoder pro Raspberry PI 2 | |
* Author : Denis Colesnicov | |
* E-mail : eugustus@gmail.com | |
* Notice : Original code https://www.sunfounder.com/learn/Super_Kit_V2_for_RaspberryPi/lesson-8-rotary-encoder-super-kit-for-raspberrypi.html | |
* Date : 2017/11/17 | |
**********************************************************************/ | |
/********************************************************************** | |
************************* TODO *************************************** | |
* | |
* - Pridat funkci swRelease | |
* | |
*********************************************************************/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
#include <wiringPi.h> | |
#define RoAPin 0 | |
#define RoBPin 1 | |
#define RoSPin 2 | |
class RotaryEncoder{ | |
public: | |
RotaryEncoder(int clk, int data, int sw, unsigned int sw_delay = 200){ | |
pinCLK = clk; | |
pinData = data; | |
pinSw = sw; | |
swDelay = sw_delay; | |
lastStatus= false; | |
tick = false; | |
resetAll(); | |
pinMode(pinCLK, INPUT); | |
pinMode(pinData, INPUT); | |
pinMode(pinSw, INPUT); | |
pullUpDnControl(pinSw, PUD_UP); | |
} | |
void update(){ | |
lastStatus = digitalRead(pinData); | |
bool currentStatus; | |
while(!digitalRead(pinCLK)){ | |
currentStatus = digitalRead(pinData); | |
tick = true; | |
} | |
if(tick){ | |
tick = false; | |
if((!lastStatus)&&(currentStatus)){ | |
counter ++; | |
} | |
if((lastStatus)&&(!currentStatus)){ | |
counter --; | |
} | |
} | |
if(digitalRead(pinSw) == 0) | |
{ | |
clicked = true; | |
delay(swDelay); | |
} | |
} | |
void resetSw(){ | |
clicked = false; | |
} | |
void resetRotary(){ | |
counter = 0; | |
} | |
void resetAll(){ | |
clicked = false; | |
counter = 0; | |
} | |
int getRotary(){ | |
return counter; | |
} | |
bool isClicked(){ | |
return clicked; | |
} | |
private: | |
bool clicked; | |
int counter; | |
bool tick; | |
bool lastStatus; | |
int pinCLK; | |
int pinData; | |
int pinSw; | |
unsigned int swDelay; | |
}; | |
int main(void) | |
{ | |
if(wiringPiSetup() < 0){ | |
fprintf(stderr, "Unable to setup wiringPi:%s\n",strerror(errno)); | |
return 1; | |
} | |
RotaryEncoder re(RoAPin, RoBPin, RoSPin); | |
int lastRE = re.getRotary(); | |
while(1){ | |
re.update(); | |
int newRE = re.getRotary(); | |
if(lastRE != newRE){ | |
printf("rotary : %d\n",re.getRotary()); | |
lastRE = newRE; | |
} | |
bool click = re.isClicked(); | |
if(click){ | |
printf("click : %d\n",click); | |
re.resetSw(); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment