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