Skip to content

Commit

Permalink
make LEDs round
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb committed Nov 22, 2011
1 parent a826543 commit b40d2bc
Show file tree
Hide file tree
Showing 15 changed files with 2,689 additions and 6 deletions.
4 changes: 4 additions & 0 deletions firmware/sim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ BINARIES=$(SOURCES:../apps/%.c=%)
LIB_SOURCES=$(wildcard ../libs/*.c)
LIB_HEADERS=$(wildcard ../libs/*.h)

LIB_SOURCES+= sdl_draw/SDL_draw.c
LIB_HEADERS+= sdl_draw/SDL_draw.h


FLAGS=--std=gnu99 -Wall

ifeq ($(UNAME), Darwin)
Expand Down
23 changes: 17 additions & 6 deletions firmware/sim/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include <SDL/SDL.h>

#include "main.h"
#include "sdl_draw/SDL_draw.h"

#define ZOOM 50

int leds[LED_HEIGHT][LED_WIDTH];
int interval;


uint16_t volatile current_adc_value = 900;

tick_fun tick_fp;
Expand Down Expand Up @@ -67,6 +69,20 @@ int main(int argc, char *argv[]) {
SDL_Surface* screen = SDL_SetVideoMode(LED_WIDTH * ZOOM, LED_HEIGHT * ZOOM,
32, SDL_SWSURFACE | SDL_DOUBLEBUF);

SDL_Rect rect = { 0, 0, LED_WIDTH*ZOOM, LED_HEIGHT*ZOOM };
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0x20,0x20,0x60));

unsigned int color[] = {
SDL_MapRGB(screen->format, 0x57,0x10,0x10),
SDL_MapRGB(screen->format, 0x6f,0x20,0x20),
SDL_MapRGB(screen->format, 0x87,0x30,0x30),
SDL_MapRGB(screen->format, 0x9f,0x40,0x40),
SDL_MapRGB(screen->format, 0xb7,0x50,0x50),
SDL_MapRGB(screen->format, 0xcf,0x60,0x60),
SDL_MapRGB(screen->format, 0xe7,0x70,0x70),
SDL_MapRGB(screen->format, 0xff,0x80,0x80)
};

int running = 1;
while(running) {
SDL_Event ev;
Expand All @@ -92,15 +108,10 @@ int main(int argc, char *argv[]) {

running &= !tick_fp();

const unsigned int color[] = {
// 0x000000, 0x7f0000, 0xcf0000, 0xff3f3f,
0x000000, 0x240000, 0x480000,0x6c0000,0x900000,0xb40000,0xd90000, 0xff3f3f,
};
int x, y;
for(x = 0; x < LED_WIDTH; x++) {
for(y = 0; y < LED_HEIGHT; y++) {
SDL_Rect rect = { x * ZOOM, (LED_HEIGHT - y - 1) * ZOOM, ZOOM, ZOOM };
SDL_FillRect(screen, &rect, color[leds[y][x]]);
Draw_FillCircle(screen, ZOOM*x+(ZOOM/2),ZOOM*(LED_HEIGHT - y - 1)+(ZOOM/2), (ZOOM*0.4), color[leds[y][x]]);
}
}
SDL_Flip(screen);
Expand Down
109 changes: 109 additions & 0 deletions firmware/sim/sdl_draw/Draw_Circle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*!
\file Draw_Circle.c
\author Mario Palomo <mpalomo@ihman.com>
\author Jose M. de la Huerga Fernández
\author Pepe González Mora
\date 05-2002
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#define SDL_DRAW_PUTPIXEL_BPP(A, B, C) \
*(A(B(Uint8*)super->pixels + (y0+y)*super->pitch + \
(x0+x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0-y)*super->pitch + \
(x0+x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0+y)*super->pitch + \
(x0-x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0-y)*super->pitch + \
(x0-x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0+x)*super->pitch + \
(x0+y)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0-x)*super->pitch + \
(x0+y)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0+x)*super->pitch + \
(x0-y)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0-x)*super->pitch + \
(x0-y)*SDL_DRAW_BPP)) = C;

#if SDL_DRAW_BPP == 1
#define SDL_DRAW_PUTPIXEL SDL_DRAW_PUTPIXEL_BPP(0+,0+,color)

#elif SDL_DRAW_BPP == 2
#define SDL_DRAW_PUTPIXEL SDL_DRAW_PUTPIXEL_BPP((Uint16*),0+,color)

#elif SDL_DRAW_BPP == 3
#define SDL_DRAW_PUTPIXEL \
SDL_DRAW_PUTPIXEL_BPP(0+,1+,colorbyte1) \
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { \
SDL_DRAW_PUTPIXEL_BPP(0+,0+,colorbyte2) \
SDL_DRAW_PUTPIXEL_BPP(0+,2+,colorbyte0) \
}else{ \
SDL_DRAW_PUTPIXEL_BPP(0+,0+,colorbyte0) \
SDL_DRAW_PUTPIXEL_BPP(0+,2+,colorbyte2) \
}

#elif SDL_DRAW_BPP == 4
#define SDL_DRAW_PUTPIXEL SDL_DRAW_PUTPIXEL_BPP((Uint32*),0+,color)

#endif /*SDL_DRAW_BPP*/


void SDL_DRAWFUNCTION(SDL_Surface *super,
Sint16 x0, Sint16 y0, Uint16 r,
Uint32 color)
{
#if SDL_DRAW_BPP == 3
Uint8 colorbyte0 = (Uint8) (color & 0xff);
Uint8 colorbyte1 = (Uint8) ((color >> 8) & 0xff);
Uint8 colorbyte2 = (Uint8) ((color >> 16) & 0xff);
#endif

Sint16 x = 0;
Sint16 y = r-1; /*radius zero == draw nothing*/
Sint16 d = 3 - 2*r;
Sint16 diagonalInc = 10 - 4*r;
Sint16 rightInc = 6;

/* Lock surface */
if (SDL_MUSTLOCK(super)) {
if (SDL_LockSurface(super) < 0) { return; }
}

while (x <= y) {

SDL_DRAW_PUTPIXEL

if (d >= 0) {
d += diagonalInc;
diagonalInc += 8;
y -= 1;
} else {
d += rightInc;
diagonalInc += 4;
}
rightInc += 4;
x += 1;
}

/* Unlock surface */
if (SDL_MUSTLOCK(super)) { SDL_UnlockSurface(super); }

}/*Draw_Circle*/


#undef SDL_DRAW_PUTPIXEL
#undef SDL_DRAW_PUTPIXEL_BPP

145 changes: 145 additions & 0 deletions firmware/sim/sdl_draw/Draw_Ellipse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*!
\file Draw_Ellipse.c
\author Mario Palomo <mpalomo@ihman.com>
\author Jose M. de la Huerga Fernández
\author Pepe González Mora
\date 05-2002
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#define SDL_DRAW_PUTPIXEL_BPP(A, B, C) \
*(A(B(Uint8*)super->pixels + (y0+y)*super->pitch + \
(x0+x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0-y)*super->pitch + \
(x0+x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0+y)*super->pitch + \
(x0-x)*SDL_DRAW_BPP)) = C; \
*(A(B(Uint8*)super->pixels + (y0-y)*super->pitch + \
(x0-x)*SDL_DRAW_BPP)) = C;


#if SDL_DRAW_BPP == 1
#define SDL_DRAW_PUTPIXEL SDL_DRAW_PUTPIXEL_BPP(0+,0+,color)

#elif SDL_DRAW_BPP == 2
#define SDL_DRAW_PUTPIXEL SDL_DRAW_PUTPIXEL_BPP((Uint16*),0+,color)

#elif SDL_DRAW_BPP == 3
#define SDL_DRAW_PUTPIXEL \
SDL_DRAW_PUTPIXEL_BPP(0+,1+,colorbyte1) \
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { \
SDL_DRAW_PUTPIXEL_BPP(0+,0+,colorbyte2) \
SDL_DRAW_PUTPIXEL_BPP(0+,2+,colorbyte0) \
}else{ \
SDL_DRAW_PUTPIXEL_BPP(0+,0+,colorbyte0) \
SDL_DRAW_PUTPIXEL_BPP(0+,2+,colorbyte2) \
}

#elif SDL_DRAW_BPP == 4
#define SDL_DRAW_PUTPIXEL SDL_DRAW_PUTPIXEL_BPP((Uint32*),0+,color)

#endif /*SDL_DRAW_BPP*/


void SDL_DRAWFUNCTION(SDL_Surface *super,
Sint16 x0, Sint16 y0,
Uint16 Xradius, Uint16 Yradius,
Uint32 color)
{
Sint32 x, y;
Sint32 Xchange, Ychange;
Sint32 EllipseError;
Sint32 TwoASquare, TwoBSquare;
Sint32 StoppingX, StoppingY;

#if SDL_DRAW_BPP == 3
Uint8 colorbyte0 = (Uint8) (color & 0xff);
Uint8 colorbyte1 = (Uint8) ((color >> 8) & 0xff);
Uint8 colorbyte2 = (Uint8) ((color >> 16) & 0xff);
#endif

TwoASquare = 2*Xradius*Xradius;
TwoBSquare = 2*Yradius*Yradius;

/*1st set of points*/
x = Xradius-1; /*radius zero == draw nothing*/
y = 0;

Xchange = Yradius*Yradius*(1-2*Xradius);
Ychange = Xradius*Xradius;

EllipseError = 0;

StoppingX = TwoBSquare*Xradius;
StoppingY = 0;

/* Lock surface */
if (SDL_MUSTLOCK(super)) {
if (SDL_LockSurface(super) < 0) { return; }
}

/*Plot four ellipse points by iteration*/
while (StoppingX > StoppingY) {

SDL_DRAW_PUTPIXEL

++y;
StoppingY += TwoASquare;
EllipseError += Ychange;
Ychange += TwoASquare;
if (( 2*EllipseError + Xchange) > 0) {
--x;
StoppingX -= TwoBSquare;
EllipseError += Xchange;
Xchange += TwoBSquare;
}
}/*while*/

/*2nd set of points*/
x = 0;
y = Yradius-1; /*radius zero == draw nothing*/
Xchange = Yradius*Yradius;
Ychange = Xradius*Xradius*(1-2*Yradius);
EllipseError = 0;
StoppingX = 0;
StoppingY = TwoASquare*Yradius;

/*Plot four ellipse points by iteration*/
while (StoppingX < StoppingY) {

SDL_DRAW_PUTPIXEL

++x;
StoppingX += TwoBSquare;
EllipseError += Xchange;
Xchange += TwoBSquare;
if ((2*EllipseError + Ychange) > 0) {
--y;
StoppingY -= TwoASquare;
EllipseError += Ychange;
Ychange += TwoASquare;
}
}

/* Unlock surface */
if (SDL_MUSTLOCK(super)) { SDL_UnlockSurface(super); }

}/*Draw_Ellipse*/


#undef SDL_DRAW_PUTPIXEL
#undef SDL_DRAW_PUTPIXEL_BPP

Loading

0 comments on commit b40d2bc

Please sign in to comment.