1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
==================== updateTestData() ====================
*/
// Sets testNumber at the end of testData then updates it
// to demonstrate that each test is with new data.
void updateTestData(char* testData, char* testNumber)
{
// Set testNumber in testData
testData[8] = testNumber[0];
// Increment testNumber.
testNumber[0] += 1;
} // end updateTestData()
// Carries out a *handshake* process with the nominated slave unit.
// Transmits 9 test messages and receives acknowledgement utilizing the
// *enableAckPayload* functionality in the RF24 header
//
//
// NOTE: The parameter 'slaveAddress' is passed by value because it is not
// going to be changed by the function.
bool handshake(uint64_t slaveAddress,
unsigned long* timeData,
char* testData,
char* testNumber,
int* ackData,
bool* newData,
bool* dataSwapped)
{
do
{
// Record the current time.
timeData[0] = millis();
// if current - previous >= interval
if (timeData[0] - timeData[1] >= timeData[2])
{
// debug
Serial.println();
Serial.println();
Serial.println();
Serial.println("In 'handshake()' at start");
Serial.print("*newData* is >");
Serial.print(*dataSwapped);
Serial.print("< and *dataSwapped* is >");
Serial.print(*dataSwapped);
Serial.println('<');
Serial.print("Acknowledgement data is ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.print(ackData[1]);
Serial.print(", ");
Serial.println(ackData[2]);
// debug
send(slaveAddress,
timeData,
testData,
testNumber,
ackData,
newData,
dataSwapped);
//debug
Serial.println("In 'handshake()' after cycle");
Serial.print("*newData* is >");
Serial.print(*dataSwapped);
Serial.print("< and *dataSwapped* is >");
Serial.print(*dataSwapped);
Serial.println('<');
Serial.print("Acknowledgement data is ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.print(ackData[1]);
Serial.print(", ");
Serial.println(ackData[2]);
// debug
} // end if ( currentTime
// Ensure that both Finite State Machine Flags are unset.
*newData = false;
*dataSwapped = false;
} while (*testNumber != '9');
// if handshake process has completed and *dataSwapped* is left as TRUE,
// that is *dataSwapped* was not set to FALSE in *send()*, then contact
// with the slave is established.
return *dataSwapped;
} // end handshake()
// Transmits the test data and listens for acknowledgement.
// If TX acknowledges, prints ackData, else prints error message.
void send(uint64_t slaveAddress,
unsigned long* timeData,
char* testData,
char* testNumber,
int* ackData,
bool* newData,
bool* dataSwapped)
{
// local variable
bool rslt;
// Only stop listening for as long as it takes to send the message.
radio.stopListening();
// Open the writing pipe for whichever slave master is talking to now.
radio.openWritingPipe(slaveAddress);
// Always use sizeof() as it gives the size as the number of bytes. That
// saves having genius programmers making a wild guess at the size.
//
// 'write()' returns TRUE if transmission is successful.
rslt = radio.write(&testData, sizeof(testData));
radio.startListening();
if (rslt) // Decide what to do.
{
if (radio.isAckPayloadAvailable())
{
// ackPayload must be read a.s.a.p after availability
// has been confirmed.
radio.read(&ackData, sizeof(ackData));
// Set the Finite State Machine Flags
*dataSwapped = true;
*newData = true;
//debug
Serial.println("In 'send()'");
Serial.print("'send()' has sent ");
Serial.println(testData);
Serial.println("Acknowledgement received");
Serial.print("*newData* is >");
Serial.print(*dataSwapped);
Serial.print("< and *dataSwapped* is >");
Serial.print(*dataSwapped);
Serial.println('<');
Serial.print("Acknowledgement data is ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.print(ackData[1]);
Serial.print(", ");
Serial.println(ackData[2]);
// debug
// ASSERT: at this point *newData* is TRUE so there is
// no point in sending it as a parameter. It is just being
// send for debug purposes. <<< ================================
showAckData(newData, ackData);
// *testData* has been sent so reset for next cycle.
updateTestData(testData, testNumber);
}
else
{
// Tell the world there is no acknowledgement
Serial.println("No acknowledgement sent to master node ");
// Unset the Finite State Machine Flag.
if (!*testNumber == '9')
{
*dataSwapped = false;
}
} // end if ( radio.isAckPayloadAvailable() )
// Now that transmission is complete, NOW is *Current Time*.
timeData[1] = millis();
}
else
{
// Tell the world no data sent.
Serial.println("****Tx failed");
// Unset the finite State Machine Flag
*dataSwapped = false;
// Since transmission failed do not reset *Current Time* and master
// will try to send message again a.s.a.p.
} // end if (rslt)
} // end send()
// Displays the acknowledgement data.
// Returns the state of 'newData' to default after printing.
void showAckData(bool* newData, int* ackData)
{
// debug
Serial.println("In 'showAckData()'");
Serial.print("*newData* is >");
Serial.print(*newData);
Serial.print("< and *dataSwapped* is >");
Serial.print(*dataSwapped);
Serial.println('<');
// debug
Serial.print("Acknowledgement data is ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.print(ackData[1]);
Serial.print(", ");
Serial.println(ackData[2]);
} // end showAckData()
|