1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include <stdint.h>
|
4 | #include <string.h>
|
5 |
|
6 | #define ERROR_BUFFER_ID_OVERFLOW 1
|
7 | #define ERROR_BUFFER_NUM_OVERFLOW 2
|
8 |
|
9 | #define ERROR_BUFFER_ID_SIZE 10
|
10 | #define ERROR_BUFFER_NUM_SIZE 4
|
11 |
|
12 | typedef struct
|
13 | {
|
14 | /*
|
15 | * Fehlerspeicher
|
16 | */
|
17 | uint8_t buff[ERROR_BUFFER_ID_SIZE][ERROR_BUFFER_NUM_SIZE];
|
18 |
|
19 | /*
|
20 | * Welche Art von Fehler ist aufgetreten?!
|
21 | *
|
22 | * z.B.: UART , LCD , I2C usw.
|
23 | */
|
24 | uint8_t id;
|
25 |
|
26 | /*
|
27 | * Zähler für aufgetretene Fehler
|
28 | */
|
29 | uint8_t err[ERROR_BUFFER_ID_SIZE][1];
|
30 |
|
31 | }error_t;
|
32 |
|
33 | static uint8_t checkBuff( error_t *strc )
|
34 | {
|
35 | uint8_t index;
|
36 | for( index = 0 ; index < ERROR_BUFFER_ID_SIZE ; index++ )
|
37 | {
|
38 | if( strc->buff[index][0] != 'x' )
|
39 | {
|
40 | return 1; // Im Speicher sind Daten vorhanden!
|
41 | }
|
42 | }
|
43 |
|
44 | index = 0;
|
45 | for( index = 0 ; index < ERROR_BUFFER_ID_SIZE ; index++ )
|
46 | {
|
47 | if( strc->err[index][0] > ERROR_BUFFER_NUM_SIZE )
|
48 | {
|
49 | return 2; // Kein Platz mehr im Speicher!
|
50 | }
|
51 | }
|
52 |
|
53 | if( strc->id >= ERROR_BUFFER_ID_SIZE )
|
54 | {
|
55 | return 3;
|
56 | }
|
57 |
|
58 | return 0; // Keine Daten vorhanden!
|
59 | }
|
60 |
|
61 | uint8_t errorInit( error_t *strc )
|
62 | {
|
63 | uint8_t index;
|
64 |
|
65 | for( index = 0 ; index < ERROR_BUFFER_ID_SIZE ; index++ )
|
66 | {
|
67 | memset( strc->buff + index , 'x' , ERROR_BUFFER_NUM_SIZE );
|
68 | memset( strc->err + index , 0 , ERROR_BUFFER_NUM_SIZE );
|
69 | }
|
70 |
|
71 | return 0;
|
72 | }
|
73 |
|
74 | uint8_t errorWrite( error_t *strc , uint8_t id , uint8_t err )
|
75 | {
|
76 | uint8_t ret;
|
77 |
|
78 | /*
|
79 | * Hier schlägt irgendwas fehl..
|
80 | * id wird nicht in "strc->typ" kopiert?!
|
81 | */
|
82 | strc->id = id;
|
83 |
|
84 | ret = checkBuff( (error_t*)strc );
|
85 |
|
86 | if( ret > 1 )
|
87 | {
|
88 | return ret;
|
89 | }
|
90 |
|
91 | strc->buff[id][strc->err[id][0]++] = err;
|
92 |
|
93 | return 0;
|
94 | }
|
95 |
|
96 | char *errorShowExist( error_t *strc )
|
97 | {
|
98 | uint8_t indexTyp;
|
99 | uint8_t indexErr;
|
100 | char conv[2] = "";
|
101 |
|
102 | static char out[ ERROR_BUFFER_ID_SIZE * ERROR_BUFFER_NUM_SIZE] = "";
|
103 |
|
104 | for( indexTyp = 0 ; indexTyp < ERROR_BUFFER_ID_SIZE ; indexTyp++ )
|
105 | {
|
106 | if( strc->buff[indexTyp][0] != 'x' )
|
107 | {
|
108 | itoa( indexTyp , conv , 10 );
|
109 | strcat( out , conv );
|
110 | strcat( out , "#" );
|
111 |
|
112 | for( indexErr = 0 ; indexErr < strc->err[indexTyp][0] ; indexErr++ )
|
113 | {
|
114 | itoa( strc->buff[indexTyp][indexErr] , conv , 10 );
|
115 |
|
116 | strcat( out , conv );
|
117 | if ( strc->buff[indexTyp][indexErr+1] != 'x' )
|
118 | {
|
119 | strcat( out , "," );
|
120 | }
|
121 | else
|
122 | {
|
123 | strcat( out , "\r\n");
|
124 | }
|
125 | }
|
126 | }
|
127 | }
|
128 |
|
129 | return out;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | int main()
|
135 | {
|
136 | error_t err;
|
137 | errorInit( &err );
|
138 |
|
139 | uint8_t i = 0;
|
140 |
|
141 | for( ; i < 30 ; i++ )
|
142 | {
|
143 | printf( "%d" , errorWrite( &err , i , i ));
|
144 | }
|
145 |
|
146 | return 0 ;
|
147 | }
|