Browse Source

Implemented RGB, HSV and random fade

Thomas Buck 9 years ago
parent
commit
47e7abc7af
2 changed files with 134 additions and 12 deletions
  1. 133
    11
      CaseLights/AppDelegate.m
  2. 1
    1
      CaseLights/Info.plist

+ 133
- 11
CaseLights/AppDelegate.m View File

24
 #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
24
 #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
25
 #define TEXT_RGB_FADE @"RGB Fade"
25
 #define TEXT_RGB_FADE @"RGB Fade"
26
 #define TEXT_HSV_FADE @"HSV Fade"
26
 #define TEXT_HSV_FADE @"HSV Fade"
27
+#define TEXT_RANDOM @"Random"
27
 
28
 
28
 #define KEY_CPU_TEMPERATURE @"TC0D"
29
 #define KEY_CPU_TEMPERATURE @"TC0D"
29
 #define KEY_GPU_TEMPERATURE @"TG0D"
30
 #define KEY_GPU_TEMPERATURE @"TG0D"
49
 @synthesize serial, lastLEDMode;
50
 @synthesize serial, lastLEDMode;
50
 
51
 
51
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
52
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
53
+    srand((unsigned)time(NULL));
54
+    
52
     serial = [[Serial alloc] init];
55
     serial = [[Serial alloc] init];
53
     lastLEDMode = nil;
56
     lastLEDMode = nil;
54
     animation = nil;
57
     animation = nil;
120
     
123
     
121
     // Prepare animations menu
124
     // Prepare animations menu
122
     NSArray *animationStrings = [NSArray arrayWithObjects:
125
     NSArray *animationStrings = [NSArray arrayWithObjects:
123
-                        TEXT_RGB_FADE,
124
-                        TEXT_HSV_FADE,
125
-                        nil];
126
+                                 TEXT_RGB_FADE,
127
+                                 TEXT_HSV_FADE,
128
+                                 TEXT_RANDOM,
129
+                                 nil];
126
     for (NSString *key in animationStrings) {
130
     for (NSString *key in animationStrings) {
127
         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
131
         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
128
         if ([key isEqualToString:lastMode]) {
132
         if ([key isEqualToString:lastMode]) {
245
 - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
249
 - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
246
     if ([sender state] == NSOffState) {
250
     if ([sender state] == NSOffState) {
247
         lastLEDMode = nil;
251
         lastLEDMode = nil;
252
+        
253
+        // Stop previous timer setting
254
+        if (animation != nil) {
255
+            [animation invalidate];
256
+            animation = nil;
257
+        }
248
 
258
 
249
         // Turn off all other LED menu items
259
         // Turn off all other LED menu items
250
         for (int i = 0; i < [menuColors numberOfItems]; i++) {
260
         for (int i = 0; i < [menuColors numberOfItems]; i++) {
314
 }
324
 }
315
 
325
 
316
 - (void)visualizeGPUUsage:(NSTimer *)timer {
326
 - (void)visualizeGPUUsage:(NSTimer *)timer {
327
+    // TODO
317
 }
328
 }
318
 
329
 
319
 - (void)visualizeVRAMUsage:(NSTimer *)timer {
330
 - (void)visualizeVRAMUsage:(NSTimer *)timer {
331
+    // TODO
320
 }
332
 }
321
 
333
 
322
 - (void)visualizeCPUUsage:(NSTimer *)timer {
334
 - (void)visualizeCPUUsage:(NSTimer *)timer {
335
+    // TODO
323
 }
336
 }
324
 
337
 
325
 - (void)visualizeRAMUsage:(NSTimer *)timer {
338
 - (void)visualizeRAMUsage:(NSTimer *)timer {
339
+    // TODO
326
 }
340
 }
327
 
341
 
328
 - (void)visualizeGPUTemperature:(NSTimer *)timer {
342
 - (void)visualizeGPUTemperature:(NSTimer *)timer {
343
+    // TODO
329
 }
344
 }
330
 
345
 
331
 - (void)visualizeCPUTemperature:(NSTimer *)timer {
346
 - (void)visualizeCPUTemperature:(NSTimer *)timer {
347
+    // TODO
332
 }
348
 }
333
 
349
 
334
 - (void)visualizeRGBFade:(NSTimer *)timer {
350
 - (void)visualizeRGBFade:(NSTimer *)timer {
351
+    static unsigned char color[3] = { 255, 0, 0 };
352
+    static int dec = 0;
353
+    static int val = 0;
354
+    
355
+    // Adapted from:
356
+    // https://gist.github.com/jamesotron/766994
357
+    
358
+    if (dec < 3) {
359
+        int inc = (dec == 2) ? 0 : (dec + 1);
360
+        if (val < 255) {
361
+            color[dec] -= 1;
362
+            color[inc] += 1;
363
+            val++;
364
+        } else {
365
+            val = 0;
366
+            dec++;
367
+        }
368
+    } else {
369
+        dec = 0;
370
+    }
371
+    
372
+    if ([serial isOpen]) {
373
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", color[0], color[1], color[2]]];
374
+    }
335
 }
375
 }
336
 
376
 
337
 - (void)visualizeHSVFade:(NSTimer *)timer {
377
 - (void)visualizeHSVFade:(NSTimer *)timer {
378
+    static float h = 0.0;
379
+    
380
+    if (h < 360.0) {
381
+        h += 0.5;
382
+    } else {
383
+        h = 0.0;
384
+    }
385
+    
386
+    unsigned char r, g, b;
387
+    [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
388
+    
389
+    if ([serial isOpen]) {
390
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
391
+    }
392
+}
393
+
394
+- (void)visualizeRandom:(NSTimer *)timer {
395
+    if ([serial isOpen]) {
396
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", rand() % 256, rand() % 256, rand() % 256]];
397
+    }
338
 }
398
 }
339
 
399
 
340
 - (BOOL)timedVisualization:(NSString *)mode {
400
 - (BOOL)timedVisualization:(NSString *)mode {
346
     
406
     
347
     // Schedule next invocation for this animation...
407
     // Schedule next invocation for this animation...
348
     if ([mode isEqualToString:TEXT_GPU_USAGE]) {
408
     if ([mode isEqualToString:TEXT_GPU_USAGE]) {
349
-        animation = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
409
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
350
     } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
410
     } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
351
-        animation = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
411
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
352
     } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
412
     } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
353
-        animation = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
413
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
354
     } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
414
     } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
355
-        animation = [NSTimer timerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
415
+        animation = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
356
     } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
416
     } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
357
-        animation = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
417
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
358
     } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
418
     } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
359
-        animation = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
419
+        animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
360
     } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
420
     } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
361
-        animation = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
421
+        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
362
     } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
422
     } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
363
-        animation = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
423
+        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
424
+    } else if ([mode isEqualToString:TEXT_RANDOM]) {
425
+        animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRandom:) userInfo:mode repeats:YES];
364
     } else {
426
     } else {
365
         return NO;
427
         return NO;
366
     }
428
     }
367
     
429
     
430
+#ifdef DEBUG
431
+    NSLog(@"Scheduled animation for \"%@\"!\n", mode);
432
+#endif
433
+    
368
     // ...and also execute it right now
434
     // ...and also execute it right now
369
     [animation fire];
435
     [animation fire];
370
     return YES;
436
     return YES;
487
     [application orderFrontStandardAboutPanel:self];
553
     [application orderFrontStandardAboutPanel:self];
488
 }
554
 }
489
 
555
 
556
+- (void)convertH:(double)h S:(double)s V:(double)v toR:(unsigned char *)r G:(unsigned char *)g B:(unsigned char *)b {
557
+    // Adapted from:
558
+    // https://gist.github.com/hdznrrd/656996
559
+    
560
+    if (s == 0.0) {
561
+        // Achromatic
562
+        *r = *g = *b = (unsigned char)(v * 255);
563
+        return;
564
+    }
565
+    
566
+    h /= 60; // sector 0 to 5
567
+    int i = floor(h);
568
+    double f = h - i; // factorial part of h
569
+    double p = v * (1 - s);
570
+    double q = v * (1 - s * f);
571
+    double t = v * (1 - s * (1 - f));
572
+    
573
+    switch (i) {
574
+        case 0:
575
+            *r = (unsigned char)round(255 * v);
576
+            *g = (unsigned char)round(255 * t);
577
+            *b = (unsigned char)round(255 * p);
578
+            break;
579
+            
580
+        case 1:
581
+            *r = (unsigned char)round(255 * q);
582
+            *g = (unsigned char)round(255 * v);
583
+            *b = (unsigned char)round(255 * p);
584
+            break;
585
+            
586
+        case 2:
587
+            *r = (unsigned char)round(255 * p);
588
+            *g = (unsigned char)round(255 * v);
589
+            *b = (unsigned char)round(255 * t);
590
+            break;
591
+            
592
+        case 3:
593
+            *r = (unsigned char)round(255 * p);
594
+            *g = (unsigned char)round(255 * q);
595
+            *b = (unsigned char)round(255 * v);
596
+            break;
597
+            
598
+        case 4:
599
+            *r = (unsigned char)round(255 * t);
600
+            *g = (unsigned char)round(255 * p);
601
+            *b = (unsigned char)round(255 * v);
602
+            break;
603
+            
604
+        default: case 5:
605
+            *r = (unsigned char)round(255 * v);
606
+            *g = (unsigned char)round(255 * p);
607
+            *b = (unsigned char)round(255 * q);
608
+            break;
609
+    }
610
+}
611
+
490
 @end
612
 @end

+ 1
- 1
CaseLights/Info.plist View File

21
 	<key>CFBundleSignature</key>
21
 	<key>CFBundleSignature</key>
22
 	<string>????</string>
22
 	<string>????</string>
23
 	<key>CFBundleVersion</key>
23
 	<key>CFBundleVersion</key>
24
-	<string>84</string>
24
+	<string>93</string>
25
 	<key>LSApplicationCategoryType</key>
25
 	<key>LSApplicationCategoryType</key>
26
 	<string>public.app-category.utilities</string>
26
 	<string>public.app-category.utilities</string>
27
 	<key>LSMinimumSystemVersion</key>
27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save