Subversion Repositories Projects

Rev

Rev 388 | Rev 400 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 388 Rev 389
Line 30... Line 30...
30
#include <util/delay.h>
30
#include <util/delay.h>
31
#include <avr/pgmspace.h> 
31
#include <avr/pgmspace.h> 
32
#include "main.h"
32
#include "main.h"
33
#include "max7456_software_spi.h"
33
#include "max7456_software_spi.h"
34
#include "usart1.h"
34
#include "usart1.h"
-
 
35
#include "osd_helpers.h"
Line 35... Line 36...
35
 
36
 
36
/* TODO:
37
/* TODO:
37
 * - verifiy correctness of values
38
 * - verifiy correctness of values
38
 * - clean up code :)
39
 * - clean up code :)
Line 74... Line 75...
74
 
75
 
75
// store more fixed strings in progmen
76
// store more fixed strings in progmen
76
char ON[] PROGMEM = "ON ";
77
char ON[] PROGMEM = "ON ";
Line 77... Line -...
77
char OFF[] PROGMEM = "OFF";
-
 
78
 
-
 
79
// big vario arrays 
-
 
80
char vario_00[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0xff};
-
 
81
char vario_01[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0xc0};
-
 
82
char vario_02[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0xc1};
-
 
83
char vario_03[5] PROGMEM = {0x00, 0x00, 0xc2, 0xff, 0x00};
-
 
84
char vario_04[5] PROGMEM = {0x00, 0x00, 0xc2, 0xc0, 0x00};
-
 
85
char vario_05[5] PROGMEM = {0x00, 0x00, 0xc2, 0xc1, 0x00};
-
 
86
char vario_06[5] PROGMEM = {0x00, 0x00, 0xc2, 0x00, 0x00};
-
 
87
char vario_07[5] PROGMEM = {0x00, 0x00, 0xbb, 0x00, 0x00};
-
 
88
char vario_08[5] PROGMEM = {0x00, 0x00, 0xc3, 0x00, 0x00};
-
 
89
char vario_09[5] PROGMEM = {0x00, 0xc4, 0xc3, 0x00, 0x00};
-
 
90
char vario_10[5] PROGMEM = {0x00, 0xc5, 0xc3, 0x00, 0x00};
-
 
91
char vario_11[5] PROGMEM = {0x00, 0xff, 0xc3, 0x00, 0x00};
-
 
92
char vario_12[5] PROGMEM = {0xc4, 0xff, 0xc3, 0x00, 0x00};
-
 
93
char vario_13[5] PROGMEM = {0xc5, 0xff, 0xc3, 0x00, 0x00};
-
 
94
char vario_14[5] PROGMEM = {0xff, 0xff, 0xc3, 0x00, 0x00};
-
 
95
char* vario_pnt[15] PROGMEM = {vario_00, vario_01, vario_02, vario_03, vario_04,
-
 
96
                                vario_05, vario_06, vario_07, vario_08,
-
 
97
                                vario_09, vario_10, vario_11, vario_12,
-
 
98
                                vario_13, vario_14};
78
char OFF[] PROGMEM = "OFF";
Line 99... Line 79...
99
 
79
 
100
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
80
#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))
101
 
81
 
Line 123... Line 103...
123
                if (S2_PRESSED) return 1;
103
                if (S2_PRESSED) return 1;
124
        }
104
        }
125
        return 0;
105
        return 0;
126
}
106
}
Line 127... Line -...
127
 
-
 
-
 
107
 
128
 
108
/* ##########################################################################
129
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
-
 
-
 
109
 * Interrupt handler
130
 
110
 * ##########################################################################*/
131
/**
111
/**
-
 
112
 * handler for undefined Interrupts
132
 * serial support
113
 * if not defined AVR will reset in case any unhandled interrupts occur
133
 */
114
 */
-
 
115
ISR(__vector_default) {
134
//#include "usart1.c"
116
        asm("nop");
Line -... Line 117...
-
 
117
}
135
 
118
 
136
 
119
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
137
/* ##########################################################################
120
/* ##########################################################################
138
 * timer stuff
-
 
139
 * ##########################################################################*/
121
 * timer stuff
140
 
122
 * ##########################################################################*/
141
/**
123
/**
142
 * timer kicks in every 1000uS ^= 1ms
124
 * timer kicks in every 1000uS ^= 1ms
143
 */
125
 */
Line 148... Line 130...
148
        timer = 999;
130
        timer = 999;
149
                seconds_since_last_data++;
131
                seconds_since_last_data++;
150
    }
132
    }
151
}
133
}
Line 152... Line -...
152
 
-
 
153
/* ##########################################################################
-
 
154
 * compass stuff
-
 
155
 * ##########################################################################*/
-
 
156
 
-
 
157
/**
-
 
158
 * convert the <heading> gotton from NC into an index
-
 
159
 */
-
 
160
uint8_t heading_conv(uint16_t heading) {
-
 
161
    if (heading > 23 && heading < 68) {
-
 
162
        //direction = "NE";
-
 
163
        return 0;
-
 
164
    } else if (heading > 67 && heading < 113) {
-
 
165
        //direction = "E ";
-
 
166
        return 1;
-
 
167
    } else if (heading > 112 && heading < 158) {
-
 
168
        //direction = "SE";
-
 
169
        return 2;
-
 
170
    } else if (heading > 157 && heading < 203) {
-
 
171
        //direction = "S ";
-
 
172
        return 3;
-
 
173
    } else if (heading > 202 && heading < 248) {
-
 
174
        //direction = "SW";
-
 
175
        return 4;
-
 
176
    } else if (heading > 247 && heading < 293) {
-
 
177
        //direction = "W ";
-
 
178
        return 5;
-
 
179
    } else if (heading > 292 && heading < 338) {
-
 
180
        //direction = "NW";
-
 
181
        return 6;
-
 
182
    }
-
 
183
    //direction = "N ";
-
 
184
    return 7;
-
 
185
}
-
 
186
 
-
 
187
/**
-
 
188
 * convert the <heading> gotton from NC into a more
-
 
189
 * precise index
-
 
190
 */
-
 
191
uint8_t heading_fine_conv(uint16_t heading) {
-
 
192
        heading = ((heading * 10) + 113) % 3600;
-
 
193
        return (heading / 225);
-
 
194
}
-
 
195
 
-
 
196
/**
-
 
197
 * draw a compass rose at <x>/<y> for <heading>
-
 
198
 */
-
 
199
void draw_compass(uint8_t x, uint8_t y, uint16_t heading) {
-
 
200
    //char* rose = "---N---O---S---W---N---O---S---W---N---O---S---W";
-
 
201
    char rose[48] = {216, 215, 216, 211, 216, 215, 216, 213, 216, 215, 216, 212,
-
 
202
                    216, 215, 216, 214, 216, 215, 216, 211, 216, 215, 216, 213,
-
 
203
                    216, 215, 216, 212, 216, 215, 216, 214, 216, 215, 216, 211,
-
 
204
                    216, 215, 216, 213, 216, 215, 216, 212, 216, 215, 216, 214};
-
 
205
        // the center is char 19 (north), we add the current heading in 8th
-
 
206
        // which would be 22.5 degrees, but float would bloat up the code
-
 
207
        // and *10 / 225 would take ages... so we take the uncorrect way
-
 
208
    uint8_t front = 19 + (heading / 22);
-
 
209
    for (uint8_t i = 0; i < 9; i++) {
-
 
210
                write_char_xy(x++, y, rose[front - 4 + i]);
-
 
211
    }
-
 
212
}
-
 
213
 
-
 
214
/* ##########################################################################
-
 
215
 * battery index
-
 
216
 * ##########################################################################*/
-
 
217
/**
-
 
218
 * draw a battery symbol at <x>/<y> according to <voltage>
-
 
219
 */
-
 
220
void draw_battery(uint8_t x, uint8_t y, uint16_t voltage) {
-
 
221
        uint8_t percent = (100* (voltage - UBAT_WRN) / (UBAT_MAX - UBAT_WRN));
-
 
222
        if (percent > 100) percent = 100;
-
 
223
        if (voltage < UBAT_WRN) percent = 0;
-
 
224
        write_char_xy(x, y, 0x9d - (percent * 13 / 100));
-
 
225
        //write_ndigit_number_u(x, y-1, percent * 13 / 100, 100, 0);
-
 
226
}
-
 
227
 
-
 
228
/* ##########################################################################
-
 
229
 * variometer
-
 
230
 * ##########################################################################*/
-
 
231
/**
-
 
232
 * draw variometer arrows at <x>/<y> according to <variometer>
-
 
233
 */
-
 
234
void draw_variometer(uint8_t x, uint8_t y, int16_t variometer) {
-
 
235
        if (variometer == 0) {
-
 
236
                write_char_xy(x, y, 0xbb); // plain line
-
 
237
        } else if (variometer > 0) { // gain height
-
 
238
                switch (variometer / 5){
-
 
239
                        case 0:
-
 
240
                                //write_char_xy(x, y, 0xba); // smallest arrow up
-
 
241
                                write_char_xy(x, y, 0x70); // one arrow up
-
 
242
                                break;
-
 
243
                        case 1:
-
 
244
                                //write_char_xy(x, y, 0xb9); // small arrow up
-
 
245
                                write_char_xy(x, y, 0x71); // two arrows up
-
 
246
                                break;
-
 
247
                        case 2:
-
 
248
                                //write_char_xy(x, y, 0xb8); // large arrow up
-
 
249
                                write_char_xy(x, y, 0x72); // three arrows up
-
 
250
                                break;
-
 
251
                        default:
-
 
252
                                //write_char_xy(x, y, 0xb7); // largest arrow up
-
 
253
                                write_char_xy(x, y, 0x73); // three black arrows up
-
 
254
                }
-
 
255
        } else { // sink
-
 
256
                switch (variometer / -5){
-
 
257
                        case 0:
-
 
258
                                //write_char_xy(x, y, 0xbc); // smallest arrow down
-
 
259
                                write_char_xy(x, y, 0x77); // one arrow down
-
 
260
                                break;
-
 
261
                        case 1:
-
 
262
                                //write_char_xy(x, y, 0xbd); // small arrow down
-
 
263
                                write_char_xy(x, y, 0x76); // two arrows down
-
 
264
                                break;
-
 
265
                        case 2:
-
 
266
                                //write_char_xy(x, y, 0xbe); // large arrow down
-
 
267
                                write_char_xy(x, y, 0x75); // three arrows down
-
 
268
                                break;
-
 
269
                        default:
-
 
270
                                //write_char_xy(x, y, 0xbf); // largest arrow down
-
 
271
                                write_char_xy(x, y, 0x74); // three black arrows down
-
 
272
                }
-
 
273
        }
-
 
274
}
-
 
275
 
-
 
276
/**
-
 
277
 * draw a bigger vario with middle at <x>/<y> acording to <variometer>
-
 
278
 */
-
 
279
void draw_big_variometer(uint8_t x, uint8_t y, int16_t variometer) {   
-
 
280
        int16_t index = 7 + variometer;
-
 
281
        if (index > 14) index = 14;
-
 
282
        else if (index < 0) index = 0;
-
 
283
       
-
 
284
        // TODO: why does write_string_pgm_down(x, y-2, vario_pnt[index], 5);
-
 
285
        // not work??? WTF?!
-
 
286
        switch (index) {
-
 
287
                case 0: write_string_pgm_down(x, y-2, vario_pnt[0], 5);
-
 
288
                        break;
-
 
289
                case 1: write_string_pgm_down(x, y-2, vario_pnt[1], 5);
-
 
290
                        break;
-
 
291
                case 2: write_string_pgm_down(x, y-2, vario_pnt[2], 5);
-
 
292
                        break;
-
 
293
                case 3: write_string_pgm_down(x, y-2, vario_pnt[3], 5);
-
 
294
                        break;
-
 
295
                case 4: write_string_pgm_down(x, y-2, vario_pnt[4], 5);
-
 
296
                        break;
-
 
297
                case 5: write_string_pgm_down(x, y-2, vario_pnt[5], 5);
-
 
298
                        break;
-
 
299
                case 6: write_string_pgm_down(x, y-2, vario_pnt[6], 5);
-
 
300
                        break;
-
 
301
                case 7: write_string_pgm_down(x, y-2, vario_pnt[7], 5);
-
 
302
                        break;
-
 
303
                case 8: write_string_pgm_down(x, y-2, vario_pnt[8], 5);
-
 
304
                        break;
-
 
305
                case 9: write_string_pgm_down(x, y-2, vario_pnt[9], 5);
-
 
306
                        break;
-
 
307
                case 10: write_string_pgm_down(x, y-2, vario_pnt[10], 5);
-
 
308
                        break;
-
 
309
                case 11: write_string_pgm_down(x, y-2, vario_pnt[11], 5);
-
 
310
                        break;
-
 
311
                case 12: write_string_pgm_down(x, y-2, vario_pnt[12], 5);
-
 
312
                        break;
-
 
313
                case 13: write_string_pgm_down(x, y-2, vario_pnt[13], 5);
-
 
314
                        break;
-
 
315
                default: write_string_pgm_down(x, y-2, vario_pnt[14], 5);
-
 
316
        }              
-
 
317
}
-
 
318
 
-
 
319
/* ##########################################################################
-
 
320
 * artificial horizon
-
 
321
 * ##########################################################################*/
-
 
322
// remember last time displayed values
-
 
323
int8_t old_af_x = -1, old_af_y = -1;
-
 
324
 
-
 
325
/**
-
 
326
 * draw roll und nick indicators (could be enhanced to full artificial horizon)
-
 
327
 * from line <firstline> to <listlines> for given <nick> and <roll> values
-
 
328
 */
-
 
329
void draw_artificial_horizon(uint8_t firstline, uint8_t lastline, int16_t nick, int16_t roll) {
-
 
330
        char noodle[5] = {225, 225, 226, 227, 227};
-
 
331
        uint8_t center_x = 15;
-
 
332
        uint8_t center_y = lastline - firstline;
-
 
333
        center_y = 7;
-
 
334
        write_char_xy(center_x,center_y,228);
-
 
335
        uint8_t cpos, nicky, rollx;
-
 
336
       
-
 
337
        // which line
-
 
338
        int8_t ypos =  nick / 20;
-
 
339
        // which character from the array?
-
 
340
        if (nick < 0) {
-
 
341
                cpos = -1*((nick - (ypos * 20))/4);
-
 
342
                ypos--;
-
 
343
        } else cpos = 4-((nick - (ypos * 20))/4);
-
 
344
        if (cpos > 4) cpos = 4;
-
 
345
 
-
 
346
        nicky = center_y - ypos;
-
 
347
        if (nicky > lastline) nicky = lastline;
-
 
348
        else if (nicky < firstline) nicky = firstline;
-
 
349
 
-
 
350
        // ensure roll-borders
-
 
351
        rollx = (roll / 8)+15;
-
 
352
        if (rollx < 2) rollx = 2;
-
 
353
        else if (rollx > 28) rollx = 28;
-
 
354
 
-
 
355
 
-
 
356
        // clear roll
-
 
357
        if (old_af_x != rollx && old_af_x >= 0) {
-
 
358
                write_char_xy(old_af_x,13,0);
-
 
359
        }
-
 
360
 
-
 
361
        // clear nick
-
 
362
        if (old_af_y != nicky && old_af_y >= 0) {
-
 
363
                write_char_xy(center_x-1,old_af_y,0);
-
 
364
                write_char_xy(center_x+1,old_af_y,0);
-
 
365
        }
-
 
366
 
-
 
367
 
-
 
368
        // draw nick
-
 
369
        write_char_xy(center_x-1,nicky,noodle[cpos]);
-
 
370
        write_char_xy(center_x+1,nicky,noodle[cpos]);
-
 
371
 
-
 
372
        // draw roll
-
 
373
        write_char_xy(rollx,lastline,229);
-
 
374
 
-
 
375
        // update old vars
-
 
376
        old_af_x = rollx;
-
 
377
        old_af_y = nicky;
-
 
378
 
-
 
379
        // debug numbers
-
 
380
        //write_3digit_number_u(20,6,cpos);
-
 
381
        //write_number_s(20,7,ypos);    
-
 
382
        //write_number_s(0,7,nick);             
-
 
383
        //write_number_s(18,11,roll);   
-
 
Line 384... Line 134...
384
}
134
 
385
 
135
 
386
/* ##########################################################################
136
/* ##########################################################################
Line 630... Line 380...
630
 
380
 
631
    //write_ascii_string(2,  7, "         CaScAdE          ");
381
    //write_ascii_string(2,  7, "         CaScAdE          ");
632
    //write_ascii_string(2,  8, "is TESTING his open source");
382
    //write_ascii_string(2,  8, "is TESTING his open source");
Line 633... Line -...
633
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
-
 
634
 
-
 
635
    // custom char preview
-
 
636
    /*write_char_xy( 2, 7, 200);
-
 
637
    write_char_xy( 3, 7, 201);
-
 
638
    write_char_xy( 4, 7, 202);
-
 
639
    write_char_xy( 5, 7, 203);
-
 
640
    write_char_xy( 6, 7, 204);
-
 
641
    write_char_xy( 7, 7, 205);
-
 
642
    write_char_xy( 8, 7, 206);
-
 
643
    write_char_xy( 9, 7, 207);
-
 
644
    write_char_xy(10, 7, 208);
-
 
645
    write_char_xy(11, 7, 209);
-
 
646
    write_char_xy(12, 7, 210);
-
 
647
    write_char_xy(13, 7, 211);
-
 
648
    write_char_xy(14, 7, 212);
-
 
649
    write_char_xy(15, 7, 213);
-
 
650
    write_char_xy(16, 7, 214);
-
 
651
    write_char_xy(17, 7, 215);*/
383
    //write_ascii_string(2,  9, "    EPi OSD Firmware");
652
 
384
 
Line 653... Line -...
653
    // we are ready
-
 
654
    LED3_ON
-
 
655
 
385
    // we are ready
656
 
386
    LED3_ON
657
 
387
 
658
#if ALLCHARSDEBUG | (WRITECHARS != -1)
388
#if ALLCHARSDEBUG | (WRITECHARS != -1)
659
        clear();
389
        clear();
Line 671... Line 401...
671
        usart1_request_mk_data(1, 'o', 100);
401
        usart1_request_mk_data(1, 'o', 100);
Line 672... Line 402...
672
 
402
 
673
    // and disable debug...
403
    // and disable debug...
Line 674... Line -...
674
        usart1_request_mk_data(0, 'd', 0);
-
 
675
 
-
 
676
    // disable TXD-pin
-
 
677
    //usart1_DisableTXD();
404
        usart1_request_mk_data(0, 'd', 0);
678
 
405
 
679
    // stats for after flight
406
    // stats for after flight
680
    int16_t max_Altimeter = 0;
407
    int16_t max_Altimeter = 0;
681
    uint16_t max_GroundSpeed = 0;
408
    uint16_t max_GroundSpeed = 0;
Line 687... Line 414...
687
    uint8_t old_MKFlags = 0;
414
    uint8_t old_MKFlags = 0;
Line 688... Line 415...
688
 
415
 
689
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
416
    char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "};
Line 690... Line -...
690
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
-
 
691
 
-
 
692
        /*write_ndigit_number_s_10th(5, 10, 1, 100, 0);
-
 
693
        write_ndigit_number_s_10th(5, 11, -1, 100, 0);
-
 
694
        write_ndigit_number_s_10th(5, 12, -11, 100, 0);*/
-
 
695
 
-
 
696
        //write_ndigit_number_u(2,2, heading_fine_conv(34), 100, 0);
-
 
697
        //write_char_xy(1, 2, 0xa0 + heading_fine_conv(34));
417
    //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219};
698
 
418
 
699
    while (1) {
419
    while (1) {
700
        // write icons at init or after menu/mode-switch
420
        // write icons at init or after menu/mode-switch
701
        if (!(COSD_FLAGS & COSD_ICONS_WRITTEN) && (COSD_FLAGS & COSD_FLAG_HUD)) {
421
        if (!(COSD_FLAGS & COSD_ICONS_WRITTEN) && (COSD_FLAGS & COSD_FLAG_HUD)) {
Line 853... Line 573...
853
        // handle keypress
573
        // handle keypress
854
        if (s1_pressed()) {
574
        if (s1_pressed()) {
855
            config_menu();
575
            config_menu();
856
        }
576
        }
857
        if (seconds_since_last_data > 2) {
577
        if (seconds_since_last_data > 2) {
858
                        /*if (COSD_FLAGS & COSD_FLAG_HUD) {
-
 
859
                                write_ascii_string(2, 3, "ERROR: NO DATA for");
-
 
860
                                write_time(21, 3, seconds_since_last_data);
-
 
861
                        }*/
-
 
862
                        // request OSD Data from NC every 100ms
578
                        // request OSD Data from NC every 100ms
863
                        usart1_request_mk_data(1, 'o', 100);
579
                        usart1_request_mk_data(1, 'o', 100);
864
                        _delay_ms(200);
-
 
865
                        //seconds_since_last_data = 0;
580
                        seconds_since_last_data = 0;
866
        }
581
        }
867
    }
582
    }
868
#endif
583
#endif
869
    return 0;
584
    return 0;
870
}
585
}