Page 1 of 1

setPixel Actual Code

Posted: Thu Jul 17, 2014 3:54 pm
by avrdude
Hi,
Recently I while using my OLED display, I have been noticing more and more that it doesn't seem to update quite as fast as I'd like (obviously I understand that super speedy operation is probably not possible).
And it was one specific function that I was interested in knowing what the actual code used is, and that is setPixel(), so I can run that code directly and optimise it for my purpose, to speed things up.
So that's my question: what's executed when setPixel() is called?

Thanks

Re: setPixel Actual Code

Posted: Fri Jul 18, 2014 4:13 am
by angusgr
Hi avrdude,

The code for the library is in the FTOLED.cpp & FTOLED.h files in the FTOLED directory - and also available on github. The setPixel() function itself is a thin wrapper around an inlined function called _setPixel(), which send a write command for the pixel point (literally saying "I will write one pixel from X,Y to X,Y") and then writes the colour data out.

Implementation starts here: https://github.com/freetronics/FTOLED/b ... LED.h#L253

If you post your sketch then I may be able to give you some pointers to improve the performance.


Angus

Re: setPixel Actual Code

Posted: Fri Jul 18, 2014 2:21 pm
by avrdude
Thanks for that link, I updated my version of the library to that one and found what I needed to do.
After using this code:

Code: Select all

void writeCommandFast(byte command) {
  PORTD &= ~(1 << 2);
  SPI.transfer(command);
  PORTD |= (1 << 2);
}

void sendPixelFast(int x, int y, Colour colour) {
  PORTD &= ~(1 << 7);
  writeCommandFast(0xA0);
  SPI.transfer(100);
  writeCommandFast(0x15);
  SPI.transfer(x);
  SPI.transfer(x);
  writeCommandFast(0x75);
  SPI.transfer(y);
  SPI.transfer(y);
  writeCommandFast(0x5C);
  SPI.transfer((colour.green >> 3)|(colour.red << 3));
  SPI.transfer((colour.green << 5)|(colour.blue));
  PORTD |= (1 << 7);
}
directly in my sketches, I decided to test it up against the original in the library:
Original speed: 1271012 microseconds
New speed: 390180 microseconds
These are results for setting the whole screen pixel by pixel to red, and it's visibly faster. The sketch I originally designed this fast version for runs a lot smoother.
It seems my version resulted in it taking 3 times less time, but obviously I don't know any black magic tricks that you guys don't know - this comes with drawbacks. It assumes one is using the default connections from the quickstart guide (which I think most people would be using, me included), and assumes that no screen rotation is being used.

So I then added setPixelFast() in my FTOLED library, so now I permanently have a faster version :D

All of this got me thinking, perhaps you guys (Freetronics that is) would like to add this to the source library on github, for those who wish to have a faster setPixel already built in?

avrdude

Re: setPixel Actual Code

Posted: Sun Jul 20, 2014 11:31 pm
by angusgr
Hi avrdude,

Glad you got the performance boost you were looking for.

Regarding putting the code into the library, we'll probably leave it as-is for now. Given the choice between performance and flexibility we try to aim for flexibility, within reason. If people want a heavily optimised version then they can apply these tweaks you've published, thanks for publishing them.

Cheers,


Angus

Re: setPixel Actual Code

Posted: Wed Jul 23, 2014 1:27 pm
by avrdude
Alright that's cool, I was just wondering. And as you said it is here now so people can get it if they want.
Anyway thanks for the help previously with finding the code.