Skip to content

Commit 3edade7

Browse files
Added new images
1 parent 1aebbfb commit 3edade7

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed

content/hardware/10.mega/shields/giga-display-shield/tutorials/10.square-line-tutorial/assets/button_label_gui.svg

Lines changed: 15 additions & 0 deletions
Loading

content/hardware/10.mega/shields/giga-display-shield/tutorials/10.square-line-tutorial/assets/names_highlight.svg

Lines changed: 17 additions & 0 deletions
Loading

content/hardware/10.mega/shields/giga-display-shield/tutorials/10.square-line-tutorial/content.md

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: Benjamin Dannegård
55
tags: [Display, squareline, LVGL]
66
---
77

8-
The GIGA Display Shield with the GIGA R1 WiFi board can run LVGL which allows for the creation of GUIs. To make the design of the GUI easier we can use the software SquareLine Studio. This will allow us to drag and drop different elements and then export it for usage on the display shield.
8+
The GIGA Display Shield with the GIGA R1 WiFi board can run LVGL which allows for the creation of advanced GUIs. To make the design of the GUI easier we can use the software SquareLine Studio. This will allow us to drag and drop different elements and then export it for usage on the display shield.
99

1010
## Hardware & Software Needed
1111

@@ -53,7 +53,7 @@ In the folder that you exported the files to, go into the "libraries" folder and
5353

5454
![Folder structure of the exported files](assets/folder_structure.svg)
5555

56-
Place this folder in your **Arduino->Libraries** folder.
56+
Place this folder in your **Libraries** folder found inside your **Arduino** folder. This is the same **Arduino** folder that contains your Arduino IDE sketches.
5757

5858
Now to run the SquareLine Studio project, use this sketch:
5959

@@ -82,6 +82,128 @@ void loop() {
8282
}
8383
```
8484

85+
## Using SquareLine Studio Widgets in Arduino IDE
86+
87+
The project from SquareLine Studio is exported as a library, let's take a look at how we can reference a specific element in the GUI. To demonstrate this we will first create a simple GUI with a increase button, a decrease button and a label. The label will show the number that will decrease or increase depending on what button is pressed. Our GUI will look like this:
88+
89+
![The GUI we will be using in this section](assets/button_label_gui.svg)
90+
91+
Pay attention to the names of the button and the label, which can be seen on the right side and the upper left side here:
92+
93+
![Name sections highlighted in SquareLine Studio](assets/names_highlight.svg)
94+
95+
Name the widgets accordingly:
96+
97+
- **Increase Button**: ui_ButtonInc
98+
- **Decrease Button**: ui_ButtonDec
99+
- **Number Label**: ui_LabelNum
100+
101+
Now export the project and put the library in the Arduino libraries folder, as shown in the previous section.
102+
103+
Download this [lv_conf.h]() file and put it in your */mbed/libraries/Arduino_H7_Video/src folder. After setting this up we can go ahead and link the buttons to the label in our sketch.
104+
105+
First declare the libraries and set up the screen, this will be the same as the sketch above.
106+
107+
```arduino
108+
#include "Arduino_H7_Video.h"
109+
#include "Arduino_GigaDisplayTouch.h"
110+
111+
#include "lvgl.h"
112+
#include "ui.h"
113+
114+
/* Insert resolution WxH according to your SquareLine studio project settings */
115+
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
116+
Arduino_GigaDisplayTouch Touch;
117+
```
118+
119+
Then it is as simple as using the names of the widgets in a LVGL function. For example, when a function like `lv_label_set_text_fmt(NAME_OF_LABEL, "Label");` needs a reference to a label object, we can enter the name of the label that we created in SquareLine Studio. Same goes for the button widgets, like this in the `setup()` function:
120+
121+
```arduino
122+
void setup() {
123+
Display.begin();
124+
Touch.begin();
125+
126+
/* Initialize the user interface designed with SquareLine Studio */
127+
ui_init();
128+
129+
/* Add buttons event handlers */
130+
lv_obj_add_event_cb(ui_ButtonInc, ButtonInc_evt_handler, LV_EVENT_ALL, NULL);
131+
lv_obj_add_event_cb(ui_ButtonDec, ButtonDec_evt_handler, LV_EVENT_ALL, NULL);
132+
133+
/* Set initial value of the label to zero */
134+
label_val = 0;
135+
lv_label_set_text_fmt(ui_LabelNum, "%d", label_val);
136+
}
137+
```
138+
139+
The last important part of the sketch are the callback functions that will run when a button is pressed. In the segment above you can see how the callback functions are tied to the buttons. The function will check when the button is pressed and increase the count on the label:
140+
141+
```arduino
142+
static void ButtonInc_evt_handler(lv_event_t * e) {
143+
lv_event_code_t code = lv_event_get_code(e);
144+
145+
if(code == LV_EVENT_CLICKED) {
146+
label_val++;
147+
lv_label_set_text_fmt(ui_LabelNum, "%d", label_val);
148+
}
149+
}
150+
```
151+
152+
### Full Sketch
153+
154+
```arduino
155+
#include "Arduino_H7_Video.h"
156+
#include "Arduino_GigaDisplayTouch.h"
157+
158+
#include "lvgl.h"
159+
#include "ui.h"
160+
161+
/* Initialize the GIGA Display Shield with a resolution of 800x480 pixels */
162+
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
163+
Arduino_GigaDisplayTouch Touch;
164+
165+
int label_val;
166+
167+
static void ButtonInc_evt_handler(lv_event_t * e) {
168+
lv_event_code_t code = lv_event_get_code(e);
169+
170+
if(code == LV_EVENT_CLICKED) {
171+
label_val++;
172+
lv_label_set_text_fmt(ui_LabelNum, "%d", label_val);
173+
}
174+
}
175+
176+
static void ButtonDec_evt_handler(lv_event_t * e) {
177+
lv_event_code_t code = lv_event_get_code(e);
178+
179+
if(code == LV_EVENT_CLICKED) {
180+
label_val--;
181+
lv_label_set_text_fmt(ui_LabelNum, "%d", label_val);
182+
}
183+
}
184+
185+
void setup() {
186+
Display.begin();
187+
Touch.begin();
188+
189+
/* Initialize the user interface designed with SquareLine Studio */
190+
ui_init();
191+
192+
/* Add buttons event handlers */
193+
lv_obj_add_event_cb(ui_ButtonInc, ButtonInc_evt_handler, LV_EVENT_ALL, NULL);
194+
lv_obj_add_event_cb(ui_ButtonDec, ButtonDec_evt_handler, LV_EVENT_ALL, NULL);
195+
196+
/* Set initial value of the label to zero */
197+
label_val = 0;
198+
lv_label_set_text_fmt(ui_LabelNum, "%d", label_val);
199+
}
200+
201+
void loop() {
202+
/* Feed LVGL engine */
203+
lv_timer_handler();
204+
}
205+
```
206+
85207
## Next Step
86208

87209
If you are curious about how LVGL works with the GIGA Display Shield, take a look at our [LVGL Guide](/tutorials/giga-display-shield/lvgl-guide).

0 commit comments

Comments
 (0)