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