Subversion Repositories HomeAutomation

Rev

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

Rev 463 Rev 481
Line 12... Line 12...
12
/*-----------------------------------------------------------------------------
12
/*-----------------------------------------------------------------------------
13
 * Includes
13
 * Includes
14
 *---------------------------------------------------------------------------*/
14
 *---------------------------------------------------------------------------*/
15
/* system files */
15
/* system files */
16
#include <avr/io.h>
16
#include <avr/io.h>
-
 
17
#include <inttypes.h>
17
#include <avr/interrupt.h>
18
#include <avr/interrupt.h>
18
#include <stdio.h>
19
#include <stdio.h>
19
/* lib files */
20
/* lib files */
20
#include <bios.h>
21
#include <bios.h>
-
 
22
#include <config.h>
21
#include <lcd_HD44780.h>
23
#include <lcd_HD44780.h>
22
/* funcdefs */
24
/* funcdefs */
23
#include <funcdefs.h>
25
#include <funcdefs/funcdefs.h>
24
 
26
 
25
/*-----------------------------------------------------------------------------
27
/*-----------------------------------------------------------------------------
26
 * Defines
28
 * Defines
27
 *---------------------------------------------------------------------------*/
29
 *---------------------------------------------------------------------------*/
28
#define SIZE_X  20
30
#define SIZE_X  20
Line 34... Line 36...
34
#define FALSE 0
36
#define FALSE 0
35
 
37
 
36
#define LEFT    0x01
38
#define LEFT    0x01
37
#define RIGHT   0x02
39
#define RIGHT   0x02
38
 
40
 
39
Can_Message_t txMsg;
41
volatile Can_Message_t rxMsg;
40
 
42
 
41
char incoming_text[ BUFFER_SIZE ];
43
char incoming_text[ BUFFER_SIZE ];
42
uint8_t msg_received = FALSE;
44
uint8_t rxMsgFull;
43
 
45
 
44
uint8_t rot_lastdir = 0,
-
 
45
        rot_laststate = 0;
-
 
46
 
46
 
47
char buffer[ BUFFER_SIZE ];
47
char buffer[ BUFFER_SIZE ];
48
 
48
 
49
void send_dimensions();
49
void send_dimensions();
-
 
50
void rotenc();
50
 
51
 
-
 
52
// CAN message reception callback
-
 
53
// This function runs with interrupts disabled, keep it as short as possible
51
void can_receive(Can_Message_t *msg){
54
void can_receive(Can_Message_t *msg){
52
Can_Message_t txMsg;
-
 
53
    uint32_t act;
-
 
54
    uint8_t m, act_type, lcd_action, lcd_size;
-
 
55
    if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
-
 
56
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
-
 
57
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
-
 
58
        lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
-
 
59
        lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
-
 
60
    }
-
 
61
 
-
 
62
    if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
-
 
63
        switch( lcd_action ){
-
 
64
            case LCD_ACTION_CLR:
-
 
65
                    // Clear LCD
-
 
66
                    lcd_clrscr();
-
 
67
                break;
-
 
68
            case LCD_ACTION_CURS:
-
 
69
                    // Move cursor
-
 
70
                    lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] );
-
 
71
                break;
-
 
72
            case LCD_ACTION_GET_SIZE:
-
 
73
                    // Request LCD dimension
-
 
74
                    send_dimensions();
-
 
75
                break;
-
 
76
            case LCD_ACTION_TEXT:
-
 
77
                // Print text to LCD
-
 
78
                for(m=0;m<msg->DataLength;m++){
-
 
79
                    lcd_putc( (char)msg->Data.bytes[m] );
-
 
80
                }
-
 
81
                break;
-
 
82
            case LCD_ACTION_SET_CONT:
-
 
83
                // Set contrast
-
 
84
                if(msg->DataLength == 1){
-
 
85
                    OCR0B = msg->Data.bytes[0];
-
 
86
                }
-
 
87
                break;
-
 
88
            case LCD_ACTION_SET_BLIGHT:
-
 
89
                // Set backlight
-
 
90
                if(msg->DataLength == 1){
-
 
91
                    OCR1AL = msg->Data.bytes[0];
-
 
92
                }
-
 
93
                break;
-
 
94
            case LCD_ACTION_GET_CONT:
-
 
95
                // Get contrast
-
 
96
                txMsg.DataLength = 1;
-
 
97
                txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
-
 
98
                txMsg.Data.bytes[0] = OCR0B;
-
 
99
                BIOS_CanSend(&txMsg);
-
 
100
                break;
-
 
101
            case LCD_ACTION_GET_BLIGHT:
-
 
102
                // Get backlight
-
 
103
                txMsg.DataLength = 1;
-
 
104
                txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
-
 
105
                txMsg.Data.bytes[0] = OCR1AL;
-
 
106
                BIOS_CanSend(&txMsg);
-
 
107
                break;
-
 
108
            default:
-
 
109
                // Take over the world
-
 
110
                break;
-
 
111
        }
-
 
112
    }
-
 
113
}
-
 
114
 
-
 
115
ISR( PCINT2_vect ){ // For ROTENC_A and ROTENC_B
-
 
116
/*  uint8_t rot_data = 0;
-
 
117
 
-
 
118
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
-
 
119
    txMsg.DataLength=2;
-
 
120
 
-
 
121
    if(PINB&(1<<PB0)){
-
 
122
        rot_data |= 0x01;
-
 
123
    }
-
 
124
    if(PIND&(1<<PD2)){
55
    if(!rxMsgFull){
125
        rot_data |= 0x02;
-
 
126
    }
-
 
127
 
-
 
128
    if( rot_data==0 || rot_data==3 ){
-
 
129
        if( rot_data==0 && rot_laststate!=rot_data ){
56
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
130
            if( rot_lastdir&0x01 ){
-
 
131
                // Moving right
-
 
132
                txMsg.Data.bytes[0]=RIGHT;
-
 
133
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
134
                BIOS_CanSend(&txMsg);
-
 
135
                // For testing TODO remove
-
 
136
                if(OCR1A<0xFF){
-
 
137
                    OCR1A++;
-
 
138
                }
-
 
139
                if(OCR0B<0xFF){
-
 
140
                    OCR0B++;
-
 
141
                }
-
 
142
            }else{
-
 
143
                // Moving left
-
 
144
                txMsg.Data.bytes[0]=LEFT;
-
 
145
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
146
                BIOS_CanSend(&txMsg);
-
 
147
                // For testing TODO remove
-
 
148
                if(OCR1A>0){
-
 
149
                    OCR1A--;
-
 
150
                }
-
 
151
                if(OCR0B>0){
-
 
152
                    OCR0B--;
-
 
153
                }
-
 
154
            }
-
 
155
        }
-
 
156
        rot_laststate = rot_data;
-
 
157
    }else{
-
 
158
        rot_lastdir = rot_data;
-
 
159
    }*/
-
 
160
    rotenc();
-
 
161
}
-
 
162
 
-
 
163
ISR( PCINT0_vect ){ // For ROTENC_BTN
-
 
164
//  txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x02<<SNS_ID_BITS )| NODE_ID;
-
 
165
//  txMsg.Data.bytes[0] = (PINB&(1<<PB7))?0:1; // Check if buton is pressed or not
-
 
166
//  txMsg.DataLength=1;
-
 
167
//  BIOS_CanSend(&txMsg);
-
 
168
    rotenc();
-
 
169
}
-
 
170
 
-
 
171
void rotenc(){
-
 
172
    uint8_t rot_data = 0;
-
 
173
Can_Message_t txMsg;
-
 
174
    txMsg.ExtendedFlag=1;
-
 
175
    txMsg.RemoteFlag=0;
-
 
176
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
-
 
177
    txMsg.DataLength=2;
-
 
178
 
-
 
179
    if(PINB&(1<<PB0)){
-
 
180
        rot_data |= 0x01;
57
        rxMsgFull = 1;
181
    }
-
 
182
    if(PIND&(1<<PD2)){
-
 
183
        rot_data |= 0x02;
-
 
184
    }
-
 
185
 
-
 
186
    if( rot_data==0 || rot_data==3 ){
-
 
187
        if( rot_data==0 && rot_laststate!=rot_data ){
-
 
188
            if( rot_lastdir&0x01 ){
-
 
189
                // Moving right
-
 
190
                txMsg.Data.bytes[0]=RIGHT;
-
 
191
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
192
    txMsg.DataLength=2;
-
 
193
                BIOS_CanSend(&txMsg);
-
 
194
                // For testing TODO remove
-
 
195
                if(OCR1A<0xFF){
-
 
196
                    OCR1A++;
-
 
197
                }
-
 
198
                if(OCR0B<0xFF){
-
 
199
                    OCR0B++;
-
 
200
                }
-
 
201
            }else{
-
 
202
                // Moving left
-
 
203
                txMsg.Data.bytes[0]=LEFT;
-
 
204
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
205
    txMsg.DataLength=2;
-
 
206
                BIOS_CanSend(&txMsg);
-
 
207
                // For testing TODO remove
-
 
208
                if(OCR1A>0){
-
 
209
                    OCR1A--;
-
 
210
                }
-
 
211
                if(OCR0B>0){
-
 
212
                    OCR0B--;
-
 
213
                }
-
 
214
            }
-
 
215
        }
-
 
216
        rot_laststate = rot_data;
-
 
217
    }else{
-
 
218
        rot_lastdir = rot_data;
-
 
219
    }
58
    }
220
}
59
}
221
 
60
 
-
 
61
ISR( PCINT2_vect ){
-
 
62
// For ROTENC_A and ROTENC_B FIXME är inte riktigt rätt
-
 
63
    rotenc();
-
 
64
}
-
 
65
 
-
 
66
ISR( PCINT0_vect ){
-
 
67
// For ROTENC_BTN
-
 
68
rotenc();
-
 
69
}
222
 
70
 
223
/*-----------------------------------------------------------------------------
71
/*-----------------------------------------------------------------------------
224
 * Main Program
72
 * Main Program
225
 *---------------------------------------------------------------------------*/
73
 *---------------------------------------------------------------------------*/
226
int main(void) {
74
int main(void) {
-
 
75
    uint32_t act;
-
 
76
    uint8_t m, act_type, lcd_action, lcd_size;
-
 
77
 
-
 
78
    sei();
227
 
79
 
228
    /*
80
    /*
229
     * Initialize rotaryencoders and buttons
81
     * Initialize rotaryencoders and buttons
230
     */
82
     */
231
    /* Set as input */
83
    // Set as input
232
    DDRD &= ~(1<<DDD2);
84
    DDRD &= ~(1<<DDD2);
233
    DDRB &= ~((1<<DDB7)|(1<<DDB0));
85
    DDRB &= ~((1<<DDB7)|(1<<DDB0));
234
    /* Enable pull-up */
86
    // Enable pull-up
235
    PORTD |= (1<<PD2);
87
    PORTD |= (1<<PD2);
236
    PORTB |= (1<<PB7)|(1<<PB0);
88
    PORTB |= (1<<PB7)|(1<<PB0);
237
    /* Enable IO-pin interrupt */
89
    // Enable IO-pin interrupt
238
    PCICR |= (1<<PCIE2)|(1<<PCIE0);
90
    PCICR |= (1<<PCIE2)|(1<<PCIE0);
239
    /* Unmask PB7, PB0 and PD2 */
91
    // Unmask PB7, PB0 and PD2
240
    PCMSK2 |= (1<<PCINT18);
92
    PCMSK2 |= (1<<PCINT18);
241
    PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
93
    PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
242
 
94
 
243
    /* Contrast */
95
    // Contrast
244
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
96
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
245
    TCCR0B |= (1<<CS00);
97
    TCCR0B |= (1<<CS00);
246
    OCR0B = 0x20;
98
    OCR0B = 0x10;
247
    DDRD |= (1<<DDD5);
99
    DDRD |= (1<<DDD5);
248
 
100
 
249
    /* Backlight */
101
    // Backlight
250
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
102
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
251
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
103
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
252
    ICR1 = 0xFF; // Make it 8 bits
104
    ICR1 = 0xFF; // Make it 8 bits
253
    OCR1A = 0x20;
105
    OCR1A = 0x80;
254
    DDRB |= (1<<DDB1);
106
    DDRB |= (1<<DDB1);
255
 
107
 
256
    Can_Message_t txMsg;
108
    Can_Message_t txMsg;
257
    txMsg.ExtendedFlag=1;
109
    txMsg.ExtendedFlag=1;
258
    txMsg.RemoteFlag=0;
110
    txMsg.RemoteFlag=0;
259
 
-
 
260
    sei();
111
// FIXME skicka app startad
261
    BIOS_CanCallback = &can_receive;
112
    BIOS_CanCallback = &can_receive;
262
 
113
 
263
    lcd_init( LCD_DISP_ON );
114
    lcd_init( LCD_DISP_ON );
264
    lcd_clrscr();
115
    lcd_clrscr();
265
    lcd_puts("HomeAutomation\n");
116
    lcd_puts("HomeAutomation\n");
Line 268... Line 119...
268
    lcd_puts("Very nice!\n");
119
    lcd_puts("Very nice!\n");
269
 
120
 
270
    /* main loop */
121
    /* main loop */
271
    while(1){
122
    while(1){
272
        /* check if any messages have been received */
123
        /* check if any messages have been received */
273
        if(msg_received){
124
        if(rxMsgFull){
274
 
125
 
-
 
126
            if( ((rxMsg.Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){ // FIXME ett jäkla herk, städa upp
-
 
127
                act = (rxMsg.Id & TYPE_MASK) >> TYPE_MASK_BITS;
-
 
128
                act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
-
 
129
                lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
-
 
130
                lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
-
 
131
 
-
 
132
                if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
-
 
133
                    switch( lcd_action ){
-
 
134
                    case LCD_ACTION_CLR:
-
 
135
                        // Clear LCD
-
 
136
                        lcd_clrscr();
-
 
137
                    break;
-
 
138
                    case LCD_ACTION_CURS:
-
 
139
                        // Move cursor
-
 
140
                        lcd_gotoxy( rxMsg.Data.bytes[0],rxMsg.Data.bytes[1] );
-
 
141
                    break;
-
 
142
                    case LCD_ACTION_GET_SIZE:
-
 
143
                        // Request LCD dimension
-
 
144
                        send_dimensions();
-
 
145
                    break;
-
 
146
                    case LCD_ACTION_TEXT:
-
 
147
                        // Print text to LCD
-
 
148
                        for(m=0;m<rxMsg.DataLength;m++){
-
 
149
                            lcd_putc( (char)rxMsg.Data.bytes[m] );
-
 
150
                        }
-
 
151
                    break;
-
 
152
                    case LCD_ACTION_SET_CONT:
-
 
153
                        // Set contrast
-
 
154
                        if(rxMsg.DataLength == 1){
-
 
155
                            OCR0B = rxMsg.Data.bytes[0];
-
 
156
                        }
-
 
157
                    break;
-
 
158
                    case LCD_ACTION_SET_BLIGHT:
-
 
159
                        // Set backlight
-
 
160
                        if(rxMsg.DataLength == 1){
-
 
161
                            OCR1AL = rxMsg.Data.bytes[0];
-
 
162
                        }
-
 
163
                    break;
-
 
164
                    case LCD_ACTION_GET_CONT:
-
 
165
                        // Get contrast
-
 
166
                        txMsg.DataLength = 1;
-
 
167
                        txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
-
 
168
                        txMsg.Data.bytes[0] = OCR0B;
-
 
169
                        BIOS_CanSend(&txMsg);
-
 
170
                    break;
-
 
171
                    case LCD_ACTION_GET_BLIGHT:
-
 
172
                        // Get backlight
-
 
173
                        txMsg.DataLength = 1;
-
 
174
                        txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
-
 
175
                        txMsg.Data.bytes[0] = OCR1AL;
-
 
176
                        BIOS_CanSend(&txMsg);
-
 
177
                    break;
-
 
178
                    default:
275
            msg_received = FALSE;
179
                    // Take over the world
-
 
180
                    break;
-
 
181
                    }
-
 
182
                }
-
 
183
            }
-
 
184
            rxMsgFull = 0;
276
        }
185
        }
277
        lcd_puts("test");
-
 
278
    }
186
    }
279
}
187
}
280
 
188
 
281
void send_dimensions(){
189
void send_dimensions(){
282
    Can_Message_t txMsg;
190
    Can_Message_t txMsg;
-
 
191
 
283
    txMsg.ExtendedFlag=1;
192
    txMsg.ExtendedFlag=1;
284
    txMsg.RemoteFlag=0;
193
    txMsg.RemoteFlag=0;
285
    txMsg.Id=0xA000001;
194
    txMsg.Id=0xA000001;
286
    txMsg.Data.bytes[0] = SIZE_X;
195
    txMsg.Data.bytes[0] = SIZE_X;
287
    txMsg.Data.bytes[1] = SIZE_Y;
196
    txMsg.Data.bytes[1] = SIZE_Y;
288
    txMsg.DataLength = 2;
197
    txMsg.DataLength = 2;
-
 
198
 
289
    BIOS_CanSend(&txMsg);
199
    BIOS_CanSend(&txMsg);
290
}
200
}
-
 
201
 
-
 
202
void rotenc(){
-
 
203
    uint8_t rot_data = 0;
-
 
204
    static uint8_t rot_lastdir = 0, rot_laststate = 0; // FIXME måste flyttas ut igen?
-
 
205
 
-
 
206
    Can_Message_t txMsg;
-
 
207
    txMsg.ExtendedFlag=1;
-
 
208
    txMsg.RemoteFlag=0;
-
 
209
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
-
 
210
    txMsg.DataLength=2;
-
 
211
 
-
 
212
    if(PINB&(1<<PB0)){
-
 
213
        rot_data |= 0x01;
-
 
214
    }
-
 
215
    if(PIND&(1<<PD2)){
-
 
216
        rot_data |= 0x02;
-
 
217
    }
-
 
218
 
-
 
219
    if( rot_data==0 || rot_data==3 ){
-
 
220
        if( rot_data==0 && rot_laststate!=rot_data ){
-
 
221
            if( rot_lastdir&0x01 ){
-
 
222
                // Moving right
-
 
223
                txMsg.Data.bytes[0]=RIGHT;
-
 
224
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
225
                txMsg.DataLength=2;
-
 
226
                BIOS_CanSend(&txMsg);
-
 
227
                // For testing TODO remove
-
 
228
                if(OCR1A<0xFF){
-
 
229
                    OCR1A++;
-
 
230
                }
-
 
231
                if(OCR0B>0){
-
 
232
                    OCR0B--;
-
 
233
                }
-
 
234
            }else{
-
 
235
                // Moving left
-
 
236
                txMsg.Data.bytes[0]=LEFT;
-
 
237
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
238
                txMsg.DataLength=2;
-
 
239
                BIOS_CanSend(&txMsg);
-
 
240
                // For testing TODO remove
-
 
241
                if(OCR1A>0){
-
 
242
                    OCR1A--;
-
 
243
                }
-
 
244
                if(OCR0B<0xFF){
-
 
245
                    OCR0B++;
-
 
246
                }
-
 
247
            }
-
 
248
        }
-
 
249
        rot_laststate = rot_data;
-
 
250
    }else{
-
 
251
        rot_lastdir = rot_data;
-
 
252
    }
-
 
253
}
-
 
254