|
| 1 | +// Visual Micro is in vMicro>General>Tutorial Mode |
| 2 | +// |
| 3 | +/* |
| 4 | + Name: PingPong_example.ino |
| 5 | + Created: 10/12/2018 08.35.50 |
| 6 | + Author: RAHR\michael |
| 7 | +*/ |
| 8 | + |
| 9 | +// Define User Types below here or use a .h file |
| 10 | +// |
| 11 | +#include "NRF51_Radio_library.h" |
| 12 | +#include <Adafruit_Microbit.h> |
| 13 | + |
| 14 | +Adafruit_Microbit_Matrix microbit; //We only use this library to get easy access to the MATRIX LED, to sad you need to set the soft device to S110, even though we do not use it |
| 15 | + //Should be easy to fix, by removing the ble class of the in the library |
| 16 | +NRF51_Radio MicrobitRadio = NRF51_Radio(); //Let's get a new instance of the Radio |
| 17 | +enum STATENAME { |
| 18 | + START, |
| 19 | + SEND, |
| 20 | + RECV, |
| 21 | + RECVACT, |
| 22 | + SENDACK, |
| 23 | + WAITTOSEND |
| 24 | +}; |
| 25 | + |
| 26 | + |
| 27 | +// Define Function Prototypes that use User Types below here or use a .h file |
| 28 | +// |
| 29 | +FrameBuffer *myDataSendData; //FrameBuffer for sending data to another device |
| 30 | +FrameBuffer* myData; |
| 31 | +int current_state; //Var to hold the current state |
| 32 | +static long currentMillis; //Var to store the time gone since last time |
| 33 | +const long interval = 5000; //Wait time before sending |
| 34 | +const long start_time = 10000; //Wait time doing startup, we use this time to see if any other device is already running |
| 35 | +const long send_interval = 200; //In state send, after start we send out at 1 sec interval, ontil we start receiving something |
| 36 | +const long wait_to_send = 100; |
| 37 | +bool got_data = false; |
| 38 | + |
| 39 | +// Define Functions below here or use other .ino or cpp files |
| 40 | +// |
| 41 | + |
| 42 | +// The setup() function runs once each time the micro-controller starts |
| 43 | +void setup() |
| 44 | +{ |
| 45 | + Serial.begin(115200); |
| 46 | + Serial.println("Starting the PingPong example using Group 10, frekvens band 50"); |
| 47 | + microbit.begin(); |
| 48 | + microbit.show(microbit.HEART); |
| 49 | + MicrobitRadio.enable(); |
| 50 | + MicrobitRadio.setGroup(10); |
| 51 | + MicrobitRadio.setFrequencyBand(50); |
| 52 | + Serial.println("Radio running"); |
| 53 | + |
| 54 | + current_state = STATENAME::START; |
| 55 | + |
| 56 | + myDataSendData = new FrameBuffer(); |
| 57 | + currentMillis = millis(); |
| 58 | + |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +// Add the main program code into the continuous loop() function |
| 63 | +void loop() |
| 64 | +{ |
| 65 | + |
| 66 | + if ((MicrobitRadio.dataReady() > 0) &&(got_data!=true)) //Check if there are any data ready for us |
| 67 | + { |
| 68 | + myData = MicrobitRadio.recv(); //If so then let's get it |
| 69 | + got_data = true; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + switch (current_state) |
| 74 | + { |
| 75 | + case STATENAME::START: { |
| 76 | + |
| 77 | + if (got_data == true) |
| 78 | + { |
| 79 | + current_state = STATENAME::RECV; //If we got data then we are not the first one, we change state to recv. |
| 80 | + got_data == false; //reset the data flag; |
| 81 | + } |
| 82 | + |
| 83 | + if (millis() - currentMillis >= start_time) { |
| 84 | + current_state = STATENAME::SEND; //We did not find any signal, so now we assume that we are the first one running |
| 85 | + currentMillis = millis(); //so we start to send, and then wait for a ack. |
| 86 | + Serial.println("State go to SEND"); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + }; break; |
| 91 | + |
| 92 | + case STATENAME::RECV: { |
| 93 | + |
| 94 | + |
| 95 | + if (got_data == true) |
| 96 | + { |
| 97 | + |
| 98 | + if (myData->group == 2) { |
| 99 | + Serial.println("Got Data"); |
| 100 | + current_state = STATENAME::SENDACK; //If we got data then we are not the first one, we change state to recv. |
| 101 | + } |
| 102 | + |
| 103 | + delete myData; |
| 104 | + got_data = false; |
| 105 | + } |
| 106 | + |
| 107 | + }; break; |
| 108 | + |
| 109 | + case STATENAME::SENDACK: { |
| 110 | + Serial.println("Send ACK"); |
| 111 | + microbit.print("A"); |
| 112 | + MicrobitRadio.send(myDataSendData); |
| 113 | + myDataSendData->length = 3; |
| 114 | + myDataSendData->group = 1; //(1=ACK 2=SEND) |
| 115 | + myDataSendData->version = 10; |
| 116 | + myDataSendData->protocol = 10; |
| 117 | + MicrobitRadio.send(myDataSendData); |
| 118 | + current_state = STATENAME::WAITTOSEND; |
| 119 | + currentMillis = millis(); |
| 120 | + |
| 121 | + } break; |
| 122 | + |
| 123 | + case STATENAME::RECVACT: { |
| 124 | + current_state = STATENAME::RECV; |
| 125 | + }; break; |
| 126 | + |
| 127 | + case STATENAME::WAITTOSEND: { |
| 128 | + if (millis() - currentMillis >= wait_to_send) { |
| 129 | + Serial.println("State go to SEND"); |
| 130 | + current_state = STATENAME::SEND; |
| 131 | + } |
| 132 | + |
| 133 | + }; break; |
| 134 | + |
| 135 | + case STATENAME::SEND: { |
| 136 | + |
| 137 | + if (got_data == true) |
| 138 | + { |
| 139 | + if (myData->group == 1) { |
| 140 | + current_state = STATENAME::RECV; //If we got data then we are not the first one, we change state to recv. |
| 141 | + Serial.println("Got ACK"); |
| 142 | + } |
| 143 | + delete myData; |
| 144 | + got_data = false; //reset the data flag; |
| 145 | + } |
| 146 | + if (millis() - currentMillis >= send_interval) { |
| 147 | + Serial.println("State = SEND"); |
| 148 | + current_state = STATENAME::SEND; //We did not find any signal, so now we assume that we are the first one running |
| 149 | + currentMillis = millis(); //so we start to send, and then wait for a ack. |
| 150 | + microbit.print("S"); |
| 151 | + myDataSendData->length = 3; |
| 152 | + myDataSendData->group = 2; //(1=ACK 2=SEND) |
| 153 | + myDataSendData->version = 10; |
| 154 | + myDataSendData->protocol = 10; |
| 155 | + MicrobitRadio.send(myDataSendData); |
| 156 | + } |
| 157 | + }; break; |
| 158 | + |
| 159 | + |
| 160 | + default: |
| 161 | + break; |
| 162 | + } |
| 163 | +} |
0 commit comments