Skip to content

Commit dd0a759

Browse files
Updated with new sketch
1 parent aeb4fab commit dd0a759

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

content/hardware/09.mega/shields/giga-display-shield/tutorials/image-orientation/orientation-content.md

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Any modern device uses sensors to determine the correct orientation in which an
1212
- [GIGA R1 WiFi](/hardware/giga-r1).
1313
- [GIGA Display Shield]()
1414
- [Arduino IDE](https://www.arduino.cc/en/software)
15-
- [Arduino_BMI270_BMM150 library]()
16-
- [ArduinoGraphics library]()
17-
- [Library]() library.
15+
- [Arduino_BMI270_BMM150 library](https://reference.arduino.cc/reference/en/libraries/arduino_bmi270_bmm150/)
16+
- [Arduino_H7_Video](https://github.com/arduino/ArduinoCore-mbed/tree/main/libraries/Arduino_H7_Video) library.
17+
- [Lvgl](https://reference.arduino.cc/reference/en/libraries/lvgl/) library.
1818

1919
## Downloading the Library and Core
2020

@@ -43,41 +43,37 @@ Any image could be used here. This tutorial will use the following image of the
4343
Now to first get the readings from the IMU we will use the `"Arduino_BMI270_BMM150.h"` library. Then we need to set the image name variables with `extern const lv_img_dsc_t arduino_logo_1;`. To use the IMU set it up with `BoschSensorClass imu(Wire1);`.
4444

4545
```arduino
46-
47-
#include "Portenta_lvgl.h"
48-
#include "lv_demo_widgets.h"
4946
#include "Arduino_BMI270_BMM150.h"
47+
#include "Arduino_H7_Video.h"
48+
#include "lvgl.h"
5049
51-
extern const lv_img_dsc_t arduino_logo_1;
52-
53-
void LCD_ST7701_Init();
50+
Arduino_H7_Video Display(800, 480, GigaDisplayShield); /* Arduino_H7_Video Display(1024, 768, USBCVideo); */
5451
5552
BoschSensorClass imu(Wire1);
5653
```
5754

5855
initialize the display with `Display.begin();` and start recieving IMU readings with `imu.begin();`. Next, we can give the image its attributes.
5956

6057
```arduino
58+
LV_IMG_DECLARE(img_arduinologo);
59+
lv_obj_t * img;
60+
6161
void setup() {
6262
Serial.begin(115200);
63+
6364
Display.begin();
6465
imu.begin();
6566
66-
LV_IMG_DECLARE(arduino_logo_1);
67-
lv_obj_t * obj;
68-
obj = lv_obj_create(lv_scr_act());
69-
lv_obj_set_size(obj, 350, 350);
70-
71-
avatar = lv_img_create(obj);
72-
lv_img_set_src(avatar, &arduino_logo_1);
67+
img = lv_img_create(lv_scr_act());
68+
lv_img_set_src(img, &img_arduinologo);
69+
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
70+
lv_img_set_pivot(img, (img_arduinologo.header.w)/2, (img_arduinologo.header.h)/2); /* Rotate around the center of the image */
7371
}
74-
7572
```
7673

7774
Now all that is left is to change the image depending on the IMU readings. First, declare the variables that will hold the values. Then to assign them the IMU reading values use `imu.readAcceleration(x, y, z);`. Next, we use `if ()` statements to change the rotation variable depending on the readings we are getting. And at the end, we render the image with the correct rotation.
7875

7976
```arduino
80-
8177
uint8_t rotation = 0;
8278
8379
void loop() {
@@ -94,12 +90,16 @@ void loop() {
9490
} else if (y > 0.8) {
9591
rotation = 3;
9692
}
97-
lv_img_set_angle(avatar, 900 - atan(x / y) * 180.0 / M_PI * 10);
93+
int16_t rot_angle = 900 - atan(x / y) * 180.0 / M_PI * 10;
94+
lv_img_set_angle(img, rot_angle);
9895
}
9996
}
97+
lv_timer_handler();
10098
}
10199
```
102100

101+
### IMU Test Sketch
102+
103103
The easiest way to tell what values you are getting depending on the orientation of the device is to use a simple sketch, like the one below that will simply print the IMU values in the serial monitor. Take note of the values you are getting when you rotate the shield and you can use them in the previous sketch.
104104

105105
```arduino
@@ -125,10 +125,56 @@ void loop(){
125125
}
126126
```
127127

128+
### Full Sketch
129+
128130
Now to put it all together where the image will change depending on how we rotate the board and shield:
129131

130132
```arduino
133+
#include "Arduino_BMI270_BMM150.h"
134+
#include "Arduino_H7_Video.h"
135+
#include "lvgl.h"
136+
137+
Arduino_H7_Video Display(800, 480, GigaDisplayShield); /* Arduino_H7_Video Display(1024, 768, USBCVideo); */
138+
139+
BoschSensorClass imu(Wire1);
140+
141+
LV_IMG_DECLARE(img_arduinologo);
142+
lv_obj_t * img;
143+
144+
void setup() {
145+
Serial.begin(115200);
146+
147+
Display.begin();
148+
imu.begin();
149+
150+
img = lv_img_create(lv_scr_act());
151+
lv_img_set_src(img, &img_arduinologo);
152+
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
153+
lv_img_set_pivot(img, (img_arduinologo.header.w)/2, (img_arduinologo.header.h)/2); /* Rotate around the center of the image */
154+
}
131155
156+
uint8_t rotation = 0;
157+
158+
void loop() {
159+
float x, y, z;
160+
if (imu.accelerationAvailable()) {
161+
imu.readAcceleration(x, y, z);
162+
if ( z < 0.8 && z > -0.8) {
163+
if (x < -0.8) {
164+
rotation = 0;
165+
} else if (x > 0.8) {
166+
rotation = 2;
167+
} else if (y < -0.8) {
168+
rotation = 1;
169+
} else if (y > 0.8) {
170+
rotation = 3;
171+
}
172+
int16_t rot_angle = 900 - atan(x / y) * 180.0 / M_PI * 10;
173+
lv_img_set_angle(img, rot_angle);
174+
}
175+
}
176+
lv_timer_handler();
177+
}
132178
```
133179

134180
## Testing it Out

0 commit comments

Comments
 (0)