Skip to content

Instantly share code, notes, and snippets.

@felipemanga
Created November 27, 2018 00:33
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 felipemanga/8ea06daed446a3f2f29de8d967e6a393 to your computer and use it in GitHub Desktop.
Save felipemanga/8ea06daed446a3f2f29de8d967e6a393 to your computer and use it in GitHub Desktop.
Aseprite keypad
#include <Keyboard.h>
struct Binding {
int pin;
const char *seq;
int state;
bool type;
};
const char cf[] = {0x80, 'f', 0};
Binding alts[][4] = {
// white
{
{ 9, "\xb3" }, // btn white = Tab
{ 6, cf }, // btn yellow = ctrl+shift+f
{ 0, "\xC8" }, // btn green = preview
{ 2, "\x82\xb3" } // btn red = alt-tab
},
// yellow
{
{ 9, "\x80=" }, // btn white = zoom +
{ 6, "e" }, // btn yellow = eraser
{ 0, "i" }, // btn green = eyedropper
{ 2, "\x80-" } // btn red = zoom -
},
// green
{
{ 9, "=" }, // btn white = brush +
{ 6, "g" }, // btn yellow = eraser
{ 0, "b" }, // btn green = brush
{ 2, "-" } // btn red = Ctrl
},
// red
{
{ 9, "\x80y" }, // btn white = Ctrl+Y
{ 6, "killall palmreject.sh\xb0nohup ~/palmreject.sh &\xb0", 0, 1 }, // btn yellow = eraser
{ 0, "b" }, // btn green = brush
{ 2, "\x80z" } // btn red = Ctrl+Z
}
};
Binding bindings[] = {
{ 9, "\xb3" }, // btn white = Tab
{ 6, "e" }, // btn yellow = eraser
{ 0, "b" }, // btn green = brush
{ 2, "\x80z" } // btn red = Ctrl+Z
};
const int bindCount = sizeof(bindings) / sizeof(bindings[0]);
int altnum, ignore, lastPress;
void setup()
{
altnum = -1;
ignore = 0;
for( int i=0; i<bindCount; ++i ){
Binding &bind = bindings[i];
pinMode( bind.pin, INPUT ); // Set the button as an input
digitalWrite( bind.pin, HIGH ); // Pull the button high
bind.state = 0;
}
Keyboard.begin();
pinMode( 1, OUTPUT );
pinMode( 10, OUTPUT );
digitalWrite( 1, HIGH );
delay(500);
digitalWrite( 1, LOW );
}
void shortcut( const char *str ){
int l=0;
while( *str ){
l++;
Keyboard.press( *str++ );
delay(20);
}
while( l-- ){
str--;
Keyboard.release(*str);
delay(20);
}
delay(100);
}
void type( const char *str ){
while( *str ){
digitalWrite( 1, HIGH );
Keyboard.press( *str );
delay(20);
digitalWrite( 1, LOW);
digitalWrite( 10, HIGH );
Keyboard.release( *str++ );
delay(20);
digitalWrite( 1, LOW);
}
}
void loop()
{
int pressCount = 0;
for( int i=0; i<bindCount; ++i ){
Binding *bind = &bindings[i];
int state = digitalRead(bind->pin) == 0;
pressCount += state;
if( state == bind->state )
continue;
bind->state = state;
if( state ){
lastPress = i;
if( altnum == -1 ){
altnum = i;
digitalWrite( 1, HIGH );
delay(100);
digitalWrite( 1, LOW );
}
ignore = 0;
continue;
}
if( ignore )
continue;
digitalWrite( 10, HIGH );
ignore = 1;
bind = &alts[altnum][lastPress];
if( bind->type )
type( bind->seq );
else
shortcut( bind->seq );
digitalWrite( 10, LOW );
}
if( pressCount == 0 )
altnum = -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment