Pārlūkot izejas kodu

Implemented sending command for static colors and lights

Thomas Buck 9 gadus atpakaļ
vecāks
revīzija
4c42101104
3 mainītis faili ar 94 papildinājumiem un 72 dzēšanām
  1. 92
    70
      CaseLights/AppDelegate.m
  2. 1
    1
      CaseLights/Info.plist
  3. 1
    1
      CaseLights/Serial.m

+ 92
- 70
CaseLights/AppDelegate.m Parādīt failu

@@ -23,6 +23,9 @@
23 23
 #define TEXT_CPU_TEMPERATURE @"CPU Temperature"
24 24
 #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
25 25
 
26
+#define KEY_CPU_TEMPERATURE @"TC0D"
27
+#define KEY_GPU_TEMPERATURE @"TG0D"
28
+
26 29
 @interface AppDelegate ()
27 30
 
28 31
 @property (weak) NSMenuItem *lastLEDMode;
@@ -60,8 +63,33 @@
60 63
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
61 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 90
     if ([lastMode isEqualToString:@""]) {
64
-        [buttonOff setState:NSOnState];
91
+        [buttonOff setState:NSOffState];
92
+        [self turnLEDsOff:buttonOff];
65 93
     }
66 94
     
67 95
     // Prepare static colors menu
@@ -77,7 +105,7 @@
77 105
     for (NSString *key in [staticColors allKeys]) {
78 106
         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
79 107
         if ([key isEqualToString:lastMode]) {
80
-            [item setState:NSOnState];
108
+            [self selectedVisualization:item];
81 109
         }
82 110
         [menuColors addItem:item];
83 111
     }
@@ -91,20 +119,20 @@
91 119
     NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
92 120
     
93 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 123
 #endif
96 124
     
97 125
     // Add CPU Usage menu item
98 126
     NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
99 127
     if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
100
-        [cpuUsageItem setState:NSOnState];
128
+        [self selectedVisualization:cpuUsageItem];
101 129
     }
102 130
     [menuVisualizations addItem:cpuUsageItem];
103 131
     
104 132
     // Add Memory Usage item
105 133
     NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_RAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
106 134
     if ([lastMode isEqualToString:TEXT_RAM_USAGE]) {
107
-        [memoryUsageItem setState:NSOnState];
135
+        [self selectedVisualization:memoryUsageItem];
108 136
     }
109 137
     [menuVisualizations addItem:memoryUsageItem];
110 138
     
@@ -117,13 +145,13 @@
117 145
     } else {
118 146
         NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
119 147
         if ([lastMode isEqualToString:TEXT_GPU_USAGE]) {
120
-            [itemUsage setState:NSOnState];
148
+            [self selectedVisualization:itemUsage];
121 149
         }
122 150
         [menuVisualizations addItem:itemUsage];
123 151
         
124 152
         NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:TEXT_VRAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
125 153
         if ([lastMode isEqualToString:TEXT_VRAM_USAGE]) {
126
-            [itemVRAM setState:NSOnState];
154
+            [self selectedVisualization:itemVRAM];
127 155
         }
128 156
         [menuVisualizations addItem:itemVRAM];
129 157
     }
@@ -137,58 +165,38 @@
137 165
         NSString *name = [smc humanReadableNameForKey:key];
138 166
         NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
139 167
 #endif
140
-        
141
-        if ([key isEqualToString:@"TC0D"]) {
168
+
169
+        if ([key isEqualToString:KEY_CPU_TEMPERATURE]) {
142 170
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
143 171
             if ([lastMode isEqualToString:TEXT_CPU_TEMPERATURE]) {
144
-                [item setState:NSOnState];
172
+                [self selectedVisualization:item];
145 173
             }
146 174
             [menuVisualizations addItem:item];
147 175
         }
148 176
         
149
-        if ([key isEqualToString:@"TG0D"]) {
177
+        if ([key isEqualToString:KEY_GPU_TEMPERATURE]) {
150 178
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
151 179
             if ([lastMode isEqualToString:TEXT_GPU_TEMPERATURE]) {
152
-                [item setState:NSOnState];
180
+                [self selectedVisualization:item];
153 181
             }
154 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 186
     // Restore previously used lights configuration
182 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 193
         [buttonLights setState:NSOnState];
186 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 202
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
@@ -248,10 +256,13 @@
248 256
         [store synchronize];
249 257
         
250 258
 #ifdef DEBUG
251
-        NSLog(@"Stored new mode: \"\"!\n");
259
+        NSLog(@"Stored new mode: \"off\"!\n");
252 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 266
     } else {
256 267
         // Try to restore last LED setting
257 268
         if (lastLEDMode != nil) {
@@ -262,11 +273,17 @@
262 273
 
263 274
 - (IBAction)toggleLights:(NSMenuItem *)sender {
264 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 281
         [sender setState:NSOnState];
268 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 288
         [sender setState:NSOffState];
272 289
     }
@@ -279,56 +296,61 @@
279 296
 
280 297
 - (void)selectedVisualization:(NSMenuItem *)sender {
281 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 314
     [buttonOff setState:NSOffState];
292 315
     [sender setState:NSOnState];
293 316
     
294 317
     if ([sender.title isEqualToString:TEXT_GPU_USAGE]) {
295
-        // TODO store new selection
296
-        
297 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 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 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 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 330
         // TODO send command
314 331
         
315 332
     } else if ([sender.title isEqualToString:TEXT_RAM_USAGE]) {
316
-        // TODO store new selection
317
-        
318 333
         // TODO send command
319 334
         
320 335
     } else {
321 336
         BOOL found = NO;
322 337
         
323 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 Parādīt failu

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

+ 1
- 1
CaseLights/Serial.m Parādīt failu

@@ -61,7 +61,7 @@
61 61
 #endif
62 62
     
63 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 65
     if (fd == -1) {
66 66
         NSLog(@"Error opening serial port \"%@\": %s (%d)!\n", portName, strerror(errno), errno);
67 67
         return 1;

Notiek ielāde…
Atcelt
Saglabāt