Advertisement
j0h

PiDraw.cpp

j0h
Jan 23rd, 2022
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. //https://aleksandarhaber.com/incremental-rotary-encoders-with-raspberry-pi-lpd3806-600bm/
  2. /*This project depends on the now depreciated wiring pi project
  3.  * wget https://project-downloads.drogon.net/wiringpi-latest.deb
  4.  * or a working version is also saved the the nanocluster server
  5.  * g++ -Wall -o "%e" "%f" -lwiringPi -lwiringPiDev
  6.  * */
  7. #include <wiringPi.h>
  8. #include <string>
  9. #include <iostream>
  10.  
  11. using namespace std;
  12. long encoder0;
  13. long encoder1;
  14.  
  15. int stateA=0;
  16. int stateB=0;
  17. int angle0=0;
  18. int angle1=0;
  19. int OldAngle0=0;
  20. int OldAngle1=0;
  21.  
  22. //File dataFile;
  23. string dataString = "";
  24.  
  25. //hardware IO
  26. //may require "gpio mode <pin> in"
  27. /*Encoders*/
  28. int wireA=0;  //BCM 17
  29. int wireB=2;  //BCM 27
  30. int wireC=12; //MOSI
  31. int wireD=13; //MISO
  32. /*Buttons*/
  33. const int buttonAB = 7;  //GPIO7 A-B-encoder pin7 "gpio mode 7 in"
  34. const int buttonCD = 21; //GPIO21 (SCLK) C-D encoder "gpio mode 21 in"
  35.  
  36.  
  37.  void A();
  38.  void B();
  39.  void C();
  40.  void D();
  41.  
  42.  void setup();     //sets up hardware may do all setup including fileIO in teh future
  43.  void resetImg();  //sets data in svg poly line to zero
  44.  
  45. int main(){
  46. setup();
  47.  
  48. while(1){
  49. angle0=encoder0;
  50. angle1=encoder1;
  51.         if (digitalRead(buttonAB) == LOW) {
  52.             cout << "Deleting image data\n";
  53.            dataString="";
  54.         delay(100);
  55.         }
  56.         if (digitalRead(buttonCD) == LOW) {
  57.             cout << dataString <<"\n";        
  58.         delay(100);
  59.         }
  60. if (angle0==OldAngle0 && angle1==OldAngle1){
  61.      ; //dont do shit if its not changing
  62. }else{
  63.      dataString += ", ";
  64.      dataString +=  to_string(angle0);
  65.      dataString += ", ";
  66.      dataString += to_string(angle1);
  67.      
  68.      OldAngle0=angle0;
  69.      OldAngle1=angle1;
  70.      cout<<"Angle is:"<<angle0 <<"\t" << angle1 <<endl;
  71.      
  72.      delay(100);  //read less
  73.      }
  74. }
  75.  
  76. return 0;
  77. }
  78.  
  79. void resetImg(){
  80.    
  81.     }
  82.  
  83. void setup(){
  84.      
  85.         wiringPiSetup();
  86.         //isr is an interupt signal, should respond well to random IO
  87.         wiringPiISR(wireA, INT_EDGE_BOTH,&A);
  88.         wiringPiISR(wireB, INT_EDGE_BOTH,&B);
  89.         wiringPiISR(wireC, INT_EDGE_BOTH,&C);
  90.         wiringPiISR(wireD, INT_EDGE_BOTH,&D);
  91.  
  92.         pinMode(buttonAB, INPUT);
  93.         pinMode(buttonCD, INPUT);
  94.      }
  95.  
  96. void A(){
  97.     if (stateA==stateB){
  98.         encoder0++;
  99.     }
  100.     stateA=digitalRead(wireA);
  101.     return;
  102. }
  103.  
  104. void B(){
  105.     if (stateA==stateB){
  106.         encoder0--;
  107.     }
  108.     stateB=digitalRead(wireB);
  109.     return;
  110. }
  111.  
  112. void C(){
  113.     if (stateA==stateB){
  114.         encoder1++;
  115.     }
  116.     stateA=digitalRead(wireA);
  117.     return;
  118. }
  119.  
  120. void D(){
  121.     if (stateA==stateB){
  122.         encoder1--;
  123.     }
  124.     stateB=digitalRead(wireB);
  125.     return;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement