FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
ModelineStudio

Interactive tool to find and tune VGA modelines for a specific monitor. The screen (2 colors) shows the current resolution and a frame around the visible area; every change is applied immediately and the resulting modeline is printed on the serial console, ready to be copied into your code (DisplayController.setResolution("...")).

What it demonstrates

  • The predefined resolutions (VGA_640x480_60Hz, SVGA_800x600_56Hz, ... up to 1280x768)
  • VGAController::setResolution() with a modeline string or a VGATimings structure
  • moveScreen() and shrinkScreen() to adjust position and size of the picture
  • fabgl::VGA2Controller: the 2-color controller, which allows the highest resolutions with little memory

Hardware

  • VGA output (default pins)
  • USB serial connection (115200 baud): all the commands are typed in the serial monitor (for example idf.py -p PORT monitor)

Commands

Single characters typed in the serial monitor:

Key Command
h Print the help
+ / - Next / previous predefined resolution (43 presets, starts at 640x480)
x Restore the current preset (cancels moves, shrinks and timing changes)
w / z / a / s Move the picture up / down / left / right
n / N Shrink / enlarge horizontally
m / M Shrink / enlarge vertically
e / E Decrease / increase the horizontal front porch
r / R Decrease / increase the horizontal sync pulse
t / T Decrease / increase the horizontal back porch
y / Y Decrease / increase the vertical front porch
u / U Decrease / increase the vertical sync pulse
i / I Decrease / increase the vertical back porch
o / O Decrease / increase the pixel clock by 5 kHz
. Change the order of the horizontal signals (front porch / sync / back porch / visible area first)

Porch and sync changes keep the total line (or frame) length constant, so the refresh rate does not change.

To try a custom modeline, paste it in the serial monitor (it must start with " and end with Enter), for example:

"640x350@70Hz" 25.175 640 656 752 800 350 366 462 510 -HSync -VSync

After each command the console shows the modeline, the moveScreen()/shrinkScreen() calls to reproduce the adjustments, screen and viewport size and free memory.

Build

See Building the examples.

Source code

Main file of the example, main/ModelineStudio.cpp. The complete project is in the examples/VGA/ModelineStudio folder.

1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2026 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#include "freertos/FreeRTOS.h"
28#include "freertos/task.h"
29#include "fabgl.h"
30
31
32// UART0 (console) stream
33fabgl::UARTStream Console;
34
35#include "esp_chip_info.h"
36
37
38static int chipRevision()
39{
40 esp_chip_info_t info;
41 esp_chip_info(&info);
42 return info.revision;
43}
44
45
46const char * PresetResolutions[] = {
48 VGA_320x200_60HzD,
90};
91
92
93
94int currentResolution = 24; // VESA_640x480_75Hz
95int moveX = 0;
96int moveY = 0;
97int shrinkX = 0;
98int shrinkY = 0;
99
100
101fabgl::VGA2Controller VGAController;
102
103
104
105void printHelp()
106{
107 printf("Modeline studio\n");
108 printf("Chip Revision: %d Chip Frequency: %d MHz\n\n", chipRevision(), (int) fabgl::getCPUFrequencyMHz());
109 printf("%s\n", VGAController.getResolutionTimings()->label);
110 printf("\nScreen move:\n");
111 printf(" w = Move Up z = Move Down a = Move Left s = Move Right\n");
112 printf("Screen shrink:\n");
113 printf(" n = Dec Horiz N = Inc Horiz m = Dec Vert M = Inc Vert\n");
114 printf("Resolutions:\n");
115 printf(" + = Next resolution - = Prev resolution x = Restore modeline\n");
116 printf("Modeline change:\n");
117 printf(" Just insert a modline. Example: \"640x350@70Hz\" 25.175 640 656 752 800 350 366 462 510 -HSync -VSync\n");
119 printf(" e = Decrease horiz. Front Porch E = Increase horiz. Front Porch\n");
120 printf(" r = Decrease horiz. Sync Pulse R = Increase horiz. Sync Pulse\n");
121 printf(" t = Decrease horiz. Bach Porch T = Increase horiz. Back Porch\n");
122 printf(" y = Decrease vert. Front Porch Y = Increase vert. Front Porch\n");
123 printf(" u = Decrease vert. Sync Pulse U = Increase vert. Sync Pulse\n");
124 printf(" i = Decrease vert. Bach Porch I = Increase vert. Back Porch\n");
125 //*/
126 printf(" o = Decrease frequency by 5KHz O = Increase frequency by 5KHz\n");
127 printf(" . = Change horiz. signals order\n");
128 printf("Various:\n");
129 printf(" h = Print This help\n\n");
130}
131
132
133void printInfo()
134{
135 auto t = VGAController.getResolutionTimings();
136 printf("Modeline \"%s\" %2.8g %d %d %d %d %d %d %d %d %cHSync %cVSync %s %s\n",
137 t->label,
138 t->frequency / 1000000.0,
139 t->HVisibleArea,
140 t->HVisibleArea + t->HFrontPorch,
141 t->HVisibleArea + t->HFrontPorch + t->HSyncPulse,
142 t->HVisibleArea + t->HFrontPorch + t->HSyncPulse + t->HBackPorch,
143 t->VVisibleArea,
144 t->VVisibleArea + t->VFrontPorch,
145 t->VVisibleArea + t->VFrontPorch + t->VSyncPulse,
146 t->VVisibleArea + t->VFrontPorch + t->VSyncPulse + t->VBackPorch,
147 t->HSyncLogic, t->VSyncLogic,
148 t->scanCount == 2 ? "DoubleScan" : "",
149 t->HStartingBlock == VGAScanStart::FrontPorch ? "FrontPorchBegins" :
150 (t->HStartingBlock == VGAScanStart::Sync ? "SyncBegins" :
151 (t->HStartingBlock == VGAScanStart::BackPorch ? "BackPorchBegins" : "VisibleBegins")));
152
153 //printf("VFront = %d, VSync = %d, VBack = %d\n", t->VFrontPorch, t->VSyncPulse, t->VBackPorch);
154
155 if (moveX || moveY)
156 printf("moveScreen(%d, %d)\n", moveX, moveY);
157 if (shrinkX || shrinkY)
158 printf("shrinkScreen(%d, %d)\n", shrinkX, shrinkY);
159 printf("Screen size : %d x %d\n", VGAController.getScreenWidth(), VGAController.getScreenHeight());
160 printf("Viewport size : %d x %d\n", VGAController.getViewPortWidth(), VGAController.getViewPortHeight());
161 printf("Free memory (total, min, largest): %d, %d, %d\n\n", heap_caps_get_free_size(MALLOC_CAP_32BIT),
162 heap_caps_get_minimum_free_size(MALLOC_CAP_32BIT),
163 heap_caps_get_largest_free_block(MALLOC_CAP_32BIT));
164}
165
166
167void updateScreen()
168{
169 Canvas cv(&VGAController);
170 cv.setPenColor(Color::White);
171 cv.setBrushColor(Color::Black);
172 cv.clear();
173 cv.fillRectangle(0, 0, cv.getWidth() - 1, cv.getHeight() - 1);
174 cv.drawRectangle(0, 0, cv.getWidth() - 1, cv.getHeight() - 1);
175
176 cv.selectFont(&fabgl::FONT_8x8);
177 cv.setGlyphOptions(GlyphOptions().FillBackground(true).DoubleWidth(1));
178 cv.drawText(40, 20, VGAController.getResolutionTimings()->label);
179
180 cv.setGlyphOptions(GlyphOptions());
181 cv.drawTextFmt(40, 40, "Screen Size : %d x %d", VGAController.getScreenWidth(), VGAController.getScreenHeight());
182 cv.drawTextFmt(40, 60, "Viewport Size : %d x %d", cv.getWidth(), cv.getHeight());
183 cv.drawText(40, 80, "Commands (More on UART):");
184 cv.drawText(40, 100, " w = Move Up z = Move Down");
185 cv.drawText(40, 120, " a = Move Left s = Move Right");
186 cv.drawText(40, 140, " + = Next Resolution");
187 cv.drawRectangle(35, 15, 310, 155);
188}
189
190
191
192void setup()
193{
194 Console.begin();
195
196
197 // avoid garbage into the UART
198 vTaskDelay(pdMS_TO_TICKS(500));
199
200 VGAController.begin();
201 VGAController.setResolution(PresetResolutions[currentResolution]);
202 updateScreen();
203 printHelp();
204 printInfo();
205}
206
207
208void loop()
209{
211
212 if (Console.available() > 0) {
213 char c = Console.read();
214 switch (c) {
215 case 'h':
216 printHelp();
217 break;
218 case 'w':
219 --moveY;
220 VGAController.moveScreen(0, -1);
221 printInfo();
222 break;
223 case 'z':
224 ++moveY;
225 VGAController.moveScreen(0, +1);
226 printInfo();
227 break;
228 case 'a':
229 --moveX;
230 VGAController.moveScreen(-1, 0);
231 printInfo();
232 break;
233 case 's':
234 ++moveX;
235 VGAController.moveScreen(+1, 0);
236 printInfo();
237 break;
238 case 'x':
239 moveX = moveY = shrinkX = shrinkY = 0;
240 VGAController.setResolution(PresetResolutions[currentResolution]);
241 updateScreen();
242 printInfo();
243 break;
244 case '+':
245 case '-':
246 moveX = moveY = shrinkX = shrinkY = 0;
247 currentResolution = (c == '+' ? currentResolution + 1 : currentResolution - 1);
248 if (currentResolution == sizeof(PresetResolutions) / sizeof(const char *))
249 currentResolution = 0;
250 if (currentResolution < 0)
251 currentResolution = sizeof(PresetResolutions) / sizeof(const char *) - 1;
252 VGAController.setResolution(PresetResolutions[currentResolution]);
253 updateScreen();
254 printInfo();
255 break;
256 case '"':
257 {
258 moveX = moveY = shrinkX = shrinkY = 0;
259 // read modeline until newline
260 char modeline[128] = "\"";
261 uint8_t ch;
262 int len = 1;
263 while (len < sizeof(modeline) - 1 && Console.readBytes(&ch, 1) == 1 && ch != '\n')
264 modeline[len++] = ch;
265 modeline[len] = 0;
266 VGAController.setResolution(modeline);
267 updateScreen();
268 printInfo();
269 break;
270 }
272 case 'e':
273 case 'E':
274 t = *VGAController.getResolutionTimings();
275 t.HFrontPorch = (c == 'e' ? t.HFrontPorch - 1 : t.HFrontPorch + 1);
276 t.HBackPorch = (c == 'e' ? t.HBackPorch + 1 : t.HBackPorch - 1);
277 VGAController.setResolution(t);
278 updateScreen();
279 printInfo();
280 break;
281 case 'r':
282 case 'R':
283 t = *VGAController.getResolutionTimings();
284 t.HSyncPulse = (c == 'r' ? t.HSyncPulse - 1 : t.HSyncPulse + 1);
285 t.HBackPorch = (c == 'r' ? t.HBackPorch + 1 : t.HBackPorch - 1);
286 VGAController.setResolution(t);
287 updateScreen();
288 printInfo();
289 break;
290 case 't':
291 case 'T':
292 t = *VGAController.getResolutionTimings();
293 t.HBackPorch = (c == 't' ? t.HBackPorch - 1 : t.HBackPorch + 1);
294 t.HFrontPorch = (c == 't' ? t.HFrontPorch + 1 : t.HFrontPorch - 1);
295 VGAController.setResolution(t);
296 updateScreen();
297 printInfo();
298 break;
299 case 'y':
300 case 'Y':
301 t = *VGAController.getResolutionTimings();
302 t.VFrontPorch = (c == 'y' ? t.VFrontPorch - 1 : t.VFrontPorch + 1);
303 t.VBackPorch = (c == 'y' ? t.VBackPorch + 1 : t.VBackPorch - 1);
304 VGAController.setResolution(t);
305 updateScreen();
306 printInfo();
307 break;
308 case 'u':
309 case 'U':
310 t = *VGAController.getResolutionTimings();
311 t.VSyncPulse = (c == 'u' ? t.VSyncPulse - 1 : t.VSyncPulse + 1);
312 t.VBackPorch = (c == 'u' ? t.VBackPorch + 1 : t.VBackPorch - 1);
313 VGAController.setResolution(t);
314 updateScreen();
315 printInfo();
316 break;
317 case 'i':
318 case 'I':
319 t = *VGAController.getResolutionTimings();
320 t.VBackPorch = (c == 'i' ? t.VBackPorch - 1 : t.VBackPorch + 1);
321 t.VFrontPorch = (c == 'i' ? t.VFrontPorch + 1 : t.VFrontPorch - 1);
322 VGAController.setResolution(t);
323 updateScreen();
324 printInfo();
325 break;
326 //*/
327 case 'o':
328 case 'O':
329 t = *VGAController.getResolutionTimings();
330 t.frequency = (c == 'o' ? t.frequency - 5000 : t.frequency + 5000);
331 VGAController.setResolution(t);
332 updateScreen();
333 printInfo();
334 break;
335 case '.':
336 t = *VGAController.getResolutionTimings();
337 switch (t.HStartingBlock) {
338 case VGAScanStart::FrontPorch:
339 t.HStartingBlock = VGAScanStart::Sync;
340 break;
341 case VGAScanStart::Sync:
342 t.HStartingBlock = VGAScanStart::BackPorch;
343 break;
344 case VGAScanStart::BackPorch:
345 t.HStartingBlock = VGAScanStart::VisibleArea;
346 break;
347 case VGAScanStart::VisibleArea:
348 t.HStartingBlock = VGAScanStart::FrontPorch;
349 break;
350 }
351 VGAController.setResolution(t);
352 updateScreen();
353 printInfo();
354 break;
355 case 'n':
356 ++shrinkX;
357 VGAController.shrinkScreen(+1, 0);
358 updateScreen();
359 printInfo();
360 break;
361 case 'N':
362 --shrinkX;
363 VGAController.shrinkScreen(-1, 0);
364 updateScreen();
365 printInfo();
366 break;
367 case 'm':
368 ++shrinkY;
369 VGAController.shrinkScreen(0, +1);
370 updateScreen();
371 printInfo();
372 break;
373 case 'M':
374 --shrinkY;
375 VGAController.shrinkScreen(0, -1);
376 updateScreen();
377 printInfo();
378 break;
379 }
380 }
381}
382
383
384extern "C" void app_main()
385{
386 setup();
387 while (true)
388 loop();
389}
Provides a set of drawing methods.
Definition canvas.h:73
int getScreenHeight()
Determines the screen height in pixels.
int getViewPortHeight()
Determines the height of the viewport.
int getViewPortWidth()
Determines the width of the viewport.
int getScreenWidth()
Determines the screen width in pixels.
bool begin(int port=0, int baud=0, int rxPin=-1, int txPin=-1, int rxBufferSize=1024, int txBufferSize=0)
Initializes the UART.
A Stream implemented on top of the ESP-IDF UART driver.
Definition uartstream.h:68
VGA bitmapped display controller with 2 colors.
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
void shrinkScreen(int shrinkX, int shrinkY)
Shrinks or expands the screen by the specified horizontal and vertical amounts.
void moveScreen(int offsetX, int offsetY)
Moves the screen by the specified horizontal and vertical offsets.
Single include file of the FabGL library.
#define VGA_320x200_75Hz
Definition fabglconf.h:183
#define VGA_640x480_60HzAlt1
Definition fabglconf.h:249
#define SVGA_800x600_56Hz
Definition fabglconf.h:297
#define SVGA_800x300_60Hz
Definition fabglconf.h:294
#define SVGA_800x600_60Hz
Definition fabglconf.h:300
#define VGA_640x480_60Hz
Definition fabglconf.h:246
#define VESA_640x350_85Hz
Definition fabglconf.h:231
#define VGA_720x348_73Hz
Definition fabglconf.h:273
#define SVGA_1024x768_60Hz
Definition fabglconf.h:306
#define VGA_640x480_73Hz
Definition fabglconf.h:255
#define SVGA_960x540_60Hz
Definition fabglconf.h:303
#define VGA_720x348_50HzD
Definition fabglconf.h:267
#define SVGA_1280x768_50Hz
Definition fabglconf.h:324
#define VGA_640x350_70Hz
Definition fabglconf.h:225
#define SVGA_1280x720_60Hz
Definition fabglconf.h:318
#define VGA_480x300_75Hz
Definition fabglconf.h:195
#define VGA_640x200_60HzD
Definition fabglconf.h:210
#define PAL_720x576_50Hz
Definition fabglconf.h:285
#define VGA_256x384_60Hz
Definition fabglconf.h:175
#define VGA_400x300_60Hz
Definition fabglconf.h:192
#define VESA_768x576_60Hz
Definition fabglconf.h:291
#define VGA_640x400_70Hz
Definition fabglconf.h:240
#define VGA_640x240_60Hz
Definition fabglconf.h:219
#define VGA_512x384_60Hz
Definition fabglconf.h:201
#define QVGA_320x240_60Hz
Definition fabglconf.h:189
#define VESA_720x400_85Hz
Definition fabglconf.h:282
#define VGA_640x384_60Hz
Definition fabglconf.h:237
#define VGA_320x200_70Hz
Definition fabglconf.h:180
#define VGA_512x448_60Hz
Definition fabglconf.h:204
#define SVGA_1280x600_60Hz
Definition fabglconf.h:315
#define VGA_640x200_70Hz
Definition fabglconf.h:213
#define VGA_720x400_70Hz
Definition fabglconf.h:279
#define VGA_640x480_60HzD
Definition fabglconf.h:252
#define VESA_640x480_75Hz
Definition fabglconf.h:258
#define VGA_640x400_60Hz
Definition fabglconf.h:243
#define VGA_640x350_70HzAlt1
Definition fabglconf.h:228
#define VGA_512x512_58Hz
Definition fabglconf.h:207
#define SVGA_1280x720_60HzAlt1
Definition fabglconf.h:321
#define SVGA_1024x768_75Hz
Definition fabglconf.h:312
#define SVGA_1024x768_70Hz
Definition fabglconf.h:309
#define VGA_512x192_60Hz
Definition fabglconf.h:198
#define VGA_640x382_60Hz
Definition fabglconf.h:234
size_t readBytes(uint8_t *buffer, size_t length)
Reads up to length bytes, waiting until all bytes have been received or the timeout expires.
Definition fabutils.cpp:74
VGAScanStart HStartingBlock
Specifies the VGA timings (a decoded modeline).
Specifies glyph painting options.