Added the config.* files as used in the helights case
[cerebrum.git] / msp / timer-pwm-rgb.c.tp
blob891fa7a7b46375a51037c4febeea63fab666da6f
2 #ifndef _TIMER_PWM_RGB_STATIC_
3 #define _TIMER_PWM_RGB_STATIC_
4 #include <msp430.h>
5 #include <math.h>
7 typedef unsigned char uint8_t;
8 typedef unsigned int uint16_t;
10 typedef struct{
11     uint8_t r, g, b;
12 } rgbval; 
14 typedef struct{
15     float h, s, v;
16 } hsvval;
18 //8 bit to 16 bit gamma correction table
19 const uint16_t gammatable[] = { 0x0, 0x5, 0x11, 0x22, 0x38, 0x51, 0x6f, 0x91, 0xb6, 0xde, 0x10a, 0x139, 0x16b, 0x19f, 0x1d7, 0x212, 0x250, 0x290, 0x2d3, 0x318, 0x361, 0x3ab, 0x3f9, 0x449, 0x49b, 0x4f0, 0x547, 0x5a1, 0x5fc, 0x65b, 0x6bb, 0x71e, 0x783, 0x7ea, 0x854, 0x8c0, 0x92e, 0x99e, 0xa10, 0xa84, 0xafa, 0xb73, 0xbee, 0xc6a, 0xce9, 0xd6a, 0xdec, 0xe71, 0xef8, 0xf81, 0x100b, 0x1098, 0x1126, 0x11b7, 0x1249, 0x12de, 0x1374, 0x140c, 0x14a6, 0x1542, 0x15e0, 0x167f, 0x1721, 0x17c4, 0x1869, 0x1910, 0x19b9, 0x1a63, 0x1b10, 0x1bbe, 0x1c6e, 0x1d1f, 0x1dd3, 0x1e88, 0x1f3f, 0x1ff7, 0x20b2, 0x216e, 0x222c, 0x22eb, 0x23ac, 0x246f, 0x2534, 0x25fa, 0x26c2, 0x278c, 0x2857, 0x2924, 0x29f3, 0x2ac3, 0x2b95, 0x2c69, 0x2d3e, 0x2e15, 0x2eed, 0x2fc7, 0x30a3, 0x3180, 0x325f, 0x3340, 0x3422, 0x3505, 0x35eb, 0x36d1, 0x37ba, 0x38a4, 0x398f, 0x3a7c, 0x3b6b, 0x3c5b, 0x3d4d, 0x3e40, 0x3f35, 0x402c, 0x4124, 0x421d, 0x4318, 0x4414, 0x4512, 0x4612, 0x4713, 0x4815, 0x491a, 0x4a1f, 0x4b26, 0x4c2f, 0x4d39, 0x4e44, 0x4f51, 0x505f, 0x516f, 0x5281, 0x5393, 0x54a8, 0x55bd, 0x56d5, 0x57ed, 0x5907, 0x5a23, 0x5b40, 0x5c5e, 0x5d7e, 0x5e9f, 0x5fc2, 0x60e6, 0x620c, 0x6333, 0x645b, 0x6585, 0x66b0, 0x67dd, 0x690b, 0x6a3b, 0x6b6b, 0x6c9e, 0x6dd1, 0x6f06, 0x703d, 0x7175, 0x72ae, 0x73e8, 0x7524, 0x7662, 0x77a0, 0x78e0, 0x7a22, 0x7b65, 0x7ca9, 0x7dee, 0x7f35, 0x807e, 0x81c7, 0x8312, 0x845f, 0x85ac, 0x86fb, 0x884c, 0x899d, 0x8af0, 0x8c45, 0x8d9a, 0x8ef1, 0x904a, 0x91a3, 0x92fe, 0x945b, 0x95b8, 0x9717, 0x9878, 0x99d9, 0x9b3c, 0x9ca0, 0x9e06, 0x9f6d, 0xa0d5, 0xa23e, 0xa3a9, 0xa515, 0xa683, 0xa7f1, 0xa961, 0xaad2, 0xac45, 0xadb9, 0xaf2e, 0xb0a4, 0xb21c, 0xb395, 0xb50f, 0xb68a, 0xb807, 0xb985, 0xbb04, 0xbc85, 0xbe07, 0xbf8a, 0xc10e, 0xc294, 0xc41b, 0xc5a3, 0xc72c, 0xc8b7, 0xca43, 0xcbd0, 0xcd5e, 0xceee, 0xd07f, 0xd211, 0xd3a4, 0xd539, 0xd6cf, 0xd866, 0xd9fe, 0xdb98, 0xdd32, 0xdece, 0xe06c, 0xe20a, 0xe3aa, 0xe54b, 0xe6ed, 0xe890, 0xea35, 0xebda, 0xed81, 0xef2a, 0xf0d3, 0xf27e, 0xf42a, 0xf5d7, 0xf785, 0xf935, 0xfae5, 0xfc97, 0xfe4a };
21 rgbval hsv2rgb(hsvval in){
22     int hi = (int)floorf(in.h/60.0F);
23     float f = (in.h/60.0F-hi);
24     float p = in.v*(1.0F-in.s);
25     float q = in.v*(1.0F-in.s*f);
26     float t = in.v*(1.0F+in.s*f-in.s);
27     rgbval ret;
28     switch(hi){
29         case 0:
30             ret.r = in.v*0xFF;
31             ret.g = t*0xFF;
32             ret.b = p*0xFF;
33             break;
34         case 1:
35             ret.r = q*0xFF;
36             ret.g = in.v*0xFF;
37             ret.b = p*0xFF;
38             break;
39         case 2:
40             ret.r = p*0xFF;
41             ret.g = in.v*0xFF;
42             ret.b = t*0xFF;
43             break;
44         case 3:
45             ret.r = p*0xFF;
46             ret.g = q*0xFF;
47             ret.b = in.v*0xFF;
48             break;
49         case 4:
50             ret.r = t*0xFF;
51             ret.g = p*0xFF;
52             ret.b = in.v*0xFF;
53             break;
54         case 5:
55             ret.r = in.v*0xFF;
56             ret.g = p*0xFF;
57             ret.b = q*0xFF;
58             break;
59     }
60     return ret;
63 #endif//_TIMER_PWM_RGB_STATIC_
65 void ${init_function} (){
66     //PWM Timer setup (uses both TIMER_As)
67     TA0CTL = MC_2 | TASSEL_2; //Continous with SMCLK
68     TA1CTL = MC_2 | TASSEL_2; //Continous with SMCLK
69 % if "pwm_inverted" in dev and dev["pwm_inverted"]:
70     TA0CCTL1 = OUTMOD_7;
71     TA1CCTL1 = OUTMOD_7;
72     TA1CCTL2 = OUTMOD_7;
73 % else:
74     TA0CCTL1 = OUTMOD_3;
75     TA1CCTL1 = OUTMOD_3;
76     TA1CCTL2 = OUTMOD_3;
77 % endif
78     //PWM output io setup
79     P1DIR |= 0x40; //P1.6
80     P1SEL |= 0x40;
81     P2DIR |= 0x12; //P2.1 2.4
82     P2SEL |= 0x12;
85 ${modulevar("col", "hsvval", "fff")} = {0.0, 1.0, 1.0};
87 void ${loop_function} (){
88     rgbval rgb = hsv2rgb( ${modulevar("col")} );
89     TA1CCR2 = gammatable[rgb.r]; // red
90     TA0CCR1 = gammatable[rgb.g]; // green 
91     TA1CCR1 = gammatable[rgb.b]; // blue