all a's working

This commit is contained in:
Jack Humbert 2018-06-09 23:53:26 -04:00
parent 0f89d7efed
commit 13f49ad8d9
3 changed files with 28 additions and 22 deletions

View File

@ -157,6 +157,11 @@ uint8_t twi2c_start_listening(uint8_t address, twi2c_message_received callback)
return 0;
}
uint8_t twi2c_restart_listening(uint8_t address) {
i2cMatchAddress(&I2C_DRIVER, address/2);
return 0;
}
void twi2c_stop(void) {
i2cUnmatchAll(&I2C_DRIVER);
i2cStop(&I2C_DRIVER);

View File

@ -43,5 +43,6 @@ void twi2c_stop(void);
uint8_t twi2c_reply(I2CDriver * i2cp, uint8_t * data, uint16_t length);
uint8_t twi2c_transmit_receive(uint8_t address, uint8_t * tx_body, uint16_t tx_length, uint8_t * rx_body, uint16_t rx_length);
uint8_t twi2c_start_listening(uint8_t address, twi2c_message_received callback);
uint8_t twi2c_restart_listening(uint8_t address);
#endif

View File

@ -44,14 +44,13 @@ uint8_t qwiic_keyboard_matrix_message[QWIIC_KEYBOARD_ROWS] = {0};
twi2c_message_received qwiic_keyboard_message_received_ptr = qwiic_keyboard_message_received;
uint16_t qwiic_keyboard_keymap[QWIIC_KEYBOARD_LAYERS][QWIIC_KEYBOARD_ROWS][QWIIC_KEYBOARD_COLS] = {0};
uint8_t qwiic_keyboard_new_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
uint8_t qwiic_keyboard_listening_address = QWIIC_KEYBOARD_LISTENING_ADDRESS_START;
uint8_t qwiic_keyboard_processing_slave = false;
void qwiic_keyboard_init(void) {
twi2c_init();
twi2c_start();
twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received_ptr);
twi2c_start_listening(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS, qwiic_keyboard_message_received_ptr);
}
void qwiic_keyboard_set_master(void) {
@ -79,12 +78,11 @@ void qwiic_keyboard_task(void) {
qwiic_matrix_t matrix_row = 0;
qwiic_matrix_t matrix_change = 0;
qwiic_keyboard_processing_slave = true;
SEND_STRING("K.");
for (uint8_t r = 0; r < QWIIC_KEYBOARD_ROWS; r++) {
matrix_row = qwiic_keyboard_matrix_message[r];
matrix_change = matrix_row ^ matrix_prev[r];
if (matrix_change) {
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
for (uint8_t c = 0; c < QWIIC_KEYBOARD_COLS; c++) {
if (matrix_change & ((qwiic_matrix_t)1<<c)) {
action_exec((keyevent_t){
.key = (keypos_t){ .row = r, .col = c },
@ -98,7 +96,7 @@ void qwiic_keyboard_task(void) {
if (++keys_processed >= QMK_KEYS_PER_SCAN)
#endif
// process a key per task call
goto MATRIX_LOOP_END;
//goto MATRIX_LOOP_END;
}
}
}
@ -109,7 +107,7 @@ void qwiic_keyboard_task(void) {
if (!keys_processed)
#endif
action_exec(TICK);
MATRIX_LOOP_END:
//MATRIX_LOOP_END:
qwiic_keyboard_processing_slave = false;
} else {
// disconnect
@ -118,12 +116,10 @@ void qwiic_keyboard_task(void) {
} else { // if not connected
// send new address to listen on, expect back keymap
if (MSG_OK == twi2c_transmit_receive(QWIIC_KEYBOARD_HANDSHAKE_ADDRESS,
&qwiic_keyboard_new_listening_address, 1,
&qwiic_keyboard_listening_address, 1,
qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE
)) {
qwiic_keyboard_connected = true;
// increment address (for future implemenations of supporting multiple devices)
qwiic_keyboard_new_listening_address+=2;
// load keymap into memory
qwiic_keyboard_read_keymap(qwiic_keyboard_handshake_message);
}
@ -131,27 +127,30 @@ void qwiic_keyboard_task(void) {
}
}
uint8_t qwiic_keyboard_reply[MATRIX_ROWS];
float song_one_up[][2] = SONG(ONE_UP_SOUND);
bool first_message = true;
void qwiic_keyboard_message_received(I2CDriver *i2cp, uint8_t * body, uint16_t size) {
if (qwiic_keyboard_connected) {
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
qwiic_keyboard_reply[row] = matrix_get_row(row);
for (uint8_t row = 0; row < QWIIC_KEYBOARD_ROWS; row++) {
if (row < MATRIX_ROWS) {
qwiic_keyboard_matrix_message[row] = matrix_get_row(row);
} else {
qwiic_keyboard_matrix_message[row] = 0;
}
}
twi2c_reply(i2cp, qwiic_keyboard_matrix_message, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
if (first_message) {
PLAY_SONG(song_one_up);
first_message = false;
}
twi2c_reply(i2cp, qwiic_keyboard_reply, QWIIC_KEYBOARD_MATRIX_MESSAGE_SIZE);
} else {
qwiic_keyboard_connected = true;
qwiic_keyboard_master = false;
qwiic_keyboard_listening_address = body[0];
qwiic_keyboard_write_keymap(qwiic_keyboard_reply);
twi2c_reply(i2cp, qwiic_keyboard_reply, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
twi2c_stop();
twi2c_start();
twi2c_start_listening(qwiic_keyboard_listening_address, qwiic_keyboard_message_received_ptr);
stop_all_notes();
PLAY_SONG(song_one_up);
twi2c_restart_listening(qwiic_keyboard_listening_address);
qwiic_keyboard_write_keymap(qwiic_keyboard_handshake_message);
twi2c_reply(i2cp, qwiic_keyboard_handshake_message, QWIIC_KEYBOARD_HANDSHAKE_MESSAGE_SIZE);
}
}
@ -191,7 +190,8 @@ bool is_keyboard_master(void) {
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
if (qwiic_keyboard_processing_slave) {
// trick the built-in handling to accept our replacement keymap
return qwiic_keyboard_keymap[(layer)][(key.row)][(key.col)];
//return qwiic_keyboard_keymap[(layer)][(key.row)][(key.col)];
return KC_A;
} else {
// Read entire word (16bits)
return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);