Back-ported the working version from avr to msp. This is NOT YET TESTED.
[cerebrum.git] / msp / timer-pwm-rgb.c.tp
blob2b792d38cf862acb514d86787ff9839feb9892fa
1 /*
2  Copyright (C) 2012 jaseg <s@jaseg.de>
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 3 as published by the Free Software Foundation.
7  */
9 #ifndef _TIMER_PWM_RGB_STATIC_
10 #define _TIMER_PWM_RGB_STATIC_
11 #include <msp430.h>
12 #include <math.h>
14 typedef unsigned char uint8_t;
15 typedef unsigned int uint16_t;
17 typedef struct{
18     uint8_t r, g, b;
19 } rgbval; 
21 typedef struct{
22     float h, s, v;
23 } hsvval;
25 //8 bit to 16 bit gamma correction table
26 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 };
28 rgbval hsv2rgb(hsvval in){
29     int hi = (int)floorf(in.h/60.0F);
30     float f = (in.h/60.0F-hi);
31     float p = in.v*(1.0F-in.s);
32     float q = in.v*(1.0F-in.s*f);
33     float t = in.v*(1.0F+in.s*f-in.s);
34     rgbval ret;
35     switch(hi){
36         case 0:
37             ret.r = in.v*0xFF;
38             ret.g = t*0xFF;
39             ret.b = p*0xFF;
40             break;
41         case 1:
42             ret.r = q*0xFF;
43             ret.g = in.v*0xFF;
44             ret.b = p*0xFF;
45             break;
46         case 2:
47             ret.r = p*0xFF;
48             ret.g = in.v*0xFF;
49             ret.b = t*0xFF;
50             break;
51         case 3:
52             ret.r = p*0xFF;
53             ret.g = q*0xFF;
54             ret.b = in.v*0xFF;
55             break;
56         case 4:
57             ret.r = t*0xFF;
58             ret.g = p*0xFF;
59             ret.b = in.v*0xFF;
60             break;
61         case 5:
62             ret.r = in.v*0xFF;
63             ret.g = p*0xFF;
64             ret.b = q*0xFF;
65             break;
66     }
67     return ret;
70 #endif//_TIMER_PWM_RGB_STATIC_
72 void ${init_function()} (){
73     //PWM Timer setup (uses both TIMER_As)
74     TA0CTL = MC_2 | TASSEL_2; //Continous with SMCLK
75     TA1CTL = MC_2 | TASSEL_2; //Continous with SMCLK
76 % if "pwm_inverted" in member and member["pwm_inverted"]:
77     TA0CCTL1 = OUTMOD_7;
78     TA1CCTL1 = OUTMOD_7;
79     TA1CCTL2 = OUTMOD_7;
80 % else:
81     TA0CCTL1 = OUTMOD_3;
82     TA1CCTL1 = OUTMOD_3;
83     TA1CCTL2 = OUTMOD_3;
84 % endif
85     //PWM output io setup
86     P1DIR |= 0x40; //P1.6
87     P1SEL |= 0x40;
88     P2DIR |= 0x12; //P2.1 2.4
89     P2SEL |= 0x12;
92 ${modulevar("col", "hsvval", "fff")} = {0.0, 1.0, 1.0};
94 void ${loop_function()} (){
95     rgbval rgb = hsv2rgb( ${modulevar("col")} );
96     TA1CCR2 = gammatable[rgb.r]; // red
97     TA0CCR1 = gammatable[rgb.g]; // green 
98     TA1CCR1 = gammatable[rgb.b]; // blue