Subversion Repositories Projects

Rev

Rev 474 | Go to most recent revision | Details | 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
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
33
 
34
// store more fixed strings in progmen
35
char ON[] PROGMEM = "ON ";
36
char OFF[] PROGMEM = "OFF";
37
 
38
uint8_t EEMEM ee_checkbyte1 = CHECKBYTE1;
39
uint8_t EEMEM ee_checkbyte2 = CHECKBYTE2;
40
uint16_t EEMEM ee_cal_ampere = 512;
41
uint8_t EEMEM ee_sensor = 50;
42
uint8_t EEMEM ee_COSD_FLAGS;
43
 
44
// store init strings in progmem
45
char init_0[] PROGMEM = "C-OSD Initialisation";
46
char init_1[] PROGMEM = "FC only Mode";
47
char init_2[] PROGMEM = "NaviCtrl Mode";
48
char init_3[] PROGMEM = "Guessing Number of Cells";
49
char init_4[] PROGMEM = "Number of Cells:";
50
char init_5[] PROGMEM = "Warn Voltage   :";
51
char init_6[] PROGMEM = "Max Voltage    :";
52
char* init_point[] PROGMEM = {init_0, init_1, init_2, init_3, init_4, init_5, init_6};
53
 
54
char ee_message0[] PROGMEM = "Loading Data from EEPROM";
55
char ee_message1[] PROGMEM = "No saved Data in EEPROM";
56
char* ee_msg[] PROGMEM = {ee_message0, ee_message1};
57
 
58
// menu strings to progmem
59
char menu_item0[] PROGMEM = "Full HUD";
60
char menu_item1[] PROGMEM = "Art.Horizon in HUD";
61
char menu_item2[] PROGMEM = "Big Vario bar";
62
char menu_item3[] PROGMEM = "Statistics";
63
char menu_item4[] PROGMEM = "Warnings"; // TODO: do it!
64
char menu_item5[] PROGMEM = "Reset uptime";
65
char menu_item6[] PROGMEM = "Request OSD-data";
66
char menu_item7[] PROGMEM = "Disable Debug-data";
67
char menu_item8[] PROGMEM = "Save config";
68
char menu_item9[] PROGMEM = "EXIT";
69
char* menu[] = {menu_item0, menu_item1, menu_item2, menu_item3, menu_item4,
70
        menu_item5, menu_item6, menu_item7, menu_item8, menu_item9};
71
 
72
/**
73
 * read data saved in eeprom
74
 */
75
void get_eeprom() {
76
        if (eeprom_read_byte(&ee_checkbyte1) == CHECKBYTE1 && eeprom_read_byte(&ee_checkbyte2) == CHECKBYTE2) {
77
                write_ascii_string_pgm(2, 9, ee_msg[0]); // Loading data
78
                COSD_FLAGS = eeprom_read_byte(&ee_COSD_FLAGS);
79
        } else {
80
                write_ascii_string_pgm(2, 9, ee_msg[1]); // No data found
81
        }
82
}
83
 
84
/**
85
 * save data to eeprom
86
 */
87
void save_eeprom() {
88
        eeprom_write_byte(&ee_checkbyte1, CHECKBYTE1);
89
        eeprom_write_byte(&ee_checkbyte2, CHECKBYTE2);
90
        eeprom_write_byte(&ee_COSD_FLAGS, COSD_FLAGS);
91
}
92
 
93
/**
94
 * auto config some stuff on startup, currently only battery cells
95
 * TODO: this is testing stuff, strings should go progmem and so on...
96
 */
97
void init_cosd(uint8_t UBat) {
98
    clear();
99
        write_ascii_string_pgm(2, 1, init_point[0]); // C-OSD Initialisation
100
    //write_ascii_string(2, 1, "C-OSD Initialisation");
101
#if FCONLY
102
        write_ascii_string_pgm(2, 2, init_point[1]); // FC only mode
103
    //write_ascii_string(2, 2, "FC only Mode");
104
#else
105
        write_ascii_string_pgm(2, 2, init_point[2]); // NaviCtrl Mode
106
    //write_ascii_string(2, 2, "NaviCtrl Mode");
107
#endif
108
    write_ascii_string(2, 3, BUILDDATE);
109
    uint8_t cellnum = 0;
110
    if (CELL_NUM == -1) {
111
                write_ascii_string_pgm(2, 4, init_point[3]); // Guessing Number of Cells
112
        //write_ascii_string(2, 6, "Guessing Number of Cells");
113
        do {
114
            cellnum++;
115
        } while (UBat > ((cellnum * CELL_VOLT_MAX) + 23));
116
    } else {
117
        cellnum = CELL_NUM;
118
    }
119
    min_voltage = cellnum * CELL_VOLT_MIN;
120
    max_voltage = cellnum * CELL_VOLT_MAX;
121
        write_ascii_string_pgm(2, 5, init_point[4]); // Number of Cells
122
    //write_ascii_string(2, 5, "Number of Cells:");
123
    write_ndigit_number_u(21, 5, cellnum, 1, 0);
124
        write_ascii_string_pgm(2, 6, init_point[5]); // Warn Voltage
125
    //write_ascii_string(2, 6, "Warn Voltage   :");
126
    write_ndigit_number_s_10th(20, 6, min_voltage, 100, 0);
127
        write_ascii_string_pgm(2, 7, init_point[6]); // Max Voltage
128
    //write_ascii_string(2, 7, "Max Voltage    :");
129
    write_ndigit_number_s_10th(20, 7, max_voltage, 100, 0);
130
 
131
        get_eeprom();
132
 
133
    _delay_ms(200);
134
    clear();
135
    // update flags to paint display again because of clear
136
    COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
137
}
138
 
139
/* ##########################################################################
140
 * A simple config menu for the flags
141
 * ##########################################################################*/
142
 
143
/**
144
 * helper function for menu updating
145
 */
146
void config_menu_drawings(uint8_t chosen) {
147
    // clear prevoius _cursor_
148
    write_ascii_string(3, (chosen + 3) % 9, " ");
149
    // draw current _cursor_
150
    write_ascii_string(3, chosen + 3, ">");
151
    if (COSD_FLAGS & COSD_FLAG_HUD) {
152
        write_ascii_string_pgm(23, 3, ON);
153
    } else {
154
        write_ascii_string_pgm(23, 3, OFF);
155
    }
156
    if (COSD_FLAGS & COSD_FLAG_ARTHORIZON) {
157
        write_ascii_string_pgm(23, 4, ON);
158
    } else {
159
        write_ascii_string_pgm(23, 4, OFF);
160
    }
161
    if (COSD_FLAGS & COSD_FLAG_BIGVARIO) {
162
        write_ascii_string_pgm(23, 5, ON);
163
    } else {
164
        write_ascii_string_pgm(23, 5, OFF);
165
    }
166
    if (COSD_FLAGS & COSD_FLAG_STATS) {
167
        write_ascii_string_pgm(23, 6, ON);
168
    } else {
169
        write_ascii_string_pgm(23, 6, OFF);
170
    }
171
    if (COSD_FLAGS & COSD_FLAG_WARNINGS) {
172
        write_ascii_string_pgm(23, 7, ON);
173
    } else {
174
        write_ascii_string_pgm(23, 7, OFF);
175
    }
176
}
177
 
178
/**
179
 * some sort of clicking response in the menu
180
 */
181
void config_menu_doclick(uint8_t chosen, char** menu) {
182
    write_ascii_string(4, chosen + 3, "DONE              ");
183
    _delay_ms(500);
184
    write_ascii_string_pgm(4, chosen + 3, menu[chosen]);
185
}
186
 
187
/**
188
 * a simple config menu tryout
189
 */
190
void config_menu(void) {
191
    // disable interrupts (makes the menu more smoothely)
192
    cli();
193
 
194
    // clear screen
195
    clear();
196
 
197
    uint8_t inmenu = 1;
198
    uint8_t chosen = 0;
199
    write_ascii_string(6, 2, "C-OSD Config Menu");
200
 
201
    // wait a bit before doing stuff so user has chance to release button
202
    _delay_ms(250);
203
 
204
    write_ascii_string_pgm(4, 3, menu[0]);
205
    write_ascii_string_pgm(4, 4, menu[1]);
206
    write_ascii_string_pgm(4, 5, menu[2]);
207
    write_ascii_string_pgm(4, 6, menu[3]);
208
    write_ascii_string_pgm(4, 7, menu[4]);
209
    write_ascii_string_pgm(4, 8, menu[5]);
210
    write_ascii_string_pgm(4, 9, menu[6]);
211
    write_ascii_string_pgm(4, 10, menu[7]);
212
    write_ascii_string_pgm(4, 11, menu[8]);
213
        write_ascii_string_pgm(4, 12, menu[9]);
214
 
215
    config_menu_drawings(chosen);
216
 
217
    while (inmenu) {
218
        if (s2_pressed()) {
219
            write_ascii_string(3, chosen + 3, " ");
220
            chosen = (chosen + 1) % 10;
221
            write_ascii_string(3, chosen + 3, ">");
222
            _delay_ms(500);
223
        } else if (s1_pressed()) {
224
            switch (chosen) {
225
                case 0: // full HUD
226
                    COSD_FLAGS ^= COSD_FLAG_HUD;
227
                    config_menu_drawings(chosen);
228
                    break;
229
                case 1: // art horizon
230
                    COSD_FLAGS ^= COSD_FLAG_ARTHORIZON;
231
                    config_menu_drawings(chosen);
232
                    break;
233
                case 2: // big vario
234
                    COSD_FLAGS ^= COSD_FLAG_BIGVARIO;
235
                    config_menu_drawings(chosen);
236
                    break;
237
                case 3: // statistics
238
                    COSD_FLAGS ^= COSD_FLAG_STATS;
239
                    config_menu_drawings(chosen);
240
                    break;
241
                case 4: // warnings
242
                    COSD_FLAGS ^= COSD_FLAG_WARNINGS;
243
                    config_menu_drawings(chosen);
244
                    break;
245
                case 5: // reset uptime
246
                    uptime = 0;
247
                    config_menu_doclick(chosen, menu);
248
                    break;
249
                case 6: // re-request OSD data
250
#if FCONLY
251
                    // request data ever 100ms from FC;
252
                    usart1_request_mk_data(0, 'd', 100);
253
#else
254
                    // request OSD Data from NC every 100ms
255
                    usart1_request_mk_data(1, 'o', 100);
256
 
257
                    // and disable debug...
258
                    usart1_request_mk_data(0, 'd', 0);
259
#endif
260
                    config_menu_doclick(chosen, menu);
261
                    break;
262
                case 7: // disable debug data
263
                    // disable sending of debug data
264
                    // may result in smoother ddata display
265
                    usart1_request_mk_data(0, 'd', 0);
266
                    config_menu_doclick(chosen, menu);
267
                    break;
268
                case 8: // save
269
                    save_eeprom();
270
                    config_menu_doclick(chosen, menu);
271
                    break;
272
                case 9: // exit
273
                    inmenu = 0;
274
                    break;
275
            }
276
            _delay_ms(250);
277
        }
278
    }
279
 
280
    // clear screen up again
281
    clear();
282
 
283
    // update flags to paint display again if needed
284
    COSD_FLAGS2 &= ~COSD_ICONS_WRITTEN;
285
 
286
    // enable interrupts again
287
    sei();
288
}
289
 
290
#endif