void Driver::write_color_table(int i,int j,RGB color) { if(j%2==0) { write_gamma_color(&buf.pixels[j*8+i],color.r,color.g,color.b); } else if (j%2!=0) { write_gamma_color(&buf.pixels[(j+1)*8-1-i],color.r,color.g,color.b); } send_buffer(fd,&buf); }
void Driver::clear_led() { for(int i=0;i<leds;i++) { write_gamma_color(&buf.pixels[i],0x00,0x00,0x00); } send_buffer(fd,&buf); }
Driver::Driver() { setup_pcf(); set_gamma(2.5,2.5,2.5); leds= 64; fd = open("/dev/spidev0.0",O_WRONLY); if(fd<0) { /* Open failed */ fprintf(stderr, "Error: SPI device open failed.\n"); exit(1); } /* Initialize SPI bus for lpd8806 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(lpd8806_init(&buf,leds)<0) { /* Memory allocation failed */ fprintf(stderr, "Insufficient memory for pixel buffer.\n"); exit(1); } /* Loop Forever */ for(int i=0;i<leds;i++) { write_gamma_color(&buf.pixels[i],0x00,0x00,0x00); } send_buffer(fd,&buf); }
int main(int argc, char *argv[]) { tcl_buffer buf; int fd; int return_value; tcl_color *p; int i; struct timeval start_time; unsigned long *change_times; unsigned long current_time; /* 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); /* Get the current time */ return_value = gettimeofday(&start_time,NULL); if(return_value==-1) { fprintf(stderr, "Error reading the current time: %s\n", strerror(errno)); exit(1); } /* Set scattered intervals for the lights to change */ /* Set all pixels to black */ change_times = (unsigned long *)malloc(leds*sizeof(unsigned long)); if(change_times==NULL) { fprintf(stderr, "Error allocating array of change times.\n"); exit(1); } for(i=0;i<leds;i++) { change_times[i] = min_interval+random()%(max_interval-min_interval); 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); /* Loop while continue_looping is true */ while(continue_looping) { current_time = microseconds_since(&start_time); for(i=0;i<leds;i++) { if(current_time>change_times[i]) { p = &buf.pixels[i]; if(p->red==0x00 && p->blue==0x00 && p->green==0x00) { write_gamma_color(p,(int)random()%256,(int)random()%256,(int)random%256); } else { write_gamma_color(p,0x00,0x00,0x00); } change_times[i]=current_time+min_interval+random()%(max_interval-min_interval); } } send_buffer(fd,&buf); } tcl_free(&buf); free(change_times); close(fd); printf("Program Terminated.\n"); return 0; }
// helper to flash some leds. Index is the start index of the leds that should flash void flash_leds(int index){ // iterate all leds int i; for( i=0 ; i<leds ; i++){ // with 4 being the amount of leds for one sign if( i >= index && i < index + 4){ // the dimmed leds because of tag scan based on index write_gamma_color(&buf.pixels[i],0,0,0); write_gamma_color(&buf.pixels[i],0,0,0); write_gamma_color(&buf.pixels[i],0,0,0); write_gamma_color(&buf.pixels[i],0,0,0); } else if ( i < 4) { // red leds write_gamma_color(&buf.pixels[i],150,0,0); write_gamma_color(&buf.pixels[i],150,0,0); write_gamma_color(&buf.pixels[i],150,0,0); write_gamma_color(&buf.pixels[i],150,0,0); } else if ( i < 8 ){ // blue leds write_gamma_color(&buf.pixels[i],0,0,150); write_gamma_color(&buf.pixels[i],0,0,150); write_gamma_color(&buf.pixels[i],0,0,150); write_gamma_color(&buf.pixels[i],0,0,150); } else if (i < 12){ // green leds write_gamma_color(&buf.pixels[i],0,150,0); write_gamma_color(&buf.pixels[i],0,150,0); write_gamma_color(&buf.pixels[i],0,150,0); write_gamma_color(&buf.pixels[i],0,150,0); } } // flush thins information send_buffer(fd,&buf); }
int main(int argc, char *argv[]) { int fd; /* SPI device file descriptor */ const int leds = 500; /* 50 LEDs in the strand */ lpd8806_buffer buf; /* Memory buffer for pixel values */ int count; /* Count of iterations (up to 3) */ int i; /* Counting Integer */ set_gamma(2.5,2.5,2.5); /* Open SPI device */ fd = open("/dev/spidev0.0",O_WRONLY); if(fd<0) { /* Open failed */ fprintf(stderr, "Error: SPI device open failed.\n"); exit(1); } /* Initialize SPI bus for lpd8806 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(lpd8806_init(&buf,leds)<0) { /* Memory allocation failed */ fprintf(stderr, "Insufficient memory for pixel buffer.\n"); exit(1); } /* Loop Forever */ while(1) { /* Do three iterations */ for(count=0;count<3;count++) { /* Write color for every pixel */ for(i=0;i<leds;i++) { if((i+count)%3==0) { /* Red pixel */ write_gamma_color(&buf.pixels[i],255,0,0); } else if((i+count)%3==1) { /* Green pixel */ write_gamma_color(&buf.pixels[i],0,255,0); } else { /* Blue pixel */ write_gamma_color(&buf.pixels[i],0,0,255); } } /* Send the data to the lpd8806 lighting strand */ if(send_buffer(fd,&buf)<0) { fprintf(stderr, "Error sending data.\n"); exit(1); } /* Sleep for 1 second */ usleep(1000000/60.0f); } } for(i=0;i<leds;i++) { write_gamma_color(&buf.pixels[i],0x00,0x00,0x00); } send_buffer(fd,&buf); /* Although the program never gets to this point, below is how to clean up */ /* Free the pixel buffer */ lpd8806_free(&buf); /* Close the SPI device */ close(fd); return 0; }
void * spi_handler(void) { lpd8806_buffer buf; int fd; int return_value; lpd8806_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)); pthread_exit(NULL); } /* 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)); pthread_exit(NULL); } /* Initialize pixel buffer */ if(lpd8806_init(&buf,leds)<0) { fprintf(stderr,"Pixel buffer initialization error: Not enough memory.\n"); pthread_exit(NULL); } /* 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); h = 0.0; printf("Entering spi loop\n"); /* Loop while continue_looping is true */ for(;;){ pthread_mutex_lock(&playback_status_lock); if(is_playing){ h+=2.0; if(h>=360.0) h=0.0; memmove(buf.pixels+1,buf.pixels,sizeof(lpd8806_color)*(leds-2)); HSVtoRGB(h,1.0,1.0,&r,&g,&b); p = &buf.pixels[0]; write_gamma_color(p,(int)(r*255.0),(int)(g*255.0),(int)floor(b*255.0)); send_buffer(fd,&buf); } else { for(i=0;i<leds;i++) { p = &buf.pixels[i]; write_gamma_color(p,0x00,0x00,0x00); } send_buffer(fd,&buf); } pthread_mutex_unlock(&playback_status_lock); usleep(10000); } for(i=0;i<leds;i++) { p = &buf.pixels[i]; write_gamma_color(p,0x00,0x00,0x00); } send_buffer(fd,&buf); lpd8806_free(&buf); close(fd); printf("lpd thread terminated.\n"); pthread_exit(NULL); }