Subversion Repositories Projects

Rev

Rev 162 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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