From 2b23b391b477be839df8f7777dd1d68c316928ee Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Sun, 15 Apr 2018 22:44:48 +0200 Subject: [PATCH] Do not expose buffer to the client --- LedCube/FrameBuffer.cpp | 25 +++++++++++++++++++++++-- LedCube/FrameBuffer.h | 41 ++++++++++++++++++++++++++++++++--------- LedCube/LedCube.ino | 34 ++++++++++++++++++---------------- 3 files changed, 73 insertions(+), 27 deletions(-) diff --git a/LedCube/FrameBuffer.cpp b/LedCube/FrameBuffer.cpp index dee2e8e..157f6de 100644 --- a/LedCube/FrameBuffer.cpp +++ b/LedCube/FrameBuffer.cpp @@ -269,7 +269,7 @@ void FrameBufferSwitch(void) { #endif } -Frame *FrameBufferGetFront(void) { +static Frame *FrameBufferGetFront(void) { #if FRAMEBUFFER_POINTERS return FrameBuffer.front; #else @@ -277,7 +277,7 @@ Frame *FrameBufferGetFront(void) { #endif } -Frame *FrameBufferGetBack(void) { +static Frame *FrameBufferGetBack(void) { #if FRAMEBUFFER_POINTERS return FrameBuffer.back; #else @@ -285,10 +285,31 @@ Frame *FrameBufferGetBack(void) { #endif } +void FrameBufferReadFront(char x, char y, char z, unsigned char *val) { + *val = FrameBufferGetFront()->data[x][y][z]; +} + +void FrameBufferReadBack(char x, char y, char z, unsigned char *val) { + *val = FrameBufferGetBack()->data[x][y][z]; +} + +void FrameBufferCopy(char x1, char y1, char z1, char x2, char y2, char z2) { + FrameBufferGetBack()->data[x1][y1][z1] = + FrameBufferGetFront()->data[x2][y2][z2]; +} + void FrameBufferWrite(char x, char y, char z, unsigned char val) { FrameBufferGetBack()->data[x][y][z] = val; } +void FrameBufferSet(unsigned char val) { + memset(FrameBufferGetBack(), val, FRAME_SIZE()); +} + +void FrameBufferBlank() { + FrameBufferSet(0); +} + static void letter(char l, char brightness) { memset(FrameBufferGetBack(), 0, FRAME_SIZE()); for (char x = 0; x < MAX_X; x++) { diff --git a/LedCube/FrameBuffer.h b/LedCube/FrameBuffer.h index 39cd821..ee9ece0 100644 --- a/LedCube/FrameBuffer.h +++ b/LedCube/FrameBuffer.h @@ -68,30 +68,53 @@ extern struct FrameBuffer FrameBuffer; void FrameBufferSwitch(void); /** - * Returns the current front (visible) frame buffer + * Read the brightness ``val'' of the led at position (x,y,z), in the front + * buffer. */ -Frame *FrameBufferGetFront(void); - +void FrameBufferReadFront(char x, char y, char z, unsigned char *val); /** - * Returns the current back (invisible) frame buffer + * Read the brightness ``val'' of the led at position (x,y,z), in the back + * buffer. */ -Frame *FrameBufferGetBack(void); +void FrameBufferReadBack(char x, char y, char z, unsigned char *val); + +/** + * Copy the value from (x2, y2, z2) in the front buffer, to (x1,y1,z1) in + * the back buffer. + */ +void FrameBufferCopy(char x1, char y1, char z1, char x2, char y2, char z2); /** * Write the brightness ``val'' to the led at position (x,y,z), in the back * buffer. - * + * * In order to make the value visible, a call to FrameBufferSwitch is needed, * once the drawing of the frame is complete. */ void FrameBufferWrite(char x, char y, char z, unsigned char val); /** - * Print the string, a letter at a time, showing each for delayPerLetter time, + * Write the brightness ``val'' to the whole back buffer. + * + * In order to make the value visible, a call to FrameBufferSwitch is needed, + * once the drawing of the frame is complete. + */ +void FrameBufferSet(unsigned char val); + +/** + * Blank the Back Buffer. + * + * In order to make the value visible, a call to FrameBufferSwitch is needed, + * once the drawing of the frame is complete. + */ +void FrameBufferBlank(); + +/** + * Print the string, a letter at a time, showing each for delayPerLetter time, * with the associated brightness. - * - * This has minimal checks, only lowercase font, space and zero to terminate + * + * This has minimal checks, only lowercase font, space and zero to terminate * the string. */ void FrameBufferWriteStr(char * str, const short delayPerLetter, diff --git a/LedCube/LedCube.ino b/LedCube/LedCube.ino index 9520da6..9276822 100644 --- a/LedCube/LedCube.ino +++ b/LedCube/LedCube.ino @@ -86,13 +86,13 @@ unsigned char sinewave[] = { */ void levels(void) { for(int i = 0; i < BRIGHTNESS_MAX; i++) { - memset(FrameBufferGetBack(), i, FRAME_SIZE()); + FrameBufferSet(i); FrameBufferSwitch(); delay(100); } for(int i = BRIGHTNESS_MAX; i >= 0; i--) { - memset(FrameBufferGetBack(), i, FRAME_SIZE()); + FrameBufferSet(i); FrameBufferSwitch(); delay(100); } @@ -104,11 +104,11 @@ void levels(void) { * Mainly useful as a test of the led cube. */ void testfreq(void) { - memset(FrameBufferGetBack(), 255, FRAME_SIZE()); + FrameBufferSet(255); FrameBufferSwitch(); delay(200); - memset(FrameBufferGetBack(), 1, FRAME_SIZE()); + FrameBufferSet(1); FrameBufferSwitch(); delay(200); } @@ -117,11 +117,11 @@ void testfreq(void) { * Blinks the whole cube on and off with a period `period`. */ void blinkWholeCube(int period /* [ms] */) { - memset(FrameBufferGetBack(), 255, FRAME_SIZE()); + FrameBufferSet(255); FrameBufferSwitch(); delay(period >> 1); - memset(FrameBufferGetBack(), 0, FRAME_SIZE()); + FrameBufferBlank(); FrameBufferSwitch(); delay(period >> 1); } @@ -131,7 +131,7 @@ void blinkWholeCube(int period /* [ms] */) { */ void sine(int period /* [ms] */) { for(short int i = 0; i < BRIGHTNESS_MAX; i++) { - memset(FrameBufferGetBack()->data, sinewave[i], FRAME_SIZE()); + FrameBufferSet(sinewave[i]); FrameBufferSwitch(); delay(period / BRIGHTNESS_MAX); } @@ -143,12 +143,12 @@ void sine(int period /* [ms] */) { */ void sine2(int period /* [ms] */) { for(short int i = 0; i < BRIGHTNESS_MAX; i++) { - memset(FrameBufferGetBack(), 0, FRAME_SIZE()); + FrameBufferBlank(); for (char x = 1; x < 3; x++) { for (char y = 1; y < 3; y++) { for (char z = 1; z < 3; z++) { - FrameBufferGetBack()->data[x][y][z] = sinewave[i]; + FrameBufferWrite(x, y, z, sinewave[i]); } } } @@ -164,11 +164,11 @@ void sine2(int period /* [ms] */) { */ void sine3(int period /* [ms] */) { for(int i = 0; i < 256; i++) { - memset(FrameBufferGetBack()->data, 0, FRAME_SIZE()); + FrameBufferBlank();; for (char x = 1; x < 3; x++) { for (char y = 1; y < 3; y++) { for (char z = 1; z < 3; z++) { - FrameBufferGetBack()->data[x][y][z] = sinewave[i]; + FrameBufferWrite(x, y, z, sinewave[i]); } } } @@ -179,7 +179,7 @@ void sine3(int period /* [ms] */) { if ( ((x == 0) || (x == 3)) ||((y == 0) || (y == 3)) ||((z == 0) || (z == 3))) { - FrameBufferGetBack()->data[x][y][z] = sinewave[(i+128)%256]; + FrameBufferWrite(x, y, z, sinewave[(i+128)%256]); } } } @@ -194,14 +194,16 @@ void sine3(int period /* [ms] */) { * Random rain drpos fall to the bottom of the cube */ void randomRain() { + unsigned char val; for (char a = MAX_Z; a > 0; a--) { // animation of 4 steps, requiring computing 4 full frames for (char x = 0; x < MAX_X; x++) { for (char y = 0; y < MAX_Y; y++) { - FrameBufferGetBack()->data[x][y][3] = (random(0, 4) == 0) ? 1 : random(0, BRIGHTNESS_MAX); - FrameBufferGetBack()->data[x][y][2] = FrameBufferGetFront()->data[x][y][3]; - FrameBufferGetBack()->data[x][y][1] = FrameBufferGetFront()->data[x][y][2]; - FrameBufferGetBack()->data[x][y][0] = FrameBufferGetFront()->data[x][y][1]; + FrameBufferWrite(x, y, 3, + (random(0, 4) == 0) ? 1 : random(0, BRIGHTNESS_MAX)); + FrameBufferCopy(x, y, 2, x, y, 3); + FrameBufferCopy(x, y, 1, x, y, 2); + FrameBufferCopy(x, y, 0, x, y, 1); } }