Commit 20377810 authored by Martin Natano's avatar Martin Natano

initial commi

parents
#include "HID.h"
static const uint8_t hidReportDescriptor[] PROGMEM = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x05, // USAGE (Game Pad)
0xa1, 0x01, // COLLECTION (Application)
0xa1, 0x00, // COLLECTION (Physical)
0x85, 0x03, // REPORT_ID (3)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x06, // USAGE_MAXIMUM (Button 6)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x06, // REPORT_COUNT (6)
0x81, 0x02, // INPUT (Data, Var, Abs)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x02, // REPORT_COUNT (2)
0x01, 0x01, // INPUT (Constant)
0xc0, // END_COLLECTION
0xc0, // END_COLLECTION
};
#define NBUTTONS 6
static const int button_pins[NBUTTONS] = {7, 8, 9, 10, 11, 12};
void setup() {
int i;
pinMode(LED_BUILTIN, OUTPUT);
for (i = 0; i < NBUTTONS; i++)
pinMode(button_pins[i], INPUT_PULLUP);
static HIDSubDescriptor node(hidReportDescriptor, sizeof(hidReportDescriptor));
HID().AppendDescriptor(&node);
}
// the loop function runs over and over again forever
void loop() {
uint8_t reportData;
static uint8_t previousReportData;
int i;
reportData = 0;
for (i = 0; i < NBUTTONS; i++) {
int pressed = !digitalRead(button_pins[i]);
if (pressed)
reportData |= (1 << i);
}
if (reportData != previousReportData) {
HID().SendReport(0x03, &reportData, sizeof(reportData));
if (reportData)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
previousReportData = reportData;
}
delayMicroseconds(1000);
}
#include "Keyboard.h"
#define NBUTTONS 4
const int button_pins[NBUTTONS] = {9, 10, 11, 12};
const int button_keys[NBUTTONS] = {KEY_LEFT_ARROW, KEY_DOWN_ARROW, KEY_UP_ARROW, KEY_RIGHT_ARROW};
int button_states;
void setup() {
int i;
pinMode(LED_BUILTIN, OUTPUT);
for (i = 0; i < NBUTTONS; i++)
pinMode(button_pins[i], INPUT_PULLUP);
Keyboard.begin();
Serial.println("hello");
}
// the loop function runs over and over again forever
void loop() {
int i;
for (i = 0; i < NBUTTONS; i++) {
int pressed = !digitalRead(button_pins[i]);
//Serial.println(pressed);
if (pressed && ((1 << i) & button_states) == 0) {
//Serial.print("button pressed: ");
//Serial.println(i);
Keyboard.press(button_keys[i]);
button_states |= (1 << i);
} else if (!pressed && ((1 << i) & button_states) != 0) {
Keyboard.release(button_keys[i]);
//Serial.print("button released: ");
//Serial.println(i);
button_states &= ~(1 << i);
}
}
if (button_states)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment