Updated and Validated
[betaflight.git] / src / main / io / displayport_max7456.c
blobe2026ff58d74256ebb937f681c3de4034da67797
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>
24 #include "platform.h"
26 #ifdef USE_MAX7456
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"
40 #include "osd/osd.h"
42 #include "pg/displayport_profiles.h"
43 #include "pg/max7456.h"
44 #include "pg/vcd.h"
46 static displayPort_t max7456DisplayPort;
47 static vcdProfile_t const *max7456VcdProfile;
49 static int grab(displayPort_t *displayPort)
51 UNUSED(displayPort);
53 return 0;
56 static int release(displayPort_t *displayPort)
58 UNUSED(displayPort);
60 return 0;
63 static int clearScreen(displayPort_t *displayPort, displayClearOption_e options)
65 UNUSED(displayPort);
66 UNUSED(options);
68 max7456Invert(displayPortProfileMax7456()->invert);
69 max7456Brightness(displayPortProfileMax7456()->blackBrightness, displayPortProfileMax7456()->whiteBrightness);
71 max7456ClearScreen();
73 return 0;
76 // Return true if screen still being transferred
77 static bool drawScreen(displayPort_t *displayPort)
79 UNUSED(displayPort);
80 return max7456DrawScreen();
83 static int screenSize(const displayPort_t *displayPort)
85 UNUSED(displayPort);
86 return maxScreenSize;
89 static int writeString(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, const char *s)
91 UNUSED(displayPort);
92 UNUSED(attr);
94 max7456Write(x, y, s);
96 return 0;
99 static int writeChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
101 UNUSED(displayPort);
102 UNUSED(attr);
104 max7456WriteChar(x, y, c);
106 return 0;
109 static bool isTransferInProgress(const displayPort_t *displayPort)
111 UNUSED(displayPort);
112 return max7456DmaInProgress();
115 static bool isSynced(const displayPort_t *displayPort)
117 UNUSED(displayPort);
118 return max7456BuffersSynced();
121 static void redraw(displayPort_t *displayPort)
123 UNUSED(displayPort);
125 if (!ARMING_FLAG(ARMED)) {
126 max7456RefreshAll();
130 static int heartbeat(displayPort_t *displayPort)
132 UNUSED(displayPort);
134 // (Re)Initialize MAX7456 at startup or stall is detected.
135 return max7456ReInitIfRequired(false);
138 static uint32_t txBytesFree(const displayPort_t *displayPort)
140 UNUSED(displayPort);
141 return UINT32_MAX;
144 static bool layerSupported(displayPort_t *displayPort, displayPortLayer_e layer)
146 UNUSED(displayPort);
147 return max7456LayerSupported(layer);
150 static bool layerSelect(displayPort_t *displayPort, displayPortLayer_e layer)
152 UNUSED(displayPort);
153 return max7456LayerSelect(layer);
156 static bool layerCopy(displayPort_t *displayPort, displayPortLayer_e destLayer, displayPortLayer_e sourceLayer)
158 UNUSED(displayPort);
159 return max7456LayerCopy(destLayer, sourceLayer);
162 static bool writeFontCharacter(displayPort_t *displayPort, uint16_t addr, const osdCharacter_t *chr)
164 UNUSED(displayPort);
166 return max7456WriteNvm(addr, (const uint8_t *)chr);
169 static bool checkReady(displayPort_t *displayPort, bool rescan)
171 UNUSED(displayPort);
172 if (!max7456IsDeviceDetected()) {
173 if (!rescan) {
174 return false;
175 } else {
176 // Try to initialize the device
177 if (max7456Init(max7456Config(), max7456VcdProfile, systemConfig()->cpu_overclock) != MAX7456_INIT_OK) {
178 return false;
180 // At this point the device has been initialized and detected
181 redraw(&max7456DisplayPort);
185 displayPort->rows = max7456GetRowsCount() + displayPortProfileMax7456()->rowAdjust;
186 displayPort->cols = 30 + displayPortProfileMax7456()->colAdjust;
188 return true;
191 void setBackgroundType(displayPort_t *displayPort, displayPortBackground_e backgroundType)
193 UNUSED(displayPort);
194 max7456SetBackgroundType(backgroundType);
197 static const displayPortVTable_t max7456VTable = {
198 .grab = grab,
199 .release = release,
200 .clearScreen = clearScreen,
201 .drawScreen = drawScreen,
202 .screenSize = screenSize,
203 .writeString = writeString,
204 .writeChar = writeChar,
205 .isTransferInProgress = isTransferInProgress,
206 .heartbeat = heartbeat,
207 .redraw = redraw,
208 .isSynced = isSynced,
209 .txBytesFree = txBytesFree,
210 .layerSupported = layerSupported,
211 .layerSelect = layerSelect,
212 .layerCopy = layerCopy,
213 .writeFontCharacter = writeFontCharacter,
214 .checkReady = checkReady,
215 .setBackgroundType = setBackgroundType,
218 bool max7456DisplayPortInit(const vcdProfile_t *vcdProfile, displayPort_t **displayPort)
220 max7456VcdProfile = vcdProfile;
222 switch (max7456Init(max7456Config(), max7456VcdProfile, systemConfig()->cpu_overclock)) {
223 case MAX7456_INIT_NOT_CONFIGURED:
224 // MAX7456 IO pins are not defined. We either don't have
225 // it on board or either the configuration for it has
226 // not been set.
227 *displayPort = NULL;
229 return false;
231 break;
232 case MAX7456_INIT_NOT_FOUND:
233 // MAX7456 IO pins are defined, but we could not get a reply
234 // from it at this time. Delay full initialization to
235 // checkReady() with 'rescan' enabled
236 displayInit(&max7456DisplayPort, &max7456VTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
237 *displayPort = &max7456DisplayPort;
239 return false;
241 break;
242 case MAX7456_INIT_OK:
243 // MAX7456 configured and detected
244 displayInit(&max7456DisplayPort, &max7456VTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
245 *displayPort = &max7456DisplayPort;
247 break;
250 return true;
252 #endif // USE_MAX7456