Subversion Repositories HomeAutomation

Rev

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

Rev 285 Rev 286
Line 1... Line 1...
1
/**
1
/**
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
2
 * CAN RGB LED. This application is used to control a RGB LED.
3
 * message is increased each time for testing purposes.
3
 * It uses the HSL colorspace. 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.
4
 *
7
 *
5
 * @date    2006-11-21
8
 * @date    2007-02-04
6
 * @author  Jimmy Myhrman
9
 * @author  Martin Nordén
7
 *  
10
 *  
8
 */
11
 */
9
 
12
 
10
/*-----------------------------------------------------------------------------
13
/*-----------------------------------------------------------------------------
11
 * Includes
14
 * Includes
Line 22... Line 25...
22
#include <math.h>
25
#include <math.h>
23
 
26
 
24
/*-----------------------------------------------------------------------------
27
/*-----------------------------------------------------------------------------
25
 * Variables
28
 * Variables
26
 *---------------------------------------------------------------------------*/
29
 *---------------------------------------------------------------------------*/
-
 
30
uint32_t timeStamp;
27
 
31
 
28
uint8_t currRed;        //Current Red PWM dutycycle
32
uint8_t R;      //Current Red PWM dutycycle
29
uint8_t currGreen;  //Current Green PWM dutycycle
33
uint8_t G;      //Current Green PWM dutycycle
30
uint8_t currBlue;       //Current Blue PWM dutycycle
34
uint8_t B;      //Current Blue PWM dutycycle
31
 
35
 
32
uint8_t destRed;        //Destination Red PWM dutycycle
36
double currH;   //HSL går mellan 0 och 1
-
 
37
double currS;
-
 
38
double currL;
-
 
39
 
33
uint8_t destGreen;  //Destination Green PWM dutycycle
40
double destH;
-
 
41
double destS;
34
uint8_t destBlue;       //Destination Blue PWM dutycycle
42
double destL;
35
 
43
 
36
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading
44
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading
37
uint8_t program;        //This variable keeps track of what program is currently running. 0 means no program.
45
uint8_t program;        //This variable keeps track of what program is currently running. 0 means no program.
38
uint8_t programstate;   //This variable keeps track of where in a program we currently are.
46
uint8_t programstate;   //This variable keeps track of where in a program we currently are.
39
 
47
 
40
/*----------------------------------------------------------------------------
48
/*----------------------------------------------------------------------------
41
 * Functions
49
 * Functions
42
 * -------------------------------------------------------------------------*/
50
 * -------------------------------------------------------------------------*/
43
void updateLED();
51
void updateLED();
-
 
52
uint8_t Hue_2_RGB(double v1, double v2, double v3);
44
 
53
 
45
 
54
 
46
/*-----------------------------------------------------------------------------
55
/*-----------------------------------------------------------------------------
47
 * Main Program
56
 * Main Program
48
 *---------------------------------------------------------------------------*/
57
 *---------------------------------------------------------------------------*/
49
int main(void) {
58
int main(void) {
50
    Timebase_Init();
59
    Timebase_Init();
51
    Serial_Init();
60
    Serial_Init();
-
 
61
    Can_Init();
52
 
62
 
53
//Setting up OC1A, 16 bit counter used as 8 bit.
63
//Setting up OC1A, 16 bit counter used as 8 bit.
54
    /* Use OC1A */
64
    /* Use OC1A */
55
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
65
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
56
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */
66
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */
57
    ICR1 = 256; //8 bit precision to match the other two counters.
67
    ICR1 = 256; //8 bit precision to match the other two counters.
58
    OCR1A = 128;  //50% as standard
68
    OCR1A = 128;  //50% as standard
59
    DDRB |= (1<<PB1); /* Enable pin as output */
69
    DDRB |= (1<<PB1); /* Enable pin as output */
60
 
70
 
61
//Initialize variables
71
//Initialize variables
62
    destRed = 128;
-
 
63
    fadeSpeed = 10;
72
    fadeSpeed = 20;
64
    uint32_t timeStamp = 0;
73
    timeStamp = 0;
65
 
74
 
66
    Can_Message_t rxMsg;
75
    Can_Message_t rxMsg;
-
 
76
   
-
 
77
    //Init HSL
-
 
78
    currH = 0;
-
 
79
    currS = 1;
-
 
80
    currL = 0.5;
67
 
81
 
68
    sei(); 
82
    sei(); 
69
 
83
 
70
    /* main loop */
84
    /* main loop */
71
    while (1) {
85
    while (1) {
72
        /* service the CAN routines */
86
        /* service the CAN routines */
73
        Can_Service();
87
        Can_Service();
74
       
88
 
75
        /* Time to see if we want to update our values */
89
        /* Time to see if we want to update our values */
76
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
90
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
77
            timeStamp = Timebase_CurrentTime();
91
            timeStamp = Timebase_CurrentTime();
-
 
92
            currH = currH + 0.01;
-
 
93
            if (currH > 1) currH = 0;
78
            updateLED();
94
            updateLED();
79
        }
-
 
80
 
-
 
81
 
-
 
82
        /* This code really needs cleaning up. Support for fade-programs */
-
 
83
        /* Maybe create a struct for one program-step? TargetRED, TargetGREEN, TargetBLUE, Speed, NextAction? */
-
 
84
        if (program != 0){
-
 
85
            switch(program){
-
 
86
                case 1:
-
 
87
                    if (destRed != 255){
-
 
88
                        destRed = destRed + 1;
-
 
89
                    }
-
 
90
                    break;
-
 
91
                case 2:
-
 
92
                    if (destRed != 0){
-
 
93
                        destRed = destRed - 1;
-
 
94
                    }
-
 
95
                    break;
-
 
96
                case 3:
-
 
97
                    switch(programstate){
-
 
98
                        case 0:
-
 
99
                            destRed = 0;
-
 
100
                            fadeSpeed = 10;
-
 
101
                            if (currRed == 0){
-
 
102
                                programstate = 1;
-
 
103
                            }
-
 
104
                        break;
-
 
105
                        case 1:
-
 
106
                            destRed = 255;
-
 
107
                            fadeSpeed = 100;
-
 
108
                            if (currRed == 255){
-
 
109
                                programstate = 2;
-
 
110
                            }
-
 
111
                        break;
-
 
112
                        case 2:
-
 
113
                            destRed = 60;
-
 
114
                            fadeSpeed = 5;
-
 
115
                            if (currRed == 60){
-
 
116
                                programstate = 3;
-
 
117
                            }
-
 
118
                        break;
-
 
119
                        case 3:
-
 
120
                            destRed = 170;
-
 
121
                            fadeSpeed = 20;
-
 
122
                            if (currRed == 170){
-
 
123
                                programstate = 4;
-
 
124
                            }
-
 
125
                        break;
-
 
126
                        case 4:
-
 
127
                            destRed = 0;
-
 
128
                            fadeSpeed = 40;
-
 
129
                            if (currRed == 40){
-
 
130
                                programstate = 0;
-
 
131
                                program = 0;
-
 
132
                            }
-
 
133
                        break;
-
 
134
                    }
-
 
135
                    break;
-
 
136
            }
-
 
137
        }
95
        }
138
 
96
 
139
 
97
 
140
       
-
 
141
        /* check if any messages have been received */
98
        /* check if any messages have been received */
142
        /* A lot of remote control crap going on here now for testing */
99
        /* A lot of remote control crap going on here now for testing */
143
        while (Can_Receive(&rxMsg) == CAN_OK) {
100
        while (Can_Receive(&rxMsg) == CAN_OK) {
144
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
101
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
145
                program = 1;
102
                program = 1;
146
                fadeSpeed = 0;
103
                fadeSpeed = 0;
147
            }
104
            }
148
 
105
 
149
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
106
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
150
                program = 2;
107
                program = 2;
151
                fadeSpeed = 0;
108
                fadeSpeed = 0;
152
            }
-
 
153
 
-
 
154
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x20)){
-
 
155
                program = 0;
-
 
156
 
-
 
157
            }
-
 
158
 
-
 
159
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x21)){
-
 
160
                program = 0;
-
 
161
            }
109
            }
162
 
-
 
163
 
-
 
164
            //Ready-made programs!
-
 
165
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x01)){
-
 
166
                destRed = 255;
-
 
167
            }
110
        }
168
 
111
 
169
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x02)){
-
 
170
                destRed = 0;
-
 
171
            }
-
 
172
 
112
 
173
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x04)){
-
 
174
                destRed = 0;
-
 
175
                fadeSpeed = 0;
-
 
176
            }
113
    }
177
 
114
   
178
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x05)){
-
 
179
                destRed = 255;
115
    return 0;
180
                fadeSpeed = 10;
-
 
181
            }
116
}
182
 
117
 
183
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x07)){
-
 
184
                program = 3;
-
 
185
                programstate = 0;
-
 
186
            }
118
 
187
 
119
 
188
        }
120
 
189
 
121
 
-
 
122
void updateLED(){
190
 
123
 
191
    }
124
 
-
 
125
if ( currS == 0 )                       //HSL values = 0 ÷ 1
192
   
126
{
-
 
127
   R = currL * 255;                      //RGB results = 0 ÷ 255
-
 
128
   G = currL * 255;
193
    return 0;
129
   B = currL * 255;
194
}
130
}
-
 
131
else
-
 
132
{
-
 
133
   double var_1;
-
 
134
   double var_2;
-
 
135
   if ( currL < 0.5 ) var_2 = currL * ( 1 + currS );
-
 
136
   else           var_2 = ( currL + currS ) - ( currS * currL );
195
 
137
 
-
 
138
   var_1 = 2 * currL - var_2;
196
 
139
 
-
 
140
   R = Hue_2_RGB( var_1, var_2, currH + ( 1 / 3 ) );
-
 
141
   G = Hue_2_RGB( var_1, var_2, currH );
-
 
142
   B = Hue_2_RGB( var_1, var_2, currH - ( 1 / 3 ) );
-
 
143
}
197
 
144
 
198
 
145
 
199
 
146
 
200
void updateLED(){
147
OCR1A = R;
-
 
148
 
201
//Red
149
//Red
202
    if (currRed < destRed){
-
 
203
        currRed = currRed + 1;
-
 
204
        OCR1A = currRed;
-
 
205
    } else if (currRed > destRed){
-
 
206
        currRed = currRed - 1;
-
 
207
        OCR1A = currRed;
-
 
208
    }
-
 
209
//Green
150
//Green
210
//Blue
151
//Blue
211
}
152
}
212
 
153
 
-
 
154
 
-
 
155
uint8_t Hue_2_RGB(double v1, double v2, double vH )             //Function Hue_2_RGB
-
 
156
{
-
 
157
   if ( vH < 0 ) vH += 1;
-
 
158
   if ( vH > 1 ) vH -= 1;
-
 
159
   if ( ( 6 * vH ) < 1 ) return 255*( v1 + ( v2 - v1 ) * 6 * vH );
-
 
160
   if ( ( 2 * vH ) < 1 ) return 255*( v2 );
-
 
161
   if ( ( 3 * vH ) < 2 ) return 255*( v1 + ( v2 - v1 ) * ( ( 2 / 3 ) - vH ) * 6 );
-
 
162
   return 255*( v1 );
-
 
163
}