vec3f hsl2rgb(const vec3f &hsl) { float s = hsl.y; float l = hsl.z; if (s == 0) { return vec3f(l, l, l); } else { float v1, v2; if (l < 0.5) { v2 = l * (1 + s); } else { v2 = l + s - s * l; } v1 = 2 * l - v2; float r = h2rgb(v1, v2, hsl.x + 1.0f / 3); float g = h2rgb(v1, v2, hsl.x); float b = h2rgb(v1, v2, hsl.x - 1.0f / 3); return vec3f(r, g, b); } }
uint32_t PixelBone_Pixel::HSL(uint32_t hue, uint32_t saturation,uint32_t lightness) { uint32_t red, green, blue; uint32_t var1, var2; if (hue > 359) hue = hue % 360; if (saturation > 100) saturation = 100; if (lightness > 100) lightness = 100; // algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19 if (saturation == 0) { red = green = blue = lightness * 255 / 100; } else { if (lightness < 50) { var2 = lightness * (100 + saturation); } else { var2 = ((lightness + saturation) * 100) - (saturation * lightness); } var1 = lightness * 200 - var2; red = h2rgb(var1, var2, (hue < 240) ? hue + 120 : hue - 240) * 255 / 600000; green = h2rgb(var1, var2, hue) * 255 / 600000; blue = h2rgb(var1, var2, (hue >= 120) ? hue - 120 : hue + 240) * 255 / 600000; } return (red << 16) | (green << 8) | blue; }
// hue is between 0 and 1024 void MIYbot::setColorByHue(int hue){ int r; int g; int b; float h = ((float)hue)/1024; h2rgb(h, r, g, b); setRGB( r, g, b); Serial.print (h) ; Serial.print (" ") ; Serial.print (r) ; Serial.print (" ") ; Serial.print (g) ; Serial.print (" ") ; Serial.print (b) ; Serial.println (" ") ; }
void shadePixel(double t, int pixel, float x, float y){ float r, g, b; float angle = fmod( (double)(t)/10, (double)360); angle /= 57.2957795; float px, py, cs, sn; // Move origin to center x-=0.5; y-=0.5; // Pan the X/Y position on a sine wave x+=sin(t/10000.0); y+=sin(y/10000.0); // Rotate the pixels cs = cos(angle); sn = sin(angle); px = (x * cs) - (y * sn); py = (y * cs) + (x * sn); // Convert hue to RGB float hue = (((px+py)/8) + t / 10000.0); h2rgb(hue, &r, &g, &b); // Clamp max value if(r>1.0) r=1.0; if(g>1.0) g=1.0; if(b>1.0) b=1.0; setPixelColorRGB(pixel, (int)(r*255), (int)(g*255), (int)(b*255)); }
int main(void) { LPC_GPIO2->DIR |= (1<<10); /* PLL is already setup */ SystemCoreClockUpdate(); /* Blue LED Set as output */ /* Relay EN Set as output */ LPC_GPIO1->DIR |= (1<<5); uartInit(115200); puts("Sys Initted\n"); puts("uart Initted\n"); initSwitch(); puts("Switches Initted\n"); SysTick_Config(SystemCoreClock/1000); puts("SysTick Initted\n"); setup_pwm(1000,4092); puts("PWM Initted\n"); setup_GPIO_INT(); puts("GPIO INT Initted\n"); filt_adc_init(450); delay_ms(500); puts("Filt ADC Initted\n"); while(1) { while(next_run > msTicks) __WFI(); next_run= msTicks + 10; if ((msTicks - last_motion ) > (MAX(W_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS, RGB_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS ) + 2000)) { shut_off_supply(); transfer_to_sleep(); turn_on_supply(); /* Wait for the power to actually come on before starting the ramp up */ } // WW_SWITCH_HANDLER // debounce for 20ms, since we check for the absolute val if the user // holds the switch forever it will only trigger the service once if(getSwitch(WW_SWITCH) == 0) { if(ww_sw_asserted == 5) { puts("WW SWITCH PRESSED\n"); white_on = !white_on; white_scale = 0; } ww_sw_asserted += 1; } else ww_sw_asserted = 0; // OFF_SWITCH_HANDLER // debounce for 20ms, since we check for the absolute val if the user // holds the switch forever it will only trigger the service once if(getSwitch(OFF_SWITCH) == 0) { if(off_sw_asserted == 5) { puts("OFF SWITCH PRESSED\n"); white_on = 0; white_scale = 0; rgb_on = 0; rgb_scale = 0; } off_sw_asserted += 1; } else off_sw_asserted = 0; // RGB_SWITCH_HANDLER // debounce for 20ms, since we check for the absolute val if the user // holds the switch forever it will only trigger the service once if(getSwitch(RGB_SWITCH) == 0) { if(rgb_sw_asserted == 5) { puts("RGB SWITCH PRESSED\n"); rgb_on = !rgb_on; rgb_scale = 0; } if(rgb_sw_asserted == 100) { puts("RGB SWITCH HELD\n"); rgb_on = 2; } if(rgb_sw_asserted == 500) { puts("RGB SWITCH LONG HELD\n"); rgb_on = 3; } rgb_sw_asserted += 1; } else rgb_sw_asserted = 0; /* This handles the WW LEDs */ if (((msTicks - last_motion ) > W_POWER_OFF_TIME_MS) && white_on) { white_scale = RAMP_DOWN_TIME_MS - ((msTicks - last_motion) - W_POWER_OFF_TIME_MS); if ((msTicks - last_motion) > (W_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS)) white_scale = 0; } if ( white_scale < RAMP_DOWN_TIME_MS && white_on && ((msTicks - last_motion ) < W_POWER_OFF_TIME_MS)) { white_scale = msTicks - last_motion; /* Check just in case we get held up and skip the 1000th ms after motion */ if ( white_scale > RAMP_DOWN_TIME_MS) white_scale = RAMP_DOWN_TIME_MS; } if (white_scale != RAMP_DOWN_TIME_MS) { setWW( (white_scale * getADCVal(WW_POT)) / RAMP_DOWN_TIME_MS ); setWW2( (white_scale * getADCVal(WW_POT)) / RAMP_DOWN_TIME_MS ); } else { setWW(getADCVal(WW_POT)); setWW2(getADCVal(WW_POT)); } /* This handles the RGB LEDs */ if (((msTicks - last_motion ) > RGB_POWER_OFF_TIME_MS) && rgb_on) { rgb_scale = RAMP_DOWN_TIME_MS - ((msTicks - last_motion) - RGB_POWER_OFF_TIME_MS); if ((msTicks - last_motion) > (RGB_POWER_OFF_TIME_MS + RAMP_DOWN_TIME_MS)) rgb_scale = 0; } if ( rgb_scale < RAMP_DOWN_TIME_MS && rgb_on && ((msTicks - last_motion ) < RGB_POWER_OFF_TIME_MS)) { rgb_scale = msTicks - last_motion; /* Check just in case we get held up and skip the 1000th ms after motion */ if ( rgb_scale > RAMP_DOWN_TIME_MS) rgb_scale = RAMP_DOWN_TIME_MS; } if (rgb_on == 1 || rgb_on == 0) { if (rgb_scale != RAMP_DOWN_TIME_MS) setRGB((rgb_scale * getADCVal(RED_POT)) / RAMP_DOWN_TIME_MS, (rgb_scale * getADCVal(GREEN_POT)) / RAMP_DOWN_TIME_MS, (rgb_scale * getADCVal(BLUE_POT)) / RAMP_DOWN_TIME_MS ); else setRGB(getADCVal(RED_POT), getADCVal(GREEN_POT), getADCVal(BLUE_POT)); } else if (rgb_on == 2) { uint32_t R,G,B; h2rgb((msTicks)%(H2RGB_OUT_RANGE*6), &R, &G, &B); if (rgb_scale != RAMP_DOWN_TIME_MS) setRGB((rgb_scale * R) / RAMP_DOWN_TIME_MS, (rgb_scale * G) / RAMP_DOWN_TIME_MS, (rgb_scale * B) / RAMP_DOWN_TIME_MS ); else setRGB(R, G, B); } else if (rgb_on == 3) { uint32_t R,G,B; if (rgb_mode_3_next_change < msTicks) { rgb_mode_3_next_change = msTicks + (getADCVal(RED_POT)/4); rgb_mode_3_color += 2; if(rgb_mode_3_color >=6) rgb_mode_3_color = 0; } h2rgb((H2RGB_OUT_RANGE * rgb_mode_3_color), &R, &G, &B); if (rgb_scale != RAMP_DOWN_TIME_MS) setRGB((rgb_scale * R) / RAMP_DOWN_TIME_MS, (rgb_scale * G) / RAMP_DOWN_TIME_MS, (rgb_scale * B) / RAMP_DOWN_TIME_MS ); else setRGB(R, G, B); } } }