Skip to content

Commit

Permalink
split out TCL support into a C++ class
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulski committed Nov 10, 2012
1 parent 377226b commit e41c94f
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 63 deletions.
46 changes: 46 additions & 0 deletions .cproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?>

<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.toolchain.gnu.cross.base.1475140007">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.cross.base.1475140007" moduleId="org.eclipse.cdt.core.settings" name="Default">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration buildProperties="" id="cdt.managedbuild.toolchain.gnu.cross.base.1475140007" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
<folderInfo id="cdt.managedbuild.toolchain.gnu.cross.base.1475140007.1869955441" name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.base.1523050523" name="cdt.managedbuild.toolchain.gnu.cross.base" superClass="cdt.managedbuild.toolchain.gnu.cross.base">
<option id="cdt.managedbuild.option.gnu.cross.prefix.1450687038" name="Prefix" superClass="cdt.managedbuild.option.gnu.cross.prefix"/>
<option id="cdt.managedbuild.option.gnu.cross.path.170755611" name="Path" superClass="cdt.managedbuild.option.gnu.cross.path"/>
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.1791638063" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
<builder id="cdt.managedbuild.builder.gnu.cross.2066092194" managedBuildOn="false" name="Gnu Make Builder.Default" superClass="cdt.managedbuild.builder.gnu.cross"/>
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.147196899" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler"/>
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.716879604" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler"/>
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.1256744201" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/>
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.1764035620" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker"/>
<tool id="cdt.managedbuild.tool.gnu.cross.archiver.834425787" name="Cross GCC Archiver" superClass="cdt.managedbuild.tool.gnu.cross.archiver"/>
<tool id="cdt.managedbuild.tool.gnu.cross.assembler.1099179912" name="Cross GCC Assembler" superClass="cdt.managedbuild.tool.gnu.cross.assembler"/>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="udpLights.null.952024016" name="udpLights"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
</cproject>
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

nbproject

27 changes: 27 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>udpLights</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CFLAGS = -O3
CC = gcc
CC = g++
BUNDLE = Makefile main.cpp UDPSender.cpp UDPSender.h tclled.h tclled.c
VERSION = 1.1
ARCHIVE = udplights
Expand All @@ -20,8 +20,13 @@ clean:
udplights: main.o UDPSender.o tclled.o
$(CC) $(CFLAGS) $(LIBS) -o $@ $^

udpfast: udpfast.o UDPSender.o tclled.o
$(CC) $(CFLAGS) $(LIBS) -lpthread -o $@ $^



udpfast.o: udpfast.cpp UDPSender.h

main.o: main.cpp UDPSender.h

UDPSender.o: UDPSender.cpp UDPSender.h
Expand Down
119 changes: 119 additions & 0 deletions TCLFast.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* File: TCLFast.hxx
* Author: kukulski
*
* Created on November 9, 2012, 2:58 PM
*/

#ifndef TCLFAST_HXX
#define TCLFAST_HXX

#include "tclled.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

class TCLFast {

public:

TCLFast(int wd, int ht) {
width = wd;
height = ht;

initTCL();
pixels = tcl.pixels;
set_gamma(3.0, 3.0, 3.0);

rawBuffer = new unsigned int[wd*ht];

}

~TCLFast() {
delete rawBuffer;
}

unsigned int *getRawBuffer() {
return rawBuffer;
}

size_t getBufferSize() { return width * height * 4;}

void prepPixels() {
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
prepPixel(i,j);
}

void testPattern() {
tcl_color *pixels = tcl.pixels;

for(int i = 0; i < width*height; i++) {

int row = i/width;
int col = i%width;
int quad = i /25;

tcl_color *px = getTCLPixel(row, col);

::write_gamma_color(px, row*255/4,col*255/4,quad*255/3);
}
}

void send() {
::send_buffer(spi,&tcl);
}


private:
tcl_buffer tcl;
tcl_color *pixels;
unsigned int *rawBuffer;
int width, height;
int spi;

void prepPixel(int row, int col) {
unsigned int px = getPixel(row,col);
::write_bgra_gamma(getTCLPixel(row,col),px);
}
tcl_color *getTCLPixel(int row, int col) {
int phase = row & 1;
int unwoundCol = phase?col : (width-col-1);
return &pixels[width*row+unwoundCol];
}


unsigned int getPixel(int row, int col) {
return rawBuffer[width*row+col];
}

void initTCL() {

int leds = width * height;

/* Open SPI device */
spi = open("/dev/spidev2.0",O_WRONLY);
if(spi<0) {
/* Open failed */
fprintf(stderr, "Error: SPI device open failed.\n");
exit(1);
}

/* Initialize SPI bus for TCL pixels */
if(spi_init(spi)<0) {
/* Initialization failed */
fprintf(stderr, "Unable to initialize SPI bus.\n");
exit(1);
}

/* Allocate memory for the pixel buffer and initialize it */
if(tcl_init(&tcl,leds)<0) {
/* Memory allocation failed */
fprintf(stderr, "Insufficient memory for pixel buffer.\n");
exit(1);
}

}
}; // class TCLFast
#endif /* TCLFAST_HXX */

84 changes: 24 additions & 60 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,45 @@
#include <stdio.h>
#include "UDPSender.h"
#include <stdlib.h>

#include <signal.h>
#include "TCLFast.hxx"

void prepPixels(unsigned int *raw, const tcl_buffer &tcl);
void initTclBuf(const tcl_buffer &tcl);
void interruptHandler(int param);

int main(int argc, const char * argv[])
{

int fd; /* SPI device file descriptor */
const int leds = 100; /* 50 LEDs in the strand */
tcl_buffer tclBuf;
signal(SIGINT, interruptHandler);

fprintf(stderr,"%s running, listening on port 54321\n",argv[0]);

/* Open SPI device */
fd = open("/dev/spidev2.0",O_WRONLY);
if(fd<0) {
/* Open failed */
fprintf(stderr, "Error: SPI device open failed.\n");
exit(1);
}

/* Initialize SPI bus for TCL pixels */
if(spi_init(fd)<0) {
/* Initialization failed */
fprintf(stderr, "Unable to initialize SPI bus.\n");
exit(1);
}

/* Allocate memory for the pixel buffer and initialize it */
if(tcl_init(&tclBuf,leds)<0) {
/* Memory allocation failed */
fprintf(stderr, "Insufficient memory for pixel buffer.\n");
exit(1);
}

set_gamma(3.0, 3.0, 3.0);


TCLFast tcl(10,10);

UDPListener udp(54321);
unsigned int udpBuf[100];
unsigned int *udpBuf = tcl.getRawBuffer();
size_t bufSize = tcl.getBufferSize();


tcl.testPattern();
tcl.send();

while(1) {
size_t amount = udp.listen(udpBuf,sizeof(udpBuf));
if(amount == sizeof(udpBuf)) {
prepPixels(udpBuf, tclBuf);
send_buffer(fd,&tclBuf);
size_t amount = udp.listen(udpBuf,bufSize);
if(amount == bufSize) {
tcl.prepPixels();
tcl.send();
}
}

return 0;
}


void writePixel(unsigned int *raw, tcl_color *pixels, int row, int col);
unsigned int getPixel(unsigned int *raw, int row, int col);
tcl_color *getTCLPixel(tcl_color *pixels, int row, int col);

void prepPixels( unsigned int *raw, const tcl_buffer &tcl) {

tcl_color *pixels = tcl.pixels;

for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
writePixel(raw, pixels,i,j);
}

void writePixel(unsigned int *raw, tcl_color *pixels, int row, int col) {
unsigned int px = getPixel(raw,row,col);
write_bgra_gamma(getTCLPixel(pixels,row,col),px);
}
tcl_color *getTCLPixel(tcl_color *pixels, int row, int col) {
int phase = row & 1;
int unwoundCol = phase?col : 9-col;
return &pixels[10*row+unwoundCol];
}


unsigned int getPixel( unsigned int *raw, int row, int col) {
return raw[10*row+col];
void interruptHandler(int param) {

fprintf(stderr,"quitting\n");
exit(1);

}
5 changes: 3 additions & 2 deletions tclled.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ uint8_t make_flag(uint8_t red, uint8_t green, uint8_t blue) {
return ~flag;
}

ssize_t write_all(int filedes, const void *buf, size_t size) {
ssize_t buf_len = (ssize_t)size;
ssize_t write_all(int filedes, const void *vbuf, size_t size) {
const char *buf = (const char*)vbuf;
ssize_t buf_len = (ssize_t)size;
size_t attempt = size;
ssize_t result;

Expand Down

0 comments on commit e41c94f

Please sign in to comment.