void scan_matrix() { bool changed = false; for (uint8_t i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) IO_set(j | 0x80, true); IO_set((i) | 0x80, false); _delay_us(10); for (uint8_t j = 0; j < 19; ++j) { bool state = !IO_get(j); uint8_t layer = 0; uint8_t code = LAYOUT_get_scancode(layer, matrix[j][i]); if (state != states[j][i]) { changed = true; states[j][i] = state; switch (state) { case true: if (code == KEY_ESC && (HID_scancode_is_pressed(KEY_LEFT_SHIFT) || HID_scancode_is_pressed(KEY_RIGHT_SHIFT))) code = KEY_TILDE; HID_set_scancode_state(code, true); break; case false: if (code == KEY_ESC) HID_set_scancode_state(KEY_TILDE, false); HID_set_scancode_state(code, false); break; } } } } if (changed) HID_commit_state(); }
struct scan_result SCANNER_scan(uint8_t noutputs, uint8_t outputs[], uint8_t ninputs, uint8_t inputs[]) { struct scan_result result = {.status = SCAN_ENONE}; bool found = false; for (int i = 0; i < noutputs; ++i) { IO_config(outputs[i], OUTPUT); IO_set(outputs[i], true); } for (int j = 0; j < ninputs; ++j) { IO_config(inputs[j], INPUT); IO_set(inputs[j], true); /* pull-up */ } for (int i = 0; i < noutputs; ++i) { IO_set(outputs[i], false); for (int j = 0; j < ninputs; ++j) { if (IO_get(inputs[j])) /* if not pulled down */ continue; if (!found) { result.a = outputs[i]; result.b = inputs[j]; result.status = SCAN_OK_UNI; found = true; } else { if (result.a == inputs[j] && result.b == outputs[i]) result.status = SCAN_OK_BI; else result.status = SCAN_ETOOMANY; } } IO_set(outputs[i], true); } return result; }
//------------------------------------------------------------------------------ // Display results //------------------------------------------------------------------------------ void show() { uint64_t btn0; uint64_t btn1; IO_get(&button[0], &btn0); IO_get(&button[1], &btn1); IO_display_clear(&display); IO_print(&display, "Btn #0: %lld\r\n", btn0); IO_print(&display, "Btn #1: %lld\r\n", btn1); IO_print(&display, "ADC #0: %lld\r\n", sliderR); // We should not do it in an interrupt handler, but since there is // nothing elso going on, then who cares. IO_sync(&display); }
uint16_t get_input() { uint16_t ret = 0; for (int i = 0; i < 16; ++i) { ret <<= 1; ret |= IO_get(i) ? 0x0001 : 0x0000; } return ret; }
bool MATRIX_scan() { bool changed = false; /* scan the matrix */ for (uint8_t i = 0; i < nrows; ++i) { IO_set(rows[i], GPIO_PIN_SET); for (uint8_t j = 0; j < ncols; ++j) { bool state = IO_get(cols[j]); if (state == is_pressed(i, j)) continue; changed = true; set_state(i, j, state); LAYOUT_set_key_state(matrix[i][j], state); } IO_set(rows[i], GPIO_PIN_RESET); } return changed; }
//------------------------------------------------------------------------------ // An ADC event //------------------------------------------------------------------------------ void slider_event(IO_io *io, uint16_t event) { IO_get(&slider, &sliderR); IO_set(&timer, 100000000); // fire in 0.1 sec show(); }