// 1000Hz debounce sampling staticvoidhandler_debounce(nrf_drv_rtc_int_type_t int_type) { // debouncing, waits until there have been no transitions in 5ms (assuming five 1ms ticks) if (debouncing) { // if debouncing, check if current keystates equal to the snapshot if (keys_snapshot == read_keys()) { // DEBOUNCE ticks of stable sampling needed before sending data debounce_ticks++; if (debounce_ticks == DEBOUNCE) { keys = keys_snapshot; send_data(); } } else { // if keys change, start period again debouncing = false; } } else { // if the keystate is different from the last data // sent to the receiver, start debouncing if (keys != read_keys()) { keys_snapshot = read_keys(); debouncing = true; debounce_ticks = 0; } }
// looking for 500 ticks of no keys pressed, to go back to deep sleep if (read_keys() == 0) { activity_ticks++; if (activity_ticks > ACTIVITY) { nrf_drv_rtc_disable(&rtc_maint); nrf_drv_rtc_disable(&rtc_deb); } } else { activity_ticks = 0; } }
// If a data packet was received, identify half, and throw flag voidnrf_gzll_host_rx_data_ready(uint32_t pipe, nrf_gzll_host_rx_info_t rx_info) { uint32_t data_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
if (pipe == 0) { packet_received_left = true; left_active = 0; // Pop packet and write first byte of the payload to the GPIO port. nrf_gzll_fetch_packet_from_rx_fifo(pipe, data_payload_left, &data_payload_length); } elseif (pipe == 1) { packet_received_right = true; right_active = 0; // Pop packet and write first byte of the payload to the GPIO port. nrf_gzll_fetch_packet_from_rx_fifo(pipe, data_payload_right, &data_payload_length); }
// not sure if required, I guess if enough packets are missed during blocking uart nrf_gzll_flush_rx_fifo(pipe);
// checking for a poll request from QMK if (app_uart_get(&c) == NRF_SUCCESS && c == 's') { // sending data to QMK, and an end byte nrf_drv_uart_tx(data_buffer,10); app_uart_put(0xE0);
//the s character requests the RF slave to send the matrix SERIAL_UART_DATA = 's';
//trust the external keystates entirely, erase the last data uint8_t uart_data[11] = {0};
//there are 10 bytes corresponding to 10 columns, and an end byte for (uint8_t i = 0; i < 11; i++) { //wait for the serial data, timeout if it's been too long //this only happened in testing with a loose wire, but does no //harm to leave it in here while(!SERIAL_UART_RXD_PRESENT){ timeout++; if (timeout > 10000){ break; } } uart_data[i] = SERIAL_UART_DATA; }
//check for the end packet, the key state bytes use the LSBs, so 0xE0 //will only show up here if the correct bytes were recieved if (uart_data[10] == 0xE0) { //shifting and transferring the keystates to the QMK matrix variable for (uint8_t i = 0; i < MATRIX_ROWS; i++) { matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 5; } }
matrix_scan_quantum(); return1; }
首先此函數會傳送一個 s 以請求 nRF51822 開始傳送按鍵狀態封包。
接著,一個 for 迴圈會處理來自 UART 的按鍵狀態封包。當接收完成後,判斷結束封包是否正確(為 0xE0),如果沒問題的話就將按鍵狀態封包處理並賦值給 matrix[],接下來就是讓 QMK 去處理了。