Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / io / displayport_crsf.c
blobf6cb031a90188849f0deb2036edc51adc46f41b4
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <string.h>
24 #include "platform.h"
26 #if defined(USE_CRSF_CMS_TELEMETRY)
28 #include "cms/cms.h"
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)
54 UNUSED(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;
59 return 0;
62 static int crsfRelease(displayPort_t *displayPort)
64 displayPort->grabCount = 0;
65 return crsfClearScreen(displayPort);
68 static bool crsfDrawScreen(displayPort_t *displayPort)
70 UNUSED(displayPort);
71 return 0;
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)
82 UNUSED(displayPort);
83 UNUSED(attr);
84 if (row >= crsfScreen.rows || col >= crsfScreen.cols) {
85 return 0;
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);
93 return 0;
96 static int crsfWriteChar(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, uint8_t c)
98 char s[1];
99 tfp_sprintf(s, "%c", c);
100 return crsfWriteString(displayPort, col, row, attr, s);
103 static bool crsfIsTransferInProgress(const displayPort_t *displayPort)
105 UNUSED(displayPort);
106 return false;
109 static bool crsfIsSynced(const displayPort_t *displayPort)
111 UNUSED(displayPort);
112 return true;
115 static int crsfHeartbeat(displayPort_t *displayPort)
117 UNUSED(displayPort);
118 return 0;
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)
129 UNUSED(displayPort);
130 return UINT32_MAX;
133 static const displayPortVTable_t crsfDisplayPortVTable = {
134 .grab = crsfGrab,
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,
147 .layerSelect = NULL,
148 .layerCopy = NULL,
151 crsfDisplayPortScreen_t *crsfDisplayPortScreen(void)
153 return &crsfScreen;
156 void crsfDisplayPortMenuOpen(void)
158 if (cmsInMenu) {
159 return;
161 if (cmsDisplayPortSelect(&crsfDisplayPort)) {
162 cmsMenuOpen();
163 delayTransportUntilMs = millis() + CRSF_DISPLAY_PORT_OPEN_DELAY_MS;
167 void crsfDisplayPortMenuExit(void)
169 if (!cmsInMenu) {
170 return;
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)
185 if (!cmsInMenu) {
186 crsfDisplayPortMenuOpen();
187 return;
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());
214 #endif