~aleteoryx/muditaos

ref: d066f87772f0c8878714153022cae8d479d678a2 muditaos/module-bsp/board/rt1051/bsp/battery-charger/battery_charger_utils.hpp -rw-r--r-- 687 bytes
d066f877 — Wojtek Rzepecki [EGD-6431] Add battery charging temperature ranges 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once

#include <bitset>

namespace bsp::battery_charger::utils
{
    template <typename T> int twosComplimentToInt(T toConvert)
    {
        constexpr auto bitSize = sizeof(T) * 8;
        std::bitset<bitSize> bitset{toConvert};

        if (bitset[bitSize - 1]) {
            bitset = std::bitset<bitSize>(toConvert - 1);
            bitset.flip();
            return static_cast<int>(bitset.to_ulong() * -1);
        }
        else {
            return static_cast<int>(toConvert);
        }
    }
} // namespace bsp::battery_charger::utils