Reorganization for use with the CMake "Modules"

- Include option is currently "hacked" and needs to be fixed
- Builds on Linux, but Mac and Windows needs to be tested
- Loader script generation isn't complete
This commit is contained in:
Jacob Alexander 2011-09-28 23:25:51 -07:00
parent 0415ba2481
commit b4e1868d19
19 changed files with 2910 additions and 7 deletions

181
Scan/matrix/matrix.c Normal file
View file

@ -0,0 +1,181 @@
/* Copyright (C) 2011 by Jacob Alexander
*
* 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 "matrix.h"
#define REG_SET(reg) reg |= (1 << ( matrix[row][col] % 10 ) )
#define PIN_SET_COL(pin) \
switch ( scanMode ) { \
case scanCol: \
case scanCol_powrRow: \
case scanDual: \
REG_SET(port##pin); break; \
case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
} \
break
#define PIN_SET_ROW(pin) \
switch ( scanMode ) { \
case scanRow: \
case scanRow_powrCol: \
case scanDual: \
REG_SET(port##pin); break; \
case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
} \
break
#define PIN_CASE(pinLetter) \
case pin##pinLetter##0: \
case pin##pinLetter##1: \
case pin##pinLetter##2: \
case pin##pinLetter##3: \
case pin##pinLetter##4: \
case pin##pinLetter##5: \
case pin##pinLetter##6: \
case pin##pinLetter##7
#define PIN_TEST_COL(pin) \
if ( !( pin & ( 1 << ( matrix[0][col] % 10 ) ) \
detectArray[matrix[row][col]]++; \
break
void matrix_pinSetup( uint8_t *matrix )
{
// Setup the variables
uint8_t portA = 0x00;
uint8_t portB = 0x00;
uint8_t portC = 0x00;
uint8_t portD = 0x00;
uint8_t portE = 0x00;
uint8_t portF = 0x00;
uint8_t ddrA = 0x00;
uint8_t ddrB = 0x00;
uint8_t ddrC = 0x00;
uint8_t ddrD = 0x00;
uint8_t ddrE = 0x00;
uint8_t ddrF = 0x00;
// Loop through all the pin assignments, for the initial pin settings
int row, col;
// Rows
for ( row = 1; row < sizeof(matrix); row++ ) {
switch ( matrix[row][col] ) {
PIN_CASE(A):
PIN_SET_ROW(A);
PIN_CASE(B):
PIN_SET_ROW(B);
PIN_CASE(C):
PIN_SET_ROW(C);
PIN_CASE(D):
PIN_SET_ROW(D);
PIN_CASE(E):
PIN_SET_ROW(E);
PIN_CASE(F):
PIN_SET_ROW(F);
default:
continue;
}
}
// Columns
for ( col = 1; col < sizeof(matrix[0]); row++ ) {
switch ( matrix[row][col] ) {
PIN_CASE(A):
PIN_SET_COL(A);
PIN_CASE(B):
PIN_SET_COL(B);
PIN_CASE(C):
PIN_SET_COL(C);
PIN_CASE(D):
PIN_SET_COL(D);
PIN_CASE(E):
PIN_SET_COL(E);
PIN_CASE(F):
PIN_SET_COL(F);
default:
continue;
}
}
// Setting the pins
DDRA = ddrA;
DDRB = ddrB;
DDRC = ddrC;
DDRD = ddrD;
DDRE = ddrE;
DDRF = ddrF;
PORTA = portA;
PORTB = portB;
PORTC = portC;
PORTD = portD;
PORTE = portE;
PORTF = portF;
}
// TODO Proper matrix scanning
void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
{
// Column Scan
#if scanMode == scanCol
uint8_t col = 1;
uint8_t row = 1;
for ( ; col < sizeof(matrix[1]); col++ ) {
switch ( matrix[0][col] / 10 ) {
case 0: // PINA
PIN_TEST_COL(PINA);
case 1: // PINB
PIN_TEST_COL(PINB);
case 2: // PINC
PIN_TEST_COL(PINC);
case 3: // PIND
PIN_TEST_COL(PIND);
case 4: // PINE
PIN_TEST_COL(PINE);
case 5: // PINF
PIN_TEST_COL(PINF);
}
}
#endif
// Row Scan
#if scanMode == scanRow
#endif
// Column Scan, Power Row
#if scanMode == scanCol_powrRow
#endif
// Row Scan, Power Column
#if scanMode == scanRow_powrCol
#endif
// Dual Scan
#if scanMode == scanDual
#endif
}

151
Scan/matrix/matrix.h Normal file
View file

@ -0,0 +1,151 @@
/* Copyright (C) 2011 by Jacob Alexander
*
* 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.
*/
#ifndef __MATRIX_H
#define __MATRIX_H
// ----- Quick Map (don't change) -----
#define pinA0 0
#define pinA1 1
#define pinA2 2
#define pinA3 3
#define pinA4 4
#define pinA5 5
#define pinA6 6
#define pinA7 7
#define pinB0 10
#define pinB1 11
#define pinB2 12
#define pinB3 13
#define pinB4 14
#define pinB5 15
#define pinB6 16
#define pinB7 17
#define pinC0 20
#define pinC1 21
#define pinC2 22
#define pinC3 23
#define pinC4 24
#define pinC5 25
#define pinC6 26
#define pinC7 27
#define pinD0 30
#define pinD1 31
#define pinD2 32
#define pinD3 33
#define pinD4 34
#define pinD5 35
#define pinD6 36
#define pinD7 37
#define pinE0 40
#define pinE1 41
#define pinE2 42
#define pinE3 43
#define pinE4 44
#define pinE5 45
#define pinE6 46
#define pinE7 47
#define pinF0 50
#define pinF1 51
#define pinF2 52
#define pinF3 53
#define pinF4 54
#define pinF5 55
#define pinF6 56
#define pinF7 57
#define pinNULL 128
// ----- Scan Mode (usually dual-scan) -----
// Ordered by increasing memory/CPU usage
#define scanRow 0 // Needed for powered switches (Hall-Effect)
#define scanCol 1 // Opposite of scanRow
#define scanRow_powrCol 2 // NKRO supported (simple detection)
#define scanCol_powrRow 3 // Opposite of scanRow_powrCol
#define scanDual 4 // Typical ~2KRO matrix
// ----- Scan Mode Setting -----
#define scanMode scanCol
// ----- Key Settings -----
#define keyboardSize 16 // # of keys
// ----- Matrix Configuration -----
static uint8_t matrix_pinout[][] = {
// Just layout the matrix by rows and columns
// Usually you'll want to set the scanMode above to scanDual or scanCol_powrRow/scanRow_powrCol
// The mode allows for optimization in the kind of scanning algorithms that are done
//
// The key numbers are used to translate into the keymap table (array) (and always start from 1, not 0).
// See the keymap.h file for the various preconfigured arrays.
// Scan Mode | Col 1 | Col 2 | Col 3 | Col 4 | Col 4 | ...
// -------------------------------------------------------
// Row 1 | Key 1 Key 7 Key32 ...
// Row 2 | Key 3 Key92 ...
// Row 3 | Key23 ...
// Row 4 | ...
// Row 5 |
// ... |
{ scanMode, pinF4, pinA6, pinA1, pinA3, pinF5, pinA5, pinA2, pinF0, pinF6, pinA7, pinA0, pinF1, pinF3, pinF7, pinA4, pinF2 },
{ pinNULL, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
// Example Rows
//{ pinE0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
//{ pinE1, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, },
};
// ----- Variables -----
// NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
// Other Bits: Pressed state sample counter
uint8_t keyboardDetectArray[keyboardSize + 1];
// ----- Functions -----
#endif // __MATRIX_H

137
Scan/matrix/scan_loop.c Normal file
View file

@ -0,0 +1,137 @@
/* Copyright (C) 2011 by Jacob Alexander
*
* 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 <stdint.h>
#include <usb_keyboard_debug.h>
#include <keymap.h>
// Debouncing Defines
#define SAMPLE_THRESHOLD 110
#define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
// Loop over all of the sampled keys of the given array
// If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
// This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
#define DEBOUNCE_ASSESS(table,size) \
for ( uint8_t key = 1; key < size + 1; key++ ) {\
table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
} \
// NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
// Other Bits: Pressed state sample counter
#define KEYBOARD_SIZE 23
uint8_t keyboardDetectArray[KEYBOARD_SIZE + 1];
// Interrupt Variable
volatile uint8_t sendKeypresses = 0;
// USB Data Send
void usb_send( uint8_t validKeys )
{
// TODO undo potentially old keys
for ( uint8_t c = validKeys; c < 6; c++ )
keyboard_keys[c] = 0;
// Send keypresses
usb_keyboard_send();
// Clear sendKeypresses Flag
sendKeypresses = 0;
// Clear modifiers
keyboard_modifier_keys = 0;
}
// Given a sampling array, and the current number of detected keypress
// Add as many keypresses from the sampling array to the USB key send array as possible.
void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
if ( keys[key] & (1 << 7) ) {
pint8( key );
//print(" ");
uint8_t modFound = 0;
// Determine if the key is a modifier
for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
// Modifier found
if ( modifiers[mod] == key ) {
keyboard_modifier_keys |= map[key];
modFound = 1;
break;
}
}
if ( modFound )
continue;
// Too many keys
if ( *validKeys == 6 )
break;
// Allow ignoring keys with 0's
if ( map[key] != 0 )
keyboard_keys[(*validKeys)++] = map[key];
}
}
}
// Main Detection Loop
void scan_loop( void )
{
//matrix_pinSetup( matrix_pinout );
uint8_t count = 0;
for ( ;; ) {
//matrix_scan( matrix_pinout, keyboardDetectArray );
// Check count to see if the sample threshold may have been reached, otherwise collect more data
if ( count++ < MAX_SAMPLES )
continue;
// Reset Sample Counter
count = 0;
// Assess debouncing sample table
//DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
// Send keypresses over USB if the ISR has signalled that it's time
if ( !sendKeypresses )
continue;
// Layout Setup
uint8_t validKeys = 0;
uint8_t *keyboard_MODMASK = keyboard_modifierMask;
uint8_t keyboard_NUMMODS = MODIFIERS_KEYBOARD;
uint8_t *keyboard_MAP = defaultMap;
// TODO Layout Switching
// TODO Macro Processing
// Debounce Sampling Array to USB Data Array
keyPressDetection( keyboardDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
// Send USB Data
usb_send( validKeys );
}
}

31
Scan/matrix/scan_loop.h Normal file
View file

@ -0,0 +1,31 @@
/* Copyright (C) 2011 by Jacob Alexander
*
* 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.
*/
#ifndef __SCAN_LOOP_H
#define __SCAN_LOOP_H
//extern uint8_t keyboardDetectArray[KEYBOARDZ
extern volatile uint8_t sendKeypresses;
void scan_loop( void );
#endif // __SCAN_LOOP_H

23
Scan/matrix/setup.cmake Normal file
View file

@ -0,0 +1,23 @@
###| CMake Kiibohd Controller Scan Module |###
#
# Written by Jacob Alexander in 2011 for the Kiibohd Controller
#
# Released into the Public Domain
#
###
###
# Module C files
#
set( SCAN_SRCS
scan_loop.c
)
###
# Module Specific Options TODO Fixme!! (../)
#
add_definitions( -I../Keymap )