Skip to content

Commit

Permalink
added fill/draw rect
Browse files Browse the repository at this point in the history
  • Loading branch information
damian authored and damian committed Jul 14, 2011
1 parent 3aa8ff1 commit 17839f8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 79 deletions.
102 changes: 57 additions & 45 deletions src/WatterottScreen.cpp
Expand Up @@ -54,22 +54,18 @@ const static uint8_t LCD_REGISTER = 0x70;
WatterottScreen::WatterottScreen( )
: fd(-1)
{
shared_working_buf = malloc( 320*240*2 );
}

WatterottScreen::~WatterottScreen()
{
free( shared_working_buf );
}

void WatterottScreen::display888( int x0, int y0, int w, int h, uint8_t* pixels, int offset, int stride )
{
static uint8_t* pixels565 = NULL;
static size_t pixels565_size = 0;

if ( w*h*2 > pixels565_size )
{
if ( pixels565==NULL )
pixels565 = (uint8_t*)malloc( w*h*2 );
else
pixels565 = (uint8_t*)realloc( pixels565, w*h*2 );
pixels565_size = w*h*2;
}

uint8_t* pixels565 = shared_working_buf;

// convert 888 to 565
uint8_t* source = pixels+offset;
int jump = 0;
Expand Down Expand Up @@ -547,43 +543,59 @@ bool WatterottScreen::setup( const char* device, uint8_t _mode, uint32_t _speed
return true;
}

int misc()


void WatterottScreen::drawRect(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, ofColor color)
{
fillRect(x0, y0, x0, y1, color);
fillRect(x0, y1, x1, y1, color);
fillRect(x1, y0, x1, y1, color);
fillRect(x0, y0, x1, y0, color);

return;
}


void WatterottScreen::fillRect(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, ofColor colour_of )
{
int w = 160;
int h = 120;
uint16_t stuff[w*h];
uint16_t colour = 0;
float r = 0.0f;
float g = 0.5f;
float b = 1.0f;
float t = 0.0f;
while( true )
uint16_t size;
uint16_t tmp;

if(x0 > x1)
{
float sint = sinf(t);
for ( int i=0; i<h; i+=2 )
{
int index = i*w;
for ( int j=0; j<w; j+=2 )
{

float r = 0.5f+0.25f*(sinf( t+float(i*j+i)/40019 )+cosf( float(j)/(256*sint)) );
//float g = 0.5f+0.5f*cosf( -t+(j+i*i)/1539 );
//float b = 0.5f+0.5f*sinf( t+(j+t+i)/1600 );
/*float r = (float)i/320;
float g = (float)j/240;
float b = 1.0f-(r+g)/2.0f;*/
uint16_t rgb = WatterottScreen::rgb565( r, r, r );
stuff[index+j] = rgb;
stuff[index+j+1] = rgb;
stuff[index+w+j] = rgb;
stuff[index+w+j+1] = rgb;
}
}
//screen.display( 0, 0, w, h, (uint8_t*)stuff );
t+=0.1f;
tmp = x0;
x0 = x1;
x1 = tmp;
}
if(y0 > y1)
{
tmp = y0;
y0 = y1;
y1 = tmp;
}

if((x1 >= lcd_width) ||
(y1 >= lcd_height))
{
return;
}

return 0;
// convert colour to 565
uint16_t colour = rgb565( (color_of.r)/255, float(color_of.g)/255, float(color_of.b)/255 );
uint16_t* pixels565 = (uint16_t*)shared_working_buf;

int width = (x1-x0);
int height = (y1-y0);
int count = width*height;
uint16_t* cur = pixels565;
for ( int i=0; i<count; i++ )
{
*cur++ = colour;
}
display565( x0, y0, width, height, pixels565 );

return;
}


#endif
78 changes: 44 additions & 34 deletions src/WatterottScreen.h
Expand Up @@ -6,44 +6,54 @@
#include <linux/types.h>
#include <stdint.h>
#include <linux/spi/spidev.h>
#include "ofMain.h"

class WatterottScreen

{
public:
WatterottScreen();

/// open ioctl to device (eg /dev/spidev3.1), with given mode
/// (eg SPI_CPHA | SPI_CPOL); communicate at given speed (bps)
bool setup( const char* device, uint8_t mode=0, uint32_t speed=500000 );

/// display pixels at x0, y0 on the screen. pixels is 5-6-5 format as returned by rgb565()
void display565( int x0, int y0, int w, int h, uint8_t* pixels );

/// as display565 but pixels are (grayscale, 8 bit);
/// offset and stride allow a sub-rect of pixels* with different w/h to be used as source:
/// set offset to (sub_y*pixels_width + sub_x), stride to pixels_width
void display8( int x0, int y0, int w, int h, uint8_t* pixels, int offset=0, int stride=-1 );
/// as display8 but pixels are RGB 24 bit and offset and stride are given in bytes (ie *3)
void display888( int x0, int y0, int w, int h, uint8_t* pixels, int offset=0, int stride=-1 );

int getWidth() { return 320; }
int getHeight() { return 240; }

/// return a 5-6-5 uint16_t for the colour r,g,b ([0..1])
static uint16_t rgb565( float r, float g, float b );
private:
void reset();

// tx (and rx if specified) should be count bytes long
int writeSPI( uint8_t* tx, int count, bool toggleCSOnEnd=true, uint8_t* rx=NULL, uint32_t override_speed=0 );
int readCommand( uint8_t cmd, uint8_t &result );
int writeCommand( uint8_t cmd, uint8_t arg );
int writeData( uint8_t* data, int count );

int fd;
uint8_t mode;
uint32_t speed;
public:
WatterottScreen();
~WatterottScreen();

/// open ioctl to device (eg /dev/spidev3.1), with given mode
/// (eg SPI_CPHA | SPI_CPOL); communicate at given speed (bps)
bool setup( const char* device, uint8_t mode=0, uint32_t speed=500000 );

/// display pixels at x0, y0 on the screen. pixels is 5-6-5 format as returned by rgb565()
void display565( int x0, int y0, int w, int h, uint8_t* pixels );

/// as display565 but pixels are (grayscale, 8 bit);
/// offset and stride allow a sub-rect of pixels* with different w/h to be used as source:
/// set offset to (sub_y*pixels_width + sub_x), stride to pixels_width
void display8( int x0, int y0, int w, int h, uint8_t* pixels, int offset=0, int stride=-1 );
/// as display8 but pixels are RGB 24 bit and offset and stride are given in bytes (ie *3)
void display888( int x0, int y0, int w, int h, uint8_t* pixels, int offset=0, int stride=-1 );

int getWidth() { return 320; }
int getHeight() { return 240; }

/// return a 5-6-5 uint16_t for the colour r,g,b ([0..1])
static uint16_t rgb565( float r, float g, float b );


/// drawing code
void drawRect( int x0, int y0, int x1, int y1, ofColor colour );
void fillRect( int x0, int y0, int x1, int y1, ofColor colour );

private:
void reset();

// tx (and rx if specified) should be count bytes long
int writeSPI( uint8_t* tx, int count, bool toggleCSOnEnd=true, uint8_t* rx=NULL, uint32_t override_speed=0 );
int readCommand( uint8_t cmd, uint8_t &result );
int writeCommand( uint8_t cmd, uint8_t arg );
int writeData( uint8_t* data, int count );

int fd;
uint8_t mode;
uint32_t speed;

unsigned char* shared_working_buf; // 320*240*2 bytes
};


Expand Down

0 comments on commit 17839f8

Please sign in to comment.