Rev 335 | Rev 348 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 335 | Rev 344 | ||
|---|---|---|---|
| Line 18... | Line 18... | ||
| 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 <lcd_HD44780.h> |
22 | #include <lcd_HD44780.h> |
| 23 | #include <clcd20x4.h> |
- | |
| 24 | /* funcdefs */ |
23 | /* funcdefs */ |
| 25 | //#include <global_funcdefs.h> |
- | |
| 26 |
|
24 | //#include <eqlazer_funcdefs.h> |
| 27 | 25 | ||
| 28 | /* defines */ |
26 | /* defines */ |
| - | 27 | #define SIZE_X 20 |
|
| - | 28 | #define SIZE_Y 4 |
|
| - | 29 | ||
| 29 |
|
30 | //#define BUFFER_SIZE 20 |
| 30 | #define TRUE 1 |
31 | #define TRUE 1 |
| 31 | #define FALSE 0 |
32 | #define FALSE 0 |
| 32 | 33 | ||
| - | 34 | #define LEFT 0x01 |
|
| - | 35 | #define RIGHT 0x02 |
|
| - | 36 | ||
| 33 | Can_Message_t rxMsg; |
37 | Can_Message_t rxMsg; |
| - | 38 | Can_Message_t txMsg; |
|
| - | 39 | ||
| - | 40 | ||
| - | 41 | uint8_t msg_received = FALSE; |
|
| - | 42 | ||
| - | 43 | uint8_t rot_lastdir = 0, |
|
| - | 44 | rot_laststate = 0; |
|
| - | 45 | ||
| - | 46 | ||
| - | 47 | void can_receive(Can_Message_t *msg){ |
|
| - | 48 | // FIXME |
|
| - | 49 | } |
|
| 34 | 50 | ||
| 35 | uint8_t currentView = MAIN_VIEW, |
- | |
| 36 | msg_received = FALSE; |
- | |
| 37 | - | ||
| 38 | uint8_t temperatureData[8], |
- | |
| 39 | servoPosition[4], |
- | |
| 40 | //relayStatus[4], |
- | |
| 41 | //dimmerStatus[4], |
- | |
| 42 | temperatureLength = 8, |
- | |
| 43 | servoLength = 0; |
- | |
| 44 | //, relayLength=0; //TODO mer generaliserat |
- | |
| 45 | 51 | ||
| 46 |
|
52 | ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B |
| 47 |
|
53 | uint8_t rot_data = 0; |
| 48 | 54 | ||
| 49 |
|
55 | txMsg.Id=0xA000000; |
| 50 |
|
56 | txMsg.DataLength=2; |
| 51 | 57 | ||
| 52 | if( |
58 | if(PINB&(1<<PB7)){ |
| 53 | rot_data |= 0x01; |
59 | rot_data |= 0x01; |
| 54 | } |
60 | } |
| 55 | if( |
61 | if(PINB&(1<<PB0)){ |
| 56 | rot_data |= 0x02; |
62 | rot_data |= 0x02; |
| 57 | } |
63 | } |
| 58 | 64 | ||
| - | 65 | if( rot_data==0 || rot_data==3 ){ |
|
| 59 | if( |
66 | if( rot_data==0 && rot_laststate!=rot_data ){ |
| 60 | if( rot_lastdir&0x01 ){ |
67 | if( rot_lastdir&0x01 ){ |
| 61 | // Moving left |
- | |
| 62 | if(currentView>0){ |
- | |
| 63 | currentView--; |
- | |
| 64 | msg_received = TRUE; |
- | |
| 65 | } |
- | |
| 66 | }else{ |
- | |
| 67 | // Moving right |
68 | // Moving right |
| 68 |
|
69 | txMsg.Data.bytes[0]=RIGHT; |
| - | 70 | txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1; |
|
| - | 71 | bios->can_send(&txMsg); |
|
| - | 72 | }else{ |
|
| 69 |
|
73 | // Moving left |
| - | 74 | txMsg.Data.bytes[0]=LEFT; |
|
| - | 75 | txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1; |
|
| 70 |
|
76 | bios->can_send(&txMsg); |
| 71 | } |
77 | } |
| 72 | } |
78 | } |
| 73 | rot_laststate= |
79 | rot_laststate = rot_data; |
| 74 | }else if( rot_data==0 && rot_laststate!=0 ){ |
- | |
| 75 |
|
80 | }else{ |
| 76 | }else if( rot_data==1 || rot_data==2 ){ |
- | |
| 77 | rot_lastdir = rot_data; |
81 | rot_lastdir = rot_data; |
| 78 | } |
82 | } |
| 79 | } |
- | |
| 80 | - | ||
| 81 | void can_receive(Can_Message_t *msg){ |
- | |
| 82 | uint8_t temp, m; |
- | |
| 83 | /* Check for incoming messages from IR-receiver node */ |
- | |
| 84 | if( CLASS_SNS == ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) ){ |
- | |
| 85 | /* FIXME detta är väldigt fult och borde fixas |
- | |
| 86 | * knapp 4 på fjärren bläddrar åt vänster |
- | |
| 87 | * knapp 6 åt höger |
- | |
| 88 | */ |
- | |
| 89 | if( SNS_IR_RECEIVE == (msg->Id & TYPE_MASK)>>TYPE_MASK_BITS){ |
- | |
| 90 | if(msg->Data.bytes[0] == 0 && msg->Data.bytes[1] == 0 && msg->Data.bytes[2] == 0){ |
- | |
| 91 | if(msg->Data.bytes[3] == 4 && currentView>0){ |
- | |
| 92 | currentView--; |
- | |
| 93 | msg_received = TRUE; |
- | |
| 94 | }else if(msg->Data.bytes[3] == 6 && currentView<LAST_VIEW){ |
- | |
| 95 | currentView++; |
- | |
| 96 | msg_received = TRUE; |
- | |
| 97 | } |
- | |
| 98 | } |
- | |
| 99 | } |
- | |
| 100 | /* Other sensor messages should be placed here */ |
- | |
| 101 | - | ||
| 102 | /* DS18S20 sensors */ |
- | |
| 103 | if( SNS_TEMP_DS18S20 == (msg->Id & SNS_TYPE_MASK)>>SNS_TYPE_BITS ){ |
- | |
| 104 | temp = (msg->Id & SNS_ID_MASK)>>SNS_ID_BITS; |
- | |
| 105 | temperatureData[ (temp-1)*2 ] = msg->Data.bytes[0]; |
- | |
| 106 | temperatureData[ (temp-1)*2+1 ] = msg->Data.bytes[1]; |
- | |
| 107 | } |
- | |
| 108 | - | ||
| 109 | /* Servo controllers */ |
- | |
| 110 | if( SNS_ACT_STATUS_SERVO == (msg->Id & TYPE_MASK)>>TYPE_MASK_BITS ){ // TODO annan struktur på statusmeddelanden från servo |
- | |
| 111 | temp = msg->DataLength - 3; |
- | |
| 112 | for(m=0;m<temp;m++){ |
- | |
| 113 | servoPosition[ m ] = msg->Data.bytes[m+3]; |
- | |
| 114 | } |
- | |
| 115 | servoLength = temp; |
- | |
| 116 | } |
83 | } |
| 117 | 84 | ||
| - | 85 | ISR( PCINT2_vect ){ // For ROTENC_BTN |
|
| 118 | |
86 | lcd_puts("a"); |
| 119 | } |
87 | } |
| 120 | 88 | ||
| - | 89 | void send_dimensions(); |
|
| 121 | 90 | ||
| 122 | /*----------------------------------------------------------------------------- |
91 | /*----------------------------------------------------------------------------- |
| 123 | * Main Program |
92 | * Main Program |
| 124 | *---------------------------------------------------------------------------*/ |
93 | *---------------------------------------------------------------------------*/ |
| 125 | int main(void) { |
94 | int main(void) { |
| 126 | 95 | ||
| 127 |
|
96 | // char buffer[ BUFFER_SIZE ]; |
| 128 | /* |
97 | /* |
| 129 | * Initialize rotaryencoders and buttons |
98 | * Initialize rotaryencoders and buttons |
| 130 | */ |
99 | */ |
| 131 | /* Set as input */ |
100 | /* Set as input */ |
| - | 101 | DDRD &= ~(1<<DDD2); |
|
| 132 |
|
102 | DDRB &= ~((1<<DDB7)|(1<<DDB0)); |
| - | 103 | /* Enable pull-up */ |
|
| - | 104 | PORTD |= (1<<PD2); |
|
| - | 105 | PORTB |= (1<<PB7)|(1<<PB0); |
|
| 133 | /* Enable IO-pin interrupt */ |
106 | /* Enable IO-pin interrupt */ |
| 134 | PCICR |= (1<<PCIE2); |
107 | PCICR |= (1<<PCIE2)|(1<<PCIE0); |
| 135 | /* Unmask |
108 | /* Unmask PB7, PB0 and PD2 */ |
| - | 109 | PCMSK2 |= (1<<PCINT18); |
|
| 136 |
|
110 | PCMSK0 |= (1<<PCINT7)|(1<<PCINT0); |
| - | 111 | ||
| - | 112 | txMsg.ExtendedFlag=1; |
|
| - | 113 | txMsg.RemoteFlag=0; |
|
| 137 | 114 | ||
| 138 | sei(); |
115 | sei(); |
| 139 | bios->can_callback = &can_receive; |
116 | bios->can_callback = &can_receive; |
| 140 | 117 | ||
| 141 | lcd_init( LCD_DISP_ON ); |
118 | lcd_init( LCD_DISP_ON ); |
| 142 | lcd_clrscr(); |
119 | lcd_clrscr(); |
| 143 | 120 | ||
| 144 |
|
121 | send_dimensions(); |
| 145 | printLCDview( MAIN_VIEW ); |
- | |
| 146 | 122 | ||
| 147 | /* main loop */ |
123 | /* main loop */ |
| 148 | while(1){ |
124 | while(1){ |
| 149 | /* check if any messages have been received */ |
125 | /* check if any messages have been received */ |
| 150 | if(msg_received){ |
126 | if(msg_received){ |
| 151 | /* Print views skeleton and if existing its data */ |
- | |
| 152 | printLCDview( currentView ); |
- | |
| 153 | if(currentView == TEMPSENS_VIEW){ |
- | |
| 154 | printLCDviewData( TEMPSENS_VIEW, temperatureData, temperatureLength); |
- | |
| 155 | }else if(currentView == SERVO_VIEW){ |
- | |
| 156 | printLCDviewData( SERVO_VIEW, servoPosition, servoLength); |
- | |
| 157 | } |
127 | |
| 158 | msg_received = FALSE; |
128 | msg_received = FALSE; |
| 159 | } |
129 | } |
| 160 | } |
130 | } |
| - | 131 | } |
|
| - | 132 | ||
| - | 133 | void send_dimensions(){ |
|
| - | 134 | txMsg.Id=0xA000001; |
|
| - | 135 | txMsg.Data.bytes[0] = SIZE_X; |
|
| - | 136 | txMsg.Data.bytes[1] = SIZE_Y; |
|
| - | 137 | txMsg.DataLength = 2; |
|
| - | 138 | bios->can_send(&txMsg); |
|
| 161 | } |
139 | } |