Browse Source

Added LED Brightness Slider

Thomas Buck 9 years ago
parent
commit
db44b72e00
4 changed files with 73 additions and 45 deletions
  1. 3
    0
      CaseLights/AppDelegate.h
  2. 44
    44
      CaseLights/AppDelegate.m
  3. 25
    0
      CaseLights/Base.lproj/MainMenu.xib
  4. 1
    1
      CaseLights/Info.plist

+ 3
- 0
CaseLights/AppDelegate.h View File

21
 @property (weak) IBOutlet NSMenu *menuPorts;
21
 @property (weak) IBOutlet NSMenu *menuPorts;
22
 
22
 
23
 @property (weak) IBOutlet NSMenuItem *buttonOff;
23
 @property (weak) IBOutlet NSMenuItem *buttonOff;
24
+@property (weak) IBOutlet NSMenuItem *brightnessItem;
25
+@property (weak) IBOutlet NSSlider *brightnessSlider;
26
+@property (weak) IBOutlet NSMenuItem *brightnessLabel;
24
 @property (weak) IBOutlet NSMenuItem *buttonLights;
27
 @property (weak) IBOutlet NSMenuItem *buttonLights;
25
 
28
 
26
 @end
29
 @end

+ 44
- 44
CaseLights/AppDelegate.m View File

15
 #define PREF_SERIAL_PORT @"SerialPort"
15
 #define PREF_SERIAL_PORT @"SerialPort"
16
 #define PREF_LIGHTS_STATE @"LightState"
16
 #define PREF_LIGHTS_STATE @"LightState"
17
 #define PREF_LED_MODE @"LEDMode"
17
 #define PREF_LED_MODE @"LEDMode"
18
+#define PREF_BRIGHTNESS @"Brightness"
18
 
19
 
19
 #define TEXT_CPU_USAGE @"CPU Usage"
20
 #define TEXT_CPU_USAGE @"CPU Usage"
20
 #define TEXT_RAM_USAGE @"RAM Usage"
21
 #define TEXT_RAM_USAGE @"RAM Usage"
61
 @synthesize statusMenu, application;
62
 @synthesize statusMenu, application;
62
 @synthesize menuColors, menuAnimations, menuVisualizations, menuPorts;
63
 @synthesize menuColors, menuAnimations, menuVisualizations, menuPorts;
63
 @synthesize buttonOff, buttonLights;
64
 @synthesize buttonOff, buttonLights;
65
+@synthesize brightnessItem, brightnessSlider, brightnessLabel;
64
 @synthesize statusItem, statusImage;
66
 @synthesize statusItem, statusImage;
65
 @synthesize staticColors, animation;
67
 @synthesize staticColors, animation;
66
 @synthesize serial, lastLEDMode;
68
 @synthesize serial, lastLEDMode;
84
     NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
86
     NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
85
     [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
87
     [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
86
     [appDefaults setObject:@"" forKey:PREF_LED_MODE];
88
     [appDefaults setObject:@"" forKey:PREF_LED_MODE];
89
+    [appDefaults setObject:[NSNumber numberWithFloat:50.0] forKey:PREF_BRIGHTNESS];
87
     [store registerDefaults:appDefaults];
90
     [store registerDefaults:appDefaults];
88
     [store synchronize];
91
     [store synchronize];
89
     NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
92
     NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
90
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
93
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
91
     NSString *lastMode = [store stringForKey:PREF_LED_MODE];
94
     NSString *lastMode = [store stringForKey:PREF_LED_MODE];
95
+    float brightness = [store floatForKey:PREF_BRIGHTNESS];
96
+    
97
+    // Prepare brightness menu
98
+    brightnessItem.view = brightnessSlider;
99
+    [brightnessSlider setFloatValue:brightness];
100
+    [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", brightness]];
92
     
101
     
93
     // Prepare serial port menu
102
     // Prepare serial port menu
94
     NSArray *ports = [Serial listSerialPorts];
103
     NSArray *ports = [Serial listSerialPorts];
234
         [serial closePort];
243
         [serial closePort];
235
     }
244
     }
236
 }
245
 }
246
+
247
+- (void)setLightsR:(unsigned char)r G:(unsigned char)g B:(unsigned char)b {
248
+    if ([serial isOpen]) {
249
+        unsigned char red = r * ([brightnessSlider floatValue] / 100.0);
250
+        unsigned char green = g * ([brightnessSlider floatValue] / 100.0);
251
+        unsigned char blue = b * ([brightnessSlider floatValue] / 100.0);
252
+        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", red, green, blue]];
253
+    } else {
254
+#ifdef DEBUG
255
+        NSLog(@"Trying to send RGB without opened port!\n");
256
+#endif
257
+    }
258
+}
259
+
237
 - (IBAction)relistSerialPorts:(id)sender {
260
 - (IBAction)relistSerialPorts:(id)sender {
238
     // Refill port list
261
     // Refill port list
239
     NSArray *ports = [Serial listSerialPorts];
262
     NSArray *ports = [Serial listSerialPorts];
252
     }
275
     }
253
 }
276
 }
254
 
277
 
278
+- (IBAction)brightnessMoved:(NSSlider *)sender {
279
+    [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", [sender floatValue]]];
280
+    
281
+    // Store changed value in preferences
282
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
283
+    [store setObject:[NSNumber numberWithFloat:[sender floatValue]] forKey:PREF_BRIGHTNESS];
284
+    [store synchronize];
285
+}
286
+
255
 - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
287
 - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
256
     if ([sender state] == NSOffState) {
288
     if ([sender state] == NSOffState) {
257
         lastLEDMode = nil;
289
         lastLEDMode = nil;
295
 #endif
327
 #endif
296
         
328
         
297
         // Send command to turn off LEDs
329
         // Send command to turn off LEDs
298
-        if ([serial isOpen]) {
299
-            [serial sendString:@"RGB 0 0 0\n"];
300
-        }
330
+        [self setLightsR:0 G:0 B:0];
301
     } else {
331
     } else {
302
         // Try to restore last LED setting
332
         // Try to restore last LED setting
303
         if (lastLEDMode != nil) {
333
         if (lastLEDMode != nil) {
344
         
374
         
345
         unsigned char r, g, b;
375
         unsigned char r, g, b;
346
         [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
376
         [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
-        }
377
+        [self setLightsR:r G:g B:b];
351
     }
378
     }
352
 }
379
 }
353
 
380
 
366
         
393
         
367
         unsigned char r, g, b;
394
         unsigned char r, g, b;
368
         [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
395
         [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
-        }
396
+        [self setLightsR:r G:g B:b];
373
     }
397
     }
374
 }
398
 }
375
 
399
 
384
     
408
     
385
     unsigned char r, g, b;
409
     unsigned char r, g, b;
386
     [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
410
     [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
-    }
411
+    [self setLightsR:r G:g B:b];
391
 }
412
 }
392
 
413
 
393
 - (void)visualizeRAMUsage:(NSTimer *)timer {
414
 - (void)visualizeRAMUsage:(NSTimer *)timer {
401
     
422
     
402
     unsigned char r, g, b;
423
     unsigned char r, g, b;
403
     [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
424
     [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
-    }
425
+    [self setLightsR:r G:g B:b];
408
 }
426
 }
409
 
427
 
410
 - (void)visualizeGPUTemperature:(NSTimer *)timer {
428
 - (void)visualizeGPUTemperature:(NSTimer *)timer {
431
     
449
     
432
     unsigned char r, g, b;
450
     unsigned char r, g, b;
433
     [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
451
     [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
-    }
452
+    [self setLightsR:r G:g B:b];
438
 }
453
 }
439
 
454
 
440
 - (void)visualizeCPUTemperature:(NSTimer *)timer {
455
 - (void)visualizeCPUTemperature:(NSTimer *)timer {
461
     
476
     
462
     unsigned char r, g, b;
477
     unsigned char r, g, b;
463
     [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
478
     [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
-    }
479
+    [self setLightsR:r G:g B:b];
468
 }
480
 }
469
 
481
 
470
 - (void)visualizeRGBFade:(NSTimer *)timer {
482
 - (void)visualizeRGBFade:(NSTimer *)timer {
488
     } else {
500
     } else {
489
         dec = 0;
501
         dec = 0;
490
     }
502
     }
491
-    
492
-    if ([serial isOpen]) {
493
-        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", color[0], color[1], color[2]]];
494
-    }
503
+    [self setLightsR:color[0] G:color[1] B:color[2]];
495
 }
504
 }
496
 
505
 
497
 - (void)visualizeHSVFade:(NSTimer *)timer {
506
 - (void)visualizeHSVFade:(NSTimer *)timer {
498
     static float h = 0.0;
507
     static float h = 0.0;
499
     
508
     
500
-    if (h < 360.0) {
509
+    if (h < 359.0) {
501
         h += 0.5;
510
         h += 0.5;
502
     } else {
511
     } else {
503
         h = 0.0;
512
         h = 0.0;
505
     
514
     
506
     unsigned char r, g, b;
515
     unsigned char r, g, b;
507
     [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
516
     [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
508
-    
509
-    if ([serial isOpen]) {
510
-        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", r, g, b]];
511
-    }
517
+    [self setLightsR:r G:g B:b];
512
 }
518
 }
513
 
519
 
514
 - (void)visualizeRandom:(NSTimer *)timer {
520
 - (void)visualizeRandom:(NSTimer *)timer {
515
-    if ([serial isOpen]) {
516
-        [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", rand() % 256, rand() % 256, rand() % 256]];
517
-    }
521
+    [self setLightsR:rand() % 256 G:rand() % 256 B:rand() % 256];
518
 }
522
 }
519
 
523
 
520
 - (BOOL)timedVisualization:(NSString *)mode {
524
 - (BOOL)timedVisualization:(NSString *)mode {
593
                 unsigned char red = [color redComponent] * 255;
597
                 unsigned char red = [color redComponent] * 255;
594
                 unsigned char green = [color greenComponent] * 255;
598
                 unsigned char green = [color greenComponent] * 255;
595
                 unsigned char blue = [color blueComponent] * 255;
599
                 unsigned char blue = [color blueComponent] * 255;
596
-                NSString *string = [NSString stringWithFormat:@"RGB %d %d %d\n", red, green, blue];
597
-                
598
-                if ([serial isOpen]) {
599
-                    [serial sendString:string];
600
-                }
600
+                [self setLightsR:red G:green B:blue];
601
                 
601
                 
602
                 break;
602
                 break;
603
             }
603
             }

+ 25
- 0
CaseLights/Base.lproj/MainMenu.xib View File

15
         <customObject id="Voe-Tx-rLC" customClass="AppDelegate">
15
         <customObject id="Voe-Tx-rLC" customClass="AppDelegate">
16
             <connections>
16
             <connections>
17
                 <outlet property="application" destination="-3" id="3TG-JL-HaQ"/>
17
                 <outlet property="application" destination="-3" id="3TG-JL-HaQ"/>
18
+                <outlet property="brightnessItem" destination="MYl-J7-okP" id="pON-9W-gMG"/>
19
+                <outlet property="brightnessLabel" destination="jtw-ZC-4zA" id="Inv-ur-QX5"/>
20
+                <outlet property="brightnessSlider" destination="axk-la-AqI" id="J4R-Mw-KN7"/>
18
                 <outlet property="buttonLights" destination="tLQ-BA-kGu" id="FZv-Ju-l7n"/>
21
                 <outlet property="buttonLights" destination="tLQ-BA-kGu" id="FZv-Ju-l7n"/>
19
                 <outlet property="buttonOff" destination="gT4-tm-Hvn" id="EEv-lf-Qo7"/>
22
                 <outlet property="buttonOff" destination="gT4-tm-Hvn" id="EEv-lf-Qo7"/>
20
                 <outlet property="menuAnimations" destination="FJm-ND-abA" id="iGI-Hg-tms"/>
23
                 <outlet property="menuAnimations" destination="FJm-ND-abA" id="iGI-Hg-tms"/>
693
                         <action selector="turnLEDsOff:" target="Voe-Tx-rLC" id="FQw-v8-IbT"/>
696
                         <action selector="turnLEDsOff:" target="Voe-Tx-rLC" id="FQw-v8-IbT"/>
694
                     </connections>
697
                     </connections>
695
                 </menuItem>
698
                 </menuItem>
699
+                <menuItem title="Brightness" id="Oh0-1U-yDg">
700
+                    <modifierMask key="keyEquivalentModifierMask"/>
701
+                    <menu key="submenu" title="Brightness" id="jRu-sM-3xB">
702
+                        <items>
703
+                            <menuItem title="Item" id="MYl-J7-okP">
704
+                                <modifierMask key="keyEquivalentModifierMask"/>
705
+                            </menuItem>
706
+                            <menuItem title="Value: 50%" enabled="NO" id="jtw-ZC-4zA">
707
+                                <modifierMask key="keyEquivalentModifierMask"/>
708
+                            </menuItem>
709
+                        </items>
710
+                    </menu>
711
+                </menuItem>
696
                 <menuItem isSeparatorItem="YES" id="umI-9c-ZzN"/>
712
                 <menuItem isSeparatorItem="YES" id="umI-9c-ZzN"/>
697
                 <menuItem title="Toggle Lights" keyEquivalent="w" id="tLQ-BA-kGu">
713
                 <menuItem title="Toggle Lights" keyEquivalent="w" id="tLQ-BA-kGu">
698
                     <connections>
714
                     <connections>
730
             </items>
746
             </items>
731
             <point key="canvasLocation" x="368" y="324.5"/>
747
             <point key="canvasLocation" x="368" y="324.5"/>
732
         </menu>
748
         </menu>
749
+        <slider verticalHuggingPriority="750" id="axk-la-AqI">
750
+            <rect key="frame" x="0.0" y="0.0" width="129" height="30"/>
751
+            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
752
+            <sliderCell key="cell" alignment="left" maxValue="100" doubleValue="50" tickMarkPosition="below" numberOfTickMarks="11" sliderType="linear" id="IRQ-g4-OAA"/>
753
+            <connections>
754
+                <action selector="brightnessMoved:" target="Voe-Tx-rLC" id="Wxj-Xc-ZtT"/>
755
+            </connections>
756
+            <point key="canvasLocation" x="370.5" y="496.5"/>
757
+        </slider>
733
     </objects>
758
     </objects>
734
 </document>
759
 </document>

+ 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>106</string>
24
+	<string>114</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