Subversion Repositories HomeAutomation

Rev

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

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