From 4c9efe1fc44b427a4ce1ca8e56e0843c39d0014d Mon Sep 17 00:00:00 2001
From: Delio Brignoli <brignoli.delio@gmail.com>
Date: Sat, 16 Sep 2017 18:33:15 +0100
Subject: [PATCH] ssd1306: move display vertical and horizontal mirroring to
 OpenGL scene setup

The idea is that the display orientation related code belongs in the
board's implementation. Prior to this commit boards which mount the
display with a different orientation would end up mirroring the pixels
twice: once when reading them from the framebuffer and again in the
OpenGL scene rendering. As a sideeffect now reading pixels from the
vram array is simplified.
---
 examples/board_ssd1306/ssd1306demo.c | 31 ++++++++++++++++++----------
 examples/parts/ssd1306_glut.c        | 29 +-------------------------
 2 files changed, 21 insertions(+), 39 deletions(-)

diff --git a/examples/board_ssd1306/ssd1306demo.c b/examples/board_ssd1306/ssd1306demo.c
index 5584a42..b07a3dd 100644
--- a/examples/board_ssd1306/ssd1306demo.c
+++ b/examples/board_ssd1306/ssd1306demo.c
@@ -45,6 +45,8 @@ int window_identifier;
 avr_t * avr = NULL;
 ssd1306_t ssd1306;
 
+int win_width, win_height;
+
 static void *
 avr_run_thread (void * ignore)
 {
@@ -71,7 +73,22 @@ keyCB (unsigned char key, int x, int y)
 void
 displayCB (void)
 {
+	const uint8_t seg_remap_default = ssd1306_get_flag (
+	                &ssd1306, SSD1306_FLAG_SEGMENT_REMAP_0);
+	const uint8_t seg_comscan_default = ssd1306_get_flag (
+	                &ssd1306, SSD1306_FLAG_COM_SCAN_NORMAL);
+
 	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	// Set up projection matrix
+	glMatrixMode (GL_PROJECTION);
+	// Start with an identity matrix
+	glLoadIdentity ();
+	glOrtho (0, win_width, 0, win_height, 0, 10);
+	// Apply vertical and horizontal display mirroring
+	glScalef (seg_remap_default ? 1 : -1, seg_comscan_default ? -1 : 1, 1);
+	glTranslatef (seg_remap_default ? 0 : -win_width, seg_comscan_default ? -win_height : 0, 0);
+
 	// Select modelview matrix
 	glMatrixMode (GL_MODELVIEW);
 	glPushMatrix ();
@@ -94,22 +111,14 @@ timerCB (int i)
 int
 initGL (int w, int h, float pix_size)
 {
-	w *= pix_size;
-	h *= pix_size;
+	win_width = w * pix_size;
+	win_height = h * pix_size;
 
 	// Double buffered, RGB disp mode.
 	glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE);
-	glutInitWindowSize (w, h);
+	glutInitWindowSize (win_width, win_height);
 	window_identifier = glutCreateWindow ("SSD1306 128x64 OLED");
 
-	// Set up projection matrix
-	glMatrixMode (GL_PROJECTION);
-	// Start with an identity matrix
-	glLoadIdentity ();
-	glOrtho (0, w, 0, h, 0, 10);
-	glScalef (1, -1, 1);
-	glTranslatef (0, -1 * h, 0);
-
 	// Set window's display callback
 	glutDisplayFunc (displayCB);
 	// Set window's key callback
diff --git a/examples/parts/ssd1306_glut.c b/examples/parts/ssd1306_glut.c
index fcb62d9..988f53d 100644
--- a/examples/parts/ssd1306_glut.c
+++ b/examples/parts/ssd1306_glut.c
@@ -112,34 +112,7 @@ ssd1306_gl_put_pixel_column (uint8_t block_pixel_column, float pixel_opacity,
 static uint8_t
 ssd1306_gl_get_vram_byte (ssd1306_t *part, uint8_t page, uint8_t column)
 {
-	uint8_t seg_remap_default = ssd1306_get_flag (
-	                part, SSD1306_FLAG_SEGMENT_REMAP_0);
-	uint8_t seg_comscan_default = ssd1306_get_flag (
-	                part, SSD1306_FLAG_COM_SCAN_NORMAL);
-
-	if (seg_remap_default && seg_comscan_default)
-	{
-		// Normal display
-		return part->vram[page][column];
-	} else if (seg_remap_default && !seg_comscan_default)
-	{
-		// Normal display, mirrored from upper edge
-		return ssd1306_gl_reverse_byte (
-		                part->vram[part->pages - 1 - page][column]);
-	}
-
-	else if (!seg_remap_default && !seg_comscan_default)
-	{
-		// Upside down display
-		return ssd1306_gl_reverse_byte (
-		                part->vram[part->pages - 1 - page][part->columns - 1 - column]);
-	} else if (!seg_remap_default && seg_comscan_default)
-	{
-		// Upside down display, mirrored from upper edge
-		return part->vram[page][part->columns - 1 - column];
-	}
-
-	return 0;
+	return part->vram[page][column];
 }
 
 static void
-- 
2.39.5