void Sparkler ( uint16 runtime, int fade_amount , int num_sparkles ,char white ) { int x,j; led_color temp; // length of time to run for(x = 0; x <= runtime ; x++) { if(fade_amount ) { // Fade strip FadeStrip( StripLights_MIN_X, StripLights_MAX_X , fade_amount ); } else { StripLights_MemClear(0); } // draw in same place 8 times for ( j = 0 ; j < num_sparkles ;j++ ){ temp.c.r = calculate_sparkle( j ); if (white ) { temp.c.g = temp.c.b = temp.c.r; } else { temp.c.g = calculate_sparkle( j ); temp.c.b = calculate_sparkle( j ); } // draw a pixel StripLights_Pixel(rand()%StripLights_MAX_X, 0, temp.rgb ); } // strip ready? while( StripLights_Ready() == 0); //push current data to led strip StripLights_Trigger(1); CyDelay( 3 ); } if( fade_amount ) { // fade at end for(x = 0; x <= 200 ; x++) { // Fade strip FadeStrip( StripLights_MIN_X, StripLights_MAX_X , fade_amount ); // strip ready? while( StripLights_Ready() == 0); //push current data to led strip StripLights_Trigger(1); CyDelay( 3 ); } } }
uint32 TweenerHSV( uint16_t startx, uint16_t count, uint32 from,uint32 to,uint32 delay,int direction) { int i; int offset; led_color frgb,trgb; hsv_color src, target,result; trgb.rgb = to; frgb.rgb = from; src = rgb_to_hsv( frgb ) ; target = rgb_to_hsv( trgb ); result = src; offset = startx; for( i = 1 ; i < count; i ++ ) { result.h.h = TweenU8toU8(src.h.h, target.h.h, i); StripLights_PixelHSV(offset, 0, result ); offset += direction; if (offset < (int)StripLights_MIN_X ) offset = startx ; if (offset > startx+count ) offset = StripLights_MIN_X ; BOOT_CHECK(); if(delay) { while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( delay ); } } while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 5 ); return result.hsv; }
// snake tail chaser void Snake( uint16_t count ) { int i,x; uint32 startColor; count = count ; startColor = StripLights_RED; for(x = StripLights_MIN_X+1; x <= StripLights_MAX_X; x++) { if( x & 6) for(i = StripLights_MIN_X; i <= StripLights_MAX_X; i++) { uint32_t colour = StripLights_GetPixel(i, 0); StripLights_Pixel(i, 0, colour/2); } StripLights_Pixel(x, 0, startColor); while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 15 ); if( x % 10 == 5 ) startColor+=0x010101; BOOT_CHECK(); } }
void Tweener( uint16_t count, uint32 from) { int i,newx; led_color src, target; src.rgb = from; newx = 0; for( i = 0 ; i < count; i ++ ) { //StripLights_MemClear( 0 ); target.c.r += 5-(rand()%10); target.c.g += 5-(rand()%10); target.c.b += 5-(rand()%10); newx = TweenC1toC2Range(StripLights_COLUMNS/2,newx,src.rgb,target.rgb); newx = TweenC1toC2Range(StripLights_COLUMNS/2,newx,target.rgb,src.rgb); while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 50 ); } }
void SingleLEDPingPong( uint16_t count , uint8 fade_amount, uint32 color) { int i,x; for( i=0; i < count ; i++ ) { for(x = StripLights_MIN_X; x <= StripLights_MAX_X; x++) { if(fade_amount ) { // Fade strip FadeStrip( StripLights_MIN_X, StripLights_MAX_X , fade_amount ); } else { StripLights_MemClear(0); } StripLights_Pixel(x, 0, color); while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 5 ); BOOT_CHECK(); } for(x = StripLights_MIN_X; x <= StripLights_MAX_X; x++) { if(fade_amount ) { // Fade strip FadeStrip( StripLights_MIN_X, StripLights_MAX_X , fade_amount ); } else { StripLights_MemClear(0); } StripLights_Pixel(StripLights_MAX_X-x, 0, color); while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 5 ); BOOT_CHECK(); } } }
void FadeToColor( uint16_t startx, uint16_t count, uint32 target, uint32 delay, int direction) { int j,i; int offset,oldoffset; led_color frgb,trgb; hsv_color src,target_hsv,result; frgb.rgb = target; src = rgb_to_hsv( frgb ) ; offset = startx; for ( j = 0 ; j < 100 ; j ++ ) { oldoffset = offset; for( i = 0 ; i < count; i ++ ){ // get colour of current LED at offset trgb.rgb = StripLights_GetPixel(offset,0); // convert current led color to hsv target_hsv = rgb_to_hsv( trgb ); result = target_hsv; // tween, what we want to what it is at percentage i result.h.h = TweenU8toU8( target_hsv.h.h, src.h.h, j ); // update pixel StripLights_PixelHSV(offset, 0, result ); // handle travel direction of pixel offset += direction; if (offset < (int)StripLights_MIN_X ) offset = startx ; if (offset > startx+count ) offset = StripLights_MIN_X ; } // check bootloader mode BOOT_CHECK(); // if wants a delay, update led strip and delay if(delay) { while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( delay ); } offset = oldoffset; } }
void CandyCaneSmooth ( uint16_t count , led_color c1, led_color c2 ) { int i,x,percentage; uint8_t flip =0; uint32 t1,t2; // Candy cane if (0 ) { char buffer[256]; sprintf(buffer,"c1 = %02x %02x %02x\n",c1.c.r,c1.c.g,c1.c.b); UART_UartPutString( buffer ); sprintf(buffer,"c2 = %02x %02x %02x\n",c2.c.r,c2.c.g,c2.c.b); UART_UartPutString( buffer ); } // loop effect for this many times for( i=0; i < count ; i++ ) { for( percentage = 0 ; percentage <= 100 ; percentage+=5 ) { // calculate target colours t1 = TweenC1toC2( c1, c2, percentage ) ; t2 = TweenC1toC2( c2, c1, percentage ) ; // all strip, for every other led for(x = StripLights_MIN_X; x <= StripLights_MAX_X; x+=2) { // if flipped. draw c1,c2 otherwise c2,c1 if( flip ) { StripLights_Pixel(x, 0, t1); StripLights_Pixel(x+1, 0, t2); } else { StripLights_Pixel(x, 0, t2); StripLights_Pixel(x+1, 0, t1); } } // toggle flip flip = 1 - flip; // wait and trigger while( StripLights_Ready() == 0); StripLights_Trigger(1); // delay between transitions CyDelay( 120 ); BOOT_CHECK(); } } }
/** * @brief Sets length LEDs to colour from * * @param from colour to use * @param length number of LEDS to set * * @return none */ void StripLights_SetXToColour ( uint32 from, unsigned short length ) { int x; length = MIN(StripLights_MAX_X,length); for(x = StripLights_MIN_X; x <= length ; x++) { StripLights_Pixel(x,0,from); } StripLights_Trigger(1); }
void Icicle (uint8 redraw, uint8 length, int fade_amount ) { int x,j,i; led_color temp; // for entire length of strip, plus engough to move it off the display) for(x = StripLights_MIN_X; x <= StripLights_MAX_X + ( length * 2 ); x++) { if(fade_amount ) { // Fade strip FadeStrip( StripLights_MIN_X, StripLights_MAX_X , fade_amount ); } else { StripLights_MemClear(0); } // draw in same place 8 times for ( j = 0 ; j < redraw ;j++ ){ // length of icicle for(i=0; i < length; i++) { // caculate a randow twink based on current position in length temp.c.r = temp.c.g = temp.c.b = calculate_sparkle( i ); // draw a pixel at x+i StripLights_Pixel(x+i, 0, temp.rgb ); CyDelay( 1 ); } // strip ready? while( StripLights_Ready() == 0); //push current data to led strip StripLights_Trigger(1); CyDelay( 3 ); } // check if firmware load requested if( Boot_P0_7_Read ( ) == 0 ) CySoftwareReset(); } }
void ColorWheel( uint16_t count ) { static int i =0xAAA ,x; static uint32 color; static uint32 startColor ; if (i >= count ) { i = 0; } if ( i == 0xaaa ) { i = 0; color = 0; startColor = 0; } for( ; i < count ; i++ ) { color = startColor; for(x = StripLights_MIN_X; x <= StripLights_MAX_X; x++) { StripLights_Pixel(x, 0, getColor( color )); color++; if(color >= StripLights_COLOR_WHEEL_SIZE) color = 0; } startColor++; if(startColor >= StripLights_COLOR_WHEEL_SIZE) startColor = 0; while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 50 ); BOOT_CHECK(); } }
void Twinkle( uint16_t count ) { int i,x; led_color col; uint32 startColor; startColor = StripLights_WHITE; for(x = 0; x <= count; x++) { col.c.r = rand(); col.c.g = rand(); col.c.b = rand(); startColor = col.rgb; StripLights_Pixel(rand()%StripLights_MAX_X, 0, startColor); for(i = StripLights_MIN_X; i <= StripLights_MAX_X; i++) { col.rgb = StripLights_GetPixel(i, 0); if ( col.c.r > 0 ) col.c.r -= col.c.r/2; if ( col.c.g > 0 ) col.c.g -= col.c.g/2; if ( col.c.b > 0 ) col.c.b -= col.c.b/2; StripLights_Pixel(i, 0, col.rgb ); } while( StripLights_Ready() == 0); StripLights_Trigger(1); CyDelay( 15 ); BOOT_CHECK(); } }
void CandyCane ( uint16_t count , uint32 c1, uint32 c2 ) { int i,x; uint8_t flip =0; // Candy cane // loop effect for this many times for( i=0; i < count ; i++ ) { // all strip, for every other led for(x = StripLights_MIN_X; x <= StripLights_MAX_X; x+=2) { // if flipped. draw c1,c2 otherwise c2,c1 if( flip ) { StripLights_Pixel(x, 0, c1); StripLights_Pixel(x+1, 0, c2); } else { StripLights_Pixel(x+1, 0, c1); StripLights_Pixel(x, 0, c2); } } // toggle flip flip = 1 - flip; // wait and trigger while( StripLights_Ready() == 0); StripLights_Trigger(1); // delay between transitions CyDelay( 100 ); BOOT_CHECK(); } }
int main() { uint16 Diff, FPS; CyGlobalIntEnable; /* Enable global interrupts. */ PixelMask = 0; PixelCounter = 0; NoSpeed = 0; Timer_Start(); Timer_ISR_StartEx(Timer_ISR); StripLights_Start(); StripLights_Dim(0); StripLights_MemClear(0x00000000); StripLights_Trigger(1); // EZI2C_EzI2CSetBuffer1(sizeof(I2C_Struct.I2C_Buffer), sizeof(I2C_Struct.I2C_Buffer), I2C_Struct.I2C_Buffer); // EZI2C_Start(); Diff = 60; for(;;) { //any Hall captures yet? if(Timer_Capture_Flag) { if(Timer_Capture > Timer_Capture_Last) Diff = Timer_Capture - Timer_Capture_Last; else Diff = (65535 - Timer_Capture_Last) + Timer_Capture; Timer_Capture_Flag = 0; StepColors(); NoSpeed = 0; } //nope, maybe I stopped else { Timeout++; } //kill the lights if I'm standing still if(Timeout > 20000) { NoSpeed = 1; Timeout = 0; } FPS = FPS_SCALE/Diff; //not using this but it could be fun later // I2C_Struct.I2C_Buffer[0] = (uint8)(Diff >>8); // I2C_Struct.I2C_Buffer[1] = (uint8)Diff; // I2C_Struct.I2C_Buffer[2] = (uint8)(FPS >>8); // I2C_Struct.I2C_Buffer[3] = (uint8)FPS; // if (!(EZI2C_EzI2CGetActivity() & EZI2C_EZI2C_STATUS_BUSY)) // { // I2C_Struct.Capture = Diff; // } //OK, time to write out to the LEDs if(1 == NoSpeed) StripLights_MemClear(0x00000000); StripLights_Trigger(1); while(!StripLights_Ready()); } }
/** * @brief Does all the work of getting data, simple parse and sending to LED's * * @return none */ void run_server(void) { uint8_t *buf_ptr; uint8_t ret = 0; //Set to black StripLights_DisplayClear(0); // LED off P1_6_Write(0); // this caused all sorts of issues, so i removed it #if 0 while( ret == 0 ) { ret = send_command("resetting\r\n", "AT+RST\r\n\n","ready",5000); //.com on later firmware, ready on others } P1_6_Write(0); #endif // Simple progress meter StripLights_SetXToColour( getColor(1) ,1 ); // returns "no change" , 1 CONNECT TO AP, 2 BE AN AP, 3 BOTH send_command("cwmode=3\r\n", "AT+CWMODE=1\r\n",NULL,DEFAULT_TIMEOUT); // Simple progress meter, stage 2 StripLights_SetXToColour( getColor(1) ,5 ); do { // LED On P1_6_Write(1); // Not really used, can be used to see if already connected send_command("get ip\r\n","AT+CIFSR\r\n",NULL,0); CyDelay(400); // wireless AP settings, first param is ap name, second password ret =send_command("connecting\r\n","AT+CWJAP=\"monkeysee\",\"monkeydo\"\r\n","OK",1000); // LED Off P1_6_Write(0); }while( ret == 0 ); // progress meter, stage 3 StripLights_SetXToColour( getColor(1) ,10 ); do { CyDelay(400); ret= send_command("check connection\r\n","AT+CWJAP?\r\n","OK",DEFAULT_TIMEOUT); } while( ret == 0 ); // progress meter, stage 4 StripLights_SetXToColour( getColor(1) ,15 ); //GET LOCAL IP ADDRESS do { CyDelay(400); ret= send_command("get ip\r\n","AT+CIFSR\r\n",NULL,0); } while( ret == 0 ); // progress meter, stage 5 StripLights_SetXToColour( getColor(1) ,20 ); //START UP MULTI-IP CONNECTION // 0 Single IP connection // 1 Multi IP connection do { CyDelay(400); ret= send_command("multip\r\n","AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT); } while( ret == 0 ); // progress meter, stage 6 StripLights_SetXToColour( getColor(1) ,25 ); do { CyDelay(400); ret= send_command("cipserver\r\n","AT+CIPSERVER=1,40002\r\n","OK",DEFAULT_TIMEOUT); } while( ret == 0 ); // progress meter, stage 7 StripLights_SetXToColour( getColor(1) ,30 ); // switch into UDP listen/receive mode, all data passed in will be of +IDT,0,length:data format do { CyDelay(400); ret= send_command("cipsto\r\n","AT+CIPSTO=9000\r\n","OK",DEFAULT_TIMEOUT); } while( ret == 0 ); // progress meter, stage 8 StripLights_SetXToColour( getColor(1) ,45 ); do { CyDelay(400); ret= send_command("cipmux\r\n","AT+CIPMUX=0\r\n","OK",DEFAULT_TIMEOUT); } while( ret == 0 ); // progress meter, stage 9 StripLights_SetXToColour( getColor(1) ,50 ); // setup done, tell host (if connected) UART_UartPutString("\nSetup and ready!\n"); // progress meter, stage 10, done StripLights_SetXToColour( getColor(2) ,StripLights_MAX_X ); CyDelay(200); // all off StripLights_DisplayClear(0); while(1) { int i ; uint8_t ch; // if switch is help, run into bootloader , mostly for dev BOOT_CHECK(); //led off P1_6_Write(0); // wait for data from ESP UART while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); // fetch one byte of data ch = uWIFI_UartGetChar(); // find start of +IPD,0,450: if( ch == '+' ) { //wait, this could be set to < 4 instead and then can drop the other checks. while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); ch = uWIFI_UartGetChar(); if( ch == 'I' ) { while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); ch = uWIFI_UartGetChar(); if( ch == 'P' ) { while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); ch = uWIFI_UartGetChar(); if( ch == 'D' ) { while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); ch = uWIFI_UartGetChar(); //UART_UartPutString("Found +IPD\n"); // illformatted if( ch != ',' ) { UART_UartPutString("Unexpected char #1\n"); break; } //led on P1_6_Write(1); // scan for end of descriptive // 10 will be enough 0,450: i = 10 ; do { while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); ch = uWIFI_UartGetChar(); i--; if( i ==0 ) { UART_UartPutString("couldn't find : marker\n"); break; } } while( ch != ':' ); //UART_UartPutString("Found Start of data block\n"); // point to start or end of LED buffer #if defined(REVERSE_DIRECTION) buf_ptr = (uint8_t*)&StripLights_ledArray[0][StripLights_MAX_X-1]; for( i = 0 ; i <= StripLights_MAX_X ; i++ ) { #else buf_ptr = (uint8_t*)&StripLights_ledArray[0][0]; for( i = 0 ; i <= StripLights_MAX_X ; i++ ) { #endif // fill in rx_buffer from ESP UART //gbr while ( uWIFI_SpiUartGetRxBufferSize() < 3 ); // 0 = green // 1 = red // 2 = blue buf_ptr[1] = uWIFI_UartGetChar(); buf_ptr[0] = uWIFI_UartGetChar(); buf_ptr[2] = uWIFI_UartGetChar(); #if defined(REVERSE_DIRECTION) buf_ptr -= sizeof(uint32_t); #else buf_ptr += sizeof(uint32_t); #endif } //end of buffer while ( uWIFI_SpiUartGetRxBufferSize() == 0 ); ch = uWIFI_UartGetChar(); // check this char for sanity if wanted /* UART_UartPutChar( ch) ; UART_UartPutString(" - Buffer filled\n"); UART_UartPutString( rx_buffer ); UART_UartPutString("END\n"); */ //send to LED strip while( StripLights_Ready() == 0); StripLights_Trigger(1); //CyDelay(4); BOOT_CHECK(); } } } } } } /** * @brief Just echo across UARTs * * * @return none */ void echo_uart(void) { while(1) { int ret; BOOT_CHECK(); ret = 0 ; while( ret == 0 ) { ret = send_command("resetting\r\n", "AT+RST\r\n\n","ready",5000); //.com on later firmware, ready on others } // echo from usb uart to wifi uart if( UART_SpiUartGetRxBufferSize() ) { uWIFI_UartPutChar( UART_UartGetChar() ); } //echo from wifi uart to usb uart if( uWIFI_SpiUartGetRxBufferSize() ) { UART_UartPutChar( uWIFI_UartGetChar() ); } } } // various simple effects (not used, sending PC does work) could be offline mode void ColorFader( int count , uint32 color) { while(count--){ FadeToColor( 0,StripLights_COLUMNS, color, 50,1 ); } } void Tween1( void ) { hsv_color tween; static led_color src; static hsv_color result ; src.c.r = rand()%255; src.c.g = rand()%255; src.c.b = rand()%255; tween = rgb_to_hsv((led_color)getColor(rand()%StripLights_COLOR_WHEEL_SIZE)); result.hsv = TweenerHSV( 0, StripLights_COLUMNS, result.hsv, tween.hsv, 10 ,1); // Tweener( 100,src.rgb ); src.c.r += 5-(rand()%10); src.c.g += 5-(rand()%10); src.c.b += 5-(rand()%10); result.hsv = TweenerHSV( StripLights_COLUMNS, StripLights_COLUMNS, result.hsv, tween.hsv, 10 ,-1 ); }