Updated and Validated
[betaflight.git] / src / main / io / displayport_msp.c
blobbc6c564beb59240836935f2dc158074ca7de3b93
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 "pg/vcd.h"
45 static displayPort_t mspDisplayPort;
47 static int output(displayPort_t *displayPort, uint8_t cmd, uint8_t *buf, int len)
49 UNUSED(displayPort);
51 #ifdef USE_CLI
52 // FIXME There should be no dependency on the CLI but mspSerialPush doesn't check for cli mode, and can't because it also shouldn't have a dependency on the CLI.
53 if (cliMode) {
54 return 0;
56 #endif
57 return mspSerialPush(displayPortProfileMsp()->displayPortSerial, cmd, buf, len, MSP_DIRECTION_REPLY);
60 static int heartbeat(displayPort_t *displayPort)
62 uint8_t subcmd[] = { 0 };
64 // heartbeat is used to:
65 // a) ensure display is not released by MW OSD software
66 // b) prevent OSD Slave boards from displaying a 'disconnected' status.
67 output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
69 return 0;
72 static int grab(displayPort_t *displayPort)
74 return heartbeat(displayPort);
77 static int release(displayPort_t *displayPort)
79 uint8_t subcmd[] = { 1 };
81 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
84 static int clearScreen(displayPort_t *displayPort, displayClearOption_e options)
86 UNUSED(options);
88 uint8_t subcmd[] = { 2 };
90 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
93 static bool drawScreen(displayPort_t *displayPort)
95 uint8_t subcmd[] = { 4 };
96 output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
98 return 0;
101 static int screenSize(const displayPort_t *displayPort)
103 return displayPort->rows * displayPort->cols;
106 static int writeString(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, const char *string)
108 #define MSP_OSD_MAX_STRING_LENGTH 30 // FIXME move this
109 uint8_t buf[MSP_OSD_MAX_STRING_LENGTH + 4];
111 int len = strlen(string);
112 if (len >= MSP_OSD_MAX_STRING_LENGTH) {
113 len = MSP_OSD_MAX_STRING_LENGTH;
116 buf[0] = 3;
117 buf[1] = row;
118 buf[2] = col;
119 buf[3] = displayPortProfileMsp()->attrValues[attr] & ~DISPLAYPORT_MSP_ATTR_BLINK & DISPLAYPORT_MSP_ATTR_MASK;
121 if (attr & DISPLAYPORT_ATTR_BLINK) {
122 buf[3] |= DISPLAYPORT_MSP_ATTR_BLINK;
125 memcpy(&buf[4], string, len);
127 return output(displayPort, MSP_DISPLAYPORT, buf, len + 4);
130 static int writeChar(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, uint8_t c)
132 char buf[2];
134 buf[0] = c;
135 buf[1] = 0;
136 return writeString(displayPort, col, row, attr, buf); //!!TODO - check if there is a direct MSP command to do this
139 static bool isTransferInProgress(const displayPort_t *displayPort)
141 UNUSED(displayPort);
142 return false;
145 static bool isSynced(const displayPort_t *displayPort)
147 UNUSED(displayPort);
148 return true;
151 static void redraw(displayPort_t *displayPort)
153 const uint8_t displayRows = (vcdProfile()->video_system == VIDEO_SYSTEM_PAL) ? 16 : 13;
154 displayPort->rows = displayRows + displayPortProfileMsp()->rowAdjust;
155 displayPort->cols = 30 + displayPortProfileMsp()->colAdjust;
156 drawScreen(displayPort);
159 static uint32_t txBytesFree(const displayPort_t *displayPort)
161 UNUSED(displayPort);
162 return mspSerialTxBytesFree();
165 static const displayPortVTable_t mspDisplayPortVTable = {
166 .grab = grab,
167 .release = release,
168 .clearScreen = clearScreen,
169 .drawScreen = drawScreen,
170 .screenSize = screenSize,
171 .writeString = writeString,
172 .writeChar = writeChar,
173 .isTransferInProgress = isTransferInProgress,
174 .heartbeat = heartbeat,
175 .redraw = redraw,
176 .isSynced = isSynced,
177 .txBytesFree = txBytesFree,
178 .layerSupported = NULL,
179 .layerSelect = NULL,
180 .layerCopy = NULL,
183 displayPort_t *displayPortMspInit(void)
185 displayInit(&mspDisplayPort, &mspDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MSP);
187 if (displayPortProfileMsp()->useDeviceBlink) {
188 mspDisplayPort.useDeviceBlink = true;
191 redraw(&mspDisplayPort);
192 return &mspDisplayPort;
194 #endif // USE_MSP_DISPLAYPORT