Subversion Repositories HomeAutomation

Rev

Rev 349 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 349 Rev 350
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
    uint32_t act;
52
    uint32_t act;
53
    uint8_t m, act_type, lcd_action, lcd_size;
53
    uint8_t m, act_type, lcd_action, lcd_size;
54
    if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
54
    if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
55
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
55
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
56
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
56
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
57
        lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
57
        lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
58
        lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
58
        lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
59
    }
59
    }
60
 
60
 
61
    if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
61
    if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
62
        switch( lcd_action ){
62
        switch( lcd_action ){
63
            case LCD_ACTION_CLR:
63
            case LCD_ACTION_CLR:
64
                    // Clear LCD
64
                    // Clear LCD
65
                    lcd_clrscr();
65
                    lcd_clrscr();
66
                break;
66
                break;
67
            case LCD_ACTION_CURS:
67
            case LCD_ACTION_CURS:
68
                    // Move cursor
68
                    // Move cursor
69
                    lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] );
69
                    lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] );
70
                break;
70
                break;
71
            case LCD_ACTION_GET_SIZE:
71
            case LCD_ACTION_GET_SIZE:
72
                    // Request LCD dimension
72
                    // Request LCD dimension
73
                    send_dimensions();
73
                    send_dimensions();
74
                break;
74
                break;
75
            case LCD_ACTION_TEXT:
75
            case LCD_ACTION_TEXT:
76
                // Print text to LCD
76
                // Print text to LCD
77
                for(m=0;m<msg->DataLength;m++){
77
                for(m=0;m<msg->DataLength;m++){
78
                    lcd_putc( (char)msg->Data.bytes[m] );
78
                    lcd_putc( (char)msg->Data.bytes[m] );
79
                }
79
                }
80
                break;
80
                break;
81
            case LCD_ACTION_SET_CONT:
81
            case LCD_ACTION_SET_CONT:
82
                // Set contrast
82
                // Set contrast
83
                if(msg->DataLength == 1){
83
                if(msg->DataLength == 1){
84
                    OCR0B = msg->Data.bytes[0];
84
                    OCR0B = msg->Data.bytes[0];
85
                }
85
                }
86
                break;
86
                break;
87
            case LCD_ACTION_SET_BLIGHT:
87
            case LCD_ACTION_SET_BLIGHT:
88
                // Set backlight
88
                // Set backlight
89
                if(msg->DataLength == 1){
89
                if(msg->DataLength == 1){
90
                    OCR1AL = msg->Data.bytes[0];
90
                    OCR1AL = msg->Data.bytes[0];
91
                }
91
                }
92
                break;
92
                break;
93
            case LCD_ACTION_GET_CONT:
93
            case LCD_ACTION_GET_CONT:
94
                // Get contrast
94
                // Get contrast
95
                txMsg.DataLength = 1;
95
                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;
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.Data.bytes[0] = OCR0B;
97
                txMsg.Data.bytes[0] = OCR0B;
98
                bios->can_send(&txMsg);
98
                bios->can_send(&txMsg);
99
                break;
99
                break;
100
            case LCD_ACTION_GET_BLIGHT:
100
            case LCD_ACTION_GET_BLIGHT:
101
                // Get backlight
101
                // Get backlight
102
                txMsg.DataLength = 1;
102
                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;
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.Data.bytes[0] = OCR1AL;
104
                txMsg.Data.bytes[0] = OCR1AL;
105
                bios->can_send(&txMsg);
105
                bios->can_send(&txMsg);
106
                break;
106
                break;
107
            default:
107
            default:
108
                // Take over the world
108
                // Take over the world
109
                break;
109
                break;
110
        }
110
        }
111
    }
111
    }
112
}
112
}
113
 
113
 
114
ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B
114
ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B
115
    uint8_t rot_data = 0;
115
    uint8_t rot_data = 0;
116
 
116
 
117
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
117
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
118
    txMsg.DataLength=2;
118
    txMsg.DataLength=2;
119
 
119
 
120
    if(PINB&(1<<PB7)){
120
    if(PINB&(1<<PB7)){
121
        rot_data |= 0x01;
121
        rot_data |= 0x01;
122
    }
122
    }
123
    if(PINB&(1<<PB0)){
123
    if(PINB&(1<<PB0)){
124
        rot_data |= 0x02;
124
        rot_data |= 0x02;
125
    }
125
    }
126
 
126
 
127
    if( rot_data==0 || rot_data==3 ){
127
    if( rot_data==0 || rot_data==3 ){
128
        if( rot_data==0 && rot_laststate!=rot_data ){
128
        if( rot_data==0 && rot_laststate!=rot_data ){
129
            if( rot_lastdir&0x01 ){
129
            if( rot_lastdir&0x01 ){
130
                // Moving right
130
                // Moving right
131
                txMsg.Data.bytes[0]=RIGHT;
131
                txMsg.Data.bytes[0]=RIGHT;
132
                txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
132
                txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
133
                bios->can_send(&txMsg);
133
                bios->can_send(&txMsg);
134
                // For testing TODO remove
134
                // For testing TODO remove
135
                if(OCR1A<0xFF){
135
                if(OCR1A<0xFF){
136
                    OCR1A++;
136
                    OCR1A++;
137
                }
137
                }
138
            }else{
138
            }else{
139
                // Moving left
139
                // Moving left
140
                txMsg.Data.bytes[0]=LEFT;
140
                txMsg.Data.bytes[0]=LEFT;
141
                txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
141
                txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
142
                bios->can_send(&txMsg);
142
                bios->can_send(&txMsg);
143
                // For testing TODO remove
143
                // For testing TODO remove
144
                if(OCR1A>0){
144
                if(OCR1A>0){
145
                    OCR1A--;
145
                    OCR1A--;
146
                }
146
                }
147
            }
147
            }
148
        }
148
        }
149
        rot_laststate = rot_data;
149
        rot_laststate = rot_data;
150
    }else{
150
    }else{
151
        rot_lastdir = rot_data;
151
        rot_lastdir = rot_data;
152
    }
152
    }
153
}
153
}
154
 
154
 
155
ISR( PCINT2_vect ){ // For ROTENC_BTN
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;
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
157
    txMsg.Data.bytes[0] = (PIND&(1<<PD2))?0:1; // Check if buton is pressed or not
158
    txMsg.DataLength=1;
158
    txMsg.DataLength=1;
159
    bios->can_send(&txMsg);
159
    bios->can_send(&txMsg);
160
}
160
}
161
 
161
 
162
/*-----------------------------------------------------------------------------
162
/*-----------------------------------------------------------------------------
163
 * Main Program
163
 * Main Program
164
 *---------------------------------------------------------------------------*/
164
 *---------------------------------------------------------------------------*/
165
int main(void) {
165
int main(void) {
166
 
166
 
167
    /*
167
    /*
168
     * Initialize rotaryencoders and buttons
168
     * Initialize rotaryencoders and buttons
169
     */
169
     */
170
    /* Set as input */
170
    /* Set as input */
171
    DDRD &= ~(1<<DDD2);
171
    DDRD &= ~(1<<DDD2);
172
    DDRB &= ~((1<<DDB7)|(1<<DDB0));
172
    DDRB &= ~((1<<DDB7)|(1<<DDB0));
173
    /* Enable pull-up */
173
    /* Enable pull-up */
174
    PORTD |= (1<<PD2);
174
    PORTD |= (1<<PD2);
175
    PORTB |= (1<<PB7)|(1<<PB0);
175
    PORTB |= (1<<PB7)|(1<<PB0);
176
    /* Enable IO-pin interrupt */
176
    /* Enable IO-pin interrupt */
177
    PCICR |= (1<<PCIE2)|(1<<PCIE0);
177
    PCICR |= (1<<PCIE2)|(1<<PCIE0);
178
    /* Unmask PB7, PB0 and PD2 */
178
    /* Unmask PB7, PB0 and PD2 */
179
    PCMSK2 |= (1<<PCINT18);
179
    PCMSK2 |= (1<<PCINT18);
180
    PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
180
    PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
181
 
181
 
182
    /* Contrast */
182
    /* Contrast */
183
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
183
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
184
    TCCR0B |= (1<<CS00);
184
    TCCR0B |= (1<<CS00);
185
    OCR0B = 0x20;
185
    OCR0B = 0x20;
186
    DDRD |= (1<<DDD5);
186
    DDRD |= (1<<DDD5);
187
 
187
 
188
    /* Backlight */
188
    /* Backlight */
189
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
189
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
190
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
190
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
191
    ICR1 = 0xFF; // Make it 8 bits
191
    ICR1 = 0xFF; // Make it 8 bits
192
    OCR1A = 0x20;
192
    OCR1A = 0x20;
193
    DDRB |= (1<<DDB1);
193
    DDRB |= (1<<DDB1);
194
 
-
 
195
 
194
 
196
    txMsg.ExtendedFlag=1;
195
    txMsg.ExtendedFlag=1;
197
    txMsg.RemoteFlag=0;
196
    txMsg.RemoteFlag=0;
198
 
197
 
199
    sei();
198
    sei();
200
    bios->can_callback = &can_receive;
199
    bios->can_callback = &can_receive;
201
 
200
 
202
    lcd_init( LCD_DISP_ON );
201
    lcd_init( LCD_DISP_ON );
203
    lcd_clrscr();
202
    lcd_clrscr();
204
    lcd_puts("HomeAutomation\n");
203
    lcd_puts("HomeAutomation\n");
205
 
204
 
206
    send_dimensions();
205
    send_dimensions();
207
    lcd_puts("Very nice!\n");
206
    lcd_puts("Very nice!\n");
208
 
207
 
209
    /* main loop */
208
    /* main loop */
210
    while(1){
209
    while(1){
211
        /* check if any messages have been received */
210
        /* check if any messages have been received */
212
        if(msg_received){
211
        if(msg_received){
213
 
212
 
214
            msg_received = FALSE;
213
            msg_received = FALSE;
215
        }
214
        }
216
    }
215
    }
217
}
216
}
218
 
217
 
219
void send_dimensions(){
218
void send_dimensions(){
220
    txMsg.Id=0xA000001;
219
    txMsg.Id=0xA000001;
221
    txMsg.Data.bytes[0] = SIZE_X;
220
    txMsg.Data.bytes[0] = SIZE_X;
222
    txMsg.Data.bytes[1] = SIZE_Y;
221
    txMsg.Data.bytes[1] = SIZE_Y;
223
    txMsg.DataLength = 2;
222
    txMsg.DataLength = 2;
224
    bios->can_send(&txMsg);
223
    bios->can_send(&txMsg);
225
}
224
}
226
 
225