SaikoLED fade demo using the HSI2RGBW_PWM libary
Dependencies: HSI2RGBW_PWM mbed
HSI to RGBW demo with direct PWM output
Code and library based on SaikoLED code :
https://github.com/saikoLED/MyKi/blob/master/myki_16_bit_random_fade/myki_16_bit_random_fade.ino
https://github.com/saikoLED/MyKi/blob/master/myki_16_bit_fade/myki_16_bit_fade.ino
http://blog.saikoled.com/post/44677718712/how-to-convert-from-hsi-to-rgb-white
main.cpp@0:562d0087dd7f, 2013-12-23 (annotated)
- Committer:
- frankvnk
- Date:
- Mon Dec 23 21:36:33 2013 +0000
- Revision:
- 0:562d0087dd7f
- Child:
- 3:afa2c73359fa
initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frankvnk | 0:562d0087dd7f | 1 | /* |
frankvnk | 0:562d0087dd7f | 2 | *************************************************** |
frankvnk | 0:562d0087dd7f | 3 | * Code ported from http://saikoled.com * |
frankvnk | 0:562d0087dd7f | 4 | * Licensed under GPL3 * |
frankvnk | 0:562d0087dd7f | 5 | * Created 14 February 2013 * |
frankvnk | 0:562d0087dd7f | 6 | * Original MCU board : Arduino Leonardo * |
frankvnk | 0:562d0087dd7f | 7 | * Original CPU : ATmega32u4 * |
frankvnk | 0:562d0087dd7f | 8 | * Copyright 2013, Brian Neltner * |
frankvnk | 0:562d0087dd7f | 9 | *************************************************** |
frankvnk | 0:562d0087dd7f | 10 | |
frankvnk | 0:562d0087dd7f | 11 | Sources |
frankvnk | 0:562d0087dd7f | 12 | ------- |
frankvnk | 0:562d0087dd7f | 13 | https://github.com/saikoLED/MyKi/blob/master/myki_16_bit_random_fade/myki_16_bit_random_fade.ino |
frankvnk | 0:562d0087dd7f | 14 | https://github.com/saikoLED/MyKi/blob/master/myki_16_bit_fade/myki_16_bit_fade.ino |
frankvnk | 0:562d0087dd7f | 15 | Above files are combined into this file using defines. |
frankvnk | 0:562d0087dd7f | 16 | |
frankvnk | 0:562d0087dd7f | 17 | http://blog.saikoled.com/post/44677718712/how-to-convert-from-hsi-to-rgb-white |
frankvnk | 0:562d0087dd7f | 18 | |
frankvnk | 0:562d0087dd7f | 19 | Other demos : Psychadelic demo |
frankvnk | 0:562d0087dd7f | 20 | DO NOT TRY THIS IF YOU ARE EPILEPTIC! THESE FREQUENCIES ARE EVEN WORSE THAN MOST STROBING LIGHTS! |
frankvnk | 0:562d0087dd7f | 21 | http://blog.saikoled.com/post/45760195354/generating-vivid-geometric-hallucinations-using-flicker |
frankvnk | 0:562d0087dd7f | 22 | |
frankvnk | 0:562d0087dd7f | 23 | This software implements 4x 16-bit PWM with a filtered fading algorithm |
frankvnk | 0:562d0087dd7f | 24 | in HSI color space and HSI -> RGBW conversion to allow for better |
frankvnk | 0:562d0087dd7f | 25 | pastel color. |
frankvnk | 0:562d0087dd7f | 26 | */ |
frankvnk | 0:562d0087dd7f | 27 | |
frankvnk | 0:562d0087dd7f | 28 | #include "mbed.h" |
frankvnk | 0:562d0087dd7f | 29 | #include "hsi2rgbw_pwm.h" |
frankvnk | 0:562d0087dd7f | 30 | |
frankvnk | 0:562d0087dd7f | 31 | #define RANDOM_FADE // Disable this define to set Normal fade. |
frankvnk | 0:562d0087dd7f | 32 | |
frankvnk | 0:562d0087dd7f | 33 | // Constants |
frankvnk | 0:562d0087dd7f | 34 | #define steptime 1 // Color transition time in ms. |
frankvnk | 0:562d0087dd7f | 35 | #define maxsaturation 1.0 |
frankvnk | 0:562d0087dd7f | 36 | |
frankvnk | 0:562d0087dd7f | 37 | #ifdef RANDOM_FADE |
frankvnk | 0:562d0087dd7f | 38 | #define propgain 0.0005 // "Small Constant" |
frankvnk | 0:562d0087dd7f | 39 | #define minsaturation 0.9 |
frankvnk | 0:562d0087dd7f | 40 | #else |
frankvnk | 0:562d0087dd7f | 41 | #define hue_increment 0.01 |
frankvnk | 0:562d0087dd7f | 42 | #endif |
frankvnk | 0:562d0087dd7f | 43 | |
frankvnk | 0:562d0087dd7f | 44 | Serial pc(USBTX, USBRX); |
frankvnk | 0:562d0087dd7f | 45 | |
frankvnk | 0:562d0087dd7f | 46 | // HSI to RGBW conversion with direct output to PWM channels |
frankvnk | 0:562d0087dd7f | 47 | hsi2rgbw_pwm led(PTD4, PTA12, PTA4, PTA5); |
frankvnk | 0:562d0087dd7f | 48 | |
frankvnk | 0:562d0087dd7f | 49 | // Dummy ISR for disabling NMI on PTA4 - !! DO NOT REMOVE THIS !! |
frankvnk | 0:562d0087dd7f | 50 | // More info at https://mbed.org/questions/1387/How-can-I-access-the-FTFA_FOPT-register-/ |
frankvnk | 0:562d0087dd7f | 51 | extern "C" void NMI_Handler() { |
frankvnk | 0:562d0087dd7f | 52 | DigitalIn test(PTA4); |
frankvnk | 0:562d0087dd7f | 53 | } |
frankvnk | 0:562d0087dd7f | 54 | |
frankvnk | 0:562d0087dd7f | 55 | struct HSI { |
frankvnk | 0:562d0087dd7f | 56 | float h; |
frankvnk | 0:562d0087dd7f | 57 | float s; |
frankvnk | 0:562d0087dd7f | 58 | float i; |
frankvnk | 0:562d0087dd7f | 59 | #ifdef RANDOM_FADE |
frankvnk | 0:562d0087dd7f | 60 | float htarget; |
frankvnk | 0:562d0087dd7f | 61 | float starget; |
frankvnk | 0:562d0087dd7f | 62 | #endif |
frankvnk | 0:562d0087dd7f | 63 | } color; |
frankvnk | 0:562d0087dd7f | 64 | |
frankvnk | 0:562d0087dd7f | 65 | void updatehue() { |
frankvnk | 0:562d0087dd7f | 66 | #ifdef RANDOM_FADE |
frankvnk | 0:562d0087dd7f | 67 | color.htarget += ((rand()%360)-180)*.1; |
frankvnk | 0:562d0087dd7f | 68 | color.htarget = fmod(color.htarget, 360); |
frankvnk | 0:562d0087dd7f | 69 | color.h += propgain*(color.htarget-color.h); |
frankvnk | 0:562d0087dd7f | 70 | color.h = fmod(color.h, 360); |
frankvnk | 0:562d0087dd7f | 71 | #else |
frankvnk | 0:562d0087dd7f | 72 | color.h = color.h + hue_increment; |
frankvnk | 0:562d0087dd7f | 73 | if(color.h > 360) |
frankvnk | 0:562d0087dd7f | 74 | color.h = 0; |
frankvnk | 0:562d0087dd7f | 75 | #endif |
frankvnk | 0:562d0087dd7f | 76 | } |
frankvnk | 0:562d0087dd7f | 77 | |
frankvnk | 0:562d0087dd7f | 78 | void updatesaturation() { |
frankvnk | 0:562d0087dd7f | 79 | #ifdef RANDOM_FADE |
frankvnk | 0:562d0087dd7f | 80 | color.starget += ((rand()%10000)-5000)/0.00001; |
frankvnk | 0:562d0087dd7f | 81 | if (color.starget > maxsaturation) color.starget = maxsaturation; |
frankvnk | 0:562d0087dd7f | 82 | else if (color.starget < minsaturation) color.starget = minsaturation; |
frankvnk | 0:562d0087dd7f | 83 | color.s += propgain*(color.starget-color.s); |
frankvnk | 0:562d0087dd7f | 84 | if (color.s > maxsaturation) color.s = maxsaturation; |
frankvnk | 0:562d0087dd7f | 85 | else if (color.s < minsaturation) color.s = minsaturation; |
frankvnk | 0:562d0087dd7f | 86 | #else |
frankvnk | 0:562d0087dd7f | 87 | color.s = maxsaturation; |
frankvnk | 0:562d0087dd7f | 88 | #endif |
frankvnk | 0:562d0087dd7f | 89 | } |
frankvnk | 0:562d0087dd7f | 90 | |
frankvnk | 0:562d0087dd7f | 91 | void primary_colors() { |
frankvnk | 0:562d0087dd7f | 92 | printf("briefly show RED - GREEN - BLUE using hsi2rgbw.\r\n"); |
frankvnk | 0:562d0087dd7f | 93 | for (color.h = 0 ; color.h < 360 ; color.h += 120) |
frankvnk | 0:562d0087dd7f | 94 | { |
frankvnk | 0:562d0087dd7f | 95 | led.hsi2rgbw(color.h, color.s, color.i); |
frankvnk | 0:562d0087dd7f | 96 | wait(1); |
frankvnk | 0:562d0087dd7f | 97 | } |
frankvnk | 0:562d0087dd7f | 98 | } |
frankvnk | 0:562d0087dd7f | 99 | |
frankvnk | 0:562d0087dd7f | 100 | void cycle_rgbw() { |
frankvnk | 0:562d0087dd7f | 101 | float rgbw[4] = {0,0,0,0}; |
frankvnk | 0:562d0087dd7f | 102 | printf("briefly show RED - GREEN - BLUE - WHITE using direct PWM output.\r\n"); |
frankvnk | 0:562d0087dd7f | 103 | rgbw[0] = 1.0f; |
frankvnk | 0:562d0087dd7f | 104 | led.pwm(rgbw); |
frankvnk | 0:562d0087dd7f | 105 | wait(1); |
frankvnk | 0:562d0087dd7f | 106 | rgbw[0] = 0.0f; |
frankvnk | 0:562d0087dd7f | 107 | rgbw[1] = 1.0f; |
frankvnk | 0:562d0087dd7f | 108 | led.pwm(rgbw); |
frankvnk | 0:562d0087dd7f | 109 | wait(1); |
frankvnk | 0:562d0087dd7f | 110 | rgbw[1] = 0.0f; |
frankvnk | 0:562d0087dd7f | 111 | rgbw[2] = 1.0f; |
frankvnk | 0:562d0087dd7f | 112 | led.pwm(rgbw); |
frankvnk | 0:562d0087dd7f | 113 | wait(1); |
frankvnk | 0:562d0087dd7f | 114 | rgbw[2] = 0.0f; |
frankvnk | 0:562d0087dd7f | 115 | rgbw[3] = 1.0f; |
frankvnk | 0:562d0087dd7f | 116 | led.pwm(rgbw); |
frankvnk | 0:562d0087dd7f | 117 | wait(1); |
frankvnk | 0:562d0087dd7f | 118 | } |
frankvnk | 0:562d0087dd7f | 119 | |
frankvnk | 0:562d0087dd7f | 120 | void sendcolor() { |
frankvnk | 0:562d0087dd7f | 121 | while (color.h >=360) color.h = color.h - 360; |
frankvnk | 0:562d0087dd7f | 122 | while (color.h < 0) color.h = color.h + 360; |
frankvnk | 0:562d0087dd7f | 123 | if (color.i > 1) color.i = 1; |
frankvnk | 0:562d0087dd7f | 124 | if (color.i < 0) color.i = 0; |
frankvnk | 0:562d0087dd7f | 125 | if (color.s > 1) color.s = 1; |
frankvnk | 0:562d0087dd7f | 126 | if (color.s < 0) color.s = 0; |
frankvnk | 0:562d0087dd7f | 127 | // Fix ranges (somewhat redundantly). |
frankvnk | 0:562d0087dd7f | 128 | led.hsi2rgbw(color.h, color.s, color.i); |
frankvnk | 0:562d0087dd7f | 129 | } |
frankvnk | 0:562d0087dd7f | 130 | |
frankvnk | 0:562d0087dd7f | 131 | void setup() { |
frankvnk | 0:562d0087dd7f | 132 | led.colorMode(RGBW); |
frankvnk | 0:562d0087dd7f | 133 | color.h = 0; |
frankvnk | 0:562d0087dd7f | 134 | color.s = maxsaturation; |
frankvnk | 0:562d0087dd7f | 135 | color.i = 0; |
frankvnk | 0:562d0087dd7f | 136 | #ifdef RANDOM_FADE |
frankvnk | 0:562d0087dd7f | 137 | color.htarget = 0; |
frankvnk | 0:562d0087dd7f | 138 | color.starget = maxsaturation; |
frankvnk | 0:562d0087dd7f | 139 | #endif |
frankvnk | 0:562d0087dd7f | 140 | |
frankvnk | 0:562d0087dd7f | 141 | // Initial color = off, hue of red fully saturated. |
frankvnk | 0:562d0087dd7f | 142 | while (color.i < 1) { |
frankvnk | 0:562d0087dd7f | 143 | sendcolor(); |
frankvnk | 0:562d0087dd7f | 144 | color.i = color.i + 0.001; // Increase Intensity |
frankvnk | 0:562d0087dd7f | 145 | updatehue(); |
frankvnk | 0:562d0087dd7f | 146 | updatesaturation(); |
frankvnk | 0:562d0087dd7f | 147 | wait_ms(steptime); |
frankvnk | 0:562d0087dd7f | 148 | } |
frankvnk | 0:562d0087dd7f | 149 | } |
frankvnk | 0:562d0087dd7f | 150 | |
frankvnk | 0:562d0087dd7f | 151 | int main() { |
frankvnk | 0:562d0087dd7f | 152 | pc.baud (115200); |
frankvnk | 0:562d0087dd7f | 153 | printf("HSI to RGBW random fade.\r\n"); |
frankvnk | 0:562d0087dd7f | 154 | printf("Initialize.\r\n"); |
frankvnk | 0:562d0087dd7f | 155 | setup(); |
frankvnk | 0:562d0087dd7f | 156 | primary_colors(); |
frankvnk | 0:562d0087dd7f | 157 | cycle_rgbw(); |
frankvnk | 0:562d0087dd7f | 158 | printf("Run.\r\n"); |
frankvnk | 0:562d0087dd7f | 159 | while(1) { |
frankvnk | 0:562d0087dd7f | 160 | sendcolor(); |
frankvnk | 0:562d0087dd7f | 161 | updatehue(); |
frankvnk | 0:562d0087dd7f | 162 | updatesaturation(); |
frankvnk | 0:562d0087dd7f | 163 | wait_ms(steptime); |
frankvnk | 0:562d0087dd7f | 164 | } |
frankvnk | 0:562d0087dd7f | 165 | } |