Simple RGB LED controller for Mac OS X
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AppDelegate.m 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. //
  2. // AppDelegate.m
  3. // CaseLights
  4. //
  5. // Created by Thomas Buck on 21.12.15.
  6. // Copyright © 2015 xythobuz. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "Serial.h"
  10. #import "GPUStats.h"
  11. #import "SystemInfoKit/SystemInfoKit.h"
  12. #define PREF_SERIAL_PORT @"SerialPort"
  13. #define PREF_LIGHTS_STATE @"LightState"
  14. #define PREF_LED_MODE @"LEDMode"
  15. #define PREF_BRIGHTNESS @"Brightness"
  16. #define TEXT_CPU_USAGE @"CPU Usage"
  17. #define TEXT_RAM_USAGE @"RAM Usage"
  18. #define TEXT_GPU_USAGE @"GPU Usage"
  19. #define TEXT_VRAM_USAGE @"VRAM Usage"
  20. #define TEXT_CPU_TEMPERATURE @"CPU Temperature"
  21. #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
  22. #define TEXT_RGB_FADE @"RGB Fade"
  23. #define TEXT_HSV_FADE @"HSV Fade"
  24. #define TEXT_RANDOM @"Random"
  25. #define KEY_CPU_TEMPERATURE @"TC0D"
  26. #define KEY_GPU_TEMPERATURE @"TG0D"
  27. // Temperature in Celsius
  28. #define CPU_TEMP_MIN 20
  29. #define CPU_TEMP_MAX 90
  30. // HSV Color (S = V = 1)
  31. #define CPU_COLOR_MIN 120
  32. #define CPU_COLOR_MAX 0
  33. #define GPU_TEMP_MIN 20
  34. #define GPU_TEMP_MAX 90
  35. #define GPU_COLOR_MIN 120
  36. #define GPU_COLOR_MAX 0
  37. #define RAM_COLOR_MIN 0
  38. #define RAM_COLOR_MAX 120
  39. @interface AppDelegate ()
  40. @property (strong) NSStatusItem *statusItem;
  41. @property (strong) NSImage *statusImage;
  42. @property (strong) NSDictionary *staticColors;
  43. @property (strong) NSTimer *animation;
  44. @property (strong) Serial *serial;
  45. @property (strong) NSMenuItem *lastLEDMode;
  46. @end
  47. @implementation AppDelegate
  48. @synthesize statusMenu, application;
  49. @synthesize menuColors, menuAnimations, menuVisualizations, menuPorts;
  50. @synthesize buttonOff, buttonLights;
  51. @synthesize brightnessItem, brightnessSlider, brightnessLabel;
  52. @synthesize statusItem, statusImage;
  53. @synthesize staticColors, animation;
  54. @synthesize serial, lastLEDMode;
  55. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  56. srand((unsigned)time(NULL));
  57. serial = [[Serial alloc] init];
  58. lastLEDMode = nil;
  59. animation = nil;
  60. // Prepare status bar menu
  61. statusImage = [NSImage imageNamed:@"MenuIcon"];
  62. [statusImage setTemplate:YES];
  63. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  64. [statusItem setImage:statusImage];
  65. [statusItem setMenu:statusMenu];
  66. // Set default configuration values, load existing ones
  67. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  68. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
  69. [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
  70. [appDefaults setObject:@"" forKey:PREF_LED_MODE];
  71. [appDefaults setObject:[NSNumber numberWithFloat:50.0] forKey:PREF_BRIGHTNESS];
  72. [store registerDefaults:appDefaults];
  73. [store synchronize];
  74. NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
  75. BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
  76. NSString *lastMode = [store stringForKey:PREF_LED_MODE];
  77. float brightness = [store floatForKey:PREF_BRIGHTNESS];
  78. // Prepare brightness menu
  79. brightnessItem.view = brightnessSlider;
  80. [brightnessSlider setFloatValue:brightness];
  81. [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", brightness]];
  82. // Prepare serial port menu
  83. NSArray *ports = [Serial listSerialPorts];
  84. if ([ports count] > 0) {
  85. [menuPorts removeAllItems];
  86. for (int i = 0; i < [ports count]; i++) {
  87. // Add Menu Item for this port
  88. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  89. [menuPorts addItem:item];
  90. // Set Enabled if it was used the last time
  91. if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
  92. [[menuPorts itemAtIndex:i] setState:NSOnState];
  93. // Try to open serial port
  94. [serial setPortName:savedPort];
  95. if ([serial openPort]) {
  96. // Unselect it when an error occured opening the port
  97. [[menuPorts itemAtIndex:i] setState:NSOffState];
  98. }
  99. }
  100. }
  101. }
  102. // Select "Off" button if it was last selected
  103. if ([lastMode isEqualToString:@""]) {
  104. [buttonOff setState:NSOffState];
  105. [self turnLEDsOff:buttonOff];
  106. }
  107. // Prepare static colors menu
  108. staticColors = [NSDictionary dictionaryWithObjectsAndKeys:
  109. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:0.0f alpha:0.0f], @"Red",
  110. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:0.0f alpha:0.0f], @"Green",
  111. [NSColor colorWithCalibratedRed:0.0f green:0.0f blue:1.0f alpha:0.0f], @"Blue",
  112. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:1.0f alpha:0.0f], @"Cyan",
  113. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:1.0f alpha:0.0f], @"Magenta",
  114. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:0.0f alpha:0.0f], @"Yellow",
  115. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:0.0f], @"White",
  116. nil];
  117. for (NSString *key in [staticColors allKeys]) {
  118. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
  119. if ([key isEqualToString:lastMode]) {
  120. [self selectedVisualization:item];
  121. }
  122. [menuColors addItem:item];
  123. }
  124. // Prepare animations menu
  125. NSArray *animationStrings = [NSArray arrayWithObjects:
  126. TEXT_RGB_FADE,
  127. TEXT_HSV_FADE,
  128. TEXT_RANDOM,
  129. nil];
  130. for (NSString *key in animationStrings) {
  131. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
  132. if ([key isEqualToString:lastMode]) {
  133. [self selectedVisualization:item];
  134. }
  135. [menuAnimations addItem:item];
  136. }
  137. // Add CPU Usage menu item
  138. NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  139. if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
  140. [self selectedVisualization:cpuUsageItem];
  141. }
  142. [menuVisualizations addItem:cpuUsageItem];
  143. // Add Memory Usage item
  144. NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_RAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  145. if ([lastMode isEqualToString:TEXT_RAM_USAGE]) {
  146. [self selectedVisualization:memoryUsageItem];
  147. }
  148. [menuVisualizations addItem:memoryUsageItem];
  149. // Check if GPU Stats are available, add menu items if so
  150. NSNumber *usage;
  151. NSNumber *freeVRAM;
  152. NSNumber *usedVRAM;
  153. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  154. NSLog(@"Error reading GPU information\n");
  155. } else {
  156. NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  157. if ([lastMode isEqualToString:TEXT_GPU_USAGE]) {
  158. [self selectedVisualization:itemUsage];
  159. }
  160. [menuVisualizations addItem:itemUsage];
  161. NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:TEXT_VRAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  162. if ([lastMode isEqualToString:TEXT_VRAM_USAGE]) {
  163. [self selectedVisualization:itemVRAM];
  164. }
  165. [menuVisualizations addItem:itemVRAM];
  166. }
  167. // Check available temperatures and add menu items
  168. JSKSMC *smc = [JSKSMC smc];
  169. for (int i = 0; i < [[smc workingTempKeys] count]; i++) {
  170. NSString *key = [smc.workingTempKeys objectAtIndex:i];
  171. #ifdef DEBUG
  172. NSString *name = [smc humanReadableNameForKey:key];
  173. NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
  174. #endif
  175. if ([key isEqualToString:KEY_CPU_TEMPERATURE]) {
  176. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
  177. if ([lastMode isEqualToString:TEXT_CPU_TEMPERATURE]) {
  178. [self selectedVisualization:item];
  179. }
  180. [menuVisualizations addItem:item];
  181. }
  182. if ([key isEqualToString:KEY_GPU_TEMPERATURE]) {
  183. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
  184. if ([lastMode isEqualToString:TEXT_GPU_TEMPERATURE]) {
  185. [self selectedVisualization:item];
  186. }
  187. [menuVisualizations addItem:item];
  188. }
  189. }
  190. // Restore previously used lights configuration
  191. if (turnOnLights) {
  192. // Turn on lights
  193. if ([serial isOpen]) {
  194. [serial sendString:@"UV 1\n"];
  195. }
  196. [buttonLights setState:NSOnState];
  197. } else {
  198. // Turn off lights
  199. if ([serial isOpen]) {
  200. [serial sendString:@"UV 0\n"];
  201. }
  202. }
  203. }
  204. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  205. // Close serial port, if it was opened
  206. if ([serial isOpen]) {
  207. [serial closePort];
  208. }
  209. }
  210. - (void)setLightsR:(unsigned char)r G:(unsigned char)g B:(unsigned char)b {
  211. if ([serial isOpen]) {
  212. unsigned char red = r * ([brightnessSlider floatValue] / 100.0);
  213. unsigned char green = g * ([brightnessSlider floatValue] / 100.0);
  214. unsigned char blue = b * ([brightnessSlider floatValue] / 100.0);
  215. [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", red, green, blue]];
  216. } else {
  217. #ifdef DEBUG
  218. NSLog(@"Trying to send RGB without opened port!\n");
  219. #endif
  220. }
  221. }
  222. - (IBAction)relistSerialPorts:(id)sender {
  223. // Refill port list
  224. NSArray *ports = [Serial listSerialPorts];
  225. [menuPorts removeAllItems];
  226. for (int i = 0; i < [ports count]; i++) {
  227. // Add Menu Item for this port
  228. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  229. [menuPorts addItem:item];
  230. // Mark it if it is currently open
  231. if ([serial isOpen]) {
  232. if ([[ports objectAtIndex:i] isEqualToString:[serial portName]]) {
  233. [[menuPorts itemAtIndex:i] setState:NSOnState];
  234. }
  235. }
  236. }
  237. }
  238. - (IBAction)brightnessMoved:(NSSlider *)sender {
  239. [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", [sender floatValue]]];
  240. // Restore the current configuration for items where it won't happen automatically
  241. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  242. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  243. [self selectedVisualization:[menuColors itemAtIndex:i]];
  244. }
  245. }
  246. // Store changed value in preferences
  247. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  248. [store setObject:[NSNumber numberWithFloat:[sender floatValue]] forKey:PREF_BRIGHTNESS];
  249. [store synchronize];
  250. }
  251. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  252. if ([sender state] == NSOffState) {
  253. lastLEDMode = nil;
  254. // Stop previous timer setting
  255. if (animation != nil) {
  256. [animation invalidate];
  257. animation = nil;
  258. }
  259. // Turn off all other LED menu items
  260. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  261. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  262. lastLEDMode = [menuColors itemAtIndex:i];
  263. }
  264. [[menuColors itemAtIndex:i] setState:NSOffState];
  265. }
  266. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  267. if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
  268. lastLEDMode = [menuAnimations itemAtIndex:i];
  269. }
  270. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  271. }
  272. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  273. if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
  274. lastLEDMode = [menuVisualizations itemAtIndex:i];
  275. }
  276. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  277. }
  278. // Turn on "off" menu item
  279. [sender setState:NSOnState];
  280. // Store changed value in preferences
  281. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  282. [store setObject:@"" forKey:PREF_LED_MODE];
  283. [store synchronize];
  284. #ifdef DEBUG
  285. NSLog(@"Stored new mode: \"off\"!\n");
  286. #endif
  287. // Send command to turn off LEDs
  288. [self setLightsR:0 G:0 B:0];
  289. } else {
  290. // Try to restore last LED setting
  291. if (lastLEDMode != nil) {
  292. [self selectedVisualization:lastLEDMode];
  293. }
  294. }
  295. }
  296. - (IBAction)toggleLights:(NSMenuItem *)sender {
  297. if ([sender state] == NSOffState) {
  298. // Turn on lights
  299. if ([serial isOpen]) {
  300. [serial sendString:@"UV 1\n"];
  301. }
  302. [sender setState:NSOnState];
  303. } else {
  304. // Turn off lights
  305. if ([serial isOpen]) {
  306. [serial sendString:@"UV 0\n"];
  307. }
  308. [sender setState:NSOffState];
  309. }
  310. // Store changed value in preferences
  311. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  312. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  313. [store synchronize];
  314. }
  315. - (BOOL)timedVisualization:(NSString *)mode {
  316. // Stop previous timer setting
  317. if (animation != nil) {
  318. [animation invalidate];
  319. animation = nil;
  320. }
  321. // Schedule next invocation for this animation...
  322. if ([mode isEqualToString:TEXT_GPU_USAGE]) {
  323. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
  324. } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
  325. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
  326. } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
  327. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
  328. } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
  329. animation = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
  330. } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
  331. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
  332. } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
  333. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
  334. } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
  335. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
  336. } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
  337. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
  338. } else if ([mode isEqualToString:TEXT_RANDOM]) {
  339. animation = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(visualizeRandom:) userInfo:mode repeats:YES];
  340. } else {
  341. return NO;
  342. }
  343. #ifdef DEBUG
  344. NSLog(@"Scheduled animation for \"%@\"!\n", mode);
  345. #endif
  346. // ...and also execute it right now
  347. [animation fire];
  348. return YES;
  349. }
  350. - (void)selectedVisualization:(NSMenuItem *)sender {
  351. // Turn off all other LED menu items
  352. if (menuColors != nil) {
  353. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  354. [[menuColors itemAtIndex:i] setState:NSOffState];
  355. }
  356. }
  357. if (menuAnimations != nil) {
  358. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  359. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  360. }
  361. }
  362. if (menuVisualizations != nil) {
  363. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  364. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  365. }
  366. }
  367. [buttonOff setState:NSOffState];
  368. [sender setState:NSOnState];
  369. // Check if a static color was selected
  370. BOOL found = NO;
  371. if (staticColors != nil) {
  372. for (NSString *key in [staticColors allKeys]) {
  373. if ([sender.title isEqualToString:key]) {
  374. found = YES;
  375. // Stop previous timer setting
  376. if (animation != nil) {
  377. [animation invalidate];
  378. animation = nil;
  379. }
  380. NSColor *color = [staticColors valueForKey:key];
  381. unsigned char red = [color redComponent] * 255;
  382. unsigned char green = [color greenComponent] * 255;
  383. unsigned char blue = [color blueComponent] * 255;
  384. [self setLightsR:red G:green B:blue];
  385. break;
  386. }
  387. }
  388. }
  389. if (!found) {
  390. // Check if an animated visualization was selected
  391. if ([self timedVisualization:[sender title]] == NO) {
  392. NSLog(@"Unknown LED Visualization selected!\n");
  393. return;
  394. }
  395. }
  396. // Store changed value in preferences
  397. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  398. [store setObject:[sender title] forKey:PREF_LED_MODE];
  399. [store synchronize];
  400. #ifdef DEBUG
  401. NSLog(@"Stored new mode: \"%@\"!\n", [sender title]);
  402. #endif
  403. }
  404. - (void)selectedSerialPort:(NSMenuItem *)source {
  405. // Store selection for next start-up
  406. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  407. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  408. [store synchronize];
  409. // De-select all other ports
  410. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  411. [[menuPorts itemAtIndex:i] setState:NSOffState];
  412. }
  413. // Select only the current port
  414. [source setState:NSOnState];
  415. // Close previously opened port, if any
  416. if ([serial isOpen]) {
  417. [serial closePort];
  418. }
  419. // Try to open selected port
  420. [serial setPortName:[source title]];
  421. if ([serial openPort] != 0) {
  422. [source setState:NSOffState];
  423. } else {
  424. // Restore the current configuration
  425. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  426. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  427. [self selectedVisualization:[menuColors itemAtIndex:i]];
  428. }
  429. }
  430. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  431. if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
  432. [self selectedVisualization:[menuAnimations itemAtIndex:i]];
  433. }
  434. }
  435. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  436. if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
  437. [self selectedVisualization:[menuVisualizations itemAtIndex:i]];
  438. }
  439. }
  440. if ([buttonOff state] == NSOnState) {
  441. [buttonOff setState:NSOffState];
  442. [self turnLEDsOff:buttonOff];
  443. }
  444. if ([buttonLights state] == NSOnState) {
  445. [serial sendString:@"UV 1\n"];
  446. } else {
  447. [serial sendString:@"UV 0\n"];
  448. }
  449. }
  450. }
  451. - (IBAction)showAbout:(id)sender {
  452. [NSApp activateIgnoringOtherApps:YES];
  453. [application orderFrontStandardAboutPanel:self];
  454. }
  455. // ------------------------------------------------------
  456. // ------------------- Visualizations -------------------
  457. // ------------------------------------------------------
  458. - (void)visualizeGPUUsage:(NSTimer *)timer {
  459. NSNumber *usage;
  460. NSNumber *freeVRAM;
  461. NSNumber *usedVRAM;
  462. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  463. NSLog(@"Error reading GPU information\n");
  464. } else {
  465. double h = [self map:[usage doubleValue] FromMin:0.0 FromMax:100.0 ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
  466. #ifdef DEBUG
  467. NSLog(@"GPU Usage: %.3f%%\n", [usage doubleValue]);
  468. #endif
  469. unsigned char r, g, b;
  470. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  471. [self setLightsR:r G:g B:b];
  472. }
  473. }
  474. - (void)visualizeVRAMUsage:(NSTimer *)timer {
  475. NSNumber *usage;
  476. NSNumber *freeVRAM;
  477. NSNumber *usedVRAM;
  478. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  479. NSLog(@"Error reading GPU information\n");
  480. } else {
  481. double h = [self map:[freeVRAM doubleValue] FromMin:0.0 FromMax:([freeVRAM doubleValue] + [usedVRAM doubleValue]) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
  482. #ifdef DEBUG
  483. 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);
  484. #endif
  485. unsigned char r, g, b;
  486. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  487. [self setLightsR:r G:g B:b];
  488. }
  489. }
  490. - (void)visualizeCPUUsage:(NSTimer *)timer {
  491. JSKMCPUUsageInfo cpuUsageInfo = [JSKSystemMonitor systemMonitor].cpuUsageInfo;
  492. double h = [self map:cpuUsageInfo.usage FromMin:0.0 FromMax:100.0 ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
  493. #ifdef DEBUG
  494. NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
  495. #endif
  496. unsigned char r, g, b;
  497. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  498. [self setLightsR:r G:g B:b];
  499. }
  500. - (void)visualizeRAMUsage:(NSTimer *)timer {
  501. JSKMMemoryUsageInfo memoryUsageInfo = [JSKSystemMonitor systemMonitor].memoryUsageInfo;
  502. double h = [self map:memoryUsageInfo.freeMemory FromMin:0.0 FromMax:(memoryUsageInfo.usedMemory + memoryUsageInfo.freeMemory) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
  503. #ifdef DEBUG
  504. 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);
  505. #endif
  506. unsigned char r, g, b;
  507. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  508. [self setLightsR:r G:g B:b];
  509. }
  510. - (void)visualizeGPUTemperature:(NSTimer *)timer {
  511. JSKSMC *smc = [JSKSMC smc];
  512. double temp = [smc temperatureInCelsiusForKey:KEY_GPU_TEMPERATURE];
  513. if (temp > 1000.0) {
  514. temp /= 1000.0;
  515. }
  516. if (temp > GPU_TEMP_MAX) {
  517. temp = GPU_TEMP_MAX;
  518. }
  519. if (temp < GPU_TEMP_MIN) {
  520. temp = GPU_TEMP_MIN;
  521. }
  522. double h = [self map:temp FromMin:GPU_TEMP_MIN FromMax:GPU_TEMP_MAX ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
  523. #ifdef DEBUG
  524. NSLog(@"GPU Temp %.2f mapped to color %.2f!\n", temp, h);
  525. #endif
  526. unsigned char r, g, b;
  527. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  528. [self setLightsR:r G:g B:b];
  529. }
  530. - (void)visualizeCPUTemperature:(NSTimer *)timer {
  531. JSKSMC *smc = [JSKSMC smc];
  532. double temp = [smc temperatureInCelsiusForKey:KEY_CPU_TEMPERATURE];
  533. if (temp > 1000.0) {
  534. temp /= 1000.0;
  535. }
  536. if (temp > CPU_TEMP_MAX) {
  537. temp = CPU_TEMP_MAX;
  538. }
  539. if (temp < CPU_TEMP_MIN) {
  540. temp = CPU_TEMP_MIN;
  541. }
  542. double h = [self map:temp FromMin:CPU_TEMP_MIN FromMax:CPU_TEMP_MAX ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
  543. #ifdef DEBUG
  544. NSLog(@"CPU Temp %.2f mapped to color %.2f!\n", temp, h);
  545. #endif
  546. unsigned char r, g, b;
  547. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  548. [self setLightsR:r G:g B:b];
  549. }
  550. - (void)visualizeRGBFade:(NSTimer *)timer {
  551. static unsigned char color[3] = { 255, 0, 0 };
  552. static int dec = 0;
  553. static int val = 0;
  554. // Adapted from:
  555. // https://gist.github.com/jamesotron/766994
  556. if (dec < 3) {
  557. int inc = (dec == 2) ? 0 : (dec + 1);
  558. if (val < 255) {
  559. color[dec] -= 1;
  560. color[inc] += 1;
  561. val++;
  562. } else {
  563. val = 0;
  564. dec++;
  565. }
  566. } else {
  567. dec = 0;
  568. }
  569. [self setLightsR:color[0] G:color[1] B:color[2]];
  570. }
  571. - (void)visualizeHSVFade:(NSTimer *)timer {
  572. static float h = 0.0;
  573. if (h < 359.0) {
  574. h += 0.5;
  575. } else {
  576. h = 0.0;
  577. }
  578. unsigned char r, g, b;
  579. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  580. [self setLightsR:r G:g B:b];
  581. }
  582. - (void)visualizeRandom:(NSTimer *)timer {
  583. [self setLightsR:rand() % 256 G:rand() % 256 B:rand() % 256];
  584. }
  585. // -----------------------------------------------------
  586. // --------------------- Utilities ---------------------
  587. // -----------------------------------------------------
  588. - (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
  589. double norm = (val - fmin) / (fmax - fmin);
  590. return (norm * (tmax - tmin)) + tmin;
  591. }
  592. - (void)convertH:(double)h S:(double)s V:(double)v toR:(unsigned char *)r G:(unsigned char *)g B:(unsigned char *)b {
  593. // Adapted from:
  594. // https://gist.github.com/hdznrrd/656996
  595. if (s == 0.0) {
  596. // Achromatic
  597. *r = *g = *b = (unsigned char)(v * 255);
  598. return;
  599. }
  600. h /= 60; // sector 0 to 5
  601. int i = floor(h);
  602. double f = h - i; // factorial part of h
  603. double p = v * (1 - s);
  604. double q = v * (1 - s * f);
  605. double t = v * (1 - s * (1 - f));
  606. switch (i) {
  607. case 0:
  608. *r = (unsigned char)round(255 * v);
  609. *g = (unsigned char)round(255 * t);
  610. *b = (unsigned char)round(255 * p);
  611. break;
  612. case 1:
  613. *r = (unsigned char)round(255 * q);
  614. *g = (unsigned char)round(255 * v);
  615. *b = (unsigned char)round(255 * p);
  616. break;
  617. case 2:
  618. *r = (unsigned char)round(255 * p);
  619. *g = (unsigned char)round(255 * v);
  620. *b = (unsigned char)round(255 * t);
  621. break;
  622. case 3:
  623. *r = (unsigned char)round(255 * p);
  624. *g = (unsigned char)round(255 * q);
  625. *b = (unsigned char)round(255 * v);
  626. break;
  627. case 4:
  628. *r = (unsigned char)round(255 * t);
  629. *g = (unsigned char)round(255 * p);
  630. *b = (unsigned char)round(255 * v);
  631. break;
  632. default: case 5:
  633. *r = (unsigned char)round(255 * v);
  634. *g = (unsigned char)round(255 * p);
  635. *b = (unsigned char)round(255 * q);
  636. break;
  637. }
  638. }
  639. @end