Last active
January 26, 2023 22:54
-
-
Save XianThi/18edc4d22b9c001e4a5b478346972e89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint8_t buf[8] = { | |
0 }; /* Keyboard report buffer */ | |
#define MINOR_PIN 5 | |
#define LF_PIN 6 | |
int pedal_state = 1; | |
int lf_state = 1; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(MINOR_PIN, INPUT); | |
pinMode(LF_PIN,INPUT); | |
cli(); // disable all interrupts | |
TCCR2A = (1<<WGM21)|(0<<WGM20); // Mode CTC | |
TIMSK2 = (1<<OCIE2A); // Local interruption OCIE2A | |
TCCR2B = (0<<WGM22)|(1<<CS22)|(1<<CS21); // Frequency 16Mhz/ 256 = 62500 | |
OCR2A = 250; //250*125 = 31250 = 16Mhz/256/2 | |
sei(); // enable all interrupts | |
digitalWrite(MINOR_PIN, 1); | |
digitalWrite(LF_PIN, 1); | |
// delay(200); | |
} | |
void lfTimer(){ | |
lf_state = digitalRead(LF_PIN)==HIGH; | |
if (lf_state != 1){ | |
pressKey(30); | |
} | |
} | |
void pedalTimer(bool mana){ | |
pedal_state = digitalRead(MINOR_PIN)==HIGH; | |
if (pedal_state != 1) {; | |
if (mana){ | |
pressKey(38); | |
}else{ | |
pressKey(39); | |
} | |
} | |
} | |
void loop() | |
{ | |
} | |
void pressKey(int key){ | |
buf[2] = key; | |
Serial.write(buf,8); | |
releaseKey(); | |
} | |
void releaseKey() | |
{ | |
buf[0] = 0; | |
buf[2] = 0; | |
Serial.write(buf, 8); // Release key | |
} | |
ISR(TIMER2_COMPA_vect){ // timer compare interrupt service routine | |
static int counter=0; | |
static int minor_count=0; | |
if (++counter >= 2550) { | |
counter = 0; | |
lfTimer(); | |
} | |
if(++counter % 25==0){ | |
if(++minor_count>=4){ | |
minor_count=0; | |
pedalTimer(true); | |
}else{ | |
pedalTimer(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment