Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
main.cpp@88:d3f0394da5aa, 2016-06-07 (annotated)
- Committer:
- andriym
- Date:
- Tue Jun 07 08:43:21 2016 +0000
- Revision:
- 88:d3f0394da5aa
- Parent:
- 87:a9870d513fcf
- Child:
- 89:425369a595c4
Took away BUZZ_LOW for v2 hardware.
Who changed what in which revision?
User | Revision | Line number | New 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> |
AntonLS | 0:28ca4562fe1a | 5 | |
andriym | 78:43a6b54f0372 | 6 | using namespace std; |
andriym | 78:43a6b54f0372 | 7 | |
andriym | 78:43a6b54f0372 | 8 | Timer t; |
andriym | 78:43a6b54f0372 | 9 | Output out; |
elmbed | 17:d8b901d791fd | 10 | |
andriym | 78:43a6b54f0372 | 11 | /////////////////////////////////// PINS /////////////////////////////////// |
andriym | 79:9db29f41dc9d | 12 | |
andriym | 82:1e552cc1a615 | 13 | DebounceIn buttonTeam(BUT_TEAM); |
andriym | 82:1e552cc1a615 | 14 | DebounceIn buttonSpace(BUT_SPACE); |
andriym | 82:1e552cc1a615 | 15 | DebounceIn buttonVolMore(BUT_VOL_MORE); |
andriym | 82:1e552cc1a615 | 16 | DebounceIn buttonVolLess(BUT_VOL_LESS); |
andriym | 81:3cd7de5d01ef | 17 | |
andriym | 79:9db29f41dc9d | 18 | DigitalOut ledTeamA(LED_TEAM_A); |
andriym | 79:9db29f41dc9d | 19 | DigitalOut ledTeamB(LED_TEAM_B); |
andriym | 79:9db29f41dc9d | 20 | DigitalOut ledSpace5(LED_SPACE5); |
andriym | 79:9db29f41dc9d | 21 | DigitalOut ledSpace10(LED_SPACE10); |
andriym | 79:9db29f41dc9d | 22 | DigitalOut ledSpace15(LED_SPACE15); |
andriym | 79:9db29f41dc9d | 23 | DigitalOut ledSpace20(LED_SPACE20); |
andriym | 81:3cd7de5d01ef | 24 | DigitalOut ledBuzzer(LED_BUZZER_ON); |
andriym | 81:3cd7de5d01ef | 25 | |
andriym | 79:9db29f41dc9d | 26 | AnalogIn ain(ANALOG_IN); // used for randomizing |
andriym | 78:43a6b54f0372 | 27 | #ifdef NORDIC |
andriym | 79:9db29f41dc9d | 28 | DigitalOut buzzer(BUZZER); |
andriym | 88:d3f0394da5aa | 29 | #ifndef HARD_V2 |
andriym | 79:9db29f41dc9d | 30 | DigitalOut buzzLow(BUZZ_LOW); |
andriym | 88:d3f0394da5aa | 31 | #endif |
andriym | 79:9db29f41dc9d | 32 | DigitalOut buzzMed(BUZZ_MED); |
andriym | 79:9db29f41dc9d | 33 | DigitalOut buzzHigh(BUZZ_HIGH); |
andriym | 79:9db29f41dc9d | 34 | #else |
andriym | 79:9db29f41dc9d | 35 | PwmOut speaker(BUZZER); // passive buzzer |
andriym | 79:9db29f41dc9d | 36 | #endif |
AntonLS | 30:c60b0d52b067 | 37 | |
andriym | 78:43a6b54f0372 | 38 | /////////////////////////////////// RADIO /////////////////////////////////// |
andriym | 83:79cb2ba44b66 | 39 | #ifdef ENABLE_PIN |
andriym | 83:79cb2ba44b66 | 40 | static RFM69 radio(RFM_MOSI, RFM_MISO, RFM_SCK, RFM_SS, RFM_IRQ, RFM_ISM_EN); |
andriym | 83:79cb2ba44b66 | 41 | #else |
andriym | 79:9db29f41dc9d | 42 | static RFM69 radio(RFM_MOSI, RFM_MISO, RFM_SCK, RFM_SS, RFM_IRQ); |
andriym | 83:79cb2ba44b66 | 43 | #endif |
AntonLS | 12:6d313d575f84 | 44 | |
elmbed | 76:6638d83939d5 | 45 | static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network |
AntonLS | 0:28ca4562fe1a | 46 | |
andriym | 78:43a6b54f0372 | 47 | /////////////////////////////////// FUNCTIONS /////////////////////////////////// |
andriym | 78:43a6b54f0372 | 48 | |
andriym | 81:3cd7de5d01ef | 49 | void beep(float period, float time, uint8_t vol) { |
andriym | 78:43a6b54f0372 | 50 | #ifdef ENABLE_SOUND |
andriym | 81:3cd7de5d01ef | 51 | if(!vol) return; |
andriym | 79:9db29f41dc9d | 52 | #ifdef NORDIC |
andriym | 81:3cd7de5d01ef | 53 | if(vol>=3) { |
andriym | 88:d3f0394da5aa | 54 | #ifndef HARD_V2 |
andriym | 88:d3f0394da5aa | 55 | buzzLow = LOW_ON; |
andriym | 88:d3f0394da5aa | 56 | #endif |
andriym | 88:d3f0394da5aa | 57 | buzzMed = MED_OFF; buzzHigh = HIGH_OFF; |
andriym | 81:3cd7de5d01ef | 58 | } else if(vol>=2) { |
andriym | 88:d3f0394da5aa | 59 | #ifndef HARD_V2 |
andriym | 88:d3f0394da5aa | 60 | buzzLow = LOW_OFF; |
andriym | 88:d3f0394da5aa | 61 | #endif |
andriym | 88:d3f0394da5aa | 62 | buzzMed = MED_ON; buzzHigh = HIGH_OFF; |
andriym | 79:9db29f41dc9d | 63 | } else { |
andriym | 88:d3f0394da5aa | 64 | #ifndef HARD_V2 |
andriym | 88:d3f0394da5aa | 65 | buzzLow = LOW_OFF; |
andriym | 88:d3f0394da5aa | 66 | #endif |
andriym | 88:d3f0394da5aa | 67 | buzzMed = MED_OFF; buzzHigh = HIGH_ON; |
andriym | 79:9db29f41dc9d | 68 | } |
andriym | 80:8d4f190bd253 | 69 | buzzer = 0; // P-MOSFET |
andriym | 80:8d4f190bd253 | 70 | wait(time); |
andriym | 79:9db29f41dc9d | 71 | buzzer = 1; |
andriym | 79:9db29f41dc9d | 72 | #else |
andriym | 78:43a6b54f0372 | 73 | speaker.period(period); |
andriym | 81:3cd7de5d01ef | 74 | if(vol>=3) |
andriym | 81:3cd7de5d01ef | 75 | speaker = 0.5; //50% duty cycle - max volume |
andriym | 81:3cd7de5d01ef | 76 | else if(vol>=2) |
andriym | 81:3cd7de5d01ef | 77 | speaker = 0.33; |
andriym | 81:3cd7de5d01ef | 78 | else |
andriym | 81:3cd7de5d01ef | 79 | speaker = 0.2; |
andriym | 78:43a6b54f0372 | 80 | wait(time); |
andriym | 78:43a6b54f0372 | 81 | speaker=0.0; // turn off audio |
andriym | 78:43a6b54f0372 | 82 | #endif |
andriym | 79:9db29f41dc9d | 83 | #endif |
andriym | 78:43a6b54f0372 | 84 | } |
andriym | 78:43a6b54f0372 | 85 | |
andriym | 78:43a6b54f0372 | 86 | void binary_sound(int z) { |
andriym | 78:43a6b54f0372 | 87 | // Beeps numbers according to their binary form |
andriym | 78:43a6b54f0372 | 88 | // (used for debugging in display-less and serial-less environments) |
andriym | 78:43a6b54f0372 | 89 | #ifdef ENABLE_SOUND |
andriym | 79:9db29f41dc9d | 90 | #ifndef NORDIC |
andriym | 78:43a6b54f0372 | 91 | speaker.period(0.004); |
andriym | 78:43a6b54f0372 | 92 | while(z) { |
andriym | 78:43a6b54f0372 | 93 | speaker = 0.5; |
andriym | 78:43a6b54f0372 | 94 | if(z&1) wait(0.5); |
andriym | 78:43a6b54f0372 | 95 | else wait(0.25); |
andriym | 78:43a6b54f0372 | 96 | speaker = 0.0; |
andriym | 78:43a6b54f0372 | 97 | wait(1.0); |
andriym | 78:43a6b54f0372 | 98 | z >>= 1; |
andriym | 78:43a6b54f0372 | 99 | } |
andriym | 81:3cd7de5d01ef | 100 | beep(NOTE_A4, 1.0, 2); |
andriym | 79:9db29f41dc9d | 101 | #endif |
andriym | 78:43a6b54f0372 | 102 | #endif |
andriym | 78:43a6b54f0372 | 103 | } |
andriym | 78:43a6b54f0372 | 104 | |
andriym | 78:43a6b54f0372 | 105 | void generate_name(char rand_name[], uint8_t size) { |
andriym | 78:43a6b54f0372 | 106 | // Generate random name on the 62 character alphabet |
andriym | 78:43a6b54f0372 | 107 | memset(rand_name, 0x00, size); |
andriym | 78:43a6b54f0372 | 108 | char alph[63] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
andriym | 78:43a6b54f0372 | 109 | uint32_t seed = ((ain.read_u16()+3)/17)*(radio.readTemperature(-1)+37)*((ain.read_u16()+5)/13); |
andriym | 78:43a6b54f0372 | 110 | srand(seed); |
andriym | 78:43a6b54f0372 | 111 | for(int i=0;i<size-1;i++) { |
andriym | 78:43a6b54f0372 | 112 | rand_name[i] = alph[abs(rand()) % 62]; |
andriym | 78:43a6b54f0372 | 113 | } |
andriym | 78:43a6b54f0372 | 114 | } |
andriym | 78:43a6b54f0372 | 115 | |
andriym | 86:2a569d9e67f5 | 116 | void spaceLEDs(int level) { |
andriym | 85:4692a1790cfa | 117 | if(level<=0) { |
andriym | 85:4692a1790cfa | 118 | ledSpace5 = 1; ledSpace10 = 0; ledSpace15 = 0; ledSpace20 = 0; |
andriym | 85:4692a1790cfa | 119 | } else if(level<=1) { |
andriym | 85:4692a1790cfa | 120 | ledSpace5 = 0; ledSpace10 = 1; ledSpace15 = 0; ledSpace20 = 0; |
andriym | 85:4692a1790cfa | 121 | } else if(level<=2) { |
andriym | 85:4692a1790cfa | 122 | ledSpace5 = 0; ledSpace10 = 0; ledSpace15 = 1; ledSpace20 = 0; |
andriym | 85:4692a1790cfa | 123 | } else { |
andriym | 85:4692a1790cfa | 124 | ledSpace5 = 0; ledSpace10 = 0; ledSpace15 = 0; ledSpace20 = 1; |
andriym | 85:4692a1790cfa | 125 | } |
andriym | 85:4692a1790cfa | 126 | } |
andriym | 85:4692a1790cfa | 127 | |
andriym | 78:43a6b54f0372 | 128 | /////////////////////////////////// CLASSES /////////////////////////////////// |
andriym | 78:43a6b54f0372 | 129 | |
andriym | 78:43a6b54f0372 | 130 | struct Contact { |
andriym | 78:43a6b54f0372 | 131 | int16_t rssi; |
andriym | 78:43a6b54f0372 | 132 | uint32_t time_ms; |
andriym | 78:43a6b54f0372 | 133 | }; |
andriym | 78:43a6b54f0372 | 134 | |
andriym | 78:43a6b54f0372 | 135 | class Player { |
andriym | 78:43a6b54f0372 | 136 | public: |
andriym | 78:43a6b54f0372 | 137 | char name[NAME_LEN+1]; |
andriym | 78:43a6b54f0372 | 138 | int8_t team; |
andriym | 78:43a6b54f0372 | 139 | Player(const char rand_name[]) { |
andriym | 78:43a6b54f0372 | 140 | memset(name, 0x00, NAME_LEN+1); |
andriym | 78:43a6b54f0372 | 141 | memcpy(name, rand_name, NAME_LEN); |
andriym | 78:43a6b54f0372 | 142 | } |
andriym | 78:43a6b54f0372 | 143 | void update(int8_t _team, int rssi, uint32_t time=NULL) { |
andriym | 78:43a6b54f0372 | 144 | // Remember this contact |
andriym | 78:43a6b54f0372 | 145 | team = _team; |
andriym | 78:43a6b54f0372 | 146 | Contact c; |
andriym | 78:43a6b54f0372 | 147 | c.rssi = rssi; |
andriym | 78:43a6b54f0372 | 148 | c.time_ms = (time!=NULL) ? time : t.read_ms(); |
andriym | 78:43a6b54f0372 | 149 | contacts.push_front(c); |
andriym | 78:43a6b54f0372 | 150 | |
andriym | 78:43a6b54f0372 | 151 | // Cleanup |
andriym | 78:43a6b54f0372 | 152 | while(contacts.size() > MAX_RECORDS) contacts.pop_back(); |
andriym | 78:43a6b54f0372 | 153 | while(!contacts.empty() && c.time_ms - contacts.back().time_ms > MAX_HISTORY_MS) contacts.pop_back(); |
andriym | 78:43a6b54f0372 | 154 | } |
andriym | 78:43a6b54f0372 | 155 | int16_t get_distance(int &cnt) { |
andriym | 78:43a6b54f0372 | 156 | // Find max RSSI |
andriym | 78:43a6b54f0372 | 157 | uint32_t cur_time = t.read_ms(); |
andriym | 78:43a6b54f0372 | 158 | int16_t max_rssi=-128; |
andriym | 78:43a6b54f0372 | 159 | cnt=0; |
andriym | 78:43a6b54f0372 | 160 | for (list<Contact>::iterator it=contacts.begin(); it != contacts.end(); ++it) { |
andriym | 78:43a6b54f0372 | 161 | if(cur_time - it->time_ms > SIGNALS_VALID_MS) break; |
andriym | 78:43a6b54f0372 | 162 | if(it->rssi > max_rssi) max_rssi = it->rssi; |
andriym | 78:43a6b54f0372 | 163 | cnt++; |
andriym | 78:43a6b54f0372 | 164 | } |
andriym | 78:43a6b54f0372 | 165 | return max_rssi; |
andriym | 78:43a6b54f0372 | 166 | } |
andriym | 78:43a6b54f0372 | 167 | inline bool operator==(const char rhs[]){ return memcmp(name, rhs, NAME_LEN)==0; } |
andriym | 78:43a6b54f0372 | 168 | inline bool operator==(const Player& rhs){ return *this==rhs.name; } |
andriym | 78:43a6b54f0372 | 169 | //inline bool operator!=(const char rhs[]){ return !(*this == rhs); } |
andriym | 78:43a6b54f0372 | 170 | private: |
andriym | 78:43a6b54f0372 | 171 | list<Contact> contacts; |
andriym | 78:43a6b54f0372 | 172 | }; |
andriym | 78:43a6b54f0372 | 173 | |
andriym | 78:43a6b54f0372 | 174 | class Players { |
andriym | 78:43a6b54f0372 | 175 | public: |
andriym | 78:43a6b54f0372 | 176 | list<Player> players; |
andriym | 78:43a6b54f0372 | 177 | |
andriym | 78:43a6b54f0372 | 178 | void update(const char name[], int8_t team, int16_t rssi, uint32_t time_ms=NULL) { |
andriym | 78:43a6b54f0372 | 179 | list<Player>::iterator it; |
andriym | 78:43a6b54f0372 | 180 | for (it=players.begin(); it != players.end(); ++it) { |
andriym | 78:43a6b54f0372 | 181 | if(*it==name) break; |
andriym | 78:43a6b54f0372 | 182 | } |
andriym | 78:43a6b54f0372 | 183 | if(it!=players.end()) { |
andriym | 78:43a6b54f0372 | 184 | it->update(team, rssi, time_ms); |
andriym | 78:43a6b54f0372 | 185 | } else { |
andriym | 78:43a6b54f0372 | 186 | Player p(name); |
andriym | 78:43a6b54f0372 | 187 | p.update(team, rssi, time_ms); |
andriym | 78:43a6b54f0372 | 188 | players.push_front(p); |
andriym | 78:43a6b54f0372 | 189 | } |
andriym | 78:43a6b54f0372 | 190 | } |
andriym | 78:43a6b54f0372 | 191 | |
andriym | 81:3cd7de5d01ef | 192 | void showAll(int8_t team, uint8_t level, uint8_t volume) { |
andriym | 79:9db29f41dc9d | 193 | // Output the current state to the user: |
andriym | 79:9db29f41dc9d | 194 | // - show the current information table and/or |
andriym | 79:9db29f41dc9d | 195 | // - beep if the user is too close to another one |
andriym | 79:9db29f41dc9d | 196 | int i=0, nContacts, signal, maxTeamSignal = -128; |
andriym | 78:43a6b54f0372 | 197 | list<Player>::iterator it; |
andriym | 78:43a6b54f0372 | 198 | out.clear(); |
andriym | 78:43a6b54f0372 | 199 | for (it=players.begin(); it != players.end(); ++it) { |
andriym | 79:9db29f41dc9d | 200 | signal = it->get_distance(nContacts); |
andriym | 79:9db29f41dc9d | 201 | if(signal>-128) { |
andriym | 78:43a6b54f0372 | 202 | out.printf("%d ", ++i); |
andriym | 78:43a6b54f0372 | 203 | out.printf((team==it->team) ? "+" : "-"); // teammate or opponent? |
andriym | 78:43a6b54f0372 | 204 | out.printf("%s ", it->name); |
andriym | 79:9db29f41dc9d | 205 | out.printf("%d/%d", -signal, nContacts); |
andriym | 79:9db29f41dc9d | 206 | out.printf((-signal<SPACE[level]) ? "!" : " "); |
andriym | 79:9db29f41dc9d | 207 | out.printf("\r\n"); |
andriym | 79:9db29f41dc9d | 208 | } |
andriym | 79:9db29f41dc9d | 209 | if(team==it->team && signal>maxTeamSignal) { |
andriym | 79:9db29f41dc9d | 210 | maxTeamSignal = signal; |
andriym | 78:43a6b54f0372 | 211 | } |
andriym | 78:43a6b54f0372 | 212 | } |
andriym | 79:9db29f41dc9d | 213 | //if(!i) { |
andriym | 79:9db29f41dc9d | 214 | // out.printf("Nobody around\r\n"); |
andriym | 81:3cd7de5d01ef | 215 | // beep(NOTE_A5,0.5,volume); |
andriym | 79:9db29f41dc9d | 216 | //} |
andriym | 79:9db29f41dc9d | 217 | if(-maxTeamSignal<SPACE[level]) { |
andriym | 81:3cd7de5d01ef | 218 | beep(NOTE_A4, 0.33, volume); |
andriym | 78:43a6b54f0372 | 219 | } |
andriym | 78:43a6b54f0372 | 220 | } |
andriym | 78:43a6b54f0372 | 221 | }; |
andriym | 78:43a6b54f0372 | 222 | |
andriym | 78:43a6b54f0372 | 223 | /////////////////////////////////// MAIN CODE /////////////////////////////////// |
andriym | 78:43a6b54f0372 | 224 | |
elmbed | 76:6638d83939d5 | 225 | int main() |
andriym | 78:43a6b54f0372 | 226 | { |
elmbed | 76:6638d83939d5 | 227 | char tx_buff[100] = {0}; |
elmbed | 76:6638d83939d5 | 228 | char rx_buff[100] = {0}; |
andriym | 78:43a6b54f0372 | 229 | char rand_name[NAME_LEN+1] = {0}; |
andriym | 78:43a6b54f0372 | 230 | char other_name[NAME_LEN+1] = {0}; |
andriym | 79:9db29f41dc9d | 231 | int8_t myTeam = 1, otherTeam; |
andriym | 87:a9870d513fcf | 232 | uint8_t level = 1, volume = 1; // SPACE10, VOL_LOW |
elmbed | 76:6638d83939d5 | 233 | |
andriym | 81:3cd7de5d01ef | 234 | ledBuzzer = volume ? 1 : 0; |
andriym | 81:3cd7de5d01ef | 235 | beep(NOTE_A5, 0.5, volume); |
andriym | 78:43a6b54f0372 | 236 | |
andriym | 79:9db29f41dc9d | 237 | buttonSpace.mode(PullDown); |
andriym | 81:3cd7de5d01ef | 238 | buttonVolMore.mode(PullDown); |
andriym | 81:3cd7de5d01ef | 239 | buttonVolLess.mode(PullDown); |
andriym | 79:9db29f41dc9d | 240 | #ifdef NORDIC |
andriym | 79:9db29f41dc9d | 241 | buttonTeam.mode(PullDown); |
andriym | 79:9db29f41dc9d | 242 | int bTeamOld = 0; |
andriym | 79:9db29f41dc9d | 243 | #else |
andriym | 79:9db29f41dc9d | 244 | buttonTeam.mode(PullUp); |
andriym | 79:9db29f41dc9d | 245 | int bTeamOld = 1; |
andriym | 79:9db29f41dc9d | 246 | #endif |
andriym | 81:3cd7de5d01ef | 247 | int bTeamNew, bSpaceOld=0, bSpaceNew, bVMNew, bVLNew, bVMOld=0, bVLOld=0; |
andriym | 85:4692a1790cfa | 248 | |
andriym | 85:4692a1790cfa | 249 | spaceLEDs(level); |
elmbed | 76:6638d83939d5 | 250 | |
andriym | 78:43a6b54f0372 | 251 | char this_node = int(ain.read()*255+17)*int(ain.read()*255+11); // random node value |
andriym | 78:43a6b54f0372 | 252 | out.printf("Node: %d\r\n", this_node); |
andriym | 78:43a6b54f0372 | 253 | |
andriym | 78:43a6b54f0372 | 254 | // Init radio |
andriym | 78:43a6b54f0372 | 255 | radio.initialize(FREQUENCY, this_node, NETWORKID); |
andriym | 78:43a6b54f0372 | 256 | #ifdef HIGH_POWER |
andriym | 78:43a6b54f0372 | 257 | radio.setHighPower(true); |
andriym | 78:43a6b54f0372 | 258 | #endif |
elmbed | 76:6638d83939d5 | 259 | radio.encrypt(0); |
elmbed | 76:6638d83939d5 | 260 | radio.promiscuous(promiscuousMode); |
andriym | 85:4692a1790cfa | 261 | if(FREQUENCY == RF69_868MHZ) |
andriym | 85:4692a1790cfa | 262 | radio.setFrequency(868000000); |
andriym | 85:4692a1790cfa | 263 | else if(FREQUENCY == RF69_915MHZ) |
andriym | 85:4692a1790cfa | 264 | radio.setFrequency(915000000); |
andriym | 85:4692a1790cfa | 265 | |
AntonLS | 11:d3aa5fca2330 | 266 | |
andriym | 78:43a6b54f0372 | 267 | generate_name(rand_name, sizeof(rand_name)); |
andriym | 78:43a6b54f0372 | 268 | out.printf("Name: %s\r\n", rand_name); |
andriym | 78:43a6b54f0372 | 269 | out.sleep(2.0); |
andriym | 78:43a6b54f0372 | 270 | |
elmbed | 76:6638d83939d5 | 271 | t.start(); |
andriym | 79:9db29f41dc9d | 272 | ledTeamA = myTeam & 1; |
andriym | 79:9db29f41dc9d | 273 | ledTeamB = ~myTeam & 1; |
andriym | 79:9db29f41dc9d | 274 | #ifdef NORDIC |
andriym | 79:9db29f41dc9d | 275 | buzzMed = 1; |
andriym | 79:9db29f41dc9d | 276 | #endif |
andriym | 78:43a6b54f0372 | 277 | |
andriym | 78:43a6b54f0372 | 278 | Players players; |
andriym | 78:43a6b54f0372 | 279 | |
andriym | 78:43a6b54f0372 | 280 | uint32_t last_send = t.read_ms(); |
andriym | 78:43a6b54f0372 | 281 | uint32_t last_shown = t.read_ms(); |
andriym | 78:43a6b54f0372 | 282 | //uint32_t min_wait = SEND_RATE_MS - 0.5*SEND_RATE_MS*SEND_DESYNC; |
andriym | 78:43a6b54f0372 | 283 | //uint32_t send_wait = min_wait; |
AntonLS | 49:626e84ce5e52 | 284 | |
elmbed | 76:6638d83939d5 | 285 | while (true) |
AntonLS | 19:afcbb425b3cf | 286 | { |
andriym | 79:9db29f41dc9d | 287 | // Read team button |
andriym | 79:9db29f41dc9d | 288 | bTeamNew = buttonTeam; |
andriym | 79:9db29f41dc9d | 289 | if(bTeamNew==PRESSED && bTeamOld==RELEASED) { |
andriym | 79:9db29f41dc9d | 290 | myTeam = (myTeam==1) ? 2 : 1; |
andriym | 79:9db29f41dc9d | 291 | ledTeamA = myTeam & 1; |
andriym | 79:9db29f41dc9d | 292 | ledTeamB = ~myTeam & 1; |
andriym | 78:43a6b54f0372 | 293 | out.clear(); |
andriym | 79:9db29f41dc9d | 294 | out.printf("New team: %d\r\n", myTeam); |
andriym | 78:43a6b54f0372 | 295 | out.sleep(2); |
andriym | 78:43a6b54f0372 | 296 | } |
andriym | 79:9db29f41dc9d | 297 | bTeamOld = bTeamNew; |
andriym | 79:9db29f41dc9d | 298 | |
andriym | 79:9db29f41dc9d | 299 | // Read space button |
andriym | 79:9db29f41dc9d | 300 | bSpaceNew = buttonSpace; |
andriym | 79:9db29f41dc9d | 301 | if(bSpaceNew && !bSpaceOld) { |
andriym | 79:9db29f41dc9d | 302 | level = (level+1) & 0b11; // four states |
andriym | 85:4692a1790cfa | 303 | spaceLEDs(level); |
andriym | 79:9db29f41dc9d | 304 | out.clear(); |
andriym | 79:9db29f41dc9d | 305 | out.printf("New level: %d\r\n", level); |
andriym | 79:9db29f41dc9d | 306 | out.sleep(2); |
andriym | 79:9db29f41dc9d | 307 | } |
andriym | 79:9db29f41dc9d | 308 | bSpaceOld = bSpaceNew; |
andriym | 81:3cd7de5d01ef | 309 | |
andriym | 81:3cd7de5d01ef | 310 | // Read volume buttons |
andriym | 82:1e552cc1a615 | 311 | bVMNew = buttonVolMore.read(); |
andriym | 82:1e552cc1a615 | 312 | bVLNew = buttonVolLess.read(); |
andriym | 81:3cd7de5d01ef | 313 | if(bVMNew && !bVMOld) { |
andriym | 81:3cd7de5d01ef | 314 | volume++; |
andriym | 81:3cd7de5d01ef | 315 | if(volume>3) volume=3; |
andriym | 81:3cd7de5d01ef | 316 | ledBuzzer = 1; |
andriym | 81:3cd7de5d01ef | 317 | out.clear(); |
andriym | 81:3cd7de5d01ef | 318 | out.printf("New volume: %d\r\n", volume); |
andriym | 81:3cd7de5d01ef | 319 | out.sleep(2); |
andriym | 81:3cd7de5d01ef | 320 | } |
andriym | 81:3cd7de5d01ef | 321 | if(bVLNew && !bVLOld) { |
andriym | 81:3cd7de5d01ef | 322 | if(volume>0) volume--; |
andriym | 81:3cd7de5d01ef | 323 | if(!volume) ledBuzzer = 0; |
andriym | 81:3cd7de5d01ef | 324 | out.clear(); |
andriym | 81:3cd7de5d01ef | 325 | out.printf("New volume: %d\r\n", volume); |
andriym | 81:3cd7de5d01ef | 326 | out.sleep(2); |
andriym | 81:3cd7de5d01ef | 327 | } |
andriym | 81:3cd7de5d01ef | 328 | bVMOld = bVMNew; |
andriym | 81:3cd7de5d01ef | 329 | bVLOld = bVLNew; |
andriym | 81:3cd7de5d01ef | 330 | |
andriym | 81:3cd7de5d01ef | 331 | // Output |
elmbed | 76:6638d83939d5 | 332 | unsigned long current_time = t.read_ms(); |
elmbed | 76:6638d83939d5 | 333 | |
andriym | 79:9db29f41dc9d | 334 | if (current_time - last_shown > CYCLE_MS) |
andriym | 78:43a6b54f0372 | 335 | { |
andriym | 81:3cd7de5d01ef | 336 | players.showAll(myTeam, level, volume); |
andriym | 78:43a6b54f0372 | 337 | last_shown = current_time; |
andriym | 78:43a6b54f0372 | 338 | } |
andriym | 78:43a6b54f0372 | 339 | |
andriym | 81:3cd7de5d01ef | 340 | // Radios |
andriym | 78:43a6b54f0372 | 341 | if (current_time - last_send > SEND_RATE_MS) |
AntonLS | 19:afcbb425b3cf | 342 | { |
elmbed | 76:6638d83939d5 | 343 | // Send message |
andriym | 79:9db29f41dc9d | 344 | snprintf(tx_buff, sizeof(tx_buff), "N=%s,%d", rand_name, myTeam); |
andriym | 78:43a6b54f0372 | 345 | radio.send(EVERY_NODE, tx_buff, strlen(tx_buff)); |
andriym | 78:43a6b54f0372 | 346 | last_send = current_time; |
andriym | 78:43a6b54f0372 | 347 | //send_wait = min_wait + (rand() % SEND_RATE_MS)*SEND_DESYNC; |
andriym | 78:43a6b54f0372 | 348 | |
andriym | 78:43a6b54f0372 | 349 | //out.clear(); |
andriym | 78:43a6b54f0372 | 350 | //out.printf("Sent: %s\r\n", tx_buff); |
andriym | 78:43a6b54f0372 | 351 | //uint8_t tempC = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient |
andriym | 78:43a6b54f0372 | 352 | //uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3; // LNA Current Gain |
andriym | 78:43a6b54f0372 | 353 | //uint32_t freq = radio.getFrequency(); |
andriym | 78:43a6b54f0372 | 354 | //out.printf("T: %d, G: %d\r\n", tempC, gain); |
andriym | 78:43a6b54f0372 | 355 | //if(freq!=868000000) out.printf("Freq: %d\r\n", freq); |
elmbed | 76:6638d83939d5 | 356 | |
andriym | 81:3cd7de5d01ef | 357 | //beep(NOTE_A3, 0.05, volume); |
elmbed | 76:6638d83939d5 | 358 | } |
andriym | 78:43a6b54f0372 | 359 | |
elmbed | 76:6638d83939d5 | 360 | if (radio.receiveDone()) |
AntonLS | 11:d3aa5fca2330 | 361 | { |
elmbed | 76:6638d83939d5 | 362 | memset(rx_buff, 0x00, sizeof(rx_buff)); |
elmbed | 76:6638d83939d5 | 363 | memcpy(rx_buff, (char*)radio.DATA, radio.DATALEN > sizeof(rx_buff)-1 ? sizeof(rx_buff)-1 : radio.DATALEN); |
elmbed | 76:6638d83939d5 | 364 | |
andriym | 78:43a6b54f0372 | 365 | if(rx_buff[0]=='N' && rx_buff[1]=='=') { |
andriym | 78:43a6b54f0372 | 366 | memcpy(other_name, rx_buff+2, NAME_LEN); |
andriym | 78:43a6b54f0372 | 367 | other_name[5] = 0x00; |
andriym | 78:43a6b54f0372 | 368 | if(sizeof(rx_buff)>8 && rx_buff[7]==',') { |
andriym | 79:9db29f41dc9d | 369 | otherTeam = rx_buff[8]-'0'; |
andriym | 79:9db29f41dc9d | 370 | } else otherTeam = 1; |
andriym | 79:9db29f41dc9d | 371 | players.update(other_name, otherTeam, radio.RSSI, t.read_ms()); |
andriym | 78:43a6b54f0372 | 372 | |
andriym | 78:43a6b54f0372 | 373 | //uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3; // LNA Current Gain |
andriym | 78:43a6b54f0372 | 374 | //out.clear(); |
andriym | 78:43a6b54f0372 | 375 | //out.printf("Other: %s\r\n", other_name); |
andriym | 78:43a6b54f0372 | 376 | //out.printf("RSSI: %d, G: %d\r\n", radio.RSSI, gain); |
andriym | 78:43a6b54f0372 | 377 | |
andriym | 81:3cd7de5d01ef | 378 | //beep(NOTE_A5, 0.5, volume); |
andriym | 78:43a6b54f0372 | 379 | } else { // received unknown signal |
andriym | 78:43a6b54f0372 | 380 | uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3; // LNA Current Gain |
andriym | 78:43a6b54f0372 | 381 | out.clear(); |
andriym | 78:43a6b54f0372 | 382 | out.printf("Got: %s\r\n", rx_buff); |
andriym | 78:43a6b54f0372 | 383 | out.printf("RSSI: %d, G: %d\r\n", radio.RSSI, gain); |
andriym | 78:43a6b54f0372 | 384 | out.sleep(2.0); |
andriym | 78:43a6b54f0372 | 385 | } |
AntonLS | 11:d3aa5fca2330 | 386 | } |
AntonLS | 0:28ca4562fe1a | 387 | } |
elmbed | 76:6638d83939d5 | 388 | } |