void loop () { if (Serial.available()) handleInput(Serial.read()); if (tempTimer.poll()) { sendTempPacket(); tempTimer.set(TEMP_INTERVAL); } unsigned long now = millis(); cli(); bool needSendPulse = false; if (pulseActivity && (lastPulsePacket + PULSE_INTERVAL < now)) { pulseActivity = false; lastPulsePacket = now; needSendPulse = true; } sei(); if (needSendPulse) { sendPulsePacket(); } if (!rf12_recvDone() || rf12_crc != 0) return; // nothing to do digitalWrite(PIN_LED, HIGH); forwardPacket(); digitalWrite(PIN_LED, LOW); }
void setup () { Serial.begin(57600); Serial.print("\n[groupRelay]"); loadConfig(); tempTimer.set(TEMP_INTERVAL); pinMode(PIN_LED, OUTPUT); attachInterrupt(1, pulse, CHANGE); // pinMode(PIN_TEMP1, INPUT); // pinMode(PIN_TEMP2, INPUT); // digitalWrite(PIN_TEMP1, LOW); // disable pullup // digitalWrite(PIN_TEMP2, LOW); // disable pullup }
void forwardPacket() { // make copies, because rf12_* will change in next rf12_recvDone byte hdr = rf12_hdr, len = rf12_len; if (config.multi_node) { // special case: insert original header (src node ID) as first data byte // careful with max-length packets in multi-node mode: drop last byte! // this is necessary because we're inserting an extra byte at the front if (len >= sizeof buf) --len; buf[0]= hdr; } memcpy(buf + config.multi_node, (void*) rf12_data, len); // save these for later as well, same reason as above byte wantsAck = RF12_WANTS_ACK, ackReply = RF12_ACK_REPLY; if (config.acks_enable) { // if we're not supposed to send back ACKs, then don't ask for 'em wantsAck = false; hdr &= ~ RF12_HDR_ACK; } Serial.print("\n[*]"); // switch to outgoing group rf12_initialize(config.out_node, code2type(config.freq), config.out_group); // send our packet, once possible while (!rf12_canSend()) rf12_recvDone(); rf12_sendStart(hdr, buf, len + config.multi_node, 1); if (wantsAck) { ackTimer.set(100); // wait up to 100 ms for a valid ack packet wantsAck = false; while (!wantsAck && !ackTimer.poll()) wantsAck = rf12_recvDone() && rf12_crc == 0; } // switch back to incoming group rf12_initialize(config.in_node, code2type(config.freq), config.in_group); if (wantsAck) { // copy ack packet to our temp buffer, same reason as above len = rf12_len; memcpy(buf, (void*) rf12_data, rf12_len); // send ACK packet back, once possible while (!rf12_canSend()) rf12_recvDone(); rf12_sendStart(ackReply, buf, len, 1); } }