~aleteoryx/muditaos

ref: 6c4c4e8aef35966ae5367d6e7f28ac2cdc73d8d1 muditaos/module-bsp/board/linux/battery-charger/battery_charger.cpp -rw-r--r-- 2.9 KiB
6c4c4e8a — Lucjan Bryndza [EGD-5098] Fix and remove old vfs class 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * battery_charger.cpp
 *
 *  Created on: Jul 1, 2019
 *      Author: kuba
 */
#include <stdint.h>

extern "C"
{
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
}

#include <sys/stat.h>
#include <fcntl.h>
#include <module-utils/common_data/EventStore.hpp>

#include "board.h"
#include "bsp/battery-charger/battery_charger.hpp"

#define BSP_BATTERY_CHARGER_I2C_ADDR (0xD2 >> 1)
#define BSP_FUEL_GAUGE_I2C_ADDR      (0x6C >> 1)
#define BSP_TOP_CONTROLLER_I2C_ADDR  (0xCC >> 1)

static xQueueHandle qHandleIrq            = NULL;
static TaskHandle_t battery_worker_handle = NULL;

static uint8_t battLevel = 100;
static bool plugged      = false;
namespace bsp
{

    static void battery_worker(void *pvp);

    int battery_Init(xQueueHandle qHandle)
    {
        qHandleIrq = qHandle;
        if (xTaskCreate(battery_worker, "battery", 512, qHandle, 0, &battery_worker_handle) != pdPASS) {
            return 1;
        }
        Store::Battery::modify().level = battLevel;
        return 0;
    }

    void battery_Deinit(void)
    {
        qHandleIrq = NULL;
        vTaskDelete(battery_worker_handle);
    }
    void battery_getBatteryLevel(uint8_t &levelPercent)
    {
        levelPercent = battLevel;
        Store::Battery::modify().level = battLevel;
    }

    void battery_getChargeStatus(bool &status)
    {
        status = plugged;
    }

    // TODO function unused in linux driver, left for compatibility with target driver
    void battery_ClearAllIRQs(void)
    {}
    // TODO function unused in linux driver, left for compatibility with target driver
    void battery_clearFuelGuageIRQ(void)
    {}

    static void battery_worker(void *pvp)
    {

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

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

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

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

            if (readedBytes > 0) {

                uint8_t notification = 0;
                switch (buff[0]) {
                case 'p':
                    notification = 0x02;
                    plugged      = 1 - plugged;
                    break;
                case ']':
                    notification = 0x01;
                    if (battLevel < 100)
                        battLevel++;
                    break;
                case '[':
                    notification = 0x01;
                    if (battLevel >= 1)
                        battLevel--;
                    break;
                }
                xQueueSend(qHandleIrq, &notification, 100);
            }
            vTaskDelay(50);
        }
    }
} // namespace bsp