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/>.
28 #ifdef USE_MSP_DISPLAYPORT
32 #include "common/utils.h"
34 #include "drivers/display.h"
35 #include "drivers/osd.h"
37 #include "io/displayport_msp.h"
40 #include "msp/msp_protocol.h"
41 #include "msp/msp_serial.h"
45 static displayPort_t mspDisplayPort
;
47 static int output(displayPort_t
*displayPort
, uint8_t cmd
, uint8_t *buf
, int len
)
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.
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 return output(displayPort
, MSP_DISPLAYPORT
, subcmd
, sizeof(subcmd
));
70 static int grab(displayPort_t
*displayPort
)
72 return heartbeat(displayPort
);
75 static int release(displayPort_t
*displayPort
)
77 uint8_t subcmd
[] = { 1 };
79 return output(displayPort
, MSP_DISPLAYPORT
, subcmd
, sizeof(subcmd
));
82 static int clearScreen(displayPort_t
*displayPort
)
84 uint8_t subcmd
[] = { 2 };
86 return output(displayPort
, MSP_DISPLAYPORT
, subcmd
, sizeof(subcmd
));
89 static int drawScreen(displayPort_t
*displayPort
)
91 uint8_t subcmd
[] = { 4 };
92 return output(displayPort
, MSP_DISPLAYPORT
, subcmd
, sizeof(subcmd
));
95 static int screenSize(const displayPort_t
*displayPort
)
97 return displayPort
->rows
* displayPort
->cols
;
100 static int writeString(displayPort_t
*displayPort
, uint8_t col
, uint8_t row
, uint8_t attr
, const char *string
)
102 #define MSP_OSD_MAX_STRING_LENGTH 30 // FIXME move this
103 uint8_t buf
[MSP_OSD_MAX_STRING_LENGTH
+ 4];
105 int len
= strlen(string
);
106 if (len
>= MSP_OSD_MAX_STRING_LENGTH
) {
107 len
= MSP_OSD_MAX_STRING_LENGTH
;
113 buf
[3] = displayPortProfileMsp()->attrValues
[attr
] & ~DISPLAYPORT_MSP_ATTR_BLINK
& DISPLAYPORT_MSP_ATTR_MASK
;
115 if (attr
& DISPLAYPORT_ATTR_BLINK
) {
116 buf
[3] |= DISPLAYPORT_MSP_ATTR_BLINK
;
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 attr
, uint8_t c
)
130 return writeString(displayPort
, col
, row
, attr
, buf
); //!!TODO - check if there is a direct MSP command to do this
133 static bool isTransferInProgress(const displayPort_t
*displayPort
)
139 static bool isSynced(const displayPort_t
*displayPort
)
145 static void redraw(displayPort_t
*displayPort
)
147 const uint8_t displayRows
= (vcdProfile()->video_system
== VIDEO_SYSTEM_PAL
) ? 16 : 13;
148 displayPort
->rows
= displayRows
+ displayPortProfileMsp()->rowAdjust
;
149 displayPort
->cols
= 30 + displayPortProfileMsp()->colAdjust
;
150 drawScreen(displayPort
);
153 static uint32_t txBytesFree(const displayPort_t
*displayPort
)
156 return mspSerialTxBytesFree();
159 static const displayPortVTable_t mspDisplayPortVTable
= {
162 .clearScreen
= clearScreen
,
163 .drawScreen
= drawScreen
,
164 .screenSize
= screenSize
,
165 .writeString
= writeString
,
166 .writeChar
= writeChar
,
167 .isTransferInProgress
= isTransferInProgress
,
168 .heartbeat
= heartbeat
,
170 .isSynced
= isSynced
,
171 .txBytesFree
= txBytesFree
,
172 .layerSupported
= NULL
,
177 displayPort_t
*displayPortMspInit(void)
179 displayInit(&mspDisplayPort
, &mspDisplayPortVTable
, DISPLAYPORT_DEVICE_TYPE_MSP
);
181 if (displayPortProfileMsp()->useDeviceBlink
) {
182 mspDisplayPort
.useDeviceBlink
= true;
185 redraw(&mspDisplayPort
);
186 return &mspDisplayPort
;
188 #endif // USE_MSP_DISPLAYPORT