Skip to content

Instantly share code, notes, and snippets.

@obono
Last active April 13, 2021 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obono/c797e8473a3868b8b589ec56dcd77d09 to your computer and use it in GitHub Desktop.
Save obono/c797e8473a3868b8b589ec56dcd77d09 to your computer and use it in GitHub Desktop.
A foot pedal using ATtiny85 as HID keyboard device.
/**
* ATtiny85FootPedal
*
* Copyright (c) 2021 OBONO
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <DigiKeyboard.h>
// https://github.com/digistump/DigisparkArduinoIntegration/tree/master/libraries/DigisparkKeyboard
#define PIN_LEFT_PEDAL 0
#define PIN_RIGHT_PEDAL 1
#define PIN_MODE_CHANGE A0
#define MODE_MAX 5
#define PRESSING_LONG 10
#define LOOP_DELAY 50
#define ANALOG_THETA 768
#define KEY_TAB 43
#define KEY_PAGE_UP 75
#define KEY_PAGE_DOWN 78
#define KEY_RIGHT 79
#define KEY_LEFT 80
#define KEY_DOWN 81
#define KEY_UP 82
static int mode, pressingCount;
static boolean isModeChanging, isLastModePressed;
PROGMEM static const byte keyTable[MODE_MAX][5] = {
{ KEY_LEFT, 0, KEY_RIGHT, 0, true },
{ KEY_PAGE_UP, 0, KEY_PAGE_DOWN, 0, true },
{ KEY_LEFT, MOD_ALT_LEFT, KEY_RIGHT, MOD_ALT_LEFT, false },
{ KEY_TAB, 0, KEY_ENTER, 0, false },
{ KEY_C, MOD_CONTROL_LEFT, KEY_V, MOD_CONTROL_LEFT, false },
};
void setup()
{
pinMode(PIN_LEFT_PEDAL, INPUT_PULLUP);
pinMode(PIN_RIGHT_PEDAL, INPUT_PULLUP);
pinMode(PIN_MODE_CHANGE, INPUT);
mode = 0;
pressingCount = 0;
isModeChanging = false;
isLastModePressed = false;
DigiKeyboard.sendKeyStroke(0);
}
void loop()
{
boolean isLeftPressed = (digitalRead(PIN_LEFT_PEDAL) == LOW);
boolean isRightPressed = (digitalRead(PIN_RIGHT_PEDAL) == LOW);
boolean isModePressed = (analogRead(PIN_MODE_CHANGE) < ANALOG_THETA);
boolean isRepatable = pgm_read_byte(&keyTable[mode][4]);
if (isLeftPressed == isRightPressed) {
pressingCount = 0;
} else {
if (pressingCount < PRESSING_LONG) pressingCount++;
if (pressingCount == 1 || (isRepatable && pressingCount == PRESSING_LONG)) {
const byte *p = &keyTable[mode][(isLeftPressed) ? 0 : 2];
DigiKeyboard.sendKeyStroke(pgm_read_byte(p), pgm_read_byte(p + 1));
isModeChanging = false;
}
}
if (isModePressed && !isLastModePressed) {
mode = (isModeChanging) ? (mode + 1) % MODE_MAX : 0;
isModeChanging = true;
}
isLastModePressed = isModePressed;
delay(LOOP_DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment