Skip to content
Permalink
Browse files

fix screen drawing for aleph.

the aleph screen is mounted upside-down. when porting from aleph to teletype, half the region-drawing functions were inverted, and half were left unchanged (and unused.) now all the functions should draw correctly for both hardwares.
  • Loading branch information
catfact committed Oct 30, 2016
1 parent c18ffb9 commit 541f4d2f13de031fda37d26674c5d31a3a14843b
Showing with 61 additions and 64 deletions.
  1. +61 −64 src/screen.c
@@ -1,24 +1,14 @@
// std
/// FIXME: eliminate!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// ASF
#include "delay.h"
#include "gpio.h"
//#include "util.h"
#include "intc.h"
#include "print_funcs.h"
#include "spi.h"
//#include "conf_aleph.h"
// #include "fix.h"
#include "font.h"
// #include "global.h"
#include "screen.h"

//-----------------------------
//---- variables
// const U8 lines[CHAR_ROWS] = { 0, 8, 16, 24, 32, 40, 48, 56 };

// screen buffer
static u8 screenBuf[GRAM_BYTES];
@@ -28,10 +18,6 @@ static u32 i, j;
static u8* pScr; // movable pointer to screen buf
static u32 nb; // count of destination bytes

//static u32 pos;
// fixed-point text buffer
//static char buf[FIX_DIG_TOTAL];

static void write_command(U8 c);
static void write_command(U8 c) {
spi_selectChip(OLED_SPI, OLED_SPI_NPCS);
@@ -57,11 +43,7 @@ void screen_set_rect(u8 x, u8 y, u8 w, u8 h) {
//------------------
// al functions
void init_oled(void) {
//U32 i;
// volatile u64 delay;
// cpu_irq_disable();
Disable_global_interrupt();
// delay_ms(1);
// flip the reset pin
gpio_set_gpio_pin(OLED_RES_PIN);
delay_ms(1);
@@ -132,48 +114,60 @@ void init_oled(void) {
Enable_global_interrupt();
}

static void writeScreenBuffer(u8 x, u8 y, u8 w, u8 h) {
// set drawing region
screen_set_rect(x, y, w, h);
// select chip for data
spi_selectChip(OLED_SPI, OLED_SPI_NPCS);
// register select high for data
gpio_set_gpio_pin(OLED_DC_PIN);
// send data
for(i=0; i<(nb); i++) {
spi_write(OLED_SPI, screenBuf[i]);
}
spi_unselectChip(OLED_SPI, OLED_SPI_NPCS);
}

// draw data given target rect
// assume x-offset and width are both even!
void screen_draw_region(u8 x, u8 y, u8 w, u8 h, u8* data) {
// 1 row address = 2 horizontal pixels

// physical screen memory: 2px = 1byte
w >>= 1;
x >>= 1;
nb = w * h;

/// the screen is mounted upside down!
// copy, pack, and reverse into the top of the screen buffer
// 2 bytes input -> 1 byte output
// pScr = (u8*)screenBuf + nb - 1;
pScr = (u8*)screenBuf;// + x + (y*128) - 1;

#ifdef MOD_ALEPH // aleph screen is mounted upside down...
pScr = (u8*)screenBuf + nb - 1;
x = SCREEN_ROW_BYTES - x - w;
y = SCREEN_COL_BYTES - y - h;
// copy and pack into the screen buffer
// 2 bytes input per 1 byte output
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
*pScr = (0xf0 & ((*data) << 4) );
data++;
*pScr |= ((*data) & 0xf);
data++;
pScr--;
}
}
#else
pScr = (u8*)screenBuf;
// copy and pack into the screen buffer
// 2 bytes input per 1 byte output
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
// 2 bytes input per 1 byte output
*pScr = ((*data) & 0xf);
data++;
*pScr |= (0xf0 & ((*data) << 4) );
data++;
pScr++;
}
}

// flip the screen coordinates
// x = SCREEN_ROW_BYTES - x - w;
// y = SCREEN_COL_BYTES - y - h;

// set drawing region
screen_set_rect(x, y, w, h);
// select chip for data
spi_selectChip(OLED_SPI, OLED_SPI_NPCS);
// register select high for data
gpio_set_gpio_pin(OLED_DC_PIN);
// send data
for(i=0; i<(nb); i++) {
spi_write(OLED_SPI, screenBuf[i]);
}
spi_unselectChip(OLED_SPI, OLED_SPI_NPCS);
#endif

writeScreenBuffer(x, y, w, h);
}

// draw data at given rectangle, with starting byte offset within the region data.
@@ -194,15 +188,16 @@ void screen_draw_region_offset(u8 x, u8 y, u8 w, u8 h, u32 len, u8* data, u32 of
x >>= 1;
nb = len >> 1;

/// the screen is mounted upside down!
// copy, pack, and reverse into the top of the screen buffer
// 2 bytes input -> 1 byte output
pScr = (u8*)screenBuf + nb - 1;


#ifdef MOD_ALEPH // aleph screen is mounted upside down...
pScr = (u8*)screenBuf + nb - 1;
x = SCREEN_ROW_BYTES - x - w;
y = SCREEN_COL_BYTES - y - h;
// copy and pack into the screen buffer
// 2 bytes input per 1 byte output
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
// 2 bytes input per 1 byte output
*pScr = (0xf0 & ((*data) << 4) );
*pScr = (0xf0 & ((*data) << 4) );
data++;
if(data > dataEnd) { data = dataStart; }
*pScr |= ((*data) & 0xf);
@@ -211,22 +206,24 @@ void screen_draw_region_offset(u8 x, u8 y, u8 w, u8 h, u32 len, u8* data, u32 of
pScr--;
}
}

// flip the screen coordinates
x = SCREEN_ROW_BYTES - x - w;
y = SCREEN_COL_BYTES - y - h;

// set drawing region
screen_set_rect(x, y, w, h);
// select chip for data
spi_selectChip(OLED_SPI, OLED_SPI_NPCS);
// register select high for data
gpio_set_gpio_pin(OLED_DC_PIN);
// send data
for(i=0; i<(nb); i++) {
spi_write(OLED_SPI, screenBuf[i]);
#else
pScr = (u8*)screenBuf;
// copy and pack into the screen buffer
// 2 bytes input per 1 byte output
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
*pScr = ((*data) & 0xf);
data++;
if(data > dataEnd) { data = dataStart; }
*pScr |= (0xf0 & ((*data) << 4) );
data++;
if(data > dataEnd) { data = dataStart; }
pScr++;
}
}
spi_unselectChip(OLED_SPI, OLED_SPI_NPCS);
#endif

writeScreenBuffer(x, y, w, h);
}


0 comments on commit 541f4d2

Please sign in to comment.