Commits

Christopher De Vries committed 20a3d25

Working on a rainbow thing like you often see. Will have to make decisions about what to include in 1.1

  • Participants
  • Parent commits 931e52f

Comments (0)

Files changed (3)

 CFLAGS = -O3
 CC = gcc
-BUNDLE = Makefile README.txt LICENSE.txt tclled.h tclled.c tcl_speedtest.c blink_test.c simple_example.c blink_hsv.c
+BUNDLE = Makefile README.txt LICENSE.txt tclled.h tclled.c tcl_speedtest.c blink_test.c simple_example.c blink_hsv.c rainbows.c
 VERSION = 1.1
 ARCHIVE = elinux-tcl
 LIBS = -lm
 
-all: tcl_speedtest blink_test simple_example blink_hsv
+all: tcl_speedtest blink_test simple_example blink_hsv rainbows
 
 archive: $(BUNDLE)
 	mkdir $(ARCHIVE)-$(VERSION)
 simple_example: simple_example.o tclled.o
 	$(CC) $(CFLAGS) $(LIBS) -o $@ $^
 
+rainbows.o: tclled.h rainbows.c
+
+rainbows: rainbows.o tclled.o
+	$(CC) $(CFLAGS) $(LIBS) -o $@ $^
+
 tclled.o: tclled.h tclled.c
+/*
+ * blink_test.c
+ *
+ * Copyright 2012 Christopher De Vries
+ * This program is distributed under the Artistic License 2.0, a copy of which
+ * is included in the file LICENSE.txt
+ */
+#include "tclled.h"
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <math.h>
+
+static const char *device = "/dev/spidev2.0";
+static const int leds = 500;
+static int continue_looping;
+
+void stop_program(int sig);
+void HSVtoRGB(double h, double s, double v, double *r, double *g, double *b);
+
+int main(int argc, char *argv[]) {
+  tcl_buffer buf;
+  int fd;
+  int return_value;
+  tcl_color *p;
+  int i;
+  double h, r, g, b;
+
+  /* Open the device file using Low-Level IO */
+  fd = open(device,O_WRONLY);
+  if(fd<0) {
+    fprintf(stderr,"Error %d: %s\n",errno,strerror(errno));
+    exit(1);
+  }
+
+  /* Initialize the SPI bus for Total Control Lighting */
+  return_value = spi_init(fd);
+  if(return_value==-1) {
+    fprintf(stderr,"SPI initialization error %d: %s\n",errno, strerror(errno));
+    exit(1);
+  }
+
+  /* Initialize pixel buffer */
+  if(tcl_init(&buf,leds)<0) {
+    fprintf(stderr,"Pixel buffer initialization error: Not enough memory.\n");
+    exit(1);
+  }
+
+  /* Set the gamma correction factors for each color */
+  set_gamma(2.2,2.2,2.2);
+
+  /* Blank the pixels */
+  for(i=0;i<leds;i++) {
+    write_gamma_color(&buf.pixels[i],0x00,0x00,0x00);
+  }
+
+  send_buffer(fd,&buf);
+
+  /* Prepare to receive ctrl-c to stop looping */
+  continue_looping = 1;
+  signal(SIGINT,stop_program);
+  h = 0.0;
+
+  /* Loop while continue_looping is true */
+  while(continue_looping) {
+    h+=0.1;
+    if(h>=360.0) h=0.0;
+    memmove(buf.pixels+1,buf.pixels,sizeof(tcl_pixel)*(leds-2));
+    HSVtoRGB(h,1.0,1.0,&r,&g,&b);
+    write_gamma_color(p,(int)(r*255.0),(int)(g*255.0),(int)floor(b*255.0));
+    send_buffer(fd,&buf);
+  }
+
+  tcl_free(&buf);
+  close(fd);
+  printf("Program Terminated.\n");
+
+  return 0;
+}
+
+void stop_program(int sig) {
+  /* Ignore the signal */
+  signal(sig,SIG_IGN);
+
+  /* stop the looping */
+  continue_looping = 0;
+
+  /* Put the ctrl-c to default action in case something goes wrong */
+  signal(sig,SIG_DFL);
+}
+
+/* Convert hsv values (0<=h<360, 0<=s<=1, 0<=v<=1) to rgb values (0<=r<=1, etc) */
+void HSVtoRGB(double h, double s, double v, double *r, double *g, double *b) {
+  int i;
+	double f, p, q, t;
+
+	if( s < 1.0e-6 ) {
+		/* grey */
+		*r = *g = *b = v;
+		return;
+	}
+
+	h /= 60.0;			        /* Divide into 6 regions (0-5) */
+	i = (int)floor( h );
+	f = h - (double)i;			/* fractional part of h */
+	p = v * ( 1.0 - s );
+	q = v * ( 1.0 - s * f );
+	t = v * ( 1.0 - s * ( 1.0 - f ) );
+
+	switch( i ) {
+		case 0:
+		  *r = v;
+			*g = t;
+			*b = p;
+			break;
+		case 1:
+			*r = q;
+			*g = v;
+			*b = p;
+			break;
+		case 2:
+			*r = p;
+			*g = v;
+			*b = t;
+			break;
+		case 3:
+			*r = p;
+			*g = q;
+			*b = v;
+			break;
+		case 4:
+			*r = t;
+			*g = p;
+			*b = v;
+			break;
+		default:		// case 5:
+			*r = v;
+			*g = p;
+			*b = q;
+			break;
+	}
+}