Subversion Repositories HomeAutomation

Rev

Rev 731 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 731 Rev 745
Line 1... Line 1...
1
/**
1
/**
2
 * AntennaDisplay
2
 * Antenna Display.
3
 * For controlling of the cAntenna bus using one
3
 * For controlling of the cAntenna bus using one
4
 * HD44780 LCD display and buttons.
4
 * HD44780 LCD display and som buttons.
5
 *
5
 *
-
 
6
 *
-
 
7
 *
6
 * @date    2007-12-10
8
 * @date    2007-11-19
7
 * @author  Erik Larsson
9
 * @author  Erik Larsson
8
 *
10
 *
9
 */
11
 */
10
 
12
 
11
/*-----------------------------------------------------------------------------
13
/*-----------------------------------------------------------------------------
12
 * Includes
14
 * Includes
13
 *---------------------------------------------------------------------------*/
15
 *---------------------------------------------------------------------------*/
14
/* system files */
16
/* system files */
15
#include <avr/io.h>
17
#include <avr/io.h>
16
#include <inttypes.h>
18
#include <inttypes.h>
17
#include <avr/interrupt.h>
19
#include <avr/interrupt.h>
18
#include <stdio.h>
20
#include <stdio.h>
19
#include <stdlib.h>
21
#include <stdlib.h>
20
 
22
 
21
/* lib files */
23
/* lib files */
22
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
24
#include <bios.h>
23
#include <config.h> // All configuration parameters
25
#include <config.h>
24
#include <drivers/timer/timer.h>
26
#include <drivers/timer/timer.h>
25
#include <drivers/lcd/clcd/lcd_HD44780.h>
27
#include <drivers/lcd/clcd/lcd_HD44780.h>
26
 
28
 
27
#include <string.h> //for memcpy
29
#include <string.h> //for memcpy
28
 
30
 
29
/*-----------------------------------------------------------------------------
31
/*-----------------------------------------------------------------------------
30
 * Defines
32
 * Defines
31
 *---------------------------------------------------------------------------*/
33
 *---------------------------------------------------------------------------*/
32
#define APP_TYPE    0x1234
-
 
33
#define APP_VERSION 0x0002
-
 
34
 
-
 
35
// LCD size
-
 
36
#define SIZE_X  16
34
#define SIZE_X  16
37
#define SIZE_Y  2
35
#define SIZE_Y  2
38
 
36
 
39
#define BUFFER_SIZE SIZE_X
37
#define BUFFER_SIZE SIZE_X
40
 
38
 
Line 42... Line 40...
42
#define AZIMUTH 1
40
#define AZIMUTH 1
43
#define ELEVATION 2
41
#define ELEVATION 2
44
#define SCREEN_NORMAL 3
42
#define SCREEN_NORMAL 3
45
#define SCREEN_CAL 4
43
#define SCREEN_CAL 4
46
 
44
 
47
// Display modes
-
 
48
#define NORMAL_MODE 0x01
45
#define NORMAL_MODE 0x01
49
#define CAL_MODE 0x02
46
#define CAL_MODE 0x02
50
 
47
 
51
// Actions
-
 
52
#define AZIMUTH_PLUS 0x05
48
#define AZIMUTH_PLUS 0x05
53
#define AZIMUTH_MINUS 0x06
49
#define AZIMUTH_MINUS 0x06
54
#define ELEVATION_PLUS 0x07
50
#define ELEVATION_PLUS 0x07
55
#define ELEVATION_MINUS 0x08
51
#define ELEVATION_MINUS 0x08
56
#define AZIMUTH_STOP 0x09
52
#define AZIMUTH_STOP 0x09
57
#define ELEVATION_STOP 0x0a
53
#define ELEVATION_STOP 0x0a
58
#define GET_CAL 0x0b
54
#define GET_CAL 0x0b
59
#define SET_CAL 0x0c
55
#define SET_CAL 0x0c
-
 
56
 
-
 
57
 
-
 
58
#define APP_TYPE    0x1234
-
 
59
#define APP_VERSION 0x0001
60
 
60
 
61
volatile Can_Message_t rxMsg;
61
volatile Can_Message_t rxMsg;
62
 
62
 
-
 
63
//char incoming_text[ BUFFER_SIZE ];
63
uint8_t rxMsgFull=0;
64
uint8_t rxMsgFull=0;
64
 
65
 
-
 
66
uint8_t mode = NORMAL_MODE;
-
 
67
 
65
// Buttons
68
// Buttons
66
uint8_t azimuth_plus = 0, azimuth_minus = 0;
69
uint8_t azimuth_plus = 0, azimuth_minus = 0;
67
uint8_t elevation_plus = 0, elevation_minus = 0;
70
uint8_t elevation_plus = 0, elevation_minus = 0;
68
uint8_t calibration = 0;
71
uint8_t calibration = 0;
69
 
72
 
70
 
73
 
71
/*-----------------------------------------------------------------------------
74
//void send_dimensions(void);
72
 * Declarations
75
//void rotenc(void);
73
 *---------------------------------------------------------------------------*/
-
 
74
 
76
 
75
/**
-
 
76
 * IO_init
-
 
77
 * Enable button inputs
77
//void button(void);
78
 * Enable contrast and backlight for LCD
-
 
79
 *
78
 
80
 * @param void
-
 
81
 * @return void
-
 
82
 */
-
 
83
void IO_init( void );
79
void IO_init( void );
84
 
80
 
85
/**
-
 
86
 * print_sceen
-
 
87
 * Prints normal and calibration data to LCD
-
 
88
 *
-
 
89
 * @param screen type, normal or calibration
-
 
90
 * @param data to print
-
 
91
 * @return void
-
 
92
 */
-
 
93
void print_screen( uint8_t type, uint16_t data );
81
void print_screen( uint8_t type, uint16_t data );
94
 
82
 
95
/**
-
 
96
 * send_message
-
 
97
 * Handles all messages to antenna nodes
-
 
98
 *
-
 
99
 * @param type of message
-
 
100
 * @param data to send
-
 
101
 * @return void
-
 
102
 */
-
 
103
void send_message( uint8_t type, uint16_t data );
83
void send_message( uint8_t type, uint16_t data );
104
 
84
 
105
/**
-
 
106
 * calibrate
-
 
107
 * Gets calibration data from node, adjusts it and returns
-
 
108
 *
-
 
109
 * @param void
-
 
110
 * @return void
-
 
111
 */
-
 
112
void calibrate( void );
85
void calibrate( void );
113
 
86
 
114
/**
-
 
115
 * read_buttons
87
// CAN message reception callback
-
 
88
// This function runs with interrupts disabled, keep it as short as possible
116
 * Periodicly read buttons
89
void can_receive(Can_Message_t *msg){
117
 *
90
    if(!rxMsgFull){
118
 * @param trigging timer
91
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
119
 * @return void
92
        rxMsgFull = 1;
120
 */
93
    }
-
 
94
}
-
 
95
 
121
void read_buttons(uint8_t timer) {
96
void read_buttons(uint8_t timer) {
-
 
97
    // Periodicly read buttons
122
    // Read buttons
98
    // Read buttons
123
    calibration = (PIND & (1<<PD2))?0:1;
99
        calibration = (PIND & (1<<PD2))?0:1;
124
    azimuth_plus = (PINB & (1<<PB0))?0:1;
100
        azimuth_plus = (PINB & (1<<PB0))?0:1;
125
    azimuth_minus = (PINB & (1<<PB7))?0:1;
101
        azimuth_minus = (PINB & (1<<PB7))?0:1;
126
    elevation_plus = (PINC & (1<<PC1))?0:1;
102
    elevation_plus = (PINC & (1<<PC1))?0:1;
127
    elevation_minus = (PINC & (1<<PC0))?0:1;
103
    elevation_minus = (PINC & (1<<PC0))?0:1;
128
}
104
}
129
 
105
 
130
// CAN message reception callback
-
 
131
// This function runs with interrupts disabled, keep it as short as possible
-
 
132
void can_receive(Can_Message_t *msg){
-
 
133
    if(!rxMsgFull){
-
 
134
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
-
 
135
        rxMsgFull = 1;
-
 
136
    }
-
 
137
}
-
 
138
 
106
 
139
/*-----------------------------------------------------------------------------
107
/*-----------------------------------------------------------------------------
140
 * Main Program
108
 * Main Program
141
 *---------------------------------------------------------------------------*/
109
 *---------------------------------------------------------------------------*/
142
int main( void ) {
110
int main( void ) {
143
    // Flag for automatic sending stop message after released buttons
-
 
144
    uint8_t autostop = 0;
111
    uint8_t autostop = 0;
145
 
112
 
146
    sei();
113
    sei();
147
   
114
   
148
    // Enable timers
-
 
149
    Timer_Init();
115
    Timer_Init();
150
 
116
 
151
    // Enable buttons and display
-
 
152
    IO_init();
117
    IO_init();
153
 
118
 
154
    Can_Message_t txMsg;
119
    Can_Message_t txMsg;
155
   
120
   
156
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
121
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
157
    txMsg.ExtendedFlag=1;
122
    txMsg.ExtendedFlag=1;
158
    txMsg.RemoteFlag=0;
123
    txMsg.RemoteFlag=0;
159
    txMsg.DataLength = 4;
124
    txMsg.DataLength = 4;
160
   
125
   
161
    txMsg.Data.words[0] = APP_TYPE;
126
    txMsg.Data.words[0] = APP_TYPE;
162
    txMsg.Data.words[1] = APP_VERSION;
127
    txMsg.Data.words[1] = APP_VERSION;
163
   
128
   
164
    // Send startup message
129
    // Send startup message
165
    BIOS_CanSend(&txMsg);
130
    BIOS_CanSend(&txMsg);
166
   
131
   
167
    BIOS_CanCallback = &can_receive;
132
    BIOS_CanCallback = &can_receive;
168
   
133
 
-
 
134
    lcd_init( LCD_DISP_ON );
-
 
135
 
-
 
136
        print_screen( SCREEN_NORMAL, 0 );
-
 
137
   
169
    // Timer for reading buttons and doing actions
138
    // Timer for reading buttons
170
    Timer_SetTimeout(0, 50, TimerTypeFreeRunning, &read_buttons);
139
    Timer_SetTimeout(0, 50, TimerTypeFreeRunning, &read_buttons);
171
    Timer_SetTimeout(1, 500, TimerTypeFreeRunning, 0);
140
    Timer_SetTimeout(1, 500, TimerTypeFreeRunning, 0);
172
   
141
   
173
    /* main loop */
142
    /* main loop */
174
    while(1){
143
    while(1){
175
       
144
       
176
        /* check if any messages have been received */
145
        /* check if any messages have been received */
177
        if( rxMsgFull ){
146
        if( rxMsgFull ){
178
 
147
 
179
            if( rxMsg.Id == 0x1f8f0100 ){
148
            if( rxMsg.Id == 0x1f8f0100 ){
180
                // Received new azimuth data and print it
-
 
181
                print_screen( AZIMUTH, rxMsg.Data.words[0] );
149
                print_screen( AZIMUTH, rxMsg.Data.words[0] );
182
               
-
 
183
            }else if( rxMsg.Id == 0x1f8f0200 ){
150
            }else if( rxMsg.Id == 0x1f8f0200 ){
184
                // Received new elevation data and print it
-
 
185
                print_screen( ELEVATION, rxMsg.Data.words[0] );
151
                print_screen( ELEVATION, rxMsg.Data.words[0] );
186
            }
152
            }
187
           
153
           
188
            rxMsgFull = 0;
154
            rxMsgFull = 0;
189
        }
155
        }
190
       
156
         
191
        // Periodicly take care of button actions
-
 
192
        if (Timer_Expired(1)) {
157
        if (Timer_Expired(1)) {
193
 
158
 
194
            // Perhaps someone pressed a button
159
            // Perhaps someone pressed a button
195
            if( calibration || azimuth_plus || azimuth_minus || elevation_plus || elevation_minus ){
160
            if( calibration || azimuth_plus || azimuth_minus || elevation_plus || elevation_minus ){
196
                if( calibration ){
161
                if( calibration ){
197
                    // Enters calibration mode
-
 
198
                    // Send all stop
162
                    // send all stop
199
                    send_message( AZIMUTH_STOP, 0);
163
                    send_message( AZIMUTH_STOP, 0);
200
                    send_message( ELEVATION_STOP, 0);
164
                    send_message( ELEVATION_STOP, 0);
201
 
165
 
202
                    // Start calibration
166
                    // Start calibration
203
                    calibrate();
167
                    calibrate();
204
 
168
 
205
                }else{
169
                }else{
206
                    if( azimuth_plus ){
170
                    if( azimuth_plus ){
207
                        // Send rotate aximuth plus
171
                        //send rotate aximuth plus
208
                        send_message( AZIMUTH_PLUS, 0 );
172
                        send_message( AZIMUTH_PLUS, 0 );
209
                       
-
 
210
                    }else if( azimuth_minus ){
173
                    }else if( azimuth_minus ){
211
                        // Send rotate azimuth minus
174
                        //send rotate azimuth minus
212
                        send_message( AZIMUTH_MINUS, 0 );
175
                        send_message( AZIMUTH_MINUS, 0 );
213
                       
-
 
214
                    }else{
176
                    }else{
215
                        // Send azimuth stop
177
                        // send azimuth stop
216
                        send_message( AZIMUTH_STOP, 0 );
178
                        send_message( AZIMUTH_STOP, 0 );
217
                    }
179
                    }
218
   
180
   
219
                    if( elevation_plus ){
181
                    if( elevation_plus ){
220
                        // Send elevate plus
182
                        //send elevate plus
221
                        send_message( ELEVATION_PLUS, 0 );
183
                        send_message( ELEVATION_PLUS, 0 );
222
                       
-
 
223
                    }else if( elevation_minus ){
184
                    }else if( elevation_minus ){
224
                        // Send elevate minus
185
                        //send elevate minus
225
                        send_message( ELEVATION_MINUS, 0 );
186
                        send_message( ELEVATION_MINUS, 0 );
226
                       
-
 
227
                    }else{
187
                    }else{
228
                        // Send elevation stop
188
                        //send elevation stop
229
                        send_message( ELEVATION_STOP, 0 );
189
                        send_message( ELEVATION_STOP, 0 );
230
                    }
190
                    }
231
 
191
 
232
                    autostop = 1; // Stop all rotors when no button is pressed
192
                    autostop = 1; // Stop all rotors when no button is pressed
233
                }
193
                }
234
 
194
 
235
            }else if ( autostop ){
195
            }else if ( autostop ){
236
                // Send all stop
196
                // send all stop
237
                // Only doing it once, no need to spam
-
 
238
                send_message( AZIMUTH_STOP, 0);
197
                send_message( AZIMUTH_STOP, 0);
239
                send_message( ELEVATION_STOP, 0);
198
                send_message( ELEVATION_STOP, 0);
240
 
199
 
241
                autostop = 0;
200
                autostop = 0;
242
            }
201
            }
243
        }
202
        }
244
    }
203
    }
245
}
204
}
246
 
-
 
247
/*-----------------------------------------------------------------------------
-
 
248
 * Definitions
-
 
249
 *---------------------------------------------------------------------------*/
-
 
250
 
205
 
251
void send_message( uint8_t type, uint16_t data ){
206
void send_message( uint8_t type, uint16_t data ){
252
    Can_Message_t txMsg;
207
    Can_Message_t txMsg;
253
 
208
 
254
    txMsg.ExtendedFlag=1;
209
    txMsg.ExtendedFlag=1;
255
    txMsg.RemoteFlag=0;
210
    txMsg.RemoteFlag=0;
256
 
211
 
257
    switch(type){
212
    switch(type){
258
   
213
   
259
    case AZIMUTH_PLUS:
214
    case AZIMUTH_PLUS:
260
        // Increase azimuth
-
 
261
        txMsg.Id = 0x11100005UL;
215
        txMsg.Id = 0x11100005UL;
262
        txMsg.Data.bytes[0] = 0x01;
216
        txMsg.Data.bytes[0] = 0x01;
263
        txMsg.DataLength = 1;
217
        txMsg.DataLength = 1;
264
        break;
218
        break;
265
   
219
   
266
    case AZIMUTH_MINUS:
220
    case AZIMUTH_MINUS:
267
        // Decrease azimuth
-
 
268
        txMsg.Id = 0x11100005UL;
221
        txMsg.Id = 0x11100005UL;
269
        txMsg.Data.bytes[0] = 0x02;
222
        txMsg.Data.bytes[0] = 0x02;
270
        txMsg.DataLength = 1;
223
        txMsg.DataLength = 1;
271
        break;
224
        break;
272
   
225
   
273
    case AZIMUTH_STOP:
226
    case AZIMUTH_STOP:
274
        // Stop azimuth rotating
-
 
275
        txMsg.Id = 0x11100005UL;
227
        txMsg.Id = 0x11100005UL;
276
        txMsg.Data.bytes[0] = 0x00;
228
        txMsg.Data.bytes[0] = 0x00;
277
        txMsg.DataLength = 1;
229
        txMsg.DataLength = 1;
278
        break;
230
        break;
279
   
231
   
280
    case ELEVATION_PLUS:
232
    case ELEVATION_PLUS:
281
        // Increase elevation angle
-
 
282
        txMsg.Id = 0x11100015UL;
233
        txMsg.Id = 0x11100015UL;
283
        txMsg.Data.bytes[0] = 0x01;
234
        txMsg.Data.bytes[0] = 0x01;
284
        txMsg.DataLength = 1;
235
        txMsg.DataLength = 1;
285
        break;
236
        break;
286
   
237
   
287
    case ELEVATION_MINUS:
238
    case ELEVATION_MINUS:
288
        // Decrease elevation angle
-
 
289
        txMsg.Id = 0x111000015UL;
239
        txMsg.Id = 0x111000015UL;
290
        txMsg.Data.bytes[0] = 0x02;
240
        txMsg.Data.bytes[0] = 0x02;
291
        txMsg.DataLength = 1;
241
        txMsg.DataLength = 1;
292
        break;
242
        break;
293
   
243
   
294
    case ELEVATION_STOP:
244
    case ELEVATION_STOP:
295
        // Stop elevation rotating
-
 
296
        txMsg.Id = 0x11100015UL;
245
        txMsg.Id = 0x11100015UL;
297
        txMsg.Data.bytes[0] = 0x00;
246
        txMsg.Data.bytes[0] = 0x00;
298
        txMsg.DataLength = 1;
247
        txMsg.DataLength = 1;
299
        break;
248
        break;
300
       
249
       
301
    case GET_CAL:
250
    case GET_CAL:
302
        // Get calibration data from nodes
-
 
303
        //FIXME add elevation
-
 
304
        txMsg.Id = 0x11100002UL;
251
        txMsg.Id = 0x11100002UL; // MSG_CAL_GET
305
        txMsg.DataLength = 0;
252
        txMsg.DataLength = 0;
306
        break;
253
        break;
307
       
254
       
308
    case SET_CAL:
255
    case SET_CAL:
309
        // Set calibration data in nodes
-
 
310
        //FIXME add elevation
-
 
311
        txMsg.Id = 0x11100001UL;
256
        txMsg.Id = 0x11100001UL; // MSG_CAL_SET
312
        txMsg.DataLength = 2;
257
        txMsg.DataLength = 2;
313
        txMsg.Data.words[0] = data;
258
        txMsg.Data.words[0] = data;
314
        break;
259
        break;
315
       
260
       
316
    default:
261
    default:
317
        // Stop all rotating
262
        // Stop rotating
318
        txMsg.Id = 0x11100005UL;
263
        txMsg.Id = 0x11100005UL;
319
        txMsg.Data.bytes[0] = 0x00;
264
        txMsg.Data.bytes[0] = 0x00;
320
        txMsg.DataLength = 1;
265
        txMsg.DataLength = 1;
321
        break;
266
        break;
322
    }
267
    }
323
 
268
 
324
    BIOS_CanSend(&txMsg);
269
    BIOS_CanSend(&txMsg);
325
}
270
}
326
 
-
 
327
/**
-
 
328
 * print_screen, 2x16 characters
-
 
329
 *
-
 
330
 * Normal screen (for rotating):
-
 
331
 * ##################
-
 
332
 * #Azimuth     183*#
-
 
333
 * #Elevation    45*#
-
 
334
 * ##################  
-
 
335
 *
-
 
336
 * Calibration screen:
-
 
337
 * ##################
-
 
338
 * #Cal Azimuth  20*#
-
 
339
 * #Cal Elevat.  12*#
-
 
340
 * ##################
-
 
341
 */
-
 
342
 
271
 
343
void print_screen(uint8_t type, uint16_t data){
272
void print_screen(uint8_t type, uint16_t data){
344
    char buffer[ BUFFER_SIZE ];
273
    char buffer[ BUFFER_SIZE ];
345
 
274
 
346
    if( SCREEN_NORMAL == type){
275
    if( SCREEN_NORMAL == type){ // rotation
-
 
276
        lcd_clrscr();
-
 
277
        lcd_puts("Azimuth     ---*\n");
347
        // Normal screen for rotating
278
        lcd_puts("Elevation   ---*\n");
-
 
279
 
-
 
280
    }else if( SCREEN_CAL == type){ // calibration
348
        lcd_clrscr();
281
    lcd_clrscr();
349
        lcd_puts("Azimuth     ???*\n");
282
        lcd_puts("Cal Azimuth ---*\n");
350
        lcd_puts("Elevation   ???*\n");
283
        lcd_puts("Cal Elevat. ---*\n");
-
 
284
 
-
 
285
    }else if( AZIMUTH == type ){ // azimuth position
-
 
286
        lcd_gotoxy(12,0);
-
 
287
        lcd_puts("   *");
-
 
288
        lcd_gotoxy(12,0);
351
 
289
 
352
    }else if( SCREEN_CAL == type){
290
    /*  if( data&0x8000 ){ //Negative position, -360 - 0
353
        // Screen for calibration
291
            //
354
        lcd_clrscr();
292
            lcd_gotoxy(0,1);
355
        lcd_puts("Cal Azimuth ???*\n");
293
            lcd_puts("     ");
356
        lcd_puts("Cal Elevat. ???*\n");
294
            lcd_gotoxy(0,1);
357
 
-
 
358
    }else if( AZIMUTH == type ){
295
            snprintf(buffer, BUFFER_SIZE, "%i", data);
359
        // Prints azimuth position or calibration data
296
            lcd_puts(buffer);
-
 
297
            //
360
        lcd_gotoxy(12,0);
298
            lcd_gotoxy(12,0);
361
        lcd_puts("   *");
299
            lcd_puts("-");
-
 
300
            data = -data;
-
 
301
            snprintf(buffer, BUFFER_SIZE-1, "%i", data);
362
        lcd_gotoxy(12,0);
302
            lcd_puts(buffer);
363
 
-
 
-
 
303
        }else{*/ //Positive position, 0 - 360
364
        snprintf(buffer, BUFFER_SIZE, "%i", data);
304
            snprintf(buffer, BUFFER_SIZE, "%i", data);
365
        lcd_puts(buffer);
305
                lcd_puts(buffer);
-
 
306
        //}
366
 
307
 
367
    }else if( ELEVATION == type ){
308
    }else if( ELEVATION == type ){ // elevation position
368
        // Prints elevation position or calibration data
-
 
369
        lcd_gotoxy(12,1);
309
        lcd_gotoxy(12,1);
370
        lcd_puts("   *");
310
        lcd_puts("   *");
371
        lcd_gotoxy(12,1);
311
        lcd_gotoxy(12,1);
372
 
312
 
-
 
313
        if( data&0x80 ){ //Negative position, -360 - 0
-
 
314
            lcd_puts("-");
-
 
315
            data = -data;
-
 
316
            snprintf(buffer, BUFFER_SIZE-1, "%i", data);
-
 
317
                lcd_puts(buffer);
-
 
318
        }else{ //Positive position, 0 - 360
373
        snprintf(buffer, BUFFER_SIZE, "%i", data);
319
            snprintf(buffer, BUFFER_SIZE, "%i", data);
374
        lcd_puts(buffer);
320
                lcd_puts(buffer);
375
    }
321
        }
376
}
322
    }
-
 
323
}
377
 
324
 
-
 
325
void IO_init( void )
-
 
326
{
378
/**
327
    /*
379
 * Enable buttons and display light
328
     * Enable buttons and display light
380
 *
-
 
381
 * PB0: Azimuth plus
-
 
382
 * PB7: Azimuth minus
-
 
383
 * PC0: Elevation plus
-
 
384
 * PC1: Elevation minus
-
 
385
 * PD2: Calibration
-
 
386
 */
329
     */
387
void IO_init( void )
-
 
388
{
330
 
389
    // Set as inputs
331
    // Set as inputs
390
    DDRD |= ( 1<<DDD2 ); // Calibration
332
    DDRD &= ~( 1<<DDD2 ); // Calibration
391
    DDRB |= ( 1<<DDB0 )|( 1<<DDB7 ); // Azimuth
333
    DDRB &= ~(( 1<<DDB0 )|( 1<<DDB7 )); // Azimuth
392
    DDRC |= ( 1<<DDC0 )|( 1<<DDC1 ); // Elevation
334
    DDRC &= ~(( 1<<DDC0 )|( 1<<DDC1 )); // Elevation
393
 
335
 
394
    // Enable pull up
336
    // Enable pull up
395
    PORTD |= ( 1<<PD2 );
337
    PORTD |= ( 1<<PD2 );
396
    PORTB |= ( 1<<PB0 )|( 1<<PB7 );
338
    PORTB |= ( 1<<PB0 )|( 1<<PB7 );
397
    PORTC |= ( 1<<PC0 )|( 1<<PC1 );
339
    PORTC |= ( 1<<PC0 )|( 1<<PC1 );
Line 406... Line 348...
406
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
348
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
407
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
349
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
408
    ICR1 = 0xFF; // Make it 8 bits
350
    ICR1 = 0xFF; // Make it 8 bits
409
    OCR1A = 0xC0;
351
    OCR1A = 0xC0;
410
    DDRB |= (1<<DDB1);
352
    DDRB |= (1<<DDB1);
411
   
-
 
412
    // Startup LCD
-
 
413
    lcd_init( LCD_DISP_ON );
-
 
414
 
-
 
415
    // Prints first characters to display
-
 
416
    print_screen( SCREEN_NORMAL, 0 );
-
 
417
}
353
}
418
 
354
 
419
 
355
 
420
void calibrate( void ){
356
void calibrate( void ){
421
    int16_t calib_value_az = 0, calib_value_elv = 0;
357
    int16_t calib_value_az = 0, calib_value_elv = 0;
422
   
358
   
423
    // Flags
359
    // flags
424
    uint8_t received_cal = 0, calibration_mode = 0;
360
    uint8_t received_cal = 0, calibration_mode = 0;
425
   
361
   
426
    // Show calibration display
-
 
427
    print_screen( SCREEN_CAL, 0 );
362
    print_screen( SCREEN_CAL, 0 );
428
   
363
   
429
    // Aquire old calibration data from node
-
 
430
    send_message( GET_CAL, 0 );
364
    send_message( GET_CAL, 0 );
431
   
365
   
432
    while ( 1 ) {
366
    while ( 1 ) {
433
   
367
   
434
        /* check if any messages have been received */
368
            /* check if any messages have been received */
435
        if( rxMsgFull ){
369
        if( rxMsgFull ){
436
            if( 0x111000f2 == rxMsg.Id && 2 == rxMsg.DataLength ){ // MSG_CAL_RET
370
            if( rxMsg.Id == 0x111000f2 && rxMsg.DataLength == 2 ){ // MSG_CAL_RET
437
                calib_value_az = rxMsg.Data.words[0];
371
                calib_value_az = rxMsg.Data.words[0];
438
               
372
               
439
                received_cal = 1;
373
                received_cal = 1;
440
               
-
 
441
                // Print old calibration data
-
 
442
                print_screen( AZIMUTH, calib_value_az );
374
                print_screen( AZIMUTH, calib_value_az );
443
            } //FIXME elevation
375
            }
444
               
376
               
445
            rxMsgFull = 0;
377
            rxMsgFull = 0;
446
        }
378
        }
447
   
379
   
448
        /* When we got the old calibration data and we want to adjust it */
-
 
449
        if ( Timer_Expired(1) && received_cal ) {
380
        if ( Timer_Expired(1) && received_cal ) {
450
            if ( !calibration ){// wait for released calibration button
381
            if ( !calibration ){// wait for released button
451
                calibration_mode = 1;
382
                calibration_mode = 1;
452
               
383
               
453
                if(azimuth_plus){
384
                if(azimuth_plus){
454
                    // Increase value
-
 
455
                    calib_value_az++;
385
                    calib_value_az++;
456
                    print_screen( AZIMUTH, calib_value_az );               
386
                    print_screen( AZIMUTH, calib_value_az );               
457
 
387
 
458
                }else if(azimuth_minus){
388
                }else if(azimuth_minus){
459
                    // Decrease value
-
 
460
                    calib_value_az--;
389
                    calib_value_az--;
461
                    print_screen( AZIMUTH, calib_value_az );
390
                    print_screen( AZIMUTH, calib_value_az );
462
                }
391
                }
463
 
392
   
464
                // FIXME elevation  
-
 
465
                if(elevation_plus){
393
                if(elevation_plus){
466
                    // Increase value
-
 
467
                    calib_value_elv++;
394
                    calib_value_elv++;
468
               
395
               
469
                }else if(elevation_minus){
396
                }else if(elevation_minus){
470
                    // Decrease value
-
 
471
                    calib_value_elv--;
397
                    calib_value_elv--;
472
                }  
398
                }  
473
 
399
 
474
            }else if( calibration && calibration_mode ){
400
            }else if( calibration && calibration_mode ){
475
                // Send new calibration value to antenna node
401
                // Send calibration value to antenna node
476
                send_message( SET_CAL, calib_value_az );
402
                send_message( SET_CAL, calib_value_az );
477
                //FIXME elevation
-
 
478
 
-
 
479
                // Return to normal screen
403
                // return to normal state
480
                print_screen( SCREEN_NORMAL, 0 );
404
                print_screen( SCREEN_NORMAL, 0 );
481
               
405
               
482
                return;
406
                return;
483
            }
407
            }
484
        }
408
        }