Skip to content
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

implement display rotation #18

Open
wants to merge 1 commit into
base: main
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
implement display rotation
  • Loading branch information
Jonas Grosse-Holz committed Feb 23, 2024
commit e4a483602de694d977c29539f6b25945fb9789e0
24 changes: 24 additions & 0 deletions ssd1306.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ bool ssd1306_init(ssd1306_t *p, uint16_t width, uint16_t height, uint8_t address
p->height=height;
p->pages=height/8;
p->address=address;
p->rotation=ROT_0;

p->i2c_i=i2c_instance;

Expand Down Expand Up @@ -140,13 +141,36 @@ inline void ssd1306_clear(ssd1306_t *p) {
memset(p->buffer, 0, p->bufsize);
}

inline void ssd1306_set_rotation(ssd1306_t *p, rotation_t rotation) {
p->rotation = rotation;
}

void rotate_coordinates(ssd1306_t *p, uint32_t* x, uint32_t* y) {
uint32_t tmp_x = *x;
if(p->rotation == ROT_90) {
*x = p->height - *y - 1;
*y = tmp_x;
} else if(p->rotation == ROT_180) {
*x = p->width - *x - 1;
*y = p->height - *y - 1;
} else if(p->rotation == ROT_270) {
*x = *y;
*y = p->width - tmp_x - 1;
}
// Keep coordinates as is for ROT_0
}

void ssd1306_clear_pixel(ssd1306_t *p, uint32_t x, uint32_t y) {
rotate_coordinates(p, &x, &y);

if(x>=p->width || y>=p->height) return;

p->buffer[x+p->width*(y>>3)]&=~(0x1<<(y&0x07));
}

void ssd1306_draw_pixel(ssd1306_t *p, uint32_t x, uint32_t y) {
rotate_coordinates(p, &x, &y);

if(x>=p->width || y>=p->height) return;

p->buffer[x+p->width*(y>>3)]|=0x1<<(y&0x07); // y>>3==y/8 && y&0x7==y%8
Expand Down
20 changes: 20 additions & 0 deletions ssd1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ typedef enum {
SET_CHARGE_PUMP = 0x8D
} ssd1306_command_t;

/**
* @brief defines rotations/orientations
*/
typedef enum {
ROT_0 = 0,
ROT_90 = 1,
ROT_180 = 2,
ROT_270 = 3,
} rotation_t;


/**
* @brief holds the configuration
*/
Expand All @@ -68,6 +79,7 @@ typedef struct {
bool external_vcc; /**< whether display uses external vcc */
uint8_t *buffer; /**< display buffer */
size_t bufsize; /**< buffer size */
rotation_t rotation; /**< rotation of display */
} ssd1306_t;

/**
Expand Down Expand Up @@ -143,6 +155,14 @@ void ssd1306_show(ssd1306_t *p);
*/
void ssd1306_clear(ssd1306_t *p);

/**
@brief sets the rotation for

@param[in] p : instance of display
@param[in] rotation : rotation angle
*/
void ssd1306_set_rotation(ssd1306_t *p, rotation_t rotation);

/**
@brief clear pixel on buffer

Expand Down