Rev 227 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 227 | Rev 307 | ||
---|---|---|---|
1 | /*************************************************************************** |
1 | /*************************************************************************** |
2 | * Copyright (C) 2008 by Manuel Schrape * |
2 | * Copyright (C) 2008 by Manuel Schrape * |
3 | * manuel.schrape@gmx.de * |
3 | * manuel.schrape@gmx.de * |
4 | * * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License. * |
7 | * the Free Software Foundation; either version 2 of the License. * |
8 | * * |
8 | * * |
9 | * This program is distributed in the hope that it will be useful, * |
9 | * This program is distributed in the hope that it will be useful, * |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
12 | * GNU General Public License for more details. * |
12 | * GNU General Public License for more details. * |
13 | * * |
13 | * * |
14 | * You should have received a copy of the GNU General Public License * |
14 | * You should have received a copy of the GNU General Public License * |
15 | * along with this program; if not, write to the * |
15 | * along with this program; if not, write to the * |
16 | * Free Software Foundation, Inc., * |
16 | * Free Software Foundation, Inc., * |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
18 | ***************************************************************************/ |
18 | ***************************************************************************/ |
19 | 19 | ||
20 | #include "ToolBox.h" |
20 | #include "ToolBox.h" |
21 | 21 | ||
22 | ToolBox::ToolBox() |
22 | ToolBox::ToolBox() |
23 | { |
23 | { |
24 | } |
24 | } |
25 | 25 | ||
26 | void ToolBox::Wait(int Time) |
26 | void ToolBox::Wait(int Time) |
27 | { |
27 | { |
28 | #ifndef _WIN32_ |
28 | #ifndef _WIN32_ |
29 | usleep(Time); |
29 | usleep(Time); |
30 | #else |
30 | #else |
31 | // sleep(Time); |
31 | // sleep(Time); |
32 | #endif |
32 | #endif |
33 | } |
33 | } |
34 | 34 | ||
35 | QString ToolBox::get_Float(long Wert, long Count) |
35 | QString ToolBox::get_Float(long Wert, long Count) |
36 | { |
36 | { |
37 | QString Temp; |
37 | QString Temp; |
38 | 38 | ||
39 | if (Wert > 0) |
39 | if (Wert > 0) |
40 | Temp = ""; |
40 | Temp = ""; |
41 | else |
41 | else |
42 | Temp = "-"; |
42 | Temp = "-"; |
43 | 43 | ||
44 | Temp = Temp + QString("%1").arg(Wert / Count) + "." + QString("%1").arg(Wert % Count); |
44 | Temp = Temp + QString("%1").arg(Wert / Count) + "." + QString("%1").arg(Wert % Count); |
45 | 45 | ||
46 | return Temp; |
46 | return Temp; |
47 | } |
47 | } |
48 | 48 | ||
49 | 49 | ||
50 | // Base64 Decoder |
50 | // Base64 Decoder |
51 | bool ToolBox::Decode64(sRxData &RX, bool Long) |
51 | bool ToolBox::Decode64(sRxData &RX, bool Long) |
52 | { |
52 | { |
53 | unsigned char a,b,c,d; |
53 | unsigned char a,b,c,d; |
54 | unsigned char ptr = 0; |
54 | unsigned char ptr = 0; |
55 | unsigned char x,y,z; |
55 | unsigned char x,y,z; |
56 | int ptrOut[150]; |
56 | int ptrOut[150]; |
57 | 57 | ||
58 | int ptrIn = 3; |
58 | int ptrIn = 3; |
59 | int max = RX.String.length(); |
59 | int max = RX.String.length(); |
60 | int len = RX.String.length(); |
60 | int len = RX.String.length(); |
61 | int DecLen = 0; |
61 | int DecLen = 0; |
62 | 62 | ||
63 | if (RX.Input[ptrIn] == 0) |
63 | if (RX.Input[ptrIn] == 0) |
64 | { |
64 | { |
65 | qDebug("QString to Char ERROR...!!!!"); |
65 | qDebug("QString to Char ERROR...!!!!"); |
66 | return false; |
66 | return false; |
67 | } |
67 | } |
68 | 68 | ||
69 | while(len != 0) |
69 | while(len != 0) |
70 | { |
70 | { |
71 | a = RX.Input[ptrIn++] - '='; |
71 | a = RX.Input[ptrIn++] - '='; |
72 | b = RX.Input[ptrIn++] - '='; |
72 | b = RX.Input[ptrIn++] - '='; |
73 | c = RX.Input[ptrIn++] - '='; |
73 | c = RX.Input[ptrIn++] - '='; |
74 | d = RX.Input[ptrIn++] - '='; |
74 | d = RX.Input[ptrIn++] - '='; |
75 | 75 | ||
76 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
76 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
77 | 77 | ||
78 | x = (a << 2) | (b >> 4); |
78 | x = (a << 2) | (b >> 4); |
79 | y = ((b & 0x0f) << 4) | (c >> 2); |
79 | y = ((b & 0x0f) << 4) | (c >> 2); |
80 | z = ((c & 0x03) << 6) | d; |
80 | z = ((c & 0x03) << 6) | d; |
81 | 81 | ||
82 | if(len--) ptrOut[ptr++] = x; else break; |
82 | if(len--) ptrOut[ptr++] = x; else break; |
83 | if(len--) ptrOut[ptr++] = y; else break; |
83 | if(len--) ptrOut[ptr++] = y; else break; |
84 | if(len--) ptrOut[ptr++] = z; else break; |
84 | if(len--) ptrOut[ptr++] = z; else break; |
85 | } |
85 | } |
86 | 86 | ||
87 | for (int a=0; a<ptr; a++) |
87 | for (int a=0; a<ptr; a++) |
88 | { |
88 | { |
89 | if (Long == false) |
89 | if (Long == false) |
90 | { |
90 | { |
91 | int b1, b2, b3; |
91 | int b1, b2, b3; |
92 | 92 | ||
93 | b1 = ptrOut[a++]; |
93 | b1 = ptrOut[a++]; |
94 | b2 = ptrOut[a]; |
94 | b2 = ptrOut[a]; |
95 | 95 | ||
96 | b3 = (b2 << 8) | b1; |
96 | b3 = (b2 << 8) | b1; |
97 | 97 | ||
98 | if (b3 > 32767) |
98 | if (b3 > 32767) |
99 | b3 = b3 - 65536; |
99 | b3 = b3 - 65536; |
100 | 100 | ||
101 | RX.Decode[DecLen] = b3; |
101 | RX.Decode[DecLen] = b3; |
102 | DecLen++; |
102 | DecLen++; |
103 | } |
103 | } |
104 | else |
104 | else |
105 | { |
105 | { |
106 | RX.Decode[DecLen] = ptrOut[a]; |
106 | RX.Decode[DecLen] = ptrOut[a]; |
107 | DecLen++; |
107 | DecLen++; |
108 | } |
108 | } |
109 | 109 | ||
110 | RX.DecLen = DecLen; |
110 | RX.DecLen = DecLen; |
111 | } |
111 | } |
112 | return true; |
112 | return true; |
113 | } |
113 | } |
114 | 114 | ||
115 | // Base64 Encoder |
115 | // Base64 Encoder |
116 | QString ToolBox::Encode64(char Data[150],unsigned int Length) |
116 | QString ToolBox::Encode64(char Data[150],unsigned int Length) |
117 | { |
117 | { |
118 | unsigned int pt = 0; |
118 | unsigned int pt = 0; |
119 | unsigned char a,b,c; |
119 | unsigned char a,b,c; |
120 | unsigned char ptr = 0; |
120 | unsigned char ptr = 0; |
121 | 121 | ||
122 | char TX_Buff[150]; |
122 | char TX_Buff[150]; |
123 | 123 | ||
124 | while(Length > 0) |
124 | while(Length > 0) |
125 | { |
125 | { |
126 | if(Length) { a = Data[ptr++]; Length--;} else a = 0; |
126 | if(Length) { a = Data[ptr++]; Length--;} else a = 0; |
127 | if(Length) { b = Data[ptr++]; Length--;} else b = 0; |
127 | if(Length) { b = Data[ptr++]; Length--;} else b = 0; |
128 | if(Length) { c = Data[ptr++]; Length--;} else c = 0; |
128 | if(Length) { c = Data[ptr++]; Length--;} else c = 0; |
129 | 129 | ||
130 | TX_Buff[pt++] = '=' + (a >> 2); |
130 | TX_Buff[pt++] = '=' + (a >> 2); |
131 | TX_Buff[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
131 | TX_Buff[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
132 | TX_Buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
132 | TX_Buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
133 | TX_Buff[pt++] = '=' + ( c & 0x3f); |
133 | TX_Buff[pt++] = '=' + ( c & 0x3f); |
134 | } |
134 | } |
135 | TX_Buff[pt] = 0; |
135 | TX_Buff[pt] = 0; |
136 | 136 | ||
137 | return QString(TX_Buff); |
137 | return QString(TX_Buff); |
138 | } |
138 | } |
- | 139 | ||
- | 140 | // Datensatz nach 8bit Integer |
|
- | 141 | int ToolBox::Data2Char(int *Data , int Start, bool is_signed) |
|
- | 142 | { |
|
- | 143 | int Out = (Data[Start]); |
|
- | 144 | ||
- | 145 | if ((Out > 128) && (is_signed)) |
|
- | 146 | Out = Out - 256; |
|
- | 147 | ||
- | 148 | return Out; |
|
- | 149 | ||
- | 150 | } |
|
- | 151 | ||
- | 152 | // Datensatz nach 8bit Integer |
|
- | 153 | int ToolBox::Char2Data(int Data) |
|
- | 154 | { |
|
- | 155 | if (Data < 0) |
|
- | 156 | { |
|
- | 157 | return Data + 256; |
|
- | 158 | } |
|
- | 159 | return Data; |
|
- | 160 | } |
|
- | 161 | ||
139 | 162 | ||
140 | // Datensatz nach 16bit Integer |
163 | // Datensatz nach 16bit Integer |
141 | int ToolBox::Data2Int(int *Data , int Start, bool is_signed) |
164 | int ToolBox::Data2Int(int *Data , int Start, bool is_signed) |
142 | { |
165 | { |
143 | int Out = (Data[Start+1]<<8) | (Data[Start+0]); |
166 | int Out = (Data[Start+1]<<8) | (Data[Start+0]); |
144 | 167 | ||
145 | if ((Out > 32767) && (is_signed)) |
168 | if ((Out > 32767) && (is_signed)) |
146 | Out = Out - 65536; |
169 | Out = Out - 65536; |
147 | 170 | ||
148 | return Out; |
171 | return Out; |
149 | 172 | ||
150 | } |
173 | } |
151 | 174 | ||
152 | // Datensatz nach 32bit Long |
175 | // Datensatz nach 32bit Long |
153 | long ToolBox::Data2Long(int *Data , int Start, bool is_signed) |
176 | long ToolBox::Data2Long(int *Data , int Start, bool is_signed) |
154 | { |
177 | { |
155 | long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]); |
178 | long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]); |
156 | 179 | ||
157 | if ((Out > 32767) && (is_signed)) |
180 | if ((Out > 32767) && (is_signed)) |
158 | Out = Out; |
181 | Out = Out; |
159 | 182 | ||
160 | return Out; |
183 | return Out; |
161 | } |
184 | } |
162 | 185 | ||
163 | // Datensatz nach QString |
186 | // Datensatz nach QString |
164 | QString ToolBox::Data2QString(int Data[150], int Start, int End) |
187 | QString ToolBox::Data2QString(int Data[150], int Start, int End) |
165 | { |
188 | { |
166 | char String[150]; |
189 | char String[150]; |
167 | for (int a = Start; a < End; a++) |
190 | for (int a = Start; a < End; a++) |
168 | { |
191 | { |
169 | String[a - Start] = Data[a]; |
192 | String[a - Start] = Data[a]; |
170 | } |
193 | } |
171 | String[End - Start] = '\0'; |
194 | String[End - Start] = '\0'; |
172 | 195 | ||
173 | return QString(String); |
196 | return QString(String); |
174 | } |
197 | } |
175 | 198 | ||
176 | // Datensatz-CRC prüfen |
199 | // Datensatz-CRC prüfen |
177 | bool ToolBox::check_CRC(QString RXString) |
200 | bool ToolBox::check_CRC(QString RXString) |
178 | { |
201 | { |
179 | int CRC = 0; |
202 | int CRC = 0; |
180 | char *RX; |
203 | char *RX; |
181 | 204 | ||
182 | int Length = RXString.length(); |
205 | int Length = RXString.length(); |
183 | 206 | ||
184 | RX = RXString.toLatin1().data(); |
207 | RX = RXString.toLatin1().data(); |
185 | 208 | ||
186 | if (RX[1] == 127) |
209 | if (RX[1] == 127) |
187 | { |
210 | { |
188 | RX[1] = 0; |
211 | RX[1] = 0; |
189 | } |
212 | } |
190 | 213 | ||
191 | for(int i=0; i < Length-2; i++) |
214 | for(int i=0; i < Length-2; i++) |
192 | { |
215 | { |
193 | CRC+=RX[i]; |
216 | CRC+=RX[i]; |
194 | } |
217 | } |
195 | 218 | ||
196 | CRC = CRC % 4096; |
219 | CRC = CRC % 4096; |
197 | 220 | ||
198 | if(RX[Length - 2] != ('=' + (CRC / 64))) |
221 | if(RX[Length - 2] != ('=' + (CRC / 64))) |
199 | { |
222 | { |
200 | return false; |
223 | return false; |
201 | } |
224 | } |
202 | 225 | ||
203 | if(RX[Length - 1] != ('=' + CRC % 64)) |
226 | if(RX[Length - 1] != ('=' + CRC % 64)) |
204 | { |
227 | { |
205 | return false; |
228 | return false; |
206 | } |
229 | } |
207 | 230 | ||
208 | return true; |
231 | return true; |
209 | } |
232 | } |
210 | 233 | ||
211 | // Datensatz-CRC hinzufügen |
234 | // Datensatz-CRC hinzufügen |
212 | QString ToolBox::add_CRC(QString TXString) |
235 | QString ToolBox::add_CRC(QString TXString) |
213 | { |
236 | { |
214 | unsigned int tmpCRC = 0; |
237 | unsigned int tmpCRC = 0; |
215 | 238 | ||
216 | char *TXBuff; |
239 | char *TXBuff; |
217 | char CRC[2]; |
240 | char CRC[2]; |
218 | 241 | ||
219 | TXBuff = TXString.toLatin1().data(); |
242 | TXBuff = TXString.toLatin1().data(); |
220 | 243 | ||
221 | for(int i = 0; i < TXString.length(); i++) |
244 | for(int i = 0; i < TXString.length(); i++) |
222 | { |
245 | { |
223 | tmpCRC += TXBuff[i]; |
246 | tmpCRC += TXBuff[i]; |
224 | } |
247 | } |
225 | 248 | ||
226 | tmpCRC %= 4096; |
249 | tmpCRC %= 4096; |
227 | 250 | ||
228 | CRC[0] = '=' + tmpCRC / 64; |
251 | CRC[0] = '=' + tmpCRC / 64; |
229 | CRC[1] = '=' + tmpCRC % 64; |
252 | CRC[1] = '=' + tmpCRC % 64; |
230 | CRC[2] = '\0'; |
253 | CRC[2] = '\0'; |
231 | 254 | ||
232 | QString Return = TXString + QString(CRC); |
255 | QString Return = TXString + QString(CRC); |
233 | 256 | ||
234 | return Return; |
257 | return Return; |
235 | } |
258 | } |
236 | 259 | ||
237 | // Alle Icons |
260 | // Alle Icons |
238 | QIcon ToolBox::Icon(int ID) |
261 | QIcon ToolBox::Icon(int ID) |
239 | { |
262 | { |
240 | QIcon Icons[5] ; |
263 | QIcon Icons[5] ; |
241 | Icons[0].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledred.png")), QIcon::Normal, QIcon::Off); |
264 | Icons[0].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledred.png")), QIcon::Normal, QIcon::Off); |
242 | Icons[1].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledyellow.png")), QIcon::Normal, QIcon::Off); |
265 | Icons[1].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledyellow.png")), QIcon::Normal, QIcon::Off); |
243 | Icons[3].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledyellow.png")), QIcon::Normal, QIcon::Off); |
266 | Icons[3].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledyellow.png")), QIcon::Normal, QIcon::Off); |
244 | Icons[4].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledoff.png")), QIcon::Normal, QIcon::Off); |
267 | Icons[4].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledoff.png")), QIcon::Normal, QIcon::Off); |
245 | return Icons[ID]; |
268 | return Icons[ID]; |
246 | } |
269 | } |
247 | 270 |