Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Created May 8, 2014 03:07
Show Gist options
  • Save deoxxa/6cca390f9dc16832f69c to your computer and use it in GitHub Desktop.
Save deoxxa/6cca390f9dc16832f69c to your computer and use it in GitHub Desktop.
volatile unsigned char kbd_clock_status = 0;
volatile unsigned char kbd_bit = 0;
volatile unsigned char kbd_code = 0;
void read_keyboard() {
// clock was before low and clock is now high, thus rising
if (kbd_clock_status == 0 && bit_is_set(PIND, PIND6) == 0) {
kbd_clock_status = 1;
return;
}
// clock wasn't high before or clock isn't currently low, thus not falling and
// we've probably been triggered by the data line changing - we don't want to
// react yet
if (kbd_clock_status == 0 || bit_is_set(PIND, PIND6) == 0) {
return;
}
// by process of elimination, we're now at the clock's falling edge
// reset set the clock status, since the line is now low
kbd_clock_status = 0;
if (kbd_bit == 0) {
// start bit, should be 0
// light onboard led if it's not 0
if (bit_is_set(PIND, PIND7) == 0) {
PORTB |= _BV(PORTB5);
}
// reset the keyboard code
kbd_code = 0;
} else if (kbd_bit >= 1 && kbd_bit <= 8) {
if (bit_is_set(PIND, PIND7) == 0) {
kbd_code += (1 << (kbd_bit - 1));
}
} else if (kbd_bit == 9) {
// parity bit
// TODO: handle parity bit, lol
if (bit_is_set(PIND, PIND7) == 0) {
PORTB |= _BV(PORTB5);
}
} else if (kbd_bit == 10) {
// stop bit, should be 1
// light onboard led if it's not 1
if (bit_is_set(PIND, PIND7) != 0) {
PORTB |= _BV(PORTB5);
}
}
// 10th bit is the end, reset the bit counter and act on the input
if (kbd_bit == 10) {
kbd_bit = 0;
write_leds(kbd_code);
} else {
kbd_bit++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment