Allan Wang / Mbed 2 deprecated rgbstick

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "lut.h"
00003 
00004 SPI spi(p11, p12, p13);
00005 DigitalOut latchpin(p10);
00006 DigitalOut led1(LED1);
00007 DigitalOut led2(LED2);
00008 InterruptIn hall(p7);
00009 
00010 #define LEDS 96
00011 #define MAX_BRIGHTNESS 4095
00012 
00013 void HSVtoRGB( int  *r, int *g,int *b, int h, int s, int v );
00014 
00015 
00016 void hall_fall() {
00017     led1 = 1;
00018 }
00019 
00020 void hall_rise() {
00021     led1 = 0;
00022 }
00023 
00024 void latch() {
00025     latchpin = 1;
00026     latchpin = 1;
00027     latchpin = 1;
00028     latchpin = 1;
00029     latchpin = 1;
00030     latchpin = 1;
00031     latchpin = 0;
00032 }
00033 
00034 int main() {
00035     // When magnet goes near Hall-effect sensor, its output will go LOW
00036     hall.fall(&hall_fall);
00037     hall.rise(&hall_rise);
00038 
00039     spi.format(12, 0);
00040     spi.frequency(16 * 1000 * 1000);
00041 
00042     int brightness = 0;
00043     int dir = 0;
00044     int r, g, b;
00045 
00046     latchpin = 0;
00047     while (1) {
00048         for (int h = 3599; h >= 0; h--) {
00049             for (int i = LEDS-1; i >= 0; i--) {
00050                 HSVtoRGB(&r, &g, &b, h + i*150/4, 4095, brightness / 4);
00051 
00052                 spi.write(gamma[b]);
00053                 spi.write(gamma[g]);
00054                 spi.write(gamma[r]);
00055             }
00056             latch();
00057 
00058             if (dir == 0) {
00059                 brightness += 1;
00060                 if (brightness >= 4096) {
00061                     brightness = 4095;
00062                     dir = 1;
00063                 }
00064             } else {
00065                 brightness -= 1;
00066                 if (brightness < 0) {
00067                     brightness = 0;
00068                     dir = 0;
00069                 }
00070             }
00071         }
00072     }
00073 }
00074 
00075 void HSVtoRGB( int  *r, int *g,int *b, int h, int s, int v ) {
00076     int f;
00077     int p, q, t;
00078 
00079     if ( s == 0 ) {
00080         *r = *g = *b = v;
00081         return;
00082     }
00083 
00084     h = h % 3600;
00085 
00086     f = ((h%600)*4095)/600;
00087     h /= 600;
00088 
00089     p = (v * (4096-s))/4096;
00090     q = (v * (4096 - (s*f)/4096))/4096;
00091     t = (v * (4096 - (s*(4096-f))/4096))/4096;
00092 
00093     switch ( h ) {
00094         case 0:
00095             *r = v;
00096             *g = t;
00097             *b = p;
00098             break;
00099         case 1:
00100             *r = q;
00101             *g = v;
00102             *b = p;
00103             break;
00104         case 2:
00105             *r = p;
00106             *g = v;
00107             *b = t;
00108             break;
00109         case 3:
00110             *r = p;
00111             *g = q;
00112             *b = v;
00113             break;
00114         case 4:
00115             *r = t;
00116             *g = p;
00117             *b = v;
00118             break;
00119         default:        // case 5:
00120             *r = v;
00121             *g = p;
00122             *b = q;
00123             break;
00124     }
00125 }