update serTcpOpen declaration to fix compile errors (#14113)
[betaflight.git] / src / test / unit / unittest_displayport.h
blob81cd71517008874a026271edf590b2269b93b378
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 uint32_t displayPortTestTxBytesFree(const displayPort_t *displayPort)
109 UNUSED(displayPort);
110 return 0;
113 static const displayPortVTable_t testDisplayPortVTable = {
114 .grab = displayPortTestGrab,
115 .release = displayPortTestRelease,
116 .clearScreen = displayPortTestClearScreen,
117 .drawScreen = displayPortTestDrawScreen,
118 .screenSize = displayPortTestScreenSize,
119 .writeString = displayPortTestWriteString,
120 .writeChar = displayPortTestWriteChar,
121 .isTransferInProgress = displayPortTestIsTransferInProgress,
122 .heartbeat = displayPortTestHeartbeat,
123 .redraw = displayPortTestRedraw,
124 .txBytesFree = displayPortTestTxBytesFree
127 displayPort_t *displayPortTestInit(void)
129 displayInit(&testDisplayPort, &testDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
130 testDisplayPort.rows = UNITTEST_DISPLAYPORT_ROWS;
131 testDisplayPort.cols = UNITTEST_DISPLAYPORT_COLS;
132 return &testDisplayPort;
135 void displayPortTestPrint(void)
137 for (int i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
138 if (i > 0 && i % UNITTEST_DISPLAYPORT_COLS == 0) {
139 printf("\n");
141 printf("%c", testDisplayPortBuffer[i]);
143 printf("\n\n");
146 void displayPortTestBufferIsEmpty()
148 for (size_t i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
149 EXPECT_EQ(' ', testDisplayPortBuffer[i]);
150 if (testDisplayPortBuffer[i] != ' ') {
151 testDisplayPortBuffer[UNITTEST_DISPLAYPORT_BUFFER_LEN - 1] = '\0';
152 printf("FIRST ERROR AT:%d\r\n", (int)i);
153 printf("DISPLAY:%s\r\n", testDisplayPortBuffer);
154 break;
159 void displayPortTestBufferSubstring(int x, int y, const char * expectedFormat, ...)
161 char expected[UNITTEST_DISPLAYPORT_BUFFER_LEN];
163 va_list args;
164 va_start(args, expectedFormat);
165 vsnprintf(expected, UNITTEST_DISPLAYPORT_BUFFER_LEN, expectedFormat, args);
166 va_end(args);
168 #ifdef DEBUG_OSD
169 displayPortTestPrint();
170 #endif
172 for (size_t i = 0; i < strlen(expected); i++) {
173 EXPECT_EQ(expected[i], testDisplayPortBuffer[(y * testDisplayPort.cols) + x + i]);