Rev 757 | Rev 761 | 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> |
||
758 | cascade | 26 | //#include <avr/delay.h> |
471 | cascade | 27 | #include "max7456_software_spi.h" |
28 | #include "config.h" |
||
29 | #include "main.h" |
||
30 | #include "buttons.h" |
||
31 | #include "usart1.h" |
||
32 | |||
33 | uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1; |
||
34 | uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2; |
||
523 | cascade | 35 | uint8_t EEMEM ee_COSD_FLAGS_MODES = 0; |
36 | uint8_t EEMEM ee_COSD_FLAGS_CONFIG = 0; |
||
497 | cascade | 37 | uint8_t EEMEM ee_COSD_DISPLAYMODE = 0; |
471 | cascade | 38 | |
514 | cascade | 39 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
40 | |||
474 | cascade | 41 | // video modes |
489 | woggle | 42 | const char VM_PAL[] PROGMEM = "PAL "; |
43 | const char VM_NTSC[] PROGMEM = "NTSC"; |
||
474 | cascade | 44 | |
471 | cascade | 45 | |
497 | cascade | 46 | const displaymode_t * mode; |
47 | |||
514 | cascade | 48 | #endif |
49 | |||
50 | const char ee_message0[] PROGMEM = "Loading Data from EEPROM"; |
||
51 | const char ee_message1[] PROGMEM = "No saved Data in EEPROM"; |
||
52 | const char* ee_msg[] PROGMEM = {ee_message0, ee_message1}; |
||
53 | |||
471 | cascade | 54 | /** |
474 | cascade | 55 | * read data saved in eeprom, print out message if <verbose> is set |
471 | cascade | 56 | */ |
474 | cascade | 57 | void get_eeprom(uint8_t verbose) { |
471 | cascade | 58 | if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) { |
514 | cascade | 59 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
474 | cascade | 60 | if (verbose) write_ascii_string_pgm(2, 9, ee_msg[0]); // Loading data |
514 | cascade | 61 | #endif |
523 | cascade | 62 | COSD_FLAGS_MODES = eeprom_read_byte(&ee_COSD_FLAGS_MODES); |
63 | COSD_FLAGS_CONFIG = eeprom_read_byte(&ee_COSD_FLAGS_CONFIG); |
||
497 | cascade | 64 | COSD_DISPLAYMODE = eeprom_read_byte(&ee_COSD_DISPLAYMODE); |
519 | cascade | 65 | //if (verbose) write_ndigit_number_u(23, 11, COSD_DISPLAYMODE, 2, 0); |
471 | cascade | 66 | } else { |
514 | cascade | 67 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
68 | if (verbose) write_ascii_string_pgm(2, 9, ee_msg[1]); // Loading data |
||
69 | #endif |
||
471 | cascade | 70 | } |
71 | } |
||
72 | |||
514 | cascade | 73 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
74 | |||
471 | cascade | 75 | /** |
76 | * save data to eeprom |
||
77 | */ |
||
78 | void save_eeprom() { |
||
79 | eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1); |
||
80 | eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2); |
||
523 | cascade | 81 | eeprom_write_byte(&ee_COSD_FLAGS_MODES, COSD_FLAGS_MODES); |
82 | eeprom_write_byte(&ee_COSD_FLAGS_CONFIG, COSD_FLAGS_CONFIG); |
||
497 | cascade | 83 | eeprom_write_byte(&ee_COSD_DISPLAYMODE, COSD_DISPLAYMODE); |
471 | cascade | 84 | } |
85 | |||
758 | cascade | 86 | |
471 | cascade | 87 | /** |
88 | * auto config some stuff on startup, currently only battery cells |
||
89 | */ |
||
90 | void init_cosd(uint8_t UBat) { |
||
91 | clear(); |
||
734 | cascade | 92 | write_ascii_string_pgm(2, 1, PSTR("C-OSD Initialisation")); // C-OSD Initialisation |
471 | cascade | 93 | #if FCONLY |
734 | cascade | 94 | write_ascii_string_pgm(2, 2, PSTR("FC only Mode")); // FC only mode |
471 | cascade | 95 | #else |
734 | cascade | 96 | write_ascii_string_pgm(2, 2, PSTR("NaviCtrl Mode")); // NaviCtrl Mode |
471 | cascade | 97 | #endif |
523 | cascade | 98 | write_ascii_string_pgm(2, 3, PSTR(BUILDDATE)); |
471 | cascade | 99 | uint8_t cellnum = 0; |
100 | if (CELL_NUM == -1) { |
||
734 | cascade | 101 | write_ascii_string_pgm(2, 4, PSTR("Guessing Number of Cells")); // Guessing Number of Cells |
471 | cascade | 102 | do { |
103 | cellnum++; |
||
104 | } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23)); |
||
105 | } else { |
||
106 | cellnum = CELL_NUM; |
||
107 | } |
||
108 | min_voltage = cellnum * CELL_VOLT_MIN; |
||
109 | max_voltage = cellnum * CELL_VOLT_MAX; |
||
734 | cascade | 110 | write_ascii_string_pgm(2, 5, PSTR("Number of Cells:")); // Number of Cells |
471 | cascade | 111 | write_ndigit_number_u(21, 5, cellnum, 1, 0); |
734 | cascade | 112 | write_ascii_string_pgm(2, 6, PSTR("Warn Voltage :")); // Warn Voltage |
519 | cascade | 113 | write_ndigit_number_s_10th(20, 6, min_voltage, 3, 0); |
734 | cascade | 114 | write_ascii_string_pgm(2, 7, PSTR("Max Voltage :")); // Max Voltage |
519 | cascade | 115 | write_ndigit_number_s_10th(20, 7, max_voltage, 3, 0); |
471 | cascade | 116 | |
474 | cascade | 117 | get_eeprom(1); |
471 | cascade | 118 | |
474 | cascade | 119 | //write_ascii_string_pgm(23, 2, vm[COSD_FLAGS & COSD_FLAG_NTSC]); |
523 | cascade | 120 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
474 | cascade | 121 | write_ascii_string_pgm(23, 2, VM_NTSC); |
122 | } else { |
||
123 | write_ascii_string_pgm(23, 2, VM_PAL); |
||
124 | } |
||
125 | |||
677 | cascade | 126 | // request version from board |
127 | rxd_buffer_locked = 0; |
||
738 | cascade | 128 | usart1_EnableTXD(); |
677 | cascade | 129 | #if FCONLY |
738 | cascade | 130 | //usart1_request_mk_data(0, 'v', 0); |
131 | write_ascii_string_pgm(2, 11, PSTR("FC VERSION: ........")); |
||
132 | usart1_puts_pgm(PSTR(REQUEST_FC_VERSION)); |
||
677 | cascade | 133 | #else |
738 | cascade | 134 | //usart1_request_mk_data(1, 'v', 0); |
135 | usart1_puts_pgm(PSTR(REQUEST_NC_VERSION)); |
||
136 | write_ascii_string_pgm(2, 11, PSTR("NC VERSION: ........")); |
||
677 | cascade | 137 | #endif |
738 | cascade | 138 | usart1_DisableTXD(); |
677 | cascade | 139 | // wait for response |
140 | while (rxd_buffer_locked == 0) { |
||
141 | asm("nop"); |
||
142 | } |
||
143 | Decode64(); |
||
144 | str_VersionInfo VersionInfo; |
||
145 | VersionInfo = *((str_VersionInfo*) pRxData); |
||
146 | |||
147 | write_ndigit_number_u(14, 11, VersionInfo.SWMajor, 3, 1); |
||
148 | write_ndigit_number_u(18, 11, VersionInfo.SWMinor, 3, 1); |
||
149 | write_ascii_char(22 + 11*30, 'a' + VersionInfo.SWPatch); |
||
150 | // end version request |
||
497 | cascade | 151 | |
677 | cascade | 152 | |
497 | cascade | 153 | #if FCONLY |
154 | COSD_DISPLAYMODE %= (sizeof(fcdisplaymodes) / sizeof(displaymode_t)); |
||
155 | mode = fcdisplaymodes; |
||
156 | mode += COSD_DISPLAYMODE; |
||
157 | osd_fcmode = (int(*)(void)) pgm_read_word(&mode->dfun); |
||
677 | cascade | 158 | // re-request data ever 100ms from FC; |
738 | cascade | 159 | //usart1_request_mk_data(0, 'd', 100); |
497 | cascade | 160 | #else |
161 | COSD_DISPLAYMODE %= (sizeof(ncdisplaymodes) / sizeof(displaymode_t)); |
||
162 | mode = ncdisplaymodes; |
||
163 | mode += COSD_DISPLAYMODE; |
||
164 | osd_ncmode = (int(*)(void)) pgm_read_word(&mode->dfun); |
||
677 | cascade | 165 | // re-request OSD Data from NC every 100ms |
738 | cascade | 166 | //usart1_request_mk_data(1, 'o', 100); |
497 | cascade | 167 | #endif |
168 | |||
758 | cascade | 169 | _delay_ms(3000); |
471 | cascade | 170 | clear(); |
171 | // update flags to paint display again because of clear |
||
523 | cascade | 172 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
471 | cascade | 173 | } |
174 | |||
175 | /* ########################################################################## |
||
176 | * A simple config menu for the flags |
||
177 | * ##########################################################################*/ |
||
178 | |||
179 | /** |
||
736 | cascade | 180 | * helper function for flag display |
181 | */ |
||
757 | cascade | 182 | void onoff(uint8_t col, uint8_t line, uint8_t onoff) { |
736 | cascade | 183 | if (onoff) { |
757 | cascade | 184 | write_ascii_string_pgm(col, line, PSTR("ON ")); |
736 | cascade | 185 | } else { |
757 | cascade | 186 | write_ascii_string_pgm(col, line, PSTR("OFF")); |
736 | cascade | 187 | } |
188 | } |
||
189 | |||
757 | cascade | 190 | |
736 | cascade | 191 | /** |
471 | cascade | 192 | * helper function for menu updating |
193 | */ |
||
194 | void config_menu_drawings(uint8_t chosen) { |
||
757 | cascade | 195 | static uint8_t old_y = 0; |
196 | uint8_t x = MENU_LEFT, y = MENU_TOP, line = MENU_TOP; |
||
736 | cascade | 197 | |
757 | cascade | 198 | if (chosen > 5 && chosen < 12) { // right |
199 | x = MENU_MIDDLE; |
||
200 | y = chosen - 6 + MENU_TOP; |
||
201 | } else if (chosen < 7) { |
||
202 | y = chosen + MENU_TOP; |
||
203 | } else { |
||
204 | y = chosen - 6 + MENU_TOP; |
||
205 | } |
||
206 | |||
207 | // clear prevoius _cursor_ and draw current |
||
208 | for (uint8_t myx = MENU_LEFT; myx < 29; myx++) { |
||
209 | write_char_att_xy(myx, old_y, 0); |
||
210 | if (myx > x - 1 && myx < x + 14) { |
||
211 | write_char_att_xy(myx, y, BLACKBG | INVERT); |
||
212 | } |
||
736 | cascade | 213 | }; |
474 | cascade | 214 | |
757 | cascade | 215 | write_ascii_string_pgm(MENU_LEFT, line, PSTR("Video")); |
523 | cascade | 216 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
757 | cascade | 217 | write_ascii_string_pgm(MENU_LEFT + 10, line, VM_NTSC); |
474 | cascade | 218 | } else { |
757 | cascade | 219 | write_ascii_string_pgm(MENU_LEFT + 10, line, VM_PAL); |
474 | cascade | 220 | } |
497 | cascade | 221 | |
757 | cascade | 222 | write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Full HUD")); |
223 | onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_HUD); |
||
736 | cascade | 224 | |
757 | cascade | 225 | write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Horizon")); |
226 | onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_ARTHORIZON); |
||
227 | |||
228 | write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Aggr.Hor.")); |
||
229 | onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_AGGRHORIZON); |
||
736 | cascade | 230 | |
757 | cascade | 231 | write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("Stats")); |
232 | onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_STATS); |
||
736 | cascade | 233 | |
757 | cascade | 234 | write_ascii_string_pgm(MENU_LEFT, ++line, PSTR("A by FC")); |
235 | onoff(MENU_LEFT + 10, line, COSD_FLAGS_MODES & COSD_FLAG_FCCURRENT); |
||
736 | cascade | 236 | |
757 | cascade | 237 | // 2nd col |
238 | line = 2; |
||
736 | cascade | 239 | |
757 | cascade | 240 | write_ascii_string_pgm(MENU_MIDDLE, line, PSTR("V C-Strom")); |
241 | onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_MODES & COSD_FLAG_STROMVOLT); |
||
736 | cascade | 242 | |
757 | cascade | 243 | write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Height by")); |
523 | cascade | 244 | if (COSD_FLAGS_CONFIG & COSD_FLAG_GPSHEIGHT) { |
757 | cascade | 245 | write_ascii_string_pgm(MENU_MIDDLE + 10, line, PSTR(" GPS")); |
523 | cascade | 246 | } else { |
757 | cascade | 247 | write_ascii_string_pgm(MENU_MIDDLE + 10, line, PSTR("BARO")); |
523 | cascade | 248 | } |
736 | cascade | 249 | |
757 | cascade | 250 | write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Feet/mph")); |
251 | onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_CONFIG & COSD_FLAG_FEET); |
||
252 | |||
253 | write_ascii_string_pgm(MENU_MIDDLE, ++line, PSTR("Big Vario")); |
||
254 | onoff(MENU_MIDDLE + 10, line, COSD_FLAGS_MODES & COSD_FLAG_BIGVARIO); |
||
255 | |||
256 | |||
257 | // bottom |
||
258 | write_ascii_string_pgm(MENU_LEFT, 9, PSTR("Reset uptime")); |
||
259 | |||
260 | write_ascii_string_pgm(MENU_LEFT, 10, PSTR("Display Mode")); |
||
261 | write_ascii_string_pgm(18, 10, (const char *) (pgm_read_word(&(mode->desc)))); |
||
262 | |||
263 | write_ascii_string_pgm(MENU_LEFT, 11, PSTR("Save config")); |
||
264 | write_ascii_string_pgm(MENU_LEFT, 12, PSTR("EXIT")); |
||
265 | |||
266 | old_y = y; |
||
471 | cascade | 267 | } |
268 | |||
269 | /** |
||
270 | * some sort of clicking response in the menu |
||
271 | */ |
||
736 | cascade | 272 | void config_menu_doclick(uint8_t chosen) { |
757 | cascade | 273 | write_ascii_string_pgm(MENU_LEFT, chosen + MENU_TOP - 6, PSTR("DONE ")); |
471 | cascade | 274 | _delay_ms(500); |
736 | cascade | 275 | config_menu_drawings(chosen); |
471 | cascade | 276 | } |
277 | |||
278 | /** |
||
279 | * a simple config menu tryout |
||
280 | */ |
||
281 | void config_menu(void) { |
||
282 | // disable interrupts (makes the menu more smoothely) |
||
283 | cli(); |
||
284 | |||
285 | // clear screen |
||
286 | clear(); |
||
287 | |||
288 | uint8_t chosen = 0; |
||
736 | cascade | 289 | uint8_t inmenu = 1; |
523 | cascade | 290 | write_ascii_string_pgm(6, 1, PSTR("C-OSD Config Menu")); |
471 | cascade | 291 | |
292 | // wait a bit before doing stuff so user has chance to release button |
||
293 | _delay_ms(250); |
||
294 | |||
295 | config_menu_drawings(chosen); |
||
296 | |||
297 | while (inmenu) { |
||
298 | if (s2_pressed()) { |
||
757 | cascade | 299 | chosen = (chosen + 1) % 17; |
300 | if (chosen == 10) chosen = 13; // SKIP unused menu space for now |
||
736 | cascade | 301 | config_menu_drawings(chosen); |
302 | _delay_ms(500); |
||
471 | cascade | 303 | } else if (s1_pressed()) { |
304 | switch (chosen) { |
||
523 | cascade | 305 | case 0: // NTSC or PAL |
306 | COSD_FLAGS_CONFIG ^= COSD_FLAG_NTSC; |
||
474 | cascade | 307 | // Setup Video Mode |
523 | cascade | 308 | if (COSD_FLAGS_CONFIG & COSD_FLAG_NTSC) { |
474 | cascade | 309 | // NTSC + enable display immediately (VM0) |
310 | spi_send_byte(0x00, 0b00001000); |
||
311 | |||
312 | bottom_line = 12; |
||
313 | } else { |
||
314 | // PAL + enable display immediately (VM0) |
||
315 | spi_send_byte(0x00, 0b01001000); |
||
316 | |||
317 | bottom_line = 14; |
||
318 | } |
||
319 | break; |
||
320 | case 1: // full HUD |
||
523 | cascade | 321 | COSD_FLAGS_MODES ^= COSD_FLAG_HUD; |
471 | cascade | 322 | break; |
474 | cascade | 323 | case 2: // art horizon |
523 | cascade | 324 | COSD_FLAGS_MODES ^= COSD_FLAG_ARTHORIZON; |
471 | cascade | 325 | break; |
757 | cascade | 326 | case 3: // aggressiva horizon |
327 | COSD_FLAGS_MODES ^= COSD_FLAG_AGGRHORIZON; |
||
471 | cascade | 328 | break; |
474 | cascade | 329 | case 4: // statistics |
523 | cascade | 330 | COSD_FLAGS_MODES ^= COSD_FLAG_STATS; |
471 | cascade | 331 | break; |
728 | cascade | 332 | case 5: // current by fc |
333 | COSD_FLAGS_MODES ^= COSD_FLAG_FCCURRENT; |
||
471 | cascade | 334 | break; |
728 | cascade | 335 | case 6: // 2nd voltage by c-strom |
523 | cascade | 336 | COSD_FLAGS_MODES ^= COSD_FLAG_STROMVOLT; |
507 | cascade | 337 | break; |
757 | cascade | 338 | case 7: // GPS or BARO height |
339 | COSD_FLAGS_CONFIG ^= COSD_FLAG_GPSHEIGHT; |
||
340 | break; |
||
341 | case 8: // Feet and mph? |
||
342 | COSD_FLAGS_CONFIG ^= COSD_FLAG_FEET; |
||
343 | break; |
||
344 | case 9: // big vario |
||
345 | COSD_FLAGS_MODES ^= COSD_FLAG_BIGVARIO; |
||
346 | break; |
||
347 | case 13: // reset uptime |
||
471 | cascade | 348 | uptime = 0; |
757 | cascade | 349 | config_menu_doclick(chosen); |
471 | cascade | 350 | break; |
757 | cascade | 351 | case 14: // change mode |
497 | cascade | 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 |
||
471 | cascade | 363 | break; |
757 | cascade | 364 | case 15: // save |
471 | cascade | 365 | save_eeprom(); |
757 | cascade | 366 | config_menu_doclick(chosen); |
471 | cascade | 367 | break; |
757 | cascade | 368 | case 16: // exit |
736 | cascade | 369 | inmenu = 0; |
757 | cascade | 370 | config_menu_doclick(chosen); |
471 | cascade | 371 | break; |
372 | } |
||
757 | cascade | 373 | config_menu_drawings(chosen); |
471 | cascade | 374 | _delay_ms(250); |
375 | } |
||
376 | } |
||
377 | |||
378 | // clear screen up again |
||
379 | clear(); |
||
380 | |||
381 | // update flags to paint display again if needed |
||
523 | cascade | 382 | COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN; |
471 | cascade | 383 | |
384 | // enable interrupts again |
||
385 | sei(); |
||
386 | } |
||
387 | |||
388 | #endif |