-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLayer_BackgroundInterpolation.h
155 lines (131 loc) · 8.13 KB
/
Layer_BackgroundInterpolation.h
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
/*
* SmartMatrix Library - Background Layer Class
*
* Copyright (c) 2015 Louis Beaudoin (Pixelmatix)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _LAYER_BACKGROUND_INTERPOLATION_H_
#define _LAYER_BACKGROUND_INTERPOLATION_H_
#include "Layer.h"
#include "MatrixCommon.h"
#include "MatrixFontCommon.h"
#define SM_BACKGROUND_OPTIONS_NONE 0
#define BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS 3
#define SIZE_OF_BG_INT_CC_LUT (BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS >= 3 ? 4096 : (sizeof(SM_RGB) <= 3 ? 256 : 4096))
#define SMARTMATRIX_ALLOCATE_BACKGROUND_INTERPOLATION_LAYER(layer_name, width, height, storage_depth, background_options) \
typedef RGB_TYPE(storage_depth) SM_RGB; \
static RGB_TYPE(storage_depth) layer_name##Bitmap[BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS*width*height]; \
static color_chan_t layer_name##colorCorrectionLUT[SIZE_OF_BG_INT_CC_LUT]; \
static SMLayerBackgroundInterpolation<RGB_TYPE(storage_depth), background_options> layer_name(layer_name##Bitmap, width, height, layer_name##colorCorrectionLUT)
template <typename RGB, unsigned int optionFlags>
class SMLayerBackgroundInterpolation : public SM_Layer {
public:
SMLayerBackgroundInterpolation(RGB * buffer, uint16_t width, uint16_t height, color_chan_t * colorCorrectionLUT);
SMLayerBackgroundInterpolation(uint16_t width, uint16_t height);
void begin(void);
void frameRefreshCallback();
void fillRefreshRow(uint16_t hardwareY, rgb48 refreshRow[], int brightnessShifts = 0);
void fillRefreshRow(uint16_t hardwareY, rgb24 refreshRow[], int brightnessShifts = 0);
int getRequestedBrightnessShifts();
bool isLayerChanged();
void swapBuffers(bool copy = true, unsigned long interpolationPeriod_us = 0);
bool isSwapPending();
void copyRefreshToDrawing(void);
void setBrightnessShifts(int numShifts);
void updateInterpolationPeriod(unsigned long interpolationPeriod_us);
void drawPixel(int16_t x, int16_t y, const RGB& color);
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, const RGB& color);
void drawFastVLine(int16_t x, int16_t y0, int16_t y1, const RGB& color);
void drawFastHLine(int16_t x0, int16_t x1, int16_t y, const RGB& color);
void drawCircle(int16_t x0, int16_t y0, uint16_t radius, const RGB& color);
void fillCircle(int16_t x0, int16_t y0, uint16_t radius, const RGB& outlineColor, const RGB& fillColor);
void fillCircle(int16_t x0, int16_t y0, uint16_t radius, const RGB& color);
void drawEllipse(int16_t x0, int16_t y0, uint16_t radiusX, uint16_t radiusY, const RGB& color);
void drawTriangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3, const RGB& color);
void fillTriangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3, const RGB& fillColor);
void fillTriangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3,
const RGB& outlineColor, const RGB& fillColor);
void drawRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, const RGB& color);
void fillRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, const RGB& color);
void fillRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, const RGB& outlineColor, const RGB& fillColor);
void drawRoundRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t radius, const RGB& outlineColor);
void fillRoundRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t radius, const RGB& fillColor);
void fillRoundRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t radius,
const RGB& outlineColor, const RGB& fillColor);
void fillScreen(const RGB& color);
void drawChar(int16_t x, int16_t y, const RGB& charColor, char character);
void drawString(int16_t x, int16_t y, const RGB& charColor, const char text[]);
void drawString(int16_t x, int16_t y, const RGB& charColor, const RGB& backColor, const char text[]);
void drawMonoBitmap(int16_t x, int16_t y, uint8_t width, uint8_t height, const RGB& bitmapColor, const uint8_t *bitmap);
// reads pixel from drawing buffer, not refresh buffer
const RGB readPixel(int16_t x, int16_t y);
RGB *backBuffer(void);
void setBackBuffer(RGB *newBuffer);
RGB *getRealBackBuffer();
void setFont(fontChoices newFont);
void setBrightness(uint8_t brightness);
void enableColorCorrection(bool enabled);
void enableInterpolation(bool enabled);
private:
bool ccEnabled = true;
bool interpolationEnabled = (BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS >= 3 ? true : false);
RGB *currentDrawBufferPtr;
RGB *currentRefreshBufferPtr;
#if (BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS >= 3)
RGB *prevRefreshBufferPtr;
#endif
RGB *backgroundBuffers[BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS];
RGB *getCurrentRefreshRow(uint16_t y);
void loadPixelToDrawBuffer(int16_t hwx, int16_t hwy, const RGB& color);
const RGB readPixelFromDrawBuffer(int16_t hwx, int16_t hwy);
void getBackgroundRefreshPixel(uint16_t x, uint16_t y, RGB &refreshPixel);
bool getForegroundRefreshPixel(uint16_t x, uint16_t y, RGB &xyPixel);
// drawing functions not meant for user
void drawHardwareHLine(uint16_t x0, uint16_t x1, uint16_t y, const RGB& color);
void drawHardwareVLine(uint16_t x, uint16_t y0, uint16_t y1, const RGB& color);
void bresteepline(int16_t x3, int16_t y3, int16_t x4, int16_t y4, const RGB& color);
void fillFlatSideTriangleInt(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3, const RGB& color);
// todo: move somewhere else
static bool getBitmapPixelAtXY(uint8_t x, uint8_t y, uint8_t width, uint8_t height, const uint8_t *bitmap);
uint8_t backgroundBrightness = 255;
color_chan_t * backgroundColorCorrectionLUT;
bitmap_font *font;
// idealBrightnessShifts is the number of shifts towards MSB the pixel data can handle without overflowing
int idealBrightnessShifts = 0;
// pendingIdealBrightnessShifts keeps track of the data queued up with swapBuffers()
int pendingIdealBrightnessShifts = 0;
// keeping track of drawing buffers
volatile unsigned char currentDrawBuffer;
volatile unsigned char currentRefreshBuffer;
#if (BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS >= 3)
volatile unsigned char prevRefreshBuffer;
#endif
volatile bool swapPending;
void handleBufferSwap(void);
CircularBuffer_SM bufferPool;
#if (BACKGROUND_LAYER_INTERPOLATION_NUM_BUFFERS >= 3)
uint32_t calculateFcInterpCoefficient();
uint32_t icPrev;
uint32_t icNext;
unsigned long interpolationStartTime_micros;
unsigned long interpolationEndTime_micros;
#endif
};
#include "Layer_BackgroundInterpolation_Impl.h"
#endif