1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <Preferences.h> 25 #include <driver/gpio.h> 26 #include <esp_random.h> 27 #include <esp_timer.h> 28 29 #include "bird.h" 30 #include "knock_lock.h" 31 #include "onsets.h" 32 #include "rhythm.h" 33 #include "sound_lab.h" 34 35 namespace { 36 37 38 39 snd :: Onsets ears ; 40 41 enum class Screen : uint8_t { Knock , Bird , Party } ; 42 Screen screen ( const SoundLab & lab ) { return Screen ( lab . view ( ) ) ; } 43 44 45 struct Lock { 46 snd :: Rhythm secret ; 47 knock :: Collector collector ; 48 bool setting = false ; 49 snd :: Rhythm attempt ; 50 snd :: RhythmMatch match ; 51 uint32_t attemptAt = 0 ; 52 uint32_t openUntil = 0 ; 53 uint32_t openedAt = 0 ; 54 float tolerance = 0.12f ; 55 } lock ; 56 57 58 bird :: Game game ( esp_random ( ) ) ; 59 int best = 0 ; 60 uint32_t overAt = 0 , lastStep = 0 ; 61 float stepAcc = 0 ; 62 bird :: Game :: State lastState = bird :: Game :: State :: Ready ; 63 uint32_t flapAt = 0 ; 64 65 66 snd :: BeatTracker beats ; 67 struct Ring { 68 uint32_t at ; 69 int16_t x , y ; 70 uint8_t hue ; 71 } ; 72 constexpr int kRings = 8 ; 73 Ring rings [ kRings ] ; 74 int ringHead = 0 ; 75 uint8_t hue = 0 ; 76 constexpr int kBeatLog = 32 ; 77 uint32_t beatLog [ kBeatLog ] ; 78 int beatHead = 0 ; 79 uint32_t lastBeat = 0 ; 80 bool partyClick = false ; 81 82 83 84 bool soundOn = true ; 85 86 enum class Sfx : uint8_t { Flap , Point , Crash , Best , Open , Wrong , Tick } ; 87 88 void sfx ( SoundLab & lab , Sfx s ) { 89 if ( ! soundOn ) return ; 90 snd :: Speaker & spk = lab . speaker ( ) ; 91 switch ( s ) { 92 case Sfx :: Flap : spk . tone ( 900 , 20 , syn :: Wave :: Square , 0.25f ) ; break ; 93 case Sfx :: Point : spk . tone ( 1568 , 30 , syn :: Wave :: Sine , 0.45f ) ; break ; 94 case Sfx :: Crash : spk . tune ( "C4/16 G3/16 C3/8" , 300 , syn :: Wave :: Saw ) ; break ; 95 case Sfx :: Best : lab . chime ( Chime :: Score ) ; break ; 96 case Sfx :: Open : spk . tune ( "C5/16 E5/16 G5/16 C6/8" , 280 , syn :: Wave :: Triangle ) ; break ; 97 case Sfx :: Wrong : lab . chime ( Chime :: No ) ; break ; 98 case Sfx :: Tick : spk . tone ( 2200 , 8 , syn :: Wave :: Square , 0.3f ) ; break ; 99 } 100 } 101 102 103 104 struct RhythmPlayer { 105 snd :: Rhythm r ; 106 int next = 0 ; 107 int64_t startUs = 0 ; 108 snd :: Speaker * speaker = nullptr ; 109 esp_timer_handle_t timer = nullptr ; 110 } replay ; 111 112 void onReplayTimer ( void * ) { 113 const int64_t now = esp_timer_get_time ( ) - replay . startUs ; 114 while ( replay . next < replay . r . count && now >= int64_t ( replay . r . atMs [ replay . next ] - replay . r . atMs [ 0 ] ) * 1000 ) { 115 replay . speaker -> tone ( replay . next == 0 ? 1320 : 1100 , 25 , syn :: Wave :: Square , 0.5f ) ; 116 replay . next ++ ; 117 } 118 if ( replay . next >= replay . r . count ) esp_timer_stop ( replay . timer ) ; 119 } 120 121 void playRhythm ( SoundLab & lab , const snd :: Rhythm & r ) { 122 if ( ! soundOn || r . count < 1 ) return ; 123 if ( ! replay . timer ) { 124 esp_timer_create_args_t args = { } ; 125 args . callback = onReplayTimer ; 126 args . name = "knock-replay" ; 127 if ( esp_timer_create ( & args , & replay . timer ) != ESP_OK ) return ; 128 } 129 esp_timer_stop ( replay . timer ) ; 130 replay . r = r ; 131 replay . next = 0 ; 132 replay . speaker = & lab . speaker ( ) ; 133 replay . startUs = esp_timer_get_time ( ) + 300000 ; 134 esp_timer_start_periodic ( replay . timer , 2000 ) ; 135 } 136 137 138 139 void saveSecret ( ) { 140 Preferences p ; 141 p . begin ( "knock" , false ) ; 142 uint32_t rel [ snd :: Rhythm :: kMax ] ; 143 for ( int i = 0 ; i < lock . secret . count ; i ++ ) rel [ i ] = lock . secret . atMs [ i ] - lock . secret . atMs [ 0 ] ; 144 if ( lock . secret . count ) p . putBytes ( "secret" , rel , lock . secret . count * sizeof ( uint32_t ) ) ; 145 else p . remove ( "secret" ) ; 146 p . end ( ) ; 147 } 148 149 void loadAll ( ) { 150 Preferences p ; 151 p . begin ( "knock" , true ) ; 152 if ( p . isKey ( "secret" ) ) { 153 const size_t n = p . getBytesLength ( "secret" ) / sizeof ( uint32_t ) ; 154 if ( n >= 2 && n <= size_t ( snd :: Rhythm :: kMax ) ) { 155 p . getBytes ( "secret" , lock . secret . atMs , n * sizeof ( uint32_t ) ) ; 156 lock . secret . count = int ( n ) ; 157 } 158 } 159 p . end ( ) ; 160 p . begin ( "bird" , true ) ; 161 best = int ( p . getUInt ( "best" , 0 ) ) ; 162 p . end ( ) ; 163 p . begin ( "games" , true ) ; 164 soundOn = p . getBool ( "sound" , true ) ; 165 p . end ( ) ; 166 } 167 168 void saveBest ( ) { 169 Preferences p ; 170 p . begin ( "bird" , false ) ; 171 p . putUInt ( "best" , uint32_t ( best ) ) ; 172 p . end ( ) ; 173 } 174 175 176 177 void finishPattern ( SoundLab & lab , const snd :: Rhythm & r ) { 178 if ( lock . setting ) { 179 lock . setting = false ; 180 if ( r . count < 2 ) return lab . note ( sv :: Note :: Warn , "a secret needs 2 knocks or more" ) ; 181 lock . secret = r ; 182 saveSecret ( ) ; 183 playRhythm ( lab , r ) ; 184 return lab . note ( sv :: Note :: Good , "secret saved: %d knocks, %.1f s" , r . count , r . lengthMs ( ) / 1000.0f ) ; 185 } 186 lock . attempt = r ; 187 lock . attemptAt = millis ( ) ; 188 lock . match = snd :: matchRhythm ( lock . secret , r , lock . tolerance ) ; 189 if ( lock . match . ok ) { 190 lock . openUntil = millis ( ) + 5000 ; 191 lock . openedAt = millis ( ) ; 192 digitalWrite ( PIN_LED , HIGH ) ; 193 sfx ( lab , Sfx :: Open ) ; 194 lab . note ( sv :: Note :: Good , "OPEN (score %d)" , lock . match . score ) ; 195 } else { 196 sfx ( lab , Sfx :: Wrong ) ; 197 lab . note ( sv :: Note :: Bad , "locked: %s" , lock . match . why ) ; 198 } 199 } 200 201 void frame ( SoundLab & lab , const snd :: Frame & f , uint32_t ms ) { 202 const float above = lab . level ( ) . instAbove ( ) ; 203 if ( screen ( lab ) == Screen :: Party ) { 204 if ( beats . push ( f . rms , above , ms ) ) { 205 hue += 37 ; 206 lastBeat = millis ( ) ; 207 beatLog [ beatHead ] = lastBeat ; 208 beatHead = ( beatHead + 1 ) % kBeatLog ; 209 rings [ ringHead ] = { lastBeat , int16_t ( 40 + esp_random ( ) % 240 ) , int16_t ( 50 + esp_random ( ) % 90 ) , hue } ; 210 ringHead = ( ringHead + 1 ) % kRings ; 211 if ( partyClick ) sfx ( lab , Sfx :: Tick ) ; 212 } 213 return ; 214 } 215 if ( lab . meter ( ) . learning ( ) ) return ; 216 const snd :: Onsets :: Event e = ears . push ( above , ms ) ; 217 if ( screen ( lab ) == Screen :: Bird && e . kind == snd :: Onsets :: Kind :: Onset ) { 218 219 if ( game . state ( ) == bird :: Game :: State :: Over ) { 220 if ( millis ( ) - overAt < 800 ) return ; 221 game . reset ( ) ; 222 } 223 game . flap ( ) ; 224 flapAt = millis ( ) ; 225 sfx ( lab , Sfx :: Flap ) ; 226 } 227 if ( screen ( lab ) == Screen :: Knock ) { 228 if ( e . kind == snd :: Onsets :: Kind :: Clap && ! lock . collector . add ( e . ms ) ) lab . note ( sv :: Note :: Warn , "12 knocks is the most" ) ; 229 snd :: Rhythm done ; 230 if ( lock . collector . poll ( ms , done ) ) finishPattern ( lab , done ) ; 231 } 232 } 233 234 235 236 237 void rhythmRow ( FrameBuffer & fb , int x , int y , int w , const snd :: Rhythm & r , uint16_t c , const snd :: Rhythm * secret = nullptr , 238 uint32_t scaleMs = 0 ) { 239 fb . drawFastHLine ( x , y + 10 , w , pal :: dim ) ; 240 const uint32_t len = scaleMs ? scaleMs : r . lengthMs ( ) ; 241 for ( int i = 0 ; i < r . count ; i ++ ) { 242 const int kx = x + ( len ? int ( uint64_t ( r . atMs [ i ] - r . atMs [ 0 ] ) * w / len ) : 0 ) ; 243 fb . fillRoundRect ( kx - 2 , y , 5 , 21 , 2 , c ) ; 244 245 if ( secret && i + 1 < r . count && secret -> count == r . count ) { 246 const float err = fabsf ( secret -> share ( i ) - r . share ( i ) ) ; 247 const int nx = x + int ( uint64_t ( r . atMs [ i + 1 ] - r . atMs [ 0 ] ) * w / len ) ; 248 const uint16_t ec = err <= lock . tolerance * 0.5f ? pal :: teal : err <= lock . tolerance ? pal :: amber : pal :: coral ; 249 fb . fillRect ( kx + 4 , y + 24 , std :: max ( 1 , nx - kx - 8 ) , 3 , ec ) ; 250 } 251 } 252 } 253 254 void padlock ( FrameBuffer & fb , int cx , int cy , bool open , float lift , float shake , float t ) { 255 const int x = cx + int ( shake ) ; 256 const uint16_t c = open ? pal :: teal : sv :: kSound ; 257 258 const int sy = cy - 14 - int ( lift * 10 ) ; 259 for ( int r = 10 ; r <= 13 ; r ++ ) fb . drawCircle ( x , sy , r , c ) ; 260 fb . fillRect ( x - 14 , sy , 29 , 15 + int ( lift * 10 ) , pal :: night ) ; 261 fb . fillRect ( x - 13 , sy , 4 , 15 + int ( lift * 10 ) , c ) ; 262 if ( ! open ) fb . fillRect ( x + 10 , sy , 4 , 15 , c ) ; 263 264 fb . fillRoundRect ( x - 20 , cy , 41 , 32 , 5 , c ) ; 265 fb . fillCircle ( x , cy + 12 , 4 , pal :: night ) ; 266 fb . fillRect ( x - 1 , cy + 14 , 3 , 9 , pal :: night ) ; 267 if ( open ) 268 for ( int i = 0 ; i < 6 ; i ++ ) { 269 const float a = i * 1.047f + t * 2 ; 270 fb . fillCircle ( x + int ( cosf ( a ) * 30 ) , cy + 16 + int ( sinf ( a ) * 26 ) , 2 , pal :: teal ) ; 271 } 272 } 273 274 void drawKnock ( SoundLab & , FrameBuffer & fb , float t ) { 275 const uint32_t now = millis ( ) ; 276 const bool open = now < lock . openUntil ; 277 const float lift = open ? std :: min ( 1.0f , ( now - lock . openedAt ) / 300.0f ) : 0 ; 278 const bool failed = ! lock . match . ok && now - lock . attemptAt < 500 && lock . attemptAt ; 279 const float shake = failed ? 5 * sinf ( ( now - lock . attemptAt ) * 0.08f ) : 0 ; 280 padlock ( fb , 40 , sv :: kTop + 44 , open , lift , shake , t ) ; 281 sv :: bold ( fb , 14 , sv :: kTop + 100 , open ? pal :: teal : pal :: muted , open ? "OPEN" : "LOCKED" ) ; 282 283 const int x = 96 , w = 210 ; 284 sv :: small ( fb , x , sv :: kTop + 6 , pal :: muted , "secret" ) ; 285 if ( lock . secret . count >= 2 ) rhythmRow ( fb , x , sv :: kTop + 16 , w , lock . secret , sv :: kSound ) ; 286 else sv :: small ( fb , x + 44 , sv :: kTop + 22 , pal :: amber , "none yet: knock set" ) ; 287 288 const snd :: Rhythm & open_ = lock . collector . open ( ) ; 289 if ( open_ . count ) { 290 sv :: small ( fb , x , sv :: kTop + 52 , pal :: amber , lock . setting ? "new secret..." : "listening..." ) ; 291 292 const uint32_t scale = std :: max < uint32_t > ( lock . secret . lengthMs ( ) , open_ . lengthMs ( ) ) ; 293 rhythmRow ( fb , x , sv :: kTop + 62 , w , open_ , pal :: amber , nullptr , scale ? scale : 1 ) ; 294 } else if ( lock . attempt . count ) { 295 char text [ 40 ] ; 296 if ( lock . match . ok ) snprintf ( text , sizeof ( text ) , "your knock: score %d" , lock . match . score ) ; 297 else snprintf ( text , sizeof ( text ) , "your knock: %s" , lock . match . why ) ; 298 sv :: small ( fb , x , sv :: kTop + 52 , lock . match . ok ? pal :: teal : pal :: coral , text ) ; 299 rhythmRow ( fb , x , sv :: kTop + 62 , w , lock . attempt , pal :: ink , & lock . secret ) ; 300 } 301 const char * hint = lock . setting ? "knock your secret, then wait 1.5 s" 302 : lock . secret . count < 2 ? "double-click, then knock a rhythm" 303 : "knock on the table near the mic" ; 304 sv :: small ( fb , x , sv :: kTop + 104 , lock . setting ? pal :: amber : pal :: muted , hint ) ; 305 char tol [ 32 ] ; 306 snprintf ( tol , sizeof ( tol ) , "tolerance %d%%" , int ( lock . tolerance * 100 + 0.5f ) ) ; 307 sv :: small ( fb , x , sv :: kTop + 116 , pal :: dim , tol ) ; 308 } 309 310 void drawBird ( SoundLab & , FrameBuffer & fb , float t ) { 311 using namespace bird ; 312 313 const float drift = game . state ( ) == Game :: State :: Playing ? t * 12 : 0 ; 314 for ( int x = 0 ; x < 320 ; x += 2 ) { 315 const int h = 10 + int ( 8 * sinf ( ( x + drift ) * 0.03f ) + 5 * sinf ( ( x + drift ) * 0.071f ) ) ; 316 fb . drawFastVLine ( x , int ( kBottom ) - h , h , sv :: kPanel ) ; 317 fb . drawFastVLine ( x + 1 , int ( kBottom ) - h , h , sv :: kPanel ) ; 318 } 319 320 for ( int i = 0 ; i < kPipes ; i ++ ) { 321 const Pipe & p = game . pipe ( i ) ; 322 const int px = int ( p . x ) , top = int ( p . gapY - kGap / 2 ) , bottom = int ( p . gapY + kGap / 2 ) ; 323 fb . fillRect ( px , int ( kTop ) , int ( kPipeW ) , top - int ( kTop ) , mix ( pal :: teal , pal :: night , 90 ) ) ; 324 fb . fillRect ( px , bottom , int ( kPipeW ) , int ( kBottom ) - bottom , mix ( pal :: teal , pal :: night , 90 ) ) ; 325 fb . fillRect ( px - 3 , top - 8 , int ( kPipeW ) + 6 , 8 , pal :: teal ) ; 326 fb . fillRect ( px - 3 , bottom , int ( kPipeW ) + 6 , 8 , pal :: teal ) ; 327 } 328 fb . drawFastHLine ( 0 , int ( kBottom ) , 320 , pal :: muted ) ; 329 330 const float by = game . state ( ) == Game :: State :: Ready ? game . y ( ) + 4 * sinf ( t * 4 ) : game . y ( ) ; 331 const int bx = int ( kBirdX ) , iy = int ( by ) ; 332 const bool over = game . state ( ) == Game :: State :: Over ; 333 fb . fillCircle ( bx , iy , int ( kRadius ) + 1 , over ? pal :: coral : pal :: amber ) ; 334 const bool wingUp = millis ( ) - flapAt < 120 ; 335 fb . fillTriangle ( bx - 7 , iy + ( wingUp ? - 1 : 1 ) , bx - 1 , iy , bx - 5 , iy + ( wingUp ? - 8 : 7 ) , pal :: ink ) ; 336 fb . fillCircle ( bx + 3 , iy - 2 , 2 , pal :: ink ) ; 337 fb . drawPixel ( bx + 4 , iy - 2 , pal :: night ) ; 338 fb . fillTriangle ( bx + 6 , iy - 1 , bx + 11 , iy + 1 , bx + 6 , iy + 3 , pal :: coral ) ; 339 340 char text [ 40 ] ; 341 snprintf ( text , sizeof ( text ) , "%d" , game . score ( ) ) ; 342 sv :: bold ( fb , 160 - sv :: textWidth ( fb , text ) / 2 , int ( kTop ) + 18 , pal :: ink , text ) ; 343 snprintf ( text , sizeof ( text ) , "best %d" , best ) ; 344 sv :: small ( fb , 312 - sv :: textWidth ( fb , text ) , int ( kTop ) + 6 , pal :: muted , text ) ; 345 if ( game . state ( ) == Game :: State :: Ready ) sv :: bold ( fb , 112 , 112 , sv :: kSound , "clap to fly!" ) ; 346 if ( over ) { 347 fb . fillRoundRect ( 80 , 70 , 160 , 44 , 6 , sv :: kPanel ) ; 348 snprintf ( text , sizeof ( text ) , "score %d" , game . score ( ) ) ; 349 sv :: bold ( fb , 160 - sv :: textWidth ( fb , text ) / 2 , 88 , game . score ( ) >= best && best ? pal :: amber : pal :: ink , text ) ; 350 sv :: small ( fb , 104 , 100 , pal :: muted , millis ( ) - overAt < 800 ? "" : "clap to try again" ) ; 351 } 352 } 353 354 void drawParty ( SoundLab & lab , FrameBuffer & fb , float ) { 355 const uint32_t now = millis ( ) ; 356 357 const uint32_t since = now - lastBeat ; 358 if ( lastBeat && since < 120 ) fb . fillRect ( 0 , sv :: kTop , 320 , 6 , mix ( hsv ( hue , 255 , 255 ) , pal :: night , uint8_t ( since * 2 ) ) ) ; 359 360 for ( const Ring & r : rings ) { 361 if ( ! r . at || now - r . at > 700 ) continue ; 362 const uint32_t age = now - r . at ; 363 const uint16_t c = mix ( hsv ( r . hue , 255 , 255 ) , pal :: night , uint8_t ( age * 255 / 700 ) ) ; 364 const int rad = 4 + int ( age * 0.09f ) ; 365 fb . drawCircle ( r . x , r . y , rad , c ) ; 366 fb . drawCircle ( r . x , r . y , rad + 1 , c ) ; 367 if ( age < 150 ) fb . fillCircle ( r . x , r . y , 6 - int ( age / 30 ) , c ) ; 368 } 369 370 char text [ 24 ] ; 371 const int bpm = beats . bpm ( ) ; 372 snprintf ( text , sizeof ( text ) , bpm ? "%d" : "--" , bpm ) ; 373 sv :: big ( fb , 12 , sv :: kTop + 52 , since < 150 ? hsv ( hue , 200 , 255 ) : pal :: ink , text ) ; 374 sv :: small ( fb , 14 , sv :: kTop + 58 , pal :: muted , "BPM" ) ; 375 snprintf ( text , sizeof ( text ) , "%lu beats" , static_cast < unsigned long > ( beats . beats ( ) ) ) ; 376 sv :: small ( fb , 14 , sv :: kTop + 72 , pal :: muted , text ) ; 377 378 const int x = 8 , y = sv :: kTop + 100 , w = 304 ; 379 fb . drawFastHLine ( x , y + 14 , w , pal :: dim ) ; 380 for ( uint32_t b : beatLog ) { 381 if ( ! b || now - b > 8000 ) continue ; 382 const int bx = x + w - int ( ( now - b ) * w / 8000 ) ; 383 fb . fillRect ( bx , y , 2 , 14 , pal :: amber ) ; 384 } 385 sv :: small ( fb , x , y + 18 , pal :: muted , "beats, last 8 s" ) ; 386 snprintf ( text , sizeof ( text ) , "sens x%.1f" , beats . ratio ) ; 387 sv :: small ( fb , x + w - sv :: textWidth ( fb , text ) , y + 18 , pal :: muted , text ) ; 388 ( void ) lab ; 389 } 390 391 const SoundView kViews [ ] = { 392 { "knock" , drawKnock , "knock set" } , 393 { "bird" , drawBird , "bird" } , 394 { "party" , drawParty , "party reset" } , 395 } ; 396 397 398 399 void printRhythm ( const char * what , const snd :: Rhythm & r ) { 400 Serial . printf ( " %s: %d knocks over %lu ms, gaps:" , what , r . count , static_cast < unsigned long > ( r . lengthMs ( ) ) ) ; 401 for ( int i = 0 ; i + 1 < r . count ; i ++ ) Serial . printf ( " %lu (%d%%)" , static_cast < unsigned long > ( r . atMs [ i + 1 ] - r . atMs [ i ] ) , int ( r . share ( i ) * 100 + 0.5f ) ) ; 402 Serial . println ( ) ; 403 } 404 405 void cmdKnock ( SoundLab & lab , const String & , const String & lower ) { 406 lab . showView ( "knock" ) ; 407 if ( lower == "set" ) { 408 lock . setting = true ; 409 lock . collector . clear ( ) ; 410 return lab . note ( sv :: Note :: Info , "knock your new secret, then wait 1.5 s" ) ; 411 } 412 if ( lower == "clear" ) { 413 lock . secret . clear ( ) ; 414 saveSecret ( ) ; 415 return lab . note ( sv :: Note :: Info , "secret erased" ) ; 416 } 417 if ( lower == "play" ) { 418 if ( lock . secret . count < 2 ) return lab . warn ( "no secret yet: knock set" ) ; 419 if ( ! soundOn ) return lab . warn ( "sound is off: sound on" ) ; 420 playRhythm ( lab , lock . secret ) ; 421 return lab . note ( sv :: Note :: Info , "playing the secret: %d knocks" , lock . secret . count ) ; 422 } 423 if ( ! lower . isEmpty ( ) && lower != "show" ) return lab . warn ( "knock set | knock clear | knock show | knock play" ) ; 424 if ( lock . secret . count < 2 ) Serial . println ( " no secret yet: knock set" ) ; 425 else printRhythm ( "secret" , lock . secret ) ; 426 if ( lock . attempt . count ) { 427 printRhythm ( "last try" , lock . attempt ) ; 428 Serial . printf ( " match: %s, score %d, worst gap off by %d%% (tolerance %d%%)\n" , lock . match . ok ? "OPEN" : lock . match . why , lock . match . score , 429 int ( lock . match . worst * 100 + 0.5f ) , int ( lock . tolerance * 100 + 0.5f ) ) ; 430 } 431 } 432 433 void cmdTol ( SoundLab & lab , const String & arg , const String & ) { 434 long pct = 0 ; 435 if ( ! SoundLab :: number ( arg , 3 , 30 , pct ) ) 436 return lab . warn ( "tol <percent>, 3-30: how far each gap may be off, as a share of the pattern (now %d)" , int ( lock . tolerance * 100 + 0.5f ) ) ; 437 lock . tolerance = pct / 100.0f ; 438 lab . note ( sv :: Note :: Info , "tolerance %ld%%" , pct ) ; 439 } 440 441 void cmdBird ( SoundLab & lab , const String & , const String & lower ) { 442 lab . showView ( "bird" ) ; 443 if ( lower == "reset" ) { 444 best = 0 ; 445 saveBest ( ) ; 446 return lab . note ( sv :: Note :: Info , "best score reset" ) ; 447 } 448 game . reset ( ) ; 449 lab . note ( sv :: Note :: Info , "new round: clap to fly" ) ; 450 } 451 452 void cmdParty ( SoundLab & lab , const String & arg , const String & lower ) { 453 lab . showView ( "party" ) ; 454 if ( lower . startsWith ( "sens" ) ) { 455 String value = arg . substring ( 4 ) ; 456 value . trim ( ) ; 457 float r = 0 ; 458 if ( ! SoundLab :: decimal ( value , 1.2f , 3.0f , r ) ) 459 return lab . warn ( "party sens <ratio>, 1.2-3.0: how far a beat must jump over the last second (now %.1f)" , beats . ratio ) ; 460 beats . ratio = r ; 461 return lab . note ( sv :: Note :: Info , "beat = %.1fx the last second" , r ) ; 462 } 463 if ( lower == "click on" || lower == "click off" ) { 464 partyClick = lower == "click on" ; 465 return lab . note ( sv :: Note :: Info , partyClick ? "a tick on every beat (the board can't hear for ~200 ms after it)" : "no beat tick" ) ; 466 } 467 if ( lower == "reset" ) { 468 beats . reset ( ) ; 469 for ( uint32_t & b : beatLog ) b = 0 ; 470 return lab . note ( sv :: Note :: Info , "tempo forgotten: listening again" ) ; 471 } 472 Serial . printf ( " %lu beats, %d BPM, sens x%.1f\n" , static_cast < unsigned long > ( beats . beats ( ) ) , beats . bpm ( ) , beats . ratio ) ; 473 } 474 475 void cmdSound ( SoundLab & lab , const String & , const String & lower ) { 476 if ( ! lower . isEmpty ( ) && lower != "on" && lower != "off" ) return lab . warn ( "sound | sound on | sound off" ) ; 477 soundOn = lower . isEmpty ( ) ? ! soundOn : lower == "on" ; 478 Preferences p ; 479 p . begin ( "games" , false ) ; 480 p . putBool ( "sound" , soundOn ) ; 481 p . end ( ) ; 482 lab . note ( sv :: Note :: Info , soundOn ? "game sounds on" : "game sounds off" ) ; 483 } 484 485 const SoundCommand kCommands [ ] = { 486 { "knock" , "[set|clear|show|play]" , "set = knock a new secret; show = secret and last try; play = hear it" , cmdKnock } , 487 { "tol" , "<percent>" , "knock lock: how far each gap may be off (default 12)" , cmdTol } , 488 { "bird" , "[reset]" , "Clap Bird: a new round (reset = forget the best score)" , cmdBird } , 489 { "party" , "[sens <x>|click on|off|reset]" , "beat party: sensitivity 1.2-3.0 (default 1.6), a tick per beat, forget the tempo" , cmdParty } , 490 { "sound" , "[on|off]" , "the game sounds on the speaker (saved)" , cmdSound } , 491 } ; 492 493 494 495 void setup ( SoundLab & ) { 496 pinMode ( PIN_LED , OUTPUT ) ; 497 498 gpio_set_drive_capability ( static_cast < gpio_num_t > ( PIN_LED ) , GPIO_DRIVE_CAP_0 ) ; 499 digitalWrite ( PIN_LED , LOW ) ; 500 ears . config . maxClapMs = 200 ; 501 loadAll ( ) ; 502 if ( lock . secret . count >= 2 ) printRhythm ( "secret" , lock . secret ) ; 503 Serial . printf ( " Clap Bird best: %d\n" , best ) ; 504 } 505 506 void stepBird ( ) { 507 const uint32_t now = millis ( ) ; 508 stepAcc += std :: min ( 0.1f , ( now - lastStep ) / 1000.0f ) ; 509 lastStep = now ; 510 while ( stepAcc >= 1 / 60.0f ) game . step ( 1 / 60.0f ) , stepAcc -= 1 / 60.0f ; 511 } 512 513 void tick ( SoundLab & lab ) { 514 const uint32_t now = millis ( ) ; 515 516 if ( screen ( lab ) == Screen :: Bird ) stepBird ( ) ; 517 else lastStep = now ; 518 if ( game . state ( ) == bird :: Game :: State :: Over && lastState != bird :: Game :: State :: Over ) { 519 overAt = now ; 520 if ( game . score ( ) > best ) { 521 best = game . score ( ) ; 522 saveBest ( ) ; 523 sfx ( lab , Sfx :: Best ) ; 524 lab . note ( sv :: Note :: Good , "new best: %d" , best ) ; 525 } else { 526 sfx ( lab , Sfx :: Crash ) ; 527 lab . note ( sv :: Note :: Info , "score %d" , game . score ( ) ) ; 528 } 529 } 530 531 static int lastScore = 0 ; 532 if ( game . score ( ) > lastScore && game . state ( ) != bird :: Game :: State :: Over ) sfx ( lab , Sfx :: Point ) ; 533 lastScore = game . score ( ) ; 534 lastState = game . state ( ) ; 535 536 537 const bool open = now < lock . openUntil ; 538 if ( ! open && lock . openUntil ) { 539 lock . openUntil = 0 ; 540 digitalWrite ( PIN_LED , LOW ) ; 541 } 542 543 544 static bool claimed = false ; 545 static uint32_t shown = 0xFFFFFFFF ; 546 const bool party = screen ( lab ) == Screen :: Party ; 547 const bool want = open || party ; 548 if ( want != claimed ) lab . claimLed ( claimed = want ) , shown = 0xFFFFFFFF ; 549 if ( ! want ) return ; 550 uint8_t r = 0 , g = 0 , b = 0 ; 551 if ( open ) g = 110 , b = 20 ; 552 else if ( lastBeat && now - lastBeat < 300 ) { 553 554 const uint8_t v = uint8_t ( 160 - ( now - lastBeat ) / 75 * 40 ) ; 555 const uint16_t c = hsv ( hue , 255 , v ) ; 556 r = uint8_t ( ( c >> 11 ) << 3 ) , g = uint8_t ( ( ( c >> 5 ) & 63 ) << 2 ) , b = uint8_t ( ( c & 31 ) << 3 ) ; 557 } 558 const uint32_t colour = ( uint32_t ( r ) << 16 ) | ( uint32_t ( g ) << 8 ) | b ; 559 if ( colour == shown ) return ; 560 shown = colour ; 561 rgbLedWrite ( PIN_RGB , r , g , b ) ; 562 } 563 564 const SoundLabSpec kSpec { 565 "sound-lab-s6" , "S6" , "Sound games" , kViews , sizeof ( kViews ) / sizeof ( kViews [ 0 ] ) , 0 , 566 kCommands , sizeof ( kCommands ) / sizeof ( kCommands [ 0 ] ) , setup , frame , tick , nullptr , "knock show" , 567 } ; 568 569 SoundLab lab ( kSpec ) ; 570 571 } 572 573 void setup ( ) { lab . begin ( ) ; } 574 575 void loop ( ) { lab . loop ( ) ; }