Browse Source

Removed Thread, moved code into Serial

Thomas Buck 8 years ago
parent
commit
fa409f2f03

+ 0
- 6
CaseLights.xcodeproj/project.pbxproj View File

@@ -11,7 +11,6 @@
11 11
 		E9109B151C2AAEFD00726111 /* SystemInfoKit.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = E9109B0B1C2AAC1B00726111 /* SystemInfoKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
12 12
 		E9109B191C2AAFE100726111 /* GPUStats.m in Sources */ = {isa = PBXBuildFile; fileRef = E9109B181C2AAFE100726111 /* GPUStats.m */; };
13 13
 		E9AB18011C29F8CD00BF3C51 /* Serial.m in Sources */ = {isa = PBXBuildFile; fileRef = E9AB18001C29F8CD00BF3C51 /* Serial.m */; };
14
-		E9AB18061C2A173500BF3C51 /* Thread.m in Sources */ = {isa = PBXBuildFile; fileRef = E9AB18051C2A173500BF3C51 /* Thread.m */; };
15 14
 		E9F13E031C28B3D3004C6B95 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E9F13E021C28B3D3004C6B95 /* AppDelegate.m */; };
16 15
 		E9F13E061C28B3D3004C6B95 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E9F13E051C28B3D3004C6B95 /* main.m */; };
17 16
 		E9F13E081C28B3D3004C6B95 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E9F13E071C28B3D3004C6B95 /* Assets.xcassets */; };
@@ -61,8 +60,6 @@
61 60
 		E9109B181C2AAFE100726111 /* GPUStats.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPUStats.m; path = CaseLights/GPUStats.m; sourceTree = "<group>"; };
62 61
 		E9AB17FF1C29F8CD00BF3C51 /* Serial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Serial.h; path = CaseLights/Serial.h; sourceTree = "<group>"; };
63 62
 		E9AB18001C29F8CD00BF3C51 /* Serial.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Serial.m; path = CaseLights/Serial.m; sourceTree = "<group>"; };
64
-		E9AB18041C2A173500BF3C51 /* Thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Thread.h; sourceTree = "<group>"; };
65
-		E9AB18051C2A173500BF3C51 /* Thread.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Thread.m; sourceTree = "<group>"; };
66 63
 		E9F13DFE1C28B3D3004C6B95 /* CaseLights.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CaseLights.app; sourceTree = BUILT_PRODUCTS_DIR; };
67 64
 		E9F13E011C28B3D3004C6B95 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
68 65
 		E9F13E021C28B3D3004C6B95 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -127,8 +124,6 @@
127 124
 			children = (
128 125
 				E9F13E011C28B3D3004C6B95 /* AppDelegate.h */,
129 126
 				E9F13E021C28B3D3004C6B95 /* AppDelegate.m */,
130
-				E9AB18041C2A173500BF3C51 /* Thread.h */,
131
-				E9AB18051C2A173500BF3C51 /* Thread.m */,
132 127
 				E9109B161C2AAFA200726111 /* LowLevel */,
133 128
 				E9F13E071C28B3D3004C6B95 /* Assets.xcassets */,
134 129
 				E9F13E091C28B3D3004C6B95 /* MainMenu.xib */,
@@ -262,7 +257,6 @@
262 257
 				E9109B191C2AAFE100726111 /* GPUStats.m in Sources */,
263 258
 				E9F13E061C28B3D3004C6B95 /* main.m in Sources */,
264 259
 				E9F13E031C28B3D3004C6B95 /* AppDelegate.m in Sources */,
265
-				E9AB18061C2A173500BF3C51 /* Thread.m in Sources */,
266 260
 			);
267 261
 			runOnlyForDeploymentPostprocessing = 0;
268 262
 		};

+ 39
- 20
CaseLights/AppDelegate.m View File

@@ -79,13 +79,37 @@
79 79
         // TODO Enable item if it was last used
80 80
     }
81 81
     
82
+    JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
83
+    
84
+#ifdef DEBUG
85
+    JSKMCPUUsageInfo cpuUsageInfo = systemMonitor.cpuUsageInfo;
86
+    NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
87
+    
88
+    JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
89
+    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));
90
+#endif
91
+    
92
+    // Add CPU Usage menu item
93
+    NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:@"CPU Usage" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
94
+    [menuVisualizations addItem:cpuUsageItem];
95
+    
96
+    // TODO enable item if it was last used
97
+    
98
+    // Add Memory Usage item
99
+    NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:@"RAM Usage" action:@selector(selectedMemoryVisualization:) keyEquivalent:@""];
100
+    [menuVisualizations addItem:memoryUsageItem];
101
+    
102
+    // TODO enable item if it was last used
103
+    
82 104
     // Check available temperatures and add menu items
83 105
     JSKSMC *smc = [JSKSMC smc];
84 106
     for (int i = 0; i < [[smc workingTempKeys] count]; i++) {
85 107
         NSString *key = [smc.workingTempKeys objectAtIndex:i];
86
-        //NSString *name = [smc humanReadableNameForKey:key];
87 108
         
88
-        //NSLog(@"%@: %@\n", key, name);
109
+#ifdef DEBUG
110
+        NSString *name = [smc humanReadableNameForKey:key];
111
+        NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
112
+#endif
89 113
         
90 114
         if ([key isEqualToString:@"TC0D"]) {
91 115
             NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"CPU Temperature" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
@@ -94,26 +118,14 @@
94 118
             // TODO enable item if it was last used
95 119
         }
96 120
         
97
-        // TODO add GPU temperature item
121
+        if ([key isEqualToString:@"TG0D"]) {
122
+            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"GPU Temperature" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
123
+            [menuVisualizations addItem:item];
124
+            
125
+            // TODO enable item if it was last used
126
+        }
98 127
     }
99 128
     
100
-    // Add CPU Usage menu item
101
-    NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:@"CPU Usage" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
102
-    [menuVisualizations addItem:cpuUsageItem];
103
-    
104
-    // TODO enable item if it was last used
105
-    
106
-    // Add Memory Usage item
107
-    NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:@"RAM Usage" action:@selector(selectedMemoryVisualization:) keyEquivalent:@""];
108
-    [menuVisualizations addItem:memoryUsageItem];
109
-    
110
-    // TODO enable item if it was last used
111
-    
112
-    JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
113
-    //JSKMCPUUsageInfo cpuUsageInfo = systemMonitor.cpuUsageInfo;
114
-    JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
115
-    NSLog(@"Memory Usage: %lld Free, %lld Used, %lld Active, %lld Inactive, %lld Compressed, %lld Wired\n", memoryUsageInfo.freeMemory / 1000000, memoryUsageInfo.usedMemory / 1000000, memoryUsageInfo.activeMemory / 1000000, memoryUsageInfo.inactiveMemory / 1000000, memoryUsageInfo.compressedMemory / 1000000, memoryUsageInfo.wiredMemory / 1000000);
116
-    
117 129
     // Prepare serial port menu
118 130
     NSArray *ports = [Serial listSerialPorts];
119 131
     if ([ports count] > 0) {
@@ -237,6 +249,13 @@
237 249
         // TODO store new selection
238 250
         
239 251
         // TODO send command
252
+    } else if ([sender.title isEqualToString:@"GPU Temperature"]) {
253
+        // Turn on "off" menu item
254
+        [sender setState:NSOnState];
255
+        
256
+        // TODO store new selection
257
+        
258
+        // TODO send command
240 259
     } else {
241 260
         NSLog(@"Unknown GPU Visualization selected!\n");
242 261
     }

+ 4
- 3
CaseLights/GPUStats.m View File

@@ -43,12 +43,13 @@
43 43
                         CFNumberGetValue((CFNumberRef)gpuUsage, kCFNumberSInt64Type, &gpuUsageNum);
44 44
                         CFNumberGetValue((CFNumberRef)freeVRAM, kCFNumberSInt64Type, &freeVRAMNum);
45 45
                         CFNumberGetValue((CFNumberRef)usedVRAM, kCFNumberSInt64Type, &usedVRAMNum);
46
-                        *usage = [NSNumber numberWithDouble:gpuUsageNum / (double)10000000];
46
+                        *usage = [NSNumber numberWithDouble:gpuUsageNum / 10000000.0];
47 47
                         *free = [NSNumber numberWithDouble:freeVRAMNum];
48 48
                         *used = [NSNumber numberWithDouble:usedVRAMNum];
49 49
                         
50
-                        //NSLog(@"GPU: %.3f%% VRAM: %.3f%%\n", gpuUsageNum / (double)10000000,
51
-                              //usedVRAMNum / (double)(freeVRAMNum + usedVRAMNum) * 100.0);
50
+#ifdef DEBUG
51
+                        NSLog(@"GPU: %.3f%% VRAM: %.3f%% (%.2fMB)\n", gpuUsageNum / 10000000.0, ((double)usedVRAMNum) / (freeVRAMNum + usedVRAMNum) * 100.0, (usedVRAMNum + freeVRAMNum) / (1024.0 * 1024.0));
52
+#endif
52 53
                     }
53 54
                 }
54 55
                 CFRelease(services);

+ 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>52</string>
24
+	<string>61</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

+ 6
- 0
CaseLights/Serial.h View File

@@ -10,6 +10,12 @@
10 10
 
11 11
 @interface Serial : NSObject
12 12
 
13
+@property int fd;
14
+@property (strong) NSString *portName;
15
+
16
+- (NSInteger)openPort;
17
+- (NSInteger)hasData;
18
+
13 19
 + (NSArray *)listSerialPorts;
14 20
 
15 21
 @end

+ 75
- 0
CaseLights/Serial.m View File

@@ -13,6 +13,12 @@
13 13
 #import <IOKit/IOKitLib.h>
14 14
 #import <IOKit/serial/IOSerialKeys.h>
15 15
 
16
+#import <termios.h>
17
+#import <fcntl.h>
18
+#import <unistd.h>
19
+#import <poll.h>
20
+#import <sys/ioctl.h>
21
+
16 22
 #import "Serial.h"
17 23
 
18 24
 kern_return_t findSerialPorts(io_iterator_t *matches);
@@ -20,6 +26,75 @@ kern_return_t getSerialPortPath(io_iterator_t serialPortIterator, char **deviceF
20 26
 
21 27
 @implementation Serial
22 28
 
29
+@synthesize fd, portName;
30
+
31
+- (NSInteger)openPort {
32
+    // We need a port name
33
+    if (portName == nil) {
34
+        NSLog(@"Can't open serial port without name!\n");
35
+        return 1;
36
+    }
37
+    
38
+    // Check if there was already a port opened
39
+    if (fd != -1) {
40
+        NSLog(@"Closing previously opened serial port \"%@\"!\n", portName);
41
+        close(fd);
42
+    }
43
+    
44
+    // Open port read-only, without controlling terminal, non-blocking
45
+    fd = open([portName UTF8String], O_RDONLY | O_NOCTTY | O_NONBLOCK);
46
+    if (fd == -1) {
47
+        NSLog(@"Error opening serial port \"%@\": %s (%d)!\n", portName, strerror(errno), errno);
48
+        return 1;
49
+    }
50
+    
51
+    // Prevent additional opens except by root-owned processes
52
+    if (ioctl(fd, TIOCEXCL) == -1) {
53
+        NSLog(@"Error enabling exclusive access on \"%@\": %s (%d)!\n", portName, strerror(errno), errno);
54
+        return 1;
55
+    }
56
+    
57
+    fcntl(fd, F_SETFL, 0); // Enable blocking I/O
58
+    
59
+    // Read current settings
60
+    struct termios options;
61
+    tcgetattr(fd, &options);
62
+    
63
+    // Clear all settings
64
+    options.c_lflag = 0;
65
+    options.c_oflag = 0;
66
+    options.c_iflag = 0;
67
+    options.c_cflag = 0;
68
+    
69
+    options.c_cflag |= CS8; // 8 data bits
70
+    options.c_cflag |= CREAD; // Enable receiver
71
+    options.c_cflag |= CLOCAL; // Ignore modem status lines
72
+    
73
+    // Set Baudrate
74
+    cfsetispeed(&options, B115200);
75
+    cfsetospeed(&options, B115200);
76
+    
77
+    options.c_cc[VMIN] = 0; // Return even with zero bytes...
78
+    options.c_cc[VTIME] = 1; // ...but only after .1 seconds
79
+    
80
+    // Set new settings
81
+    tcsetattr(fd, TCSANOW, &options);
82
+    tcflush(fd, TCIOFLUSH);
83
+    
84
+    return 0;
85
+}
86
+
87
+- (NSInteger)hasData {
88
+    struct pollfd fds;
89
+    fds.fd = fd;
90
+    fds.events = (POLLIN | POLLPRI); // Data may be read
91
+    if (poll(&fds, 1, 0) > 0) {
92
+        return 1;
93
+    } else {
94
+        return 0;
95
+    }
96
+}
97
+
23 98
 + (NSArray *)listSerialPorts {
24 99
     // Get Iterator with all serial ports
25 100
     io_iterator_t serialPortIterator;

+ 0
- 23
CaseLights/Thread.h View File

@@ -1,23 +0,0 @@
1
-//
2
-//  Thread.h
3
-//  SerialGamepad
4
-//
5
-//  Created by Thomas Buck on 15.12.15.
6
-//  Copyright © 2015 xythobuz. All rights reserved.
7
-//
8
-
9
-#import <Foundation/Foundation.h>
10
-
11
-@class AppDelegate;
12
-
13
-@interface Thread : NSThread
14
-
15
-@property BOOL running;
16
-@property int fd;
17
-@property (strong) NSString *portName;
18
-@property (weak) AppDelegate *appDelegate;
19
-
20
-- (id)initWithDelegate:(AppDelegate *)delegate;
21
-- (NSInteger)openPort;
22
-
23
-@end

+ 0
- 93
CaseLights/Thread.m View File

@@ -1,93 +0,0 @@
1
-//
2
-//  Thread.m
3
-//  SerialGamepad
4
-//
5
-//  Created by Thomas Buck on 15.12.15.
6
-//  Copyright © 2015 xythobuz. All rights reserved.
7
-//
8
-
9
-#import <termios.h>
10
-#import <fcntl.h>
11
-#import <unistd.h>
12
-#include <poll.h>
13
-
14
-#import "Thread.h"
15
-#import "AppDelegate.h"
16
-
17
-@implementation Thread
18
-
19
-@synthesize running, fd, portName, appDelegate;
20
-
21
-- (id)initWithDelegate:(AppDelegate *)delegate {
22
-    self = [super init];
23
-    if (self != nil) {
24
-        appDelegate = delegate;
25
-    }
26
-    return self;
27
-}
28
-
29
-- (NSInteger)openPort {
30
-    if (portName == nil) {
31
-        return 1;
32
-    }
33
-    
34
-    // Open port read-only, without controlling terminal, non-blocking
35
-    fd = open([portName UTF8String], O_RDONLY | O_NOCTTY | O_NONBLOCK);
36
-    if (fd == -1) {
37
-        NSLog(@"Error opening serial port \"%@\"!\n", portName);
38
-        return 1;
39
-    }
40
-    
41
-    fcntl(fd, F_SETFL, 0); // Enable blocking I/O
42
-    
43
-    // Read current settings
44
-    struct termios options;
45
-    tcgetattr(fd, &options);
46
-    
47
-    options.c_lflag = 0;
48
-    options.c_oflag = 0;
49
-    options.c_iflag = 0;
50
-    options.c_cflag = 0;
51
-    
52
-    options.c_cflag |= CS8; // 8 data bits
53
-    options.c_cflag |= CREAD; // Enable receiver
54
-    options.c_cflag |= CLOCAL; // Ignore modem status lines
55
-    
56
-    cfsetispeed(&options, B115200);
57
-    cfsetospeed(&options, B115200);
58
-    
59
-    options.c_cc[VMIN] = 0; // Return even with zero bytes...
60
-    options.c_cc[VTIME] = 1; // ...but only after .1 seconds
61
-    
62
-    // Set new settings
63
-    tcsetattr(fd, TCSANOW, &options);
64
-    tcflush(fd, TCIOFLUSH);
65
-    
66
-    return 0;
67
-}
68
-
69
-- (NSInteger)hasData {
70
-    struct pollfd fds;
71
-    fds.fd = fd;
72
-    fds.events = (POLLIN | POLLPRI); // Data may be read
73
-    if (poll(&fds, 1, 0) > 0) {
74
-        return 1;
75
-    } else {
76
-        return 0;
77
-    }
78
-}
79
-
80
-- (void)main {
81
-    NSLog(@"Connection running...\n");
82
-    
83
-    running = YES;
84
-    while (running) {
85
-        
86
-    }
87
-    
88
-    close(fd);
89
-    NSLog(@"Connection closed...\n");
90
-    fd = -1;
91
-}
92
-
93
-@end

Loading…
Cancel
Save