summaryrefslogtreecommitdiff
path: root/cros_ec/chip_stub/keyboard.c
blob: 6586202290197e303db27c743bffa5f9df323acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Chip stub code of keyboard. Implements the chip interface.
 */

#include <stdint.h>
#include "chip_interface/keyboard.h"


static EcKeyboardCallback core_keyboard_callback;
/* each byte presents one column in 8 rows. 1 = pressed. */
static uint8_t virtual_matrix[MAX_KEYBOARD_MATRIX_COLS];

EcError EcKeyboardRegisterCallback(EcKeyboardCallback cb) {
  core_keyboard_callback = cb;
  return EC_SUCCESS;
}


/* Returns bit array of every key press stage. */
EcError EcKeyboardGetState(uint8_t *bit_array) {
  /* TODO: implement later */
  return EC_ERROR_UNIMPLEMENTED;
}


/* Called by test code. This simulates a key press or release.
 * Usually, the test code would expect a scan code is received at host side.
 */
EcError SimulateKeyStateChange(int row, int col, int state) {
  EC_ASSERT(row < MAX_KEYBOARD_MATRIX_ROWS);
  EC_ASSERT(col < MAX_KEYBOARD_MATRIX_COLS);

  if (!core_keyboard_callback) return EC_ERROR_UNKNOWN;

  state = (state) ? 1 : 0;
  int current_state = (virtual_matrix[col] >> row) & 1;

  if (state && !current_state) {
    /* key is just pressed down */
    virtual_matrix[col] |= 1 << row;
    core_keyboard_callback(row, col, state);
  } else if (!state && current_state) {
    virtual_matrix[col] &= ~(1 << row);
    core_keyboard_callback(row, col, state);
  } else {
    /* Nothing happens if a key has been pressed or released. */
  }

  return EC_SUCCESS;
}