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/>.
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
)
45 static int displayPortTestRelease(displayPort_t
*displayPort
)
51 static int displayPortTestClearScreen(displayPort_t
*displayPort
)
54 memset(testDisplayPortBuffer
, ' ', UNITTEST_DISPLAYPORT_BUFFER_LEN
);
58 static bool displayPortTestDrawScreen(displayPort_t
*displayPort
)
64 static int displayPortTestScreenSize(const displayPort_t
*displayPort
)
70 static int displayPortTestWriteString(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, const char *s
)
74 for (unsigned int i
= 0; i
< strlen(s
); i
++) {
75 testDisplayPortBuffer
[(y
* UNITTEST_DISPLAYPORT_COLS
) + x
+ i
] = s
[i
];
80 static int displayPortTestWriteChar(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, uint8_t c
)
84 testDisplayPortBuffer
[(y
* UNITTEST_DISPLAYPORT_COLS
) + x
] = c
;
88 static bool displayPortTestIsTransferInProgress(const displayPort_t
*displayPort
)
94 static int displayPortTestHeartbeat(displayPort_t
*displayPort
)
100 static void displayPortTestRedraw(displayPort_t
*displayPort
)
105 static uint32_t displayPortTestTxBytesFree(const displayPort_t
*displayPort
)
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) {
139 printf("%c", testDisplayPortBuffer
[i
]);
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
);
157 void displayPortTestBufferSubstring(int x
, int y
, const char * expectedFormat
, ...)
159 char expected
[UNITTEST_DISPLAYPORT_BUFFER_LEN
];
162 va_start(args
, expectedFormat
);
163 vsnprintf(expected
, UNITTEST_DISPLAYPORT_BUFFER_LEN
, expectedFormat
, args
);
167 displayPortTestPrint();
170 for (size_t i
= 0; i
< strlen(expected
); i
++) {
171 EXPECT_EQ(expected
[i
], testDisplayPortBuffer
[(y
* testDisplayPort
.cols
) + x
+ i
]);