Greyscale

Libraries, utilities, bootloaders...

Re: Greyscale

Postby Myndale » Fri Mar 27, 2015 5:32 am

jonnection wrote:Grayscale on timer1 = no music. Myndale can correct me if I am wrong.


Depends how badly you want it, it can be done but you have to modify the Gamebuino library to support it.

TImer1 is initialized in the Gamebuino library with code containing the following line:

Code: Select all
OCR1A = 280; // compare match register


Hopefully Rodot will correct me if I'm wrong but Timer1 has a base frequency of 31250Hz so this causes the interrupt handler to be called 31250/280 = ~111 times per second. The interrupt handler for this routine looks like this:

Code: Select all
ISR(TIMER1_COMPA_vect){ // timer compare interrupt service routine
#if(NUM_CHANNELS > 0)
   Sound::generateOutput();
#endif
}


Now if you wanted greyscale you could call the routine to flip the screen one for every 3 calls to the ISR, this would result in a frame rate of 37 which isn't too far off the ideal value of 41FPS. To do this you would need to create a variable to keep track of how many times it has been called which you would decrement, so your new handler would look like this:

Code: Select all
volatile uint8_t counter = 3;

ISR(TIMER1_COMPA_vect){ // timer compare interrupt service routine
   if (counter) counter--;
#if(NUM_CHANNELS > 0)
   Sound::generateOutput();
#endif
}


Note that counter is now being set to 0 once every 3 calls but the sound code is still being called at the regular rate. The graphics update routine can then set this value to 3 whenever it's done drawing a frame and then on the next frame wait until it drops to 0 again, i.e. something like this:

Code: Select all
void Display::update(void) {

   while (counter); // wait until it's time to flip
   counter = 3; // reset counter for the new


   // rest of the normal Display::update() code goes here
}


Unfortunately this is going to interfere with the Gamebuino code that controls how often to draw a frame so that would have to be disabled by commenting out the following:

Code: Select all
boolean Gamebuino::update() {
   /* if (((nextFrameMillis - millis()) > timePerFrame) && frameEndMicros) */   {   // <<- leave this parenthesis in


That should allow you to do both greyscale and music at the same time. It also results in the graphics update being very accurately timed (important for minimizing flicker) which can't be done if you rely on the Gamebuino library due to the way the frame rate is currently controlled. If you wanted to be more accurate then you could change the initialization of OCR1A to 254 which should result in 41FPS, although that would also change the rate at which all the sound effects are changed so you'd need to factor that in to your composition.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Greyscale

Postby Sorunome » Fri Mar 27, 2015 2:08 pm

Are there any more timers on the AVR than just timer1?
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Greyscale

Postby Myndale » Fri Mar 27, 2015 9:12 pm

Sorunome wrote:Are there any more timers on the AVR than just timer1?


As far as I'm aware Timer0 is used both to generate the PWM for the backlight and for the regular time functions in the core while Timer2 is used to generate the PWM for the speaker. The speaker PWM frequency is fixed, so In theory you could use the interrupt for your own purposes but you'd again have to modify the Gamebuino library to never set the duty cycle to 0% or 100% because AFAIK that would prevent the interrupt ever firing,
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Previous

Return to Software Development

Who is online

Users browsing this forum: No registered users and 24 guests

cron