Advertisement
Guest User

Tiny85 Volume Control With Mute on p1

a guest
Sep 1st, 2017
2,487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include "TrinketHidCombo.h"
  2.  
  3. #define PIN_ENCODER_A 0
  4. #define PIN_ENCODER_B 2
  5. #define PIN_BUTTON 1
  6. #define TRINKET_PINx  PINB
  7.  
  8. static uint8_t enc_prev_pos = 0;
  9. static uint8_t enc_flags    = 0;
  10.  
  11. void setup()
  12. {
  13.   // set pins as input with internal pull-up resistors enabled
  14.   pinMode(PIN_ENCODER_A, INPUT);
  15.   pinMode(PIN_ENCODER_B, INPUT);
  16.   pinMode(PIN_BUTTON, INPUT);
  17.   digitalWrite(PIN_ENCODER_A, HIGH);
  18.   digitalWrite(PIN_ENCODER_B, HIGH);
  19.  
  20.   TrinketHidCombo.begin(); // start the USB device engine and enumerate
  21.  
  22.   // get an initial reading on the encoder pins
  23.   if (digitalRead(PIN_ENCODER_A) == LOW) {
  24.     enc_prev_pos |= (1 << 0);
  25.   }
  26.   if (digitalRead(PIN_ENCODER_B) == LOW) {
  27.     enc_prev_pos |= (1 << 1);
  28.   }
  29. }
  30.  
  31. void loop()
  32. {
  33.   int8_t enc_action = 0; // 1 or -1 if moved, sign is direction
  34.  
  35.   // note: for better performance, the code will now use
  36.   // direct port access techniques
  37.   // http://www.arduino.cc/en/Reference/PortManipulation
  38.   uint8_t enc_cur_pos = 0;
  39.   // read in the encoder state first
  40.   if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
  41.     enc_cur_pos |= (1 << 0);
  42.   }
  43.   if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
  44.     enc_cur_pos |= (1 << 1);
  45.   }
  46.  
  47.   // if any rotation at all
  48.   if (enc_cur_pos != enc_prev_pos)
  49.   {
  50.     if (enc_prev_pos == 0x00)
  51.     {
  52.       // this is the first edge
  53.       if (enc_cur_pos == 0x01) {
  54.         enc_flags |= (1 << 0);
  55.       }
  56.       else if (enc_cur_pos == 0x02) {
  57.         enc_flags |= (1 << 1);
  58.       }
  59.     }
  60.  
  61.     if (enc_cur_pos == 0x03)
  62.     {
  63.       // this is when the encoder is in the middle of a "step"
  64.       enc_flags |= (1 << 4);
  65.     }
  66.     else if (enc_cur_pos == 0x00)
  67.     {
  68.       // this is the final edge
  69.       if (enc_prev_pos == 0x02) {
  70.         enc_flags |= (1 << 2);
  71.       }
  72.       else if (enc_prev_pos == 0x01) {
  73.         enc_flags |= (1 << 3);
  74.       }
  75.  
  76.       // check the first and last edge
  77.       // or maybe one edge is missing, if missing then require the middle state
  78.       // this will reject bounces and false movements
  79.       if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
  80.         enc_action = 1;
  81.       }
  82.       else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
  83.         enc_action = 1;
  84.       }
  85.       else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
  86.         enc_action = -1;
  87.       }
  88.       else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
  89.         enc_action = -1;
  90.       }
  91.  
  92.       enc_flags = 0; // reset for next time
  93.     }
  94.   }
  95.  
  96.   enc_prev_pos = enc_cur_pos;
  97.  
  98.   if (digitalRead(PIN_BUTTON == HIGH)){
  99.     TrinketHidCombo.pressMultimediaKey(MMKEY_MUTE);
  100.     while(digitalRead(PIN_BUTTON == HIGH)){
  101.      
  102.     }
  103.   }
  104.   if (enc_action > 0) {
  105.     TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_UP);
  106.   }
  107.   else if (enc_action < 0) {
  108.     TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_DOWN);
  109.   }
  110.   else {
  111.     TrinketHidCombo.poll(); // do nothing, check if USB needs anything done
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement