Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / src / main / drivers / display.c
blob1c8abf2da6e6d8a929e3474cf714671b1274b230
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>
23 #include <string.h>
25 #include "platform.h"
27 #include "common/utils.h"
29 #include "drivers/display_canvas.h"
30 #include "drivers/osd.h"
32 #include "display.h"
34 void displayClearScreen(displayPort_t *instance, displayClearOption_e options)
36 instance->vTable->clearScreen(instance, options);
37 instance->cleared = true;
38 instance->cursorRow = -1;
41 // Return true if screen still being transferred
42 bool displayDrawScreen(displayPort_t *instance)
44 return instance->vTable->drawScreen(instance);
47 int displayScreenSize(const displayPort_t *instance)
49 return instance->vTable->screenSize(instance);
52 void displayGrab(displayPort_t *instance)
54 instance->vTable->grab(instance);
55 instance->vTable->clearScreen(instance, DISPLAY_CLEAR_WAIT);
56 ++instance->grabCount;
59 void displayRelease(displayPort_t *instance)
61 instance->vTable->release(instance);
62 --instance->grabCount;
65 void displayReleaseAll(displayPort_t *instance)
67 instance->vTable->release(instance);
68 instance->grabCount = 0;
71 bool displayIsGrabbed(const displayPort_t *instance)
73 // can be called before initialised
74 return (instance && instance->grabCount > 0);
77 void displaySetXY(displayPort_t *instance, uint8_t x, uint8_t y)
79 instance->posX = x;
80 instance->posY = y;
83 int displayWrite(displayPort_t *instance, uint8_t x, uint8_t y, uint8_t attr, const char *s)
85 instance->posX = x + strlen(s);
86 instance->posY = y;
87 return instance->vTable->writeString(instance, x, y, attr, s);
90 int displayWriteChar(displayPort_t *instance, uint8_t x, uint8_t y, uint8_t attr, uint8_t c)
92 instance->posX = x + 1;
93 instance->posY = y;
94 return instance->vTable->writeChar(instance, x, y, attr, c);
97 bool displayIsTransferInProgress(const displayPort_t *instance)
99 return instance->vTable->isTransferInProgress(instance);
102 bool displayIsSynced(const displayPort_t *instance)
104 return instance->vTable->isSynced(instance);
107 bool displayHeartbeat(displayPort_t *instance)
109 return instance->vTable->heartbeat(instance);
112 void displayRedraw(displayPort_t *instance)
114 instance->vTable->redraw(instance);
117 uint16_t displayTxBytesFree(const displayPort_t *instance)
119 return instance->vTable->txBytesFree(instance);
122 bool displayLayerSupported(displayPort_t *instance, displayPortLayer_e layer)
124 if (layer == DISPLAYPORT_LAYER_FOREGROUND) {
125 // Every device must support the foreground (default) layer
126 return true;
127 } else if (layer < DISPLAYPORT_LAYER_COUNT && instance->vTable->layerSupported) {
128 return instance->vTable->layerSupported(instance, layer);
130 return false;
133 bool displayLayerSelect(displayPort_t *instance, displayPortLayer_e layer)
135 if (instance->vTable->layerSelect) {
136 return instance->vTable->layerSelect(instance, layer);
138 return false;
141 bool displayLayerCopy(displayPort_t *instance, displayPortLayer_e destLayer, displayPortLayer_e sourceLayer)
143 if (instance->vTable->layerCopy && sourceLayer != destLayer) {
144 return instance->vTable->layerCopy(instance, destLayer, sourceLayer);
146 return false;
149 bool displayWriteFontCharacter(displayPort_t *instance, uint16_t addr, const osdCharacter_t *chr)
151 if (instance->vTable->writeFontCharacter) {
152 return instance->vTable->writeFontCharacter(instance, addr, chr);
154 return false;
157 void displaySetBackgroundType(displayPort_t *instance, displayPortBackground_e backgroundType)
159 if (instance->vTable->setBackgroundType) {
160 instance->vTable->setBackgroundType(instance, backgroundType);
164 bool displayCheckReady(displayPort_t *instance, bool rescan)
166 if (instance->vTable->checkReady) {
167 return instance->vTable->checkReady(instance, rescan);
169 // Drivers that don't provide a checkReady method are
170 // assumed to be immediately ready (either by actually
171 // begin ready very quickly or by blocking)
172 return true;
175 void displayBeginTransaction(displayPort_t *instance, displayTransactionOption_e opts)
177 if (instance->vTable->beginTransaction) {
178 instance->vTable->beginTransaction(instance, opts);
182 void displayCommitTransaction(displayPort_t *instance)
184 if (instance->vTable->commitTransaction) {
185 instance->vTable->commitTransaction(instance);
189 bool displayGetCanvas(displayCanvas_t *canvas, const displayPort_t *instance)
191 #if defined(USE_CANVAS)
192 if (canvas && instance->vTable->getCanvas && instance->vTable->getCanvas(canvas, instance)) {
193 canvas->gridElementWidth = canvas->width / instance->cols;
194 canvas->gridElementHeight = canvas->height / instance->rows;
195 return true;
197 #else
198 UNUSED(canvas);
199 UNUSED(instance);
200 #endif
201 return false;
204 bool displaySupportsOsdSymbols(displayPort_t *instance)
206 // Assume device types that support OSD display will support the OSD symbols (since the OSD logic will use them)
207 if ((instance->deviceType == DISPLAYPORT_DEVICE_TYPE_MAX7456)
208 || (instance->deviceType == DISPLAYPORT_DEVICE_TYPE_MSP)
209 || (instance->deviceType == DISPLAYPORT_DEVICE_TYPE_FRSKYOSD)) {
210 return true;
211 } else {
212 return false;
216 void displayInit(displayPort_t *instance, const displayPortVTable_t *vTable, displayPortDeviceType_e deviceType)
218 instance->vTable = vTable;
219 instance->useFullscreen = false;
220 instance->grabCount = 0;
221 instance->deviceType = deviceType;
223 displayBeginTransaction(instance, DISPLAY_TRANSACTION_OPT_NONE);
224 displayClearScreen(instance, DISPLAY_CLEAR_WAIT);
225 displayCommitTransaction(instance);