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