2011-09-29 06:25:51 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2011-09-30 05:22:19 +00:00
|
|
|
// ----- Includes -----
|
|
|
|
|
2011-09-30 08:30:34 +00:00
|
|
|
// AVR Includes
|
|
|
|
#include <avr/io.h>
|
2011-11-29 06:20:04 +00:00
|
|
|
#include <util/delay.h>
|
2011-09-30 08:30:34 +00:00
|
|
|
|
2011-10-16 03:01:46 +00:00
|
|
|
// Project Includes
|
|
|
|
#include <print.h>
|
|
|
|
|
2011-09-30 05:22:19 +00:00
|
|
|
// Local Includes
|
2011-10-16 03:01:46 +00:00
|
|
|
#include "matrix_scan.h"
|
|
|
|
|
|
|
|
// Matrix Configuration
|
|
|
|
#include <matrix.h>
|
2011-09-29 06:25:51 +00:00
|
|
|
|
2011-09-30 05:22:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----- Macros -----
|
|
|
|
|
2011-10-17 03:08:37 +00:00
|
|
|
// -- pinSetup Macros --
|
2011-10-16 03:01:46 +00:00
|
|
|
#define REG_SET(reg) reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
|
2011-09-29 06:25:51 +00:00
|
|
|
|
2011-11-29 06:20:04 +00:00
|
|
|
#define PIN_SET_COL(pin,scan) \
|
|
|
|
switch ( scan ) { \
|
2011-09-29 06:25:51 +00:00
|
|
|
case scanCol: \
|
2011-11-29 06:20:04 +00:00
|
|
|
case scanRow_powrCol: \
|
2011-09-29 06:25:51 +00:00
|
|
|
case scanDual: \
|
|
|
|
REG_SET(port##pin); break; \
|
2011-11-29 06:20:04 +00:00
|
|
|
case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
|
2011-09-29 06:25:51 +00:00
|
|
|
} \
|
|
|
|
break
|
|
|
|
|
2011-11-29 06:20:04 +00:00
|
|
|
#define PIN_SET_ROW(pin,scan) \
|
|
|
|
switch ( scan ) { \
|
2011-09-29 06:25:51 +00:00
|
|
|
case scanRow: \
|
2011-11-29 06:20:04 +00:00
|
|
|
case scanCol_powrRow: \
|
2011-09-29 06:25:51 +00:00
|
|
|
case scanDual: \
|
|
|
|
REG_SET(port##pin); break; \
|
2011-11-29 06:20:04 +00:00
|
|
|
case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
|
2011-09-29 06:25:51 +00:00
|
|
|
} \
|
|
|
|
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
|
|
|
|
|
2011-10-17 03:08:37 +00:00
|
|
|
// -- Column Scan Macros --
|
2011-09-29 06:25:51 +00:00
|
|
|
#define PIN_TEST_COL(pin) \
|
2011-10-17 03:08:37 +00:00
|
|
|
scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
|
|
|
|
if ( scanCode && !( pin & ( 1 << ( matrix[0*(MAX_ROW_SIZE+1)+col] % 10 ) ) ) ) \
|
2011-11-29 06:20:04 +00:00
|
|
|
{ \
|
|
|
|
warn_print("YAY!"); \
|
2011-10-17 03:08:37 +00:00
|
|
|
detectArray[scanCode]++; \
|
2011-11-29 06:20:04 +00:00
|
|
|
} \
|
2011-10-17 03:08:37 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
// -- Row Scan Macros --
|
|
|
|
#define PIN_TEST_ROW(pin) \
|
|
|
|
scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
|
|
|
|
if ( scanCode && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) ) \
|
|
|
|
detectArray[scanCode]++; \
|
|
|
|
break
|
|
|
|
|
|
|
|
// -- Scan Dual Macros --
|
|
|
|
#define PIN_DUALTEST_ROW(pin) \
|
|
|
|
scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
|
|
|
|
if ( scanCode \
|
|
|
|
&& !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) \
|
|
|
|
&& detectArray[scanCode] & 0x01 ) \
|
|
|
|
{ \
|
|
|
|
detectArray[scanCode]++; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
if ( detectArray[scanCode] & 0x01 ) \
|
|
|
|
detectArray[scanCode]--; \
|
|
|
|
} \
|
2011-09-29 06:25:51 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
|
2011-09-30 05:22:19 +00:00
|
|
|
|
|
|
|
// ----- Variables -----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----- Functions -----
|
|
|
|
|
2011-10-16 03:01:46 +00:00
|
|
|
// Goes through the defined matrix and matrix mode, and sets the initial state of all of the available pins
|
2011-11-29 06:20:04 +00:00
|
|
|
void matrix_pinSetup( uint8_t *matrix, uint8_t scanType )
|
2011-09-29 06:25:51 +00:00
|
|
|
{
|
|
|
|
// 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
|
2011-10-16 03:01:46 +00:00
|
|
|
uint16_t row, col;
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
// Rows
|
2011-10-16 03:01:46 +00:00
|
|
|
for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
|
|
|
|
{
|
|
|
|
// We can't pass 2D arrays, so just point to the first element and calculate directly
|
|
|
|
switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
|
|
|
|
{
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(A):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_ROW(A, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(B):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_ROW(B, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(C):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_ROW(C, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(D):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_ROW(D, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(E):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_ROW(E, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(F):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_ROW(F, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Columns
|
2011-10-16 03:01:46 +00:00
|
|
|
for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
|
|
|
|
{
|
|
|
|
// We can't pass 2D arrays, so just point to the first element and calculate directly
|
|
|
|
switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
|
|
|
|
{
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(A):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_COL(A, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(B):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_COL(B, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(C):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_COL(C, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(D):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_COL(D, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(E):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_COL(E, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
PIN_CASE(F):
|
2011-11-29 06:20:04 +00:00
|
|
|
PIN_SET_COL(F, scanType);
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-10-16 03:01:46 +00:00
|
|
|
|
|
|
|
// Pin Status
|
2011-11-29 06:20:04 +00:00
|
|
|
if ( scanType == scanMode )
|
|
|
|
{
|
|
|
|
char tmpStr[6];
|
|
|
|
info_print("Initial Matrix Pin Setup");
|
|
|
|
info_print(" ddrA ddrB ddrC ddrD ddrE ddrF");
|
|
|
|
print(" ");
|
|
|
|
hexToStr_op( ddrA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( ddrB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( ddrC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( ddrD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( ddrE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( ddrF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
print("\n");
|
|
|
|
info_print("portA portB portC portD portE portF");
|
|
|
|
print(" ");
|
|
|
|
hexToStr_op( portA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( portB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( portC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( portD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( portE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
hexToStr_op( portF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
|
|
|
|
print("\n");
|
|
|
|
int8ToStr( scanType, tmpStr );
|
|
|
|
}
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
// Setting the pins
|
2011-10-16 03:01:46 +00:00
|
|
|
#if defined(__AVR_AT90USB1286__)
|
2011-09-29 06:25:51 +00:00
|
|
|
DDRA = ddrA;
|
2011-10-16 03:01:46 +00:00
|
|
|
#endif
|
2011-09-29 06:25:51 +00:00
|
|
|
DDRB = ddrB;
|
|
|
|
DDRC = ddrC;
|
|
|
|
DDRD = ddrD;
|
|
|
|
DDRE = ddrE;
|
|
|
|
DDRF = ddrF;
|
|
|
|
|
2011-10-16 03:01:46 +00:00
|
|
|
#if defined(__AVR_AT90USB1286__)
|
2011-09-29 06:25:51 +00:00
|
|
|
PORTA = portA;
|
2011-10-16 03:01:46 +00:00
|
|
|
#endif
|
2011-09-29 06:25:51 +00:00
|
|
|
PORTB = portB;
|
|
|
|
PORTC = portC;
|
|
|
|
PORTD = portD;
|
|
|
|
PORTE = portE;
|
|
|
|
PORTF = portF;
|
|
|
|
}
|
|
|
|
|
2011-10-17 03:08:37 +00:00
|
|
|
// Scans the given matrix determined by the scanMode method
|
2011-10-16 03:01:46 +00:00
|
|
|
inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
|
2011-09-29 06:25:51 +00:00
|
|
|
{
|
2011-10-17 03:08:37 +00:00
|
|
|
// Loop variables for all modes
|
2011-10-16 03:01:46 +00:00
|
|
|
uint16_t col = 1;
|
|
|
|
uint16_t row = 1;
|
2011-10-17 03:08:37 +00:00
|
|
|
uint16_t scanCode = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Column Scan and Column Scan, Power Row
|
|
|
|
#if scanMode == scanCol || scanMode == scanCol_powrRow
|
|
|
|
for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
|
2011-10-16 03:01:46 +00:00
|
|
|
{
|
2011-10-17 03:08:37 +00:00
|
|
|
// Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
|
|
|
|
// (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
|
2011-10-16 03:01:46 +00:00
|
|
|
switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
|
2011-11-29 06:20:04 +00:00
|
|
|
REG_SET(port##pin); break; \
|
2011-10-16 03:01:46 +00:00
|
|
|
{
|
|
|
|
#if defined(__AVR_AT90USB1286__)
|
2011-09-29 06:25:51 +00:00
|
|
|
case 0: // PINA
|
|
|
|
PIN_TEST_COL(PINA);
|
2011-10-16 03:01:46 +00:00
|
|
|
#endif
|
2011-09-29 06:25:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 03:08:37 +00:00
|
|
|
#endif // scanMode
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
|
2011-10-17 03:08:37 +00:00
|
|
|
// Row Scan and Row Scan, Power Row
|
|
|
|
#if scanMode == scanRow || scanMode == scanRow_powrCol
|
|
|
|
for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
|
|
|
|
{
|
|
|
|
// Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
|
|
|
|
// (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
|
|
|
|
switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
|
|
|
|
{
|
|
|
|
#if defined(__AVR_AT90USB1286__)
|
|
|
|
case 0: // PINA
|
|
|
|
PIN_TEST_ROW(PINA);
|
2011-09-29 06:25:51 +00:00
|
|
|
#endif
|
2011-10-17 03:08:37 +00:00
|
|
|
case 1: // PINB
|
|
|
|
PIN_TEST_ROW(PINB);
|
|
|
|
case 2: // PINC
|
|
|
|
PIN_TEST_ROW(PINC);
|
|
|
|
case 3: // PIND
|
|
|
|
PIN_TEST_ROW(PIND);
|
|
|
|
case 4: // PINE
|
|
|
|
PIN_TEST_ROW(PINE);
|
|
|
|
case 5: // PINF
|
|
|
|
PIN_TEST_ROW(PINF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // scanMode
|
2011-09-29 06:25:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Dual Scan
|
|
|
|
#if scanMode == scanDual
|
2011-10-17 03:08:37 +00:00
|
|
|
// First do a scan of all of the columns, marking each one
|
2011-11-29 06:20:04 +00:00
|
|
|
matrix_pinSetup( matrix, scanCol_powrRow );
|
|
|
|
_delay_us( 1 );
|
2011-10-17 03:08:37 +00:00
|
|
|
for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
|
|
|
|
{
|
|
|
|
// Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
|
|
|
|
// (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
|
|
|
|
switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
|
|
|
|
{
|
|
|
|
#if defined(__AVR_AT90USB1286__)
|
|
|
|
case 0: // PINA
|
|
|
|
PIN_TEST_COL(PINA);
|
2011-09-29 06:25:51 +00:00
|
|
|
#endif
|
2011-10-17 03:08:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 06:25:51 +00:00
|
|
|
|
2011-10-17 03:08:37 +00:00
|
|
|
// Next, do a scan of all of the rows, clearing any "vague" keys (only detected on row, but not column, or vice-versa)
|
|
|
|
// And marking any keys that are detected on the row and column
|
2011-11-29 06:20:04 +00:00
|
|
|
matrix_pinSetup( matrix, scanRow_powrCol );
|
|
|
|
_delay_us( 1 );
|
2011-10-17 03:08:37 +00:00
|
|
|
col = 1;
|
|
|
|
row = 1;
|
|
|
|
for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
|
|
|
|
{
|
|
|
|
// Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
|
|
|
|
// (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
|
|
|
|
switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
|
|
|
|
{
|
|
|
|
#if defined(__AVR_AT90USB1286__)
|
|
|
|
case 0: // PINA
|
|
|
|
PIN_DUALTEST_ROW(PINA);
|
|
|
|
#endif
|
|
|
|
case 1: // PINB
|
|
|
|
PIN_DUALTEST_ROW(PINB);
|
|
|
|
case 2: // PINC
|
|
|
|
PIN_DUALTEST_ROW(PINC);
|
|
|
|
case 3: // PIND
|
|
|
|
PIN_DUALTEST_ROW(PIND);
|
|
|
|
case 4: // PINE
|
|
|
|
PIN_DUALTEST_ROW(PINE);
|
|
|
|
case 5: // PINF
|
|
|
|
PIN_DUALTEST_ROW(PINF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|