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