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

Color composite video (PAL / NTSC) test screen, generated by the ESP32 internal DAC with no external components.

The screen shows a small GUI to choose the video standard, plus an RGB color bar test and a circle to check the aspect ratio. The selected settings are saved in NVS and restored at the next boot.

What it demonstrates

  • fabgl::CVBS16Controller: 16-color composite video output on GPIO25
  • Interlaced and progressive PAL-B and NTSC-M modes, including wide and extended variants
  • Monochrome output and horizontal rate multiplier (setHorizontalRate())
  • The FabGL GUI (uiApp, combo box, check box, button) on a composite display
  • fabgl::Preferences (NVS) to store settings

Hardware

  • Composite video output on GPIO25 (connect to the yellow RCA input of a TV; a 5 MHz low-pass filter may improve the picture)
  • PS/2 keyboard on port 0 and PS/2 mouse on port 1, to operate the GUI

Usage

Use the mouse (or the keyboard: Tab moves the focus, Space/Enter activates) to change:

Control Meaning
Mode Video standard: Interlaced/Progressive PAL-B, NTSC-M, their Wide variants and Progressive NTSC-M Ext (default: Progressive PAL-B)
B/W Monochrome output (applied immediately)
H. Rate Horizontal rate multiplier x1, x2, x3 (higher values give more horizontal pixels; default x2)
Apply Saves the settings. If Mode or H. Rate changed, the board restarts to apply them

The bottom of the screen shows free memory and the current resolution.

If a mode does not sync on your TV, erase the NVS partition (idf.py erase-flash) to go back to the defaults.

Build

See Building the examples.

Note: this example has not been tested on hardware with FabGL2 yet.

Source code

Main file of the example, main/BaseExample.cpp. The complete project is in the examples/Composite/BaseExample 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
28
29#include "freertos/FreeRTOS.h"
30#include "freertos/task.h"
31#include "esp_system.h"
32#include "fabgl.h"
33#include "devdrivers/cvbsgenerator.h"
34
35
36#define VIDEOOUT_GPIO GPIO_NUM_25
37
38
39Preferences preferences;
40
41
42fabgl::CVBS16Controller DisplayController;
43fabgl::PS2Controller PS2Controller;
44
45
46
47// preferences keys
48static const char * PREF_MODE = "Mode";
49static const char * PREF_MONO = "Mono";
50static const char * PREF_HRATE = "HRate";
51
52
53
54// defaults
55#define MODES_DEFAULT 1 // P-PAL-B
56#define MONO_DEFAULT 0 // color
57#define HRATE_DEFAULT 2 // horizontal rate (1 = x1, 2 = x2, 3 = x3)
58
59
60
61// modes
62
63static const char * MODES_DESC[] = { "Interlaced PAL-B",
64 "Progressive PAL-B",
65 "Interlaced NTSC-M",
66 "Progressive NTSC-M",
67 "Interlaced PAL-B Wide",
68 "Progressive PAL-B Wide",
69 "Interlaced NTSC-M Wide",
70 "Progressive NTSC-M Wide",
71 "Progressive NTSC-M Ext",
72 };
73
74static const char * MODES_STD[] = { "I-PAL-B",
75 "P-PAL-B",
76 "I-NTSC-M",
77 "P-NTSC-M",
78 "I-PAL-B-WIDE",
79 "P-PAL-B-WIDE",
80 "I-NTSC-M-WIDE",
81 "P-NTSC-M-WIDE",
82 "P-NTSC-M-EXT",
83 };
84
85
86
87// UI Style
88
89enum { STYLE_NONE, STYLE_LABEL, STYLE_STATICLABEL, STYLE_BUTTON, STYLE_COMBOBOX, STYLE_CHECKBOX };
90
91#define BACKGROUND_COLOR RGB888(0, 0, 0)
92
93#define COLORBARS_HEIGHT 30
94#define CIRCLE_SIZE 80
95
96struct DialogStyle : uiStyle {
97 void setStyle(uiObject * object, uint32_t styleClassID) {
98 switch (styleClassID) {
99 case STYLE_LABEL:
100 ((uiLabel*)object)->labelStyle().backgroundColor = BACKGROUND_COLOR;
101 ((uiLabel*)object)->labelStyle().textColor = RGB888(255, 255, 255);
102 break;
103 case STYLE_STATICLABEL:
104 ((uiStaticLabel*)object)->labelStyle().backgroundColor = BACKGROUND_COLOR;
105 ((uiStaticLabel*)object)->labelStyle().textColor = RGB888(255, 255, 255);
106 break;
107 case STYLE_BUTTON:
108 ((uiButton*)object)->windowStyle().borderColor = RGB888(128, 128, 128);
109 ((uiButton*)object)->buttonStyle().backgroundColor = RGB888(0, 255, 0);
110 break;
111 case STYLE_COMBOBOX:
112 ((uiComboBox*)object)->windowStyle().borderColor = RGB888(255, 255, 255);
113 ((uiComboBox*)object)->windowStyle().borderSize = 1;
114 break;
115 case STYLE_CHECKBOX:
116 ((uiCheckBox*)object)->windowStyle().borderColor = RGB888(255, 255, 255);
117 ((uiCheckBox*)object)->checkBoxStyle().mouseOverForegroundColor = RGB888(0, 0, 0);
118 ((uiCheckBox*)object)->checkBoxStyle().mouseOverBackgroundColor = RGB888(0, 255, 0);
119 break;
120 }
121 }
122} dialogStyle;
123
124
125
126// main app
127
128struct MyApp : public uiApp {
129
130 uiComboBox * modeComboBox;
131 uiCheckBox * monoCheckbox;
132 uiComboBox * hrateComboBox;
133
134 void init() {
135
136 setStyle(&dialogStyle);
137
138 // set root window background color to dark green
139 rootWindow()->frameStyle().backgroundColor = BACKGROUND_COLOR;
140 //rootWindow()->windowStyle().borderColor = RGB888(255, 255, 0);
141 //rootWindow()->windowStyle().borderSize = 1;
142
143 // some static text
144 rootWindow()->onPaint = [&]() {
145 auto cv = canvas();
146 cv->selectFont(&fabgl::FONT_SLANT_8x14);
147 cv->setPenColor(RGB888(0, 128, 255));
148 cv->drawText(2, 5, "Composite Video Test - fabgl.com");
149
150 cv->selectFont(&fabgl::FONT_std_12);
151 cv->setPenColor(RGB888(255, 128, 0));
152 cv->drawText(2, 19, "2019/22 by Fabrizio Di Vittorio");
153
154 // color test
155 auto w = cv->getWidth();
156 auto h = cv->getHeight();
157 cv->drawText(2, h - COLORBARS_HEIGHT - 15, "RGB Colors Test");
158 cv->setBrushColor(Color::BrightRed);
159 cv->fillRectangle(0, h - COLORBARS_HEIGHT, w / 3, h - 1);
160 cv->setBrushColor(Color::BrightGreen);
161 cv->fillRectangle(w / 3, h - COLORBARS_HEIGHT, w * 2 / 3, h - 1);
162 cv->setBrushColor(Color::BrightBlue);
163 cv->fillRectangle(w * 2 / 3, h - COLORBARS_HEIGHT, w - 1, h - 1);
164
165 // aspect ratio test
166 int cx = w - CIRCLE_SIZE / 2 - 1;
167 int cy = h - COLORBARS_HEIGHT - CIRCLE_SIZE / 2 - 1;
168 cv->setPenColor(Color::BrightYellow);
169 cv->drawText(cx - CIRCLE_SIZE / 2 + 2, cy - 3, "Aspect Ratio Test");
170 cv->setBrushColor(Color::BrightWhite);
171 cv->drawEllipse(cx, cy, CIRCLE_SIZE, CIRCLE_SIZE);
172 };
173
174 // mode selection
175 new uiStaticLabel(rootWindow(), "Mode", Point(2, 60), true, STYLE_STATICLABEL);
176 modeComboBox = new uiComboBox(rootWindow(), Point(40, 58), Size(140, 20), 80, true, STYLE_COMBOBOX);
177 modeComboBox->items().append(MODES_DESC, sizeof(MODES_DESC) / sizeof(char*));
178 modeComboBox->selectItem(getMode());
179
180 // monochromatic checkbox
181 new uiStaticLabel(rootWindow(), "B/W", Point(2, 88), true, STYLE_STATICLABEL);
182 monoCheckbox = new uiCheckBox(rootWindow(), Point(40, 85), Size(16, 20), uiCheckBoxKind::CheckBox, true, STYLE_CHECKBOX);
183 monoCheckbox->setChecked(getMonochromatic());
184 monoCheckbox->onChange = [&]() {
185 DisplayController.setMonochrome(monoCheckbox->checked());
186 };
187
188 // horizontal rate
189 new uiStaticLabel(rootWindow(), "H. Rate", Point(103, 88), true, STYLE_STATICLABEL);
190 hrateComboBox = new uiComboBox(rootWindow(), Point(145, 84), Size(35, 20), 55, true, STYLE_COMBOBOX);
191 hrateComboBox->items().appendSepList("x1,x2,x3", ',');
192 hrateComboBox->selectItem(getHRate() - 1);
193
194 // apply button
195 auto applyButton = new uiButton(rootWindow(), "Apply", Point(40, 117), Size(80, 20), uiButtonKind::Button, true, STYLE_BUTTON);
196 applyButton->onClick = [&]() {
197 apply();
198 };
199
200 // info label
201 auto infoLabel = new uiLabel(rootWindow(), "", Point(2, rootWindow()->clientSize().height - COLORBARS_HEIGHT * 2), Size(0, 0), true, STYLE_LABEL);
202 infoLabel->setTextFmt("Free Mem: %d KiB Resolution: %d x %d", heap_caps_get_free_size(MALLOC_CAP_32BIT) / 1024, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight());
203
204 }
205
206 static int getMode() {
207 return preferences.getInt(PREF_MODE, MODES_DEFAULT);
208 }
209
210 static bool getMonochromatic() {
211 return preferences.getInt(PREF_MONO, MONO_DEFAULT);
212 }
213
214 static int getHRate() {
215 return preferences.getInt(PREF_HRATE, HRATE_DEFAULT);
216 }
217
218 void apply() {
219 bool reboot = modeComboBox->selectedItem() != getMode() ||
220 hrateComboBox->selectedItem() != getHRate();
221
222 preferences.putInt(PREF_MODE, modeComboBox->selectedItem());
223 preferences.putInt(PREF_MONO, monoCheckbox->checked());
224 preferences.putInt(PREF_HRATE, hrateComboBox->selectedItem() + 1);
225
226 if (reboot)
227 esp_restart();
228 }
229
230
231} app;
232
233
234
235void setup()
236{
237
238 preferences.begin("CVBS", false);
239
240 PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
241
242 DisplayController.begin(VIDEOOUT_GPIO);
243
244 auto mode = MyApp::getMode();
245
246 DisplayController.setHorizontalRate(MyApp::getHRate());
247 DisplayController.setMonochrome(MyApp::getMonochromatic());
248
249 DisplayController.setResolution(MODES_STD[mode]);
250
251}
252
253
254#if FABGLIB_CVBSCONTROLLER_PERFORMANCE_CHECK
255namespace fabgl {
256 extern volatile uint64_t s_cvbsctrlcycles;
257}
258using fabgl::s_cvbsctrlcycles;
259#endif
260
261
262void loop()
263{
264 app.runAsync(&DisplayController, 2000);
265
266 #if FABGLIB_CVBSCONTROLLER_PERFORMANCE_CHECK
267 uint32_t s1 = 0;
268 while (true) {
269 printf("%lld / %d (%d%%)\n", s_cvbsctrlcycles, fabgl::getCycleCount() - s1, (int)((double)s_cvbsctrlcycles/240000000*100));
270 s_cvbsctrlcycles = 0;
271 s1 = fabgl::getCycleCount();
272 vTaskDelay(pdMS_TO_TICKS(1000));
273 }
274 #else
275 vTaskDelete(NULL);
276 #endif
277
278
279}
280
281
282extern "C" void app_main()
283{
284 setup();
285 while (true)
286 loop();
287}
bool begin(char const *name, bool readOnly=false)
Opens the specified namespace.
Simple key/value storage in non-volatile flash (NVS).
Definition preferences.h:61
int getViewPortHeight()
Determines the height of the viewport.
int getViewPortWidth()
Determines the width of the viewport.
void setMonochrome(bool value)
Enables or disables monochrome output (no color burst).
Composite video (PAL/NTSC) bitmapped display controller with 16 colors.
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes the PS/2 device controller.
PS/2 device controller.
int selectedItem()
Represents currently selected item.
Definition fabui.h:2481
void selectItem(int index)
Selects an item.
Definition fabui.cpp:4502
Represents the whole application base class.
Definition fabui.h:4323
uiButton(uiWindow *parent, char const *text, const Point &pos, const Size &size, uiButtonKind kind=uiButtonKind::Button, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:2537
Represents a button control. A button can have text and optionally a bitmap.
Definition fabui.h:1299
void setChecked(bool value)
Sets current checkbox or radiobutton checked status.
Definition fabui.cpp:4884
bool checked()
Determines whether the checkbox or radiobutton is checked.
Definition fabui.h:2744
Delegate onChange
Change event delegate.
Definition fabui.h:2777
uiCheckBox(uiWindow *parent, const Point &pos, const Size &size, uiCheckBoxKind kind=uiCheckBoxKind::CheckBox, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:4771
Represents a checkbox or a radiobutton.
Definition fabui.h:2712
StringList & items()
A list of strings representing items of the combobox.
Definition fabui.h:2570
uiComboBox(uiWindow *parent, const Point &pos, const Size &size, int listHeight, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:4694
This is a combination of a listbox and a single-line editable textbox.
Definition fabui.h:2545
uiLabel(uiWindow *parent, char const *text, const Point &pos, const Size &size=Size(0, 0), bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:3188
A label is a static text UI element.
Definition fabui.h:1606
Base class of all UI elements like windows and controls.
Definition fabui.h:301
uiStaticLabel(uiWindow *parent, char const *text, const Point &pos, bool visible=true, uint32_t styleClassID=0)
Creates an instance of the object.
Definition fabui.cpp:3304
A staticlabel is a light version of uiLabel (text must be static). uiStaticLabel has lower memory foo...
Definition fabui.h:1710
Single include file of the FabGL library.
Represents a 24-bit RGB color.
Base class for application styles.
Definition fabui.h:4244