Skip to content

Commit

Permalink
Experimental: Allow commenting out DOUBLE_BUFFER and/or GRAYSCALE to …
Browse files Browse the repository at this point in the history
…remove

the relevant code.  In particular, turning off both reduces the video buffer
RAM from 336 bytes to 24.
  • Loading branch information
root committed Aug 20, 2013
1 parent 2853bbb commit 0d7daf3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
31 changes: 27 additions & 4 deletions lib/Charliplexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ struct videoPage {
* There are SHADES frames per buffer in grayscale mode (one for each brigtness)
* and twice that many to support double-buffered grayscale.
*/
#ifdef DOUBLE_BUFFER
videoPage leds[2];
#else
videoPage leds[1];
#endif

/// Determines whether the display is in single or double buffer mode
uint8_t displayMode = SINGLE_BUFFER;

/// Flag indicating that the display page should be flipped as soon as the
/// current frame is displayed
#ifdef DOUBLE_BUFFER
volatile boolean videoFlipPage = false;
#endif

/// Pointer to the buffer that is currently being displayed
videoPage* displayBuffer;
Expand Down Expand Up @@ -221,21 +227,26 @@ void LedSign::Init(uint8_t mode)

// Record whether we are in single or double buffer mode
displayMode = mode;
#ifdef DOUBLE_BUFFER
videoFlipPage = false;
#endif

// Point the display buffer to the first physical buffer
displayBuffer = &leds[0];
displayPointer = displayBuffer->pixels;

#ifdef DOUBLE_BUFFER
// If we are in single buffered mode, point the work buffer
// at the same physical buffer as the display buffer. Otherwise,
// point it at the second physical buffer.
if( displayMode & DOUBLE_BUFFER ) {
workBuffer = &leds[1];
}
else {
} else {
workBuffer = displayBuffer;
}
#else
workBuffer = displayBuffer;
#endif

// Set up the timer buffering
frontTimer = &timer[0];
Expand All @@ -246,7 +257,9 @@ void LedSign::Init(uint8_t mode)

// Clear the buffer and display it
LedSign::Clear(0);
#ifdef DOUBLE_BUFFER
LedSign::Flip(false);
#endif

// Then start the display
TCNT2 = tcnt2;
Expand All @@ -256,6 +269,7 @@ void LedSign::Init(uint8_t mode)
TIMSK |= (1<<TOIE2);
#endif

#ifdef DOUBLE_BUFFER
// If we are in double-buffer mode, wait until the display flips before we
// return
if (displayMode & DOUBLE_BUFFER)
Expand All @@ -264,11 +278,13 @@ void LedSign::Init(uint8_t mode)
delay(1);
}
}
#endif

initialized = true;
}


#ifdef DOUBLE_BUFFER
/* ----------------------------------------------------------------- */
/** Signal that the front and back buffers should be flipped
* @param blocking if true : wait for flip before returning, if false :
Expand All @@ -287,6 +303,7 @@ void LedSign::Flip(bool blocking)
}
}
}
#endif


/* ----------------------------------------------------------------- */
Expand Down Expand Up @@ -329,10 +346,14 @@ void LedSign::Vertical(int x, int set) {
*/
void LedSign::Set(uint8_t x, uint8_t y, uint8_t c)
{
#ifdef GRAYSCALE
// If we aren't in grayscale mode, just map any pin brightness to max
if (c > 0 && !(displayMode & GRAYSCALE)) {
if (c > 0 && !(displayMode & GRAYSCALE))
c = SHADES-1;
}
#else
if (c)
c = SHADES-1;
#endif

uint8_t mask = pgm_read_byte_near(&ledMap[x+y*14].mask);
uint8_t cycle = pgm_read_byte_near(&ledMap[x+y*14].cycle);
Expand Down Expand Up @@ -507,6 +528,7 @@ ISR(TIMER2_OVF_vect) {
if (cycle >= 24) {
cycle = 0;

#ifdef DOUBLE_BUFFER
// If the page should be flipped, do it here.
if (videoFlipPage && (displayMode & DOUBLE_BUFFER))
{
Expand All @@ -517,6 +539,7 @@ ISR(TIMER2_OVF_vect) {
displayBuffer = workBuffer;
workBuffer = temp;
}
#endif

if (videoFlipTimer) {
videoFlipTimer = false;
Expand Down
8 changes: 6 additions & 2 deletions lib/Charliplexing.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
#include <inttypes.h>

#define SINGLE_BUFFER 0
#define DOUBLE_BUFFER 1
#define GRAYSCALE 2
#define DOUBLE_BUFFER 1 // comment out to save memory
#define GRAYSCALE 2 // comment out to save memory

#define DISPLAY_COLS 14 // Number of columns in the display
#define DISPLAY_ROWS 9 // Number of rows in the display
#ifdef GRAYSCALE
#define SHADES 8 // Number of distinct shades to display, including black, i.e. OFF
#else
#define SHADES 2
#endif

namespace LedSign
{
Expand Down

0 comments on commit 0d7daf3

Please sign in to comment.