Rev 728 | Rev 736 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
471 | cascade | 1 | /**************************************************************************** |
728 | cascade | 2 | * Copyright (C) 2009-2010 by Claas Anders "CaScAdE" Rathje * |
471 | cascade | 3 | * admiralcascade@gmail.com * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
||
5 | * * |
||
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 * |
||
8 | * the Free Software Foundation; either version 2 of the License. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | ****************************************************************************/ |
||
20 | |||
21 | #include <avr/io.h> |
||
22 | #include <avr/eeprom.h> |
||
23 | #include <avr/pgmspace.h> |
||
24 | #include <avr/interrupt.h> |
||
25 | #include <util/delay.h> |
||
26 | #include "max7456_software_spi.h" |
||
27 | #include "config.h" |
||
28 | #include "main.h" |
||
29 | #include "buttons.h" |
||
30 | #include "usart1.h" |
||
31 | |||
32 | uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1; |
||
33 | uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2; |
||
523 | cascade | 34 | uint8_t EEMEM ee_COSD_FLAGS_MODES = 0; |
35 | uint8_t EEMEM ee_COSD_FLAGS_CONFIG = 0; |
||
497 | cascade | 36 | uint8_t EEMEM ee_COSD_DISPLAYMODE = 0; |
471 | cascade | 37 | |
514 | cascade | 38 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
39 | |||
40 | // store more fixed strings in progmen |
||
41 | char ON[] PROGMEM = "ON "; |
||
42 | char OFF[] PROGMEM = "OFF"; |
||
43 | |||
474 | cascade | 44 | // video modes |
489 | woggle | 45 | const char VM_PAL[] PROGMEM = "PAL "; |
46 | const char VM_NTSC[] PROGMEM = "NTSC"; |
||
474 | cascade | 47 | |
471 | cascade | 48 | // menu strings to progmem |
489 | woggle | 49 | const char menu_item0[] PROGMEM = "Video Mode"; |
50 | const char menu_item1[] PROGMEM = "Full HUD"; |
||
51 | const char menu_item2[] PROGMEM = "Art.Horizon in HUD"; |
||
52 | const char menu_item3[] PROGMEM = "Big Vario bar"; |
||
53 | const char menu_item4[] PROGMEM = "Statistics"; |
||
728 | cascade | 54 | //const char menu_item5[] PROGMEM = "Warnings"; // TODO: do it! |
55 | const char menu_item5[] PROGMEM = "Current by FC"; |
||
507 | cascade | 56 | const char menu_item6[] PROGMEM = "Voltage by C-Strom"; |
57 | const char menu_item7[] PROGMEM = "Reset uptime"; |
||
497 | cascade | 58 | const char menu_item8[] PROGMEM = "Display Mode"; |
523 | cascade | 59 | const char menu_item9[] PROGMEM = "Height by"; |
60 | const char menu_item10[] PROGMEM = "Save config"; |
||
61 | const char menu_item11[] PROGMEM = "EXIT"; |
||
489 | woggle | 62 | const char* menu[] = {menu_item0, menu_item1, menu_item2, menu_item3, menu_item4, |
523 | cascade | 63 | menu_item5, menu_item6, menu_item7, menu_item8, menu_item9, menu_item10, menu_item11}; |
471 | cascade | 64 | |
497 | cascade | 65 | const displaymode_t * mode; |
66 | |||
514 | cascade | 67 | #endif |
68 | |||
69 | const char ee_message0[] PROGMEM = "Loading Data from EEPROM"; |
||
70 | const char ee_message1[] PROGMEM = "No saved Data in EEPROM"; |
||
71 | const char* ee_msg[] PROGMEM = {ee_message0, ee_message1}; |
||
72 | |||
471 | cascade | 73 | /** |
474 | cascade | 74 | * read data saved in eeprom, print out message if <verbose> is set |
471 | cascade | 75 | */ |
474 | cascade | 76 | void get_eeprom(uint8_t verbose) { |
471 | cascade | 77 | if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) { |
514 | cascade | 78 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
474 | cascade | 79 | if (verbose) write_ascii_string_pgm(2, 9, ee_msg[0]); // Loading data |
514 | cascade | 80 | #endif |
523 | cascade | 81 | COSD_FLAGS_MODES = eeprom_read_byte(&ee_COSD_FLAGS_MODES); |
82 | COSD_FLAGS_CONFIG = eeprom_read_byte(&ee_COSD_FLAGS_CONFIG); |
||
497 | cascade | 83 | COSD_DISPLAYMODE = eeprom_read_byte(&ee_COSD_DISPLAYMODE); |
519 | cascade | 84 | //if (verbose) write_ndigit_number_u(23, 11, COSD_DISPLAYMODE, 2, 0); |
471 | cascade | 85 | } else { |
514 | cascade | 86 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
87 | if (verbose) write_ascii_string_pgm(2, 9, ee_msg[1]); // Loading data |
||
88 | #endif |
||
471 | cascade | 89 | } |
90 | } |
||
91 | |||
514 | cascade | 92 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
93 | |||
471 | cascade | 94 | /** |
95 | * save data to eeprom |
||
96 | */ |
||
97 | void save_eeprom() { |
||
98 | eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1); |
||
99 | eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2); |
||
523 | cascade | 100 | eeprom_write_byte(&ee_COSD_FLAGS_MODES, COSD_FLAGS_MODES); |
101 | eeprom_write_byte(&ee_COSD_FLAGS_CONFIG, COSD_FLAGS_CONFIG); |
||
497 | cascade | 102 | eeprom_write_byte(&ee_COSD_DISPLAYMODE, COSD_DISPLAYMODE); |
471 | cascade | 103 | } |
104 | |||
105 | /** |
||
106 | * auto config some stuff on startup, currently only battery cells |
||
107 | * TODO: this is testing stuff, strings should go progmem and so on... |
||
108 | */ |
||
109 | void init_cosd(uint8_t UBat) { |
||
110 | clear(); |
||
734 | cascade | 111 | write_ascii_string_pgm(2, 1, PSTR("C-OSD Initialisation")); // C-OSD Initialisation |
471 | cascade | 112 | #if FCONLY |
734 | cascade | 113 | write_ascii_string_pgm(2, 2, PSTR("FC only Mode")); // FC only mode |
471 | cascade | 114 | #else |
734 | cascade | 115 | write_ascii_string_pgm(2, 2, PSTR("NaviCtrl Mode")); // NaviCtrl Mode |
471 | cascade | 116 | #endif |
523 | cascade | 117 | write_ascii_string_pgm(2, 3, PSTR(BUILDDATE)); |
471 | cascade | 118 | uint8_t cellnum = 0; |
119 | if (CELL_NUM == -1) { |
||
734 | cascade | 120 | write_ascii_string_pgm(2, 4, PSTR("Guessing Number of Cells")); // Guessing Number of Cells |
471 | cascade | 121 | do { |
122 | cellnum++; |
||
123 | } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23)); |
||
124 | } else { |
||
125 | cellnum = CELL_NUM; |
||
126 | } |
||
127 | min_voltage = cellnum * CELL_VOLT_MIN; |
||
128 | max_voltage = cellnum * CELL_VOLT_MAX; |
||
734 | cascade | 129 | write_ascii_string_pgm(2, 5, PSTR("Number of Cells:")); // Number of Cells |
471 | cascade | 130 | write_ndigit_number_u(21, 5, cellnum, 1, 0); |
734 | cascade | 131 | write_ascii_string_pgm(2, 6, PSTR("Warn Voltage :")); // Warn Voltage |
519 | cascade | 132 | write_ndigit_number_s_10th(20, 6, min_voltage, 3, 0); |
734 | cascade | 133 | write_ascii_string_pgm(2, 7, PSTR("Max Voltage :")); // Max Voltage |
519 | cascade | 134 | write_ndigit_number_s_10th(20, 7, max_voltage, 3, 0); |
471 | cascade | 135 | |
474 | cascade | 136 | get_eeprom(1); |
471 | cascade | 137 | |
474 | cascade | 138 | //write_ascii_string_pgm(23, 2, vm[COSD_FLAGS & COSD_FLAG_NTSC]); |
523 | cascade | 139 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
474 | cascade | 140 | write_ascii_string_pgm(23, 2, VM_NTSC); |
141 | } else { |
||
142 | write_ascii_string_pgm(23, 2, VM_PAL); |
||
143 | } |
||
144 | |||
677 | cascade | 145 | // request version from board |
146 | rxd_buffer_locked = 0; |
||
147 | #if FCONLY |
||
148 | usart1_request_mk_data(0, 'v', 0); |
||
149 | write_ascii_string_pgm(2, 11, PSTR("FC VERSION: ........")); |
||
150 | #else |
||
151 | usart1_request_mk_data(1, 'v', 0); |
||
152 | write_ascii_string_pgm(2, 11, PSTR("NC VERSION: ........")); |
||
153 | #endif |
||
154 | // wait for response |
||
155 | while (rxd_buffer_locked == 0) { |
||
156 | asm("nop"); |
||
157 | } |
||
158 | Decode64(); |
||
159 | str_VersionInfo VersionInfo; |
||
160 | VersionInfo = *((str_VersionInfo*) pRxData); |
||
161 | |||
162 | write_ndigit_number_u(14, 11, VersionInfo.SWMajor, 3, 1); |
||
163 | write_ndigit_number_u(18, 11, VersionInfo.SWMinor, 3, 1); |
||
164 | write_ascii_char(22 + 11*30, 'a' + VersionInfo.SWPatch); |
||
165 | // end version request |
||
497 | cascade | 166 | |
677 | cascade | 167 | |
497 | cascade | 168 | #if FCONLY |
169 | COSD_DISPLAYMODE %= (sizeof(fcdisplaymodes) / sizeof(displaymode_t)); |
||
170 | mode = fcdisplaymodes; |
||
171 | mode += COSD_DISPLAYMODE; |
||
172 | osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun); |
||
677 | cascade | 173 | // re-request data ever 100ms from FC; |
174 | usart1_request_mk_data(0, 'd', 100); |
||
497 | cascade | 175 | #else |
176 | COSD_DISPLAYMODE %= (sizeof(ncdisplaymodes) / sizeof(displaymode_t)); |
||
177 | mode = ncdisplaymodes; |
||
178 | mode += COSD_DISPLAYMODE; |
||
179 | osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun); |
||
677 | cascade | 180 | // re-request OSD Data from NC every 100ms |
181 | usart1_request_mk_data(1, 'o', 100); |
||
497 | cascade | 182 | #endif |
183 | |||
734 | cascade | 184 | _delay_ms(3000); |
471 | cascade | 185 | clear(); |
186 | // update flags to paint display again because of clear |
||
523 | cascade | 187 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
471 | cascade | 188 | } |
189 | |||
190 | /* ########################################################################## |
||
191 | * A simple config menu for the flags |
||
192 | * ##########################################################################*/ |
||
193 | |||
194 | /** |
||
195 | * helper function for menu updating |
||
196 | */ |
||
197 | void config_menu_drawings(uint8_t chosen) { |
||
198 | // clear prevoius _cursor_ |
||
474 | cascade | 199 | write_ascii_string(3, (chosen + 2) % 10, " "); |
471 | cascade | 200 | // draw current _cursor_ |
474 | cascade | 201 | write_ascii_string(3, chosen + 2, ">"); |
202 | |||
523 | cascade | 203 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
474 | cascade | 204 | write_ascii_string_pgm(23, 2, VM_NTSC); |
205 | } else { |
||
206 | write_ascii_string_pgm(23, 2, VM_PAL); |
||
207 | } |
||
208 | |||
523 | cascade | 209 | if (COSD_FLAGS_MODES & COSD_FLAG_HUD) { |
471 | cascade | 210 | write_ascii_string_pgm(23, 3, ON); |
211 | } else { |
||
212 | write_ascii_string_pgm(23, 3, OFF); |
||
213 | } |
||
523 | cascade | 214 | if (COSD_FLAGS_MODES & COSD_FLAG_ARTHORIZON) { |
471 | cascade | 215 | write_ascii_string_pgm(23, 4, ON); |
216 | } else { |
||
217 | write_ascii_string_pgm(23, 4, OFF); |
||
218 | } |
||
523 | cascade | 219 | if (COSD_FLAGS_MODES & COSD_FLAG_BIGVARIO) { |
471 | cascade | 220 | write_ascii_string_pgm(23, 5, ON); |
221 | } else { |
||
222 | write_ascii_string_pgm(23, 5, OFF); |
||
223 | } |
||
523 | cascade | 224 | if (COSD_FLAGS_MODES & COSD_FLAG_STATS) { |
471 | cascade | 225 | write_ascii_string_pgm(23, 6, ON); |
226 | } else { |
||
227 | write_ascii_string_pgm(23, 6, OFF); |
||
228 | } |
||
728 | cascade | 229 | /*if (COSD_FLAGS_MODES & COSD_FLAG_WARNINGS) { |
471 | cascade | 230 | write_ascii_string_pgm(23, 7, ON); |
231 | } else { |
||
232 | write_ascii_string_pgm(23, 7, OFF); |
||
728 | cascade | 233 | }*/ |
234 | if (COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT) { |
||
235 | write_ascii_string_pgm(23, 7, ON); |
||
236 | } else { |
||
237 | write_ascii_string_pgm(23, 7, OFF); |
||
471 | cascade | 238 | } |
523 | cascade | 239 | if (COSD_FLAGS_MODES & COSD_FLAG_STROMVOLT) { |
507 | cascade | 240 | write_ascii_string_pgm(23, 8, ON); |
241 | } else { |
||
242 | write_ascii_string_pgm(23, 8, OFF); |
||
243 | } |
||
519 | cascade | 244 | //write_ndigit_number_u(23, 10, COSD_DISPLAYMODE, 2, 0); |
497 | cascade | 245 | write_ascii_string_pgm(18, 10, (const char *) (pgm_read_word(&(mode->desc)))); |
246 | |||
523 | cascade | 247 | if (COSD_FLAGS_CONFIG & COSD_FLAG_GPSHEIGHT) { |
248 | write_ascii_string_pgm(20, 11, PSTR(" GPS")); |
||
249 | } else { |
||
250 | write_ascii_string_pgm(20, 11, PSTR("BARO")); |
||
251 | } |
||
471 | cascade | 252 | } |
253 | |||
254 | /** |
||
255 | * some sort of clicking response in the menu |
||
256 | */ |
||
489 | woggle | 257 | void config_menu_doclick(uint8_t chosen, const char* menu[]) { |
523 | cascade | 258 | write_ascii_string_pgm(4, chosen + 2, PSTR("DONE ")); |
471 | cascade | 259 | _delay_ms(500); |
474 | cascade | 260 | write_ascii_string_pgm(4, chosen + 2, menu[chosen]); |
471 | cascade | 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 | uint8_t inmenu = 1; |
||
274 | uint8_t chosen = 0; |
||
523 | cascade | 275 | write_ascii_string_pgm(6, 1, PSTR("C-OSD Config Menu")); |
471 | cascade | 276 | |
277 | // wait a bit before doing stuff so user has chance to release button |
||
278 | _delay_ms(250); |
||
279 | |||
474 | cascade | 280 | write_ascii_string_pgm(4, 2, menu[0]); |
281 | write_ascii_string_pgm(4, 3, menu[1]); |
||
282 | write_ascii_string_pgm(4, 4, menu[2]); |
||
283 | write_ascii_string_pgm(4, 5, menu[3]); |
||
284 | write_ascii_string_pgm(4, 6, menu[4]); |
||
285 | write_ascii_string_pgm(4, 7, menu[5]); |
||
286 | write_ascii_string_pgm(4, 8, menu[6]); |
||
287 | write_ascii_string_pgm(4, 9, menu[7]); |
||
288 | write_ascii_string_pgm(4, 10, menu[8]); |
||
289 | write_ascii_string_pgm(4, 11, menu[9]); |
||
290 | write_ascii_string_pgm(4, 12, menu[10]); |
||
523 | cascade | 291 | write_ascii_string_pgm(4, 13, menu[11]); |
471 | cascade | 292 | |
293 | config_menu_drawings(chosen); |
||
294 | |||
295 | while (inmenu) { |
||
296 | if (s2_pressed()) { |
||
474 | cascade | 297 | write_ascii_string(3, chosen + 2, " "); |
523 | cascade | 298 | chosen = (chosen + 1) % 12; |
474 | cascade | 299 | write_ascii_string(3, chosen + 2, ">"); |
471 | cascade | 300 | _delay_ms(500); |
301 | } else if (s1_pressed()) { |
||
302 | switch (chosen) { |
||
523 | cascade | 303 | case 0: // NTSC or PAL |
304 | COSD_FLAGS_CONFIG ^= COSD_FLAG_NTSC; |
||
474 | cascade | 305 | // Setup Video Mode |
523 | cascade | 306 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
474 | cascade | 307 | // NTSC + enable display immediately (VM0) |
308 | spi_send_byte(0x00, 0b00001000); |
||
309 | |||
310 | bottom_line = 12; |
||
311 | } else { |
||
312 | // PAL + enable display immediately (VM0) |
||
313 | spi_send_byte(0x00, 0b01001000); |
||
314 | |||
315 | bottom_line = 14; |
||
316 | } |
||
317 | config_menu_drawings(chosen); |
||
318 | break; |
||
319 | case 1: // full HUD |
||
523 | cascade | 320 | COSD_FLAGS_MODES ^= COSD_FLAG_HUD; |
471 | cascade | 321 | config_menu_drawings(chosen); |
322 | break; |
||
474 | cascade | 323 | case 2: // art horizon |
523 | cascade | 324 | COSD_FLAGS_MODES ^= COSD_FLAG_ARTHORIZON; |
471 | cascade | 325 | config_menu_drawings(chosen); |
326 | break; |
||
474 | cascade | 327 | case 3: // big vario |
523 | cascade | 328 | COSD_FLAGS_MODES ^= COSD_FLAG_BIGVARIO; |
471 | cascade | 329 | config_menu_drawings(chosen); |
330 | break; |
||
474 | cascade | 331 | case 4: // statistics |
523 | cascade | 332 | COSD_FLAGS_MODES ^= COSD_FLAG_STATS; |
471 | cascade | 333 | config_menu_drawings(chosen); |
334 | break; |
||
728 | cascade | 335 | /*case 5: // warnings |
523 | cascade | 336 | COSD_FLAGS_MODES ^= COSD_FLAG_WARNINGS; |
471 | cascade | 337 | config_menu_drawings(chosen); |
728 | cascade | 338 | break;*/ |
339 | case 5: // current by fc |
||
340 | COSD_FLAGS_MODES ^= COSD_FLAG_FCCURRENT; |
||
341 | config_menu_drawings(chosen); |
||
471 | cascade | 342 | break; |
728 | cascade | 343 | case 6: // 2nd voltage by c-strom |
523 | cascade | 344 | COSD_FLAGS_MODES ^= COSD_FLAG_STROMVOLT; |
507 | cascade | 345 | config_menu_drawings(chosen); |
346 | break; |
||
347 | case 7: // reset uptime |
||
471 | cascade | 348 | uptime = 0; |
349 | config_menu_doclick(chosen, menu); |
||
350 | break; |
||
497 | cascade | 351 | case 8: // change mode |
352 | #if FCONLY |
||
353 | COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof(fcdisplaymodes) / sizeof(displaymode_t)); |
||
354 | mode = fcdisplaymodes; |
||
355 | mode += COSD_DISPLAYMODE; |
||
356 | osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun); |
||
357 | #else |
||
358 | COSD_DISPLAYMODE = (COSD_DISPLAYMODE + 1) % (sizeof(ncdisplaymodes) / sizeof(displaymode_t)); |
||
359 | mode = ncdisplaymodes; |
||
360 | mode += COSD_DISPLAYMODE; |
||
361 | osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun); |
||
362 | #endif |
||
363 | config_menu_drawings(chosen); |
||
471 | cascade | 364 | break; |
523 | cascade | 365 | case 9: // GPS or BARO height |
366 | COSD_FLAGS_CONFIG ^= COSD_FLAG_GPSHEIGHT; |
||
367 | config_menu_drawings(chosen); |
||
368 | break; |
||
369 | case 10: // save |
||
471 | cascade | 370 | save_eeprom(); |
371 | config_menu_doclick(chosen, menu); |
||
372 | break; |
||
523 | cascade | 373 | case 11: // exit |
471 | cascade | 374 | inmenu = 0; |
375 | break; |
||
376 | } |
||
377 | _delay_ms(250); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | // clear screen up again |
||
382 | clear(); |
||
383 | |||
384 | // update flags to paint display again if needed |
||
523 | cascade | 385 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
471 | cascade | 386 | |
387 | // enable interrupts again |
||
388 | sei(); |
||
389 | } |
||
390 | |||
391 | #endif |