Subversion Repositories HomeAutomation

Rev

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

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