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 | |||
314 | KeyOz | 35 | QString ToolBox::get_Float(long Wert, int Count) |
227 | KeyOz | 36 | { |
314 | KeyOz | 37 | QString Temp, s_Wert; |
227 | KeyOz | 38 | |
314 | KeyOz | 39 | s_Wert = QString("%1").arg(Wert); |
40 | |||
41 | Temp = s_Wert.left(s_Wert.length() - Count) + QString(".") + s_Wert.right(Count); |
||
42 | |||
43 | /* |
||
227 | KeyOz | 44 | if (Wert > 0) |
45 | Temp = ""; |
||
46 | else |
||
47 | Temp = "-"; |
||
48 | |||
49 | Temp = Temp + QString("%1").arg(Wert / Count) + "." + QString("%1").arg(Wert % Count); |
||
314 | KeyOz | 50 | */ |
227 | KeyOz | 51 | return Temp; |
52 | } |
||
53 | |||
54 | |||
55 | // Base64 Decoder |
||
56 | bool ToolBox::Decode64(sRxData &RX, bool Long) |
||
57 | { |
||
58 | unsigned char a,b,c,d; |
||
59 | unsigned char ptr = 0; |
||
60 | unsigned char x,y,z; |
||
61 | int ptrOut[150]; |
||
62 | |||
63 | int ptrIn = 3; |
||
64 | int max = RX.String.length(); |
||
65 | int len = RX.String.length(); |
||
66 | int DecLen = 0; |
||
67 | |||
68 | if (RX.Input[ptrIn] == 0) |
||
69 | { |
||
358 | KeyOz | 70 | // qDebug("QString to Char ERROR...!!!!"); |
227 | KeyOz | 71 | return false; |
72 | } |
||
73 | |||
74 | while(len != 0) |
||
75 | { |
||
76 | a = RX.Input[ptrIn++] - '='; |
||
77 | b = RX.Input[ptrIn++] - '='; |
||
78 | c = RX.Input[ptrIn++] - '='; |
||
79 | d = RX.Input[ptrIn++] - '='; |
||
80 | |||
81 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
82 | |||
83 | x = (a << 2) | (b >> 4); |
||
84 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
85 | z = ((c & 0x03) << 6) | d; |
||
86 | |||
87 | if(len--) ptrOut[ptr++] = x; else break; |
||
88 | if(len--) ptrOut[ptr++] = y; else break; |
||
89 | if(len--) ptrOut[ptr++] = z; else break; |
||
90 | } |
||
91 | |||
92 | for (int a=0; a<ptr; a++) |
||
93 | { |
||
94 | if (Long == false) |
||
95 | { |
||
96 | int b1, b2, b3; |
||
97 | |||
98 | b1 = ptrOut[a++]; |
||
99 | b2 = ptrOut[a]; |
||
100 | |||
101 | b3 = (b2 << 8) | b1; |
||
102 | |||
103 | if (b3 > 32767) |
||
104 | b3 = b3 - 65536; |
||
105 | |||
106 | RX.Decode[DecLen] = b3; |
||
107 | DecLen++; |
||
108 | } |
||
109 | else |
||
110 | { |
||
111 | RX.Decode[DecLen] = ptrOut[a]; |
||
112 | DecLen++; |
||
113 | } |
||
114 | |||
115 | RX.DecLen = DecLen; |
||
116 | } |
||
117 | return true; |
||
118 | } |
||
119 | |||
120 | // Base64 Encoder |
||
121 | QString ToolBox::Encode64(char Data[150],unsigned int Length) |
||
122 | { |
||
123 | unsigned int pt = 0; |
||
124 | unsigned char a,b,c; |
||
125 | unsigned char ptr = 0; |
||
126 | |||
127 | char TX_Buff[150]; |
||
128 | |||
129 | while(Length > 0) |
||
130 | { |
||
131 | if(Length) { a = Data[ptr++]; Length--;} else a = 0; |
||
132 | if(Length) { b = Data[ptr++]; Length--;} else b = 0; |
||
133 | if(Length) { c = Data[ptr++]; Length--;} else c = 0; |
||
134 | |||
135 | TX_Buff[pt++] = '=' + (a >> 2); |
||
136 | TX_Buff[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
137 | TX_Buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
138 | TX_Buff[pt++] = '=' + ( c & 0x3f); |
||
139 | } |
||
140 | TX_Buff[pt] = 0; |
||
141 | |||
142 | return QString(TX_Buff); |
||
143 | } |
||
144 | |||
307 | KeyOz | 145 | // Datensatz nach 8bit Integer |
146 | int ToolBox::Data2Char(int *Data , int Start, bool is_signed) |
||
147 | { |
||
148 | int Out = (Data[Start]); |
||
149 | |||
150 | if ((Out > 128) && (is_signed)) |
||
151 | Out = Out - 256; |
||
152 | |||
153 | return Out; |
||
154 | |||
155 | } |
||
156 | |||
157 | // Datensatz nach 8bit Integer |
||
158 | int ToolBox::Char2Data(int Data) |
||
159 | { |
||
160 | if (Data < 0) |
||
161 | { |
||
162 | return Data + 256; |
||
163 | } |
||
164 | return Data; |
||
165 | } |
||
166 | |||
167 | |||
227 | KeyOz | 168 | // Datensatz nach 16bit Integer |
169 | int ToolBox::Data2Int(int *Data , int Start, bool is_signed) |
||
170 | { |
||
171 | int Out = (Data[Start+1]<<8) | (Data[Start+0]); |
||
172 | |||
173 | if ((Out > 32767) && (is_signed)) |
||
174 | Out = Out - 65536; |
||
175 | |||
176 | return Out; |
||
177 | |||
178 | } |
||
179 | |||
180 | // Datensatz nach 32bit Long |
||
181 | long ToolBox::Data2Long(int *Data , int Start, bool is_signed) |
||
182 | { |
||
183 | long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]); |
||
184 | |||
185 | if ((Out > 32767) && (is_signed)) |
||
186 | Out = Out; |
||
187 | |||
188 | return Out; |
||
189 | } |
||
190 | |||
191 | // Datensatz nach QString |
||
192 | QString ToolBox::Data2QString(int Data[150], int Start, int End) |
||
193 | { |
||
194 | char String[150]; |
||
195 | for (int a = Start; a < End; a++) |
||
196 | { |
||
197 | String[a - Start] = Data[a]; |
||
198 | } |
||
199 | String[End - Start] = '\0'; |
||
200 | |||
201 | return QString(String); |
||
202 | } |
||
203 | |||
204 | // Datensatz-CRC prüfen |
||
205 | bool ToolBox::check_CRC(QString RXString) |
||
206 | { |
||
207 | int CRC = 0; |
||
208 | char *RX; |
||
209 | |||
210 | int Length = RXString.length(); |
||
211 | |||
212 | RX = RXString.toLatin1().data(); |
||
213 | |||
214 | if (RX[1] == 127) |
||
215 | { |
||
216 | RX[1] = 0; |
||
217 | } |
||
218 | |||
219 | for(int i=0; i < Length-2; i++) |
||
220 | { |
||
221 | CRC+=RX[i]; |
||
222 | } |
||
223 | |||
224 | CRC = CRC % 4096; |
||
225 | |||
226 | if(RX[Length - 2] != ('=' + (CRC / 64))) |
||
227 | { |
||
228 | return false; |
||
229 | } |
||
230 | |||
231 | if(RX[Length - 1] != ('=' + CRC % 64)) |
||
232 | { |
||
233 | return false; |
||
234 | } |
||
235 | |||
236 | return true; |
||
237 | } |
||
238 | |||
239 | // Datensatz-CRC hinzufügen |
||
240 | QString ToolBox::add_CRC(QString TXString) |
||
241 | { |
||
242 | unsigned int tmpCRC = 0; |
||
243 | |||
244 | char *TXBuff; |
||
245 | char CRC[2]; |
||
246 | |||
247 | TXBuff = TXString.toLatin1().data(); |
||
248 | |||
249 | for(int i = 0; i < TXString.length(); i++) |
||
250 | { |
||
251 | tmpCRC += TXBuff[i]; |
||
252 | } |
||
253 | |||
254 | tmpCRC %= 4096; |
||
255 | |||
256 | CRC[0] = '=' + tmpCRC / 64; |
||
257 | CRC[1] = '=' + tmpCRC % 64; |
||
258 | CRC[2] = '\0'; |
||
259 | |||
260 | QString Return = TXString + QString(CRC); |
||
261 | |||
262 | return Return; |
||
263 | } |
||
264 | |||
265 | // Alle Icons |
||
266 | QIcon ToolBox::Icon(int ID) |
||
267 | { |
||
268 | QIcon Icons[5] ; |
||
269 | Icons[0].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledred.png")), QIcon::Normal, QIcon::Off); |
||
270 | Icons[1].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledyellow.png")), QIcon::Normal, QIcon::Off); |
||
271 | Icons[3].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledyellow.png")), QIcon::Normal, QIcon::Off); |
||
272 | Icons[4].addPixmap(QPixmap(QString::fromUtf8(":/LED/Images/16X16/ledoff.png")), QIcon::Normal, QIcon::Off); |
||
273 | return Icons[ID]; |
||
274 | } |