Rev 932 | Rev 954 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 932 | Rev 942 | ||
---|---|---|---|
1 | /**************************************************************************** |
1 | /**************************************************************************** |
2 | * Copyright (C) 2009-2011 by Claas Anders "CaScAdE" Rathje * |
2 | * Copyright (C) 2009-2011 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 + SVN * |
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 "main.h" |
28 | #include "main.h" |
29 | #include <avr/io.h> |
29 | #include <avr/io.h> |
30 | #include <avr/interrupt.h> |
30 | #include <avr/interrupt.h> |
31 | #include <util/delay.h> |
31 | #include <util/delay.h> |
32 | #include <avr/pgmspace.h> |
32 | #include <avr/pgmspace.h> |
33 | #include "max7456_software_spi.h" |
33 | #include "max7456_software_spi.h" |
34 | #ifdef ANTENNATRACKTEST |
34 | #ifdef ANTENNATRACKTEST |
35 | #include "usart0.h" |
35 | #include "usart0.h" |
36 | #endif |
36 | #endif |
37 | #include "usart1.h" |
37 | #include "usart1.h" |
38 | #include "osd_helpers.h" |
38 | #include "osd_helpers.h" |
39 | #include "config.h" |
39 | #include "config.h" |
40 | #include "spi.h" |
40 | #include "spi.h" |
41 | #include "buttons.h" |
41 | #include "buttons.h" |
42 | #include "ppm.h" |
42 | #include "ppm.h" |
43 | #include "osd_ncmode_default.h" |
43 | #include "osd_ncmode_default.h" |
44 | #include "osd_ncmode_minimal.h" |
44 | #include "osd_ncmode_minimal.h" |
45 | #include "osd_fcmode_default.h" |
45 | #include "osd_fcmode_default.h" |
46 | #include "osd_fcmode_jopl.h" |
46 | #include "osd_fcmode_jopl.h" |
47 | 47 | ||
48 | #if WRITECHARS != -1 |
48 | #if WRITECHARS != -1 |
49 | #include "characters.h" |
49 | #include "characters.h" |
50 | #endif |
50 | #endif |
51 | 51 | ||
52 | /* TODO: |
52 | /* TODO: |
53 | * - verifiy correctness of values |
53 | * - verifiy correctness of values |
54 | * - clean up code :) |
54 | * - clean up code :) |
55 | */ |
55 | */ |
56 | 56 | ||
57 | 57 | ||
58 | /* ########################################################################## |
58 | /* ########################################################################## |
59 | * global definitions and global vars |
59 | * global definitions and global vars |
60 | * ##########################################################################*/ |
60 | * ##########################################################################*/ |
61 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
61 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
62 | 62 | ||
63 | volatile NaviData_t naviData; |
63 | volatile NaviData_t naviData; |
64 | volatile str_DebugOut debugData; |
64 | volatile str_DebugOut debugData; |
65 | 65 | ||
66 | // cache old vars for blinking attribute, checkup is faster than full |
66 | // cache old vars for blinking attribute, checkup is faster than full |
67 | // attribute write each time |
67 | // attribute write each time |
68 | volatile uint8_t last_UBat = 255; |
68 | volatile uint8_t last_UBat = 255; |
69 | volatile uint8_t last_RC_Quality = 255; |
69 | volatile uint8_t last_RC_Quality = 255; |
70 | 70 | ||
71 | // 16bit should be enough, normal LiPos don't last that long |
71 | // 16bit should be enough, normal LiPos don't last that long |
72 | volatile uint16_t uptime = 0; |
72 | volatile uint16_t uptime = 0; |
73 | volatile uint16_t timer = 0; |
73 | volatile uint16_t timer = 0; |
74 | volatile uint16_t flytime_fc = 0; |
74 | volatile uint16_t flytime_fc = 0; |
75 | 75 | ||
76 | // remember last time data was received |
76 | // remember last time data was received |
77 | volatile uint8_t seconds_since_last_data = 0; |
77 | volatile uint8_t seconds_since_last_data = 0; |
78 | 78 | ||
79 | // general PAL|NTSC distingiusch stuff |
79 | // general PAL|NTSC distingiusch stuff |
80 | uint8_t top_line = 1; |
80 | uint8_t top_line = 1; |
81 | uint8_t bottom_line = 14; |
81 | uint8_t bottom_line = 14; |
82 | 82 | ||
83 | // battery voltages |
83 | // battery voltages |
84 | uint8_t min_voltage = 0; |
84 | uint8_t min_voltage = 0; |
85 | uint8_t max_voltage = 0; |
85 | uint8_t max_voltage = 0; |
86 | 86 | ||
87 | uint8_t scope[12] = { |
87 | uint8_t scope[12] = { |
88 | 5, 5, 0, |
88 | 5, 5, 0, |
89 | 25, 5, 0, |
89 | 25, 5, 0, |
90 | 5, 10, 0, |
90 | 5, 10, 0, |
91 | 25, 10, 0 |
91 | 25, 10, 0 |
92 | }; |
92 | }; |
93 | 93 | ||
94 | // Flags |
94 | // Flags |
95 | uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0; |
95 | uint8_t COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0, COSD_DISPLAYMODE = 0; |
96 | 96 | ||
97 | 97 | ||
98 | 98 | ||
99 | // stats for after flight |
99 | // stats for after flight |
100 | int16_t max_Altimeter = 0; |
100 | int16_t max_Altimeter = 0; |
101 | uint8_t min_UBat = 255; |
101 | uint8_t min_UBat = 255; |
102 | uint16_t max_GroundSpeed = 0; |
102 | uint16_t max_GroundSpeed = 0; |
103 | int16_t max_Distance = 0; |
103 | int16_t max_Distance = 0; |
104 | uint16_t max_FlyingTime = 0; |
104 | uint16_t max_FlyingTime = 0; |
105 | 105 | ||
106 | // flags from last round to check for changes |
106 | // flags from last round to check for changes |
107 | uint8_t old_MKFlags = 0; |
107 | uint8_t old_MKFlags = 0; |
108 | 108 | ||
109 | // store stats description in progmem to save space |
109 | // store stats description in progmem to save space |
110 | const char stats_item_0[] PROGMEM = "max Altitude:"; |
110 | const char stats_item_0[] PROGMEM = "max Altitude:"; |
111 | const char stats_item_1[] PROGMEM = "max Speed :"; |
111 | const char stats_item_1[] PROGMEM = "max Speed :"; |
112 | const char stats_item_2[] PROGMEM = "max Distance:"; |
112 | const char stats_item_2[] PROGMEM = "max Distance:"; |
113 | const char stats_item_3[] PROGMEM = "min Voltage :"; |
113 | const char stats_item_3[] PROGMEM = "min Voltage :"; |
114 | const char stats_item_4[] PROGMEM = "max Time:"; |
114 | const char stats_item_4[] PROGMEM = "max Time:"; |
115 | const char stats_item_5[] PROGMEM = "longitude :"; |
115 | const char stats_item_5[] PROGMEM = "longitude :"; |
116 | const char stats_item_6[] PROGMEM = "latitude:"; |
116 | const char stats_item_6[] PROGMEM = "latitude:"; |
117 | const char stats_item_7[] PROGMEM = "max current :"; |
117 | const char stats_item_7[] PROGMEM = "max current :"; |
118 | const char *stats_item_pointers[8] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
118 | const char *stats_item_pointers[8] PROGMEM = {stats_item_0, stats_item_1, stats_item_2, |
119 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
119 | stats_item_3, stats_item_4, stats_item_5, stats_item_6, stats_item_7}; |
120 | 120 | ||
121 | //char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
121 | //char* directions[8] = {"NE", "E ", "SE", "S ", "SW", "W ", "NW", "N "}; |
122 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
122 | //char arrowdir[8] = {218, 217, 224, 223, 222, 221, 220, 219}; |
123 | const char str_NE[] PROGMEM = "NE"; |
123 | const char str_NE[] PROGMEM = "NE"; |
124 | const char str_E[] PROGMEM = "E "; |
124 | const char str_E[] PROGMEM = "E "; |
125 | const char str_SE[] PROGMEM = "SE"; |
125 | const char str_SE[] PROGMEM = "SE"; |
126 | const char str_S[] PROGMEM = "S "; |
126 | const char str_S[] PROGMEM = "S "; |
127 | const char str_SW[] PROGMEM = "SW"; |
127 | const char str_SW[] PROGMEM = "SW"; |
128 | const char str_W[] PROGMEM = "W "; |
128 | const char str_W[] PROGMEM = "W "; |
129 | const char str_NW[] PROGMEM = "NW"; |
129 | const char str_NW[] PROGMEM = "NW"; |
130 | const char str_N[] PROGMEM = "N "; |
130 | const char str_N[] PROGMEM = "N "; |
131 | const char *directions[8] PROGMEM = { |
131 | const char *directions[8] PROGMEM = { |
132 | str_NE, |
132 | str_NE, |
133 | str_E, |
133 | str_E, |
134 | str_SE, |
134 | str_SE, |
135 | str_S, |
135 | str_S, |
136 | str_SW, |
136 | str_SW, |
137 | str_W, |
137 | str_W, |
138 | str_NW, |
138 | str_NW, |
139 | str_N |
139 | str_N |
140 | }; |
140 | }; |
141 | 141 | ||
142 | /* ########################################################################## |
142 | /* ########################################################################## |
143 | * Different display mode function pointers |
143 | * Different display mode function pointers |
144 | * ##########################################################################*/ |
144 | * ##########################################################################*/ |
145 | const char str_1[] PROGMEM = "default"; |
145 | const char str_1[] PROGMEM = "default"; |
146 | const char str_2[] PROGMEM = "minimal"; |
146 | const char str_2[] PROGMEM = "minimal"; |
147 | const char str_3[] PROGMEM = " jopl"; |
147 | const char str_3[] PROGMEM = " jopl"; |
148 | 148 | ||
149 | #if FCONLY |
149 | #if FCONLY |
150 | const displaymode_t fcdisplaymodes[] PROGMEM = { |
150 | const displaymode_t fcdisplaymodes[] PROGMEM = { |
151 | { osd_fcmode_default, (char *)str_1}, |
151 | { osd_fcmode_default, (char *)str_1}, |
152 | { osd_fcmode_jopl, (char *)str_3} |
152 | { osd_fcmode_jopl, (char *)str_3} |
153 | }; |
153 | }; |
154 | int (*osd_fcmode)(void) = (int(*)(void)) & osd_fcmode_default; |
154 | int (*osd_fcmode)(void) = (int(*)(void)) & osd_fcmode_default; |
155 | #else |
155 | #else |
156 | 156 | ||
157 | const displaymode_t ncdisplaymodes[] PROGMEM = { |
157 | const displaymode_t ncdisplaymodes[] PROGMEM = { |
158 | { osd_ncmode_default, (char *)str_1}, |
158 | { osd_ncmode_default, (char *)str_1}, |
159 | { osd_ncmode_minimal, (char *)str_2} |
159 | { osd_ncmode_minimal, (char *)str_2} |
160 | }; |
160 | }; |
161 | int (*osd_ncmode)(void) = (int(*)(void)) & osd_ncmode_default; |
161 | int (*osd_ncmode)(void) = (int(*)(void)) & osd_ncmode_default; |
162 | #endif |
162 | #endif |
163 | 163 | ||
164 | 164 | ||
165 | #endif |
165 | #endif |
166 | 166 | ||
167 | /* ########################################################################## |
167 | /* ########################################################################## |
168 | * Interrupt handler |
168 | * Interrupt handler |
169 | * ##########################################################################*/ |
169 | * ##########################################################################*/ |
170 | 170 | ||
171 | /** |
171 | /** |
172 | * handler for undefined Interrupts |
172 | * handler for undefined Interrupts |
173 | * if not defined AVR will reset in case any unhandled interrupts occur |
173 | * if not defined AVR will reset in case any unhandled interrupts occur |
174 | */ |
174 | */ |
175 | ISR(__vector_default) { |
175 | ISR(__vector_default) { |
176 | asm("nop"); |
176 | asm("nop"); |
177 | } |
177 | } |
178 | 178 | ||
179 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
179 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
180 | /* ########################################################################## |
180 | /* ########################################################################## |
181 | * timer stuff |
181 | * timer stuff |
182 | * ##########################################################################*/ |
182 | * ##########################################################################*/ |
183 | 183 | ||
184 | static uint8_t delay_spi = 0; |
184 | static uint8_t delay_spi = 0; |
185 | 185 | ||
186 | /** |
186 | /** |
187 | * timer kicks in every 1000uS ^= 1ms |
187 | * timer kicks in every 1000uS ^= 1ms |
188 | */ |
188 | */ |
189 | ISR(TIMER0_COMP_vect) { |
189 | ISR(TIMER0_COMP_vect) { |
190 | if (!timer--) { |
190 | if (!timer--) { |
191 | uptime++; |
191 | uptime++; |
192 | 192 | ||
193 | #if FCONLY |
193 | #if FCONLY |
194 | if (debugData.Analog[12] > 10) { |
194 | if (debugData.Analog[12] > 10) { |
195 | flytime_fc++; |
195 | flytime_fc++; |
196 | } |
196 | } |
197 | #endif |
197 | #endif |
198 | 198 | ||
199 | timer = 999; |
199 | timer = 999; |
200 | seconds_since_last_data++; |
200 | seconds_since_last_data++; |
201 | } |
201 | } |
202 | // in case there is still some spi data to send do it now |
202 | // in case there is still some spi data to send do it now |
203 | // delay to give the slave some time to compute values |
203 | // delay to give the slave some time to compute values |
204 | if (spi_ready && icnt) { |
204 | if (spi_ready && icnt) { |
205 | if (!delay_spi--) { |
205 | if (!delay_spi--) { |
206 | delay_spi = 8; |
206 | delay_spi = 8; |
207 | spi_send_next(); |
207 | spi_send_next(); |
208 | } |
208 | } |
209 | } |
209 | } |
210 | } |
210 | } |
211 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
211 | #endif // ends !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
212 | 212 | ||
213 | /* ########################################################################## |
213 | /* ########################################################################## |
214 | * MAIN |
214 | * MAIN |
215 | * ##########################################################################*/ |
215 | * ##########################################################################*/ |
216 | int main(void) { |
216 | int main(void) { |
217 | // set up FLAGS |
217 | // set up FLAGS |
218 | COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0; |
218 | COSD_FLAGS_MODES = 0, COSD_FLAGS_CONFIG = 0, COSD_FLAGS_RUNTIME = 0; |
219 | #if NTSC |
219 | #if NTSC |
220 | COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC; |
220 | COSD_FLAGS_CONFIG |= COSD_FLAG_NTSC; |
221 | #endif |
221 | #endif |
222 | #if HUD |
222 | #if HUD |
223 | COSD_FLAGS_MODES |= COSD_FLAG_HUD; |
223 | COSD_FLAGS_MODES |= COSD_FLAG_HUD; |
224 | #endif |
224 | #endif |
225 | #if ARTHORIZON |
225 | #if ARTHORIZON |
226 | COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON; |
226 | COSD_FLAGS_MODES |= COSD_FLAG_ARTHORIZON; |
227 | #endif |
227 | #endif |
228 | #if BIGVARIO |
228 | #if BIGVARIO |
229 | COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO; |
229 | COSD_FLAGS_MODES |= COSD_FLAG_BIGARIO; |
230 | #endif |
230 | #endif |
231 | #if STATS |
231 | #if STATS |
232 | COSD_FLAGS_MODES |= COSD_FLAG_STATS; |
232 | COSD_FLAGS_MODES |= COSD_FLAG_STATS; |
233 | #endif |
233 | #endif |
234 | #if WARNINGS |
234 | #if WARNINGS |
235 | COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS; |
235 | COSD_FLAGS_MODES |= COSD_FLAG_WARNINGS; |
236 | #endif |
236 | #endif |
237 | #if FCONLY |
237 | #if FCONLY |
238 | COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE; |
238 | COSD_FLAGS_CONFIG |= COSD_FLAG_FCMODE; |
239 | #endif |
239 | #endif |
240 | 240 | ||
241 | 241 | ||
242 | // set up Atmega162 Ports |
242 | // set up Atmega162 Ports |
243 | DDRA |= ((1 << PA1) | (1 << PA2) | (1 << PA3) | (1 << PA5)); // PA1 output (/CS) | PA2 output (SDIN) |PA3 output (SCLK) | PA5 output (RESET) |
243 | DDRA |= ((1 << PA1) | (1 << PA2) | (1 << PA3) | (1 << PA5)); // PA1 output (/CS) | PA2 output (SDIN) |PA3 output (SCLK) | PA5 output (RESET) |
244 | MAX_CS_HIGH |
244 | MAX_CS_HIGH |
245 | MAX_SDIN_LOW |
245 | MAX_SDIN_LOW |
246 | MAX_SCLK_LOW |
246 | MAX_SCLK_LOW |
247 | MAX_RESET_HIGH |
247 | MAX_RESET_HIGH |
248 | 248 | ||
249 | DDRC |= ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3)); // PC0 output (LED1 gn) | PC1 output (LED2 rt) | PC2 output (LED3 gn) | PC3 output (LED4 rt) |
249 | DDRC |= ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3)); // PC0 output (LED1 gn) | PC1 output (LED2 rt) | PC2 output (LED3 gn) | PC3 output (LED4 rt) |
250 | LED1_OFF |
250 | LED1_OFF |
251 | LED2_OFF |
251 | LED2_OFF |
252 | LED3_OFF |
252 | LED3_OFF |
253 | LED4_OFF |
253 | LED4_OFF |
254 | 254 | ||
255 | DDRC &= ~((1 << PC4) | (1 << PC5)); // PC4 input(MODE) | PC5 input(SET) |
255 | DDRC &= ~((1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7)); // PC4 input(MODE) | PC5 input(SET) | PC6 input SJ1 | PC7 input SJ2 |
- | 256 | PORTC |= ((1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7)); // pullup |
|
256 | PORTC |= ((1 << PC4) | (1 << PC5)); // pullup |
257 | |
257 | 258 | ||
258 | // reset the MAX7456 to be sure any undefined states do no harm |
259 | // reset the MAX7456 to be sure any undefined states do no harm |
259 | MAX_RESET_LOW |
260 | MAX_RESET_LOW |
260 | MAX_RESET_HIGH |
261 | MAX_RESET_HIGH |
261 | 262 | ||
262 | // give the FC/NC and the maxim time to come up |
263 | // give the FC/NC and the maxim time to come up |
263 | LED4_ON |
264 | LED4_ON |
264 | _delay_ms(1000); |
265 | _delay_ms(1000); |
265 | LED4_OFF |
266 | LED4_OFF |
266 | 267 | ||
267 | //Pushing NEW chars to the MAX7456 |
268 | //Pushing NEW chars to the MAX7456 |
268 | #if (WRITECHARS != -1) |
269 | #if (WRITECHARS != -1) |
269 | // DISABLE display (VM0) |
270 | // DISABLE display (VM0) |
270 | spi_send_byte(0x00, 0b00000000); |
271 | spi_send_byte(0x00, 0b00000000); |
271 | learn_all_chars_pgm(); |
272 | learn_all_chars_pgm(); |
272 | #else |
273 | #else |
273 | // read out config for NTSC/PAL distinguishing |
274 | // read out config for NTSC/PAL distinguishing |
274 | get_eeprom(0); |
275 | get_eeprom(0); |
275 | #endif |
276 | #endif |
276 | 277 | ||
277 | // Setup Video Mode |
278 | // Setup Video Mode |
278 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
279 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
279 | // NTSC + enable display immediately (VM0) |
280 | // NTSC + enable display immediately (VM0) |
280 | spi_send_byte(0x00, 0b00001000); |
281 | spi_send_byte(0x00, 0b00001000); |
281 | 282 | ||
282 | bottom_line = 12; |
283 | bottom_line = 12; |
283 | } else { |
284 | } else { |
284 | // PAL + enable display immediately (VM0) |
285 | // PAL + enable display immediately (VM0) |
285 | spi_send_byte(0x00, 0b01001000); |
286 | spi_send_byte(0x00, 0b01001000); |
286 | 287 | ||
287 | bottom_line = 14; |
288 | bottom_line = 14; |
288 | } |
289 | } |
289 | 290 | ||
290 | /*// clear all display-mem (DMM) |
291 | /*// clear all display-mem (DMM) |
291 | spi_send_byte(0x04, 0b00000100); |
292 | spi_send_byte(0x04, 0b00000100); |
292 | 293 | ||
293 | // clearing takes 12uS according to maxim so lets wait longer |
294 | // clearing takes 12uS according to maxim so lets wait longer |
294 | _delay_us(120); |
295 | _delay_us(120); |
295 | 296 | ||
296 | // 8bit mode |
297 | // 8bit mode |
297 | spi_send_byte(0x04, 0b01000000);*/ |
298 | spi_send_byte(0x04, 0b01000000);*/ |
298 | 299 | ||
299 | // clear display memory and set to 8bit mode |
300 | // clear display memory and set to 8bit mode |
300 | clear(); |
301 | clear(); |
301 | 302 | ||
302 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
303 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
303 | // init usart |
304 | // init usart |
304 | usart1_init(); |
305 | usart1_init(); |
305 | 306 | ||
306 | #ifdef SERIALDEBUGDRAW |
307 | #ifdef SERIALDEBUGDRAW |
307 | #define USART0ENABLE 1 |
308 | #define USART0ENABLE 1 |
308 | #endif |
309 | #endif |
309 | #ifdef ANTENNATRACKTEST |
310 | #ifdef ANTENNATRACKTEST |
310 | #define USART0ENABLE 1 |
311 | #define USART0ENABLE 1 |
311 | #endif |
312 | #endif |
312 | 313 | ||
313 | #ifdef USART0ENABLE |
314 | #ifdef USART0ENABLE |
314 | usart0_init(); |
315 | usart0_init(); |
315 | usart0_puts("\x1B[2J\x1B[H"); |
316 | usart0_puts("\x1B[2J\x1B[H"); |
316 | usart0_puts("Welcome\r\n"); |
317 | usart0_puts("Welcome\r\n"); |
317 | #endif |
318 | #endif |
318 | 319 | ||
319 | // keep serial port clean |
320 | // keep serial port clean |
320 | usart1_DisableTXD(); |
321 | usart1_DisableTXD(); |
321 | 322 | ||
322 | // set up timer |
323 | // set up timer |
323 | // CTC, Prescaler /64 |
324 | // CTC, Prescaler /64 |
324 | TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00); |
325 | TCCR0 = (1 << WGM01) | (0 << WGM00) | (0 << CS02) | (1 << CS01) | (1 << CS00); |
325 | 326 | ||
326 | TCNT0 = 0; |
327 | TCNT0 = 0; |
327 | OCR0 = 250; |
328 | OCR0 = 250; |
328 | 329 | ||
329 | // enable timer output compare interrupt |
330 | // enable timer output compare interrupt |
330 | TIMSK &= ~(1 << TOIE0); |
331 | TIMSK &= ~(1 << TOIE0); |
331 | TIMSK |= (1 << OCIE0); |
332 | TIMSK |= (1 << OCIE0); |
332 | 333 | ||
333 | // SPI setup |
334 | // SPI setup |
334 | DDRD |= (1 << PD2); // PD2 output (INT0) |
335 | DDRD |= (1 << PD2); // PD2 output (INT0) |
335 | SpiMasterInit(); |
336 | SpiMasterInit(); |
336 | 337 | ||
337 | // PPM detection setup |
338 | // PPM detection setup |
338 | ppm_init(); |
339 | ppm_init(); |
339 | 340 | ||
340 | // enable interrupts |
341 | // enable interrupts |
341 | sei(); |
342 | sei(); |
342 | 343 | ||
343 | 344 | ||
344 | //write_ascii_string(2,7, " CaScAdE"); |
345 | //write_ascii_string(2,7, " CaScAdE"); |
345 | //write_ascii_string(2,8, "is TESTING his open source"); |
346 | //write_ascii_string(2,8, "is TESTING his open source"); |
346 | //write_ascii_string(2,9, "EPi OSD Firmware"); |
347 | //write_ascii_string(2,9, "EPi OSD Firmware"); |
347 | 348 | ||
348 | // we are ready |
349 | // we are ready |
349 | LED3_ON |
350 | LED3_ON |
350 | 351 | ||
351 | 352 | ||
352 | // clear serial screen |
353 | // clear serial screen |
353 | //usart1_puts("\x1B[2J\x1B[H"); |
354 | //usart1_puts("\x1B[2J\x1B[H"); |
354 | 355 | ||
355 | COSD_FLAGS_RUNTIME &= ~COSD_DATARECEIVED; |
356 | COSD_FLAGS_RUNTIME &= ~COSD_DATARECEIVED; |
356 | #if !FCONLY |
357 | #if !FCONLY |
357 | usart1_request_nc_uart(); |
358 | usart1_request_nc_uart(); |
358 | #endif |
359 | #endif |
359 | 360 | ||
360 | while (1) { |
361 | while (1) { |
361 | // in case SPI is ready and there is nothing to send right now |
362 | // in case SPI is ready and there is nothing to send right now |
362 | if (!icnt && spi_ready) { |
363 | if (!icnt && spi_ready) { |
363 | // correct transfer ends with d (done) |
364 | // correct transfer ends with d (done) |
364 | if (SPI_buffer.buffer.chk == 'd') { |
365 | if (SPI_buffer.buffer.chk == 'd') { |
365 | ampere = SPI_buffer.data.ampere; |
366 | ampere = SPI_buffer.data.ampere; |
366 | ampere_wasted = SPI_buffer.data.mah; |
367 | ampere_wasted = SPI_buffer.data.mah; |
367 | s_volt = SPI_buffer.data.volt; |
368 | s_volt = SPI_buffer.data.volt; |
368 | 369 | ||
369 | // if this is the first receival we should print the small A |
370 | // if this is the first receival we should print the small A |
370 | if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) { |
371 | if (!(COSD_FLAGS_RUNTIME & COSD_FLAG_STROMREC)) { |
371 | clear(); |
372 | clear(); |
372 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
373 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
373 | // update this flag |
374 | // update this flag |
374 | COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC; |
375 | COSD_FLAGS_RUNTIME |= COSD_FLAG_STROMREC; |
375 | } |
376 | } |
376 | } else { |
377 | } else { |
377 | // update flags |
378 | // update flags |
378 | COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC; |
379 | COSD_FLAGS_RUNTIME &= ~COSD_FLAG_STROMREC; |
379 | } |
380 | } |
380 | StartTransfer(9); |
381 | StartTransfer(9); |
381 | } |
382 | } |
382 | if (rxd_buffer_locked) { |
383 | if (rxd_buffer_locked) { |
383 | #if FCONLY |
384 | #if FCONLY |
384 | if (rxd_buffer[2] == 'D') { // FC Data |
385 | if (rxd_buffer[2] == 'D') { // FC Data |
385 | Decode64(); |
386 | Decode64(); |
386 | debugData = *((str_DebugOut*)pRxData); |
387 | debugData = *((str_DebugOut*)pRxData); |
387 | 388 | ||
388 | // init on first data retrival, distinguished by last battery :) |
389 | // init on first data retrival, distinguished by last battery :) |
389 | if (last_UBat == 255) { |
390 | if (last_UBat == 255) { |
390 | if (debugData.Analog[9] > 40) { |
391 | if (debugData.Analog[9] > 40) { |
391 | // fix for min_UBat |
392 | // fix for min_UBat |
392 | min_UBat = debugData.Analog[9]; |
393 | min_UBat = debugData.Analog[9]; |
393 | last_UBat = debugData.Analog[9]; |
394 | last_UBat = debugData.Analog[9]; |
394 | init_cosd(last_UBat); |
395 | init_cosd(last_UBat); |
395 | } |
396 | } |
396 | } else { |
397 | } else { |
397 | osd_fcmode(); |
398 | osd_fcmode(); |
398 | } |
399 | } |
399 | seconds_since_last_data = 0; |
400 | seconds_since_last_data = 0; |
400 | } |
401 | } |
401 | #else |
402 | #else |
402 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
403 | if (rxd_buffer[2] == 'O') { // NC OSD Data |
403 | Decode64(); |
404 | Decode64(); |
404 | naviData = *((NaviData_t*)pRxData); |
405 | naviData = *((NaviData_t*)pRxData); |
405 | 406 | ||
406 | // init on first data retrival, distinguished by last battery :) |
407 | // init on first data retrival, distinguished by last battery :) |
407 | if (last_UBat == 255) { |
408 | if (last_UBat == 255) { |
408 | if (naviData.UBat > 40) { |
409 | if (naviData.UBat > 40) { |
409 | // fix for min_UBat |
410 | // fix for min_UBat |
410 | min_UBat = naviData.UBat; |
411 | min_UBat = naviData.UBat; |
411 | last_UBat = naviData.UBat; |
412 | last_UBat = naviData.UBat; |
412 | init_cosd(last_UBat); |
413 | init_cosd(last_UBat); |
413 | } |
414 | } |
414 | } else { |
415 | } else { |
415 | osd_ncmode(); |
416 | osd_ncmode(); |
416 | } |
417 | } |
417 | //seconds_since_last_data = 0; |
418 | //seconds_since_last_data = 0; |
418 | } |
419 | } |
419 | #endif |
420 | #endif |
420 | 421 | ||
421 | // ONLY FOR TESTING |
422 | // ONLY FOR TESTING |
422 | #ifdef ANTENNATRACKTEST |
423 | #ifdef ANTENNATRACKTEST |
423 | #include <stdlib.h> |
424 | #include <stdlib.h> |
424 | //#include <float.h> |
425 | //#include <float.h> |
425 | //#include <math.h> |
426 | //#include <math.h> |
426 | 427 | ||
427 | //usart0_puts("\x1B[2J\x1B[H"); |
428 | //usart0_puts("\x1B[2J\x1B[H"); |
428 | 429 | ||
429 | 430 | ||
430 | /* |
431 | /* |
431 | naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100) |
432 | naviData.HomePositionDeviation.Distance = 23 * 100; // 23m away (cm * 100) |
432 | naviData.HomePositionDeviation.Bearing = 35; // 35� |
433 | naviData.HomePositionDeviation.Bearing = 35; // 35� |
433 | altimeter_offset = 50; // 50m start height |
434 | altimeter_offset = 50; // 50m start height |
434 | naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000) |
435 | naviData.CurrentPosition.Altitude = (int32_t) ((int32_t)250 * (int32_t)1000); // 250m height (mm * 1000) |
435 | */ |
436 | */ |
436 | 437 | ||
437 | static char conv_array[7]; |
438 | static char conv_array[7]; |
438 | 439 | ||
439 | // should be float |
440 | // should be float |
440 | int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset); |
441 | int tanheight = (naviData.HomePositionDeviation.Distance * 100) / (naviData.CurrentPosition.Altitude - altimeter_offset); |
441 | 442 | ||
442 | // we need math.h and some faster AVR :) |
443 | // we need math.h and some faster AVR :) |
443 | //tanheight = rad2deg(atan(tanheight)); |
444 | //tanheight = rad2deg(atan(tanheight)); |
444 | 445 | ||
445 | itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10); |
446 | itoa((naviData.HomePositionDeviation.Bearing + 180) % 360, conv_array, 10); |
446 | usart0_puts("Bearing: "); |
447 | usart0_puts("Bearing: "); |
447 | usart0_puts(conv_array); |
448 | usart0_puts(conv_array); |
448 | usart0_puts("\tHeightangle: "); |
449 | usart0_puts("\tHeightangle: "); |
449 | itoa(tanheight, conv_array, 10); |
450 | itoa(tanheight, conv_array, 10); |
450 | usart0_puts(conv_array); |
451 | usart0_puts(conv_array); |
451 | usart0_puts("\r\n"); |
452 | usart0_puts("\r\n"); |
452 | #endif |
453 | #endif |
453 | 454 | ||
454 | 455 | ||
455 | rxd_buffer_locked = 0; |
456 | rxd_buffer_locked = 0; |
456 | } |
457 | } |
457 | // handle keypress |
458 | // handle keypress |
458 | if (s1_pressed()) { |
459 | if (s1_pressed()) { |
459 | config_menu(); |
460 | config_menu(); |
460 | } |
461 | } |
461 | // reqest data untill there has been some answer |
462 | // reqest data untill there has been some answer |
462 | if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) || |
463 | if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED) || |
463 | // or while not in passive mode |
464 | // or while not in passive mode |
464 | (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) { |
465 | (seconds_since_last_data > 0 && !(COSD_FLAGS_CONFIG & COSD_FLAG_PASSIVE))) { |
465 | usart1_EnableTXD(); |
466 | usart1_EnableTXD(); |
466 | //usart1_puts_pgm(PSTR("zu alt\r\n")); |
467 | //usart1_puts_pgm(PSTR("zu alt\r\n")); |
467 | #if FCONLY |
468 | #if FCONLY |
468 | // request data ever 100ms from FC; |
469 | // request data ever 100ms from FC; |
469 | //usart1_request_mk_data(0, 'd', 100); |
470 | //usart1_request_mk_data(0, 'd', 100); |
470 | usart1_puts_pgm(PSTR(REQUEST_DBG_DATA)); |
471 | usart1_puts_pgm(PSTR(REQUEST_DBG_DATA)); |
471 | #else |
472 | #else |
472 | // request OSD Data from NC every 100ms |
473 | // request OSD Data from NC every 100ms |
473 | //usart1_request_mk_data(1, 'o', 100); |
474 | //usart1_request_mk_data(1, 'o', 100); |
474 | usart1_puts_pgm(PSTR(REQUEST_OSD_DATA)); |
475 | usart1_puts_pgm(PSTR(REQUEST_OSD_DATA)); |
475 | 476 | ||
476 | // and disable debug... |
477 | // and disable debug... |
477 | //usart1_request_mk_data(0, 'd', 0); |
478 | //usart1_request_mk_data(0, 'd', 0); |
478 | #endif |
479 | #endif |
479 | // reset last time counter |
480 | // reset last time counter |
480 | seconds_since_last_data = 0; |
481 | seconds_since_last_data = 0; |
481 | usart1_DisableTXD(); |
482 | usart1_DisableTXD(); |
482 | 483 | ||
483 | // do not spam too much |
484 | // do not spam too much |
484 | if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) { |
485 | if (!(COSD_FLAGS_RUNTIME & COSD_DATARECEIVED)) { |
485 | _delay_ms(300); |
486 | _delay_ms(300); |
486 | } |
487 | } |
487 | } |
488 | } |
- | 489 | ||
- | 490 | ||
- | 491 | if (SJ1_CLOSED && !(COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we do not want the HUD anymore |
|
- | 492 | if (COSD_FLAGS_MODES & COSD_FLAG_HUD) { |
|
- | 493 | clear(); |
|
- | 494 | } |
|
- | 495 | COSD_FLAGS_MODES &= ~COSD_FLAG_HUD; |
|
- | 496 | COSD_FLAGS_RUNTIME |= COSD_BLANKBYSJ; |
|
- | 497 | } else if (!SJ1_CLOSED && (COSD_FLAGS_RUNTIME & COSD_BLANKBYSJ)) { // we want the HUD back again |
|
- | 498 | if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) { |
|
- | 499 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
|
- | 500 | } |
|
- | 501 | COSD_FLAGS_MODES |= COSD_FLAG_HUD; |
|
- | 502 | COSD_FLAGS_RUNTIME &= ~(COSD_BLANKBYSJ); |
|
- | 503 | } |
|
- | 504 | ||
488 | } |
505 | } |
489 | 506 | ||
490 | #else // character flashing... |
507 | #else // character flashing... |
491 | clear(); |
508 | clear(); |
492 | write_all_chars(); |
509 | write_all_chars(); |
493 | LED1_ON |
510 | LED1_ON |
494 | LED2_ON |
511 | LED2_ON |
495 | LED3_ON |
512 | LED3_ON |
496 | LED4_ON |
513 | LED4_ON |
497 | while (1) { |
514 | while (1) { |
498 | 515 | ||
499 | }; |
516 | }; |
500 | #endif |
517 | #endif |
501 | 518 | ||
502 | 519 | ||
503 | return 0; |
520 | return 0; |
504 | } |
521 | } |
505 | 522 |