1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <Preferences.h> 21 #include <driver/gpio.h> 22 23 #include "clap_actions.h" 24 #include "onsets.h" 25 #include "sound_lab.h" 26 27 namespace { 28 29 using claps :: Action ; 30 31 32 33 snd :: Onsets ears ; 34 snd :: Groups groups ; 35 claps :: Map map ; 36 37 38 constexpr int kHistory = 1000 ; 39 uint8_t history [ kHistory ] ; 40 int historyHead = 0 ; 41 uint32_t historyMs = 0 ; 42 43 struct Mark { 44 snd :: Onsets :: Kind kind ; 45 uint32_t ms , lengthMs ; 46 } ; 47 constexpr int kMarks = 24 ; 48 Mark marks [ kMarks ] ; 49 int markHead = 0 ; 50 51 52 struct LastGroup { 53 int count = 0 ; 54 Action action = Action :: None ; 55 uint32_t at = 0 ; 56 } last ; 57 58 59 struct Outputs { 60 bool led = false ; 61 int colour = - 2 ; 62 bool screenOff = false ; 63 uint32_t screenOffAt = 0 ; 64 uint32_t partyUntil = 0 ; 65 } out ; 66 67 struct Colour { 68 const char * name ; 69 uint8_t r , g , b ; 70 } ; 71 const Colour kColours [ ] = { { "warm white" , 90 , 60 , 30 } , { "red" , 110 , 0 , 0 } , { "orange" , 110 , 40 , 0 } , { "green" , 0 , 100 , 10 } , 72 { "cyan" , 0 , 80 , 90 } , { "blue" , 0 , 20 , 120 } , { "violet" , 70 , 0 , 110 } } ; 73 constexpr int kColourCount = sizeof ( kColours ) / sizeof ( kColours [ 0 ] ) ; 74 75 76 struct Learn { 77 bool on = false ; 78 int n = 0 ; 79 float strength [ 5 ] = { } ; 80 uint32_t length [ 5 ] = { } ; 81 int longs = 0 ; 82 float suggested = 0 ; 83 } learn ; 84 85 bool testing = false ; 86 bool sounds = true ; 87 88 89 const char * const kTwinkle = "C5/4 C5/4 G5/4 G5/4 A5/4 A5/4 G5/2 F5/4 F5/4 E5/4 E5/4 D5/4 D5/4 C5/2" ; 90 91 void answer ( SoundLab & lab , Action a , bool on ) { 92 if ( ! sounds ) return ; 93 switch ( a ) { 94 case Action :: Led : lab . chime ( on ? Chime :: Start : Chime :: Stop ) ; break ; 95 case Action :: Colour : lab . speaker ( ) . tune ( "E6/32 G6/32 B6/16" , 260 , syn :: Wave :: Sine ) ; break ; 96 case Action :: Dark : lab . chime ( Chime :: Stop ) ; break ; 97 case Action :: Screen : lab . chime ( Chime :: Click ) ; break ; 98 case Action :: View : lab . chime ( Chime :: Click ) ; break ; 99 case Action :: Party : lab . chime ( Chime :: Score ) ; break ; 100 case Action :: Tune : lab . speaker ( ) . tune ( kTwinkle , 160 ) ; break ; 101 default : lab . chime ( Chime :: No ) ; break ; 102 } 103 } 104 105 106 107 void save ( ) { 108 Preferences p ; 109 p . begin ( "claps" , false ) ; 110 uint8_t a [ claps :: kPatterns ] ; 111 for ( int i = 0 ; i < claps :: kPatterns ; i ++ ) a [ i ] = uint8_t ( map . action [ i + 1 ] ) ; 112 p . putBytes ( "map" , a , sizeof ( a ) ) ; 113 p . putFloat ( "rise" , ears . config . riseDb ) ; 114 p . putUInt ( "gap" , groups . gapMs ) ; 115 p . putBool ( "sound" , sounds ) ; 116 p . end ( ) ; 117 } 118 119 void load ( ) { 120 Preferences p ; 121 p . begin ( "claps" , true ) ; 122 uint8_t a [ claps :: kPatterns ] ; 123 if ( p . isKey ( "map" ) && p . getBytes ( "map" , a , sizeof ( a ) ) == sizeof ( a ) ) 124 for ( int i = 0 ; i < claps :: kPatterns ; i ++ ) map . action [ i + 1 ] = a [ i ] < uint8_t ( Action :: Count ) ? Action ( a [ i ] ) : Action :: None ; 125 if ( p . isKey ( "rise" ) ) ears . config . riseDb = p . getFloat ( "rise" , ears . config . riseDb ) ; 126 if ( p . isKey ( "gap" ) ) groups . gapMs = p . getUInt ( "gap" , groups . gapMs ) ; 127 sounds = p . getBool ( "sound" , true ) ; 128 p . end ( ) ; 129 } 130 131 132 133 void showColour ( SoundLab & lab ) { 134 lab . claimLed ( true ) ; 135 if ( out . colour < 0 ) rgbLedWrite ( PIN_RGB , 0 , 0 , 0 ) ; 136 else rgbLedWrite ( PIN_RGB , kColours [ out . colour ] . r , kColours [ out . colour ] . g , kColours [ out . colour ] . b ) ; 137 } 138 139 void setScreen ( SoundLab & lab , bool off ) { 140 out . screenOff = off ; 141 out . screenOffAt = millis ( ) ; 142 lab . display ( ) . setBacklight ( off ? 0 : 255 ) ; 143 } 144 145 void run ( SoundLab & lab , Action a ) { 146 switch ( a ) { 147 case Action :: None : break ; 148 case Action :: Led : 149 out . led = ! out . led ; 150 digitalWrite ( PIN_LED , out . led ? HIGH : LOW ) ; 151 lab . note ( sv :: Note :: Good , "LED %s" , out . led ? "on" : "off" ) ; 152 break ; 153 case Action :: Colour : 154 out . colour = out . colour < 0 ? 0 : ( out . colour + 1 ) % kColourCount ; 155 showColour ( lab ) ; 156 lab . note ( sv :: Note :: Good , "colour: %s" , kColours [ out . colour ] . name ) ; 157 break ; 158 case Action :: Dark : 159 out . colour = - 1 ; 160 showColour ( lab ) ; 161 lab . note ( sv :: Note :: Good , "RGB off" ) ; 162 break ; 163 case Action :: Screen : 164 setScreen ( lab , ! out . screenOff ) ; 165 lab . note ( sv :: Note :: Good , out . screenOff ? "screen asleep (back in 30 s, or press a button)" : "screen on" ) ; 166 break ; 167 case Action :: View : lab . setView ( lab . view ( ) + 1 ) ; break ; 168 case Action :: Party : 169 out . partyUntil = millis ( ) + 2000 ; 170 lab . claimLed ( true ) ; 171 lab . note ( sv :: Note :: Good , "party!" ) ; 172 break ; 173 case Action :: Tune : lab . note ( sv :: Note :: Good , "twinkle, twinkle (claps wait until it ends)" ) ; break ; 174 case Action :: Count : break ; 175 } 176 answer ( lab , a , out . led ) ; 177 } 178 179 180 181 void addMark ( const snd :: Onsets :: Event & e ) { 182 marks [ markHead ] = { e . kind , e . ms , e . lengthMs } ; 183 markHead = ( markHead + 1 ) % kMarks ; 184 } 185 186 void frame ( SoundLab & lab , const snd :: Frame & , uint32_t ms ) { 187 const float above = lab . level ( ) . instAbove ( ) ; 188 history [ historyHead ] = uint8_t ( std :: min ( above , 255.0f ) ) ; 189 historyHead = ( historyHead + 1 ) % kHistory ; 190 historyMs = ms ; 191 if ( lab . meter ( ) . learning ( ) ) return ; 192 193 const snd :: Onsets :: Event e = ears . push ( above , ms ) ; 194 if ( e . kind != snd :: Onsets :: Kind :: None ) { 195 addMark ( e ) ; 196 if ( testing && e . kind != snd :: Onsets :: Kind :: Onset ) 197 Serial . printf ( " %s at %lu ms: %.1f dB, %lu ms long\n" , e . kind == snd :: Onsets :: Kind :: Clap ? "clap" : "LONG (not a clap)" , 198 static_cast < unsigned long > ( e . ms ) , e . strengthDb , static_cast < unsigned long > ( e . lengthMs ) ) ; 199 } 200 if ( e . kind == snd :: Onsets :: Kind :: Clap ) { 201 if ( learn . on ) { 202 learn . strength [ learn . n ] = e . strengthDb ; 203 learn . length [ learn . n ] = e . lengthMs ; 204 if ( ++ learn . n == 5 ) { 205 learn . on = false ; 206 learn . suggested = claps :: suggestRise ( learn . strength , 5 ) ; 207 ears . config . riseDb = learn . suggested ; 208 save ( ) ; 209 lab . note ( sv :: Note :: Good , "learnt: sens %.0f dB (saved)" , learn . suggested ) ; 210 } else { 211 lab . note ( sv :: Note :: Info , "clap %d of 5: %.0f dB" , learn . n , e . strengthDb ) ; 212 } 213 } else { 214 groups . add ( e . ms ) ; 215 } 216 } 217 if ( e . kind == snd :: Onsets :: Kind :: Long && learn . on ) learn . longs ++ ; 218 219 const snd :: Groups :: Group g = groups . poll ( ms ) ; 220 if ( g . count == 0 ) return ; 221 last = { g . count , map . forClaps ( g . count ) , millis ( ) } ; 222 if ( testing ) { 223 Serial . printf ( " group of %d" , g . count ) ; 224 for ( int i = 0 ; i + 1 < g . count && i < snd :: Groups :: kMax - 1 ; i ++ ) Serial . printf ( "%s%lu ms" , i ? ", " : ": gaps " , static_cast < unsigned long > ( g . gapsMs [ i ] ) ) ; 225 Serial . println ( ) ; 226 } 227 if ( g . count > claps :: kPatterns ) lab . note ( sv :: Note :: Warn , "%d claps: applause? nothing to do" , g . count ) ; 228 else if ( last . action == Action :: None ) lab . note ( sv :: Note :: Info , "%d claps: not mapped" , g . count ) ; 229 else lab . note ( sv :: Note :: Info , "%d clap%s -> %s" , g . count , g . count > 1 ? "s" : "" , claps :: info ( last . action ) . name ) ; 230 if ( last . action == Action :: None ) answer ( lab , Action :: None , false ) ; 231 else run ( lab , last . action ) ; 232 } 233 234 235 236 237 void groupLabel ( FrameBuffer & fb , int x , int y ) { 238 char text [ 40 ] ; 239 if ( groups . pending ( ) ) { 240 snprintf ( text , sizeof ( text ) , "%d clap%s ..." , groups . pending ( ) , groups . pending ( ) > 1 ? "s" : "" ) ; 241 sv :: bold ( fb , x , y , pal :: amber , text ) ; 242 return ; 243 } 244 const uint32_t age = millis ( ) - last . at ; 245 if ( last . count == 0 || age > 3000 ) return ; 246 snprintf ( text , sizeof ( text ) , "%d clap%s -> %s" , last . count , last . count > 1 ? "s" : "" , claps :: info ( last . action ) . name ) ; 247 sv :: bold ( fb , x , y , mix ( pal :: teal , pal :: night , uint8_t ( age > 2000 ? ( age - 2000 ) * 220 / 1000 : 0 ) ) , text ) ; 248 } 249 250 void drawTimeline ( SoundLab & , FrameBuffer & fb , float ) { 251 const int x = 8 , y = sv :: kTop + 26 , w = 304 , h = 92 ; 252 const float range = 60 ; 253 auto yOf = [ & ] ( float db ) { return y + h - int ( snd :: meterFraction ( db , range ) * h ) ; } ; 254 255 for ( int col = 0 ; col < w ; col ++ ) { 256 const int a = col * kHistory / w , b = ( col + 1 ) * kHistory / w ; 257 uint8_t m = 0 ; 258 for ( int i = a ; i < b ; i ++ ) m = std :: max ( m , history [ ( historyHead + i ) % kHistory ] ) ; 259 if ( m ) fb . drawFastVLine ( x + col , yOf ( m ) , y + h - yOf ( m ) , sv :: levelColour ( m ) ) ; 260 } 261 262 for ( int i = 0 ; i < w ; i += 4 ) { 263 fb . drawPixel ( x + i , yOf ( ears . config . riseDb ) , pal :: ink ) ; 264 fb . drawPixel ( x + i , yOf ( ears . config . releaseDb ) , pal :: muted ) ; 265 } 266 char text [ 32 ] ; 267 snprintf ( text , sizeof ( text ) , "rise %.0f" , ears . config . riseDb ) ; 268 sv :: small ( fb , x + w - sv :: textWidth ( fb , text ) , yOf ( ears . config . riseDb ) - 9 , pal :: ink , text ) ; 269 270 for ( const Mark & m : marks ) { 271 if ( m . kind == snd :: Onsets :: Kind :: None || historyMs - m . ms > 4000 ) continue ; 272 const int mx = x + w - int ( ( historyMs - m . ms ) * w / 4000 ) ; 273 if ( m . kind == snd :: Onsets :: Kind :: Clap ) { 274 fb . drawFastVLine ( mx , y - 4 , h + 4 , pal :: teal ) ; 275 fb . fillTriangle ( mx , y - 4 , mx + 7 , y - 1 , mx , y + 2 , pal :: teal ) ; 276 } else if ( m . kind == snd :: Onsets :: Kind :: Long ) { 277 const int len = int ( m . lengthMs * w / 4000 ) ; 278 fb . fillRect ( mx , y + h + 2 , std :: max ( 2 , len ) , 3 , pal :: amber ) ; 279 sv :: small ( fb , mx , y + h + 6 , pal :: amber , "long" ) ; 280 } else { 281 fb . drawFastVLine ( mx , y + h - 6 , 6 , pal :: muted ) ; 282 } 283 } 284 fb . drawFastHLine ( x , y + h , w , pal :: dim ) ; 285 sv :: small ( fb , x , y + h + 6 , pal :: muted , "-4 s" ) ; 286 sv :: small ( fb , x + w - 18 , y + h + 6 , pal :: muted , "now" ) ; 287 groupLabel ( fb , x , sv :: kTop + 18 ) ; 288 } 289 290 void drawLamp ( SoundLab & , FrameBuffer & fb , float t ) { 291 292 const int bx = 80 , by = sv :: kTop + 52 ; 293 const uint16_t bulb = out . led ? pal :: amber : sv :: kPanel ; 294 if ( out . led ) 295 for ( int i = 0 ; i < 10 ; i ++ ) { 296 const float a = i * 0.628f + t * 0.6f , r0 = 30 + 3 * sinf ( t * 4 + i ) , r1 = r0 + 10 ; 297 fb . drawLine ( bx + int ( cosf ( a ) * r0 ) , by + int ( sinf ( a ) * r0 ) , bx + int ( cosf ( a ) * r1 ) , by + int ( sinf ( a ) * r1 ) , pal :: amber ) ; 298 } 299 fb . fillCircle ( bx , by , 22 , bulb ) ; 300 fb . drawCircle ( bx , by , 22 , pal :: muted ) ; 301 fb . fillRect ( bx - 10 , by + 22 , 21 , 14 , pal :: dim ) ; 302 for ( int j = 0 ; j < 3 ; j ++ ) fb . drawFastHLine ( bx - 10 , by + 25 + j * 4 , 21 , pal :: muted ) ; 303 sv :: small ( fb , bx - 30 , by + 44 , pal :: muted , out . led ? "LED GPIO 4: on" : "LED GPIO 4: off" ) ; 304 305 306 const int ox = 236 , oy = sv :: kTop + 52 ; 307 uint16_t c = sv :: kPanel ; 308 const bool party = millis ( ) < out . partyUntil ; 309 if ( party ) c = hsv ( uint8_t ( t * 300 ) , 255 , 255 ) ; 310 else if ( out . colour >= 0 ) { 311 const Colour & k = kColours [ out . colour ] ; 312 313 c = rgb ( std :: min ( 255 , k . r * 2 + 20 ) , std :: min ( 255 , k . g * 2 + 20 ) , std :: min ( 255 , k . b * 2 + 20 ) ) ; 314 } 315 if ( out . colour >= 0 || party ) 316 for ( int r = 26 ; r <= 36 ; r += 3 ) fb . drawCircle ( ox , oy , r + int ( 2 * sinf ( t * 3 ) ) , mix ( c , pal :: night , uint8_t ( 80 + ( r - 26 ) * 15 ) ) ) ; 317 fb . fillCircle ( ox , oy , 24 , out . colour >= 0 || party ? c : sv :: kPanel ) ; 318 fb . drawCircle ( ox , oy , 24 , pal :: muted ) ; 319 sv :: small ( fb , ox - 40 , oy + 44 , pal :: muted , 320 party ? "RGB: party!" : out . colour >= 0 ? kColours [ out . colour ] . name : out . colour == - 1 ? "RGB: off" : "RGB: level meter" ) ; 321 if ( party ) 322 for ( int i = 0 ; i < 14 ; i ++ ) { 323 const float a = i * 0.449f + t * 2 ; 324 const int r = 40 + int ( fmodf ( t * 60 + i * 9 , 30 ) ) ; 325 fb . fillCircle ( ox + int ( cosf ( a ) * r ) , oy + int ( sinf ( a ) * r * 0.7f ) , 2 , hsv ( uint8_t ( i * 18 + t * 200 ) , 255 , 255 ) ) ; 326 } 327 groupLabel ( fb , 112 , sv :: kTop + 120 ) ; 328 } 329 330 void drawPatterns ( SoundLab & , FrameBuffer & fb , float ) { 331 const uint32_t age = millis ( ) - last . at ; 332 for ( int n = 1 ; n <= claps :: kPatterns ; n ++ ) { 333 const int y = sv :: kTop + 6 + ( n - 1 ) * 26 ; 334 const bool hit = last . count == n && age < 3000 ; 335 if ( hit ) fb . fillRoundRect ( 4 , y - 2 , 312 , 24 , 4 , mix ( pal :: teal , pal :: night , 190 ) ) ; 336 for ( int i = 0 ; i < n ; i ++ ) { 337 const int cx = 18 + i * 18 ; 338 fb . fillCircle ( cx - 3 , y + 10 , 4 , hit ? pal :: teal : pal :: muted ) ; 339 fb . fillCircle ( cx + 3 , y + 10 , 4 , hit ? pal :: ink : pal :: dim ) ; 340 } 341 const Action a = map . action [ n ] ; 342 sv :: bold ( fb , 96 , y + 15 , a == Action :: None ? pal :: dim : hit ? pal :: ink : sv :: kSound , claps :: info ( a ) . name ) ; 343 sv :: small ( fb , 172 , y + 7 , pal :: muted , claps :: info ( a ) . what ) ; 344 } 345 char text [ 48 ] ; 346 snprintf ( text , sizeof ( text ) , "sens %.0f dB - pause %lu ms ends a pattern" , ears . config . riseDb , static_cast < unsigned long > ( groups . gapMs ) ) ; 347 sv :: small ( fb , 8 , sv :: kTop + 112 , pal :: muted , text ) ; 348 } 349 350 void drawTune ( SoundLab & , FrameBuffer & fb , float t ) { 351 char text [ 48 ] ; 352 if ( learn . on ) snprintf ( text , sizeof ( text ) , "clap %d of 5, as you normally would" , learn . n + 1 ) ; 353 else if ( learn . suggested > 0 ) snprintf ( text , sizeof ( text ) , "softest x 60%% = sens %.0f dB (saved)" , learn . suggested ) ; 354 else snprintf ( text , sizeof ( text ) , "double-click, then clap 5 times" ) ; 355 sv :: small ( fb , 8 , sv :: kTop + 6 , learn . on ? pal :: amber : pal :: muted , text ) ; 356 357 const int y0 = sv :: kTop + 104 , hMax = 72 ; 358 for ( int i = 0 ; i < 5 ; i ++ ) { 359 const int x = 16 + i * 44 ; 360 fb . drawRect ( x , y0 - hMax , 30 , hMax , pal :: dim ) ; 361 if ( i < learn . n ) { 362 const int h = int ( snd :: meterFraction ( learn . strength [ i ] , 60 ) * hMax ) ; 363 fb . fillRect ( x + 1 , y0 - h , 28 , h , sv :: levelColour ( learn . strength [ i ] ) ) ; 364 snprintf ( text , sizeof ( text ) , "%.0f" , learn . strength [ i ] ) ; 365 sv :: small ( fb , x + 8 , y0 - h - 10 , pal :: ink , text ) ; 366 snprintf ( text , sizeof ( text ) , "%lums" , static_cast < unsigned long > ( learn . length [ i ] ) ) ; 367 sv :: small ( fb , x , y0 + 4 , pal :: muted , text ) ; 368 } else if ( learn . on && i == learn . n && fmodf ( t , 1 ) < 0.5f ) { 369 fb . fillRect ( x + 1 , y0 - 4 , 28 , 3 , pal :: amber ) ; 370 } 371 } 372 373 const int ry = y0 - int ( snd :: meterFraction ( ears . config . riseDb , 60 ) * hMax ) ; 374 for ( int x = 12 ; x < 236 ; x += 3 ) fb . drawPixel ( x , ry , pal :: ink ) ; 375 snprintf ( text , sizeof ( text ) , "%.0f dB" , ears . config . riseDb ) ; 376 sv :: stat ( fb , 244 , sv :: kTop + 24 , "sens now" , text , sv :: kSound ) ; 377 snprintf ( text , sizeof ( text ) , "%d" , learn . longs ) ; 378 sv :: stat ( fb , 244 , sv :: kTop + 66 , "long (voice?)" , text , learn . longs ? pal :: amber : pal :: ink ) ; 379 } 380 381 const SoundView kViews [ ] = { 382 { "timeline" , drawTimeline , "test" } , 383 { "lamp" , drawLamp , "colour" } , 384 { "patterns" , drawPatterns , "maps" } , 385 { "tune" , drawTune , "learn" } , 386 } ; 387 388 389 390 void cmdMap ( SoundLab & lab , const String & arg , const String & lower ) { 391 const int sp = lower . indexOf ( ' ' ) ; 392 long n = 0 ; 393 Action a ; 394 if ( sp < 0 || ! SoundLab :: number ( arg . substring ( 0 , sp ) , 1 , claps :: kPatterns , n ) || ! claps :: parse ( lower . substring ( sp + 1 ) . c_str ( ) , a ) ) 395 return lab . warn ( "map <1-4> <led|colour|dark|screen|view|party|tune|none>" ) ; 396 map . action [ n ] = a ; 397 save ( ) ; 398 lab . note ( sv :: Note :: Good , "%ld clap%s -> %s (saved)" , n , n > 1 ? "s" : "" , claps :: info ( a ) . name ) ; 399 lab . showView ( "patterns" ) ; 400 } 401 402 void printMaps ( ) { 403 for ( int n = 1 ; n <= claps :: kPatterns ; n ++ ) 404 Serial . printf ( " %d clap%s -> %-7s %s\n" , n , n > 1 ? "s" : " " , claps :: info ( map . action [ n ] ) . name , claps :: info ( map . action [ n ] ) . what ) ; 405 } 406 407 void cmdSens ( SoundLab & lab , const String & arg , const String & ) { 408 float db = 0 ; 409 if ( ! SoundLab :: decimal ( arg , 6 , 50 , db ) ) return lab . warn ( "sens <dB>, 6-50: how far above the quiet a clap must jump (now %.0f)" , ears . config . riseDb ) ; 410 ears . config . riseDb = db ; 411 save ( ) ; 412 lab . note ( sv :: Note :: Good , "sens %.0f dB (saved)" , db ) ; 413 } 414 415 void cmdGap ( SoundLab & lab , const String & arg , const String & ) { 416 long ms = 0 ; 417 if ( ! SoundLab :: number ( arg , 250 , 2000 , ms ) ) 418 return lab . warn ( "gap <ms>, 250-2000: the pause that ends a pattern (now %lu)" , static_cast < unsigned long > ( groups . gapMs ) ) ; 419 groups . gapMs = uint32_t ( ms ) ; 420 save ( ) ; 421 lab . note ( sv :: Note :: Good , "pause %ld ms ends a pattern (saved)" , ms ) ; 422 } 423 424 void cmdLearn ( SoundLab & lab , const String & , const String & ) { 425 learn = Learn { } ; 426 learn . on = true ; 427 groups . clear ( ) ; 428 lab . showView ( "tune" ) ; 429 lab . note ( sv :: Note :: Info , "clap 5 times, as you normally would" ) ; 430 } 431 432 void cmdTest ( SoundLab & lab , const String & , const String & lower ) { 433 testing = lower . isEmpty ( ) ? ! testing : lower != "off" ; 434 lab . note ( sv :: Note :: Info , testing ? "test on: every clap, long sound and group printed" : "test off" ) ; 435 } 436 437 438 void cmdDo ( SoundLab & lab , const String & , const String & lower ) { 439 Action a ; 440 if ( ! claps :: parse ( lower . c_str ( ) , a ) ) return lab . warn ( "do <led|colour|dark|screen|view|party|tune>" ) ; 441 run ( lab , a ) ; 442 } 443 444 void cmdMaps ( SoundLab & , const String & , const String & ) { printMaps ( ) ; } 445 446 void cmdSound ( SoundLab & lab , const String & , const String & lower ) { 447 if ( ! lower . isEmpty ( ) && lower != "on" && lower != "off" ) return lab . warn ( "sound | sound on | sound off" ) ; 448 sounds = lower . isEmpty ( ) ? ! sounds : lower == "on" ; 449 save ( ) ; 450 lab . note ( sv :: Note :: Info , sounds ? "sounds on (the echo guard keeps them from counting as claps)" : "sounds off" ) ; 451 } 452 453 const SoundCommand kCommands [ ] = { 454 { "map" , "<1-4> <action>" , "what n claps do: led colour dark screen view party tune none" , cmdMap } , 455 { "maps" , "" , "print the pattern map" , cmdMaps } , 456 { "sens" , "<dB>" , "how far above the quiet a clap must jump (default 18)" , cmdSens } , 457 { "gap" , "<ms>" , "the pause that ends a pattern (default 650)" , cmdGap } , 458 { "learn" , "" , "clap 5 times: sets sens from your softest clap" , cmdLearn } , 459 { "test" , "[off]" , "print every clap, long sound and group" , cmdTest } , 460 { "do" , "<action>" , "run an action now, without clapping" , cmdDo } , 461 { "sound" , "[on|off]" , "a little sound per pattern on the speaker (saved)" , cmdSound } , 462 { "colour" , "" , "next RGB colour (the lamp view's double-click)" , [ ] ( SoundLab & lab , const String & , const String & ) { run ( lab , Action :: Colour ) ; } } , 463 } ; 464 465 466 467 void setup ( SoundLab & ) { 468 pinMode ( PIN_LED , OUTPUT ) ; 469 470 gpio_set_drive_capability ( static_cast < gpio_num_t > ( PIN_LED ) , GPIO_DRIVE_CAP_0 ) ; 471 digitalWrite ( PIN_LED , LOW ) ; 472 load ( ) ; 473 printMaps ( ) ; 474 } 475 476 void tick ( SoundLab & lab ) { 477 if ( out . screenOff && millis ( ) - out . screenOffAt > 30000 ) setScreen ( lab , false ) ; 478 479 static bool wasParty = false ; 480 const bool party = millis ( ) < out . partyUntil ; 481 if ( party ) { 482 const uint16_t c = hsv ( uint8_t ( millis ( ) / 4 ) , 255 , 120 ) ; 483 rgbLedWrite ( PIN_RGB , uint8_t ( ( c >> 11 ) << 3 ) , uint8_t ( ( ( c >> 5 ) & 63 ) << 2 ) , uint8_t ( ( c & 31 ) << 3 ) ) ; 484 } else if ( wasParty ) { 485 if ( out . colour == - 2 ) lab . claimLed ( false ) ; 486 else showColour ( lab ) ; 487 } 488 wasParty = party ; 489 } 490 491 492 bool gesture ( SoundLab & lab , Gesture , bool ) { 493 if ( ! out . screenOff ) return false ; 494 setScreen ( lab , false ) ; 495 lab . note ( sv :: Note :: Info , "screen on" ) ; 496 return true ; 497 } 498 499 const SoundLabSpec kSpec { 500 "sound-lab-s3" , "S3" , "Clap control" , kViews , sizeof ( kViews ) / sizeof ( kViews [ 0 ] ) , 0 , 501 kCommands , sizeof ( kCommands ) / sizeof ( kCommands [ 0 ] ) , setup , frame , tick , gesture , "learn" , 502 } ; 503 504 SoundLab lab ( kSpec ) ; 505 506 } 507 508 void setup ( ) { lab . begin ( ) ; } 509 510 void loop ( ) { lab . loop ( ) ; }