Bump clang version to 18 (#14116)
[betaflight.git] / src / test / unit / unittest_displayport.h
blob6c02f2770baf0406ccf15e0902e6bda66f441442
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include <string.h>
21 #include <stdarg.h>
23 extern "C" {
24 #include "drivers/display.h"
27 #include "unittest_macros.h"
28 #include "gtest/gtest.h"
30 void displayPortTestBufferSubstring(int x, int y, const char * expectedFormat, ...) __attribute__ ((format (printf, 3, 4)));
32 #define UNITTEST_DISPLAYPORT_ROWS 16
33 #define UNITTEST_DISPLAYPORT_COLS 30
34 #define UNITTEST_DISPLAYPORT_BUFFER_LEN (UNITTEST_DISPLAYPORT_ROWS * UNITTEST_DISPLAYPORT_COLS)
36 char testDisplayPortBuffer[UNITTEST_DISPLAYPORT_BUFFER_LEN];
38 static displayPort_t testDisplayPort;
40 static int displayPortTestGrab(displayPort_t *displayPort)
42 UNUSED(displayPort);
43 return 0;
46 static int displayPortTestRelease(displayPort_t *displayPort)
48 UNUSED(displayPort);
49 return 0;
52 static int displayPortTestClearScreen(displayPort_t *displayPort, displayClearOption_e options)
54 UNUSED(displayPort);
55 UNUSED(options);
56 memset(testDisplayPortBuffer, ' ', UNITTEST_DISPLAYPORT_BUFFER_LEN);
57 return 0;
60 static bool displayPortTestDrawScreen(displayPort_t *displayPort)
62 UNUSED(displayPort);
63 return 0;
66 static int displayPortTestScreenSize(const displayPort_t *displayPort)
68 UNUSED(displayPort);
69 return 0;
72 static int displayPortTestWriteString(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, const char *s)
74 UNUSED(displayPort);
75 UNUSED(attr);
76 for (unsigned int i = 0; i < strlen(s); i++) {
77 testDisplayPortBuffer[(y * UNITTEST_DISPLAYPORT_COLS) + x + i] = s[i];
79 return 0;
82 static int displayPortTestWriteChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
84 UNUSED(displayPort);
85 UNUSED(attr);
86 testDisplayPortBuffer[(y * UNITTEST_DISPLAYPORT_COLS) + x] = c;
87 return 0;
90 static bool displayPortTestIsTransferInProgress(const displayPort_t *displayPort)
92 UNUSED(displayPort);
93 return 0;
96 static int displayPortTestHeartbeat(displayPort_t *displayPort)
98 UNUSED(displayPort);
99 return 0;
102 static void displayPortTestRedraw(displayPort_t *displayPort)
104 UNUSED(displayPort);
107 static int displayportWriteSys(displayPort_t *displayPort, uint8_t x, uint8_t y, displayPortSystemElement_e systemElement)
109 UNUSED(displayPort);
110 UNUSED(x);
111 UNUSED(y);
112 UNUSED(systemElement);
113 return 0;
116 static uint32_t displayPortTestTxBytesFree(const displayPort_t *displayPort)
118 UNUSED(displayPort);
119 return 0;
122 static const displayPortVTable_t testDisplayPortVTable = {
123 .grab = displayPortTestGrab,
124 .release = displayPortTestRelease,
125 .clearScreen = displayPortTestClearScreen,
126 .drawScreen = displayPortTestDrawScreen,
127 .screenSize = displayPortTestScreenSize,
128 .writeSys = displayportWriteSys,
129 .writeString = displayPortTestWriteString,
130 .writeChar = displayPortTestWriteChar,
131 .isTransferInProgress = displayPortTestIsTransferInProgress,
132 .heartbeat = displayPortTestHeartbeat,
133 .redraw = displayPortTestRedraw,
134 .isSynced = displayPortTestIsTransferInProgress,
135 .txBytesFree = displayPortTestTxBytesFree,
136 .layerSupported = NULL,
137 .layerSelect = NULL,
138 .layerCopy = NULL,
139 .writeFontCharacter = NULL,
140 .checkReady = NULL,
141 .beginTransaction = NULL,
142 .commitTransaction = NULL,
143 .getCanvas = NULL,
144 .setBackgroundType = NULL,
147 displayPort_t *displayPortTestInit(void)
149 displayInit(&testDisplayPort, &testDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
150 testDisplayPort.rows = UNITTEST_DISPLAYPORT_ROWS;
151 testDisplayPort.cols = UNITTEST_DISPLAYPORT_COLS;
152 return &testDisplayPort;
155 void displayPortTestPrint(void)
157 for (int i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
158 if (i > 0 && i % UNITTEST_DISPLAYPORT_COLS == 0) {
159 printf("\n");
161 printf("%c", testDisplayPortBuffer[i]);
163 printf("\n\n");
166 void displayPortTestBufferIsEmpty()
168 for (size_t i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
169 EXPECT_EQ(' ', testDisplayPortBuffer[i]);
170 if (testDisplayPortBuffer[i] != ' ') {
171 testDisplayPortBuffer[UNITTEST_DISPLAYPORT_BUFFER_LEN - 1] = '\0';
172 printf("FIRST ERROR AT:%d\r\n", (int)i);
173 printf("DISPLAY:%s\r\n", testDisplayPortBuffer);
174 break;
179 void displayPortTestBufferSubstring(int x, int y, const char * expectedFormat, ...)
181 char expected[UNITTEST_DISPLAYPORT_BUFFER_LEN];
183 va_list args;
184 va_start(args, expectedFormat);
185 vsnprintf(expected, UNITTEST_DISPLAYPORT_BUFFER_LEN, expectedFormat, args);
186 va_end(args);
188 #ifdef DEBUG_OSD
189 displayPortTestPrint();
190 #endif
192 for (size_t i = 0; i < strlen(expected); i++) {
193 EXPECT_EQ(expected[i], testDisplayPortBuffer[(y * testDisplayPort.cols) + x + i]);