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