Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to acknowledge answers #12

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 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
ammaraskar marked this conversation as resolved.
Show resolved Hide resolved
#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,29 @@ 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) {
Serial.println("Sending ACK");
ammaraskar marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -197,6 +197,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);
ammaraskar marked this conversation as resolved.
Show resolved Hide resolved

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));

_radio.send(ack_payload, 4, false);
} 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
4 changes: 4 additions & 0 deletions emulator/iSkipper/iClickerEmulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ 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 };

#define ACK_SEND_SYNC_ADDR_LEN 3
ammaraskar marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t ACK_SEND_SYNC_ADDR[ACK_SEND_SYNC_ADDR_LEN] = {0x55, 0x55, 0x55};


// 5 bytes
struct iClickerAnswerPacket
Expand Down Expand Up @@ -109,6 +112,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
3 changes: 2 additions & 1 deletion emulator/iSkipper/iClickerRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "RFM69.h"

// threshold for triggerins fifo transmit interrupt
ammaraskar marked this conversation as resolved.
Show resolved Hide resolved
#define RF_FIFOTHRESH_TXSTART_FIFOTHRESH_IC 0x04
// (should be the [smallest packet size - 1])
#define RF_FIFOTHRESH_TXSTART_FIFOTHRESH_IC 0x03


//RegSyncValue1-8 for sending is:
Expand Down