Skip to content

Commit

Permalink
port examples to USBHID library
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Feb 4, 2018
1 parent dee9fc9 commit 23e8d1f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 49 deletions.
54 changes: 9 additions & 45 deletions examples/CapacitiveController/CapacitiveController.ino
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
#include <ADCTouchSensor.h>
#include <USBHID.h> // https://github.com/arpruss/USBHID_stm32f1

//
// This requires an stm32f1 board compatible with the no-grounding-pin feature of ADCTouchSensor,
// and this branch of the stm32f1 core: https://github.com/arpruss/Arduino_STM32/tree/addMidiHID
// This requires an stm32f1 board compatible with the no-grounding-pin feature of ADCTouchSensor.
//
// Either in Joystick, Keyboard+Mouse or Keyboard+Mouse+Joystick mode. In the last mode, ground PA10
// to set joystick mode.
//

#define LED_BUILTIN PB12 // change to match your board
#define JOYSTICK_MODE PA10 // ground to set joystick mode

#define NUM_PINS 10
unsigned pins[NUM_PINS] = {PA0,PA1,PA2,PA3,PA4,PA5,PA6,PA7,PA8,PA9};
#if defined(USB_HID_KM) || defined(USB_HID_KMJ)
# define SUPPORT_KEYS
unsigned keys[NUM_PINS] = {' ',KEY_UP_ARROW,KEY_LEFT_ARROW,KEY_DOWN_ARROW,KEY_RIGHT_ARROW,'w','a','s','d','f'}; // Makey-Makey also has 'g' and CLICK, but we don't have enough ADC channels
#endif
#if defined(USB_HID_KMJ) || defined(USB_HID_J)
# define SUPPORT_JOYSTICK
unsigned buttons[NUM_PINS] = { 1, 0, 0, 0, 0, 2, 3, 4, 5, 6 };
#endif
unsigned prev[NUM_PINS];

ADCTouchSensor* sensors[NUM_PINS];

#ifdef SUPPORT_JOYSTICK
int8_t dirX, dirY;
#endif

#ifdef SUPPORT_KEYS
void processPressKeyboard(int i) {
Keyboard.press(keys[i]);
}

void processReleaseKeyboard(int i) {
Keyboard.release(keys[i]);
}
#endif

#ifdef SUPPORT_JOYSTICK
void processPressJoystick(int i) {
if (buttons[i])
Joystick.button(buttons[i], 1);
Expand All @@ -49,25 +35,15 @@ void processReleaseJoystick(int i) {
if (buttons[i])
Joystick.button(buttons[i], 0);
}
#endif

#if defined(SUPPORT_KEYS) && defined(SUPPORT_JOYSTICK)
void (*processPress)(int) = processPressKeyboard;
void (*processRelease)(int) = processReleaseKeyboard;
uint8_t joystickMode = 0;
#elif defined(SUPPORT_KEYS)
# define processPress processPressKeyboard
# define processRelease processReleaseKeyboard
# define joystickMode 0
#else
# define processPress processPressJoystick
# define processRelease processReleaseJoystick
# define joystickMode 1
#endif

void (*processPress)(int) = processPressKeyboard;
void (*processRelease)(int) = processReleaseKeyboard;
uint8_t joystickMode = 0;


void setup()
{
USBHID.begin(HID_KEYBOARD_JOYSTICK);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);

Expand All @@ -79,21 +55,16 @@ void setup()

digitalWrite(LED_BUILTIN, 1);

#ifdef SUPPORT_JOYSTICK
Joystick.setManualReportMode(true);
#ifdef SUPPORT_KEYS
pinMode(JOYSTICK_MODE, INPUT_PULLUP);
#endif
#endif
}


void loop()
{
uint8_t pressed = 0;

#ifndef joystickMode
if (! digitalRead(JOYSTICK_MODE) != joystickMode) {
if (! digitalRead(JOYSTICK_MODE) != joystickMode) {
joystickMode = ! joystickMode;
if (joystickMode) {
processPress = processPressJoystick;
Expand All @@ -104,11 +75,8 @@ void loop()
processRelease = processReleaseKeyboard;
}
}
#endif
#ifdef SUPPORT_JOYSTICK
dirX = 0;
dirY = 0;
#endif

for (int i=0; i<NUM_PINS; i++) {
if (sensors[i]->read() > 25) {
Expand All @@ -117,7 +85,6 @@ void loop()
processPress(i);
prev[i] = 1;
}
#ifdef SUPPORT_JOYSTICK
if (joystickMode && buttons[i] == 0) {
if (i==1)
dirY--;
Expand All @@ -128,7 +95,6 @@ void loop()
else
dirX++;
}
#endif
}
else {
if(prev[i]) {
Expand All @@ -138,7 +104,6 @@ void loop()
}
}

#ifdef SUPPORT_JOYSTICK
if (joystickMode) {
if (dirX < 0)
Joystick.X(0);
Expand All @@ -152,9 +117,8 @@ void loop()
Joystick.Y(1023);
else
Joystick.Y(512);
Joystick.sendManualReport();
Joystick.send();
}
#endif

digitalWrite(LED_BUILTIN, !pressed);
}
Expand Down
10 changes: 7 additions & 3 deletions examples/CapacitivePiano/CapacitivePiano.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <ADCTouchSensor.h>
#define USB_MIDI
#include <USBMIDI.h> // https://github.com/arpruss/USBHID_stm32f1

//
// On the stm32f1, this requires this branch of the stm32f1 core: https://github.com/arpruss/Arduino_STM32/tree/addMidiHID
Expand Down Expand Up @@ -60,7 +62,9 @@ void setup()

#ifndef USB_MIDI
Serial.begin(115200);
#endif
#else
USBMIDI.begin();
#endif

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1^LED_OFF);
Expand All @@ -82,9 +86,9 @@ void setup()
void midiNote(uint8_t status, uint8_t note, uint8_t velocity) {
#ifdef USB_MIDI
if (status == NOTE_ON)
MidiUSB.sendNoteOn(0, note, velocity);
USBMIDI.sendNoteOn(0, note, velocity);
else if (status == NOTE_OFF)
MidiUSB.sendNoteOff(0, note, velocity);
USBMIDI.sendNoteOff(0, note, velocity);
#else
Serial.write(status);
Serial.write(note);
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ADCTouchSensor
version=0.0.7
version=0.0.8
author=Alexander Pruss
maintainer=arpruss <[email protected]>
sentence=Create Touch Sensors with a single analog pin without external hardware
Expand Down

0 comments on commit 23e8d1f

Please sign in to comment.