Skip to content

Commit

Permalink
Added GL plot to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
TheChuckster committed Apr 21, 2012
1 parent a1f4dfb commit 5d73149
Show file tree
Hide file tree
Showing 17 changed files with 2,020 additions and 0 deletions.
28 changes: 28 additions & 0 deletions gl_plot/Makefile
@@ -0,0 +1,28 @@
CXX = g++

CFLAGS := -g -I/usr/include/SDL -O2 -pipe -Wall -ansi
LDFLAGS := -lrfftw -lfftw -lm -lGLEW -lGL -lGLU -lSDL_image -lSDL_ttf `sdl-config --cflags --libs`

PROGRAM = gl_plot

SRC=\
serial.cpp\
font.cpp\
main.cpp\
debug.cpp\
pong.cpp\
p300.cpp\

OBJ = $(SRC:%.cpp=%.o)

.cpp.o :
$(CXX) -c $< $(CFLAGS) -o $@

all: $(PROGRAM)

$(PROGRAM): $(OBJ)
$(CXX) -o $(PROGRAM) $(OBJ) $(LDFLAGS)

.PHONY : clean
clean:
rm -f $(OBJ)
18 changes: 18 additions & 0 deletions gl_plot/config.h
@@ -0,0 +1,18 @@
#ifndef CONFIG_H_
#define CONFIG_H_

#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define SCREEN_BPP 16

#define ADC_RESOLUTION 1024
#define FFT_SCALE_FACTOR (1024*1024*10)
#define X_SIZE 1024

#define TRUE 1
#define FALSE 0

#define LOG_FILENAME "eeg_log.csv"

#endif

140 changes: 140 additions & 0 deletions gl_plot/debug.cpp
@@ -0,0 +1,140 @@
#include "debug.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;

ofstream log_file;

#define BPERL 16 // byte/line for dump

bool FileExists(std::string filename)
{
std::fstream fin;
fin.open(filename.c_str(), std::ios::in);

if(fin.is_open())
{
fin.close();
return true;
}

fin.close();
return false;
}

void OpenLog()
{
string filename = "";

int i=0;
bool taken=true;

do {
i++;
filename = format("logs/log%d.txt", i);
if (FileExists(filename))
taken = true;
else
taken = false;
} while(taken == true);

printf("Using log file %s ...\n", filename.c_str());
log_file.open(filename.c_str(), ofstream::out | ofstream::trunc);
}

void CloseLog()
{
log_file.close();
printf("Log file closed.\n");
}

void log_out(const char* fmt ...)
{
va_list argList;
va_start(argList, fmt);
std::string result = vformat(fmt, argList);
va_end(argList);

char timebuf[52];
long timeval;

time(&timeval);
strftime(timebuf,32,"%I:%M:%S",localtime(&timeval));

cout << "[" << timebuf << "] " << result;

if (log_file.is_open()) log_file << "[" << timebuf << "] " << result;

return;
}

std::string format(const char* fmt ...)
{
va_list argList;
va_start(argList, fmt);
std::string result = vformat(fmt, argList);
va_end(argList);

return result;
}

std::string vformat(const char *fmt, va_list argPtr)
{
const int maxSize = 1000000;
const int bufSize = 161;
char stackBuffer[bufSize];

int attemptedSize = bufSize - 1;

int numChars = vsnprintf(stackBuffer, attemptedSize, fmt, argPtr);

if (numChars >= 0)
return std::string(stackBuffer); // Got it on the first try.

char* heapBuffer = NULL; // Now use the heap.

while ((numChars == -1) && (attemptedSize < maxSize))
{
attemptedSize *= 2; // Try a bigger size
heapBuffer = (char*)realloc(heapBuffer, attemptedSize + 1);
numChars = vsnprintf(heapBuffer, attemptedSize, fmt, argPtr);
}

std::string result = std::string(heapBuffer);
free(heapBuffer);

return result;
}

void dump(unsigned char *data, unsigned count)
{
unsigned byte1, byte2;

while(count != 0)
{
for(byte1 = 0; byte1 < BPERL; byte1++)
{
if(count == 0)
break;

printf("%02X ", data[byte1]);
count--;
}
printf("\t");
for(byte2 = 0; byte2 < byte1; byte2++)
{
/* if(data[byte2] < ' ')
printf(".");
else
printf("%c", data[byte2]);*/
if(data[byte2] >= '!' && data[byte2] <= '}')
printf("%c", data[byte2]);
else
printf(".");
}
printf("\n");
data += BPERL;
}
}
18 changes: 18 additions & 0 deletions gl_plot/debug.h
@@ -0,0 +1,18 @@
#ifndef DEBUG_H_
#define DEBUG_H_

#include <string>
#include <stdio.h>
#include <cstdarg>
#include <stdarg.h>

bool FileExists(std::string filename);
void OpenLog();
void CloseLog();

std::string format(const char* fmt ...);
std::string vformat(const char *fmt, va_list argPtr);
void dump(unsigned char *data, unsigned count);
void log_out(const char* fmt ...);

#endif
161 changes: 161 additions & 0 deletions gl_plot/font.cpp
@@ -0,0 +1,161 @@
#include <math.h>
#include "font.h"

#define FONT_SIZE 16
SDL_Color red = {255,0,0}, blue = {0,0,255};

void glBegin2D()
{
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0.0, (GLdouble)SCREEN_WIDTH, (GLdouble)SCREEN_HEIGHT, 0.0, 0.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void glEnd2D()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}

static int power_of_two(int input)
{
int value = 1;

while (value < input)
{
value <<= 1;
}
return value;
}

GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
{
GLuint texture;
int w, h;
SDL_Surface *image;
SDL_Rect area;
Uint32 saved_flags;
Uint8 saved_alpha;

w = power_of_two(surface->w);
h = power_of_two(surface->h);

texcoord[0] = 0.0f; /* Min X */
texcoord[1] = 0.0f; /* Min Y */
texcoord[2] = (GLfloat)surface->w / w; /* Max X */
texcoord[3] = (GLfloat)surface->h / h; /* Max Y */

image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);

if (image == NULL) return 0;

saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
saved_alpha = surface->format->alpha;
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) SDL_SetAlpha(surface, 0, 0);

/* Copy the surface into the GL texture image */
area.x = 0;
area.y = 0;
area.w = surface->w;
area.h = surface->h;
SDL_BlitSurface(surface, &area, image, &area);

/* Restore the alpha blending attributes */
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) SDL_SetAlpha(surface, saved_flags, saved_alpha);

/* Create an OpenGL texture for the image */
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image); /* No longer needed */

return texture;
}

TTF_Font *font;

void InitFont()
{
font = TTF_OpenFont("fonts/arial.ttf", FONT_SIZE);
if (font == NULL)
{
printf("TTF_OpenFont: %s\n", SDL_GetError());
return;
}
}

void FreeFont()
{
TTF_CloseFont(font);
}

void glPrint(std::string message, int x, int y, SDL_Color color)
{
int w, h;
GLfloat texcoord[4];
GLfloat texMinX, texMinY;
GLfloat texMaxX, texMaxY;
SDL_Surface *text;
GLuint fonttexture;

text = TTF_RenderText_Blended(font, message.c_str(), color);
fonttexture = SDL_GL_LoadTexture(text, texcoord);
SDL_FreeSurface(text);

w = text->w;
h = text->h;

texMinX = texcoord[0];
texMinY = texcoord[1];
texMaxX = texcoord[2];
texMaxY = texcoord[3];

glBegin2D();
glDisable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, fonttexture);
glColor3f(1.0,1.0,1.0);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(texMinX, texMinY); glVertex2i(x, y );
glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y );
glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y+h);
glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
glEnd();
glEnable(GL_LIGHTING);
glEnd2D();

glDeleteTextures(1, &fonttexture);
}

20 changes: 20 additions & 0 deletions gl_plot/font.h
@@ -0,0 +1,20 @@
#ifndef FONT_H_
#define FONT_H_

#include <string>
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "config.h"

void glPrint(std::string message, int x, int y, SDL_Color color);
void InitFont();
void FreeFont();

void glBegin2D();
void glEnd2D();

extern SDL_Color red, blue;

#endif
Binary file added gl_plot/fonts/arial.ttf
Binary file not shown.
Binary file added gl_plot/fonts/courier.ttf
Binary file not shown.

0 comments on commit 5d73149

Please sign in to comment.