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