The first working firmware is already working. There is room for improvement, though.
The whole code is available at github but the main components are here:
1. The keyboard is read with a small delay betwenn the changes in I/O direction and the reading
// Set P1=0, P0,P2 = inputs with pullup
pinMode(0, INPUT_PULLUP); // P0
pinMode(1, OUTPUT); // P1
digitalWrite(1, LOW);
pinMode(2, INPUT_PULLUP); // P2
DigiMouse.delay(1);
if (digitalRead(0) == 0) button_state |= (1 << 2); // Left
if (digitalRead(2) == 0) button_state |= (1 << 3); // Right
// Set P1=1, P0=0, P2 = input with pullup
pinMode(0, OUTPUT); // P0
digitalWrite(1, HIGH); // P1
digitalWrite(0, LOW);
// pinMode(2, INPUT_PULLUP); // P2 no change, still input
DigiMouse.delay(1);
if (digitalRead(2) == 0) button_state |= (1 << 0); // Up
// Set P1=1, P2=0, P0 = input with pullup
pinMode(0, INPUT_PULLUP); // P0
// digitalWrite(1,HIGH); // P1 no change from last state
pinMode(2, OUTPUT); // P2
digitalWrite(2, LOW);
DigiMouse.delay(1);
if (digitalRead(0) == 0) button_state |= (1 << 1); // Down
2. The readings are analyzed to detect changes from the last scan.
if (button_state != last_scan) {
last_scan = button_state;
....
....
}
3. The (former) useless combinations (UP+DOWN, LEFT+RIGHT) are disregarded and the sensitivity index is advanced or regressed when keys LEFT and RIGHT are held together and the keys UP or DOWN are pressed
// Eliminate useless sequences: L+R
if ( (button_state & 0x0c) ==0x0c ) {
// Do L+R Stuff
if ( (button_state & 1) && (fib_index < (MAX_FIB_INDEX-1)) ) fib_index++ ; // Up
if ( (button_state & 2) && (fib_index > 0 ) ) fib_index--; // Down
} else {
if (button_state & 4) DigiMouse.moveX(-fib_step[fib_index]); // Left
if (button_state & 8) DigiMouse.moveX( fib_step[fib_index]); // Right
}
// Eliminate useless sequences: U+D
if ( (button_state & 0x03) == 0x03 ) {
// Do U+D Stuff
fib_index = 0; // U+R reset fib index
} else {
if (button_state & 1) DigiMouse.moveY(-fib_step[fib_index]); // Up
if (button_state & 2) DigiMouse.moveY( fib_step[fib_index]); // Down
}
The sensitivity is increased/decreased according to a slice of a fibonacci sequence.
const uint8_t fib_step[MAX_FIB_INDEX]={ 1, 2, 3, 5, 8, 13 };
4. Finally a small delay is added as a primitive debouncing method
DigiMouse.delay(5);
So far that's all folks!!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.