Subversion Repositories HomeAutomation

Rev

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

Rev 256 Rev 332
Line 8... Line 8...
8
 *
8
 *
9
 * TODO Fix more types of output: relay status, mail recieved etc.
9
 * TODO Fix more types of output: relay status, mail recieved etc.
10
 * Make it configurable to use UART, because it's not necessary on the final PCB
10
 * Make it configurable to use UART, because it's not necessary on the final PCB
11
 *
11
 *
12
 */
12
 */
13
 
-
 
14
#define DEBUG 0
-
 
15
 
13
 
16
/*-----------------------------------------------------------------------------
14
/*-----------------------------------------------------------------------------
17
 * Includes
15
 * Includes
18
 *---------------------------------------------------------------------------*/
16
 *---------------------------------------------------------------------------*/
19
/* system files */
17
/* system files */
20
#include <avr/io.h>
18
#include <avr/io.h>
21
#include <avr/interrupt.h>
19
#include <avr/interrupt.h>
22
#include <avr/wdt.h>
-
 
23
#include <stdio.h>
20
#include <stdio.h>
24
/* lib files */
21
/* lib files */
25
#include <can.h>
22
#include <bios.h>
26
#include <serial.h>
-
 
27
#include <timebase.h>
-
 
28
#include <lcd_HD44780.h>
23
#include <lcd_HD44780.h>
29
#include <clcd20x4.h>
24
#include <clcd20x4.h>
30
/* funcdefs */
25
/* funcdefs */
31
#include <global_funcdefs.h>
26
//#include <global_funcdefs.h>
32
#include <eqlazer_funcdefs.h>
27
#include <eqlazer_funcdefs.h>
33
 
28
 
34
/* defines */
29
/* defines */
35
#define BUFFER_SIZE 20
30
#define BUFFER_SIZE 20
36
 
-
 
37
#define LCD_CMD_GET 0x1600
31
#define BOUNCE_TIME 10
38
#define VIEW_LEFT 0x01
32
#define TRUE 1
39
#define VIEW_RIGHT 0x02
33
#define FALSE 0
-
 
34
 
-
 
35
uint8_t currentView = MAIN_VIEW, msg_received = FALSE, encoderPosition, rot = FALSE;
40
#define VIEW_EDIT 0x03
36
Can_Message_t rxMsg;
-
 
37
uint8_t temperatureData[8], //servoPosition[4], relayStatus[4], dimmerStatus[4],
-
 
38
            temperatureLength=8;//, servoLength=0, relayLength=0;
-
 
39
uint32_t rot_timeStamp;
41
 
40
 
-
 
41
ISR( PCINT2_vect ){
42
 
42
 
43
uint8_t currentView = MAIN_VIEW;
43
    rot_timeStamp = bios->timebase_get();
-
 
44
    rot = TRUE;
-
 
45
    /* stäng av interrupt på pinnarna */
-
 
46
    PCICR &= ~(1<<PCIE2);
-
 
47
}
44
 
48
 
-
 
49
void can_receive(Can_Message_t *msg){
-
 
50
    uint8_t temp;
-
 
51
    /* Check for incoming messages from IR-receiver node */
-
 
52
    if( CLASS_SNS == ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) ){
-
 
53
        /* FIXME detta är väldigt fult och borde fixas
45
// TODO ISR() för att använda optoencoders
54
         * knapp 4 på fjärren bläddrar åt vänster
46
ISR( PCINT2_vect ){
55
         * knapp 6 åt höger
-
 
56
         */
-
 
57
        if( SNS_IR_RECEIVE == (msg->Id & TYPE_MASK)>>TYPE_MASK_BITS){
-
 
58
            if(msg->Data.bytes[0] == 0 && msg->Data.bytes[1] == 0 && msg->Data.bytes[2] == 0){
-
 
59
                if(msg->Data.bytes[3] == 4 && currentView>0){
-
 
60
                    currentView--;
47
    // TODO do the magic stuff
61
                    msg_received = TRUE;
-
 
62
                }else if(msg->Data.bytes[3] == 6 && currentView<LAST_VIEW){
48
    // vriden medurs currentView++
63
                    currentView++;
49
    // moturs currentView--
64
                    msg_received = TRUE;
-
 
65
                }
-
 
66
            }
-
 
67
        }
50
} // TODO lägga denna koden i ett eget lib?
68
        /* Other sensor messages should be placed here */
-
 
69
 
-
 
70
        /* DS18S20 sensors */
-
 
71
        if( SNS_TEMP_DS18S20 == (msg->Id & SNS_TYPE_MASK)>>SNS_TYPE_BITS ){
-
 
72
            temp = (msg->Id & SNS_ID_MASK)>>SNS_ID_BITS;
-
 
73
            temperatureData[ temp ] = msg->Data.bytes[0];
-
 
74
            temperatureData[ temp+1 ] = msg->Data.bytes[1];
-
 
75
//          if(temp == 1){
-
 
76
//              lcd_puts("ute ");
-
 
77
//          }
-
 
78
        }
-
 
79
    }
-
 
80
}
-
 
81
 
51
 
82
 
52
/*-----------------------------------------------------------------------------
83
/*-----------------------------------------------------------------------------
53
 * Main Program
84
 * Main Program
54
 *---------------------------------------------------------------------------*/
85
 *---------------------------------------------------------------------------*/
55
int main(void) {
86
int main(void) {
56
 
87
 
57
    char buffer[ BUFFER_SIZE ];
88
    char buffer[ BUFFER_SIZE ];
-
 
89
//  uint8_t a=0, b=0;
58
    uint8_t m, temperatureData[8], servoPosition[4], relayStatus[4], dimmerStatus[4],
90
/*  uint8_t m, temperatureData[8], servoPosition[4], relayStatus[4], dimmerStatus[4],
59
            temperatureLength, servoLength;
91
            temperatureLength, servoLength, relayLength;
60
 
92
*/
61
    /*
93
    /*
62
     * Initialize optoencoders and buttons
94
     * Initialize rotaryencoders and buttons
63
     */
95
     */
64
    /* Set as input and enable pull-up */ // TODO fixa rätt ingångar
96
    /* Set as input and enable pull-up */
65
    DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
97
    DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
66
    PORTD |= (1<<PD6)|(1<<PD7);
98
//    PORTD |= (1<<PD6)|(1<<PD7);
67
    /* Enable IO-pin interrupt */
99
    /* Enable IO-pin interrupt */
68
    PCICR |= (1<<PCIE1);
100
//    PCICR |= (1<<PCIE2);
69
    /* Unmask PD6 and PD7 */
101
    /* Unmask PD6 and PD7 */
70
    PCMSK2 |= (1<<PCINT22)|(1<<PCINT23);
102
    PCMSK2 |= (1<<PCINT22)|(1<<PCINT23);
71
 
103
 
72
 
-
 
73
    Timebase_Init();
-
 
74
#if DEBUG
-
 
75
    Serial_Init();
-
 
76
#endif
-
 
77
    sei();
104
    sei();
-
 
105
    bios->can_callback = &can_receive;
-
 
106
 
78
    lcd_init( LCD_DISP_ON );
107
    lcd_init( LCD_DISP_ON );
79
    lcd_clrscr();
108
    lcd_clrscr();
80
    lcd_puts("CAN LCD test\n");
109
//    lcd_puts("CAN LCD\n");
81
    lcd_puts("CanInit... ");
-
 
82
    if (Can_Init() != CAN_OK) {
-
 
83
        lcd_puts("FAILED!\n");
-
 
84
    }else{
-
 
85
        lcd_puts("OK!\n");
-
 
86
    }
-
 
87
 
-
 
88
    uint32_t timeStamp = 0;
-
 
89
 
-
 
90
    Can_Message_t txMsg;
-
 
91
    Can_Message_t rxMsg;
-
 
92
    txMsg.Id = 0;
-
 
93
    txMsg.RemoteFlag = 0;
-
 
94
    txMsg.ExtendedFlag = 1;
-
 
95
 
110
 
96
    lcd_clrscr();
111
    lcd_clrscr();
97
    printLCDview( MAIN_VIEW );
112
    printLCDview( MAIN_VIEW );
98
 
113
 
99
    /* main loop */
114
    /* main loop */
100
    while (1) {
115
    while(1){
101
        /* service the CAN routines */
-
 
102
        Can_Service();
-
 
103
 
-
 
104
        /* check if any messages have been received and print to uart */
116
        /* check if any messages have been received */
105
        while (Can_Receive(&rxMsg) == CAN_OK) {
-
 
106
#if DEBUG
-
 
107
            printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
-
 
108
            printf("data={ ");
-
 
109
 
-
 
110
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
-
 
111
                printf("%x ", rxMsg.Data.bytes[i]);
-
 
112
            }
-
 
113
            printf("}\n");
-
 
114
#endif
-
 
115
            /* Get data from bus and store */
-
 
116
            if( rxMsg.Id == 0x1502 ){
-
 
117
                /* Temperature sensor data */
-
 
118
                for(m=0; m<rxMsg.DataLength; m++){
-
 
119
                    temperatureData[m] = rxMsg.Data.bytes[m];
-
 
120
                    temperatureLength = rxMsg.DataLength;
-
 
121
                }
-
 
122
            }else if( rxMsg.Id == 0x1201 ){
-
 
123
                /* Relay status (ON/OFF) */
-
 
124
                relayStatus[1] = rxMsg.Data.bytes[2];
-
 
125
            }else if( rxMsg.Id == 0x1301){
-
 
126
                /* Servo and blinds status */
-
 
127
                for( m=3; m<rxMsg.DataLength; m++ ){
-
 
128
                    servoPosition[m-3] = rxMsg.Data.bytes[m];
-
 
129
                    servoLength=rxMsg.DataLength-3;
-
 
130
                }
-
 
131
            }
-
 
132
            /* TODO Add extra messages here */
-
 
133
 
-
 
134
            /* Incoming command to change view */
-
 
135
            if(rxMsg.Id == LCD_CMD_GET){
-
 
136
                if(rxMsg.Data.bytes[0]== VIEW_LEFT){
-
 
137
                    if(currentView>MAIN_VIEW){
-
 
138
                        currentView--;
-
 
139
                    }
-
 
140
                }else if(rxMsg.Data.bytes[0]== VIEW_RIGHT){
-
 
141
                    if(currentView<SERVO_VIEW){ /* TODO increase value if you add views */
-
 
142
                        currentView++;
-
 
143
                    }
-
 
144
                }
117
        if(msg_received){
145
 
-
 
146
                /* Print views skeleton and if existing its data */
118
                /* Print views skeleton and if existing its data */
147
                printLCDview( currentView );
119
                printLCDview( currentView );
148
                if(currentView == TEMPSENS_VIEW){
120
                if(currentView == TEMPSENS_VIEW){
149
                    printLCDviewData( TEMPSENS_VIEW, temperatureData, temperatureLength);
121
                    printLCDviewData( TEMPSENS_VIEW, temperatureData, temperatureLength);
150
                }else if(currentView == SERVO_VIEW){
122
            //    }else if(currentView == SERVO_VIEW){
151
                    printLCDviewData( SERVO_VIEW, servoPosition, servoLength);
123
            //        printLCDviewData( SERVO_VIEW, servoPosition, servoLength);
-
 
124
                }
-
 
125
                msg_received = FALSE;
152
                }
126
            }
153
            }
-
 
154
        }
-
 
155
 
127
 
156
        // TODO använd optoencoders för att växla view
128
        // TODO använd rotary encoders för att växla view
157
        // också med fjärrkontroll
129
        // också med fjärrkontroll
-
 
130
/*      if((bios->timebase_get() - rot_timeStamp >= BOUNCE_TIME) && rot == TRUE){
-
 
131
            PCICR |= (1<<PCIE2);
-
 
132
            rot = FALSE;
158
 
133
        }*/
159
    }
134
    }
160
 
135
 
161
 
136
 
162
}
137
}