Преглед изворни кода

Implemented RGB, HSV and random fade

Thomas Buck пре 9 година
родитељ
комит
47e7abc7af
2 измењених фајлова са 134 додато и 12 уклоњено
  1. 133
    11
      CaseLights/AppDelegate.m
  2. 1
    1
      CaseLights/Info.plist

+ 133
- 11
CaseLights/AppDelegate.m Прегледај датотеку

@@ -24,6 +24,7 @@
24 24
 #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
25 25
 #define TEXT_RGB_FADE @"RGB Fade"
26 26
 #define TEXT_HSV_FADE @"HSV Fade"
27
+#define TEXT_RANDOM @"Random"
27 28
 
28 29
 #define KEY_CPU_TEMPERATURE @"TC0D"
29 30
 #define KEY_GPU_TEMPERATURE @"TG0D"
@@ -49,6 +50,8 @@
49 50
 @synthesize serial, lastLEDMode;
50 51
 
51 52
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
53
+    srand((unsigned)time(NULL));
54
+    
52 55
     serial = [[Serial alloc] init];
53 56
     lastLEDMode = nil;
54 57
     animation = nil;
@@ -120,9 +123,10 @@
120 123
     
121 124
     // Prepare animations menu
122 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 130
     for (NSString *key in animationStrings) {
127 131
         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
128 132
         if ([key isEqualToString:lastMode]) {
@@ -245,6 +249,12 @@
245 249
 - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
246 250
     if ([sender state] == NSOffState) {
247 251
         lastLEDMode = nil;
252
+        
253
+        // Stop previous timer setting
254
+        if (animation != nil) {
255
+            [animation invalidate];
256
+            animation = nil;
257
+        }
248 258
 
249 259
         // Turn off all other LED menu items
250 260
         for (int i = 0; i < [menuColors numberOfItems]; i++) {
@@ -314,27 +324,77 @@
314 324
 }
315 325
 
316 326
 - (void)visualizeGPUUsage:(NSTimer *)timer {
327
+    // TODO
317 328
 }
318 329
 
319 330
 - (void)visualizeVRAMUsage:(NSTimer *)timer {
331
+    // TODO
320 332
 }
321 333
 
322 334
 - (void)visualizeCPUUsage:(NSTimer *)timer {
335
+    // TODO
323 336
 }
324 337
 
325 338
 - (void)visualizeRAMUsage:(NSTimer *)timer {
339
+    // TODO
326 340
 }
327 341
 
328 342
 - (void)visualizeGPUTemperature:(NSTimer *)timer {
343
+    // TODO
329 344
 }
330 345
 
331 346
 - (void)visualizeCPUTemperature:(NSTimer *)timer {
347
+    // TODO
332 348
 }
333 349
 
334 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 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 400
 - (BOOL)timedVisualization:(NSString *)mode {
@@ -346,25 +406,31 @@
346 406
     
347 407
     // Schedule next invocation for this animation...
348 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 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 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 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 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 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 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 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 426
     } else {
365 427
         return NO;
366 428
     }
367 429
     
430
+#ifdef DEBUG
431
+    NSLog(@"Scheduled animation for \"%@\"!\n", mode);
432
+#endif
433
+    
368 434
     // ...and also execute it right now
369 435
     [animation fire];
370 436
     return YES;
@@ -487,4 +553,60 @@
487 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 612
 @end

+ 1
- 1
CaseLights/Info.plist Прегледај датотеку

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

Loading…
Откажи
Сачувај