2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
26 #if defined(USE_CRSF_CMS_TELEMETRY)
30 #include "common/maths.h"
31 #include "common/printf.h"
32 #include "common/time.h"
34 #include "drivers/display.h"
35 #include "drivers/time.h"
37 #include "displayport_crsf.h"
39 #define CRSF_DISPLAY_PORT_OPEN_DELAY_MS 400
40 #define CRSF_DISPLAY_PORT_CLEAR_DELAY_MS 45
42 static crsfDisplayPortScreen_t crsfScreen
;
43 static timeMs_t delayTransportUntilMs
= 0;
45 displayPort_t crsfDisplayPort
;
47 static int crsfGrab(displayPort_t
*displayPort
)
49 return displayPort
->grabCount
= 1;
52 static int crsfClearScreen(displayPort_t
*displayPort
)
55 memset(crsfScreen
.buffer
, ' ', sizeof(crsfScreen
.buffer
));
56 crsfScreen
.updated
= false;
57 crsfScreen
.reset
= true;
58 delayTransportUntilMs
= millis() + CRSF_DISPLAY_PORT_CLEAR_DELAY_MS
;
62 static int crsfRelease(displayPort_t
*displayPort
)
64 displayPort
->grabCount
= 0;
65 return crsfClearScreen(displayPort
);
68 static bool crsfDrawScreen(displayPort_t
*displayPort
)
74 static int crsfScreenSize(const displayPort_t
*displayPort
)
76 return displayPort
->rows
* displayPort
->cols
;
80 static int crsfWriteString(displayPort_t
*displayPort
, uint8_t col
, uint8_t row
, uint8_t attr
, const char *s
)
84 if (row
>= crsfScreen
.rows
|| col
>= crsfScreen
.cols
) {
87 const size_t truncLen
= MIN((int)strlen(s
), crsfScreen
.cols
-col
); // truncate at colCount
88 char *rowStart
= &crsfScreen
.buffer
[row
* crsfScreen
.cols
+ col
];
89 crsfScreen
.updated
|= memcmp(rowStart
, s
, truncLen
);
90 if (crsfScreen
.updated
) {
91 memcpy(rowStart
, s
, truncLen
);
96 static int crsfWriteChar(displayPort_t
*displayPort
, uint8_t col
, uint8_t row
, uint8_t attr
, uint8_t c
)
99 tfp_sprintf(s
, "%c", c
);
100 return crsfWriteString(displayPort
, col
, row
, attr
, s
);
103 static bool crsfIsTransferInProgress(const displayPort_t
*displayPort
)
109 static bool crsfIsSynced(const displayPort_t
*displayPort
)
115 static int crsfHeartbeat(displayPort_t
*displayPort
)
121 static void crsfRedraw(displayPort_t
*displayPort
)
123 displayPort
->rows
= crsfScreen
.rows
;
124 displayPort
->cols
= crsfScreen
.cols
;
127 static uint32_t crsfTxBytesFree(const displayPort_t
*displayPort
)
133 static const displayPortVTable_t crsfDisplayPortVTable
= {
135 .release
= crsfRelease
,
136 .clearScreen
= crsfClearScreen
,
137 .drawScreen
= crsfDrawScreen
,
138 .screenSize
= crsfScreenSize
,
139 .writeString
= crsfWriteString
,
140 .writeChar
= crsfWriteChar
,
141 .isTransferInProgress
= crsfIsTransferInProgress
,
142 .heartbeat
= crsfHeartbeat
,
143 .redraw
= crsfRedraw
,
144 .isSynced
= crsfIsSynced
,
145 .txBytesFree
= crsfTxBytesFree
,
146 .layerSupported
= NULL
,
151 crsfDisplayPortScreen_t
*crsfDisplayPortScreen(void)
156 void crsfDisplayPortMenuOpen(void)
161 if (cmsDisplayPortSelect(&crsfDisplayPort
)) {
163 delayTransportUntilMs
= millis() + CRSF_DISPLAY_PORT_OPEN_DELAY_MS
;
167 void crsfDisplayPortMenuExit(void)
172 uint8_t exitMenu
= CMS_EXIT
;
173 cmsMenuExit(&crsfDisplayPort
, &exitMenu
);
176 void crsfDisplayPortSetDimensions(uint8_t rows
, uint8_t cols
)
178 crsfScreen
.rows
= MIN(rows
, CRSF_DISPLAY_PORT_ROWS_MAX
);
179 crsfScreen
.cols
= MIN(cols
, CRSF_DISPLAY_PORT_COLS_MAX
);
180 crsfRedraw(&crsfDisplayPort
);
183 void crsfDisplayPortRefresh(void)
186 crsfDisplayPortMenuOpen();
189 crsfScreen
.updated
= true;
190 crsfScreen
.reset
= true;
191 delayTransportUntilMs
= millis() + CRSF_DISPLAY_PORT_CLEAR_DELAY_MS
;
194 bool crsfDisplayPortIsReady(void)
196 const timeMs_t currentTimeMs
= millis();
197 const bool delayExpired
= (currentTimeMs
> delayTransportUntilMs
);
198 const bool cmsReady
= (cmsInMenu
&& (pCurrentDisplay
== &crsfDisplayPort
));
199 return (bool)(delayExpired
&& cmsReady
);
202 static displayPort_t
*displayPortCrsfInit()
204 crsfDisplayPortSetDimensions(CRSF_DISPLAY_PORT_ROWS_MAX
, CRSF_DISPLAY_PORT_COLS_MAX
);
205 displayInit(&crsfDisplayPort
, &crsfDisplayPortVTable
, DISPLAYPORT_DEVICE_TYPE_CRSF
);
207 return &crsfDisplayPort
;
210 void crsfDisplayportRegister(void)
212 cmsDisplayPortRegister(displayPortCrsfInit());