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 #include "common/utils.h"
30 #include "drivers/display.h"
31 #include "drivers/max7456.h"
32 #include "drivers/osd.h"
34 #include "config/config.h"
36 #include "fc/runtime_config.h"
38 #include "io/displayport_max7456.h"
42 #include "pg/displayport_profiles.h"
43 #include "pg/max7456.h"
46 static displayPort_t max7456DisplayPort
;
47 static vcdProfile_t
const *max7456VcdProfile
;
49 static int grab(displayPort_t
*displayPort
)
56 static int release(displayPort_t
*displayPort
)
63 static int clearScreen(displayPort_t
*displayPort
, displayClearOption_e options
)
68 max7456Invert(displayPortProfileMax7456()->invert
);
69 max7456Brightness(displayPortProfileMax7456()->blackBrightness
, displayPortProfileMax7456()->whiteBrightness
);
76 // Return true if screen still being transferred
77 static bool drawScreen(displayPort_t
*displayPort
)
80 return max7456DrawScreen();
83 static int screenSize(const displayPort_t
*displayPort
)
89 static int writeString(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, const char *text
)
94 max7456Write(x
, y
, text
);
99 static int writeChar(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, uint8_t c
)
104 max7456WriteChar(x
, y
, c
);
109 static bool isTransferInProgress(const displayPort_t
*displayPort
)
112 return max7456DmaInProgress();
115 static bool isSynced(const displayPort_t
*displayPort
)
118 return max7456BuffersSynced();
121 static void redraw(displayPort_t
*displayPort
)
125 if (!ARMING_FLAG(ARMED
)) {
130 static int heartbeat(displayPort_t
*displayPort
)
134 // (Re)Initialize MAX7456 at startup or stall is detected.
135 return max7456ReInitIfRequired(false);
138 static uint32_t txBytesFree(const displayPort_t
*displayPort
)
144 static bool layerSupported(displayPort_t
*displayPort
, displayPortLayer_e layer
)
147 return max7456LayerSupported(layer
);
150 static bool layerSelect(displayPort_t
*displayPort
, displayPortLayer_e layer
)
153 return max7456LayerSelect(layer
);
156 static bool layerCopy(displayPort_t
*displayPort
, displayPortLayer_e destLayer
, displayPortLayer_e sourceLayer
)
159 return max7456LayerCopy(destLayer
, sourceLayer
);
162 static bool writeFontCharacter(displayPort_t
*displayPort
, uint16_t addr
, const osdCharacter_t
*chr
)
166 return max7456WriteNvm(addr
, (const uint8_t *)chr
);
169 static bool checkReady(displayPort_t
*displayPort
, bool rescan
)
172 if (!max7456IsDeviceDetected()) {
176 // Try to initialize the device
177 if (max7456Init(max7456Config(), max7456VcdProfile
, systemConfig()->cpu_overclock
) != MAX7456_INIT_OK
) {
180 // At this point the device has been initialized and detected
181 redraw(&max7456DisplayPort
);
188 void setBackgroundType(displayPort_t
*displayPort
, displayPortBackground_e backgroundType
)
191 max7456SetBackgroundType(backgroundType
);
194 static const displayPortVTable_t max7456VTable
= {
197 .clearScreen
= clearScreen
,
198 .drawScreen
= drawScreen
,
199 .screenSize
= screenSize
,
200 .writeString
= writeString
,
201 .writeChar
= writeChar
,
202 .isTransferInProgress
= isTransferInProgress
,
203 .heartbeat
= heartbeat
,
205 .isSynced
= isSynced
,
206 .txBytesFree
= txBytesFree
,
207 .layerSupported
= layerSupported
,
208 .layerSelect
= layerSelect
,
209 .layerCopy
= layerCopy
,
210 .writeFontCharacter
= writeFontCharacter
,
211 .checkReady
= checkReady
,
212 .setBackgroundType
= setBackgroundType
,
215 bool max7456DisplayPortInit(const vcdProfile_t
*vcdProfile
, displayPort_t
**displayPort
)
217 max7456VcdProfile
= vcdProfile
;
219 switch (max7456Init(max7456Config(), max7456VcdProfile
, systemConfig()->cpu_overclock
)) {
220 case MAX7456_INIT_NOT_CONFIGURED
:
221 // MAX7456 IO pins are not defined. We either don't have
222 // it on board or either the configuration for it has
229 case MAX7456_INIT_NOT_FOUND
:
230 // MAX7456 IO pins are defined, but we could not get a reply
231 // from it at this time. Delay full initialization to
232 // checkReady() with 'rescan' enabled
233 displayInit(&max7456DisplayPort
, &max7456VTable
, DISPLAYPORT_DEVICE_TYPE_MAX7456
);
234 *displayPort
= &max7456DisplayPort
;
239 case MAX7456_INIT_OK
:
240 // MAX7456 configured and detected
241 displayInit(&max7456DisplayPort
, &max7456VTable
, DISPLAYPORT_DEVICE_TYPE_MAX7456
);
242 *displayPort
= &max7456DisplayPort
;
249 switch(vcdProfile
->video_system
) {
251 case VIDEO_SYSTEM_PAL
:
252 displayRows
= VIDEO_LINES_PAL
;
255 case VIDEO_SYSTEM_NTSC
:
256 displayRows
= VIDEO_LINES_NTSC
;
259 case VIDEO_SYSTEM_AUTO
:
260 displayRows
= max7456GetRowsCount();
264 max7456DisplayPort
.rows
= displayRows
+ displayPortProfileMax7456()->rowAdjust
;
265 max7456DisplayPort
.cols
= 30 + displayPortProfileMax7456()->colAdjust
;
269 #endif // USE_MAX7456