football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
andriym
Date:
Sat Jun 11 06:50:32 2016 +0000
Revision:
89:425369a595c4
Parent:
88:d3f0394da5aa
Child:
90:b4d53a870147
FOTA added (unable to test)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmbed 76:6638d83939d5 1 #include <RFM69.h>
andriym 78:43a6b54f0372 2 #include <list>
andriym 78:43a6b54f0372 3 #include "output.h"
andriym 82:1e552cc1a615 4 #include <DebounceIn.h>
andriym 89:425369a595c4 5 #ifdef BLE_ENABLE
andriym 89:425369a595c4 6 #include "ble/BLE.h"
andriym 89:425369a595c4 7 #endif
AntonLS 0:28ca4562fe1a 8
andriym 78:43a6b54f0372 9 using namespace std;
andriym 78:43a6b54f0372 10
andriym 89:425369a595c4 11 Timer tmr;
andriym 78:43a6b54f0372 12 Output out;
elmbed 17:d8b901d791fd 13
andriym 89:425369a595c4 14 #ifdef BLE_ENABLE
andriym 89:425369a595c4 15 /* Optional: Device Name, add for human read-ability */
andriym 89:425369a595c4 16 const static char DEVICE_NAME[] = "PassiveCoach";
andriym 89:425369a595c4 17
andriym 89:425369a595c4 18 /* You have up to 26 bytes of advertising data to use. */
andriym 89:425369a595c4 19 //const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */
andriym 89:425369a595c4 20 const static uint8_t AdvData[] = {"Keep the Space!"}; /* Example of character data */
andriym 89:425369a595c4 21 #endif // BLE_ENABLE
andriym 89:425369a595c4 22
andriym 78:43a6b54f0372 23 /////////////////////////////////// PINS ///////////////////////////////////
andriym 79:9db29f41dc9d 24
andriym 82:1e552cc1a615 25 DebounceIn buttonTeam(BUT_TEAM);
andriym 82:1e552cc1a615 26 DebounceIn buttonSpace(BUT_SPACE);
andriym 82:1e552cc1a615 27 DebounceIn buttonVolMore(BUT_VOL_MORE);
andriym 82:1e552cc1a615 28 DebounceIn buttonVolLess(BUT_VOL_LESS);
andriym 81:3cd7de5d01ef 29
andriym 79:9db29f41dc9d 30 DigitalOut ledTeamA(LED_TEAM_A);
andriym 79:9db29f41dc9d 31 DigitalOut ledTeamB(LED_TEAM_B);
andriym 79:9db29f41dc9d 32 DigitalOut ledSpace5(LED_SPACE5);
andriym 79:9db29f41dc9d 33 DigitalOut ledSpace10(LED_SPACE10);
andriym 79:9db29f41dc9d 34 DigitalOut ledSpace15(LED_SPACE15);
andriym 79:9db29f41dc9d 35 DigitalOut ledSpace20(LED_SPACE20);
andriym 81:3cd7de5d01ef 36 DigitalOut ledBuzzer(LED_BUZZER_ON);
andriym 81:3cd7de5d01ef 37
andriym 79:9db29f41dc9d 38 AnalogIn ain(ANALOG_IN); // used for randomizing
andriym 78:43a6b54f0372 39 #ifdef NORDIC
andriym 79:9db29f41dc9d 40 DigitalOut buzzer(BUZZER);
andriym 88:d3f0394da5aa 41 #ifndef HARD_V2
andriym 79:9db29f41dc9d 42 DigitalOut buzzLow(BUZZ_LOW);
andriym 88:d3f0394da5aa 43 #endif
andriym 79:9db29f41dc9d 44 DigitalOut buzzMed(BUZZ_MED);
andriym 79:9db29f41dc9d 45 DigitalOut buzzHigh(BUZZ_HIGH);
andriym 79:9db29f41dc9d 46 #else
andriym 79:9db29f41dc9d 47 PwmOut speaker(BUZZER); // passive buzzer
andriym 79:9db29f41dc9d 48 #endif
AntonLS 30:c60b0d52b067 49
andriym 78:43a6b54f0372 50 /////////////////////////////////// RADIO ///////////////////////////////////
andriym 83:79cb2ba44b66 51 #ifdef ENABLE_PIN
andriym 83:79cb2ba44b66 52 static RFM69 radio(RFM_MOSI, RFM_MISO, RFM_SCK, RFM_SS, RFM_IRQ, RFM_ISM_EN);
andriym 83:79cb2ba44b66 53 #else
andriym 79:9db29f41dc9d 54 static RFM69 radio(RFM_MOSI, RFM_MISO, RFM_SCK, RFM_SS, RFM_IRQ);
andriym 83:79cb2ba44b66 55 #endif
AntonLS 12:6d313d575f84 56
elmbed 76:6638d83939d5 57 static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network
AntonLS 0:28ca4562fe1a 58
andriym 78:43a6b54f0372 59 /////////////////////////////////// FUNCTIONS ///////////////////////////////////
andriym 78:43a6b54f0372 60
andriym 81:3cd7de5d01ef 61 void beep(float period, float time, uint8_t vol) {
andriym 78:43a6b54f0372 62 #ifdef ENABLE_SOUND
andriym 81:3cd7de5d01ef 63 if(!vol) return;
andriym 79:9db29f41dc9d 64 #ifdef NORDIC
andriym 81:3cd7de5d01ef 65 if(vol>=3) {
andriym 88:d3f0394da5aa 66 #ifndef HARD_V2
andriym 88:d3f0394da5aa 67 buzzLow = LOW_ON;
andriym 88:d3f0394da5aa 68 #endif
andriym 88:d3f0394da5aa 69 buzzMed = MED_OFF; buzzHigh = HIGH_OFF;
andriym 81:3cd7de5d01ef 70 } else if(vol>=2) {
andriym 88:d3f0394da5aa 71 #ifndef HARD_V2
andriym 88:d3f0394da5aa 72 buzzLow = LOW_OFF;
andriym 88:d3f0394da5aa 73 #endif
andriym 88:d3f0394da5aa 74 buzzMed = MED_ON; buzzHigh = HIGH_OFF;
andriym 79:9db29f41dc9d 75 } else {
andriym 88:d3f0394da5aa 76 #ifndef HARD_V2
andriym 88:d3f0394da5aa 77 buzzLow = LOW_OFF;
andriym 88:d3f0394da5aa 78 #endif
andriym 88:d3f0394da5aa 79 buzzMed = MED_OFF; buzzHigh = HIGH_ON;
andriym 79:9db29f41dc9d 80 }
andriym 80:8d4f190bd253 81 buzzer = 0; // P-MOSFET
andriym 80:8d4f190bd253 82 wait(time);
andriym 79:9db29f41dc9d 83 buzzer = 1;
andriym 79:9db29f41dc9d 84 #else
andriym 78:43a6b54f0372 85 speaker.period(period);
andriym 81:3cd7de5d01ef 86 if(vol>=3)
andriym 81:3cd7de5d01ef 87 speaker = 0.5; //50% duty cycle - max volume
andriym 81:3cd7de5d01ef 88 else if(vol>=2)
andriym 81:3cd7de5d01ef 89 speaker = 0.33;
andriym 81:3cd7de5d01ef 90 else
andriym 81:3cd7de5d01ef 91 speaker = 0.2;
andriym 78:43a6b54f0372 92 wait(time);
andriym 78:43a6b54f0372 93 speaker=0.0; // turn off audio
andriym 78:43a6b54f0372 94 #endif
andriym 79:9db29f41dc9d 95 #endif
andriym 78:43a6b54f0372 96 }
andriym 78:43a6b54f0372 97
andriym 78:43a6b54f0372 98 void binary_sound(int z) {
andriym 78:43a6b54f0372 99 // Beeps numbers according to their binary form
andriym 78:43a6b54f0372 100 // (used for debugging in display-less and serial-less environments)
andriym 78:43a6b54f0372 101 #ifdef ENABLE_SOUND
andriym 79:9db29f41dc9d 102 #ifndef NORDIC
andriym 78:43a6b54f0372 103 speaker.period(0.004);
andriym 78:43a6b54f0372 104 while(z) {
andriym 78:43a6b54f0372 105 speaker = 0.5;
andriym 78:43a6b54f0372 106 if(z&1) wait(0.5);
andriym 78:43a6b54f0372 107 else wait(0.25);
andriym 78:43a6b54f0372 108 speaker = 0.0;
andriym 78:43a6b54f0372 109 wait(1.0);
andriym 78:43a6b54f0372 110 z >>= 1;
andriym 78:43a6b54f0372 111 }
andriym 81:3cd7de5d01ef 112 beep(NOTE_A4, 1.0, 2);
andriym 79:9db29f41dc9d 113 #endif
andriym 78:43a6b54f0372 114 #endif
andriym 78:43a6b54f0372 115 }
andriym 78:43a6b54f0372 116
andriym 78:43a6b54f0372 117 void generate_name(char rand_name[], uint8_t size) {
andriym 78:43a6b54f0372 118 // Generate random name on the 62 character alphabet
andriym 78:43a6b54f0372 119 memset(rand_name, 0x00, size);
andriym 78:43a6b54f0372 120 char alph[63] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
andriym 78:43a6b54f0372 121 uint32_t seed = ((ain.read_u16()+3)/17)*(radio.readTemperature(-1)+37)*((ain.read_u16()+5)/13);
andriym 78:43a6b54f0372 122 srand(seed);
andriym 78:43a6b54f0372 123 for(int i=0;i<size-1;i++) {
andriym 78:43a6b54f0372 124 rand_name[i] = alph[abs(rand()) % 62];
andriym 78:43a6b54f0372 125 }
andriym 78:43a6b54f0372 126 }
andriym 78:43a6b54f0372 127
andriym 86:2a569d9e67f5 128 void spaceLEDs(int level) {
andriym 85:4692a1790cfa 129 if(level<=0) {
andriym 85:4692a1790cfa 130 ledSpace5 = 1; ledSpace10 = 0; ledSpace15 = 0; ledSpace20 = 0;
andriym 85:4692a1790cfa 131 } else if(level<=1) {
andriym 85:4692a1790cfa 132 ledSpace5 = 0; ledSpace10 = 1; ledSpace15 = 0; ledSpace20 = 0;
andriym 85:4692a1790cfa 133 } else if(level<=2) {
andriym 85:4692a1790cfa 134 ledSpace5 = 0; ledSpace10 = 0; ledSpace15 = 1; ledSpace20 = 0;
andriym 85:4692a1790cfa 135 } else {
andriym 85:4692a1790cfa 136 ledSpace5 = 0; ledSpace10 = 0; ledSpace15 = 0; ledSpace20 = 1;
andriym 85:4692a1790cfa 137 }
andriym 85:4692a1790cfa 138 }
andriym 85:4692a1790cfa 139
andriym 89:425369a595c4 140 #ifdef BLE_ENABLE
andriym 89:425369a595c4 141 /* Optional: Restart advertising when peer disconnects */
andriym 89:425369a595c4 142 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
andriym 89:425369a595c4 143 {
andriym 89:425369a595c4 144 BLE::Instance().gap().startAdvertising();
andriym 89:425369a595c4 145 }
andriym 89:425369a595c4 146 /**
andriym 89:425369a595c4 147 * This function is called when the ble initialization process has failed
andriym 89:425369a595c4 148 */
andriym 89:425369a595c4 149 void onBleInitError(BLE &ble, ble_error_t error)
andriym 89:425369a595c4 150 {
andriym 89:425369a595c4 151 /* Avoid compiler warnings */
andriym 89:425369a595c4 152 (void) ble;
andriym 89:425369a595c4 153 (void) error;
andriym 89:425369a595c4 154
andriym 89:425369a595c4 155 /* Initialization error handling should go here */
andriym 89:425369a595c4 156 }
andriym 89:425369a595c4 157
andriym 89:425369a595c4 158 /**
andriym 89:425369a595c4 159 * Callback triggered when the ble initialization process has finished
andriym 89:425369a595c4 160 */
andriym 89:425369a595c4 161 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andriym 89:425369a595c4 162 {
andriym 89:425369a595c4 163 BLE& ble = params->ble;
andriym 89:425369a595c4 164 ble_error_t error = params->error;
andriym 89:425369a595c4 165
andriym 89:425369a595c4 166 if (error != BLE_ERROR_NONE) {
andriym 89:425369a595c4 167 /* In case of error, forward the error handling to onBleInitError */
andriym 89:425369a595c4 168 onBleInitError(ble, error);
andriym 89:425369a595c4 169 return;
andriym 89:425369a595c4 170 }
andriym 89:425369a595c4 171
andriym 89:425369a595c4 172 /* Ensure that it is the default instance of BLE */
andriym 89:425369a595c4 173 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andriym 89:425369a595c4 174 return;
andriym 89:425369a595c4 175 }
andriym 89:425369a595c4 176
andriym 89:425369a595c4 177 /* Set device name characteristic data */
andriym 89:425369a595c4 178 ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
andriym 89:425369a595c4 179
andriym 89:425369a595c4 180 /* Optional: add callback for disconnection */
andriym 89:425369a595c4 181 ble.gap().onDisconnection(disconnectionCallback);
andriym 89:425369a595c4 182
andriym 89:425369a595c4 183 /* Sacrifice 3B of 31B to Advertising Flags */
andriym 89:425369a595c4 184 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
andriym 89:425369a595c4 185 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andriym 89:425369a595c4 186
andriym 89:425369a595c4 187 /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
andriym 89:425369a595c4 188 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
andriym 89:425369a595c4 189
andriym 89:425369a595c4 190 /* Optional: Add name to device */
andriym 89:425369a595c4 191 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andriym 89:425369a595c4 192
andriym 89:425369a595c4 193 /* Set advertising interval. Longer interval == longer battery life */
andriym 89:425369a595c4 194 ble.gap().setAdvertisingInterval(100); /* 100ms */
andriym 89:425369a595c4 195
andriym 89:425369a595c4 196 /* Start advertising */
andriym 89:425369a595c4 197 ble.gap().startAdvertising();
andriym 89:425369a595c4 198 }
andriym 89:425369a595c4 199 #endif // BLE_ENABLE
andriym 89:425369a595c4 200
andriym 78:43a6b54f0372 201 /////////////////////////////////// CLASSES ///////////////////////////////////
andriym 78:43a6b54f0372 202
andriym 78:43a6b54f0372 203 struct Contact {
andriym 78:43a6b54f0372 204 int16_t rssi;
andriym 78:43a6b54f0372 205 uint32_t time_ms;
andriym 78:43a6b54f0372 206 };
andriym 78:43a6b54f0372 207
andriym 78:43a6b54f0372 208 class Player {
andriym 78:43a6b54f0372 209 public:
andriym 78:43a6b54f0372 210 char name[NAME_LEN+1];
andriym 78:43a6b54f0372 211 int8_t team;
andriym 78:43a6b54f0372 212 Player(const char rand_name[]) {
andriym 78:43a6b54f0372 213 memset(name, 0x00, NAME_LEN+1);
andriym 78:43a6b54f0372 214 memcpy(name, rand_name, NAME_LEN);
andriym 78:43a6b54f0372 215 }
andriym 78:43a6b54f0372 216 void update(int8_t _team, int rssi, uint32_t time=NULL) {
andriym 78:43a6b54f0372 217 // Remember this contact
andriym 78:43a6b54f0372 218 team = _team;
andriym 78:43a6b54f0372 219 Contact c;
andriym 78:43a6b54f0372 220 c.rssi = rssi;
andriym 89:425369a595c4 221 c.time_ms = (time!=NULL) ? time : tmr.read_ms();
andriym 78:43a6b54f0372 222 contacts.push_front(c);
andriym 78:43a6b54f0372 223
andriym 78:43a6b54f0372 224 // Cleanup
andriym 78:43a6b54f0372 225 while(contacts.size() > MAX_RECORDS) contacts.pop_back();
andriym 78:43a6b54f0372 226 while(!contacts.empty() && c.time_ms - contacts.back().time_ms > MAX_HISTORY_MS) contacts.pop_back();
andriym 78:43a6b54f0372 227 }
andriym 78:43a6b54f0372 228 int16_t get_distance(int &cnt) {
andriym 78:43a6b54f0372 229 // Find max RSSI
andriym 89:425369a595c4 230 uint32_t cur_time = tmr.read_ms();
andriym 78:43a6b54f0372 231 int16_t max_rssi=-128;
andriym 78:43a6b54f0372 232 cnt=0;
andriym 78:43a6b54f0372 233 for (list<Contact>::iterator it=contacts.begin(); it != contacts.end(); ++it) {
andriym 78:43a6b54f0372 234 if(cur_time - it->time_ms > SIGNALS_VALID_MS) break;
andriym 78:43a6b54f0372 235 if(it->rssi > max_rssi) max_rssi = it->rssi;
andriym 78:43a6b54f0372 236 cnt++;
andriym 78:43a6b54f0372 237 }
andriym 78:43a6b54f0372 238 return max_rssi;
andriym 78:43a6b54f0372 239 }
andriym 78:43a6b54f0372 240 inline bool operator==(const char rhs[]){ return memcmp(name, rhs, NAME_LEN)==0; }
andriym 78:43a6b54f0372 241 inline bool operator==(const Player& rhs){ return *this==rhs.name; }
andriym 78:43a6b54f0372 242 //inline bool operator!=(const char rhs[]){ return !(*this == rhs); }
andriym 78:43a6b54f0372 243 private:
andriym 78:43a6b54f0372 244 list<Contact> contacts;
andriym 78:43a6b54f0372 245 };
andriym 78:43a6b54f0372 246
andriym 78:43a6b54f0372 247 class Players {
andriym 78:43a6b54f0372 248 public:
andriym 78:43a6b54f0372 249 list<Player> players;
andriym 78:43a6b54f0372 250
andriym 78:43a6b54f0372 251 void update(const char name[], int8_t team, int16_t rssi, uint32_t time_ms=NULL) {
andriym 78:43a6b54f0372 252 list<Player>::iterator it;
andriym 78:43a6b54f0372 253 for (it=players.begin(); it != players.end(); ++it) {
andriym 78:43a6b54f0372 254 if(*it==name) break;
andriym 78:43a6b54f0372 255 }
andriym 78:43a6b54f0372 256 if(it!=players.end()) {
andriym 78:43a6b54f0372 257 it->update(team, rssi, time_ms);
andriym 78:43a6b54f0372 258 } else {
andriym 78:43a6b54f0372 259 Player p(name);
andriym 78:43a6b54f0372 260 p.update(team, rssi, time_ms);
andriym 78:43a6b54f0372 261 players.push_front(p);
andriym 78:43a6b54f0372 262 }
andriym 78:43a6b54f0372 263 }
andriym 78:43a6b54f0372 264
andriym 81:3cd7de5d01ef 265 void showAll(int8_t team, uint8_t level, uint8_t volume) {
andriym 79:9db29f41dc9d 266 // Output the current state to the user:
andriym 79:9db29f41dc9d 267 // - show the current information table and/or
andriym 79:9db29f41dc9d 268 // - beep if the user is too close to another one
andriym 79:9db29f41dc9d 269 int i=0, nContacts, signal, maxTeamSignal = -128;
andriym 78:43a6b54f0372 270 list<Player>::iterator it;
andriym 78:43a6b54f0372 271 out.clear();
andriym 78:43a6b54f0372 272 for (it=players.begin(); it != players.end(); ++it) {
andriym 79:9db29f41dc9d 273 signal = it->get_distance(nContacts);
andriym 79:9db29f41dc9d 274 if(signal>-128) {
andriym 78:43a6b54f0372 275 out.printf("%d ", ++i);
andriym 78:43a6b54f0372 276 out.printf((team==it->team) ? "+" : "-"); // teammate or opponent?
andriym 78:43a6b54f0372 277 out.printf("%s ", it->name);
andriym 79:9db29f41dc9d 278 out.printf("%d/%d", -signal, nContacts);
andriym 79:9db29f41dc9d 279 out.printf((-signal<SPACE[level]) ? "!" : " ");
andriym 79:9db29f41dc9d 280 out.printf("\r\n");
andriym 79:9db29f41dc9d 281 }
andriym 79:9db29f41dc9d 282 if(team==it->team && signal>maxTeamSignal) {
andriym 79:9db29f41dc9d 283 maxTeamSignal = signal;
andriym 78:43a6b54f0372 284 }
andriym 78:43a6b54f0372 285 }
andriym 79:9db29f41dc9d 286 //if(!i) {
andriym 79:9db29f41dc9d 287 // out.printf("Nobody around\r\n");
andriym 81:3cd7de5d01ef 288 // beep(NOTE_A5,0.5,volume);
andriym 79:9db29f41dc9d 289 //}
andriym 79:9db29f41dc9d 290 if(-maxTeamSignal<SPACE[level]) {
andriym 81:3cd7de5d01ef 291 beep(NOTE_A4, 0.33, volume);
andriym 78:43a6b54f0372 292 }
andriym 78:43a6b54f0372 293 }
andriym 78:43a6b54f0372 294 };
andriym 78:43a6b54f0372 295
andriym 78:43a6b54f0372 296 /////////////////////////////////// MAIN CODE ///////////////////////////////////
andriym 78:43a6b54f0372 297
elmbed 76:6638d83939d5 298 int main()
andriym 78:43a6b54f0372 299 {
elmbed 76:6638d83939d5 300 char tx_buff[100] = {0};
elmbed 76:6638d83939d5 301 char rx_buff[100] = {0};
andriym 78:43a6b54f0372 302 char rand_name[NAME_LEN+1] = {0};
andriym 78:43a6b54f0372 303 char other_name[NAME_LEN+1] = {0};
andriym 79:9db29f41dc9d 304 int8_t myTeam = 1, otherTeam;
andriym 87:a9870d513fcf 305 uint8_t level = 1, volume = 1; // SPACE10, VOL_LOW
andriym 89:425369a595c4 306 int bTeamNew, bTeamOld, bSpaceOld, bSpaceNew, bVMNew, bVLNew, bVMOld, bVLOld;
elmbed 76:6638d83939d5 307
andriym 89:425369a595c4 308 // Blink all the space LEDs at startup
andriym 89:425369a595c4 309 spaceLEDs(0); wait(0.2); spaceLEDs(1); wait(0.2); spaceLEDs(2); wait(0.2); spaceLEDs(3); wait(0.2);
andriym 89:425369a595c4 310 // ...and go to a single LED
andriym 89:425369a595c4 311 spaceLEDs(level);
andriym 89:425369a595c4 312
andriym 89:425369a595c4 313 // Beep on startup
andriym 81:3cd7de5d01ef 314 ledBuzzer = volume ? 1 : 0;
andriym 81:3cd7de5d01ef 315 beep(NOTE_A5, 0.5, volume);
andriym 78:43a6b54f0372 316
andriym 89:425369a595c4 317 // Turn on the team LED
andriym 89:425369a595c4 318 ledTeamA = myTeam & 1;
andriym 89:425369a595c4 319 ledTeamB = ~myTeam & 1;
andriym 89:425369a595c4 320
andriym 89:425369a595c4 321 // Buttons initialization
andriym 79:9db29f41dc9d 322 buttonSpace.mode(PullDown);
andriym 81:3cd7de5d01ef 323 buttonVolMore.mode(PullDown);
andriym 81:3cd7de5d01ef 324 buttonVolLess.mode(PullDown);
andriym 79:9db29f41dc9d 325 #ifdef NORDIC
andriym 79:9db29f41dc9d 326 buttonTeam.mode(PullDown);
andriym 89:425369a595c4 327 bTeamOld = 0;
andriym 79:9db29f41dc9d 328 #else
andriym 79:9db29f41dc9d 329 buttonTeam.mode(PullUp);
andriym 89:425369a595c4 330 bTeamOld = 1;
andriym 79:9db29f41dc9d 331 #endif
andriym 89:425369a595c4 332 bSpaceOld = 0;
andriym 89:425369a595c4 333 bVMOld = 0;
andriym 89:425369a595c4 334 bVLOld = 0;
elmbed 76:6638d83939d5 335
andriym 89:425369a595c4 336 #ifdef BLE_ENABLE
andriym 89:425369a595c4 337 // BLE initialization
andriym 89:425369a595c4 338 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andriym 89:425369a595c4 339 ble.init(bleInitComplete); // Initialize BLE baselayer
andriym 89:425369a595c4 340 #endif // BLE_ENABLE
andriym 89:425369a595c4 341
andriym 89:425369a595c4 342 // Pick node number
andriym 78:43a6b54f0372 343 char this_node = int(ain.read()*255+17)*int(ain.read()*255+11); // random node value
andriym 78:43a6b54f0372 344 out.printf("Node: %d\r\n", this_node);
andriym 78:43a6b54f0372 345
andriym 89:425369a595c4 346 // Initialize the radio
andriym 78:43a6b54f0372 347 radio.initialize(FREQUENCY, this_node, NETWORKID);
andriym 78:43a6b54f0372 348 #ifdef HIGH_POWER
andriym 78:43a6b54f0372 349 radio.setHighPower(true);
andriym 78:43a6b54f0372 350 #endif
elmbed 76:6638d83939d5 351 radio.encrypt(0);
elmbed 76:6638d83939d5 352 radio.promiscuous(promiscuousMode);
andriym 85:4692a1790cfa 353 if(FREQUENCY == RF69_868MHZ)
andriym 85:4692a1790cfa 354 radio.setFrequency(868000000);
andriym 85:4692a1790cfa 355 else if(FREQUENCY == RF69_915MHZ)
andriym 85:4692a1790cfa 356 radio.setFrequency(915000000);
AntonLS 11:d3aa5fca2330 357
andriym 89:425369a595c4 358 // Pick node name
andriym 78:43a6b54f0372 359 generate_name(rand_name, sizeof(rand_name));
andriym 78:43a6b54f0372 360 out.printf("Name: %s\r\n", rand_name);
andriym 78:43a6b54f0372 361 out.sleep(2.0);
andriym 78:43a6b54f0372 362
andriym 89:425369a595c4 363 tmr.start();
andriym 78:43a6b54f0372 364
andriym 78:43a6b54f0372 365 Players players;
andriym 78:43a6b54f0372 366
andriym 89:425369a595c4 367 uint32_t last_send = tmr.read_ms();
andriym 89:425369a595c4 368 uint32_t last_shown = tmr.read_ms();
andriym 78:43a6b54f0372 369 //uint32_t min_wait = SEND_RATE_MS - 0.5*SEND_RATE_MS*SEND_DESYNC;
andriym 78:43a6b54f0372 370 //uint32_t send_wait = min_wait;
AntonLS 49:626e84ce5e52 371
elmbed 76:6638d83939d5 372 while (true)
AntonLS 19:afcbb425b3cf 373 {
andriym 79:9db29f41dc9d 374 // Read team button
andriym 79:9db29f41dc9d 375 bTeamNew = buttonTeam;
andriym 79:9db29f41dc9d 376 if(bTeamNew==PRESSED && bTeamOld==RELEASED) {
andriym 79:9db29f41dc9d 377 myTeam = (myTeam==1) ? 2 : 1;
andriym 79:9db29f41dc9d 378 ledTeamA = myTeam & 1;
andriym 79:9db29f41dc9d 379 ledTeamB = ~myTeam & 1;
andriym 78:43a6b54f0372 380 out.clear();
andriym 79:9db29f41dc9d 381 out.printf("New team: %d\r\n", myTeam);
andriym 78:43a6b54f0372 382 out.sleep(2);
andriym 78:43a6b54f0372 383 }
andriym 79:9db29f41dc9d 384 bTeamOld = bTeamNew;
andriym 79:9db29f41dc9d 385
andriym 79:9db29f41dc9d 386 // Read space button
andriym 79:9db29f41dc9d 387 bSpaceNew = buttonSpace;
andriym 79:9db29f41dc9d 388 if(bSpaceNew && !bSpaceOld) {
andriym 79:9db29f41dc9d 389 level = (level+1) & 0b11; // four states
andriym 85:4692a1790cfa 390 spaceLEDs(level);
andriym 79:9db29f41dc9d 391 out.clear();
andriym 79:9db29f41dc9d 392 out.printf("New level: %d\r\n", level);
andriym 79:9db29f41dc9d 393 out.sleep(2);
andriym 79:9db29f41dc9d 394 }
andriym 79:9db29f41dc9d 395 bSpaceOld = bSpaceNew;
andriym 81:3cd7de5d01ef 396
andriym 81:3cd7de5d01ef 397 // Read volume buttons
andriym 82:1e552cc1a615 398 bVMNew = buttonVolMore.read();
andriym 82:1e552cc1a615 399 bVLNew = buttonVolLess.read();
andriym 81:3cd7de5d01ef 400 if(bVMNew && !bVMOld) {
andriym 81:3cd7de5d01ef 401 volume++;
andriym 81:3cd7de5d01ef 402 if(volume>3) volume=3;
andriym 81:3cd7de5d01ef 403 ledBuzzer = 1;
andriym 81:3cd7de5d01ef 404 out.clear();
andriym 81:3cd7de5d01ef 405 out.printf("New volume: %d\r\n", volume);
andriym 81:3cd7de5d01ef 406 out.sleep(2);
andriym 81:3cd7de5d01ef 407 }
andriym 81:3cd7de5d01ef 408 if(bVLNew && !bVLOld) {
andriym 81:3cd7de5d01ef 409 if(volume>0) volume--;
andriym 81:3cd7de5d01ef 410 if(!volume) ledBuzzer = 0;
andriym 81:3cd7de5d01ef 411 out.clear();
andriym 81:3cd7de5d01ef 412 out.printf("New volume: %d\r\n", volume);
andriym 81:3cd7de5d01ef 413 out.sleep(2);
andriym 81:3cd7de5d01ef 414 }
andriym 81:3cd7de5d01ef 415 bVMOld = bVMNew;
andriym 81:3cd7de5d01ef 416 bVLOld = bVLNew;
andriym 81:3cd7de5d01ef 417
andriym 81:3cd7de5d01ef 418 // Output
andriym 89:425369a595c4 419 unsigned long current_time = tmr.read_ms();
elmbed 76:6638d83939d5 420
andriym 79:9db29f41dc9d 421 if (current_time - last_shown > CYCLE_MS)
andriym 78:43a6b54f0372 422 {
andriym 81:3cd7de5d01ef 423 players.showAll(myTeam, level, volume);
andriym 78:43a6b54f0372 424 last_shown = current_time;
andriym 78:43a6b54f0372 425 }
andriym 78:43a6b54f0372 426
andriym 81:3cd7de5d01ef 427 // Radios
andriym 78:43a6b54f0372 428 if (current_time - last_send > SEND_RATE_MS)
AntonLS 19:afcbb425b3cf 429 {
elmbed 76:6638d83939d5 430 // Send message
andriym 79:9db29f41dc9d 431 snprintf(tx_buff, sizeof(tx_buff), "N=%s,%d", rand_name, myTeam);
andriym 78:43a6b54f0372 432 radio.send(EVERY_NODE, tx_buff, strlen(tx_buff));
andriym 78:43a6b54f0372 433 last_send = current_time;
andriym 78:43a6b54f0372 434 //send_wait = min_wait + (rand() % SEND_RATE_MS)*SEND_DESYNC;
andriym 78:43a6b54f0372 435
andriym 89:425369a595c4 436 //// Some debugging info I used to display
andriym 78:43a6b54f0372 437 //out.clear();
andriym 78:43a6b54f0372 438 //out.printf("Sent: %s\r\n", tx_buff);
andriym 78:43a6b54f0372 439 //uint8_t tempC = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient
andriym 78:43a6b54f0372 440 //uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3; // LNA Current Gain
andriym 78:43a6b54f0372 441 //uint32_t freq = radio.getFrequency();
andriym 78:43a6b54f0372 442 //out.printf("T: %d, G: %d\r\n", tempC, gain);
andriym 78:43a6b54f0372 443 //if(freq!=868000000) out.printf("Freq: %d\r\n", freq);
elmbed 76:6638d83939d5 444
andriym 81:3cd7de5d01ef 445 //beep(NOTE_A3, 0.05, volume);
elmbed 76:6638d83939d5 446 }
andriym 78:43a6b54f0372 447
elmbed 76:6638d83939d5 448 if (radio.receiveDone())
AntonLS 11:d3aa5fca2330 449 {
elmbed 76:6638d83939d5 450 memset(rx_buff, 0x00, sizeof(rx_buff));
elmbed 76:6638d83939d5 451 memcpy(rx_buff, (char*)radio.DATA, radio.DATALEN > sizeof(rx_buff)-1 ? sizeof(rx_buff)-1 : radio.DATALEN);
elmbed 76:6638d83939d5 452
andriym 78:43a6b54f0372 453 if(rx_buff[0]=='N' && rx_buff[1]=='=') {
andriym 78:43a6b54f0372 454 memcpy(other_name, rx_buff+2, NAME_LEN);
andriym 78:43a6b54f0372 455 other_name[5] = 0x00;
andriym 78:43a6b54f0372 456 if(sizeof(rx_buff)>8 && rx_buff[7]==',') {
andriym 79:9db29f41dc9d 457 otherTeam = rx_buff[8]-'0';
andriym 79:9db29f41dc9d 458 } else otherTeam = 1;
andriym 89:425369a595c4 459 players.update(other_name, otherTeam, radio.RSSI, tmr.read_ms());
andriym 78:43a6b54f0372 460
andriym 78:43a6b54f0372 461 //uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3; // LNA Current Gain
andriym 78:43a6b54f0372 462 //out.clear();
andriym 78:43a6b54f0372 463 //out.printf("Other: %s\r\n", other_name);
andriym 78:43a6b54f0372 464 //out.printf("RSSI: %d, G: %d\r\n", radio.RSSI, gain);
andriym 78:43a6b54f0372 465
andriym 81:3cd7de5d01ef 466 //beep(NOTE_A5, 0.5, volume);
andriym 78:43a6b54f0372 467 } else { // received unknown signal
andriym 78:43a6b54f0372 468 uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3; // LNA Current Gain
andriym 78:43a6b54f0372 469 out.clear();
andriym 78:43a6b54f0372 470 out.printf("Got: %s\r\n", rx_buff);
andriym 78:43a6b54f0372 471 out.printf("RSSI: %d, G: %d\r\n", radio.RSSI, gain);
andriym 78:43a6b54f0372 472 out.sleep(2.0);
andriym 78:43a6b54f0372 473 }
AntonLS 11:d3aa5fca2330 474 }
andriym 89:425369a595c4 475
andriym 89:425369a595c4 476 #ifdef BLE_ENABLE
andriym 89:425369a595c4 477 ble.waitForEvent();
andriym 89:425369a595c4 478 #endif
AntonLS 0:28ca4562fe1a 479 }
elmbed 76:6638d83939d5 480 }