Guest User

Untitled

a guest
Dec 10th, 2020
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <M5Core2.h>
  2.  
  3. #define Faces_Encoder_I2C_ADDR     0X5E
  4.  
  5.  
  6. int encoder_increment;
  7. int encoder_direction;
  8. int button_state;
  9.  
  10.  
  11. void setLed(int i, int r, int g, int b){
  12.     Wire.beginTransmission(Faces_Encoder_I2C_ADDR);
  13.     Wire.write(i);
  14.     Wire.write(r);
  15.     Wire.write(g);
  16.     Wire.write(b);
  17.     Wire.endTransmission();
  18. }
  19.  
  20.  
  21. void getEncoderIncrement(void) {
  22.     int temp_encoder_increment;
  23.  
  24.     Wire.requestFrom(Faces_Encoder_I2C_ADDR, 3);
  25.     if (Wire.available()) {
  26.        temp_encoder_increment = Wire.read(); // get increment
  27.        button_state = Wire.read(); // get button value
  28.     }
  29.     if (temp_encoder_increment > 127) {
  30.         //anti-clockwise
  31.         encoder_direction = 1;
  32.         encoder_increment = 256 - temp_encoder_increment;
  33.     } else{
  34.         // clockwise
  35.         encoder_direction = 0;
  36.         encoder_increment = temp_encoder_increment;
  37.     }
  38. }
  39.  
  40.  
  41. void setup() {
  42.   M5.begin();
  43.   Wire.begin();
  44.   M5.Lcd.setTextFont(2);
  45.   for (int i = 0; i < 12; i++) {
  46.     setLed(i, 200, 100, 0);
  47.   }
  48. }
  49.  
  50.  
  51. void loop() {
  52.   uint8_t lines = 0;
  53.  
  54.   while(1) {
  55.     // get status from encoder
  56.     getEncoderIncrement();
  57.  
  58.     // print button/encoder status
  59.     M5.Lcd.print("encoder: ");
  60.     M5.Lcd.print(encoder_increment);
  61.     M5.Lcd.print("   dir: ");
  62.     if (encoder_direction) {
  63.       M5.Lcd.print("cw");
  64.     } else {
  65.       M5.Lcd.print("ccw");
  66.     }
  67.     M5.Lcd.print("   button: ");
  68.     M5.Lcd.println(button_state);
  69.     delay(333);
  70.  
  71.     // clear screen after 15 lines
  72.     lines++;
  73.     if (lines >= 14) {
  74.       M5.Lcd.fillScreen(0);
  75.       M5.Lcd.setCursor(0, 0);
  76.       lines = 0;
  77.     }
  78.   }
  79. }
RAW Paste Data