Skip to content

Screen rotation feature #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,33 @@ Make sure your STL is low poly. The 3D example use a model with < 5000 triangles
## Converting Sprites and Images
The Utilities folder provides a convenient [SpriteEditor](https://htmlpreview.github.io/?https://github.com/bitluni/ESP32Lib/blob/master/Utilities/SpriteEditor.html) that you can use directly from the browser. No worries, your files are not uploaded.
The correct pixel format for VGA14Bit and VGA3Bit is R5G5B4A2 (that will be improved in future). You can import PNG files to use transparency.
Each sprite has the origin in the center by default. You can modify it by changing the x/y values of the first point definition. Clicking on the image creates additional points that can be used for other purpouses like hit boxes etc.
Each sprite has the origin in the center by default. You can modify it by changing the x/y values of the first point definition. Clicking on the image creates additional points that can be used for other purpouses like hit boxes etc.
## Changing orentation
Initiate a Gfxwrapper class <br>
```C++
VGA3BitI vga;
GfxWrapper<VGA3BitI gfx(vga, 640, 480);
```
Then set the orientation of the class wherever necessary:<br>
```C++
gfx.orientation = 90;
```
There are 6 types of oreintation that is supported:<br>
* object.orientation = 0 -> (default)actual orientation
* object.orientation = 90 -> 90 degree clockwise rotation relative to actual
* object.orientation = 180 -> 180 degree clockwise rotation relative to actual
* object.orientation = 270 -> 270 degree clockwise rotation relative to actual
* object.orientation = -1 -> flip horizontally
* object.orientation = -2 -> flip vertically
### Notes
* Rotating the display causes your horizontal and vertical resolution to change. For example: if you have default resolution of 640 by 480 then rotating 90 degree clockwise will change your resolution to 480 by 640. So you have to keep it in mind while displaying rotated graphics. please refer to the [example](https://github.com/AsifKhan991/Customized-GFXwrapper-library-for-Bitluni-ESP32/blob/main/GFXwrapper_oreintation_example.ino) sketch to understand how it works.
* It is possible to set individual rotation for individual graphics objects in the display. For this just simply set the desired orientation before displaying the graphics and then just revert to previous orientation. For exaample:
```C++
gfx.orientation = 270; //setting desired orientation before displaying
gfx.setCursor(320 , 240);
gfx.print("individual rotation"); //displaying sometihng (can be text or graphics)
gfx.orientation = 0; // revert baack to original orientation
```
* Although it is not implemented, tweaking a bit in the library will enable rotating to any arbitrary angles (i.e: 15, 129, 287 etc). The functionality lies in the drawPixel() function of the Gfxwrapper class.
### How the example sketch looks in display
![alt text](https://github.com/AsifKhan991/Customized-GFXwrapper-library-for-Bitluni-ESP32/blob/main/IMG_20220807_180029.jpg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Date : 7/8/2022
* Author: Md. Asifuzzaman Khan
* Github: https://github.com/AsifKhan991/Customized-GFXwrapper-library-for-Bitluni-ESP32
*
* This example shows how the display orientation can be changed using Bitluni's ESP32 VGA library by
* the "object.orietnation" vairable of Gfxwrapper class.
*
* Tested on ESP32 DevKit-V1 & DevKitC-V4
*
* There are 4 modes total:
* object.orientation = 0 -> (default)actual orientation
* object.orientation = 90 -> 90 degree clockwise rotation relative to actual
* object.orientation = 180 -> 180 degree clockwise rotation relative to actual
* object.orientation = 270 -> 270 degree clockwise rotation relative to actual
* object.orientation = -1 -> flip horizontally
* object.orientation = -2 -> flip vertically
*
* Credit goes to bitluni for creating such an amazing library.
* Project page: https://github.com/bitluni/ESP32Lib
*/
#include <ESP32Lib.h>
#include <GfxWrapper.h>
#include <Fonts/FreeSansBold12pt7b.h>

const int redPin = 14;
const int greenPin = 12;
const int bluePin = 27;
const int hsyncPin = 32;
const int vsyncPin = 33;
VGA3BitI vga;
GfxWrapper<VGA3BitI> gfx(vga, 640, 480);

void setup() {
vga.init(vga.MODE640x480, redPin, greenPin, bluePin, hsyncPin, vsyncPin); // initiating in 640 by 480 resolution
gfx.setTextColor(0x0F00);
gfx.setFont(&FreeSansBold12pt7b);
}

void loop() {
gfx.orientation = 0; //Not necessary to declare the default orientation
gfx.setCursor(80 , 40);
gfx.print("Rotation example");
gfx.orientation = 90;
gfx.setCursor(80 , 40);
gfx.print("Rotation example");
gfx.orientation = 180;
gfx.setCursor(80 , 40);
gfx.print("Rotation example");
gfx.orientation = 270;
gfx.setCursor(80 , 40);
gfx.print("Rotation example");

gfx.orientation = 0;
gfx.setCursor(325 , 150);
gfx.print("Horizontal flip");
gfx.orientation = -1;
gfx.setCursor(325 , 150);
gfx.print("Horizontal flip");

gfx.orientation = 0;
gfx.setCursor(220 , 235);
gfx.print("Vertical flip");
gfx.orientation = -2;
gfx.setCursor(220 , 235);
gfx.print("Vertical flip");

}
27 changes: 22 additions & 5 deletions src/GfxWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@
#pragma once
#include "Adafruit_GFX.h"


template<class Base>
class GfxWrapper : public Adafruit_GFX
{
{
public:
int Xres,Yres,orientation=0;
Base &base;
typedef typename Base::Color Color;
GfxWrapper(Base &vga, const int xres, const int yres)
GfxWrapper(Base &vga,const int xres, const int yres)
:base(vga),
Adafruit_GFX(xres, yres)
{
Xres=xres;
Yres=yres;
}

virtual void drawPixel(int16_t x, int16_t y, uint16_t color)
{
base.dot(x, y, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000));
{
if(orientation==0){
base.dot(x, y, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000)); // actual orientation
}else if(orientation==90){
base.dot(Xres-y, x, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000)); // 90 degree c.c rotation from actual
}else if(orientation==180){
base.dot(Xres-x, Yres-y, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000)); //180 degree c.c rotation from actual
}else if(orientation==270){
base.dot(y, Yres-x, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000)); //270 degree c.c rotation from actual
}else if(orientation==-1){
base.dot(Xres-x, y, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000)); //horizontal flip
}else if(orientation==-2){
base.dot(x, Yres-y, base.RGBA((color >> 8) & 0b11111000, (color >> 3) & 0b11111100, (color << 3) & 0b11111000)); //vertical flip
}

}
};
};