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/>.
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
)
46 static int displayPortTestRelease(displayPort_t
*displayPort
)
52 static int displayPortTestClearScreen(displayPort_t
*displayPort
, displayClearOption_e options
)
56 memset(testDisplayPortBuffer
, ' ', UNITTEST_DISPLAYPORT_BUFFER_LEN
);
60 static bool displayPortTestDrawScreen(displayPort_t
*displayPort
)
66 static int displayPortTestScreenSize(const displayPort_t
*displayPort
)
72 static int displayPortTestWriteString(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, const char *s
)
76 for (unsigned int i
= 0; i
< strlen(s
); i
++) {
77 testDisplayPortBuffer
[(y
* UNITTEST_DISPLAYPORT_COLS
) + x
+ i
] = s
[i
];
82 static int displayPortTestWriteChar(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, uint8_t c
)
86 testDisplayPortBuffer
[(y
* UNITTEST_DISPLAYPORT_COLS
) + x
] = c
;
90 static bool displayPortTestIsTransferInProgress(const displayPort_t
*displayPort
)
96 static int displayPortTestHeartbeat(displayPort_t
*displayPort
)
102 static void displayPortTestRedraw(displayPort_t
*displayPort
)
107 static int displayportWriteSys(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, displayPortSystemElement_e systemElement
)
112 UNUSED(systemElement
);
116 static uint32_t displayPortTestTxBytesFree(const displayPort_t
*displayPort
)
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
,
139 .writeFontCharacter
= NULL
,
141 .beginTransaction
= NULL
,
142 .commitTransaction
= 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) {
161 printf("%c", testDisplayPortBuffer
[i
]);
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
);
179 void displayPortTestBufferSubstring(int x
, int y
, const char * expectedFormat
, ...)
181 char expected
[UNITTEST_DISPLAYPORT_BUFFER_LEN
];
184 va_start(args
, expectedFormat
);
185 vsnprintf(expected
, UNITTEST_DISPLAYPORT_BUFFER_LEN
, expectedFormat
, args
);
189 displayPortTestPrint();
192 for (size_t i
= 0; i
< strlen(expected
); i
++) {
193 EXPECT_EQ(expected
[i
], testDisplayPortBuffer
[(y
* testDisplayPort
.cols
) + x
+ i
]);