Rev 468 | Rev 474 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 468 | Rev 471 | ||
---|---|---|---|
1 | /**************************************************************************** |
1 | /**************************************************************************** |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
3 | * admiralcascade@gmail.com * |
3 | * admiralcascade@gmail.com * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
5 | * * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License. * |
8 | * the Free Software Foundation; either version 2 of the License. * |
9 | * * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
13 | * GNU General Public License for more details. * |
14 | * * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * * |
19 | * * |
20 | * * |
20 | * * |
21 | * Credits to: * |
21 | * Credits to: * |
22 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project * |
22 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN * |
23 | * Gregor "killagreg" Stobrawa for making the MK code readable * |
23 | * Gregor "killagreg" Stobrawa for making the MK code readable * |
24 | * Klaus "akku" Buettner for the hardware * |
24 | * Klaus "akku" Buettner for the hardware * |
25 | * Manuel "KeyOz" Schrape for explaining the MK protocol to me * |
25 | * Manuel "KeyOz" Schrape for explaining the MK protocol to me * |
26 | ****************************************************************************/ |
26 | ****************************************************************************/ |
27 | 27 | ||
28 | #include <avr/io.h> |
28 | #include <avr/io.h> |
29 | #include <avr/interrupt.h> |
29 | #include <avr/interrupt.h> |
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" |
35 | #include "osd_helpers.h" |
- | 36 | #include "config.h" |
|
- | 37 | #include "spi.h" |
|
- | 38 | #include "buttons.h" |
|
36 | 39 | ||
37 | /* TODO: |
40 | /* TODO: |
38 | * - verifiy correctness of values |
41 | * - verifiy correctness of values |
39 | * - clean up code :) |
42 | * - clean up code :) |
40 | */ |
43 | */ |
41 | 44 | ||
42 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
45 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
43 | // data structs not needed for character flashing |
46 | // data structs not needed for character flashing |
44 | #include "mk-data-structs.h" |
47 | #include "mk-data-structs.h" |
45 | 48 | ||
46 | /* ########################################################################## |
49 | /* ########################################################################## |
47 | * global definitions and global vars |
50 | * global definitions and global vars |
48 | * ##########################################################################*/ |
51 | * ##########################################################################*/ |
49 | 52 | ||
50 | volatile uint16_t setsReceived = 0; |
53 | volatile uint16_t setsReceived = 0; |
51 | 54 | ||
52 | volatile NaviData_t naviData; |
55 | volatile NaviData_t naviData; |
53 | volatile DebugOut_t debugData; |
56 | volatile DebugOut_t debugData; |
54 | 57 | ||
55 | // cache old vars for blinking attribute, checkup is faster than full |
58 | // cache old vars for blinking attribute, checkup is faster than full |
56 | // attribute write each time |
59 | // attribute write each time |
57 | volatile uint8_t last_UBat = 255; |
60 | volatile uint8_t last_UBat = 255; |
58 | volatile uint8_t last_RC_Quality = 255; |
61 | volatile uint8_t last_RC_Quality = 255; |
59 | 62 | ||
60 | // 16bit should be enough, normal LiPos don't last that long |
63 | // 16bit should be enough, normal LiPos don't last that long |
61 | volatile uint16_t uptime = 0; |
64 | volatile uint16_t uptime = 0; |
62 | volatile uint16_t timer = 0; |
65 | volatile uint16_t timer = 0; |
63 | 66 | ||
64 | // remember last time data was received |
67 | // remember last time data was received |
65 | volatile uint8_t seconds_since_last_data = 0; |
68 | volatile uint8_t seconds_since_last_data = 0; |
66 | 69 | ||
67 | // store stats description in progmem to save space |
70 | // store stats description in progmem to save space |
68 | char stats_item_0[] PROGMEM = "max Altitude:"; |
71 | char stats_item_0[] PROGMEM = "max Altitude:"; |
69 | char stats_item_1[] PROGMEM = "max Speed :"; |
72 | char stats_item_1[] PROGMEM = "max Speed :"; |
70 | char stats_item_2[] PROGMEM = "max Distance:"; |
73 | char stats_item_2[] PROGMEM = "max Distance:"; |
71 | char stats_item_3[] PROGMEM = "min voltage :"; |
74 | char stats_item_3[] PROGMEM = "min voltage :"; |
72 | char stats_item_4[] PROGMEM = "max time :"; |
75 | char stats_item_4[] PROGMEM = "max time :"; |
73 | char stats_item_5[] PROGMEM = "longitude :"; |
76 | char stats_item_5[] PROGMEM = "longitude :"; |
74 | char stats_item_6[] PROGMEM = "latitude :"; |
77 | char stats_item_6[] PROGMEM = "latitude :"; |
75 | char stats_item_7[] PROGMEM = "max current :"; |
78 | char stats_item_7[] PROGMEM = "max current :"; |
76 | char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
79 | char* stats_item_pointers[] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
77 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
80 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
78 | - | ||
79 | // store more fixed strings in progmen |
- | |
80 | char ON[] PROGMEM = "ON "; |
- | |
81 | char OFF[] PROGMEM = "OFF"; |
- | |
82 | 81 | ||
83 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
82 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
84 | 83 | ||
85 | // general PAL|NTSC distingiusch stuff |
84 | // general PAL|NTSC distingiusch stuff |
86 | uint8_t top_line = 1; |
85 | uint8_t top_line = 1; |
87 | uint8_t bottom_line = 14; |
86 | uint8_t bottom_line = 14; |
88 | 87 | ||
89 | // battery voltages |
88 | // battery voltages |
90 | uint8_t min_voltage = 0; |
89 | uint8_t min_voltage = 0; |
91 | uint8_t max_voltage = 0; |
90 | uint8_t max_voltage = 0; |
92 | 91 | ||
93 | // Flags |
92 | // Flags |
94 | uint8_t COSD_FLAGS = 0, COSD_FLAGS2; |
93 | uint8_t COSD_FLAGS = 0, COSD_FLAGS2 = 0; |
95 | - | ||
96 | /* ########################################################################## |
- | |
97 | * debounce buttons |
- | |
98 | * ##########################################################################*/ |
- | |
99 | int s1_pressed() { |
- | |
100 | if (S1_PRESSED) { |
- | |
101 | _delay_ms(25); |
- | |
102 | if (S1_PRESSED) return 1; |
- | |
103 | } |
- | |
104 | return 0; |
- | |
105 | } |
- | |
106 | - | ||
107 | int s2_pressed() { |
- | |
108 | if (S2_PRESSED) { |
- | |
109 | _delay_ms(25); |
- | |
110 | if (S2_PRESSED) return 1; |
- | |
111 | } |
- | |
112 | return 0; |
- | |
113 | } |
- | |
114 | 94 | ||
115 | /* ########################################################################## |
95 | /* ########################################################################## |
116 | * Interrupt handler |
96 | * Interrupt handler |
117 | * ##########################################################################*/ |
97 | * ##########################################################################*/ |
118 | 98 | ||
119 | /** |
99 | /** |
120 | * handler for undefined Interrupts |
100 | * handler for undefined Interrupts |
121 | * if not defined AVR will reset in case any unhandled interrupts occur |
101 | * if not defined AVR will reset in case any unhandled interrupts occur |
122 | */ |
102 | */ |
123 | ISR(__vector_default) { |
103 | ISR(__vector_default) { |
124 | asm("nop"); |
104 | asm("nop"); |
125 | } |
105 | } |
126 | 106 | ||
127 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
107 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
128 | /* ########################################################################## |
108 | /* ########################################################################## |
129 | * timer stuff |
109 | * timer stuff |
130 | * ##########################################################################*/ |
110 | * ##########################################################################*/ |
131 | - | ||
132 | - | ||
133 | /* ########################################################################## |
- | |
134 | * STROM TEST |
- | |
135 | * ##########################################################################*/ |
- | |
136 | #define INT0_HIGH PORTD |= (1 << PD2); |
- | |
137 | #define INT0_LOW PORTD &= ~(1 << PD2); |
- | |
138 | - | ||
139 | void SpiMasterInit(void) { |
- | |
140 | volatile char IOReg; |
- | |
141 | // set PB4(/SS), PB5(MOSI), PB7(SCK) as output |
- | |
142 | DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7); |
- | |
143 | PORTB |= (1 << PB4); // pullup SS |
- | |
144 | // enable SPI Interrupt and SPI in Master Mode with SCK = CK/128 |
- | |
145 | SPCR = (1 << SPIE) | (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << SPR1); |
- | |
146 | IOReg = SPSR; // clear SPIF bit in SPSR |
- | |
147 | IOReg = SPDR; |
- | |
148 | //sei(); // we do it later |
- | |
149 | } |
- | |
150 | - | ||
151 | volatile size_t icnt = 0; |
- | |
152 | volatile unsigned char * iptr; |
- | |
153 | volatile unsigned char spi_cmd_buffer[8]; |
- | |
154 | volatile uint8_t spi_ready = 1; |
- | |
155 | int16_t ampere = 0, max_ampere = 0; |
- | |
156 | int32_t ampere_wasted = 0; |
- | |
157 | - | ||
158 | /** |
- | |
159 | * SPI interrupt handler |
- | |
160 | */ |
- | |
161 | ISR(SPI_STC_vect) { |
- | |
162 | *iptr++ = SPDR; // safe received byte to buffer |
- | |
163 | icnt--; // dec length |
- | |
164 | if (icnt) { |
- | |
165 | //SPDR = *iptr; // send next byte |
- | |
166 | spi_ready = 1; // we _should_ send later because the slave needs more time |
- | |
167 | } else { |
- | |
168 | SPCR &= ~_BV(SPIE); // deactivate interrupt |
- | |
169 | INT0_HIGH // transfer is done, slave does not need to listen |
- | |
170 | } |
- | |
171 | } |
- | |
172 | - | ||
173 | /** |
- | |
174 | * check if SPI transfer is still busy |
- | |
175 | */ |
- | |
176 | int TransferIsBusy(void) { |
- | |
177 | return SPCR & _BV(SPIE); |
- | |
178 | } |
- | |
179 | - | ||
180 | /** |
- | |
181 | * start a new transfer of <data> with length <len> |
- | |
182 | */ |
- | |
183 | void StartTransfer(unsigned char *data, size_t len) { |
- | |
184 | INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen |
- | |
185 | - | ||
186 | // set up pointer and length for interrupt handler |
- | |
187 | iptr = data; |
- | |
188 | icnt = len; |
- | |
189 | - | ||
190 | SPCR |= _BV(SPIE); // enable spi interrupt |
- | |
191 | SPDR = *iptr; // start transfer by first bye |
- | |
192 | } |
- | |
193 | - | ||
194 | - | ||
195 | /* ########################################################################## |
- | |
196 | * / STROM TEST END |
- | |
197 | * ##########################################################################*/ |
- | |
198 | 111 | ||
199 | /** |
112 | /** |
200 | * timer kicks in every 1000uS ^= 1ms |
113 | * timer kicks in every 1000uS ^= 1ms |
201 | */ |
114 | */ |
202 | ISR(TIMER0_OVF_vect) { |
115 | ISR(TIMER0_OVF_vect) { |
203 | OCR0 = 15; // preload |
116 | OCR0 = 15; // preload |
204 | if (!timer--) { |
117 | if (!timer--) { |
205 | uptime++; |
118 | uptime++; |
206 | timer = 999; |
119 | timer = 999; |
207 | seconds_since_last_data++; |
120 | seconds_since_last_data++; |
208 | } |
121 | } |
209 | // in case there is still some spi data to send do it now |
122 | // in case there is still some spi data to send do it now |
210 | if (spi_ready && icnt) { |
123 | if (spi_ready && icnt) { |
211 | SPDR = *iptr; |
124 | SPDR = *iptr; |
212 | } |
125 | } |
213 | } |
126 | } |
214 | - | ||
215 | /* ########################################################################## |
- | |
216 | * A simple config menu for the flags |
- | |
217 | * ##########################################################################*/ |
- | |
218 | - | ||
219 | /** |
- | |
220 | * helper function for menu updating |
- | |
221 | */ |
- | |
222 | void config_menu_drawings(uint8_t chosen) { |
- | |
223 | // clear prevoius _cursor_ |
- | |
224 | write_ascii_string(3, (chosen + 5) % 8, " "); |
- | |
225 | // draw current _cursor_ |
- | |
226 | write_ascii_string(3, chosen + 5, ">"); |
- | |
227 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
- | |
228 | write_ascii_string_pgm(23, 5, ON); |
- | |
229 | } else { |
- | |
230 | write_ascii_string_pgm(23, 5, OFF); |
- | |
231 | } |
- | |
232 | if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) { |
- | |
233 | write_ascii_string_pgm(23, 6, ON); |
- | |
234 | } else { |
- | |
235 | write_ascii_string_pgm(23, 6, OFF); |
- | |
236 | } |
- | |
237 | if (COSD_FLAGS & COSD_FLAG_BIGVARIO) { |
- | |
238 | write_ascii_string_pgm(23, 7, ON); |
- | |
239 | } else { |
- | |
240 | write_ascii_string_pgm(23, 7, OFF); |
- | |
241 | } |
- | |
242 | if (COSD_FLAGS & COSD_FLAG_STATS) { |
- | |
243 | write_ascii_string_pgm(23, 8, ON); |
- | |
244 | } else { |
- | |
245 | write_ascii_string_pgm(23, 8, OFF); |
- | |
246 | } |
- | |
247 | if (COSD_FLAGS & COSD_FLAG_WARNINGS) { |
- | |
248 | write_ascii_string_pgm(23, 9, ON); |
- | |
249 | } else { |
- | |
250 | write_ascii_string_pgm(23, 9, OFF); |
- | |
251 | } |
- | |
252 | } |
- | |
253 | - | ||
254 | /** |
- | |
255 | * some sort of clicking response in the menu |
- | |
256 | */ |
- | |
257 | void config_menu_doclick(uint8_t chosen, char** menu) { |
- | |
258 | write_ascii_string(4, chosen + 5, "DONE "); |
- | |
259 | _delay_ms(1000); |
- | |
260 | write_ascii_string(4, chosen + 5, menu[chosen]); |
- | |
261 | } |
- | |
262 | - | ||
263 | /** |
- | |
264 | * a simple config menu tryout |
- | |
265 | */ |
- | |
266 | void config_menu(void) { |
- | |
267 | // disable interrupts (makes the menu more smoothely) |
- | |
268 | cli(); |
- | |
269 | - | ||
270 | // clear screen |
- | |
271 | clear(); |
- | |
272 | - | ||
273 | char* menu[9] = {"Full HUD", |
- | |
274 | "Art.Horizon in HUD", |
- | |
275 | "Big Vario bar", |
- | |
276 | "Statistics", |
- | |
277 | "Warnings", // TODO: do it! |
- | |
278 | "Reset uptime", |
- | |
279 | "Request OSD-data", |
- | |
280 | "Disable Debug-data", |
- | |
281 | "EXIT"}; |
- | |
282 | - | ||
283 | uint8_t inmenu = 1; |
- | |
284 | uint8_t chosen = 0; |
- | |
285 | write_ascii_string(6, 2, "C-OSD Config Menu"); |
- | |
286 | - | ||
287 | // wait a bit before doing stuff so user has chance to release button |
- | |
288 | _delay_ms(250); |
- | |
289 | - | ||
290 | write_ascii_string(4, 5, menu[0]); |
- | |
291 | write_ascii_string(4, 6, menu[1]); |
- | |
292 | write_ascii_string(4, 7, menu[2]); |
- | |
293 | write_ascii_string(4, 8, menu[3]); |
- | |
294 | write_ascii_string(4, 9, menu[4]); |
- | |
295 | write_ascii_string(4, 10, menu[5]); |
- | |
296 | write_ascii_string(4, 11, menu[6]); |
- | |
297 | write_ascii_string(4, 12, menu[7]); |
- | |
298 | write_ascii_string(4, 13, menu[8]); |
- | |
299 | - | ||
300 | config_menu_drawings(chosen); |
- | |
301 | - | ||
302 | while (inmenu) { |
- | |
303 | if (s2_pressed()) { |
- | |
304 | write_ascii_string(3, chosen + 5, " "); |
- | |
305 | chosen = (chosen + 1) % 9; |
- | |
306 | write_ascii_string(3, chosen + 5, ">"); |
- | |
307 | _delay_ms(500); |
- | |
308 | } else if (s1_pressed()) { |
- | |
309 | switch (chosen) { |
- | |
310 | case 0: // full HUD |
- | |
311 | COSD_FLAGS ^= COSD_FLAG_HUD; |
- | |
312 | config_menu_drawings(chosen); |
- | |
313 | break; |
- | |
314 | case 1: // art horizon |
- | |
315 | COSD_FLAGS ^= COSD_FLAG_ARTHORIZON; |
- | |
316 | config_menu_drawings(chosen); |
- | |
317 | break; |
- | |
318 | case 2: // big vario |
- | |
319 | COSD_FLAGS ^= COSD_FLAG_BIGVARIO; |
- | |
320 | config_menu_drawings(chosen); |
- | |
321 | break; |
- | |
322 | case 3: // statistics |
- | |
323 | COSD_FLAGS ^= COSD_FLAG_STATS; |
- | |
324 | config_menu_drawings(chosen); |
- | |
325 | break; |
- | |
326 | case 4: // warnings |
- | |
327 | COSD_FLAGS ^= COSD_FLAG_WARNINGS; |
- | |
328 | config_menu_drawings(chosen); |
- | |
329 | break; |
- | |
330 | case 5: // reset uptime |
- | |
331 | uptime = 0; |
- | |
332 | config_menu_doclick(chosen, menu); |
- | |
333 | break; |
- | |
334 | case 6: // re-request OSD data |
- | |
335 | #if FCONLY |
- | |
336 | // request data ever 100ms from FC; |
- | |
337 | usart1_request_mk_data(0, 'd', 100); |
- | |
338 | #else |
- | |
339 | // request OSD Data from NC every 100ms |
- | |
340 | usart1_request_mk_data(1, 'o', 100); |
- | |
341 | - | ||
342 | // and disable debug... |
- | |
343 | usart1_request_mk_data(0, 'd', 0); |
- | |
344 | #endif |
- | |
345 | config_menu_doclick(chosen, menu); |
- | |
346 | break; |
- | |
347 | case 7: // disable debug data |
- | |
348 | // disable sending of debug data |
- | |
349 | // may result in smoother ddata display |
- | |
350 | usart1_request_mk_data(0, 'd', 0); |
- | |
351 | config_menu_doclick(chosen, menu); |
- | |
352 | break; |
- | |
353 | case 8: // exit |
- | |
354 | inmenu = 0; |
- | |
355 | break; |
- | |
356 | } |
- | |
357 | _delay_ms(250); |
- | |
358 | } |
- | |
359 | } |
- | |
360 | - | ||
361 | // clear screen up again |
- | |
362 | clear(); |
- | |
363 | - | ||
364 | // update flags to paint display again if needed |
- | |
365 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
- | |
366 | - | ||
367 | // enable interrupts again |
- | |
368 | sei(); |
- | |
369 | } |
- | |
370 | - | ||
371 | /** |
- | |
372 | * auto config some stuff on startup, currently only battery cells |
- | |
373 | * TODO: this is testing stuff, strings should go progmem and so on... |
- | |
374 | */ |
- | |
375 | void auto_config(uint8_t UBat) { |
- | |
376 | clear(); |
- | |
377 | write_ascii_string(2, 2, "C-OSD Initialisation"); |
- | |
378 | #if FCONLY |
- | |
379 | write_ascii_string(2, 3, "FC only Mode"); |
- | |
380 | #else |
- | |
381 | write_ascii_string(2, 3, "NaviCtrl Mode"); |
- | |
382 | #endif |
- | |
383 | write_ascii_string(2, 4, BUILDDATE); |
- | |
384 | uint8_t cellnum = 0; |
- | |
385 | if (CELL_NUM == -1) { |
- | |
386 | write_ascii_string(2, 6, "Guessing Number of Cells"); |
- | |
387 | do { |
- | |
388 | cellnum++; |
- | |
389 | } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23)); |
- | |
390 | } else { |
- | |
391 | cellnum = CELL_NUM; |
- | |
392 | } |
- | |
393 | min_voltage = cellnum * CELL_VOLT_MIN; |
- | |
394 | max_voltage = cellnum * CELL_VOLT_MAX; |
- | |
395 | write_ascii_string(2, 7, "Number of Cells:"); |
- | |
396 | write_ndigit_number_u(21, 7, cellnum, 1, 0); |
- | |
397 | write_ascii_string(2, 8, "Warn Voltage :"); |
- | |
398 | write_ndigit_number_s_10th(20, 8, min_voltage, 100, 0); |
- | |
399 | write_ascii_string(2, 9, "Max Voltage :"); |
- | |
400 | write_ndigit_number_s_10th(20, 9, max_voltage, 100, 0); |
- | |
401 | _delay_ms(200); |
- | |
402 | clear(); |
- | |
403 | // update flags to paint display again because of clear |
- | |
404 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
- | |
405 | } |
- | |
406 | - | ||
407 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
127 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1))#endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
408 | 128 | ||
409 | /* ########################################################################## |
129 | /* ########################################################################## |
410 | * MAIN |
130 | * MAIN |
411 | * ##########################################################################*/ |
131 | * ##########################################################################*/ |
412 | int main(void) { |
132 | int main(void) { |
413 | 133 | ||
414 | // set up FLAGS, compiler should flatten this one |
134 | // set up FLAGS, compiler should flatten this one |
415 | COSD_FLAGS = (NTSC << 0); |
135 | COSD_FLAGS = (NTSC << 0); |
416 | COSD_FLAGS |= (HUD << 1); |
136 | COSD_FLAGS |= (HUD << 1); |
417 | COSD_FLAGS |= (ARTHORIZON << 2); |
137 | COSD_FLAGS |= (ARTHORIZON << 2); |
418 | COSD_FLAGS |= (BIGVARIO << 3); |
138 | COSD_FLAGS |= (BIGVARIO << 3); |
419 | COSD_FLAGS |= (STATS << 4); |
139 | COSD_FLAGS |= (STATS << 4); |
420 | COSD_FLAGS |= (WARNINGS << 5); |
140 | COSD_FLAGS |= (WARNINGS << 5); |
421 | 141 | ||
422 | // set up Atmega162 Ports |
142 | // set up Atmega162 Ports |
423 | DDRA |= (1 << PA1); // PA1 output (/CS) |
143 | DDRA |= (1 << PA1); // PA1 output (/CS) |
424 | MAX_CS_HIGH |
144 | MAX_CS_HIGH |
425 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
145 | DDRA |= (1 << PA2); // PA2 output (SDIN) |
426 | MAX_SDIN_LOW |
146 | MAX_SDIN_LOW |
427 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
147 | DDRA |= (1 << PA3); // PA3 output (SCLK) |
428 | MAX_SCLK_LOW |
148 | MAX_SCLK_LOW |
429 | DDRA |= (1 << PA5); // PA5 output (RESET) |
149 | DDRA |= (1 << PA5); // PA5 output (RESET) |
430 | MAX_RESET_HIGH |
150 | MAX_RESET_HIGH |
431 | 151 | ||
432 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
152 | DDRC |= (1 << PC0); // PC0 output (LED1 gn) |
433 | LED1_OFF |
153 | LED1_OFF |
434 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
154 | DDRC |= (1 << PC1); // PC1 output (LED2 rt) |
435 | LED2_OFF |
155 | LED2_OFF |
436 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
156 | DDRC |= (1 << PC2); // PC2 output (LED3 gn) |
437 | LED3_OFF |
157 | LED3_OFF |
438 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
158 | DDRC |= (1 << PC3); // PC3 output (LED4 rt) |
439 | LED4_OFF |
159 | LED4_OFF |
440 | 160 | ||
441 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
161 | DDRC &= ~(1 << PC4); // PC4 input (MODE) |
442 | PORTC |= (1 << PC4); // pullup |
162 | PORTC |= (1 << PC4); // pullup |
443 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
163 | DDRC &= ~(1 << PC5); // PC5 input (SET) |
444 | PORTC |= (1 << PC5); // pullup |
164 | PORTC |= (1 << PC5); // pullup |
445 | 165 | ||
446 | // set up top and bottom lines |
166 | // set up top and bottom lines |
447 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
167 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
448 | bottom_line = 12; |
168 | bottom_line = 12; |
449 | } else { |
169 | } else { |
450 | bottom_line = 14; |
170 | bottom_line = 14; |
451 | } |
171 | } |
452 | 172 | ||
453 | // reset the MAX7456 to be sure any undefined states do no harm |
173 | // reset the MAX7456 to be sure any undefined states do no harm |
454 | MAX_RESET_LOW |
174 | MAX_RESET_LOW |
455 | MAX_RESET_HIGH |
175 | MAX_RESET_HIGH |
456 | 176 | ||
457 | // give the FC/NC and the maxim time to come up |
177 | // give the FC/NC and the maxim time to come up |
458 | LED4_ON |
178 | LED4_ON |
459 | _delay_ms(2000); |
179 | _delay_ms(2000); |
460 | 180 | ||
461 | LED4_OFF |
181 | LED4_OFF |
462 | 182 | ||
463 | 183 | ||
464 | //Pushing NEW chars to the MAX7456 |
184 | //Pushing NEW chars to the MAX7456 |
465 | #if (WRITECHARS != -1) |
185 | #if (WRITECHARS != -1) |
466 | // DISABLE display (VM0) |
186 | // DISABLE display (VM0) |
467 | spi_send_byte(0x00, 0b00000000); |
187 | spi_send_byte(0x00, 0b00000000); |
468 | #include "characters.c" |
188 | #include "characters.c" |
469 | #endif |
189 | #endif |
470 | 190 | ||
471 | // Setup Video Mode |
191 | // Setup Video Mode |
472 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
192 | if (COSD_FLAGS & COSD_FLAG_NTSC) { |
473 | // NTSC + enable display immediately (VM0) |
193 | // NTSC + enable display immediately (VM0) |
474 | spi_send_byte(0x00, 0b00001000); |
194 | spi_send_byte(0x00, 0b00001000); |
475 | } else { |
195 | } else { |
476 | // PAL + enable display immediately (VM0) |
196 | // PAL + enable display immediately (VM0) |
477 | spi_send_byte(0x00, 0b01001000); |
197 | spi_send_byte(0x00, 0b01001000); |
478 | } |
198 | } |
479 | 199 | ||
480 | /*// clear all display-mem (DMM) |
200 | /*// clear all display-mem (DMM) |
481 | spi_send_byte(0x04, 0b00000100); |
201 | spi_send_byte(0x04, 0b00000100); |
482 | 202 | ||
483 | // clearing takes 12uS according to maxim so lets wait longer |
203 | // clearing takes 12uS according to maxim so lets wait longer |
484 | _delay_us(120); |
204 | _delay_us(120); |
485 | 205 | ||
486 | // 8bit mode |
206 | // 8bit mode |
487 | spi_send_byte(0x04, 0b01000000);*/ |
207 | spi_send_byte(0x04, 0b01000000);*/ |
488 | 208 | ||
489 | // clear display memory and set to 8bit mode |
209 | // clear display memory and set to 8bit mode |
490 | clear(); |
210 | clear(); |
491 | 211 | ||
492 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
212 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
493 | // init usart |
213 | // init usart |
494 | usart1_init(); |
214 | usart1_init(); |
495 | 215 | ||
496 | // set up timer |
216 | // set up timer |
497 | TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64 |
217 | TCCR0 |= (1 << CS00) | (1 << CS01); // timer0 prescaler 64 |
498 | OCR0 = 15; // preload |
218 | OCR0 = 15; // preload |
499 | TIMSK |= (1 << TOIE0); // enable overflow timer0 |
219 | TIMSK |= (1 << TOIE0); // enable overflow timer0 |
500 | 220 | ||
501 | DDRD |= (1 << PD2); // PD2 output (INT0) |
221 | DDRD |= (1 << PD2); // PD2 output (INT0) |
502 | SpiMasterInit(); |
222 | SpiMasterInit(); |
503 | 223 | ||
504 | // enable interrupts |
224 | // enable interrupts |
505 | sei(); |
225 | sei(); |
506 | 226 | ||
507 | #endif |
227 | #endif |
508 | 228 | ||
509 | //write_ascii_string(2, 7, " CaScAdE "); |
229 | //write_ascii_string(2, 7, " CaScAdE "); |
510 | //write_ascii_string(2, 8, "is TESTING his open source"); |
230 | //write_ascii_string(2, 8, "is TESTING his open source"); |
511 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
231 | //write_ascii_string(2, 9, " EPi OSD Firmware"); |
512 | 232 | ||
513 | // we are ready |
233 | // we are ready |
514 | LED3_ON |
234 | LED3_ON |
515 | 235 | ||
516 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
236 | #if ALLCHARSDEBUG | (WRITECHARS != -1) |
517 | clear(); |
237 | clear(); |
518 | write_all_chars(); |
238 | write_all_chars(); |
519 | #else |
239 | #else |
520 | // clear serial screen |
240 | // clear serial screen |
521 | //usart1_puts("\x1B[2J\x1B[H"); |
241 | //usart1_puts("\x1B[2J\x1B[H"); |
522 | //usart1_puts("hello world!\r\n"); |
242 | //usart1_puts("hello world!\r\n"); |
523 | #if FCONLY |
243 | #if FCONLY |
524 | // request data ever 100ms from FC; |
244 | // request data ever 100ms from FC; |
525 | usart1_request_mk_data(0, 'd', 100); |
245 | usart1_request_mk_data(0, 'd', 100); |
526 | #else |
246 | #else |
527 | // request OSD Data from NC every 100ms |
247 | // request OSD Data from NC every 100ms |
528 | usart1_request_mk_data(1, 'o', 100); |
248 | usart1_request_mk_data(1, 'o', 100); |
529 | 249 | ||
530 | // and disable debug... |
250 | // and disable debug... |
531 | usart1_request_mk_data(0, 'd', 0); |
251 | usart1_request_mk_data(0, 'd', 0); |
532 | #endif |
252 | #endif |
533 | 253 | ||
534 | // stats for after flight |
254 | // stats for after flight |
535 | int16_t max_Altimeter = 0; |
255 | int16_t max_Altimeter = 0; |
536 | uint8_t min_UBat = 255; |
256 | uint8_t min_UBat = 255; |
537 | #if !(FCONLY) |
257 | #if !(FCONLY) |
538 | uint16_t max_GroundSpeed = 0; |
258 | uint16_t max_GroundSpeed = 0; |
539 | int16_t max_Distance = 0; |
259 | int16_t max_Distance = 0; |
540 | uint16_t max_FlyingTime = 0; |
260 | uint16_t max_FlyingTime = 0; |
541 | 261 | ||
542 | // flags from last round to check for changes |
262 | // flags from last round to check for changes |
543 | uint8_t old_MKFlags = 0; |
263 | uint8_t old_MKFlags = 0; |
544 | 264 | ||
545 | char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
265 | char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
546 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
266 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
547 | #endif |
267 | #endif |
548 | - | ||
549 | - | ||
550 | 268 | ||
551 | while (1) { |
269 | while (1) { |
552 | // in case SPI is ready and there is nothing to send right now |
270 | // in case SPI is ready and there is nothing to send right now |
553 | if (!icnt && spi_ready) { |
271 | if (!icnt && spi_ready) { |
554 | // correct transfer ends with d (done) |
272 | // correct transfer ends with d (done) |
555 | if (spi_cmd_buffer[0] == 'd') { |
273 | if (spi_cmd_buffer[0] == 'd') { |
556 | ampere = spi_cmd_buffer[1] << 8; |
274 | ampere = spi_cmd_buffer[1] << 8; |
557 | ampere |= spi_cmd_buffer[2]; |
275 | ampere |= spi_cmd_buffer[2]; |
558 | ampere_wasted = spi_cmd_buffer[3]; |
276 | ampere_wasted = spi_cmd_buffer[3]; |
559 | ampere_wasted = ampere_wasted << 8; |
277 | ampere_wasted = ampere_wasted << 8; |
560 | ampere_wasted |= spi_cmd_buffer[4]; |
278 | ampere_wasted |= spi_cmd_buffer[4]; |
561 | ampere_wasted = ampere_wasted << 8; |
279 | ampere_wasted = ampere_wasted << 8; |
562 | ampere_wasted |= spi_cmd_buffer[5]; |
280 | ampere_wasted |= spi_cmd_buffer[5]; |
563 | ampere_wasted = ampere_wasted << 8; |
281 | ampere_wasted = ampere_wasted << 8; |
564 | ampere_wasted |= spi_cmd_buffer[6]; |
282 | ampere_wasted |= spi_cmd_buffer[6]; |
565 | //ampere = *((int16_t *) spi_cmd_buffer + 1); |
283 | //ampere = *((int16_t *) spi_cmd_buffer + 1); |
566 | //ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3); |
284 | //ampere_wasted = *((int32_t *) &spi_cmd_buffer + 3); |
567 | // if this is the first receival we should print the small A |
285 | // if this is the first receival we should print the small A |
568 | if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) { |
286 | if (!(COSD_FLAGS2 & COSD_FLAG_STROMREC)) { |
569 | COSD_FLAGS &= ~COSD_ICONS_WRITTEN; |
287 | COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN; |
570 | // update this flag |
288 | // update this flag |
571 | COSD_FLAGS2 |= COSD_FLAG_STROMREC; |
289 | COSD_FLAGS2 |= COSD_FLAG_STROMREC; |
572 | } |
290 | } |
573 | //write_ascii_char(8+4*30, 'v'); // valid |
291 | //write_ascii_char(8+4*30, 'v'); // valid |
574 | } else { |
292 | } else { |
575 | // update flags |
293 | // update flags |
576 | COSD_FLAGS2 &= ~COSD_FLAG_STROMREC; |
294 | COSD_FLAGS2 &= ~COSD_FLAG_STROMREC; |
577 | //write_ascii_char(8+4*30, 'i'); // invalid |
295 | //write_ascii_char(8+4*30, 'i'); // invalid |
578 | } |
296 | } |
579 | spi_cmd_buffer[0] = 'A'; |
297 | spi_cmd_buffer[0] = 'A'; |
580 | spi_cmd_buffer[1] = 'B'; |
298 | spi_cmd_buffer[1] = 'B'; |
581 | spi_cmd_buffer[2] = 'C'; |
299 | spi_cmd_buffer[2] = 'C'; |
582 | spi_cmd_buffer[3] = 'D'; |
300 | spi_cmd_buffer[3] = 'D'; |
583 | spi_cmd_buffer[4] = 'E'; |
301 | spi_cmd_buffer[4] = 'E'; |
584 | spi_cmd_buffer[5] = 'F'; |
302 | spi_cmd_buffer[5] = 'F'; |
585 | spi_cmd_buffer[6] = 'G'; |
303 | spi_cmd_buffer[6] = 'G'; |
586 | StartTransfer((unsigned char*) spi_cmd_buffer, 7); |
304 | StartTransfer((unsigned char*) spi_cmd_buffer, 7); |
587 | } |
305 | } |
588 | if (rxd_buffer_locked) { |
306 | if (rxd_buffer_locked) { |
589 | if (COSD_FLAGS & COSD_FLAG_HUD) { |
- | |
590 | #if FCONLY |
307 | #if FCONLY |
591 | if (rxd_buffer[2] == 'D') { // FC Data |
308 | if (rxd_buffer[2] == 'D') { // FC Data |
592 | Decode64(); |
309 | Decode64(); |
593 | debugData = *((DebugOut_t*) pRxData); |
310 | debugData = *((DebugOut_t*) pRxData); |
594 | 311 | ||
595 | // init on first data retrival, distinguished by last battery :) |
312 | // init on first data retrival, distinguished by last battery :) |
596 | if (last_UBat == 255) { |
313 | if (last_UBat == 255) { |
597 | // fix for min_UBat |
314 | // fix for min_UBat |
598 | min_UBat = debugData.Analog[9]; |
315 | min_UBat = debugData.Analog[9]; |
599 | auto_config(debugData.Analog[9]); |
316 | init_cosd(debugData.Analog[9]); |
600 | } |
- | |
601 | #include "osd_fcmode_default.c" |
- | |
602 | } |
317 | } |
- | 318 | #include "osd_fcmode_default.c" |
|
- | 319 | } |
|
603 | #else |
320 | #else |
604 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
321 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
605 | Decode64(); |
322 | Decode64(); |
606 | naviData = *((NaviData_t*) pRxData); |
323 | naviData = *((NaviData_t*) pRxData); |
607 | 324 | ||
608 | // init on first data retrival, distinguished by last battery :) |
325 | // init on first data retrival, distinguished by last battery :) |
609 | if (last_UBat == 255) { |
326 | if (last_UBat == 255) { |
610 | // fix for min_UBat |
327 | // fix for min_UBat |
611 | min_UBat = naviData.UBat; |
328 | min_UBat = naviData.UBat; |
612 | auto_config(naviData.UBat); |
329 | init_cosd(naviData.UBat); |
613 | } |
- | |
614 | #include "osd_ncmode_default.c" |
- | |
615 | } |
330 | } |
616 | #endif |
331 | #include "osd_ncmode_default.c" |
617 | } |
332 | } |
- | 333 | #endif |
|
618 | rxd_buffer_locked = 0; |
334 | rxd_buffer_locked = 0; |
619 | } |
335 | } |
620 | // handle keypress |
336 | // handle keypress |
621 | if (s1_pressed()) { |
337 | if (s1_pressed()) { |
622 | config_menu(); |
338 | config_menu(); |
623 | } |
339 | } |
624 | if (seconds_since_last_data > 2) { |
340 | if (seconds_since_last_data > 2) { |
625 | #if FCONLY |
341 | #if FCONLY |
626 | // request data ever 100ms from FC; |
342 | // request data ever 100ms from FC; |
627 | usart1_request_mk_data(0, 'd', 100); |
343 | usart1_request_mk_data(0, 'd', 100); |
628 | #else |
344 | #else |
629 | // request OSD Data from NC every 100ms |
345 | // request OSD Data from NC every 100ms |
630 | usart1_request_mk_data(1, 'o', 100); |
346 | usart1_request_mk_data(1, 'o', 100); |
631 | 347 | ||
632 | // and disable debug... |
348 | // and disable debug... |
633 | usart1_request_mk_data(0, 'd', 0); |
349 | usart1_request_mk_data(0, 'd', 0); |
634 | #endif |
350 | #endif |
635 | } |
351 | } |
636 | } |
352 | } |
637 | #endif |
353 | #endif |
638 | return 0; |
354 | return 0; |
639 | } |
355 | } |
640 | 356 |