Move telemetry displayport init and cms device registering
[betaflight.git] / src / test / unit / unittest_displayport.h
blob88168b42f89aa0c2f76688b25fef3afe46f47520
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)
53 UNUSED(displayPort);
54 memset(testDisplayPortBuffer, ' ', UNITTEST_DISPLAYPORT_BUFFER_LEN);
55 return 0;
58 static bool displayPortTestDrawScreen(displayPort_t *displayPort)
60 UNUSED(displayPort);
61 return 0;
64 static int displayPortTestScreenSize(const displayPort_t *displayPort)
66 UNUSED(displayPort);
67 return 0;
70 static int displayPortTestWriteString(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, const char *s)
72 UNUSED(displayPort);
73 UNUSED(attr);
74 for (unsigned int i = 0; i < strlen(s); i++) {
75 testDisplayPortBuffer[(y * UNITTEST_DISPLAYPORT_COLS) + x + i] = s[i];
77 return 0;
80 static int displayPortTestWriteChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
82 UNUSED(displayPort);
83 UNUSED(attr);
84 testDisplayPortBuffer[(y * UNITTEST_DISPLAYPORT_COLS) + x] = c;
85 return 0;
88 static bool displayPortTestIsTransferInProgress(const displayPort_t *displayPort)
90 UNUSED(displayPort);
91 return 0;
94 static int displayPortTestHeartbeat(displayPort_t *displayPort)
96 UNUSED(displayPort);
97 return 0;
100 static void displayPortTestRedraw(displayPort_t *displayPort)
102 UNUSED(displayPort);
105 static uint32_t displayPortTestTxBytesFree(const displayPort_t *displayPort)
107 UNUSED(displayPort);
108 return 0;
111 static const displayPortVTable_t testDisplayPortVTable = {
112 .grab = displayPortTestGrab,
113 .release = displayPortTestRelease,
114 .clearScreen = displayPortTestClearScreen,
115 .drawScreen = displayPortTestDrawScreen,
116 .screenSize = displayPortTestScreenSize,
117 .writeString = displayPortTestWriteString,
118 .writeChar = displayPortTestWriteChar,
119 .isTransferInProgress = displayPortTestIsTransferInProgress,
120 .heartbeat = displayPortTestHeartbeat,
121 .redraw = displayPortTestRedraw,
122 .txBytesFree = displayPortTestTxBytesFree
125 displayPort_t *displayPortTestInit(void)
127 displayInit(&testDisplayPort, &testDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
128 testDisplayPort.rows = UNITTEST_DISPLAYPORT_ROWS;
129 testDisplayPort.cols = UNITTEST_DISPLAYPORT_COLS;
130 return &testDisplayPort;
133 void displayPortTestPrint(void)
135 for (int i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
136 if (i > 0 && i % UNITTEST_DISPLAYPORT_COLS == 0) {
137 printf("\n");
139 printf("%c", testDisplayPortBuffer[i]);
141 printf("\n\n");
144 void displayPortTestBufferIsEmpty()
146 for (size_t i = 0; i < UNITTEST_DISPLAYPORT_BUFFER_LEN; i++) {
147 EXPECT_EQ(' ', testDisplayPortBuffer[i]);
148 if (testDisplayPortBuffer[i] != ' ') {
149 testDisplayPortBuffer[UNITTEST_DISPLAYPORT_BUFFER_LEN - 1] = '\0';
150 printf("FIRST ERROR AT:%d\r\n", (int)i);
151 printf("DISPLAY:%s\r\n", testDisplayPortBuffer);
152 break;
157 void displayPortTestBufferSubstring(int x, int y, const char * expectedFormat, ...)
159 char expected[UNITTEST_DISPLAYPORT_BUFFER_LEN];
161 va_list args;
162 va_start(args, expectedFormat);
163 vsnprintf(expected, UNITTEST_DISPLAYPORT_BUFFER_LEN, expectedFormat, args);
164 va_end(args);
166 #ifdef DEBUG_OSD
167 displayPortTestPrint();
168 #endif
170 for (size_t i = 0; i < strlen(expected); i++) {
171 EXPECT_EQ(expected[i], testDisplayPortBuffer[(y * testDisplayPort.cols) + x + i]);