Browse Source

Implemented sending command for static colors and lights

Thomas Buck 8 years ago
parent
commit
4c42101104
3 changed files with 94 additions and 72 deletions
  1. 92
    70
      CaseLights/AppDelegate.m
  2. 1
    1
      CaseLights/Info.plist
  3. 1
    1
      CaseLights/Serial.m

+ 92
- 70
CaseLights/AppDelegate.m View File

23
 #define TEXT_CPU_TEMPERATURE @"CPU Temperature"
23
 #define TEXT_CPU_TEMPERATURE @"CPU Temperature"
24
 #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
24
 #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
25
 
25
 
26
+#define KEY_CPU_TEMPERATURE @"TC0D"
27
+#define KEY_GPU_TEMPERATURE @"TG0D"
28
+
26
 @interface AppDelegate ()
29
 @interface AppDelegate ()
27
 
30
 
28
 @property (weak) NSMenuItem *lastLEDMode;
31
 @property (weak) NSMenuItem *lastLEDMode;
60
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
63
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
61
     NSString *lastMode = [store stringForKey:PREF_LED_MODE];
64
     NSString *lastMode = [store stringForKey:PREF_LED_MODE];
62
     
65
     
66
+    // Prepare serial port menu
67
+    NSArray *ports = [Serial listSerialPorts];
68
+    if ([ports count] > 0) {
69
+        [menuPorts removeAllItems];
70
+        for (int i = 0; i < [ports count]; i++) {
71
+            // Add Menu Item for this port
72
+            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
73
+            [menuPorts addItem:item];
74
+            
75
+            // Set Enabled if it was used the last time
76
+            if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
77
+                [[menuPorts itemAtIndex:i] setState:NSOnState];
78
+                
79
+                // Try to open serial port
80
+                [serial setPortName:savedPort];
81
+                if ([serial openPort]) {
82
+                    // Unselect it when an error occured opening the port
83
+                    [[menuPorts itemAtIndex:i] setState:NSOffState];
84
+                }
85
+            }
86
+        }
87
+    }
88
+    
89
+    // Select "Off" button if it was last selected
63
     if ([lastMode isEqualToString:@""]) {
90
     if ([lastMode isEqualToString:@""]) {
64
-        [buttonOff setState:NSOnState];
91
+        [buttonOff setState:NSOffState];
92
+        [self turnLEDsOff:buttonOff];
65
     }
93
     }
66
     
94
     
67
     // Prepare static colors menu
95
     // Prepare static colors menu
77
     for (NSString *key in [staticColors allKeys]) {
105
     for (NSString *key in [staticColors allKeys]) {
78
         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
106
         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
79
         if ([key isEqualToString:lastMode]) {
107
         if ([key isEqualToString:lastMode]) {
80
-            [item setState:NSOnState];
108
+            [self selectedVisualization:item];
81
         }
109
         }
82
         [menuColors addItem:item];
110
         [menuColors addItem:item];
83
     }
111
     }
91
     NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
119
     NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
92
     
120
     
93
     JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
121
     JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
94
-    NSLog(@"Memory Usage: %.2fGB Free, %.2fGB Used, %.2fGB Active, %.2fGB Inactive, %.2fGB Compressed, %.2fGB Wired\n", memoryUsageInfo.freeMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.usedMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.activeMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.inactiveMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.compressedMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.wiredMemory / (1024.0 * 1024.0 * 1024.0));
122
+    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));
95
 #endif
123
 #endif
96
     
124
     
97
     // Add CPU Usage menu item
125
     // Add CPU Usage menu item
98
     NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
126
     NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
99
     if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
127
     if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
100
-        [cpuUsageItem setState:NSOnState];
128
+        [self selectedVisualization:cpuUsageItem];
101
     }
129
     }
102
     [menuVisualizations addItem:cpuUsageItem];
130
     [menuVisualizations addItem:cpuUsageItem];
103
     
131
     
104
     // Add Memory Usage item
132
     // Add Memory Usage item
105
     NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_RAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
133
     NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_RAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
106
     if ([lastMode isEqualToString:TEXT_RAM_USAGE]) {
134
     if ([lastMode isEqualToString:TEXT_RAM_USAGE]) {
107
-        [memoryUsageItem setState:NSOnState];
135
+        [self selectedVisualization:memoryUsageItem];
108
     }
136
     }
109
     [menuVisualizations addItem:memoryUsageItem];
137
     [menuVisualizations addItem:memoryUsageItem];
110
     
138
     
117
     } else {
145
     } else {
118
         NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
146
         NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
119
         if ([lastMode isEqualToString:TEXT_GPU_USAGE]) {
147
         if ([lastMode isEqualToString:TEXT_GPU_USAGE]) {
120
-            [itemUsage setState:NSOnState];
148
+            [self selectedVisualization:itemUsage];
121
         }
149
         }
122
         [menuVisualizations addItem:itemUsage];
150
         [menuVisualizations addItem:itemUsage];
123
         
151
         
124
         NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:TEXT_VRAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
152
         NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:TEXT_VRAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
125
         if ([lastMode isEqualToString:TEXT_VRAM_USAGE]) {
153
         if ([lastMode isEqualToString:TEXT_VRAM_USAGE]) {
126
-            [itemVRAM setState:NSOnState];
154
+            [self selectedVisualization:itemVRAM];
127
         }
155
         }
128
         [menuVisualizations addItem:itemVRAM];
156
         [menuVisualizations addItem:itemVRAM];
129
     }
157
     }
137
         NSString *name = [smc humanReadableNameForKey:key];
165
         NSString *name = [smc humanReadableNameForKey:key];
138
         NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
166
         NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
139
 #endif
167
 #endif
140
-        
141
-        if ([key isEqualToString:@"TC0D"]) {
168
+
169
+        if ([key isEqualToString:KEY_CPU_TEMPERATURE]) {
142
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
170
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
143
             if ([lastMode isEqualToString:TEXT_CPU_TEMPERATURE]) {
171
             if ([lastMode isEqualToString:TEXT_CPU_TEMPERATURE]) {
144
-                [item setState:NSOnState];
172
+                [self selectedVisualization:item];
145
             }
173
             }
146
             [menuVisualizations addItem:item];
174
             [menuVisualizations addItem:item];
147
         }
175
         }
148
         
176
         
149
-        if ([key isEqualToString:@"TG0D"]) {
177
+        if ([key isEqualToString:KEY_GPU_TEMPERATURE]) {
150
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
178
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
151
             if ([lastMode isEqualToString:TEXT_GPU_TEMPERATURE]) {
179
             if ([lastMode isEqualToString:TEXT_GPU_TEMPERATURE]) {
152
-                [item setState:NSOnState];
180
+                [self selectedVisualization:item];
153
             }
181
             }
154
             [menuVisualizations addItem:item];
182
             [menuVisualizations addItem:item];
155
         }
183
         }
156
     }
184
     }
157
     
185
     
158
-    // Prepare serial port menu
159
-    NSArray *ports = [Serial listSerialPorts];
160
-    if ([ports count] > 0) {
161
-        [menuPorts removeAllItems];
162
-        for (int i = 0; i < [ports count]; i++) {
163
-            // Add Menu Item for this port
164
-            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
165
-            [menuPorts addItem:item];
166
-            
167
-            // Set Enabled if it was used the last time
168
-            if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
169
-                [[menuPorts itemAtIndex:i] setState:NSOnState];
170
-                
171
-                // Try to open serial port
172
-                [serial setPortName:savedPort];
173
-                if ([serial openPort]) {
174
-                    // Unselect it when an error occured opening the port
175
-                    [[menuPorts itemAtIndex:i] setState:NSOffState];
176
-                }
177
-            }
178
-        }
179
-    }
180
-    
181
     // Restore previously used lights configuration
186
     // Restore previously used lights configuration
182
     if (turnOnLights) {
187
     if (turnOnLights) {
183
-        // TODO Turn on lights
188
+        // Turn on lights
189
+        if ([serial isOpen]) {
190
+            [serial sendString:@"UV 1\n"];
191
+        }
184
         
192
         
185
         [buttonLights setState:NSOnState];
193
         [buttonLights setState:NSOnState];
186
     } else {
194
     } else {
187
-        // TODO Turn off lights
188
-        
195
+        // Turn off lights
196
+        if ([serial isOpen]) {
197
+            [serial sendString:@"UV 0\n"];
198
+        }
189
     }
199
     }
190
-    
191
-    // TODO Restore previously used LED configuration
192
 }
200
 }
193
 
201
 
194
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
202
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
248
         [store synchronize];
256
         [store synchronize];
249
         
257
         
250
 #ifdef DEBUG
258
 #ifdef DEBUG
251
-        NSLog(@"Stored new mode: \"\"!\n");
259
+        NSLog(@"Stored new mode: \"off\"!\n");
252
 #endif
260
 #endif
253
         
261
         
254
-        // TODO Send command to turn off LEDs
262
+        // Send command to turn off LEDs
263
+        if ([serial isOpen]) {
264
+            [serial sendString:@"RGB 0 0 0\n"];
265
+        }
255
     } else {
266
     } else {
256
         // Try to restore last LED setting
267
         // Try to restore last LED setting
257
         if (lastLEDMode != nil) {
268
         if (lastLEDMode != nil) {
262
 
273
 
263
 - (IBAction)toggleLights:(NSMenuItem *)sender {
274
 - (IBAction)toggleLights:(NSMenuItem *)sender {
264
     if ([sender state] == NSOffState) {
275
     if ([sender state] == NSOffState) {
265
-        // TODO Turn on lights
276
+        // Turn on lights
277
+        if ([serial isOpen]) {
278
+            [serial sendString:@"UV 1\n"];
279
+        }
266
         
280
         
267
         [sender setState:NSOnState];
281
         [sender setState:NSOnState];
268
     } else {
282
     } else {
269
-        // TODO Turn off lights
283
+        // Turn off lights
284
+        if ([serial isOpen]) {
285
+            [serial sendString:@"UV 0\n"];
286
+        }
270
         
287
         
271
         [sender setState:NSOffState];
288
         [sender setState:NSOffState];
272
     }
289
     }
279
 
296
 
280
 - (void)selectedVisualization:(NSMenuItem *)sender {
297
 - (void)selectedVisualization:(NSMenuItem *)sender {
281
     // Turn off all other LED menu items
298
     // Turn off all other LED menu items
282
-    for (int i = 0; i < [menuColors numberOfItems]; i++) {
283
-        [[menuColors itemAtIndex:i] setState:NSOffState];
299
+    if (menuColors != nil) {
300
+        for (int i = 0; i < [menuColors numberOfItems]; i++) {
301
+            [[menuColors itemAtIndex:i] setState:NSOffState];
302
+        }
284
     }
303
     }
285
-    for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
286
-        [[menuAnimations itemAtIndex:i] setState:NSOffState];
304
+    if (menuAnimations != nil) {
305
+        for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
306
+            [[menuAnimations itemAtIndex:i] setState:NSOffState];
307
+        }
287
     }
308
     }
288
-    for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
289
-        [[menuVisualizations itemAtIndex:i] setState:NSOffState];
309
+    if (menuVisualizations != nil) {
310
+        for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
311
+            [[menuVisualizations itemAtIndex:i] setState:NSOffState];
312
+        }
290
     }
313
     }
291
     [buttonOff setState:NSOffState];
314
     [buttonOff setState:NSOffState];
292
     [sender setState:NSOnState];
315
     [sender setState:NSOnState];
293
     
316
     
294
     if ([sender.title isEqualToString:TEXT_GPU_USAGE]) {
317
     if ([sender.title isEqualToString:TEXT_GPU_USAGE]) {
295
-        // TODO store new selection
296
-        
297
         // TODO send command
318
         // TODO send command
298
-    } else if ([sender.title isEqualToString:TEXT_VRAM_USAGE]) {
299
-        // TODO store new selection
300
         
319
         
320
+    } else if ([sender.title isEqualToString:TEXT_VRAM_USAGE]) {
301
         // TODO send command
321
         // TODO send command
302
-    } else if ([sender.title isEqualToString:TEXT_GPU_TEMPERATURE]) {
303
-        // TODO store new selection
304
         
322
         
323
+    } else if ([sender.title isEqualToString:TEXT_GPU_TEMPERATURE]) {
305
         // TODO send command
324
         // TODO send command
306
-    } else if ([sender.title isEqualToString:TEXT_CPU_USAGE]) {
307
-        // TODO store new selection
308
         
325
         
326
+    } else if ([sender.title isEqualToString:TEXT_CPU_USAGE]) {
309
         // TODO send command
327
         // TODO send command
310
-    } else if ([sender.title isEqualToString:TEXT_CPU_TEMPERATURE]) {
311
-        // TODO store new selection
312
         
328
         
329
+    } else if ([sender.title isEqualToString:TEXT_CPU_TEMPERATURE]) {
313
         // TODO send command
330
         // TODO send command
314
         
331
         
315
     } else if ([sender.title isEqualToString:TEXT_RAM_USAGE]) {
332
     } else if ([sender.title isEqualToString:TEXT_RAM_USAGE]) {
316
-        // TODO store new selection
317
-        
318
         // TODO send command
333
         // TODO send command
319
         
334
         
320
     } else {
335
     } else {
321
         BOOL found = NO;
336
         BOOL found = NO;
322
         
337
         
323
         // Check if a static color was selected
338
         // Check if a static color was selected
324
-        for (NSString *key in [staticColors allKeys]) {
325
-            if ([sender.title isEqualToString:key]) {
326
-                found = YES;
327
-                
328
-                // TODO store new selection
329
-                
330
-                // TODO send command
331
-                
339
+        if (staticColors != nil) {
340
+            for (NSString *key in [staticColors allKeys]) {
341
+                if ([sender.title isEqualToString:key]) {
342
+                    found = YES;
343
+                    
344
+                    NSColor *color = [staticColors valueForKey:key];
345
+                    unsigned char red = [color redComponent] * 255;
346
+                    unsigned char green = [color greenComponent] * 255;
347
+                    unsigned char blue = [color blueComponent] * 255;
348
+                    NSString *string = [NSString stringWithFormat:@"RGB %d %d %d\n", red, green, blue];
349
+                    
350
+                    if ([serial isOpen]) {
351
+                        [serial sendString:string];
352
+                    }
353
+                }
332
             }
354
             }
333
         }
355
         }
334
         
356
         

+ 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>75</string>
24
+	<string>83</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>

+ 1
- 1
CaseLights/Serial.m View File

61
 #endif
61
 #endif
62
     
62
     
63
     // Open port read-only, without controlling terminal, non-blocking
63
     // Open port read-only, without controlling terminal, non-blocking
64
-    fd = open([portName UTF8String], O_RDONLY | O_NOCTTY | O_NONBLOCK);
64
+    fd = open([portName UTF8String], O_RDWR | O_NOCTTY | O_NONBLOCK);
65
     if (fd == -1) {
65
     if (fd == -1) {
66
         NSLog(@"Error opening serial port \"%@\": %s (%d)!\n", portName, strerror(errno), errno);
66
         NSLog(@"Error opening serial port \"%@\": %s (%d)!\n", portName, strerror(errno), errno);
67
         return 1;
67
         return 1;

Loading…
Cancel
Save