Skip to content

Commit

Permalink
Merge pull request #12 from ammaraskar/master
Browse files Browse the repository at this point in the history
Add method to acknowledge answers
  • Loading branch information
wizard97 authored Jun 6, 2019
2 parents c2fd2f9 + 09a9230 commit edf230f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
16 changes: 11 additions & 5 deletions emulator/iSkipper/examples/capture/capture.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include <string.h>

/* UPDATE THESE FOR YOUR PARTICULAR BOARD */
#define IS_RFM69HW false //make true if using w version
#define IS_RFM69HW true //make true if using w version
#define IRQ_PIN 3 // This is 3 on adafruit feather
#define CSN 8 // This is 8 on adafruit feather
/* END THINGS YOU MUST UPDATE */


#define MAX_BUFFERED_PACKETS 20
#define SEND_ACKS true

iClickerEmulator clicker(CSN, IRQ_PIN, digitalPinToInterrupt(IRQ_PIN), IS_RFM69HW);
RingBufCPP<iClickerPacket, MAX_BUFFERED_PACKETS> recvBuf;
Expand All @@ -29,23 +30,28 @@ void setup()
void loop()
{
char tmp[50];
iClickerPacket r;
iClickerPacket r;

//see if there is a pending packet, check if its an answer packet

while (recvBuf.pull(&r)) {
while (recvBuf.pull(&r) && r.type == PACKET_ANSWER) {
uint8_t *id = r.packet.answerPacket.id;
char answer = iClickerEmulator::answerChar(r.packet.answerPacket.answer);
snprintf(tmp, sizeof(tmp), "Captured: %c (%02X, %02X, %02X, %02X) \n", answer, id[0], id[1], id[2], id[3]);
Serial.println(tmp);
}

delay(100);

}


void recvPacketHandler(iClickerPacket *recvd)
{
if (SEND_ACKS && recvd->type == PACKET_ANSWER) {
clicker.acknowledgeAnswer(&recvd->packet.answerPacket, true);
// restore the frequency back to AA and go back to promiscous mode
clicker.setChannel(iClickerChannels::AA);
clicker.startPromiscuous(CHANNEL_SEND, recvPacketHandler);
}

recvBuf.add(*recvd);
}
27 changes: 27 additions & 0 deletions emulator/iSkipper/iClickerEmulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,33 @@ bool iClickerEmulator::submitAnswer(uint8_t id[ICLICKER_ID_LEN], iClickerAnswer
return true;
}

void iClickerEmulator::acknowledgeAnswer(iClickerAnswerPacket* packet, bool accept) {
// ACKs are either 4 (accept) or 5 (reject) bytes
uint8_t ack_payload[5];
encodeId(packet->id, ack_payload);

configureRadio(CHANNEL_RECV, ACK_SEND_SYNC_ADDR);

if (accept) {
// When accepting, we simply replay the encoded payload
// ack[0-2] = encodedId[0-2]
// ack[3] = (encodedId[3] & 0xF0) | answer_nibble
ack_payload[3] = (ack_payload[3] & 0xF0) | (0x0F & getAnswerOffset(packet->answer));
// accept packets are only 4 bytes long so we zero out the last byte
ack_payload[4] = 0;
} else {
// ack[0-1] = encodedId[0-1]
// ack[2] = ~encodedId[2]
ack_payload[2] = ~ack_payload[2];
// ack[3] = (encodedId[3] & 0xF0) | 0x6
ack_payload[3] = (ack_payload[3] & 0xF0) | 0x6;
// ack[4] = 0x66
ack_payload[4] = 0x66;
}

_radio.send(ack_payload, 5, false);
}


void iClickerEmulator::setChannel(iClickerChannel chan)
{
Expand Down
5 changes: 4 additions & 1 deletion emulator/iSkipper/iClickerEmulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define ICLICKER_ID_LEN 4

#define SEND_SYNC_ADDR_LEN 3
#define RECV_SYNC_ADDR_LEN 2
#define RECV_SYNC_ADDR_LEN 3

#define DEFAULT_ACK_TIMEOUT 1500

Expand Down Expand Up @@ -46,6 +46,8 @@ const uint8_t answerOffsets[NUM_ANSWER_CHOICES] = {0x1, 0x5, 0xd, 0xe, 0xa, 0x2}
const uint8_t DEFAULT_SEND_SYNC_ADDR[SEND_SYNC_ADDR_LEN] =
{RF_SYNC_BYTE1_VALUE_IC, RF_SYNC_BYTE2_VALUE_IC , RF_SYNC_BYTE3_VALUE_IC };

const uint8_t ACK_SEND_SYNC_ADDR[RECV_SYNC_ADDR_LEN] = {0x55, 0x55, 0x55};


// 5 bytes
struct iClickerAnswerPacket
Expand Down Expand Up @@ -109,6 +111,7 @@ class iClickerEmulator
bool begin(iClickerChannel chan);
bool submitAnswer(uint8_t id[ICLICKER_ID_LEN], iClickerAnswer ans,
bool withAck=false, uint32_t timeout=DEFAULT_ACK_TIMEOUT, bool waitClear = true);
void acknowledgeAnswer(iClickerAnswerPacket* packet, bool accept=true);

void startPromiscuous(iClickerChannelType chanType, void (*cb)(iClickerPacket *));
void stopPromiscuous();
Expand Down
2 changes: 1 addition & 1 deletion emulator/iSkipper/iClickerRadioIncludes.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//packet length
#define PAYLOAD_LENGTH_SEND 0x05
#define PAYLOAD_LENGTH_RECV 0x07
#define PAYLOAD_LENGTH_RECV 0x05

typedef enum iClickerChannelType
{
Expand Down

0 comments on commit edf230f

Please sign in to comment.