Subversion Repositories HomeAutomation

Rev

Rev 563 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 563 Rev 584
Line 19... Line 19...
19
#include <avr/interrupt.h>
19
#include <avr/interrupt.h>
20
#include <inttypes.h>
20
#include <inttypes.h>
21
#include <stdio.h>
21
#include <stdio.h>
22
#include <avr/pgmspace.h> //To allow read/write from flash
22
#include <avr/pgmspace.h> //To allow read/write from flash
23
 
23
 
-
 
24
#include <math.h>
24
 
25
 
25
#include <drivers/timer/timebase.h>
26
#include <drivers/timer/timebase.h>
26
 
27
 
27
#include <config.h> // All configuration parameters
28
#include <config.h> // All configuration parameters
28
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
29
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
Line 30... Line 31...
30
#include <string.h> //for memcpy
31
#include <string.h> //for memcpy
31
/* lib files */
32
/* lib files */
32
#include <bios.h>
33
#include <bios.h>
33
//#include <funcdefs/funcdefs.h>
34
//#include <funcdefs/funcdefs.h>
34
 
35
 
35
//#include <settings.h> //Migrated to config.inc
36
//#include <settings.h>
36
 
37
 
37
/*----------------------------------------------------------------------------
38
/*----------------------------------------------------------------------------
38
 * Defines
39
 * Defines
39
 * -------------------------------------------------------------------------*/
40
 * -------------------------------------------------------------------------*/
40
 
41
 
Line 126... Line 127...
126
 
127
 
127
Can_Message_t txMsg;    //Transmit message storage
128
Can_Message_t txMsg;    //Transmit message storage
128
 
129
 
129
volatile Can_Message_t rxMsg; // Message storage
130
volatile Can_Message_t rxMsg; // Message storage
130
volatile uint8_t rxMsgFull;   // Synchronization flag
131
volatile uint8_t rxMsgFull;   // Synchronization flag
-
 
132
 
-
 
133
uint8_t lastr;
-
 
134
uint8_t lastg;
-
 
135
uint8_t lastb;
131
 
136
 
132
/*----------------------------------------------------------------------------
137
/*----------------------------------------------------------------------------
133
 * Functions
138
 * Functions
134
 * -------------------------------------------------------------------------*/
139
 * -------------------------------------------------------------------------*/
135
void updateLED(void);
140
void updateLED(void);
Line 153... Line 158...
153
 *---------------------------------------------------------------------------*/
158
 *---------------------------------------------------------------------------*/
154
int main(void) {
159
int main(void) {
155
 
160
 
156
//Setting up OC1A, 16 bit counter used as 8 bit.
161
//Setting up OC1A, 16 bit counter used as 8 bit.
157
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
162
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
158
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
163
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11)| (1<<CS10); /* Timer uses main system clock with ? prescale */
159
    ICR1 = 256; //8 bit precision to match the other two counters.
164
    ICR1 = 256; //8 bit precision to match the other two counters.
160
    OCR1A = 0;  //0% as standard
165
    OCR1A = 0;  //0% as standard
161
    DDRB |= (1<<PB1); /* Enable pin as output */
166
    DDRB |= (1<<PB1); /* Enable pin as output */
162
//Setting up OC0A
167
//Setting up OC0A
163
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
168
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
164
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
169
    TCCR0B |= (1<<CS01)|  (1<<CS00); /* Prescaler updating now */
165
    OCR0A = 0;  //0% standard
170
    OCR0A = 0;  //0% standard
166
    DDRD |= (1<<PD6);
171
    DDRD |= (1<<PD6);
167
//Setting up OC0B
172
//Setting up OC0B
168
    TCCR0A |= (1<<COM0B1);
173
    TCCR0A |= (1<<COM0B1);
169
    OCR0B = 0;
174
    OCR0B = 0;
170
    DDRD |= (1<<PD5);
175
    DDRD |= (1<<PD5);
171
 
176
 
172
//Initialize variables
177
//Initialize variables
173
    fadeSpeed = 10;
178
    fadeSpeed = 40;
174
    dimSpeed = 10;
179
    dimSpeed = 10;
175
    timeStamp = 0;
180
    timeStamp = 0;
176
    dimStamp = 0;
181
    dimStamp = 0;
177
 
182
 
178
    reformatRGB(0x00,0x00,0x00,1);
183
    reformatRGB(0x00,0x00,0x00,1);
179
    destColor.Intens = 50;
184
    destColor.Intens = 0xa0;
180
    currColor = destColor;
185
    currColor = destColor;
181
 
186
 
182
//Just temporary for now, they keep track of some kind of demo-program.
187
//Just temporary for now, they keep track of some kind of demo-program.
183
    currProgram = 1;
188
    currProgram = 1;
184
    programMode = 0;
189
    programMode = 0;
185
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
190
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
186
 
191
 
187
    sei();
192
    sei();
188
 
193
 
189
    BIOS_CanCallback = &can_receive;
194
    BIOS_CanCallback = &can_receive;
-
 
195
 
190
 
196
 
191
//Send a message that we are alive!
197
//Send a message that we are alive!
192
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
198
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
193
    txMsg.DataLength = 4;
199
    txMsg.DataLength = 4;
194
    txMsg.RemoteFlag = 0;
200
    txMsg.RemoteFlag = 0;
Line 264... Line 270...
264
                rgbledid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
270
                rgbledid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
265
               
271
               
266
                if ((acttype == ACT_TYPE_RGBLED) && (rgbledid == THISRGBLEDID)) {
272
                if ((acttype == ACT_TYPE_RGBLED) && (rgbledid == THISRGBLEDID)) {                                                          
267
                    // This is a message for this rgbled
273
                    // This is a message for this rgbled
268
                    if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
274
                    if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
269
                        /* Set a program! */
275
                        /* Set a program! Untested! */
270
                        currProgram = rxMsg.Data.bytes[1];
276
                        currProgram = rxMsg.Data.bytes[1];
271
                        temp_adress = pgm_read_word(&programs[currProgram-1]);             
277
                        temp_adress = pgm_read_word(&programs[currProgram-1]);             
272
                    } else if ( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
278
                    } else if ( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
273
                        /* Set intensity! */
279
                        /* Set intensity! Works!*/
274
                        if (rxMsg.Data.bytes[1] == TRUE){
280
                        if (rxMsg.Data.bytes[1] == TRUE){
275
                            currColor.Intens = rxMsg.Data.bytes[2];                
281
                            currColor.Intens = rxMsg.Data.bytes[2];                
276
                        }
282
                        }
277
                        destColor.Intens = rxMsg.Data.bytes[2];
283
                        destColor.Intens = rxMsg.Data.bytes[2];
278
                        if (rxMsg.Data.bytes[3] != 0){
284
                        if (rxMsg.Data.bytes[3] != 0){
Line 317... Line 323...
317
 *     TODO: Get rid of the tempcontainer!                                                      *
323
 *     TODO: Get rid of the tempcontainer!                                                      *
318
 ************************************************************************************************/
324
 ************************************************************************************************/
319
void updateLED(){
325
void updateLED(){
320
    uint8_t tempcontainer;
326
    uint8_t tempcontainer;
321
 
327
   
322
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
328
    tempcontainer = currColor.R * currColor.Intens / 0xff;  //I really want to skip this, don't know how yet though.
323
    OCR1A = tempcontainer; 
329
    OCR1A = tempcontainer;
324
      OCR0A = currColor.G * currColor.Intens / 255;
330
    OCR0A = currColor.G * currColor.Intens / 0xff;
325
    OCR0B = currColor.B * currColor.Intens / 255;      
331
    OCR0B = currColor.B * currColor.Intens / 0xff;
326
 
-
 
327
       
332
   
328
    //Bara för debugcrap
333
    //Bara för debugcrap
329
    Can_Message_t txMsg;
334
    Can_Message_t txMsg;
330
    txMsg.Id = 0x55;
335
    txMsg.Id = 0x55;
331
    txMsg.RemoteFlag = 0;
336
    txMsg.RemoteFlag = 0;
332
    txMsg.ExtendedFlag = 1;
337
    txMsg.ExtendedFlag = 1;
333
    txMsg.DataLength = 4;
338
    txMsg.DataLength = 4;
334
    txMsg.Data.bytes[0] = currColor.R;
339
    txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 0xff;
335
    txMsg.Data.bytes[1] = currColor.G;
340
    txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 0xff;
336
    txMsg.Data.bytes[2] = currColor.B;
341
    txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 0xff;
337
    txMsg.Data.bytes[3] = currColor.Intens;
342
    txMsg.Data.bytes[3] = currColor.Intens;
338
 
343
 
339
    BIOS_CanSend(&txMsg);
344
    //BIOS_CanSend(&txMsg); //Strictly for debug..
340
 
345
 
341
}
346
}
342
 
347
 
343
 
348
 
344
 
349