Browse Source

Reapply color after changing brightness. Moved some code around.

Thomas Buck 9 years ago
parent
commit
7cd204a3bc
1 changed files with 172 additions and 157 deletions
  1. 172
    157
      CaseLights/AppDelegate.m

+ 172
- 157
CaseLights/AppDelegate.m View File

@@ -278,6 +278,13 @@
278 278
 - (IBAction)brightnessMoved:(NSSlider *)sender {
279 279
     [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", [sender floatValue]]];
280 280
     
281
+    // Restore the current configuration for items where it won't happen automatically
282
+    for (int i = 0; i < [menuColors numberOfItems]; i++) {
283
+        if ([[menuColors itemAtIndex:i] state] == NSOnState) {
284
+            [self selectedVisualization:[menuColors itemAtIndex:i]];
285
+        }
286
+    }
287
+    
281 288
     // Store changed value in preferences
282 289
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
283 290
     [store setObject:[NSNumber numberWithFloat:[sender floatValue]] forKey:PREF_BRIGHTNESS];
@@ -359,6 +366,168 @@
359 366
     [store synchronize];
360 367
 }
361 368
 
369
+- (BOOL)timedVisualization:(NSString *)mode {
370
+    // Stop previous timer setting
371
+    if (animation != nil) {
372
+        [animation invalidate];
373
+        animation = nil;
374
+    }
375
+    
376
+    // Schedule next invocation for this animation...
377
+    if ([mode isEqualToString:TEXT_GPU_USAGE]) {
378
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
379
+    } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
380
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
381
+    } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
382
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
383
+    } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
384
+        animation = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
385
+    } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
386
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
387
+    } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
388
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
389
+    } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
390
+        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
391
+    } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
392
+        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
393
+    } else if ([mode isEqualToString:TEXT_RANDOM]) {
394
+        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRandom:) userInfo:mode repeats:YES];
395
+    } else {
396
+        return NO;
397
+    }
398
+    
399
+#ifdef DEBUG
400
+    NSLog(@"Scheduled animation for \"%@\"!\n", mode);
401
+#endif
402
+    
403
+    // ...and also execute it right now
404
+    [animation fire];
405
+    return YES;
406
+}
407
+
408
+- (void)selectedVisualization:(NSMenuItem *)sender {
409
+    // Turn off all other LED menu items
410
+    if (menuColors != nil) {
411
+        for (int i = 0; i < [menuColors numberOfItems]; i++) {
412
+            [[menuColors itemAtIndex:i] setState:NSOffState];
413
+        }
414
+    }
415
+    if (menuAnimations != nil) {
416
+        for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
417
+            [[menuAnimations itemAtIndex:i] setState:NSOffState];
418
+        }
419
+    }
420
+    if (menuVisualizations != nil) {
421
+        for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
422
+            [[menuVisualizations itemAtIndex:i] setState:NSOffState];
423
+        }
424
+    }
425
+    [buttonOff setState:NSOffState];
426
+    [sender setState:NSOnState];
427
+    
428
+    // Check if a static color was selected
429
+    BOOL found = NO;
430
+    if (staticColors != nil) {
431
+        for (NSString *key in [staticColors allKeys]) {
432
+            if ([sender.title isEqualToString:key]) {
433
+                found = YES;
434
+                
435
+                // Stop previous timer setting
436
+                if (animation != nil) {
437
+                    [animation invalidate];
438
+                    animation = nil;
439
+                }
440
+                
441
+                NSColor *color = [staticColors valueForKey:key];
442
+                unsigned char red = [color redComponent] * 255;
443
+                unsigned char green = [color greenComponent] * 255;
444
+                unsigned char blue = [color blueComponent] * 255;
445
+                [self setLightsR:red G:green B:blue];
446
+                
447
+                break;
448
+            }
449
+        }
450
+    }
451
+    
452
+    if (!found) {
453
+        // Check if an animated visualization was selected
454
+        if ([self timedVisualization:[sender title]] == NO) {
455
+            NSLog(@"Unknown LED Visualization selected!\n");
456
+            return;
457
+        }
458
+    }
459
+    
460
+    // Store changed value in preferences
461
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
462
+    [store setObject:[sender title] forKey:PREF_LED_MODE];
463
+    [store synchronize];
464
+    
465
+#ifdef DEBUG
466
+    NSLog(@"Stored new mode: \"%@\"!\n", [sender title]);
467
+#endif
468
+}
469
+
470
+- (void)selectedSerialPort:(NSMenuItem *)source {
471
+    // Store selection for next start-up
472
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
473
+    [store setObject:[source title] forKey:PREF_SERIAL_PORT];
474
+    [store synchronize];
475
+    
476
+    // De-select all other ports
477
+    for (int i = 0; i < [menuPorts numberOfItems]; i++) {
478
+        [[menuPorts itemAtIndex:i] setState:NSOffState];
479
+    }
480
+    
481
+    // Select only the current port
482
+    [source setState:NSOnState];
483
+    
484
+    // Close previously opened port, if any
485
+    if ([serial isOpen]) {
486
+        [serial closePort];
487
+    }
488
+    
489
+    // Try to open selected port
490
+    [serial setPortName:[source title]];
491
+    if ([serial openPort] != 0) {
492
+        [source setState:NSOffState];
493
+    } else {
494
+        // Restore the current configuration
495
+        for (int i = 0; i < [menuColors numberOfItems]; i++) {
496
+            if ([[menuColors itemAtIndex:i] state] == NSOnState) {
497
+                [self selectedVisualization:[menuColors itemAtIndex:i]];
498
+            }
499
+        }
500
+        for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
501
+            if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
502
+                [self selectedVisualization:[menuAnimations itemAtIndex:i]];
503
+            }
504
+        }
505
+        for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
506
+            if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
507
+                [self selectedVisualization:[menuVisualizations itemAtIndex:i]];
508
+            }
509
+        }
510
+        if ([buttonOff state] == NSOnState) {
511
+            [buttonOff setState:NSOffState];
512
+            [self turnLEDsOff:buttonOff];
513
+        }
514
+        if ([buttonLights state] == NSOnState) {
515
+            [serial sendString:@"UV 1\n"];
516
+        } else {
517
+            [serial sendString:@"UV 0\n"];
518
+        }
519
+    }
520
+}
521
+
522
+- (IBAction)showAbout:(id)sender {
523
+    [NSApp activateIgnoringOtherApps:YES];
524
+    [application orderFrontStandardAboutPanel:self];
525
+}
526
+
527
+// ------------------------------------------------------
528
+// ------------------- Visualizations -------------------
529
+// ------------------------------------------------------
530
+
362 531
 - (void)visualizeGPUUsage:(NSTimer *)timer {
363 532
     NSNumber *usage;
364 533
     NSNumber *freeVRAM;
@@ -521,163 +690,9 @@
521 690
     [self setLightsR:rand() % 256 G:rand() % 256 B:rand() % 256];
522 691
 }
523 692
 
524
-- (BOOL)timedVisualization:(NSString *)mode {
525
-    // Stop previous timer setting
526
-    if (animation != nil) {
527
-        [animation invalidate];
528
-        animation = nil;
529
-    }
530
-    
531
-    // Schedule next invocation for this animation...
532
-    if ([mode isEqualToString:TEXT_GPU_USAGE]) {
533
-        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
534
-    } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
535
-        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
536
-    } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
537
-        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
538
-    } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
539
-        animation = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
540
-    } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
541
-        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
542
-    } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
543
-        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
544
-    } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
545
-        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
546
-    } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
547
-        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
548
-    } else if ([mode isEqualToString:TEXT_RANDOM]) {
549
-        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRandom:) userInfo:mode repeats:YES];
550
-    } else {
551
-        return NO;
552
-    }
553
-    
554
-#ifdef DEBUG
555
-    NSLog(@"Scheduled animation for \"%@\"!\n", mode);
556
-#endif
557
-    
558
-    // ...and also execute it right now
559
-    [animation fire];
560
-    return YES;
561
-}
562
-
563
-- (void)selectedVisualization:(NSMenuItem *)sender {
564
-    // Turn off all other LED menu items
565
-    if (menuColors != nil) {
566
-        for (int i = 0; i < [menuColors numberOfItems]; i++) {
567
-            [[menuColors itemAtIndex:i] setState:NSOffState];
568
-        }
569
-    }
570
-    if (menuAnimations != nil) {
571
-        for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
572
-            [[menuAnimations itemAtIndex:i] setState:NSOffState];
573
-        }
574
-    }
575
-    if (menuVisualizations != nil) {
576
-        for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
577
-            [[menuVisualizations itemAtIndex:i] setState:NSOffState];
578
-        }
579
-    }
580
-    [buttonOff setState:NSOffState];
581
-    [sender setState:NSOnState];
582
-    
583
-    // Check if a static color was selected
584
-    BOOL found = NO;
585
-    if (staticColors != nil) {
586
-        for (NSString *key in [staticColors allKeys]) {
587
-            if ([sender.title isEqualToString:key]) {
588
-                found = YES;
589
-                
590
-                // Stop previous timer setting
591
-                if (animation != nil) {
592
-                    [animation invalidate];
593
-                    animation = nil;
594
-                }
595
-                
596
-                NSColor *color = [staticColors valueForKey:key];
597
-                unsigned char red = [color redComponent] * 255;
598
-                unsigned char green = [color greenComponent] * 255;
599
-                unsigned char blue = [color blueComponent] * 255;
600
-                [self setLightsR:red G:green B:blue];
601
-                
602
-                break;
603
-            }
604
-        }
605
-    }
606
-    
607
-    if (!found) {
608
-        // Check if an animated visualization was selected
609
-        if ([self timedVisualization:[sender title]] == NO) {
610
-            NSLog(@"Unknown LED Visualization selected!\n");
611
-            return;
612
-        }
613
-    }
614
-    
615
-    // Store changed value in preferences
616
-    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
617
-    [store setObject:[sender title] forKey:PREF_LED_MODE];
618
-    [store synchronize];
619
-    
620
-#ifdef DEBUG
621
-    NSLog(@"Stored new mode: \"%@\"!\n", [sender title]);
622
-#endif
623
-}
624
-
625
-- (void)selectedSerialPort:(NSMenuItem *)source {
626
-    // Store selection for next start-up
627
-    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
628
-    [store setObject:[source title] forKey:PREF_SERIAL_PORT];
629
-    [store synchronize];
630
-    
631
-    // De-select all other ports
632
-    for (int i = 0; i < [menuPorts numberOfItems]; i++) {
633
-        [[menuPorts itemAtIndex:i] setState:NSOffState];
634
-    }
635
-    
636
-    // Select only the current port
637
-    [source setState:NSOnState];
638
-    
639
-    // Close previously opened port, if any
640
-    if ([serial isOpen]) {
641
-        [serial closePort];
642
-    }
643
-    
644
-    // Try to open selected port
645
-    [serial setPortName:[source title]];
646
-    if ([serial openPort] != 0) {
647
-        [source setState:NSOffState];
648
-    } else {
649
-        // Restore the current configuration
650
-        for (int i = 0; i < [menuColors numberOfItems]; i++) {
651
-            if ([[menuColors itemAtIndex:i] state] == NSOnState) {
652
-                [self selectedVisualization:[menuColors itemAtIndex:i]];
653
-            }
654
-        }
655
-        for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
656
-            if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
657
-                [self selectedVisualization:[menuAnimations itemAtIndex:i]];
658
-            }
659
-        }
660
-        for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
661
-            if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
662
-                [self selectedVisualization:[menuVisualizations itemAtIndex:i]];
663
-            }
664
-        }
665
-        if ([buttonOff state] == NSOnState) {
666
-            [buttonOff setState:NSOffState];
667
-            [self turnLEDsOff:buttonOff];
668
-        }
669
-        if ([buttonLights state] == NSOnState) {
670
-            [serial sendString:@"UV 1\n"];
671
-        } else {
672
-            [serial sendString:@"UV 0\n"];
673
-        }
674
-    }
675
-}
676
-
677
-- (IBAction)showAbout:(id)sender {
678
-    [NSApp activateIgnoringOtherApps:YES];
679
-    [application orderFrontStandardAboutPanel:self];
680
-}
693
+// -----------------------------------------------------
694
+// --------------------- Utilities ---------------------
695
+// -----------------------------------------------------
681 696
 
682 697
 - (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
683 698
     double norm = (val - fmin) / (fmax - fmin);

Loading…
Cancel
Save