CapTouchKeyboard.ino
  1  // SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  ////////////////////////////////////////////////////////////////////////////
  6  // Circuit Playground Capacitive Touch Tones
  7  //
  8  // Send keyboard and mouse events for each touch pad.
  9  // https://www.arduino.cc/en/Reference/MouseKeyboard
 10  //
 11  // Author: Carter Nelson
 12  // MIT License (https://opensource.org/licenses/MIT)
 13  
 14  #include <Adafruit_CircuitPlayground.h>
 15  #include <Mouse.h>
 16  #include <Keyboard.h>
 17  
 18  #define CAP_THRESHOLD   50
 19  #define DEBOUNCE        250
 20  
 21  uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
 22  uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);
 23  
 24  boolean emulatorActive = false;
 25  
 26  ////////////////////////////////////////////////////////////////////////////
 27  void takeAction(uint8_t pad) {
 28    Serial.print("PAD "); Serial.println(pad);
 29    switch (pad) {
 30      case 3:
 31        sendKey(KEY_LEFT_ARROW);
 32        break;
 33      case 2:
 34        sendKey(KEY_UP_ARROW);
 35        break;
 36      case 0:
 37        sendKey(KEY_DOWN_ARROW);
 38        break;
 39      case 1:
 40        sendKey(KEY_RIGHT_ARROW);
 41        break;
 42      case 12:
 43        sendKey(' ');
 44        break;
 45      case 6:
 46        sendMouse(MOUSE_LEFT);
 47        break;
 48      case 9:
 49        sendMouse(MOUSE_MIDDLE);
 50        break;
 51      case 10:
 52        sendMouse(MOUSE_RIGHT);
 53        break;
 54      default:
 55        Serial.println("THIS SHOULD NEVER HAPPEN.");
 56    }
 57  }
 58  
 59  ////////////////////////////////////////////////////////////////////////////
 60  boolean capButton(uint8_t pad) {
 61    // Check if capacitive touch exceeds threshold.
 62    if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
 63      return true;  
 64    } else {
 65      return false;
 66    }
 67  }
 68  
 69  ////////////////////////////////////////////////////////////////////////////
 70  void sendKey(char key) {
 71    Keyboard.press(key);
 72    Keyboard.releaseAll();
 73  }
 74  
 75  ////////////////////////////////////////////////////////////////////////////
 76  void sendMouse(char key) {
 77    Mouse.click(key);
 78  }
 79  
 80  ////////////////////////////////////////////////////////////////////////////
 81  void setup() {
 82    // Initialize serial.
 83    Serial.begin(9600); 
 84    
 85    // Initialize Circuit Playground library.
 86    CircuitPlayground.begin();
 87  }
 88  
 89  ////////////////////////////////////////////////////////////////////////////
 90  void loop() {
 91    // Indicate emulator status on red LED.
 92    CircuitPlayground.redLED(emulatorActive);
 93  
 94    // Check slide switch.
 95    if (!CircuitPlayground.slideSwitch()) {
 96  
 97      //-----------| EMULATOR OFF |-------------
 98      if (emulatorActive) {
 99        Keyboard.end();
100        Mouse.end();
101        emulatorActive = false;
102      }
103      
104    } else {
105  
106      //-----------| EMULATOR ON |-------------
107      if (!emulatorActive) {
108        Keyboard.begin();
109        Mouse.begin();
110        emulatorActive = true;
111      } 
112   
113      // Loop over every pad.
114      for (int i=0; i<numberOfPads; i++) {
115        
116        // Check if pad is touched.
117        if (capButton(pads[i])) {
118          
119          // Do something.
120          takeAction(pads[i]);
121          
122          // But not too often.
123          delay(DEBOUNCE);
124        }
125      }
126    }
127  }