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); }
// Wait a few milliseconds for proper ACK byte waitForAck(byte myNodeID) { MilliTimer ackTimer; while (!ackTimer.poll(ACK_TIME)) { if (rf12_recvDone() && rf12_crc == 0 && rf12_hdr == (RF12_HDR_DST | RF12_HDR_CTL | myNodeID)) return 1; } return 0; }
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); } }
void loop() { for (int i = 0; i < NUM_COUNTERS; i++) { // loop through the counters // read the counter input pin: counterState[i] = digitalRead(counterPin[i]); // compare the counterState to its previous state if (counterState[i] != lastCounterState[i]) { if (counterState[i] == LOW) { digitalWrite(LED1, HIGH); // if the current state is HIGH then the button // wend from off to on: #ifdef DEBUG_ENABLED Serial.print(i); Serial.println(" LOW"); #endif payload.active_counter = i; counterMillis[i] = millis(); payload.counter_millis = counterMillis[i]; if (counterMillis[i] - lastCounterMillis[i] > 90) { // send a packet while (!rf12_canSend()) rf12_recvDone(); // send as broadcast, payload will be encrypted rf12_sendStart(0, &payload, sizeof(payload)); rf12_sendWait(1); } lastCounterMillis[i] = counterMillis[i]; #ifdef DEBUG_ENABLED } else { // transision from LOW to HIGH means the pulse is over // we're not interested in that Serial.print(i); Serial.println(" HIGH"); #endif } Serial.println(); } // save the current state as the last state, //for next time through the loop lastCounterState[i] = counterState[i]; if (led1Timer.poll(90)) { digitalWrite(LED1, LOW); } } wdt_reset(); }