2020-05-02 09:14:18 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Peter Johanson
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zephyr.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <drivers/kscan.h>
|
2020-05-02 13:10:11 +10:00
|
|
|
#include <logging/log.h>
|
2020-05-02 09:14:18 +10:00
|
|
|
|
2020-05-25 08:22:16 +10:00
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
2020-05-02 13:10:11 +10:00
|
|
|
|
2020-06-21 07:54:52 +10:00
|
|
|
#include <zmk/matrix_transform.h>
|
2020-06-30 14:31:09 +10:00
|
|
|
#include <zmk/event-manager.h>
|
|
|
|
#include <zmk/events/position-state-changed.h>
|
2020-05-02 13:37:37 +10:00
|
|
|
|
2020-05-02 13:10:11 +10:00
|
|
|
#define ZMK_KSCAN_EVENT_STATE_PRESSED 0
|
|
|
|
#define ZMK_KSCAN_EVENT_STATE_RELEASED 1
|
|
|
|
|
2020-05-11 07:13:24 +10:00
|
|
|
struct zmk_kscan_event
|
|
|
|
{
|
2020-05-02 13:10:11 +10:00
|
|
|
u32_t row;
|
|
|
|
u32_t column;
|
|
|
|
u32_t state;
|
|
|
|
};
|
|
|
|
|
2020-05-11 07:13:24 +10:00
|
|
|
struct zmk_kscan_msg_processor
|
|
|
|
{
|
2020-05-02 13:10:11 +10:00
|
|
|
struct k_work work;
|
|
|
|
} msg_processor;
|
|
|
|
|
|
|
|
K_MSGQ_DEFINE(zmk_kscan_msgq, sizeof(struct zmk_kscan_event), CONFIG_ZMK_KSCAN_EVENT_QUEUE_SIZE, 4);
|
|
|
|
|
|
|
|
static void zmk_kscan_callback(struct device *dev, u32_t row, u32_t column, bool pressed)
|
|
|
|
{
|
|
|
|
struct zmk_kscan_event ev = {
|
|
|
|
.row = row,
|
|
|
|
.column = column,
|
2020-05-11 07:13:24 +10:00
|
|
|
.state = (pressed ? ZMK_KSCAN_EVENT_STATE_PRESSED : ZMK_KSCAN_EVENT_STATE_RELEASED)};
|
2020-05-02 13:10:11 +10:00
|
|
|
|
|
|
|
k_msgq_put(&zmk_kscan_msgq, &ev, K_NO_WAIT);
|
|
|
|
k_work_submit(&msg_processor.work);
|
2020-05-02 09:14:18 +10:00
|
|
|
}
|
|
|
|
|
2020-05-02 13:10:11 +10:00
|
|
|
void zmk_kscan_process_msgq(struct k_work *item)
|
|
|
|
{
|
|
|
|
struct zmk_kscan_event ev;
|
|
|
|
|
2020-05-11 07:13:24 +10:00
|
|
|
while (k_msgq_get(&zmk_kscan_msgq, &ev, K_NO_WAIT) == 0)
|
|
|
|
{
|
2020-05-02 13:10:11 +10:00
|
|
|
bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED);
|
2020-06-21 07:54:52 +10:00
|
|
|
u32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column);
|
2020-06-30 14:31:09 +10:00
|
|
|
struct position_state_changed *pos_ev;
|
2020-06-21 07:54:52 +10:00
|
|
|
LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position, (pressed ? "true" : "false"));
|
2020-06-30 14:31:09 +10:00
|
|
|
pos_ev = new_position_state_changed();
|
|
|
|
pos_ev->state = pressed;
|
|
|
|
pos_ev->position = position;
|
|
|
|
ZMK_EVENT_RAISE(pos_ev);
|
2020-05-02 13:10:11 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:13:24 +10:00
|
|
|
int zmk_kscan_init(char *name)
|
2020-05-02 13:10:11 +10:00
|
|
|
{
|
2020-05-02 09:14:18 +10:00
|
|
|
struct device *dev = device_get_binding(name);
|
2020-05-11 07:13:24 +10:00
|
|
|
if (dev == NULL)
|
|
|
|
{
|
2020-05-02 13:10:11 +10:00
|
|
|
LOG_ERR("Failed to get the KSCAN device");
|
2020-05-02 09:14:18 +10:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:10:11 +10:00
|
|
|
k_work_init(&msg_processor.work, zmk_kscan_process_msgq);
|
2020-05-02 09:14:18 +10:00
|
|
|
|
2020-05-02 13:10:11 +10:00
|
|
|
kscan_config(dev, zmk_kscan_callback);
|
2020-05-02 09:14:18 +10:00
|
|
|
kscan_enable_callback(dev);
|
2020-05-04 05:49:33 +10:00
|
|
|
|
|
|
|
return 0;
|
2020-05-02 09:14:18 +10:00
|
|
|
}
|