V148

Fork of RadioHead-148 by David Rimer

Committer:
ilkaykozak
Date:
Wed Oct 25 05:14:09 2017 +0000
Revision:
1:b7641da2b203
Parent:
0:ab4e012489ef
V148

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidr99 0:ab4e012489ef 1 // RHGenericDriver.cpp
davidr99 0:ab4e012489ef 2 //
davidr99 0:ab4e012489ef 3 // Copyright (C) 2014 Mike McCauley
davidr99 0:ab4e012489ef 4 // $Id: RHGenericDriver.cpp,v 1.18 2015/01/02 21:38:24 mikem Exp $
davidr99 0:ab4e012489ef 5
davidr99 0:ab4e012489ef 6 #include <RHGenericDriver.h>
davidr99 0:ab4e012489ef 7
davidr99 0:ab4e012489ef 8 Timer _millisTimer;
davidr99 0:ab4e012489ef 9
davidr99 0:ab4e012489ef 10 RHGenericDriver::RHGenericDriver()
davidr99 0:ab4e012489ef 11 :
davidr99 0:ab4e012489ef 12 _mode(RHModeInitialising),
davidr99 0:ab4e012489ef 13 _thisAddress(RH_BROADCAST_ADDRESS),
davidr99 0:ab4e012489ef 14 _txHeaderTo(RH_BROADCAST_ADDRESS),
davidr99 0:ab4e012489ef 15 _txHeaderFrom(RH_BROADCAST_ADDRESS),
davidr99 0:ab4e012489ef 16 _txHeaderId(0),
davidr99 0:ab4e012489ef 17 _txHeaderFlags(0),
davidr99 0:ab4e012489ef 18 _rxBad(0),
davidr99 0:ab4e012489ef 19 _rxGood(0),
davidr99 0:ab4e012489ef 20 _txGood(0)
davidr99 0:ab4e012489ef 21 {
davidr99 0:ab4e012489ef 22 #if (RH_PLATFORM == RH_PLATFORM_MBED)
davidr99 0:ab4e012489ef 23 _millisTimer.start();
davidr99 0:ab4e012489ef 24 #endif
davidr99 0:ab4e012489ef 25 }
davidr99 0:ab4e012489ef 26
davidr99 0:ab4e012489ef 27 bool RHGenericDriver::init()
davidr99 0:ab4e012489ef 28 {
davidr99 0:ab4e012489ef 29 return true;
davidr99 0:ab4e012489ef 30 }
davidr99 0:ab4e012489ef 31
davidr99 0:ab4e012489ef 32 // Blocks until a valid message is received
davidr99 0:ab4e012489ef 33 void RHGenericDriver::waitAvailable()
davidr99 0:ab4e012489ef 34 {
davidr99 0:ab4e012489ef 35 while (!available())
davidr99 0:ab4e012489ef 36 YIELD;
davidr99 0:ab4e012489ef 37 }
davidr99 0:ab4e012489ef 38
davidr99 0:ab4e012489ef 39 // Blocks until a valid message is received or timeout expires
davidr99 0:ab4e012489ef 40 // Return true if there is a message available
davidr99 0:ab4e012489ef 41 // Works correctly even on millis() rollover
davidr99 0:ab4e012489ef 42 bool RHGenericDriver::waitAvailableTimeout(uint16_t timeout)
davidr99 0:ab4e012489ef 43 {
davidr99 0:ab4e012489ef 44 unsigned long starttime = millis();
davidr99 0:ab4e012489ef 45 while ((millis() - starttime) < timeout)
davidr99 0:ab4e012489ef 46 {
davidr99 0:ab4e012489ef 47 if (available())
davidr99 0:ab4e012489ef 48 return true;
davidr99 0:ab4e012489ef 49 YIELD;
davidr99 0:ab4e012489ef 50 }
davidr99 0:ab4e012489ef 51 return false;
davidr99 0:ab4e012489ef 52 }
davidr99 0:ab4e012489ef 53
davidr99 0:ab4e012489ef 54 bool RHGenericDriver::waitPacketSent()
davidr99 0:ab4e012489ef 55 {
davidr99 0:ab4e012489ef 56 while (_mode == RHModeTx)
davidr99 0:ab4e012489ef 57 YIELD; // Wait for any previous transmit to finish
davidr99 0:ab4e012489ef 58 return true;
davidr99 0:ab4e012489ef 59 }
davidr99 0:ab4e012489ef 60
davidr99 0:ab4e012489ef 61 bool RHGenericDriver::waitPacketSent(uint16_t timeout)
davidr99 0:ab4e012489ef 62 {
davidr99 0:ab4e012489ef 63 unsigned long starttime = millis();
davidr99 0:ab4e012489ef 64 while ((millis() - starttime) < timeout)
davidr99 0:ab4e012489ef 65 {
davidr99 0:ab4e012489ef 66 if (_mode != RHModeTx) // Any previous transmit finished?
davidr99 0:ab4e012489ef 67 return true;
davidr99 0:ab4e012489ef 68 YIELD;
davidr99 0:ab4e012489ef 69 }
davidr99 0:ab4e012489ef 70 return false;
davidr99 0:ab4e012489ef 71 }
davidr99 0:ab4e012489ef 72
davidr99 0:ab4e012489ef 73 void RHGenericDriver::setPromiscuous(bool promiscuous)
davidr99 0:ab4e012489ef 74 {
davidr99 0:ab4e012489ef 75 _promiscuous = promiscuous;
davidr99 0:ab4e012489ef 76 }
davidr99 0:ab4e012489ef 77
davidr99 0:ab4e012489ef 78 void RHGenericDriver::setThisAddress(uint8_t address)
davidr99 0:ab4e012489ef 79 {
davidr99 0:ab4e012489ef 80 _thisAddress = address;
davidr99 0:ab4e012489ef 81 }
davidr99 0:ab4e012489ef 82
davidr99 0:ab4e012489ef 83 void RHGenericDriver::setHeaderTo(uint8_t to)
davidr99 0:ab4e012489ef 84 {
davidr99 0:ab4e012489ef 85 _txHeaderTo = to;
davidr99 0:ab4e012489ef 86 }
davidr99 0:ab4e012489ef 87
davidr99 0:ab4e012489ef 88 void RHGenericDriver::setHeaderFrom(uint8_t from)
davidr99 0:ab4e012489ef 89 {
davidr99 0:ab4e012489ef 90 _txHeaderFrom = from;
davidr99 0:ab4e012489ef 91 }
davidr99 0:ab4e012489ef 92
davidr99 0:ab4e012489ef 93 void RHGenericDriver::setHeaderId(uint8_t id)
davidr99 0:ab4e012489ef 94 {
davidr99 0:ab4e012489ef 95 _txHeaderId = id;
davidr99 0:ab4e012489ef 96 }
davidr99 0:ab4e012489ef 97
davidr99 0:ab4e012489ef 98 void RHGenericDriver::setHeaderFlags(uint8_t set, uint8_t clear)
davidr99 0:ab4e012489ef 99 {
davidr99 0:ab4e012489ef 100 _txHeaderFlags &= ~clear;
davidr99 0:ab4e012489ef 101 _txHeaderFlags |= set;
davidr99 0:ab4e012489ef 102 }
davidr99 0:ab4e012489ef 103
davidr99 0:ab4e012489ef 104 uint8_t RHGenericDriver::headerTo()
davidr99 0:ab4e012489ef 105 {
davidr99 0:ab4e012489ef 106 return _rxHeaderTo;
davidr99 0:ab4e012489ef 107 }
davidr99 0:ab4e012489ef 108
davidr99 0:ab4e012489ef 109 uint8_t RHGenericDriver::headerFrom()
davidr99 0:ab4e012489ef 110 {
davidr99 0:ab4e012489ef 111 return _rxHeaderFrom;
davidr99 0:ab4e012489ef 112 }
davidr99 0:ab4e012489ef 113
davidr99 0:ab4e012489ef 114 uint8_t RHGenericDriver::headerId()
davidr99 0:ab4e012489ef 115 {
davidr99 0:ab4e012489ef 116 return _rxHeaderId;
davidr99 0:ab4e012489ef 117 }
davidr99 0:ab4e012489ef 118
davidr99 0:ab4e012489ef 119 uint8_t RHGenericDriver::headerFlags()
davidr99 0:ab4e012489ef 120 {
davidr99 0:ab4e012489ef 121 return _rxHeaderFlags;
davidr99 0:ab4e012489ef 122 }
davidr99 0:ab4e012489ef 123
davidr99 0:ab4e012489ef 124 int8_t RHGenericDriver::lastRssi()
davidr99 0:ab4e012489ef 125 {
davidr99 0:ab4e012489ef 126 return _lastRssi;
davidr99 0:ab4e012489ef 127 }
davidr99 0:ab4e012489ef 128
davidr99 0:ab4e012489ef 129 RHGenericDriver::RHMode RHGenericDriver::mode()
davidr99 0:ab4e012489ef 130 {
davidr99 0:ab4e012489ef 131 return _mode;
davidr99 0:ab4e012489ef 132 }
davidr99 0:ab4e012489ef 133
davidr99 0:ab4e012489ef 134 void RHGenericDriver::setMode(RHMode mode)
davidr99 0:ab4e012489ef 135 {
davidr99 0:ab4e012489ef 136 _mode = mode;
davidr99 0:ab4e012489ef 137 }
davidr99 0:ab4e012489ef 138
davidr99 0:ab4e012489ef 139 bool RHGenericDriver::sleep()
davidr99 0:ab4e012489ef 140 {
davidr99 0:ab4e012489ef 141 return false;
davidr99 0:ab4e012489ef 142 }
davidr99 0:ab4e012489ef 143
davidr99 0:ab4e012489ef 144 // Diagnostic help
davidr99 0:ab4e012489ef 145 void RHGenericDriver::printBuffer(const char* prompt, const uint8_t* buf, uint8_t len)
davidr99 0:ab4e012489ef 146 {
davidr99 0:ab4e012489ef 147 uint8_t i;
davidr99 0:ab4e012489ef 148
davidr99 0:ab4e012489ef 149 #ifdef RH_HAVE_SERIAL
davidr99 0:ab4e012489ef 150 Serial.println(prompt);
davidr99 0:ab4e012489ef 151 for (i = 0; i < len; i++)
davidr99 0:ab4e012489ef 152 {
davidr99 0:ab4e012489ef 153 if (i % 16 == 15)
davidr99 0:ab4e012489ef 154 Serial.println(buf[i], HEX);
davidr99 0:ab4e012489ef 155 else
davidr99 0:ab4e012489ef 156 {
davidr99 0:ab4e012489ef 157 Serial.print(buf[i], HEX);
davidr99 0:ab4e012489ef 158 Serial.print(' ');
davidr99 0:ab4e012489ef 159 }
davidr99 0:ab4e012489ef 160 }
davidr99 0:ab4e012489ef 161 Serial.println("");
davidr99 0:ab4e012489ef 162 #endif
davidr99 0:ab4e012489ef 163 }
davidr99 0:ab4e012489ef 164
davidr99 0:ab4e012489ef 165 uint16_t RHGenericDriver::rxBad()
davidr99 0:ab4e012489ef 166 {
davidr99 0:ab4e012489ef 167 return _rxBad;
davidr99 0:ab4e012489ef 168 }
davidr99 0:ab4e012489ef 169
davidr99 0:ab4e012489ef 170 uint16_t RHGenericDriver::rxGood()
davidr99 0:ab4e012489ef 171 {
davidr99 0:ab4e012489ef 172 return _rxGood;
davidr99 0:ab4e012489ef 173 }
davidr99 0:ab4e012489ef 174
davidr99 0:ab4e012489ef 175 uint16_t RHGenericDriver::txGood()
davidr99 0:ab4e012489ef 176 {
davidr99 0:ab4e012489ef 177 return _txGood;
davidr99 0:ab4e012489ef 178 }
davidr99 0:ab4e012489ef 179
davidr99 0:ab4e012489ef 180 #if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined(RH_PLATFORM_ATTINY)
davidr99 0:ab4e012489ef 181 // Tinycore does not have __cxa_pure_virtual, so without this we
davidr99 0:ab4e012489ef 182 // get linking complaints from the default code generated for pure virtual functions
davidr99 0:ab4e012489ef 183 extern "C" void __cxa_pure_virtual()
davidr99 0:ab4e012489ef 184 {
davidr99 0:ab4e012489ef 185 while (1);
davidr99 0:ab4e012489ef 186 }
davidr99 0:ab4e012489ef 187 #endif