Skip to content

Commit e313917

Browse files
committed
Add RSSI Example
1 parent da0a4a8 commit e313917

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

examples/RSSIScan/RSSIScan.ino

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/***********************************
2+
* Ant RSSI Scan Example
3+
*
4+
* Opens a channel in rx scan mode
5+
* then reports all found devices
6+
* and their RSSI values
7+
*
8+
* Author Curtis Malainey
9+
************************************/
10+
11+
#include "ANT.h"
12+
#define BAUD_RATE 9600
13+
Ant ant = Ant();
14+
15+
const uint8_t NETWORK_KEY[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
16+
17+
void parseMessage();
18+
void parseEventMessage(uint8_t code);
19+
20+
void setup()
21+
{
22+
AssignChannel ac;
23+
ResetSystem rs;
24+
SetNetworkKey snk;
25+
ChannelId ci;
26+
ChannelRfFrequency crf;
27+
LibConfig lb;
28+
OpenRxScanMode osm;
29+
30+
Serial1.begin(BAUD_RATE);
31+
ant.setSerial(Serial1);
32+
ant.send(rs);
33+
// Delay after resetting the radio to give the user time to connect on serial
34+
delay(10000);
35+
Serial.begin(9600);
36+
Serial.println("Running");
37+
parseMessage();
38+
39+
snk.setNetwork(0);
40+
snk.setKey((uint8_t*)NETWORK_KEY);
41+
ant.send(snk);
42+
parseMessage();
43+
44+
ac.setChannel(0);
45+
ac.setChannelType(CHANNEL_TYPE_BIDIRECTIONAL_RECEIVE); //can't wildcard this
46+
ac.setChannelNetwork(0);
47+
ant.send(ac);
48+
parseMessage();
49+
50+
ci.setChannel(0);
51+
ci.setDeviceNumber(0);
52+
ci.setDeviceType(0);
53+
ci.setTransmissionType(0);
54+
ant.send(ci);
55+
parseMessage();
56+
57+
lb.setConfig(LIB_CONFIG_RSSI | LIB_CONFIG_CHANNEL_ID);
58+
ant.send(lb);
59+
parseMessage();
60+
61+
crf.setChannel(0);
62+
crf.setRfFrequency(0); //can't wildcard this
63+
ant.send(crf);
64+
parseMessage();
65+
66+
ant.send(osm);
67+
parseMessage();
68+
}
69+
70+
void loop()
71+
{
72+
parseMessage();
73+
}
74+
75+
void parseMessage() {
76+
ant.readPacket();
77+
if(ant.getResponse().isAvailable())
78+
{
79+
uint8_t msgId = ant.getResponse().getMsgId();
80+
switch (msgId) {
81+
case CHANNEL_EVENT:
82+
{
83+
ChannelEventResponse cer;
84+
ant.getResponse().getChannelEventResponseMsg(cer);
85+
Serial.println("Received Msg: ChannelEventResponse");
86+
Serial.print("Channel: ");
87+
Serial.println(cer.getChannelNumber());
88+
parseEventMessage(cer.getCode());
89+
break;
90+
}
91+
92+
case START_UP_MESSAGE:
93+
{
94+
StartUpMessage sum;
95+
ant.getResponse().getStartUpMsg(sum);
96+
Serial.println("Received Msg: StartupMessage");
97+
Serial.print("Message: ");
98+
Serial.println(sum.getMessage());
99+
break;
100+
}
101+
102+
case BROADCAST_DATA:
103+
{
104+
BroadcastData bd;
105+
ant.getResponse().getBroadcastDataMsg(bd);
106+
if ((bd.getFlagByte() & LIB_CONFIG_RSSI) && (bd.getFlagByte() & LIB_CONFIG_CHANNEL_ID)) {
107+
Serial.print("Device Number: ");
108+
Serial.print(bd.getDeviceNumber());
109+
Serial.print(", Device Type: ");
110+
Serial.print(bd.getDeviceType());
111+
Serial.print(", RSSI: ");
112+
Serial.println(bd.getRSSIValue());
113+
}
114+
break;
115+
}
116+
117+
case ACKNOWLEDGED_DATA:
118+
{
119+
AcknowledgedData ad;
120+
ant.getResponse().getAcknowledgedDataMsg(ad);
121+
if ((ad.getFlagByte() & LIB_CONFIG_CHANNEL_ID) && (ad.getFlagByte() & LIB_CONFIG_RSSI)) {
122+
Serial.print("Device Number: ");
123+
Serial.print(ad.getDeviceNumber());
124+
Serial.print(", Device Type: ");
125+
Serial.print(ad.getDeviceType());
126+
Serial.print(", RSSI: ");
127+
Serial.println(ad.getRSSIValue());
128+
}
129+
}
130+
131+
default:
132+
Serial.print("Undefined Message: ");
133+
Serial.println(msgId, HEX);
134+
break;
135+
}
136+
}
137+
else if (ant.getResponse().isError())
138+
{
139+
Serial.print("ANT MSG ERROR: ");
140+
Serial.println(ant.getResponse().getErrorCode());
141+
}
142+
}
143+
144+
void parseEventMessage(uint8_t code)
145+
{
146+
BroadcastDataMsg bm;
147+
Serial.print("Code: ");
148+
switch (code)
149+
{
150+
case STATUS_RESPONSE_NO_ERROR:
151+
Serial.println("RESPONSE_NO_ERROR");
152+
break;
153+
154+
case STATUS_EVENT_CHANNEL_CLOSED:
155+
Serial.println("EVENT_CHANNEL_CLOSED");
156+
break;
157+
158+
default:
159+
Serial.println(code);
160+
break;
161+
}
162+
}

0 commit comments

Comments
 (0)