~aleteoryx/muditaos

ref: 661d20329730b1399e4aab17c4619076cb6e229a muditaos/module-services/service-eink/EinkDisplay.cpp -rw-r--r-- 5.9 KiB
661d2032 — Marcin Smoczyński add changelog for v0.54.2 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "EinkDisplay.hpp"

#include <gui/core/Color.hpp>
#include <gsl/gsl_util>

#include <cstdio>
#include <cstring>

namespace service::eink
{
    namespace
    {
        constexpr auto DefaultSurroundingTemperature = -1000;
        constexpr auto LutsFileName                  = "Luts.bin";
        constexpr auto LUTDSize                      = 16385;
        constexpr auto LUTCSize                      = 64;
        constexpr auto LUTRSize                      = 256; ///< Needed due to \ref EINK_LUTS_FILE_PATH structure
        constexpr auto LUTSTotalSize                 = LUTDSize + LUTCSize + LUTRSize;
        constexpr auto LUTVersionInterval            = 3;
        constexpr auto LUTSubcritical                = 12;
        constexpr auto LUTCritical                   = 13;

        EinkWaveFormSettings_t createDefaultWaveFormSettings(EinkWaveforms_e waveformMode)
        {
            EinkWaveFormSettings_t settings{};
            settings.mode        = waveformMode;
            settings.temperature = DefaultSurroundingTemperature;
            settings.LUTCData    = nullptr;
            settings.LUTCSize    = 0;
            settings.LUTDData    = nullptr;
            settings.LUTDSize    = 0;
            return settings;
        }
    } // namespace

    EinkDisplay::EinkDisplay(::gui::Size screenSize)
        : size{screenSize}, waveformSettings{createDefaultWaveFormSettings(EinkWaveformGC16)},
          displayMode{EinkDisplayColorMode_e::EinkDisplayColorModeStandard}
    {}

    EinkDisplay::~EinkDisplay() noexcept
    {
        delete[] waveformSettings.LUTCData;
        delete[] waveformSettings.LUTDData;
    }

    EinkStatus_e EinkDisplay::resetAndInit()
    {
        return EinkResetAndInitialize();
    }

    void EinkDisplay::dither()
    {
        EinkDitherDisplay();
    }

    void EinkDisplay::powerOn()
    {
        EinkPowerOn();
    }

    void EinkDisplay::powerOff()
    {
        EinkPowerOff();
    }

    void EinkDisplay::shutdown()
    {
        EinkPowerDown();
    }

    EinkStatus_e EinkDisplay::update(std::uint8_t *displayBuffer)
    {
        return EinkUpdateFrame(pointTopLeft.x,
                               pointTopLeft.y,
                               size.width,
                               size.height,
                               displayBuffer,
                               getCurrentBitsPerPixelFormat(),
                               displayMode);
    }

    EinkBpp_e EinkDisplay::getCurrentBitsPerPixelFormat() const noexcept
    {
        if (waveformSettings.mode == EinkWaveformDU2) {
            return Eink1Bpp;
        }
        return Eink4Bpp;
    }

    EinkStatus_e EinkDisplay::refresh(EinkDisplayTimingsMode_e refreshMode)
    {
        return EinkRefreshImage(pointTopLeft.x, pointTopLeft.y, size.width, size.height, refreshMode);
    }

    bool EinkDisplay::changeWaveform(EinkWaveforms_e mode, std::int32_t temperature)
    {
        if (temperature == waveformSettings.temperature && mode == waveformSettings.mode) {
            return EinkOK;
        }
        waveformSettings.temperature = temperature;
        waveformSettings.mode        = mode;

        const auto segment = calculateWaveFormSegment(temperature);
        auto offset        = calculateWaveFormOffset(mode, segment);

        auto file = std::fopen(LutsFileName, "rb");
        if (file == nullptr) {
            LOG_FATAL("Could not find the LUTS.bin file. Returning");
            return false;
        }
        auto fileHandlerCleanup = gsl::finally([&file]() { std::fclose(file); });

        resetWaveFormSettings();
        std::fseek(file, offset, SEEK_SET);
        std::fread(&waveformSettings.LUTDData[1], 1, LUTDSize, file);

        // 0x00 - 1 frame, ... , 0x0F - 16 frames
        const uint8_t frameCount = waveformSettings.LUTDData[1] + 1;
        // (frameCount * 64) - size of actual LUT; (+1) - the byte containing frameCount; (+1) - EinkLUTD command
        waveformSettings.LUTDSize = (frameCount * 64) + 1 + 1;

        offset += LUTDSize;
        std::fseek(file, offset, SEEK_SET);
        std::fread(&waveformSettings.LUTCData[1], 1, LUTCSize, file);

        EinkUpdateWaveform(&waveformSettings);
        return true;
    }

    unsigned int EinkDisplay::calculateWaveFormSegment(std::int32_t temperature) const
    {
        if (temperature < 38) {
            return temperature / LUTVersionInterval;
        }
        if (temperature < 43) {
            return LUTSubcritical;
        }
        return LUTCritical;
    }

    unsigned int EinkDisplay::calculateWaveFormOffset(EinkWaveforms_e mode, unsigned int segment) const
    {
        switch (mode) {
        case EinkWaveformINIT:
            return LUTSTotalSize * segment;
        case EinkWaveformA2:
            return LUTSTotalSize * (14 + segment);
        case EinkWaveformDU2:
            return LUTSTotalSize * (28 + segment);
        case EinkWaveformGLD16:
            return LUTSTotalSize * (42 + segment);
        case EinkWaveformGC16:
            return LUTSTotalSize * (56 + segment);
        }
        throw std::invalid_argument{"Invalid waveform mode."};
    }

    void EinkDisplay::resetWaveFormSettings()
    {
        delete[] waveformSettings.LUTDData;
        waveformSettings.LUTDSize    = 0;
        waveformSettings.LUTDData    = new std::uint8_t[LUTDSize + 1];
        waveformSettings.LUTDData[0] = EinkLUTD;

        delete[] waveformSettings.LUTCData;
        waveformSettings.LUTCSize    = LUTCSize;
        waveformSettings.LUTCData    = new std::uint8_t[LUTCSize + 1];
        waveformSettings.LUTCData[0] = EinkLUTC;
    }

    void EinkDisplay::setMode(EinkDisplayColorMode_e mode) noexcept
    {
        displayMode = mode;
    }

    ::gui::Size EinkDisplay::getSize() const noexcept
    {
        return size;
    }
} // namespace service::eink