Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / io / displayport_max7456.c
blob52c7659c6996145cb242e9e7fd57343d5d47ea23
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)
65 UNUSED(displayPort);
67 max7456Invert(displayPortProfileMax7456()->invert);
68 max7456Brightness(displayPortProfileMax7456()->blackBrightness, displayPortProfileMax7456()->whiteBrightness);
70 max7456ClearScreen();
72 return 0;
75 // Return true if screen still being transferred
76 static bool drawScreen(displayPort_t *displayPort)
78 UNUSED(displayPort);
79 return max7456DrawScreen();
82 static int screenSize(const displayPort_t *displayPort)
84 UNUSED(displayPort);
85 return maxScreenSize;
88 static int writeString(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, const char *s)
90 UNUSED(displayPort);
91 UNUSED(attr);
93 max7456Write(x, y, s);
95 return 0;
98 static int writeChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
100 UNUSED(displayPort);
101 UNUSED(attr);
103 max7456WriteChar(x, y, c);
105 return 0;
108 static bool isTransferInProgress(const displayPort_t *displayPort)
110 UNUSED(displayPort);
111 return max7456DmaInProgress();
114 static bool isSynced(const displayPort_t *displayPort)
116 UNUSED(displayPort);
117 return max7456BuffersSynced();
120 static void redraw(displayPort_t *displayPort)
122 UNUSED(displayPort);
124 if (!ARMING_FLAG(ARMED)) {
125 max7456RefreshAll();
129 static int heartbeat(displayPort_t *displayPort)
131 UNUSED(displayPort);
133 // (Re)Initialize MAX7456 at startup or stall is detected.
134 return max7456ReInitIfRequired(false);
137 static uint32_t txBytesFree(const displayPort_t *displayPort)
139 UNUSED(displayPort);
140 return UINT32_MAX;
143 static bool layerSupported(displayPort_t *displayPort, displayPortLayer_e layer)
145 UNUSED(displayPort);
146 return max7456LayerSupported(layer);
149 static bool layerSelect(displayPort_t *displayPort, displayPortLayer_e layer)
151 UNUSED(displayPort);
152 return max7456LayerSelect(layer);
155 static bool layerCopy(displayPort_t *displayPort, displayPortLayer_e destLayer, displayPortLayer_e sourceLayer)
157 UNUSED(displayPort);
158 return max7456LayerCopy(destLayer, sourceLayer);
161 static bool writeFontCharacter(displayPort_t *displayPort, uint16_t addr, const osdCharacter_t *chr)
163 UNUSED(displayPort);
165 return max7456WriteNvm(addr, (const uint8_t *)chr);
168 static bool checkReady(displayPort_t *displayPort, bool rescan)
170 UNUSED(displayPort);
171 if (!max7456IsDeviceDetected()) {
172 if (!rescan) {
173 return false;
174 } else {
175 // Try to initialize the device
176 if (max7456Init(max7456Config(), max7456VcdProfile, systemConfig()->cpu_overclock) != MAX7456_INIT_OK) {
177 return false;
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;
187 return true;
190 void setBackgroundType(displayPort_t *displayPort, displayPortBackground_e backgroundType)
192 UNUSED(displayPort);
193 max7456SetBackgroundType(backgroundType);
196 static const displayPortVTable_t max7456VTable = {
197 .grab = grab,
198 .release = release,
199 .clearScreen = clearScreen,
200 .drawScreen = drawScreen,
201 .screenSize = screenSize,
202 .writeString = writeString,
203 .writeChar = writeChar,
204 .isTransferInProgress = isTransferInProgress,
205 .heartbeat = heartbeat,
206 .redraw = redraw,
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
225 // not been set.
226 *displayPort = NULL;
228 return false;
230 break;
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;
238 return false;
240 break;
241 case MAX7456_INIT_OK:
242 // MAX7456 configured and detected
243 displayInit(&max7456DisplayPort, &max7456VTable, DISPLAYPORT_DEVICE_TYPE_MAX7456);
244 *displayPort = &max7456DisplayPort;
246 break;
249 return true;
251 #endif // USE_MAX7456