Subversion Repositories HomeAutomation

Rev

Rev 448 | Rev 481 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 448 Rev 463
Line 47... Line 47...
47
char buffer[ BUFFER_SIZE ];
47
char buffer[ BUFFER_SIZE ];
48
 
48
 
49
void send_dimensions();
49
void send_dimensions();
50
 
50
 
51
void can_receive(Can_Message_t *msg){
51
void can_receive(Can_Message_t *msg){
-
 
52
Can_Message_t txMsg;
52
    uint32_t act;
53
    uint32_t act;
53
    uint8_t m, act_type, lcd_action, lcd_size;
54
    uint8_t m, act_type, lcd_action, lcd_size;
54
    if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
55
    if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
55
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
56
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
56
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
57
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
Line 93... Line 94...
93
            case LCD_ACTION_GET_CONT:
94
            case LCD_ACTION_GET_CONT:
94
                // Get contrast
95
                // Get contrast
95
                txMsg.DataLength = 1;
96
                txMsg.DataLength = 1;
96
                txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
97
                txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
97
                txMsg.Data.bytes[0] = OCR0B;
98
                txMsg.Data.bytes[0] = OCR0B;
98
                bios->can_send(&txMsg);
99
                BIOS_CanSend(&txMsg);
99
                break;
100
                break;
100
            case LCD_ACTION_GET_BLIGHT:
101
            case LCD_ACTION_GET_BLIGHT:
101
                // Get backlight
102
                // Get backlight
102
                txMsg.DataLength = 1;
103
                txMsg.DataLength = 1;
103
                txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
104
                txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
104
                txMsg.Data.bytes[0] = OCR1AL;
105
                txMsg.Data.bytes[0] = OCR1AL;
105
                bios->can_send(&txMsg);
106
                BIOS_CanSend(&txMsg);
106
                break;
107
                break;
107
            default:
108
            default:
108
                // Take over the world
109
                // Take over the world
109
                break;
110
                break;
110
        }
111
        }
111
    }
112
    }
112
}
113
}
113
 
114
 
114
ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B
115
ISR( PCINT2_vect ){ // For ROTENC_A and ROTENC_B
-
 
116
/*  uint8_t rot_data = 0;
-
 
117
 
-
 
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);
-
 
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
}
-
 
170
 
-
 
171
void rotenc(){
115
    uint8_t rot_data = 0;
172
    uint8_t rot_data = 0;
116
 
-
 
-
 
173
Can_Message_t txMsg;
-
 
174
    txMsg.ExtendedFlag=1;
-
 
175
    txMsg.RemoteFlag=0;
117
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
176
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
118
    txMsg.DataLength=2;
177
    txMsg.DataLength=2;
119
 
178
 
120
    if(PINB&(1<<PB7)){
179
    if(PINB&(1<<PB0)){
121
        rot_data |= 0x01;
180
        rot_data |= 0x01;
122
    }
181
    }
123
    if(PINB&(1<<PB0)){
182
    if(PIND&(1<<PD2)){
124
        rot_data |= 0x02;
183
        rot_data |= 0x02;
125
    }
184
    }
126
 
185
 
127
    if( rot_data==0 || rot_data==3 ){
186
    if( rot_data==0 || rot_data==3 ){
128
        if( rot_data==0 && rot_laststate!=rot_data ){
187
        if( rot_data==0 && rot_laststate!=rot_data ){
129
            if( rot_lastdir&0x01 ){
188
            if( rot_lastdir&0x01 ){
130
                // Moving right
189
                // Moving right
131
                txMsg.Data.bytes[0]=RIGHT;
190
                txMsg.Data.bytes[0]=RIGHT;
132
                txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
191
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
192
    txMsg.DataLength=2;
133
                bios->can_send(&txMsg);
193
                BIOS_CanSend(&txMsg);
134
                // For testing TODO remove
194
                // For testing TODO remove
135
                if(OCR1A<0xFF){
195
                if(OCR1A<0xFF){
136
                    OCR1A++;
196
                    OCR1A++;
-
 
197
                }
-
 
198
                if(OCR0B<0xFF){
-
 
199
                    OCR0B++;
137
                }
200
                }
138
            }else{
201
            }else{
139
                // Moving left
202
                // Moving left
140
                txMsg.Data.bytes[0]=LEFT;
203
                txMsg.Data.bytes[0]=LEFT;
141
                txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
204
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
-
 
205
    txMsg.DataLength=2;
142
                bios->can_send(&txMsg);
206
                BIOS_CanSend(&txMsg);
143
                // For testing TODO remove
207
                // For testing TODO remove
144
                if(OCR1A>0){
208
                if(OCR1A>0){
145
                    OCR1A--;
209
                    OCR1A--;
-
 
210
                }
-
 
211
                if(OCR0B>0){
-
 
212
                    OCR0B--;
146
                }
213
                }
147
            }
214
            }
148
        }
215
        }
149
        rot_laststate = rot_data;
216
        rot_laststate = rot_data;
150
    }else{
217
    }else{
151
        rot_lastdir = rot_data;
218
        rot_lastdir = rot_data;
152
    }
219
    }
153
}
220
}
154
 
221
 
155
ISR( PCINT2_vect ){ // For ROTENC_BTN
-
 
156
    txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x02<<SNS_ID_BITS )| NODE_ID;
-
 
157
    txMsg.Data.bytes[0] = (PIND&(1<<PD2))?0:1; // Check if buton is pressed or not
-
 
158
    txMsg.DataLength=1;
-
 
159
    bios->can_send(&txMsg);
-
 
160
}
-
 
161
 
222
 
162
/*-----------------------------------------------------------------------------
223
/*-----------------------------------------------------------------------------
163
 * Main Program
224
 * Main Program
164
 *---------------------------------------------------------------------------*/
225
 *---------------------------------------------------------------------------*/
165
int main(void) {
226
int main(void) {
Line 189... Line 250...
189
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
250
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
190
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
251
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
191
    ICR1 = 0xFF; // Make it 8 bits
252
    ICR1 = 0xFF; // Make it 8 bits
192
    OCR1A = 0x20;
253
    OCR1A = 0x20;
193
    DDRB |= (1<<DDB1);
254
    DDRB |= (1<<DDB1);
194
 
255
 
-
 
256
    Can_Message_t txMsg;
195
    txMsg.ExtendedFlag=1;
257
    txMsg.ExtendedFlag=1;
196
    txMsg.RemoteFlag=0;
258
    txMsg.RemoteFlag=0;
197
 
259
 
198
    sei();
260
    sei();
199
    bios->can_callback = &can_receive;
261
    BIOS_CanCallback = &can_receive;
200
 
262
 
201
    lcd_init( LCD_DISP_ON );
263
    lcd_init( LCD_DISP_ON );
202
    lcd_clrscr();
264
    lcd_clrscr();
203
    lcd_puts("HomeAutomation\n");
265
    lcd_puts("HomeAutomation\n");
204
 
266
 
205
    send_dimensions();
267
    send_dimensions();
206
    lcd_puts("Very nice!\n");
268
    lcd_puts("Very nice!\n");
207
 
269
 
208
    /* main loop */
270
    /* main loop */
209
    while(1){
271
    while(1){
210
        /* check if any messages have been received */
272
        /* check if any messages have been received */
211
        if(msg_received){
273
        if(msg_received){
212
 
274
 
213
            msg_received = FALSE;
275
            msg_received = FALSE;
-
 
276
        }
-
 
277
        lcd_puts("test");
214
        }
278
    }
215
    }
279
}
216
}
-
 
217
 
280
 
218
void send_dimensions(){
281
void send_dimensions(){
-
 
282
    Can_Message_t txMsg;
-
 
283
    txMsg.ExtendedFlag=1;
-
 
284
    txMsg.RemoteFlag=0;
219
    txMsg.Id=0xA000001;
285
    txMsg.Id=0xA000001;
220
    txMsg.Data.bytes[0] = SIZE_X;
286
    txMsg.Data.bytes[0] = SIZE_X;
221
    txMsg.Data.bytes[1] = SIZE_Y;
287
    txMsg.Data.bytes[1] = SIZE_Y;
222
    txMsg.DataLength = 2;
288
    txMsg.DataLength = 2;
223
    bios->can_send(&txMsg);
289
    BIOS_CanSend(&txMsg);
224
}
290
}