inline void RadioTask::_checkForStateChange() { if (_radioState == RADIO_STATE_DISCOVERY) { if (_discoveryFinishingTime <= xTaskGetTickCount()) { if (_bestChannel == NO_CHANNEL_DISCOVERED) { debug::log("RadioTask: No channel discovered during this discovery period. Will sleep for certain time and try again"); vTaskDelay(RADIO_UNSUCCESSFUL_DISCOVERY_SLEEP); _initialiseDiscoveryState(); } else { _initialiseReceiveState(); } } } else if (_radioState == RADIO_STATE_RECEIVE) { if (_lastMessageReceived + RADIO_RECEIVE_TIMEOUT <= xTaskGetTickCount()) { debug::log("RadioTask: Main channel timeout - Going back to disovery"); _initialiseDiscoveryState(); } } }
void RadioReceiveTask::task() { _clearSerialBuffer(); _initialiseDiscoveryState(); // Packet byte packetBuffer[RADIO_PACKET_WITH_RSSI_LENGTH]; uint8_t packetBufferLength = 0; #ifdef RADIO_DEBUG_MODE uint8_t serialRingBufferHWM = 0; #endif _wakeUp(); _currentMessageReceiver = NO_CURRENT_MESSAGE; while (true) { uint8_t availableBytes = RADIO_SERIAL.available(); if (availableBytes > 0) { pinMode(PIN_LED_RXL, OUTPUT); digitalWrite(PIN_LED_RXL, 1); #ifdef RADIO_DEBUG_MODE if (serialRingBufferHWM < availableBytes) { serialRingBufferHWM = availableBytes; debug::log("RadioReceiveTask: serialRingBufferHWM=" + String(serialRingBufferHWM)); } #endif while (availableBytes > 0) { packetBuffer[packetBufferLength] = RADIO_SERIAL.read(); packetBufferLength++; availableBytes--; // Only handle one packet at a time if (packetBufferLength == RADIO_PACKET_WITH_RSSI_LENGTH) { break; // we have enough for one packet } } #ifdef RADIO_DEBUG_MODE_EXTENDED debug::log("RadioReceiveTask: Current buffer:"); debug::logByteArray(packetBuffer, packetBufferLength); #endif packetBufferLength = _parsePacketBuffer(packetBuffer, packetBufferLength); digitalWrite(PIN_LED_RXL, 0); _lastMessageReceived = xTaskGetTickCount(); } else { vTaskDelay(RADIO_NO_DATA_SLEEP_DURATION); } _checkForStateChange(); } }
void RadioTask::task() { // Temporary: Remove me soon! _outgoingPacketBuffer[0] = 0x90; _outgoingPacketBuffer[1] = 0x03; // ping service for (uint8_t i=2; i<RADIO_PACKET_LENGTH; i++) { _outgoingPacketBuffer[i] = i - 2; } _outgoingPacketAvailable = true; // Setup radio communitcation RADIO_SERIAL.begin(RADIO_SERIAL_BAUD); // Setup AT Mode pin pinMode(RADIO_AT_MODE_PIN, OUTPUT); _clearSerialBuffer(); _initialiseDiscoveryState(); // Packet byte packetBuffer[RADIO_PACKET_WITH_RSSI_LENGTH]; uint8_t packetBufferLength = 0; _currentMessageReceiver = NO_CURRENT_MESSAGE; while (true) { uint8_t availableBytes = RADIO_SERIAL.available(); if (availableBytes > 0) { while (availableBytes > 0) { packetBuffer[packetBufferLength] = RADIO_SERIAL.read(); packetBufferLength++; availableBytes--; // Only handle one packet at a time if (packetBufferLength == RADIO_PACKET_WITH_RSSI_LENGTH) { break; // we have enough for one packet } } packetBufferLength = _parsePacketBuffer(packetBuffer, packetBufferLength); _lastMessageReceived = xTaskGetTickCount(); } _checkForStateChange(); } }