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
, displayClearOption_e options
)
55 memset(testDisplayPortBuffer
, ' ', UNITTEST_DISPLAYPORT_BUFFER_LEN
);
59 static bool displayPortTestDrawScreen(displayPort_t
*displayPort
)
65 static int displayPortTestScreenSize(const displayPort_t
*displayPort
)
71 static int displayPortTestWriteString(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, const char *s
)
75 for (unsigned int i
= 0; i
< strlen(s
); i
++) {
76 testDisplayPortBuffer
[(y
* UNITTEST_DISPLAYPORT_COLS
) + x
+ i
] = s
[i
];
81 static int displayPortTestWriteChar(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, uint8_t c
)
85 testDisplayPortBuffer
[(y
* UNITTEST_DISPLAYPORT_COLS
) + x
] = c
;
89 static bool displayPortTestIsTransferInProgress(const displayPort_t
*displayPort
)
95 static int displayPortTestHeartbeat(displayPort_t
*displayPort
)
101 static void displayPortTestRedraw(displayPort_t
*displayPort
)
106 static uint32_t displayPortTestTxBytesFree(const displayPort_t
*displayPort
)
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) {
140 printf("%c", testDisplayPortBuffer
[i
]);
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
);
158 void displayPortTestBufferSubstring(int x
, int y
, const char * expectedFormat
, ...)
160 char expected
[UNITTEST_DISPLAYPORT_BUFFER_LEN
];
163 va_start(args
, expectedFormat
);
164 vsnprintf(expected
, UNITTEST_DISPLAYPORT_BUFFER_LEN
, expectedFormat
, args
);
168 displayPortTestPrint();
171 for (size_t i
= 0; i
< strlen(expected
); i
++) {
172 EXPECT_EQ(expected
[i
], testDisplayPortBuffer
[(y
* testDisplayPort
.cols
) + x
+ i
]);