/ M4_Eyes / user_hid.cpp
user_hid.cpp
 1  // SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  #if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
 6  
 7  #include <Arduino.h>
 8  #include "Adafruit_TinyUSB.h"
 9  #include "globals.h"
10  extern volatile uint16_t voiceLastReading, voiceMin, voiceMax; // In pdmvoice.cpp
11  
12  #define UP_BUTTON_KEYCODE_TO_SEND    HID_KEY_U
13  #define A_BUTTON_KEYCODE_TO_SEND     HID_KEY_A
14  #define DOWN_BUTTON_KEYCODE_TO_SEND  HID_KEY_D
15  #define SHAKE_KEYCODE_TO_SEND        HID_KEY_SPACE
16  // Sound reaction REQUIRES '"voice" : true' in config.eye,
17  // even if not using sound output...we're using it to steal
18  // access to the mic here.
19  #define SOUND_KEYCODE_TO_SEND        HID_KEY_SPACE
20  #define SOUND_THRESHOLD              10000
21  
22  // HID report descriptor using TinyUSB's template
23  // Single Report (no ID) descriptor
24  uint8_t const desc_hid_report[] =
25  {
26    TUD_HID_REPORT_DESC_KEYBOARD(),
27  };
28  
29  Adafruit_USBD_HID usb_hid;
30  
31  void user_setup(void) {
32    usb_hid.setPollInterval(20);
33    usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
34  
35    usb_hid.begin();
36  
37    pinMode(LED_BUILTIN, OUTPUT);
38    digitalWrite(LED_BUILTIN, LOW);
39    arcada.accel.setClick(1, 50);
40    
41    // wait until device mounted
42    while( !USBDevice.mounted() ) delay(1);
43  }
44  
45  void user_loop(void) {
46    if ( !usb_hid.ready() ) {
47      Serial.println("not ready");
48      return;
49    }
50  
51    uint8_t keycode[6] = { 0 };
52  
53    uint32_t buttonState = arcada.readButtons();
54    if (buttonState & ARCADA_BUTTONMASK_UP) {
55      Serial.println("Up");
56      keycode[0] = UP_BUTTON_KEYCODE_TO_SEND;
57    }
58    if (buttonState & ARCADA_BUTTONMASK_A) {
59      Serial.println("A");
60      keycode[1] = A_BUTTON_KEYCODE_TO_SEND;
61    }
62    if (buttonState & ARCADA_BUTTONMASK_DOWN) {
63      Serial.println("Down");
64      keycode[2] = DOWN_BUTTON_KEYCODE_TO_SEND;
65    }
66  
67    uint8_t shake = arcada.accel.getClick();
68    if (shake & 0x30) {
69      Serial.print("shake detected (0x"); Serial.print(shake, HEX); Serial.print("): ");
70      if (shake & 0x10) Serial.println(" single shake");
71      keycode[3] = SHAKE_KEYCODE_TO_SEND;
72    }
73  
74    uint16_t p2p = voiceMax - voiceMin; // Max peak-to-peak since last reading
75    if(p2p) { // usu. non-zero if voice changer enabled
76      if(p2p > SOUND_THRESHOLD) {
77        Serial.println("Sound");
78        keycode[4] = SOUND_KEYCODE_TO_SEND;
79      }
80    }
81    voiceMin = voiceMax = 32768; // Reset p2p range for next pass
82  
83    bool anypressed = false;
84    for (int k=0; k<sizeof(keycode); k++) {
85      if (keycode[k] != 0) {
86        anypressed = true;
87        break;
88      }
89    }
90    if (anypressed) {
91      digitalWrite(LED_BUILTIN, HIGH);
92      usb_hid.keyboardReport(0, 0, keycode);
93    } else {
94      digitalWrite(LED_BUILTIN, LOW);   
95      usb_hid.keyboardRelease(0);  
96    }
97  }
98  
99  #endif