Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RHGenericDriver.cpp
00001 // RHGenericDriver.cpp 00002 // 00003 // Copyright (C) 2014 Mike McCauley 00004 // $Id: RHGenericDriver.cpp,v 1.21 2017/03/04 00:59:41 mikem Exp $ 00005 // 00006 // Ported to mbed - support only a single radio - Dan Julio - 5/2017 00007 // 00008 00009 #include <RHGenericDriver.h> 00010 00011 RHGenericDriver::RHGenericDriver() 00012 : 00013 _mode(RHModeInitialising), 00014 _thisAddress(RH_BROADCAST_ADDRESS), 00015 _txHeaderTo(RH_BROADCAST_ADDRESS), 00016 _txHeaderFrom(RH_BROADCAST_ADDRESS), 00017 _txHeaderId(0), 00018 _txHeaderFlags(0), 00019 _rxBad(0), 00020 _rxGood(0), 00021 _txGood(0), 00022 _cad_timeout(0) 00023 { 00024 } 00025 00026 bool RHGenericDriver::init() 00027 { 00028 return true; 00029 } 00030 00031 // Blocks until a valid message is received 00032 void RHGenericDriver::waitAvailable() 00033 { 00034 while (!available()) 00035 Thread::yield(); 00036 } 00037 00038 // Blocks until a valid message is received or timeout expires 00039 // Return true if there is a message available 00040 // Works correctly even on millis() rollover 00041 bool RHGenericDriver::waitAvailableTimeout(uint16_t timeout) 00042 { 00043 _waitTimer.reset(); 00044 _waitTimer.start(); 00045 00046 while (_waitTimer.read_ms() < timeout) 00047 { 00048 if (available()) 00049 { 00050 _waitTimer.stop(); 00051 return true; 00052 } 00053 Thread::yield(); 00054 } 00055 _waitTimer.stop(); 00056 return false; 00057 } 00058 00059 bool RHGenericDriver::waitPacketSent() 00060 { 00061 while (_mode == RHModeTx) 00062 Thread::yield(); // Wait for any previous transmit to finish 00063 return true; 00064 } 00065 00066 bool RHGenericDriver::waitPacketSent(uint16_t timeout) 00067 { 00068 _waitTimer.reset(); 00069 _waitTimer.start(); 00070 while (_waitTimer.read_ms() < timeout) 00071 { 00072 if (_mode != RHModeTx) // Any previous transmit finished? 00073 { 00074 _waitTimer.stop(); 00075 return true; 00076 } 00077 Thread::yield(); 00078 } 00079 _waitTimer.stop(); 00080 return false; 00081 } 00082 00083 // Wait until no channel activity detected or timeout 00084 bool RHGenericDriver::waitCAD() 00085 { 00086 if (!_cad_timeout) 00087 return true; 00088 00089 // Wait for any channel activity to finish or timeout 00090 // Sophisticated DCF function... 00091 // DCF : BackoffTime = random() x aSlotTime 00092 // 100 - 1000 ms 00093 // 10 sec timeout 00094 _waitTimer.reset(); 00095 _waitTimer.start(); 00096 while (isChannelActive()) 00097 { 00098 if (_waitTimer.read_ms() > _cad_timeout) 00099 { 00100 _waitTimer.stop(); 00101 return false; 00102 } 00103 00104 wait_ms(100); 00105 } 00106 00107 _waitTimer.stop(); 00108 return true; 00109 } 00110 00111 // subclasses are expected to override if CAD is available for that radio 00112 bool RHGenericDriver::isChannelActive() 00113 { 00114 return false; 00115 } 00116 00117 void RHGenericDriver::setPromiscuous(bool promiscuous) 00118 { 00119 _promiscuous = promiscuous; 00120 } 00121 00122 void RHGenericDriver::setThisAddress(uint8_t address) 00123 { 00124 _thisAddress = address; 00125 } 00126 00127 void RHGenericDriver::setHeaderTo(uint8_t to) 00128 { 00129 _txHeaderTo = to; 00130 } 00131 00132 void RHGenericDriver::setHeaderFrom(uint8_t from) 00133 { 00134 _txHeaderFrom = from; 00135 } 00136 00137 void RHGenericDriver::setHeaderId(uint8_t id) 00138 { 00139 _txHeaderId = id; 00140 } 00141 00142 void RHGenericDriver::setHeaderFlags(uint8_t set, uint8_t clear) 00143 { 00144 _txHeaderFlags &= ~clear; 00145 _txHeaderFlags |= set; 00146 } 00147 00148 uint8_t RHGenericDriver::headerTo() 00149 { 00150 return _rxHeaderTo; 00151 } 00152 00153 uint8_t RHGenericDriver::headerFrom() 00154 { 00155 return _rxHeaderFrom; 00156 } 00157 00158 uint8_t RHGenericDriver::headerId() 00159 { 00160 return _rxHeaderId; 00161 } 00162 00163 uint8_t RHGenericDriver::headerFlags() 00164 { 00165 return _rxHeaderFlags; 00166 } 00167 00168 int8_t RHGenericDriver::lastRssi() 00169 { 00170 return _lastRssi; 00171 } 00172 00173 RHGenericDriver::RHMode RHGenericDriver::mode() 00174 { 00175 return _mode; 00176 } 00177 00178 void RHGenericDriver::setMode(RHMode mode) 00179 { 00180 _mode = mode; 00181 } 00182 00183 bool RHGenericDriver::sleep() 00184 { 00185 return false; 00186 } 00187 00188 // Diagnostic help 00189 void RHGenericDriver::printBuffer(const char* prompt, const uint8_t* buf, uint8_t len) 00190 { 00191 uint8_t i; 00192 00193 printf("%s", prompt); 00194 for (i = 0; i < len; i++) 00195 { 00196 if (i % 16 == 15) 00197 printf("0x%x\n", buf[i]); 00198 else 00199 { 00200 printf("0x%x ", buf[i]); 00201 } 00202 } 00203 printf("\n"); 00204 } 00205 00206 uint16_t RHGenericDriver::rxBad() 00207 { 00208 return _rxBad; 00209 } 00210 00211 uint16_t RHGenericDriver::rxGood() 00212 { 00213 return _rxGood; 00214 } 00215 00216 uint16_t RHGenericDriver::txGood() 00217 { 00218 return _txGood; 00219 } 00220 00221 void RHGenericDriver::setCADTimeout(unsigned long cad_timeout) 00222 { 00223 _cad_timeout = cad_timeout; 00224 } 00225
Generated on Wed Jul 13 2022 12:04:17 by
1.7.2