uint8_t matrix_scan(void) { int ret = _matrix_scan(); #ifdef USE_I2C if( i2c_transaction() ) { #else if( serial_transaction() ) { #endif // turn on the indicator led when halves are disconnected TXLED1; error_count++; if (error_count > ERROR_DISCONNECT_COUNT) { // reset other half if disconnected int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; for (int i = 0; i < ROWS_PER_HAND; ++i) { matrix[slaveOffset+i] = 0; } } } else { // turn off the indicator led on no error TXLED0; error_count = 0; } return ret; } void matrix_slave_scan(void) { _matrix_scan(); int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2); for (int i = 0; i < ROWS_PER_HAND; ++i) { serial_slave_buffer[i] = matrix[offset+i]; } }
uint8_t matrix_scan(void) { int ret = _matrix_scan(); int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; #ifdef USE_I2C // Get rows from other half over i2c // Matrix stored at 0x00-0x03 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); if (err) goto i2c_error; err = i2c_master_write(0x00); if (err) goto i2c_error; err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); if (err) goto i2c_error; if (!err) { // turn off the indicator led on no error //PORTD |= (1<<5); int i; for (i = 0; i < ROWS_PER_HAND-1; ++i) { matrix[slaveOffset+i] = i2c_master_read(I2C_ACK); } matrix[slaveOffset+i] = i2c_master_read(I2C_NACK); i2c_master_stop(); } else { i2c_error: // the cable is disconnceted, or something else went wrong // reset the i2c state TWCR = 0; // turn on the indicator led //PORTD &= ~(1<<5); // if we cannot communicate with the other half, then unset all of its keys for (int i = 0; i < ROWS_PER_HAND; ++i) { matrix[slaveOffset+i] = 0; } } #else if( serial_transaction() ) { // turn on the indicator led //PORTD &= ~(1<<5); // if we cannot communicate with the other half, then unset all of its keys for (int i = 0; i < ROWS_PER_HAND; ++i) { matrix[slaveOffset+i] = 0; } } else { // turn off the indicator led on no error //PORTD |= (1<<5); // no error for (int i = 0; i < ROWS_PER_HAND; ++i) { matrix[slaveOffset+i] = serial_slave_buffer[i]; } } #endif return ret; }