Rev 344 | Rev 349 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 344 | Rev 348 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /** |
1 | /** |
| 2 | * CanCLCD. |
2 | * CanCLCD. Simple version. |
| 3 | * For HD44780- and KS0073-based LCD displays |
3 | * For HD44780- and KS0073-based LCD displays |
| 4 | * Prints sensor data and other interesting things. |
- | |
| 5 | * |
4 | * |
| 6 | * @date 2006-12-11 |
5 | * @date 2006-12-11 |
| 7 | * @author Erik Larsson |
6 | * @author Erik Larsson |
| 8 | * |
- | |
| 9 | * TODO Fix more types of output: relay status, mail recieved etc. |
- | |
| 10 | * |
7 | * |
| 11 | */ |
8 | */ |
| 12 | 9 | ||
| 13 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 14 | * Includes |
11 | * Includes |
| Line 19... | Line 16... | ||
| 19 | #include <stdio.h> |
16 | #include <stdio.h> |
| 20 | /* lib files */ |
17 | /* lib files */ |
| 21 | #include <bios.h> |
18 | #include <bios.h> |
| 22 | #include <lcd_HD44780.h> |
19 | #include <lcd_HD44780.h> |
| 23 | /* funcdefs */ |
20 | /* funcdefs */ |
| 24 |
|
21 | #include <funcdefs.h> |
| 25 | 22 | ||
| 26 | /* defines */ |
23 | /* defines */ |
| 27 | #define SIZE_X 20 |
24 | #define SIZE_X 20 |
| 28 | #define SIZE_Y 4 |
25 | #define SIZE_Y 4 |
| - | 26 | #define SIZE_LCD LCD_SIZE_20x4 |
|
| 29 | 27 | ||
| 30 |
|
28 | #define BUFFER_SIZE SIZE_X |
| 31 | #define TRUE 1 |
29 | #define TRUE 1 |
| 32 | #define FALSE 0 |
30 | #define FALSE 0 |
| 33 | 31 | ||
| 34 | #define LEFT 0x01 |
32 | #define LEFT 0x01 |
| 35 | #define RIGHT 0x02 |
33 | #define RIGHT 0x02 |
| 36 | 34 | ||
| 37 | Can_Message_t rxMsg; |
- | |
| 38 | Can_Message_t txMsg; |
35 | Can_Message_t txMsg; |
| 39 | 36 | ||
| 40 | - | ||
| - | 37 | char incoming_text[ BUFFER_SIZE ]; |
|
| 41 | uint8_t msg_received = FALSE; |
38 | uint8_t msg_received = FALSE; |
| 42 | 39 | ||
| 43 | uint8_t rot_lastdir = 0, |
40 | uint8_t rot_lastdir = 0, |
| 44 | rot_laststate = 0; |
41 | rot_laststate = 0; |
| 45 | 42 | ||
| - | 43 | char buffer[ BUFFER_SIZE ]; |
|
| 46 | 44 | ||
| - | 45 | void send_dimensions(); |
|
| - | 46 | ||
| 47 | void can_receive(Can_Message_t *msg){ |
47 | void can_receive(Can_Message_t *msg){ |
| - | 48 | uint32_t act; |
|
| - | 49 | uint8_t m, act_type, lcd_action, lcd_size; |
|
| - | 50 | if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){ |
|
| - | 51 | act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS; |
|
| - | 52 | act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS; |
|
| - | 53 | lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS; |
|
| - | 54 | lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS; |
|
| - | 55 | } |
|
| - | 56 | ||
| - | 57 | if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){ |
|
| - | 58 | switch( lcd_action ){ |
|
| - | 59 | case LCD_ACTION_CLR: |
|
| - | 60 | // Clear LCD |
|
| - | 61 | lcd_clrscr(); |
|
| - | 62 | break; |
|
| - | 63 | case LCD_ACTION_CURS: |
|
| - | 64 | // Move cursor |
|
| - | 65 | lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] ); |
|
| - | 66 | break; |
|
| - | 67 | case LCD_ACTION_GET_SIZE: |
|
| - | 68 | // Request LCD dimension |
|
| - | 69 | send_dimensions(); |
|
| - | 70 | break; |
|
| - | 71 | case LCD_ACTION_TEXT: |
|
| - | 72 | // Print text to LCD |
|
| - | 73 | for(m=0;m<msg->DataLength;m++){ |
|
| - | 74 | lcd_putc( (char)msg->Data.bytes[m] ); |
|
| - | 75 | } |
|
| - | 76 | break; |
|
| - | 77 | case LCD_ACTION_SET_CONT: |
|
| - | 78 | // Set contrast |
|
| - | 79 | if(msg->DataLength == 1){ |
|
| - | 80 | OCR0B = msg->Data.bytes[0]; |
|
| - | 81 | } |
|
| - | 82 | break; |
|
| - | 83 | case LCD_ACTION_SET_BLIGHT: |
|
| - | 84 | // Set backlight |
|
| - | 85 | if(msg->DataLength == 1){ |
|
| - | 86 | OCR1AL = msg->Data.bytes[0]; |
|
| - | 87 | } |
|
| - | 88 | break; |
|
| - | 89 | case LCD_ACTION_GET_CONT: |
|
| - | 90 | // Get contrast |
|
| - | 91 | txMsg.DataLength = 1; |
|
| - | 92 | txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID; |
|
| - | 93 | txMsg.Data.bytes[0] = OCR0B; |
|
| - | 94 | bios->can_send(&txMsg); |
|
| - | 95 | break; |
|
| - | 96 | case LCD_ACTION_GET_BLIGHT: |
|
| - | 97 | // Get backlight |
|
| - | 98 | txMsg.DataLength = 1; |
|
| - | 99 | txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID; |
|
| - | 100 | txMsg.Data.bytes[0] = OCR1AL; |
|
| - | 101 | bios->can_send(&txMsg); |
|
| - | 102 | break; |
|
| - | 103 | default: |
|
| - | 104 | // Take over the world |
|
| 48 |
|
105 | break; |
| - | 106 | } |
|
| - | 107 | } |
|
| 49 | } |
108 | } |
| 50 | 109 | ||
| 51 | 110 | ||
| 52 | ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B |
111 | ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B |
| 53 | uint8_t rot_data = 0; |
112 | uint8_t rot_data = 0; |
| 54 | 113 | ||
| 55 | txMsg.Id= |
114 | txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID; |
| 56 | txMsg.DataLength=2; |
115 | txMsg.DataLength=2; |
| 57 | 116 | ||
| 58 | if(PINB&(1<<PB7)){ |
117 | if(PINB&(1<<PB7)){ |
| 59 | rot_data |= 0x01; |
118 | rot_data |= 0x01; |
| 60 | } |
119 | } |
| Line 67... | Line 126... | ||
| 67 | if( rot_lastdir&0x01 ){ |
126 | if( rot_lastdir&0x01 ){ |
| 68 | // Moving right |
127 | // Moving right |
| 69 | txMsg.Data.bytes[0]=RIGHT; |
128 | txMsg.Data.bytes[0]=RIGHT; |
| 70 | txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1; |
129 | txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1; |
| 71 | bios->can_send(&txMsg); |
130 | bios->can_send(&txMsg); |
| - | 131 | // For testing TODO remove |
|
| - | 132 | if(OCR1A<0xFF){ |
|
| - | 133 | OCR1A++; |
|
| - | 134 | } |
|
| 72 | }else{ |
135 | }else{ |
| 73 | // Moving left |
136 | // Moving left |
| 74 | txMsg.Data.bytes[0]=LEFT; |
137 | txMsg.Data.bytes[0]=LEFT; |
| 75 | txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1; |
138 | txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1; |
| 76 | bios->can_send(&txMsg); |
139 | bios->can_send(&txMsg); |
| - | 140 | // For testing TODO remove |
|
| - | 141 | if(OCR1A>0){ |
|
| - | 142 | OCR1A--; |
|
| - | 143 | } |
|
| 77 | } |
144 | } |
| 78 | } |
145 | } |
| 79 | rot_laststate = rot_data; |
146 | rot_laststate = rot_data; |
| 80 | }else{ |
147 | }else{ |
| 81 | rot_lastdir = rot_data; |
148 | rot_lastdir = rot_data; |
| 82 | } |
149 | } |
| 83 | } |
150 | } |
| 84 | 151 | ||
| 85 | ISR( PCINT2_vect ){ // For ROTENC_BTN |
152 | ISR( PCINT2_vect ){ // For ROTENC_BTN |
| - | 153 | txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x02<<SNS_ID_BITS )| NODE_ID; |
|
| - | 154 | txMsg.Data.bytes[0] = (PIND&(1<<PD2))?0:1; // Check if buton is pressed or not |
|
| - | 155 | txMsg.DataLength=1; |
|
| 86 |
|
156 | bios->can_send(&txMsg); |
| 87 | } |
157 | } |
| 88 | 158 | ||
| 89 | void send_dimensions(); |
- | |
| 90 | 159 | ||
| 91 | /*----------------------------------------------------------------------------- |
160 | /*----------------------------------------------------------------------------- |
| 92 | * Main Program |
161 | * Main Program |
| 93 | *---------------------------------------------------------------------------*/ |
162 | *---------------------------------------------------------------------------*/ |
| 94 | int main(void) { |
163 | int main(void) { |
| 95 | 164 | ||
| 96 | // char buffer[ BUFFER_SIZE ]; |
- | |
| 97 | /* |
165 | /* |
| 98 | * Initialize rotaryencoders and buttons |
166 | * Initialize rotaryencoders and buttons |
| 99 | */ |
167 | */ |
| 100 | /* Set as input */ |
168 | /* Set as input */ |
| 101 | DDRD &= ~(1<<DDD2); |
169 | DDRD &= ~(1<<DDD2); |
| Line 106... | Line 174... | ||
| 106 | /* Enable IO-pin interrupt */ |
174 | /* Enable IO-pin interrupt */ |
| 107 | PCICR |= (1<<PCIE2)|(1<<PCIE0); |
175 | PCICR |= (1<<PCIE2)|(1<<PCIE0); |
| 108 | /* Unmask PB7, PB0 and PD2 */ |
176 | /* Unmask PB7, PB0 and PD2 */ |
| 109 | PCMSK2 |= (1<<PCINT18); |
177 | PCMSK2 |= (1<<PCINT18); |
| 110 | PCMSK0 |= (1<<PCINT7)|(1<<PCINT0); |
178 | PCMSK0 |= (1<<PCINT7)|(1<<PCINT0); |
| - | 179 | ||
| - | 180 | /* Contrast */ |
|
| - | 181 | TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00); |
|
| - | 182 | TCCR0B |= (1<<CS00); |
|
| - | 183 | OCR0B = 0x20; |
|
| - | 184 | DDRD |= (1<<DDD5); |
|
| - | 185 | ||
| - | 186 | /* Backlight */ |
|
| - | 187 | TCCR1A |= (1<<COM1A1)|(1<<WGM11); |
|
| - | 188 | TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10); |
|
| - | 189 | ICR1 = 0xFF; // Make it 8 bits |
|
| - | 190 | OCR1A = 0x20; |
|
| - | 191 | DDRB |= (1<<DDB1); |
|
| - | 192 | ||
| 111 | 193 | ||
| 112 | txMsg.ExtendedFlag=1; |
194 | txMsg.ExtendedFlag=1; |
| 113 | txMsg.RemoteFlag=0; |
195 | txMsg.RemoteFlag=0; |
| 114 | 196 | ||
| 115 | sei(); |
197 | sei(); |
| 116 | bios->can_callback = &can_receive; |
198 | bios->can_callback = &can_receive; |
| 117 | 199 | ||
| 118 | lcd_init( LCD_DISP_ON ); |
200 | lcd_init( LCD_DISP_ON ); |
| 119 | lcd_clrscr(); |
201 | lcd_clrscr(); |
| - | 202 | lcd_puts("HomeAutomation\n"); |
|
| 120 | 203 | ||
| 121 | send_dimensions(); |
204 | send_dimensions(); |
| - | 205 | lcd_puts("Very nice!\n"); |
|
| 122 | 206 | ||
| 123 | /* main loop */ |
207 | /* main loop */ |
| 124 | while(1){ |
208 | while(1){ |
| 125 | /* check if any messages have been received */ |
209 | /* check if any messages have been received */ |
| 126 | if(msg_received){ |
210 | if(msg_received){ |