Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
112 mikeljo 1
// debug Data
2
 
3
#include <avr/io.h>
4
#include <inttypes.h>
5
#include <stdlib.h>
6
#include <avr/pgmspace.h>
7
 
8
#include "main.h"
9
#include "lcd.h"
10
#include "rs232.h"
11
#include "base64.h"
12
#include "parameter.h"
13
#include "menu.h"
14
#include "debug.h"
15
 
16
uint8_t r_buffer[129];                                                                                          // Dieser Puffer enthält die Rohdaten (kodiert)
17
 
18
uint8_t base64_decode_debug(unsigned char *ptrOut, uint8_t number)                                                              // Wandelt die base64-Rohdaten in lesbare Daten um
19
{
20
        uint8_t p,q;
21
        uint8_t a,b,c,d;
22
 
23
        p = 2;
24
        q = 0;
25
 
26
        while (p < number)
27
        {
28
                a = r_buffer[p + 0] - 61;
29
                b = r_buffer[p + 1] - 61;
30
                c = r_buffer[p + 2] - 61;
31
                d = r_buffer[p + 3] - 61;
32
 
33
                p += 4;
34
 
35
                ptrOut[q + 0] = (a << 2) | (b >> 4);                                    // gespeichert werden die Daten in ptrOut
36
                ptrOut[q + 1] = ((b & 0x0F) << 4) | (c >> 2);
37
                ptrOut[q + 2] = ((c & 0x03) << 6) | d;
38
 
39
                q += 3;
40
        }
41
        return q;                                                                                                               // Rückgabe der Anzahl der Datenbytes
42
}
43
 
44
uint8_t get_message_debug(uint8_t command)                                                                                              // Liest eine komplette Übertragung und dekodiert sie
45
{
46
        uint8_t index, comm;
47
 
48
        timer = 20;                                                                                                             // Timer für Timeout
49
 
50
        while ((RS232_get() != '#') && (timer > 0));                                    // Warten auf Start-Zeichen #
51
 
52
        if (timer > 0)                                                                                                  // Falls kein Timeout auftrat
53
        {
54
                index = 0;                                                                                                      // Die Rohdaten in r_buffer einlesen
55
                do
56
                {
57
                        comm = RS232_get();
58
                        r_buffer[index++] = comm;
59
 
60
                        if (index > 127)                                                                                // Schutz vor Puffer-Überlauf
61
                                index = 127;
62
                }
63
                while (comm != 0x0D);                                                                           // ... bis End-Signal = 0x0D kommt...
64
                if (command == 34)
65
                        base64_decode_debug((unsigned char *) &DebugIn,index);          // Die base64-kodierten Rohdaten umwandeln
66
                else
67
                        base64_decode_debug((unsigned char *) &DebugInText[command][0], index);                                                                         // Die base64-kodierten Rohdaten umwandeln
68
                        DebugInText[command][15] = 0x00;                                // Ende Zeichen setzen!!
69
                return 0;                       // kein Fehler aufgetreten
70
        }
71
        else
72
        {
73
                return 1;                       // Fehler aufgetreten
74
        }
75
}
76
 
77
uint8_t read_debug(uint8_t command_read)                        // 
78
{
79
        uint8_t timeout;
80
        char compare;
81
 
82
        timeout = 0;
83
 
84
        p_buffer[0] = '#';                                                      // Debug-Daten anfordern
85
        p_buffer[1] = 'a';
86
 
87
        if (command_read == 34)
88
        {
89
                p_buffer[2] = 'c';
90
                p_buffer[3] = 0;
91
        }
92
        if (command_read < 32)
93
        {
94
                p_buffer[2] = 'a';
95
                p_buffer[3] = command_read;
96
        }
97
 
98
        p_buffer[4] = 0;
99
        p_buffer[5] = 0;
100
        base64_send(6);
101
 
102
        if (command_read == 34)
103
                compare = 'D';
104
        else
105
                compare = 'A';
106
 
107
        do                                                                                      // warten, bis die Parameter gesendet werden
108
        {
109
                if (get_message_debug(command_read) == 1)
110
                        timeout = 30;
111
                timeout ++;
112
        }
113
        while (((r_buffer[1] < compare) || (r_buffer[1] > compare)) && (timeout < 30)); // "!=" funktioniert witzigerweise ned
114
 
115
        if (timeout >= 30)
116
                return 1;
117
        else
118
                return 0;
119
}
120
 
121
#define isminus 65536 / 2
122
 
123
void decimal_int (unsigned int data, uint8_t *text)                     // wandelt Wert in rechtsbündigen Text um 
124
{              
125
        int sign = 0;
126
        text[0] = 0x20;                                                                                 // (schneller/kleiner als printf())
127
        if (data > isminus)
128
        {
129
                data = 65536 - data;
130
                sign = 1;
131
                text[0] = '-';
132
        }
133
 
134
        text[1] = data/10000;
135
        data -= (text[1] * 10000);
136
        text[2] = data/1000;
137
        data -= (text[2] *1000);
138
 
139
        text[3] = data/100;
140
        data -= (text[3] * 100);
141
        text[4] = data/10;
142
        data -= (text[4] *10);
143
 
144
 
145
        text[5] = data + 0x30;
146
        text[1] += 0x30;
147
        text[2] += 0x30;
148
        text[3] += 0x30;
149
        text[4] += 0x30;
150
 
151
 
152
        if (text[1] == 0x30)
153
        {
154
                text[0] = 0x20;
155
                if (sign == 1) text[1] = '-'; else text[1] = 0x20;
156
                if (text[2] == 0x30)
157
                {
158
                        text[1] = 0x20;
159
                        if (sign == 1) text[2] = '-'; else text[2] = 0x20;
160
                        if (text[3] == 0x30)
161
                        {
162
                                text[2] = 0x20;
163
                                if (sign == 1) text[3] = '-'; else text[3] = 0x20;
164
                                if (text[4] == 0x30)
165
                                {
166
                                        text[3] = 0x20;
167
                                        if (sign == 1) text[4] = '-'; else text[4] = 0x20;
168
                                }
169
                        }
170
                }
171
        }
172
        text[6] = 0x00;
173
}
174
 
175
void display_debug(void)
176
{
177
        uint8_t zeile;
178
        uint8_t text[10];
179
        uint8_t i = 0;
180
        int page = 0;
181
        #define step 7
182
        uint8_t flag = 0;
183
 
184
        lcd_cls();
185
        zeile = 0;
186
        lcd_printp(PSTR("Debug-Display"),0);
187
 
188
        for (i=0;i<32;i++)
189
        {
190
                if (read_debug(i) == 1)
191
                        {
192
                                if (read_debug(i) == 1)         // 2mal Probieren
193
                                        flag = 1;
194
                        }
195
        }
196
 
197
        if (flag == 1)
198
        {      
199
                flag = 0;
200
                lcd_printp(PSTR("\r\nTimeout!"),0);
201
                timer = 200;
202
                while (timer > 0);
203
        }
204
        else
205
        {
206
 
207
                do
208
                {
209
                        while (key != 0x00);
210
                        if (read_debug(34) == 1) //Debugwerte
211
                        {
212
                                lcd_printp(PSTR("\r\nTimeout!"),0);
213
                                timer = 200;
214
                                while (timer > 0);
215
                                break;
216
                        }
217
                        else
218
                        {
219
 
220
                                decimal_int(page,text);
221
//                              lcd_print_atp(0,6,PSTR("Page"),0);
222
                                lcd_print_at(15,0,text,0);
223
 
224
                                for (i=0;i<step;i++)
225
                                {
226
                                        if ((i + (page * step)) < 32)
227
                                        {
228
                                                decimal_int(DebugIn.Analog[i + (page * step)],text);
229
                                                lcd_print_at(0,i+1,DebugInText[i + (page * step)],0);
230
                                                lcd_print_at(13,i+1,text,0);
231
                                        }
232
                                        else
233
                                                lcd_print_atp(0,i+1,PSTR("                    "),0);
234
                                }
235
 
236
//                              decimal_int(DebugIn.Analog[9],text);
237
//                              lcd_print_atp(0,7,PSTR("Spannung"),0);
238
//                              lcd_print_at(10,7,text,0);
239
 
240
                                timer = 10;
241
                                while(timer > 0);
242
 
243
                                if (key == 0x01)
244
                                {
245
                                        page--;
246
                                        if (page < 0) page = 0;
247
                                }
248
                                if (key == 0x02)
249
                                {
250
                                        page++;
251
                                        if (page > 7) page = 7;
252
                                }
253
                        } // end else
254
                }
255
                while (key != 0x04);    // ESC
256
        } // end else
257
}