~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-bsp/board/linux/keyboard/keyboard.cpp -rw-r--r-- 2.2 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 years ago
                                                                                
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * linux_keyboard.cpp
 *
 *  Created on: May 27, 2019
 *      Author: kuba
 */

#include <iostream>

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <memory>

#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "timers.h"

#include <sys/stat.h>
#include <fcntl.h>

#include "bsp/keyboard/keyboard.hpp"

namespace bsp
{

    static TaskHandle_t linux_keyboard_worker_handle = NULL;

    static uint8_t kcode  = 0;
    static uint8_t kevent = 0;

    static int fd;

    static void linux_keyboard_worker(void *pvp)
    {

        const char *myfifo = "/tmp/myfifo3";

        // Creating the named file(FIFO)
        // mkfifo(<pathname>, <permission>)
        mkfifo(myfifo, 0666);

        // Open FIFO for write only
        fd = open(myfifo, O_RDONLY | O_NONBLOCK);

        while (1) {
            uint8_t buff[10];
            int32_t readedBytes = read(fd, buff, 10);

            if (readedBytes > 1) {
                kevent = buff[0];
                kcode  = buff[1];

                xQueueHandle qhandle = reinterpret_cast<xQueueHandle>(pvp);

                uint8_t notification = 0;
                if (kcode == static_cast<uint8_t>(bsp::KeyCodes::FnRight)) {
                    if (kevent == static_cast<uint8_t>(bsp::KeyEvents::Pressed))
                        notification = 0x02;
                    else
                        notification = 0x04;
                }
                else
                    notification = 0x01;
                xQueueSend(qhandle, &notification, 100);
            }
            vTaskDelay(50);
        }

        close(fd);
    }

    void keyboard_get_data(const uint8_t &notification, uint8_t &event, uint8_t &code)
    {
        event = kevent;
        code  = kcode;
    }

    int32_t keyboard_Init(xQueueHandle qHandle)
    {

        if (xTaskCreate(linux_keyboard_worker, "keyboard", 512, qHandle, 0, &linux_keyboard_worker_handle) != pdPASS) {
            return 1;
        }
        return 0;
    }

    int32_t keyboard_Deinit(void)
    {
        vTaskDelete(linux_keyboard_worker_handle);
        close(fd);
        return 0;
    }

} // namespace bsp