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