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 uint32_t displayPortTestTxBytesFree(const displayPort_t
*displayPort
)
113 static const displayPortVTable_t testDisplayPortVTable
= {
114 .grab
= displayPortTestGrab
,
115 .release
= displayPortTestRelease
,
116 .clearScreen
= displayPortTestClearScreen
,
117 .drawScreen
= displayPortTestDrawScreen
,
118 .screenSize
= displayPortTestScreenSize
,
119 .writeString
= displayPortTestWriteString
,
120 .writeChar
= displayPortTestWriteChar
,
121 .isTransferInProgress
= displayPortTestIsTransferInProgress
,
122 .heartbeat
= displayPortTestHeartbeat
,
123 .redraw
= displayPortTestRedraw
,
124 .txBytesFree
= displayPortTestTxBytesFree
127 displayPort_t
*displayPortTestInit(void)
129 displayInit(&testDisplayPort
, &testDisplayPortVTable
, DISPLAYPORT_DEVICE_TYPE_MAX7456
);
130 testDisplayPort
.rows
= UNITTEST_DISPLAYPORT_ROWS
;
131 testDisplayPort
.cols
= UNITTEST_DISPLAYPORT_COLS
;
132 return &testDisplayPort
;
135 void displayPortTestPrint(void)
137 for (int i
= 0; i
< UNITTEST_DISPLAYPORT_BUFFER_LEN
; i
++) {
138 if (i
> 0 && i
% UNITTEST_DISPLAYPORT_COLS
== 0) {
141 printf("%c", testDisplayPortBuffer
[i
]);
146 void displayPortTestBufferIsEmpty()
148 for (size_t i
= 0; i
< UNITTEST_DISPLAYPORT_BUFFER_LEN
; i
++) {
149 EXPECT_EQ(' ', testDisplayPortBuffer
[i
]);
150 if (testDisplayPortBuffer
[i
] != ' ') {
151 testDisplayPortBuffer
[UNITTEST_DISPLAYPORT_BUFFER_LEN
- 1] = '\0';
152 printf("FIRST ERROR AT:%d\r\n", (int)i
);
153 printf("DISPLAY:%s\r\n", testDisplayPortBuffer
);
159 void displayPortTestBufferSubstring(int x
, int y
, const char * expectedFormat
, ...)
161 char expected
[UNITTEST_DISPLAYPORT_BUFFER_LEN
];
164 va_start(args
, expectedFormat
);
165 vsnprintf(expected
, UNITTEST_DISPLAYPORT_BUFFER_LEN
, expectedFormat
, args
);
169 displayPortTestPrint();
172 for (size_t i
= 0; i
< strlen(expected
); i
++) {
173 EXPECT_EQ(expected
[i
], testDisplayPortBuffer
[(y
* testDisplayPort
.cols
) + x
+ i
]);