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
)
67 max7456Invert(displayPortProfileMax7456()->invert
);
68 max7456Brightness(displayPortProfileMax7456()->blackBrightness
, displayPortProfileMax7456()->whiteBrightness
);
75 // Return true if screen still being transferred
76 static bool drawScreen(displayPort_t
*displayPort
)
79 return max7456DrawScreen();
82 static int screenSize(const displayPort_t
*displayPort
)
88 static int writeString(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, const char *s
)
93 max7456Write(x
, y
, s
);
98 static int writeChar(displayPort_t
*displayPort
, uint8_t x
, uint8_t y
, uint8_t attr
, uint8_t c
)
103 max7456WriteChar(x
, y
, c
);
108 static bool isTransferInProgress(const displayPort_t
*displayPort
)
111 return max7456DmaInProgress();
114 static bool isSynced(const displayPort_t
*displayPort
)
117 return max7456BuffersSynced();
120 static void redraw(displayPort_t
*displayPort
)
124 if (!ARMING_FLAG(ARMED
)) {
129 static int heartbeat(displayPort_t
*displayPort
)
133 // (Re)Initialize MAX7456 at startup or stall is detected.
134 return max7456ReInitIfRequired(false);
137 static uint32_t txBytesFree(const displayPort_t
*displayPort
)
143 static bool layerSupported(displayPort_t
*displayPort
, displayPortLayer_e layer
)
146 return max7456LayerSupported(layer
);
149 static bool layerSelect(displayPort_t
*displayPort
, displayPortLayer_e layer
)
152 return max7456LayerSelect(layer
);
155 static bool layerCopy(displayPort_t
*displayPort
, displayPortLayer_e destLayer
, displayPortLayer_e sourceLayer
)
158 return max7456LayerCopy(destLayer
, sourceLayer
);
161 static bool writeFontCharacter(displayPort_t
*displayPort
, uint16_t addr
, const osdCharacter_t
*chr
)
165 return max7456WriteNvm(addr
, (const uint8_t *)chr
);
168 static bool checkReady(displayPort_t
*displayPort
, bool rescan
)
171 if (!max7456IsDeviceDetected()) {
175 // Try to initialize the device
176 if (max7456Init(max7456Config(), max7456VcdProfile
, systemConfig()->cpu_overclock
) != MAX7456_INIT_OK
) {
179 // At this point the device has been initialized and detected
180 redraw(&max7456DisplayPort
);
184 displayPort
->rows
= max7456GetRowsCount() + displayPortProfileMax7456()->rowAdjust
;
185 displayPort
->cols
= 30 + displayPortProfileMax7456()->colAdjust
;
190 void setBackgroundType(displayPort_t
*displayPort
, displayPortBackground_e backgroundType
)
193 max7456SetBackgroundType(backgroundType
);
196 static const displayPortVTable_t max7456VTable
= {
199 .clearScreen
= clearScreen
,
200 .drawScreen
= drawScreen
,
201 .screenSize
= screenSize
,
202 .writeString
= writeString
,
203 .writeChar
= writeChar
,
204 .isTransferInProgress
= isTransferInProgress
,
205 .heartbeat
= heartbeat
,
207 .isSynced
= isSynced
,
208 .txBytesFree
= txBytesFree
,
209 .layerSupported
= layerSupported
,
210 .layerSelect
= layerSelect
,
211 .layerCopy
= layerCopy
,
212 .writeFontCharacter
= writeFontCharacter
,
213 .checkReady
= checkReady
,
214 .setBackgroundType
= setBackgroundType
,
217 bool max7456DisplayPortInit(const vcdProfile_t
*vcdProfile
, displayPort_t
**displayPort
)
219 max7456VcdProfile
= vcdProfile
;
221 switch (max7456Init(max7456Config(), max7456VcdProfile
, systemConfig()->cpu_overclock
)) {
222 case MAX7456_INIT_NOT_CONFIGURED
:
223 // MAX7456 IO pins are not defined. We either don't have
224 // it on board or either the configuration for it has
231 case MAX7456_INIT_NOT_FOUND
:
232 // MAX7456 IO pins are defined, but we could not get a reply
233 // from it at this time. Delay full initialization to
234 // checkReady() with 'rescan' enabled
235 displayInit(&max7456DisplayPort
, &max7456VTable
, DISPLAYPORT_DEVICE_TYPE_MAX7456
);
236 *displayPort
= &max7456DisplayPort
;
241 case MAX7456_INIT_OK
:
242 // MAX7456 configured and detected
243 displayInit(&max7456DisplayPort
, &max7456VTable
, DISPLAYPORT_DEVICE_TYPE_MAX7456
);
244 *displayPort
= &max7456DisplayPort
;
251 #endif // USE_MAX7456