Skip to content

Commit dda683a

Browse files
franciscomunozandrewboie
authored andcommitted
samples: drivers: kscan: Add kscan sample application
This illustrates how a keyboard matrix (laptop keyboard) reports key events to a user application. In addition, it shows how to handle the typematic rate and delay from user space. Signed-off-by: Francisco Munoz <[email protected]>
1 parent 2f60c35 commit dda683a

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
5+
project(kscan)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/drivers/kscan/README.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. kscan-sample:
2+
3+
KSCAN Interface
4+
####################################
5+
6+
Overview
7+
********
8+
9+
This sample demonstrates how to use the :ref:`KSCAN API <kscan>`.
10+
Callbacks are registered that will write to the console indicating KSCAN events.
11+
These events indicate key presses and releases.
12+
13+
Building and Running
14+
********************
15+
16+
The sample can be built and executed on boards supporting a Keyboard Matrix.
17+
Please connect a Keyboard Matrix to exercise the functionality. You need to
18+
obtain the right keymap from the vendor because they vary across different
19+
manufactures.
20+
21+
Sample output
22+
=============
23+
24+
.. code-block:: console
25+
26+
KSCAN test with a Keyboard matrix
27+
Note: You are expected to see several callbacks
28+
as you press and release keys!

samples/drivers/kscan/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_PRINTK=y
3+
CONFIG_KSCAN=y
4+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# kscan + mec15xxevb_assy6853
2+
CONFIG_CONSOLE=y
3+
CONFIG_LOG=y
4+
CONFIG_LOG_PROCESS_THREAD_SLEEP_MS=100
5+
CONFIG_KSCAN=y

samples/drivers/kscan/sample.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sample:
2+
name: KSCAN driver sample
3+
tests:
4+
sample.driver.kscan:
5+
tags: drivers
6+
harness: console
7+
harness_config:
8+
type: multi_line
9+
ordered: true
10+
regex:
11+
- "kb data(.*)"
12+
depends_on: kscan

samples/drivers/kscan/src/main.c

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright (c) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <misc/printk.h>
9+
#include <gpio.h>
10+
#include <soc.h>
11+
#include <kscan.h>
12+
13+
#define LOG_LEVEL LOG_LEVEL_DBG
14+
#include <logging/log.h>
15+
16+
LOG_MODULE_REGISTER(main);
17+
18+
struct device *kscan_dev;
19+
static struct k_timer typematic_timer;
20+
static struct k_timer block_matrix_timer;
21+
22+
#define KEY_RSVD 0U
23+
#define MAX_MATRIX_KEY_COLS 16
24+
#define MAX_MATRIX_KEY_ROWS 8
25+
26+
/****************************************************************************/
27+
/* Fujitsu keyboard model N860-7401-TOO1 */
28+
/* */
29+
/* Sense7 Sense6 Sense5 Sense4 Sense3 Sense2 Sense1 Sense0 */
30+
/*+---------------------------------------------------------------+ */
31+
/*| | Capslk| | 1! | Tab | F1 | `~ | | Scan 0 */
32+
/*| | (30) | | (2) | (16) | (112) | (1) | | (KEY #) */
33+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
34+
/*| | W | Q | F7 | | Esc | F6 | F5 | Scan 1 */
35+
/*| | (18) | (17) | (118) | | (110) | (117) | (116) | (KEY #) */
36+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
37+
/*| | F8 | | 2@ | F4 | F3 | | F2 | Scan 2 */
38+
/*| | (119) | | (3) | (115) | (114) | | (113) | (KEY #) */
39+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
40+
/*| | R | E | 3# | 4$ | C | F | V | Scan 3 */
41+
/*| | (20) | (19) | (4) | (5) | (48) | (34) | (49) | (KEY #) */
42+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
43+
/*| N | Y | 6^ | 5% | B | T | H | G | Scan 4 */
44+
/*| (51) | (22) | (7) | (6) | (50) | (21) | (36) | (35) | (KEY #) */
45+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
46+
/*| SpaceB| M | A | 7& | J | D | S | U | Scan 5 */
47+
/*| (61) | (52) | (31) | (8) | (37) | (33) | (32) | (23) | (KEY #) */
48+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
49+
/*| F9 | I | ,< | 8* | | Z | X | K | Scan 6 */
50+
/*| (120) | (24) | (53) | (9) | | (46) | (47) | (38) | (KEY #) */
51+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
52+
/*| | =+ | ]} | 9( | L | | CRSL | O | Scan 7 */
53+
/*| | (13) | (28) | (10) | (39) | | (79) | (25) | (KEY #) */
54+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
55+
/*| -_ | 0) | /? | [{ | ;: | | | '" | Scan 8 */
56+
/*| (12) | (11) | (55) | (27) | (40) | | | (41) | (KEY #) */
57+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
58+
/*| F10 | Pause | NumLK | P | | | | | Scan 9 */
59+
/*| (121) | (126) | (90) | (26) | | | | | (KEY #) */
60+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
61+
/*| BkSpac| \| | F11 | .> | | | W-Appl| CRSD | Scan 10 */
62+
/*| (15) | (29) | (122) | (54) | | | (71) | (84) | (KEY #) */
63+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
64+
/*| Enter | Delete| Insert| F12 | | | CRSU | CRSR | Scan 11 */
65+
/*| (43) | (76) | (75) | (123) | | | (83) | (89) | (KEY #) */
66+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
67+
/*| | R-WIN | | | | | L-WIN | Fn | Scan 12 */
68+
/*| | (87) | | | | | (59) | (255) | (KEY #) */
69+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
70+
/*| | | | RShift| LShift| | | | Scan 13 */
71+
/*| | | | (57) | (44) | | | | (KEY #) */
72+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
73+
/*| | | | | | | L Alt | R Alt | Scan 14 */
74+
/*| | | | | | | (60) | (62) | (KEY #) */
75+
/*|-------+-------+-------+-------+-------+-------+-------+-------| */
76+
/*| R Ctrl| | | | | L Ctrl| | | Scan 15 */
77+
/*| (64) | | | | | (58) | | | (KEY #) */
78+
/*+---------------------------------------------------------------+ */
79+
/* */
80+
/****************************************************************************/
81+
82+
static const u8_t keymap[MAX_MATRIX_KEY_COLS][MAX_MATRIX_KEY_ROWS] = {
83+
{KEY_RSVD, 1, 112, 16, 2, KEY_RSVD, 30, KEY_RSVD},
84+
{116, 117, 110, KEY_RSVD, 118, 17, 18, KEY_RSVD},
85+
{113, KEY_RSVD, 114, 115, 3, KEY_RSVD, 119, KEY_RSVD},
86+
{49, 34, 48, 5, 4, 19, 20, KEY_RSVD},
87+
{35, 36, 21, 50, 6, 7, 22, 51},
88+
{23, 32, 33, 37, 8, 31, 52, 61},
89+
{38, 47, 46, KEY_RSVD, 9, 53, 24, 120},
90+
{25, 79, KEY_RSVD, 39, 10, 28, 13, KEY_RSVD},
91+
{41, KEY_RSVD, KEY_RSVD, 40, 27, 55, 11, 12},
92+
{KEY_RSVD, KEY_RSVD, KEY_RSVD, KEY_RSVD, 26, 90, 126, 121},
93+
{84, 71, KEY_RSVD, KEY_RSVD, 54, 122, 29, 15},
94+
{89, 83, KEY_RSVD, KEY_RSVD, 123, 75, 76, 43},
95+
{255, 59, KEY_RSVD, KEY_RSVD, KEY_RSVD, KEY_RSVD, 87, KEY_RSVD},
96+
{KEY_RSVD, KEY_RSVD, 44, 57, KEY_RSVD, KEY_RSVD, KEY_RSVD},
97+
{62, 60, KEY_RSVD, KEY_RSVD, KEY_RSVD, KEY_RSVD, KEY_RSVD, KEY_RSVD},
98+
{KEY_RSVD, KEY_RSVD, 58, KEY_RSVD, KEY_RSVD, KEY_RSVD, KEY_RSVD, 64},
99+
};
100+
101+
/* Key used for typematic */
102+
static u8_t last_key;
103+
104+
/* Typematic rate and delay values correspond to the data passed after
105+
* the F3 command (See the 8042 spec online)
106+
*/
107+
static const u16_t period[] = {
108+
33U, 37U, 42U, 46U, 50U, 54U, 58U, 63U,
109+
67U, 75U, 83U, 92U, 100U, 109U, 116U, 125U,
110+
133U, 149U, 167U, 182U, 200U, 217U, 232U, 250U,
111+
270U, 303U, 333U, 370U, 400U, 435U, 470U, 500U
112+
};
113+
114+
static const u16_t delay[] = { 250U, 500U, 750U, 1000U };
115+
116+
static bool block_kb_matrix;
117+
118+
static void typematic_callback(struct k_timer *timer)
119+
{
120+
LOG_INF("Typematic : %u\n", last_key);
121+
}
122+
123+
static void kb_callback(struct device *dev, u8_t row, u8_t col, bool pressed)
124+
{
125+
ARG_UNUSED(dev);
126+
last_key = keymap[col][row];
127+
128+
LOG_INF("Key code = %u Pressed = %u\n", last_key, pressed);
129+
130+
if (pressed) {
131+
k_timer_start(&typematic_timer,
132+
K_MSEC(delay[0]), K_MSEC(period[0]));
133+
} else {
134+
k_timer_stop(&typematic_timer);
135+
}
136+
}
137+
138+
static void block_matrix_callback(struct k_timer *timer)
139+
{
140+
LOG_DBG("block host : %d\n", block_kb_matrix);
141+
if (block_kb_matrix) {
142+
kscan_disable_callback(kscan_dev);
143+
block_kb_matrix = false;
144+
k_timer_stop(&typematic_timer);
145+
} else {
146+
kscan_enable_callback(kscan_dev);
147+
block_kb_matrix = true;
148+
}
149+
}
150+
151+
void main(void)
152+
{
153+
printk("Kscan matrix sample application\n");
154+
155+
kscan_dev = device_get_binding(DT_KSCAN_0_NAME);
156+
if (!kscan_dev) {
157+
LOG_ERR("kscan device %s not found", DT_KSCAN_0_NAME);
158+
return;
159+
}
160+
161+
kscan_config(kscan_dev, kb_callback);
162+
k_timer_init(&typematic_timer, typematic_callback, NULL);
163+
k_timer_init(&block_matrix_timer, block_matrix_callback, NULL);
164+
k_timer_start(&block_matrix_timer, K_SECONDS(1), K_SECONDS(3));
165+
166+
}
167+

0 commit comments

Comments
 (0)