Simple RGB LED controller for Mac OS X
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppDelegate.m 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. // Store changed value in preferences
  241. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  242. [store setObject:[NSNumber numberWithFloat:[sender floatValue]] forKey:PREF_BRIGHTNESS];
  243. [store synchronize];
  244. }
  245. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  246. if ([sender state] == NSOffState) {
  247. lastLEDMode = nil;
  248. // Stop previous timer setting
  249. if (animation != nil) {
  250. [animation invalidate];
  251. animation = nil;
  252. }
  253. // Turn off all other LED menu items
  254. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  255. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  256. lastLEDMode = [menuColors itemAtIndex:i];
  257. }
  258. [[menuColors itemAtIndex:i] setState:NSOffState];
  259. }
  260. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  261. if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
  262. lastLEDMode = [menuAnimations itemAtIndex:i];
  263. }
  264. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  265. }
  266. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  267. if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
  268. lastLEDMode = [menuVisualizations itemAtIndex:i];
  269. }
  270. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  271. }
  272. // Turn on "off" menu item
  273. [sender setState:NSOnState];
  274. // Store changed value in preferences
  275. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  276. [store setObject:@"" forKey:PREF_LED_MODE];
  277. [store synchronize];
  278. #ifdef DEBUG
  279. NSLog(@"Stored new mode: \"off\"!\n");
  280. #endif
  281. // Send command to turn off LEDs
  282. [self setLightsR:0 G:0 B:0];
  283. } else {
  284. // Try to restore last LED setting
  285. if (lastLEDMode != nil) {
  286. [self selectedVisualization:lastLEDMode];
  287. }
  288. }
  289. }
  290. - (IBAction)toggleLights:(NSMenuItem *)sender {
  291. if ([sender state] == NSOffState) {
  292. // Turn on lights
  293. if ([serial isOpen]) {
  294. [serial sendString:@"UV 1\n"];
  295. }
  296. [sender setState:NSOnState];
  297. } else {
  298. // Turn off lights
  299. if ([serial isOpen]) {
  300. [serial sendString:@"UV 0\n"];
  301. }
  302. [sender setState:NSOffState];
  303. }
  304. // Store changed value in preferences
  305. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  306. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  307. [store synchronize];
  308. }
  309. - (void)visualizeGPUUsage:(NSTimer *)timer {
  310. NSNumber *usage;
  311. NSNumber *freeVRAM;
  312. NSNumber *usedVRAM;
  313. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  314. NSLog(@"Error reading GPU information\n");
  315. } else {
  316. double h = [self map:[usage doubleValue] FromMin:0.0 FromMax:100.0 ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
  317. #ifdef DEBUG
  318. NSLog(@"GPU Usage: %.3f%%\n", [usage doubleValue]);
  319. #endif
  320. unsigned char r, g, b;
  321. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  322. [self setLightsR:r G:g B:b];
  323. }
  324. }
  325. - (void)visualizeVRAMUsage:(NSTimer *)timer {
  326. NSNumber *usage;
  327. NSNumber *freeVRAM;
  328. NSNumber *usedVRAM;
  329. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  330. NSLog(@"Error reading GPU information\n");
  331. } else {
  332. double h = [self map:[freeVRAM doubleValue] FromMin:0.0 FromMax:([freeVRAM doubleValue] + [usedVRAM doubleValue]) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
  333. #ifdef DEBUG
  334. 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);
  335. #endif
  336. unsigned char r, g, b;
  337. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  338. [self setLightsR:r G:g B:b];
  339. }
  340. }
  341. - (void)visualizeCPUUsage:(NSTimer *)timer {
  342. JSKMCPUUsageInfo cpuUsageInfo = [JSKSystemMonitor systemMonitor].cpuUsageInfo;
  343. double h = [self map:cpuUsageInfo.usage FromMin:0.0 FromMax:100.0 ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
  344. #ifdef DEBUG
  345. NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
  346. #endif
  347. unsigned char r, g, b;
  348. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  349. [self setLightsR:r G:g B:b];
  350. }
  351. - (void)visualizeRAMUsage:(NSTimer *)timer {
  352. JSKMMemoryUsageInfo memoryUsageInfo = [JSKSystemMonitor systemMonitor].memoryUsageInfo;
  353. double h = [self map:memoryUsageInfo.freeMemory FromMin:0.0 FromMax:(memoryUsageInfo.usedMemory + memoryUsageInfo.freeMemory) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
  354. #ifdef DEBUG
  355. 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);
  356. #endif
  357. unsigned char r, g, b;
  358. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  359. [self setLightsR:r G:g B:b];
  360. }
  361. - (void)visualizeGPUTemperature:(NSTimer *)timer {
  362. JSKSMC *smc = [JSKSMC smc];
  363. double temp = [smc temperatureInCelsiusForKey:KEY_GPU_TEMPERATURE];
  364. if (temp > 1000.0) {
  365. temp /= 1000.0;
  366. }
  367. if (temp > GPU_TEMP_MAX) {
  368. temp = GPU_TEMP_MAX;
  369. }
  370. if (temp < GPU_TEMP_MIN) {
  371. temp = GPU_TEMP_MIN;
  372. }
  373. double h = [self map:temp FromMin:GPU_TEMP_MIN FromMax:GPU_TEMP_MAX ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
  374. #ifdef DEBUG
  375. NSLog(@"GPU Temp %.2f mapped to color %.2f!\n", temp, h);
  376. #endif
  377. unsigned char r, g, b;
  378. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  379. [self setLightsR:r G:g B:b];
  380. }
  381. - (void)visualizeCPUTemperature:(NSTimer *)timer {
  382. JSKSMC *smc = [JSKSMC smc];
  383. double temp = [smc temperatureInCelsiusForKey:KEY_CPU_TEMPERATURE];
  384. if (temp > 1000.0) {
  385. temp /= 1000.0;
  386. }
  387. if (temp > CPU_TEMP_MAX) {
  388. temp = CPU_TEMP_MAX;
  389. }
  390. if (temp < CPU_TEMP_MIN) {
  391. temp = CPU_TEMP_MIN;
  392. }
  393. double h = [self map:temp FromMin:CPU_TEMP_MIN FromMax:CPU_TEMP_MAX ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
  394. #ifdef DEBUG
  395. NSLog(@"CPU Temp %.2f mapped to color %.2f!\n", temp, h);
  396. #endif
  397. unsigned char r, g, b;
  398. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  399. [self setLightsR:r G:g B:b];
  400. }
  401. - (void)visualizeRGBFade:(NSTimer *)timer {
  402. static unsigned char color[3] = { 255, 0, 0 };
  403. static int dec = 0;
  404. static int val = 0;
  405. // Adapted from:
  406. // https://gist.github.com/jamesotron/766994
  407. if (dec < 3) {
  408. int inc = (dec == 2) ? 0 : (dec + 1);
  409. if (val < 255) {
  410. color[dec] -= 1;
  411. color[inc] += 1;
  412. val++;
  413. } else {
  414. val = 0;
  415. dec++;
  416. }
  417. } else {
  418. dec = 0;
  419. }
  420. [self setLightsR:color[0] G:color[1] B:color[2]];
  421. }
  422. - (void)visualizeHSVFade:(NSTimer *)timer {
  423. static float h = 0.0;
  424. if (h < 359.0) {
  425. h += 0.5;
  426. } else {
  427. h = 0.0;
  428. }
  429. unsigned char r, g, b;
  430. [self convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  431. [self setLightsR:r G:g B:b];
  432. }
  433. - (void)visualizeRandom:(NSTimer *)timer {
  434. [self setLightsR:rand() % 256 G:rand() % 256 B:rand() % 256];
  435. }
  436. - (BOOL)timedVisualization:(NSString *)mode {
  437. // Stop previous timer setting
  438. if (animation != nil) {
  439. [animation invalidate];
  440. animation = nil;
  441. }
  442. // Schedule next invocation for this animation...
  443. if ([mode isEqualToString:TEXT_GPU_USAGE]) {
  444. animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
  445. } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
  446. animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
  447. } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
  448. animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
  449. } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
  450. animation = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
  451. } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
  452. animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
  453. } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
  454. animation = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
  455. } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
  456. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
  457. } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
  458. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
  459. } else if ([mode isEqualToString:TEXT_RANDOM]) {
  460. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRandom:) userInfo:mode repeats:YES];
  461. } else {
  462. return NO;
  463. }
  464. #ifdef DEBUG
  465. NSLog(@"Scheduled animation for \"%@\"!\n", mode);
  466. #endif
  467. // ...and also execute it right now
  468. [animation fire];
  469. return YES;
  470. }
  471. - (void)selectedVisualization:(NSMenuItem *)sender {
  472. // Turn off all other LED menu items
  473. if (menuColors != nil) {
  474. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  475. [[menuColors itemAtIndex:i] setState:NSOffState];
  476. }
  477. }
  478. if (menuAnimations != nil) {
  479. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  480. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  481. }
  482. }
  483. if (menuVisualizations != nil) {
  484. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  485. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  486. }
  487. }
  488. [buttonOff setState:NSOffState];
  489. [sender setState:NSOnState];
  490. // Check if a static color was selected
  491. BOOL found = NO;
  492. if (staticColors != nil) {
  493. for (NSString *key in [staticColors allKeys]) {
  494. if ([sender.title isEqualToString:key]) {
  495. found = YES;
  496. // Stop previous timer setting
  497. if (animation != nil) {
  498. [animation invalidate];
  499. animation = nil;
  500. }
  501. NSColor *color = [staticColors valueForKey:key];
  502. unsigned char red = [color redComponent] * 255;
  503. unsigned char green = [color greenComponent] * 255;
  504. unsigned char blue = [color blueComponent] * 255;
  505. [self setLightsR:red G:green B:blue];
  506. break;
  507. }
  508. }
  509. }
  510. if (!found) {
  511. // Check if an animated visualization was selected
  512. if ([self timedVisualization:[sender title]] == NO) {
  513. NSLog(@"Unknown LED Visualization selected!\n");
  514. return;
  515. }
  516. }
  517. // Store changed value in preferences
  518. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  519. [store setObject:[sender title] forKey:PREF_LED_MODE];
  520. [store synchronize];
  521. #ifdef DEBUG
  522. NSLog(@"Stored new mode: \"%@\"!\n", [sender title]);
  523. #endif
  524. }
  525. - (void)selectedSerialPort:(NSMenuItem *)source {
  526. // Store selection for next start-up
  527. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  528. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  529. [store synchronize];
  530. // De-select all other ports
  531. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  532. [[menuPorts itemAtIndex:i] setState:NSOffState];
  533. }
  534. // Select only the current port
  535. [source setState:NSOnState];
  536. // Close previously opened port, if any
  537. if ([serial isOpen]) {
  538. [serial closePort];
  539. }
  540. // Try to open selected port
  541. [serial setPortName:[source title]];
  542. if ([serial openPort] != 0) {
  543. [source setState:NSOffState];
  544. } else {
  545. // Restore the current configuration
  546. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  547. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  548. [self selectedVisualization:[menuColors itemAtIndex:i]];
  549. }
  550. }
  551. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  552. if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
  553. [self selectedVisualization:[menuAnimations itemAtIndex:i]];
  554. }
  555. }
  556. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  557. if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
  558. [self selectedVisualization:[menuVisualizations itemAtIndex:i]];
  559. }
  560. }
  561. if ([buttonOff state] == NSOnState) {
  562. [buttonOff setState:NSOffState];
  563. [self turnLEDsOff:buttonOff];
  564. }
  565. if ([buttonLights state] == NSOnState) {
  566. [serial sendString:@"UV 1\n"];
  567. } else {
  568. [serial sendString:@"UV 0\n"];
  569. }
  570. }
  571. }
  572. - (IBAction)showAbout:(id)sender {
  573. [NSApp activateIgnoringOtherApps:YES];
  574. [application orderFrontStandardAboutPanel:self];
  575. }
  576. - (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
  577. double norm = (val - fmin) / (fmax - fmin);
  578. return (norm * (tmax - tmin)) + tmin;
  579. }
  580. - (void)convertH:(double)h S:(double)s V:(double)v toR:(unsigned char *)r G:(unsigned char *)g B:(unsigned char *)b {
  581. // Adapted from:
  582. // https://gist.github.com/hdznrrd/656996
  583. if (s == 0.0) {
  584. // Achromatic
  585. *r = *g = *b = (unsigned char)(v * 255);
  586. return;
  587. }
  588. h /= 60; // sector 0 to 5
  589. int i = floor(h);
  590. double f = h - i; // factorial part of h
  591. double p = v * (1 - s);
  592. double q = v * (1 - s * f);
  593. double t = v * (1 - s * (1 - f));
  594. switch (i) {
  595. case 0:
  596. *r = (unsigned char)round(255 * v);
  597. *g = (unsigned char)round(255 * t);
  598. *b = (unsigned char)round(255 * p);
  599. break;
  600. case 1:
  601. *r = (unsigned char)round(255 * q);
  602. *g = (unsigned char)round(255 * v);
  603. *b = (unsigned char)round(255 * p);
  604. break;
  605. case 2:
  606. *r = (unsigned char)round(255 * p);
  607. *g = (unsigned char)round(255 * v);
  608. *b = (unsigned char)round(255 * t);
  609. break;
  610. case 3:
  611. *r = (unsigned char)round(255 * p);
  612. *g = (unsigned char)round(255 * q);
  613. *b = (unsigned char)round(255 * v);
  614. break;
  615. case 4:
  616. *r = (unsigned char)round(255 * t);
  617. *g = (unsigned char)round(255 * p);
  618. *b = (unsigned char)round(255 * v);
  619. break;
  620. default: case 5:
  621. *r = (unsigned char)round(255 * v);
  622. *g = (unsigned char)round(255 * p);
  623. *b = (unsigned char)round(255 * q);
  624. break;
  625. }
  626. }
  627. @end