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