Browse Source

Implemented all visualizations

Thomas Buck 9 years ago
parent
commit
a9770c4568
4 changed files with 144 additions and 19 deletions
  1. 141
    16
      CaseLights/AppDelegate.m
  2. 1
    1
      CaseLights/Base.lproj/MainMenu.xib
  3. 1
    1
      CaseLights/Info.plist
  4. 1
    1
      CaseLights/Serial.m

+ 141
- 16
CaseLights/AppDelegate.m View File

@@ -29,6 +29,22 @@
29 29
 #define KEY_CPU_TEMPERATURE @"TC0D"
30 30
 #define KEY_GPU_TEMPERATURE @"TG0D"
31 31
 
32
+// Temperature in Celsius
33
+#define CPU_TEMP_MIN 20
34
+#define CPU_TEMP_MAX 90
35
+
36
+// HSV Color (S = V = 1)
37
+#define CPU_COLOR_MIN 120
38
+#define CPU_COLOR_MAX 0
39
+
40
+#define GPU_TEMP_MIN 20
41
+#define GPU_TEMP_MAX 90
42
+#define GPU_COLOR_MIN 120
43
+#define GPU_COLOR_MAX 0
44
+
45
+#define RAM_COLOR_MIN 0
46
+#define RAM_COLOR_MAX 120
47
+
32 48
 @interface AppDelegate ()
33 49
 
34 50
 @property (strong) NSStatusItem *statusItem;
@@ -135,16 +151,6 @@
135 151
         [menuAnimations addItem:item];
136 152
     }
137 153
     
138
-    JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
139
-    
140
-#ifdef DEBUG
141
-    JSKMCPUUsageInfo cpuUsageInfo = systemMonitor.cpuUsageInfo;
142
-    NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
143
-    
144
-    JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
145
-    NSLog(@"Memory Usage: %.2fGB Free + %.2fGB Used = %.2fGB\n", memoryUsageInfo.freeMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.usedMemory / (1024.0 * 1024.0 * 1024.0), (memoryUsageInfo.freeMemory + memoryUsageInfo.usedMemory) / (1024.0 * 1024.0 * 1024.0));
146
-#endif
147
-    
148 154
     // Add CPU Usage menu item
149 155
     NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
150 156
     if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
@@ -324,27 +330,141 @@
324 330
 }
325 331
 
326 332
 - (void)visualizeGPUUsage:(NSTimer *)timer {
327
-    // TODO
333
+    NSNumber *usage;
334
+    NSNumber *freeVRAM;
335
+    NSNumber *usedVRAM;
336
+    if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
337
+        NSLog(@"Error reading GPU information\n");
338
+    } else {
339
+        double h = [self map:[usage doubleValue] FromMin:0.0 FromMax:100.0 ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
340
+        
341
+#ifdef DEBUG
342
+        NSLog(@"GPU Usage: %.3f%%\n", [usage doubleValue]);
343
+#endif
344
+        
345
+        unsigned char r, g, b;
346
+        [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
347
+        
348
+        if ([serial isOpen]) {
349
+            [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
350
+        }
351
+    }
328 352
 }
329 353
 
330 354
 - (void)visualizeVRAMUsage:(NSTimer *)timer {
331
-    // TODO
355
+    NSNumber *usage;
356
+    NSNumber *freeVRAM;
357
+    NSNumber *usedVRAM;
358
+    if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
359
+        NSLog(@"Error reading GPU information\n");
360
+    } else {
361
+        double h = [self map:[freeVRAM doubleValue] FromMin:0.0 FromMax:([freeVRAM doubleValue] + [usedVRAM doubleValue]) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
362
+        
363
+#ifdef DEBUG
364
+        NSLog(@"VRAM %.2fGB Free + %.2fGB Used = %.2fGB mapped to color %.2f!\n", [freeVRAM doubleValue] / (1024.0 * 1024.0 * 1024.0), [usedVRAM doubleValue] / (1024.0 * 1024.0 * 1024.0), ([freeVRAM doubleValue] + [usedVRAM doubleValue]) / (1024.0 * 1024.0 * 1024.0), h);
365
+#endif
366
+        
367
+        unsigned char r, g, b;
368
+        [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
369
+        
370
+        if ([serial isOpen]) {
371
+            [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
372
+        }
373
+    }
332 374
 }
333 375
 
334 376
 - (void)visualizeCPUUsage:(NSTimer *)timer {
335
-    // TODO
377
+    JSKMCPUUsageInfo cpuUsageInfo = [JSKSystemMonitor systemMonitor].cpuUsageInfo;
378
+    
379
+    double h = [self map:cpuUsageInfo.usage FromMin:0.0 FromMax:100.0 ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
380
+    
381
+#ifdef DEBUG
382
+    NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
383
+#endif
384
+    
385
+    unsigned char r, g, b;
386
+    [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
387
+    
388
+    if ([serial isOpen]) {
389
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
390
+    }
336 391
 }
337 392
 
338 393
 - (void)visualizeRAMUsage:(NSTimer *)timer {
339
-    // TODO
394
+    JSKMMemoryUsageInfo memoryUsageInfo = [JSKSystemMonitor systemMonitor].memoryUsageInfo;
395
+    
396
+    double h = [self map:memoryUsageInfo.freeMemory FromMin:0.0 FromMax:(memoryUsageInfo.usedMemory + memoryUsageInfo.freeMemory) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
397
+    
398
+#ifdef DEBUG
399
+    NSLog(@"RAM %.2fGB Free + %.2fGB Used = %.2fGB mapped to color %.2f!\n", memoryUsageInfo.freeMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.usedMemory / (1024.0 * 1024.0 * 1024.0), (memoryUsageInfo.freeMemory + memoryUsageInfo.usedMemory) / (1024.0 * 1024.0 * 1024.0), h);
400
+#endif
401
+    
402
+    unsigned char r, g, b;
403
+    [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
404
+    
405
+    if ([serial isOpen]) {
406
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
407
+    }
340 408
 }
341 409
 
342 410
 - (void)visualizeGPUTemperature:(NSTimer *)timer {
343
-    // TODO
411
+    JSKSMC *smc = [JSKSMC smc];
412
+    double temp = [smc temperatureInCelsiusForKey:KEY_GPU_TEMPERATURE];
413
+    
414
+    if (temp > 1000.0) {
415
+        temp /= 1000.0;
416
+    }
417
+    
418
+    if (temp > GPU_TEMP_MAX) {
419
+        temp = GPU_TEMP_MAX;
420
+    }
421
+    
422
+    if (temp < GPU_TEMP_MIN) {
423
+        temp = GPU_TEMP_MIN;
424
+    }
425
+    
426
+    double h = [self map:temp FromMin:GPU_TEMP_MIN FromMax:GPU_TEMP_MAX ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
427
+    
428
+#ifdef DEBUG
429
+    NSLog(@"GPU Temp %.2f mapped to color %.2f!\n", temp, h);
430
+#endif
431
+    
432
+    unsigned char r, g, b;
433
+    [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
434
+    
435
+    if ([serial isOpen]) {
436
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
437
+    }
344 438
 }
345 439
 
346 440
 - (void)visualizeCPUTemperature:(NSTimer *)timer {
347
-    // TODO
441
+    JSKSMC *smc = [JSKSMC smc];
442
+    double temp = [smc temperatureInCelsiusForKey:KEY_CPU_TEMPERATURE];
443
+    
444
+    if (temp > 1000.0) {
445
+        temp /= 1000.0;
446
+    }
447
+    
448
+    if (temp > CPU_TEMP_MAX) {
449
+        temp = CPU_TEMP_MAX;
450
+    }
451
+    
452
+    if (temp < CPU_TEMP_MIN) {
453
+        temp = CPU_TEMP_MIN;
454
+    }
455
+    
456
+    double h = [self map:temp FromMin:CPU_TEMP_MIN FromMax:CPU_TEMP_MAX ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
457
+    
458
+#ifdef DEBUG
459
+    NSLog(@"CPU Temp %.2f mapped to color %.2f!\n", temp, h);
460
+#endif
461
+    
462
+    unsigned char r, g, b;
463
+    [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
464
+    
465
+    if ([serial isOpen]) {
466
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
467
+    }
348 468
 }
349 469
 
350 470
 - (void)visualizeRGBFade:(NSTimer *)timer {
@@ -553,6 +673,11 @@
553 673
     [application orderFrontStandardAboutPanel:self];
554 674
 }
555 675
 
676
+- (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
677
+    double norm = (val - fmin) / (fmax - fmin);
678
+    return (norm * (tmax - tmin)) + tmin;
679
+}
680
+
556 681
 - (void)convertH:(double)h S:(double)s V:(double)v toR:(unsigned char *)r G:(unsigned char *)g B:(unsigned char *)b {
557 682
     // Adapted from:
558 683
     // https://gist.github.com/hdznrrd/656996

+ 1
- 1
CaseLights/Base.lproj/MainMenu.xib View File

@@ -694,7 +694,7 @@
694 694
                     </connections>
695 695
                 </menuItem>
696 696
                 <menuItem isSeparatorItem="YES" id="umI-9c-ZzN"/>
697
-                <menuItem title="UV Lights" keyEquivalent="w" id="tLQ-BA-kGu">
697
+                <menuItem title="Toggle Lights" keyEquivalent="w" id="tLQ-BA-kGu">
698 698
                     <connections>
699 699
                         <action selector="toggleLights:" target="Voe-Tx-rLC" id="eLZ-43-xtu"/>
700 700
                     </connections>

+ 1
- 1
CaseLights/Info.plist View File

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

+ 1
- 1
CaseLights/Serial.m View File

@@ -154,7 +154,7 @@
154 154
     size_t length = strlen(data);
155 155
     
156 156
 #ifdef DEBUG
157
-    NSLog(@"Sending string \"%s\"...\n", data);
157
+    NSLog(@"Sending string %s", data);
158 158
 #endif
159 159
     
160 160
     ssize_t sent = 0;

Loading…
Cancel
Save