Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
228 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
 
140
// Datensatz nach 16bit Integer
141
int ToolBox::Data2Int(int *Data , int Start, bool is_signed)
142
{
143
    int Out = (Data[Start+1]<<8) | (Data[Start+0]);
144
 
145
    if ((Out > 32767) && (is_signed))
146
      Out = Out - 65536;
147
 
148
    return Out;
149
 
150
}
151
 
152
// Datensatz nach 32bit Long
153
long ToolBox::Data2Long(int *Data , int Start, bool is_signed)
154
{
155
    long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]);
156
 
157
    if ((Out > 32767) && (is_signed))
158
      Out = Out;
159
 
160
    return Out;
161
}
162
 
163
// Datensatz nach QString
164
QString ToolBox::Data2QString(int Data[150], int Start, int End)
165
{
166
    char String[150];
167
    for (int a = Start; a < End; a++)
168
    {
169
        String[a - Start] = Data[a];
170
    }
171
    String[End - Start] = '\0';
172
 
173
    return QString(String);
174
}
175
 
176
// Datensatz-CRC prüfen
177
bool ToolBox::check_CRC(QString RXString)
178
{
179
    int CRC = 0;
180
    char *RX;
181
 
182
    int Length = RXString.length();
183
 
184
    RX = RXString.toLatin1().data();
185
 
186
    if (RX[1] == 127)
187
    {
188
        RX[1] = 0;
189
    }
190
 
191
    for(int i=0; i < Length-2; i++)
192
    {
193
            CRC+=RX[i];
194
    }
195
 
196
    CRC = CRC % 4096;
197
 
198
    if(RX[Length - 2] != ('=' + (CRC / 64)))
199
    {
200
        return false;
201
    }
202
 
203
    if(RX[Length - 1] != ('=' + CRC % 64))
204
    {
205
        return false;
206
    }
207
 
208
    return true;
209
}
210
 
211
// Datensatz-CRC hinzufügen
212
QString ToolBox::add_CRC(QString TXString)
213
{
214
    unsigned int tmpCRC = 0;
215
 
216
    char *TXBuff;
217
    char CRC[2];
218
 
219
    TXBuff = TXString.toLatin1().data();
220
 
221
    for(int i = 0; i < TXString.length(); i++)
222
    {
223
        tmpCRC += TXBuff[i];
224
    }
225
 
226
    tmpCRC %= 4096;
227
 
228
    CRC[0] = '=' + tmpCRC / 64;
229
    CRC[1] = '=' + tmpCRC % 64;
230
    CRC[2] = '\0';
231
 
232
    QString Return = TXString + QString(CRC);
233
 
234
    return Return;
235
}
236
 
237
// Alle Icons
238
QIcon ToolBox::Icon(int ID)
239
{
240
    QIcon Icons[5] ;
241
    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);
243
    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);
245
    return Icons[ID];
246
}