Updated and Validated
[betaflight.git] / src / test / unit / unittest_displayport.h
blob42908c3f55e9b01b5ba8c0c439e3779a8f029bb1
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>
22 extern "C" {
23 #include "drivers/display.h"
26 #include "unittest_macros.h"
27 #include "gtest/gtest.h"
29 void displayPortTestBufferSubstring(int x, int y, const char * expectedFormat, ...) __attribute__ ((format (printf, 3, 4)));
31 #define UNITTEST_DISPLAYPORT_ROWS 16
32 #define UNITTEST_DISPLAYPORT_COLS 30
33 #define UNITTEST_DISPLAYPORT_BUFFER_LEN (UNITTEST_DISPLAYPORT_ROWS * UNITTEST_DISPLAYPORT_COLS)
35 char testDisplayPortBuffer[UNITTEST_DISPLAYPORT_BUFFER_LEN];
37 static displayPort_t testDisplayPort;
39 static int displayPortTestGrab(displayPort_t *displayPort)
41 UNUSED(displayPort);
42 return 0;
45 static int displayPortTestRelease(displayPort_t *displayPort)
47 UNUSED(displayPort);
48 return 0;
51 static int displayPortTestClearScreen(displayPort_t *displayPort, displayClearOption_e options)
53 UNUSED(displayPort);
54 UNUSED(options);
55 memset(testDisplayPortBuffer, ' ', UNITTEST_DISPLAYPORT_BUFFER_LEN);
56 return 0;
59 static bool displayPortTestDrawScreen(displayPort_t *displayPort)
61 UNUSED(displayPort);
62 return 0;
65 static int displayPortTestScreenSize(const displayPort_t *displayPort)
67 UNUSED(displayPort);
68 return 0;
71 static int displayPortTestWriteString(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, const char *s)
73 UNUSED(displayPort);
74 UNUSED(attr);
75 for (unsigned int i = 0; i < strlen(s); i++) {
76 testDisplayPortBuffer[(y * UNITTEST_DISPLAYPORT_COLS) + x + i] = s[i];
78 return 0;
81 static int displayPortTestWriteChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
83 UNUSED(displayPort);
84 UNUSED(attr);
85 testDisplayPortBuffer[(y * UNITTEST_DISPLAYPORT_COLS) + x] = c;
86 return 0;
89 static bool displayPortTestIsTransferInProgress(const displayPort_t *displayPort)
91 UNUSED(displayPort);
92 return 0;
95 static int displayPortTestHeartbeat(displayPort_t *displayPort)
97 UNUSED(displayPort);
98 return 0;
101 static void displayPortTestRedraw(displayPort_t *displayPort)
103 UNUSED(displayPort);
106 static uint32_t displayPortTestTxBytesFree(const displayPort_t *displayPort)
108 UNUSED(displayPort);
109 return 0;
112 static const displayPortVTable_t testDisplayPortVTable = {
113 .grab = displayPortTestGrab,
114 .release = displayPortTestRelease,
115 .clearScreen = displayPortTestClearScreen,
116 .drawScreen = displayPortTestDrawScreen,
117 .screenSize = displayPortTestScreenSize,
118 .writeString = displayPortTestWriteString,
119 .writeChar = displayPortTestWriteChar,
120 .isTransferInProgress = displayPortTestIsTransferInProgress,
121 .heartbeat = displayPortTestHeartbeat,
122 .redraw = displayPortTestRedraw,
123 .txBytesFree = displayPortTestTxBytesFree
126 displayPort_t *displayPortTestInit(void)
128 displayInit(&testDisplayPort, &testDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
129 testDisplayPort.rows = UNITTEST_DISPLAYPORT_ROWS;
130 testDisplayPort.cols = UNITTEST_DISPLAYPORT_COLS;
131 return &testDisplayPort;
134 void displayPortTestPrint(void)
136 for (int i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
137 if (i > 0 && i % UNITTEST_DISPLAYPORT_COLS == 0) {
138 printf("\n");
140 printf("%c", testDisplayPortBuffer[i]);
142 printf("\n\n");
145 void displayPortTestBufferIsEmpty()
147 for (size_t i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
148 EXPECT_EQ(' ', testDisplayPortBuffer[i]);
149 if (testDisplayPortBuffer[i] != ' ') {
150 testDisplayPortBuffer[UNITTEST_DISPLAYPORT_BUFFER_LEN - 1] = '\0';
151 printf("FIRST ERROR AT:%d\r\n", (int)i);
152 printf("DISPLAY:%s\r\n", testDisplayPortBuffer);
153 break;
158 void displayPortTestBufferSubstring(int x, int y, const char * expectedFormat, ...)
160 char expected[UNITTEST_DISPLAYPORT_BUFFER_LEN];
162 va_list args;
163 va_start(args, expectedFormat);
164 vsnprintf(expected, UNITTEST_DISPLAYPORT_BUFFER_LEN, expectedFormat, args);
165 va_end(args);
167 #ifdef DEBUG_OSD
168 displayPortTestPrint();
169 #endif
171 for (size_t i = 0; i < strlen(expected); i++) {
172 EXPECT_EQ(expected[i], testDisplayPortBuffer[(y * testDisplayPort.cols) + x + i]);