-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathantmessage.cpp
124 lines (107 loc) · 3.77 KB
/
antmessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2011 Mark Liversedge ([email protected])
* Copyright (c) 2009 Mark Rages (Quarq)
* Copyright (c) 2016 Erik Botö ([email protected])
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "antmessage.h"
#include <QDebug>
ANTMessage::ANTMessage() :
length(-1)
{
}
// construct a message with all data passed (except sync and checksum)
ANTMessage::ANTMessage(const unsigned char len,
const unsigned char type,
const unsigned char b3,
const unsigned char b4,
const unsigned char b5,
const unsigned char b6,
const unsigned char b7,
const unsigned char b8,
const unsigned char b9,
const unsigned char b10,
const unsigned char b11)
{
// encode the message
data[0] = ANT_SYNC_BYTE;
data[1] = len; // message payload length
data[2] = type; // message type
data[3] = b3;
data[4] = b4;
data[5] = b5;
data[6] = b6;
data[7] = b7;
data[8] = b8;
data[9] = b9;
data[10] = b10;
data[11] = b11;
// compute the checksum and place after data
unsigned char crc = 0;
int i=0;
for (; i< (len+3); i++) crc ^= data[i];
data[i] = crc;
length = i+1;
}
ANTMessage::ANTMessage(const unsigned char * message)
{
// Verify sync
if (! (message[0] == ANT_SYNC_BYTE))
{
qDebug() << "Message with invalid sync";
}
length = message[1];
type = message[2];
memcpy(data, message, ANT_MAX_MESSAGE_SIZE);
switch(type) {
case ANT_CHANNEL_EVENT:
qDebug() << "Channel event";
break;
default:
qDebug() << "Unhandled message";
}
}
ANTMessage ANTMessage::assignChannel(const unsigned char channel,
const unsigned char type,
const unsigned char network)
{
return ANTMessage(3, ANT_ASSIGN_CHANNEL, channel, type, network);
}
ANTMessage ANTMessage::setChannelID(const unsigned char channel,
const unsigned short device,
const unsigned char devicetype,
const unsigned char txtype)
{
return ANTMessage(5, ANT_CHANNEL_ID, channel, device&0xff, (device>>8)&0xff, devicetype, txtype);
}
ANTMessage ANTMessage::setChannelPeriod(const unsigned char channel,
const unsigned short period)
{
return ANTMessage(3, ANT_CHANNEL_PERIOD, channel, period&0xff, (period>>8)&0xff);
}
ANTMessage ANTMessage::setChannelFreq(const unsigned char channel,
const unsigned char frequency)
{
// Channel Frequency = 2400 MHz + Channel RF Frequency Number * 1.0 MHz
// The range is 0-124 with a default value of 66
// ANT_SPORT_FREQ = 57
// ANT_KICKR_FREQ = 52
return ANTMessage(2, ANT_CHANNEL_FREQUENCY, channel, frequency);
}
ANTMessage ANTMessage::open(const unsigned char channel)
{
return ANTMessage(1, ANT_OPEN_CHANNEL, channel);
}