Subversion Repositories HomeAutomation

Rev

Rev 286 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 286 Rev 287
Line 1... Line 1...
1
/**
1
/**
2
 * CAN RGB LED. This application is used to control a RGB LED.
2
 * CAN RGB LED. This application is used to control a RGB LED.
3
 * It uses the HSL colorspace. Still under heavy development.
3
 * Still under heavy development
4
 * Still untested with a real RGB-LED, HSV Colorspace might be better suited. We'll see...
-
 
5
 
-
 
6
 * TODO: Use pointers in the HSL_2_RGB functions etc. It's currently a waste of memory.
-
 
7
 *
4
 *
8
 * @date    2007-02-04
5
 * @date    2007-02-04
9
 * @author  Martin Nordén
6
 * @author  Martin Nordén
10
 *  
7
 *  
11
 */
8
 */
Line 26... Line 23...
26
 
23
 
27
/*-----------------------------------------------------------------------------
24
/*-----------------------------------------------------------------------------
28
 * Variables
25
 * Variables
29
 *---------------------------------------------------------------------------*/
26
 *---------------------------------------------------------------------------*/
30
uint32_t timeStamp;
27
uint32_t timeStamp;
31
 
28
 
32
uint8_t R;      //Current Red PWM dutycycle
29
uint8_t currR;  //Current Red
33
uint8_t G;      //Current Green PWM dutycycle
30
uint8_t currG;  //Current Green
34
uint8_t B;      //Current Blue PWM dutycycle
31
uint8_t currB;  //Current Blue
35
 
32
 
36
double currH;   //HSL går mellan 0 och 1
33
uint8_t destR;  //Destination Red
37
double currS;
34
uint8_t destG;  //Destination Green
38
double currL;
35
uint8_t destB;  //Destination Blue
39
 
36
 
40
double destH;
-
 
41
double destS;
-
 
42
double destL;
37
uint8_t intensity;  //Current intensity.
43
 
38
 
44
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading
39
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading
45
uint8_t program;        //This variable keeps track of what program is currently running. 0 means no program.
40
uint8_t program;        //This variable keeps track of what program is currently running. 0 means no program.
46
uint8_t programstate;   //This variable keeps track of where in a program we currently are.
41
uint8_t programstate;   //This variable keeps track of where in a program we currently are.
47
 
42
 
48
/*----------------------------------------------------------------------------
43
/*----------------------------------------------------------------------------
49
 * Functions
44
 * Functions
50
 * -------------------------------------------------------------------------*/
45
 * -------------------------------------------------------------------------*/
51
void updateLED();
46
void updateLED();
52
uint8_t Hue_2_RGB(double v1, double v2, double v3);
47
void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
53
 
48
 
54
 
49
 
55
/*-----------------------------------------------------------------------------
50
/*-----------------------------------------------------------------------------
56
 * Main Program
51
 * Main Program
57
 *---------------------------------------------------------------------------*/
52
 *---------------------------------------------------------------------------*/
Line 67... Line 62...
67
    ICR1 = 256; //8 bit precision to match the other two counters.
62
    ICR1 = 256; //8 bit precision to match the other two counters.
68
    OCR1A = 128;  //50% as standard
63
    OCR1A = 128;  //50% as standard
69
    DDRB |= (1<<PB1); /* Enable pin as output */
64
    DDRB |= (1<<PB1); /* Enable pin as output */
70
 
65
 
71
//Initialize variables
66
//Initialize variables
72
    fadeSpeed = 20;
67
    fadeSpeed = 250;
73
    timeStamp = 0;
68
    timeStamp = 0;
74
 
69
 
75
    Can_Message_t rxMsg;
70
    Can_Message_t rxMsg;
76
   
-
 
77
    //Init HSL
-
 
78
    currH = 0;
71
    currR = 0x53;
79
    currS = 1;
72
    currG = 0x15;
80
    currL = 0.5;
73
    currB = 0xa0;
81
 
74
 
82
    sei(); 
75
    sei(); 
-
 
76
 
-
 
77
    reformatRGB(currR,currG,currB);
83
 
78
 
84
    /* main loop */
79
    /* main loop */
85
    while (1) {
80
    while (1) {
86
        /* service the CAN routines */
81
        /* service the CAN routines */
87
        Can_Service();
82
        Can_Service();
88
 
83
 
89
        /* Time to see if we want to update our values */
84
        /* Time to see if we want to update our values */
90
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
85
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
-
 
86
            intensity = intensity -1;
91
            timeStamp = Timebase_CurrentTime();
87
            timeStamp = Timebase_CurrentTime();
92
            currH = currH + 0.01;
-
 
93
            if (currH > 1) currH = 0;
-
 
94
            updateLED();
88
            updateLED();
95
        }
89
        }
96
 
90
 
97
 
91
 
98
        /* check if any messages have been received */
92
        /* check if any messages have been received */
Line 104... Line 98...
104
            }
98
            }
105
 
99
 
106
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
100
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
107
                program = 2;
101
                program = 2;
108
                fadeSpeed = 0;
102
                fadeSpeed = 0;
109
            }
103
            }
110
        }
104
        }
111
 
105
 
112
 
106
 
113
    }
107
    }
114
   
108
   
115
    return 0;
109
    return 0;
116
}
110
}
117
 
111
 
118
 
112
 
119
 
113
 
120
 
114
 
121
 
115
 
122
void updateLED(){
116
void updateLED(){
123
 
-
 
124
 
117
 
125
if ( currS == 0 )                       //HSL values = 0 ÷ 1
118
OCR1A = currR * intensity / 255;
126
{
119
 
127
   R = currL * 255;                      //RGB results = 0 ÷ 255
-
 
128
   G = currL * 255;
120
    Can_Message_t txMsg;
129
   B = currL * 255;
121
    txMsg.Id = 0x55;
130
}
-
 
131
else
-
 
132
{
-
 
133
   double var_1;
122
    txMsg.RemoteFlag = 0;
134
   double var_2;
123
    txMsg.ExtendedFlag = 1;
135
   if ( currL < 0.5 ) var_2 = currL * ( 1 + currS );
124
    txMsg.DataLength = 4;
136
   else           var_2 = ( currL + currS ) - ( currS * currL );
-
 
137
 
125
   
138
   var_1 = 2 * currL - var_2;
126
    txMsg.Data.bytes[0] = currR * intensity / 255;
139
 
-
 
140
   R = Hue_2_RGB( var_1, var_2, currH + ( 1 / 3 ) );
127
    txMsg.Data.bytes[1] = currG * intensity / 255;
141
   G = Hue_2_RGB( var_1, var_2, currH );
128
    txMsg.Data.bytes[2] = currB * intensity / 255;
142
   B = Hue_2_RGB( var_1, var_2, currH - ( 1 / 3 ) );
129
    txMsg.Data.bytes[3] = intensity;
143
}
-
 
144
 
130
 
-
 
131
    Can_Send(&txMsg);
145
 
132
 
146
 
133
 
147
OCR1A = R;
-
 
148
 
134
 
149
//Red
135
//Red
150
//Green
136
//Green
151
//Blue
137
//Blue
152
}
138
}
153
 
139
 
154
 
140
 
155
uint8_t Hue_2_RGB(double v1, double v2, double vH )             //Function Hue_2_RGB
141
void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
-
 
142
    uint8_t maxvalue;
-
 
143
    uint16_t tempcontainer;
156
{
144
 
-
 
145
    if ((R>G)&(R>B)){
-
 
146
        maxvalue = R;
157
   if ( vH < 0 ) vH += 1;
147
    } else if ((G>R)&(G>B)){
-
 
148
        maxvalue = G;
-
 
149
    } else {
-
 
150
        maxvalue = B;
-
 
151
    }
-
 
152
 
158
   if ( vH > 1 ) vH -= 1;
153
    tempcontainer = R * 255;
159
   if ( ( 6 * vH ) < 1 ) return 255*( v1 + ( v2 - v1 ) * 6 * vH );
154
    currR = tempcontainer/maxvalue;
-
 
155
    tempcontainer = G * 255;
160
   if ( ( 2 * vH ) < 1 ) return 255*( v2 );
156
    currG = tempcontainer/maxvalue;
-
 
157
    tempcontainer = B * 255;
161
   if ( ( 3 * vH ) < 2 ) return 255*( v1 + ( v2 - v1 ) * ( ( 2 / 3 ) - vH ) * 6 );
158
    currB = tempcontainer/maxvalue;
-
 
159
 
162
   return 255*( v1 );
160
    intensity = maxvalue;
-
 
161
 
163
}
162
}