Update FrSky SPI RX.md
[betaflight.git] / src / main / io / displayport_msp.c
blob003a30304e1c4bf61c87a93f9092de97ce39f2f8
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 "common/utils.h"
32 #include "pg/pg.h"
33 #include "pg/pg_ids.h"
35 #include "drivers/display.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 // no template required since defaults are zero
44 PG_REGISTER(displayPortProfile_t, displayPortProfileMsp, PG_DISPLAY_PORT_MSP_CONFIG, 0);
46 static displayPort_t mspDisplayPort;
48 #ifdef USE_CLI
49 extern uint8_t cliMode;
50 #endif
52 static int output(displayPort_t *displayPort, uint8_t cmd, uint8_t *buf, int len)
54 UNUSED(displayPort);
56 #ifdef USE_CLI
57 // 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.
58 if (cliMode) {
59 return 0;
61 #endif
62 return mspSerialPush(cmd, buf, len, MSP_DIRECTION_REPLY);
65 static int heartbeat(displayPort_t *displayPort)
67 uint8_t subcmd[] = { 0 };
69 // heartbeat is used to:
70 // a) ensure display is not released by MW OSD software
71 // b) prevent OSD Slave boards from displaying a 'disconnected' status.
72 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
75 static int grab(displayPort_t *displayPort)
77 return heartbeat(displayPort);
80 static int release(displayPort_t *displayPort)
82 uint8_t subcmd[] = { 1 };
84 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
87 static int clearScreen(displayPort_t *displayPort)
89 uint8_t subcmd[] = { 2 };
91 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
94 static int drawScreen(displayPort_t *displayPort)
96 uint8_t subcmd[] = { 4 };
97 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
100 static int screenSize(const displayPort_t *displayPort)
102 return displayPort->rows * displayPort->cols;
105 static int writeString(displayPort_t *displayPort, uint8_t col, uint8_t row, const char *string)
107 #define MSP_OSD_MAX_STRING_LENGTH 30 // FIXME move this
108 uint8_t buf[MSP_OSD_MAX_STRING_LENGTH + 4];
110 int len = strlen(string);
111 if (len >= MSP_OSD_MAX_STRING_LENGTH) {
112 len = MSP_OSD_MAX_STRING_LENGTH;
115 buf[0] = 3;
116 buf[1] = row;
117 buf[2] = col;
118 buf[3] = 0;
119 memcpy(&buf[4], string, len);
121 return output(displayPort, MSP_DISPLAYPORT, buf, len + 4);
124 static int writeChar(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t c)
126 char buf[2];
128 buf[0] = c;
129 buf[1] = 0;
130 return writeString(displayPort, col, row, buf); //!!TODO - check if there is a direct MSP command to do this
133 static bool isTransferInProgress(const displayPort_t *displayPort)
135 UNUSED(displayPort);
136 return false;
139 static bool isSynced(const displayPort_t *displayPort)
141 UNUSED(displayPort);
142 return true;
145 static void resync(displayPort_t *displayPort)
147 displayPort->rows = 13 + displayPortProfileMsp()->rowAdjust; // XXX Will reflect NTSC/PAL in the future
148 displayPort->cols = 30 + displayPortProfileMsp()->colAdjust;
149 drawScreen(displayPort);
152 static uint32_t txBytesFree(const displayPort_t *displayPort)
154 UNUSED(displayPort);
155 return mspSerialTxBytesFree();
158 static const displayPortVTable_t mspDisplayPortVTable = {
159 .grab = grab,
160 .release = release,
161 .clearScreen = clearScreen,
162 .drawScreen = drawScreen,
163 .screenSize = screenSize,
164 .writeString = writeString,
165 .writeChar = writeChar,
166 .isTransferInProgress = isTransferInProgress,
167 .heartbeat = heartbeat,
168 .resync = resync,
169 .isSynced = isSynced,
170 .txBytesFree = txBytesFree
173 displayPort_t *displayPortMspInit(void)
175 displayInit(&mspDisplayPort, &mspDisplayPortVTable);
176 resync(&mspDisplayPort);
177 return &mspDisplayPort;
179 #endif // USE_MSP_DISPLAYPORT