Adding support for UART0 (#14094)
[betaflight.git] / src / main / io / displayport_msp.c
blob08f46491e44415dfb993ef749b7499d8a47082d8
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 <stdint.h>
23 #include <string.h>
24 #include <ctype.h>
26 #include "platform.h"
28 #ifdef USE_MSP_DISPLAYPORT
30 #include "cli/cli.h"
32 #include "common/utils.h"
34 #include "drivers/display.h"
35 #include "drivers/osd.h"
37 #include "io/displayport_msp.h"
39 #include "msp/msp.h"
40 #include "msp/msp_protocol.h"
41 #include "msp/msp_serial.h"
43 #include "osd/osd.h"
45 #include "pg/vcd.h"
47 static displayPort_t mspDisplayPort;
48 static serialPortIdentifier_e displayPortSerial;
50 static int output(displayPort_t *displayPort, uint8_t cmd, uint8_t *buf, int len)
52 UNUSED(displayPort);
54 return mspSerialPush(displayPortSerial, cmd, buf, len, MSP_DIRECTION_REPLY, MSP_V1);
57 static int heartbeat(displayPort_t *displayPort)
59 uint8_t subcmd[] = { MSP_DP_HEARTBEAT };
61 // heartbeat is used to:
62 // a) ensure display is not released by MW OSD software
63 // b) prevent OSD Slave boards from displaying a 'disconnected' status.
64 output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
66 return 0;
69 static int grab(displayPort_t *displayPort)
71 return heartbeat(displayPort);
74 static int release(displayPort_t *displayPort)
76 uint8_t subcmd[] = { MSP_DP_RELEASE };
78 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
81 static int clearScreen(displayPort_t *displayPort, displayClearOption_e options)
83 UNUSED(options);
85 uint8_t subcmd[] = { MSP_DP_CLEAR_SCREEN };
87 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
90 static bool drawScreen(displayPort_t *displayPort)
92 uint8_t subcmd[] = { MSP_DP_DRAW_SCREEN };
93 output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
95 return 0;
98 static int screenSize(const displayPort_t *displayPort)
100 return displayPort->rows * displayPort->cols;
103 static int writeString(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, const char *string)
105 #define MSP_OSD_MAX_STRING_LENGTH 30 // FIXME move this
106 uint8_t buf[MSP_OSD_MAX_STRING_LENGTH + 4];
108 int len = strlen(string);
109 if (len >= MSP_OSD_MAX_STRING_LENGTH) {
110 len = MSP_OSD_MAX_STRING_LENGTH;
113 buf[0] = MSP_DP_WRITE_STRING;
114 buf[1] = row;
115 buf[2] = col;
116 buf[3] = displayPortProfileMsp()->fontSelection[attr & (DISPLAYPORT_SEVERITY_COUNT - 1)] & DISPLAYPORT_MSP_ATTR_FONT;
118 if (attr & DISPLAYPORT_BLINK) {
119 buf[3] |= DISPLAYPORT_MSP_ATTR_BLINK;
122 memcpy(&buf[4], string, len);
124 return output(displayPort, MSP_DISPLAYPORT, buf, len + 4);
127 static int writeSys(displayPort_t *displayPort, uint8_t col, uint8_t row, displayPortSystemElement_e systemElement)
129 uint8_t syscmd[4];
131 syscmd[0] = MSP_DP_SYS;
132 syscmd[1] = row;
133 syscmd[2] = col;
134 syscmd[3] = systemElement;
136 return output(displayPort, MSP_DISPLAYPORT, syscmd, sizeof(syscmd));
139 static int writeChar(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, uint8_t c)
141 char buf[2];
143 buf[0] = c;
144 buf[1] = 0;
145 return writeString(displayPort, col, row, attr, buf);
148 static bool isTransferInProgress(const displayPort_t *displayPort)
150 UNUSED(displayPort);
151 return false;
154 static bool isSynced(const displayPort_t *displayPort)
156 UNUSED(displayPort);
157 return true;
160 static void redraw(displayPort_t *displayPort)
162 drawScreen(displayPort);
165 static uint32_t txBytesFree(const displayPort_t *displayPort)
167 UNUSED(displayPort);
168 return mspSerialTxBytesFree();
171 static const displayPortVTable_t mspDisplayPortVTable = {
172 .grab = grab,
173 .release = release,
174 .clearScreen = clearScreen,
175 .drawScreen = drawScreen,
176 .screenSize = screenSize,
177 .writeSys = writeSys,
178 .writeString = writeString,
179 .writeChar = writeChar,
180 .isTransferInProgress = isTransferInProgress,
181 .heartbeat = heartbeat,
182 .redraw = redraw,
183 .isSynced = isSynced,
184 .txBytesFree = txBytesFree,
185 .layerSupported = NULL,
186 .layerSelect = NULL,
187 .layerCopy = NULL,
190 displayPort_t *displayPortMspInit(void)
192 displayInit(&mspDisplayPort, &mspDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MSP);
194 if (displayPortProfileMsp()->useDeviceBlink) {
195 mspDisplayPort.useDeviceBlink = true;
198 #ifndef USE_OSD_SD
199 if (vcdProfile()->video_system != VIDEO_SYSTEM_HD) {
200 vcdProfileMutable()->video_system = VIDEO_SYSTEM_HD;
202 #endif
203 #ifndef USE_OSD_HD
204 if (vcdProfile()->video_system == VIDEO_SYSTEM_HD) {
205 vcdProfileMutable()->video_system = VIDEO_SYSTEM_AUTO;
207 #endif
209 if (vcdProfile()->video_system == VIDEO_SYSTEM_HD) {
210 mspDisplayPort.rows = osdConfig()->canvas_rows;
211 mspDisplayPort.cols = osdConfig()->canvas_cols;
212 } else {
213 const uint8_t displayRows = (vcdProfile()->video_system == VIDEO_SYSTEM_PAL) ? VIDEO_LINES_PAL : VIDEO_LINES_NTSC;
214 mspDisplayPort.rows = displayRows + displayPortProfileMsp()->rowAdjust;
215 mspDisplayPort.cols = OSD_SD_COLS + displayPortProfileMsp()->colAdjust;
218 redraw(&mspDisplayPort);
220 return &mspDisplayPort;
223 void displayPortMspSetSerial(serialPortIdentifier_e serialPort)
225 displayPortSerial = serialPort;
228 serialPortIdentifier_e displayPortMspGetSerial(void) {
229 return displayPortSerial;
231 #endif // USE_MSP_DISPLAYPORT