Subversion Repositories HomeAutomation

Rev

Rev 448 | Rev 481 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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