Simple RGB LED controller for Mac OS X
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AppDelegate.m 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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 "Screenshot.h"
  12. #import "AudioVisualizer.h"
  13. // These are the values stored persistently in the preferences
  14. #define PREF_SERIAL_PORT @"SerialPort"
  15. #define PREF_LIGHTS_STATE @"LightState"
  16. // LED Mode contains the last selected mode as menu item text
  17. #define PREF_LED_MODE @"LEDMode"
  18. #define PREF_BRIGHTNESS @"Brightness"
  19. #define PREF_COLOR @"ManualColor"
  20. #define PREF_SENSITIVITY @"Sensitivity"
  21. #define TEXT_MANUAL @"Select..."
  22. #define TEXT_CPU_USAGE @"CPU Usage"
  23. #define TEXT_RAM_USAGE @"RAM Usage"
  24. #define TEXT_GPU_USAGE @"GPU Usage"
  25. #define TEXT_VRAM_USAGE @"VRAM Usage"
  26. #define TEXT_CPU_TEMPERATURE @"CPU Temperature"
  27. #define TEXT_GPU_TEMPERATURE @"GPU Temperature"
  28. #define TEXT_RGB_FADE @"RGB Fade"
  29. #define TEXT_HSV_FADE @"HSV Fade"
  30. #define TEXT_RANDOM @"Random"
  31. #define TEXT_TEMPLATE_AUDIO @"AudioDevice_%@"
  32. // SMC keys are checked for existence and used for reading
  33. #define KEY_CPU_TEMPERATURE @"TC0D"
  34. #define KEY_GPU_TEMPERATURE @"TG0D"
  35. // Temperature in Celsius
  36. #define CPU_TEMP_MIN 20
  37. #define CPU_TEMP_MAX 90
  38. // HSV Color (S = V = 1)
  39. #define CPU_COLOR_MIN 120
  40. #define CPU_COLOR_MAX 0
  41. #define GPU_TEMP_MIN 20
  42. #define GPU_TEMP_MAX 90
  43. #define GPU_COLOR_MIN 120
  44. #define GPU_COLOR_MAX 0
  45. #define RAM_COLOR_MIN 0
  46. #define RAM_COLOR_MAX 120
  47. // You can play around with these values (skipped pixels, display timer delay) to change CPU usage in display mode
  48. #define AVERAGE_COLOR_PERFORMANCE_INC 10
  49. #define DISPLAY_DELAY 0.1
  50. // Used to identify selected menu items
  51. // displays are all tags >= 0
  52. #define MENU_ITEM_TAG_NOTHING -1
  53. #define MENU_ITEM_TAG_AUDIO -2
  54. @interface AppDelegate ()
  55. @property (weak) IBOutlet NSMenu *statusMenu;
  56. @property (weak) IBOutlet NSMenu *menuColors;
  57. @property (weak) IBOutlet NSMenu *menuAnimations;
  58. @property (weak) IBOutlet NSMenu *menuVisualizations;
  59. @property (weak) IBOutlet NSMenuItem *menuItemDisplays;
  60. @property (weak) IBOutlet NSMenu *menuDisplays;
  61. @property (weak) IBOutlet NSMenuItem *menuItemAudio;
  62. @property (weak) IBOutlet NSMenu *menuAudio;
  63. @property (weak) IBOutlet NSMenu *menuPorts;
  64. @property (weak) IBOutlet NSMenuItem *buttonOff;
  65. @property (weak) IBOutlet NSMenuItem *brightnessItem;
  66. @property (weak) IBOutlet NSSlider *brightnessSlider;
  67. @property (weak) IBOutlet NSMenuItem *brightnessLabel;
  68. @property (weak) IBOutlet NSMenuItem *buttonLights;
  69. @property (weak) IBOutlet NSMenuItem *sensitivityItem;
  70. @property (weak) IBOutlet NSSlider *sensitivitySlider;
  71. @property (weak) IBOutlet NSMenuItem *sensitivityLabel;
  72. @property (weak) IBOutlet NSMenuItem *sensitivityMenu;
  73. @property (strong) NSMenuItem *menuItemColor;
  74. @property (strong) NSStatusItem *statusItem;
  75. @property (strong) NSImage *statusImage;
  76. @property (strong) NSDictionary *staticColors;
  77. @property (strong) NSTimer *animation;
  78. @property (strong) Serial *serial;
  79. @property (strong) NSMenuItem *lastLEDMode;
  80. @end
  81. @implementation AppDelegate
  82. @synthesize statusMenu, application;
  83. @synthesize menuColors, menuAnimations, menuVisualizations, menuPorts;
  84. @synthesize menuItemDisplays, menuDisplays;
  85. @synthesize menuItemAudio, menuAudio;
  86. @synthesize buttonOff, buttonLights;
  87. @synthesize brightnessItem, brightnessSlider, brightnessLabel;
  88. @synthesize sensitivityItem, sensitivitySlider, sensitivityLabel, sensitivityMenu;
  89. @synthesize statusItem, statusImage;
  90. @synthesize staticColors, animation;
  91. @synthesize serial, lastLEDMode, microphone;
  92. @synthesize menuItemColor;
  93. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  94. srand((unsigned)time(NULL));
  95. [AudioVisualizer setDelegate:self];
  96. serial = [[Serial alloc] init];
  97. lastLEDMode = nil;
  98. animation = nil;
  99. microphone = nil;
  100. // Prepare status bar menu
  101. statusImage = [NSImage imageNamed:@"MenuIcon"];
  102. [statusImage setTemplate:YES];
  103. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  104. [statusItem setImage:statusImage];
  105. [statusItem setMenu:statusMenu];
  106. // Set default configuration values, load existing ones
  107. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  108. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
  109. [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
  110. [appDefaults setObject:@"" forKey:PREF_LED_MODE];
  111. [appDefaults setObject:[NSNumber numberWithFloat:50.0] forKey:PREF_BRIGHTNESS];
  112. [appDefaults setObject:[NSNumber numberWithFloat:100.0] forKey:PREF_SENSITIVITY];
  113. [store registerDefaults:appDefaults];
  114. [store synchronize];
  115. NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
  116. BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
  117. NSString *lastMode = [store stringForKey:PREF_LED_MODE];
  118. float brightness = [store floatForKey:PREF_BRIGHTNESS];
  119. NSData *lastColorData = [store dataForKey:PREF_COLOR];
  120. float sensitivity = [store floatForKey:PREF_SENSITIVITY];
  121. NSColor *lastColor = nil;
  122. if (lastColorData != nil) {
  123. lastColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:lastColorData];
  124. }
  125. // Prepare brightness menu
  126. brightnessItem.view = brightnessSlider;
  127. [brightnessSlider setFloatValue:brightness];
  128. [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", brightness]];
  129. // Prepare serial port menu
  130. NSArray *ports = [Serial listSerialPorts];
  131. if ([ports count] > 0) {
  132. [menuPorts removeAllItems];
  133. for (int i = 0; i < [ports count]; i++) {
  134. // Add Menu Item for this port
  135. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  136. [item setTag:MENU_ITEM_TAG_NOTHING];
  137. [menuPorts addItem:item];
  138. // Set Enabled if it was used the last time
  139. if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
  140. [[menuPorts itemAtIndex:i] setState:NSOnState];
  141. // Try to open serial port
  142. [serial setPortName:savedPort];
  143. if ([serial openPort]) {
  144. // Unselect it when an error occured opening the port
  145. [[menuPorts itemAtIndex:i] setState:NSOffState];
  146. }
  147. }
  148. }
  149. }
  150. // Select "Off" button if it was last selected
  151. if ([lastMode isEqualToString:@""]) {
  152. [buttonOff setState:NSOffState];
  153. [self turnLEDsOff:buttonOff];
  154. }
  155. // Prepare static colors menu
  156. staticColors = [NSDictionary dictionaryWithObjectsAndKeys:
  157. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:0.0f alpha:0.0f], @"Red",
  158. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:0.0f alpha:0.0f], @"Green",
  159. [NSColor colorWithCalibratedRed:0.0f green:0.0f blue:1.0f alpha:0.0f], @"Blue",
  160. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:1.0f alpha:0.0f], @"Cyan",
  161. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:1.0f alpha:0.0f], @"Magenta",
  162. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:0.0f alpha:0.0f], @"Yellow",
  163. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:0.0f], @"White",
  164. nil];
  165. for (NSString *key in [staticColors allKeys]) {
  166. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
  167. [item setTag:MENU_ITEM_TAG_NOTHING];
  168. if ([key isEqualToString:lastMode]) {
  169. [self selectedVisualization:item];
  170. }
  171. [menuColors addItem:item];
  172. }
  173. menuItemColor = [[NSMenuItem alloc] initWithTitle:TEXT_MANUAL action:@selector(setColorSelected:) keyEquivalent:@""];
  174. if ([lastMode isEqualToString:TEXT_MANUAL]) {
  175. if (lastColor != nil) {
  176. // Restore previously set RGB color
  177. [self setLightsColor:lastColor];
  178. }
  179. [menuItemColor setState:NSOnState];
  180. }
  181. [menuItemColor setTag:MENU_ITEM_TAG_NOTHING];
  182. [menuColors addItem:menuItemColor];
  183. // Prepare animations menu
  184. NSArray *animationStrings = [NSArray arrayWithObjects:
  185. TEXT_RGB_FADE,
  186. TEXT_HSV_FADE,
  187. TEXT_RANDOM,
  188. nil];
  189. for (NSString *key in animationStrings) {
  190. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
  191. [item setTag:MENU_ITEM_TAG_NOTHING];
  192. if ([key isEqualToString:lastMode]) {
  193. [self selectedVisualization:item];
  194. }
  195. [menuAnimations addItem:item];
  196. }
  197. // Add CPU Usage menu item
  198. NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  199. [cpuUsageItem setTag:MENU_ITEM_TAG_NOTHING];
  200. if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
  201. [self selectedVisualization:cpuUsageItem];
  202. }
  203. [menuVisualizations addItem:cpuUsageItem];
  204. // Add Memory Usage item
  205. NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_RAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  206. [memoryUsageItem setTag:MENU_ITEM_TAG_NOTHING];
  207. if ([lastMode isEqualToString:TEXT_RAM_USAGE]) {
  208. [self selectedVisualization:memoryUsageItem];
  209. }
  210. [menuVisualizations addItem:memoryUsageItem];
  211. // Check if GPU Stats are available, add menu items if so
  212. NSNumber *usage;
  213. NSNumber *freeVRAM;
  214. NSNumber *usedVRAM;
  215. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  216. NSLog(@"Error reading GPU information\n");
  217. } else {
  218. NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  219. [itemUsage setTag:MENU_ITEM_TAG_NOTHING];
  220. if ([lastMode isEqualToString:TEXT_GPU_USAGE]) {
  221. [self selectedVisualization:itemUsage];
  222. }
  223. [menuVisualizations addItem:itemUsage];
  224. NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:TEXT_VRAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
  225. [itemVRAM setTag:MENU_ITEM_TAG_NOTHING];
  226. if ([lastMode isEqualToString:TEXT_VRAM_USAGE]) {
  227. [self selectedVisualization:itemVRAM];
  228. }
  229. [menuVisualizations addItem:itemVRAM];
  230. }
  231. // Check available temperatures and add menu items
  232. JSKSMC *smc = [JSKSMC smc];
  233. for (int i = 0; i < [[smc workingTempKeys] count]; i++) {
  234. NSString *key = [smc.workingTempKeys objectAtIndex:i];
  235. #ifdef DEBUG
  236. NSString *name = [smc humanReadableNameForKey:key];
  237. NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
  238. #endif
  239. if ([key isEqualToString:KEY_CPU_TEMPERATURE]) {
  240. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
  241. [item setTag:MENU_ITEM_TAG_NOTHING];
  242. if ([lastMode isEqualToString:TEXT_CPU_TEMPERATURE]) {
  243. [self selectedVisualization:item];
  244. }
  245. [menuVisualizations addItem:item];
  246. }
  247. if ([key isEqualToString:KEY_GPU_TEMPERATURE]) {
  248. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
  249. [item setTag:MENU_ITEM_TAG_NOTHING];
  250. if ([lastMode isEqualToString:TEXT_GPU_TEMPERATURE]) {
  251. [self selectedVisualization:item];
  252. }
  253. [menuVisualizations addItem:item];
  254. }
  255. }
  256. // Restore previously used lights configuration
  257. if (turnOnLights) {
  258. // Turn on lights
  259. if ([serial isOpen]) {
  260. [serial sendString:@"UV 1\n"];
  261. }
  262. [buttonLights setState:NSOnState];
  263. } else {
  264. // Turn off lights
  265. if ([serial isOpen]) {
  266. [serial sendString:@"UV 0\n"];
  267. }
  268. }
  269. // List available displays and add menu items
  270. [Screenshot init:self];
  271. NSArray *displayIDs = [Screenshot listDisplays];
  272. [self updateDisplayUI:displayIDs];
  273. // List available audio input devices and add menu items
  274. NSArray *inputDevices = [EZAudioDevice inputDevices];
  275. [menuAudio removeAllItems];
  276. for (int i = 0; i < [inputDevices count]; i++) {
  277. EZAudioDevice *dev = [inputDevices objectAtIndex:i];
  278. #ifdef DEBUG
  279. NSLog(@"Audio input device: \"%@\"\n", [dev name]);
  280. #endif
  281. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[dev name] action:@selector(selectedVisualization:) keyEquivalent:@""];
  282. [item setTag:MENU_ITEM_TAG_AUDIO];
  283. NSString *lastModeString = [NSString stringWithFormat:TEXT_TEMPLATE_AUDIO, [dev name]];
  284. if ([lastModeString isEqualToString:lastMode]) {
  285. [self selectedVisualization:item];
  286. }
  287. [menuAudio addItem:item];
  288. }
  289. if ([inputDevices count] > 0) {
  290. [menuItemAudio setHidden:NO];
  291. // Prepare sensitivity menu
  292. sensitivityItem.view = sensitivitySlider;
  293. [sensitivitySlider setFloatValue:sensitivity];
  294. [sensitivityLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", sensitivity]];
  295. [sensitivityMenu setHidden:NO];
  296. [AudioVisualizer setSensitivity:sensitivity];
  297. }
  298. }
  299. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  300. // Stop previous timer setting
  301. if (animation != nil) {
  302. [animation invalidate];
  303. animation = nil;
  304. }
  305. // Stop previous audio data retrieval
  306. if (microphone != nil) {
  307. [microphone stopFetchingAudio];
  308. }
  309. // Remove display callback
  310. [Screenshot close:self];
  311. // Turn off all lights if possible
  312. if ([serial isOpen]) {
  313. [serial sendString:@"RGB 0 0 0\n"];
  314. [serial sendString:@"UV 0\n"];
  315. [serial closePort];
  316. }
  317. }
  318. - (void)clearDisplayUI {
  319. for (int i = 0; i < [menuDisplays numberOfItems]; i++) {
  320. if ([[menuDisplays itemAtIndex:i] isEnabled] == YES) {
  321. // A display configuration is currently selected. Disable the timer
  322. if (animation != nil) {
  323. [animation invalidate];
  324. animation = nil;
  325. }
  326. }
  327. }
  328. [menuDisplays removeAllItems];
  329. [menuItemDisplays setHidden:YES];
  330. }
  331. - (void)updateDisplayUI:(NSArray *)displayIDs {
  332. if ([displayIDs count] > 0) {
  333. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  334. NSString *lastMode = [store stringForKey:PREF_LED_MODE];
  335. [menuItemDisplays setHidden:NO];
  336. for (int i = 0; i < [displayIDs count]; i++) {
  337. NSString *title = [Screenshot displayNameFromDisplayID:[displayIDs objectAtIndex:i]];
  338. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title
  339. action:@selector(selectedVisualization:)
  340. keyEquivalent:@""];
  341. [item setTag:[[displayIDs objectAtIndex:i] integerValue]];
  342. if ([title isEqualToString:lastMode]) {
  343. [self selectedVisualization:item];
  344. }
  345. [menuDisplays addItem:item];
  346. }
  347. }
  348. }
  349. - (void)setLightsColor:(NSColor *)color {
  350. CGFloat red, green, blue, alpha;
  351. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  352. [self setLightsR:red * 255 G:green * 255 B:blue * 255];
  353. // Stop previous timer setting
  354. if (animation != nil) {
  355. [animation invalidate];
  356. animation = nil;
  357. }
  358. // Stop previous audio data retrieval
  359. if (microphone != nil) {
  360. [microphone stopFetchingAudio];
  361. }
  362. // Turn off all other LED menu items
  363. if (menuColors != nil) {
  364. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  365. [[menuColors itemAtIndex:i] setState:NSOffState];
  366. }
  367. }
  368. if (menuAnimations != nil) {
  369. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  370. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  371. }
  372. }
  373. if (menuVisualizations != nil) {
  374. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  375. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  376. }
  377. }
  378. if (menuAudio != nil) {
  379. for (int i = 0; i < [menuAudio numberOfItems]; i++) {
  380. [[menuAudio itemAtIndex:i] setState:NSOffState];
  381. }
  382. }
  383. if (menuDisplays != nil) {
  384. for (int i = 0; i < [menuDisplays numberOfItems]; i++) {
  385. [[menuDisplays itemAtIndex:i] setState:NSOffState];
  386. }
  387. }
  388. [buttonOff setState:NSOffState];
  389. [menuItemColor setState:NSOnState];
  390. // Store new manually selected color
  391. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  392. NSData *data = [NSArchiver archivedDataWithRootObject:color];
  393. [store setObject:data forKey:PREF_COLOR];
  394. [store setObject:TEXT_MANUAL forKey:PREF_LED_MODE];
  395. [store synchronize];
  396. }
  397. - (void)setLightsR:(unsigned char)r G:(unsigned char)g B:(unsigned char)b {
  398. if ([serial isOpen]) {
  399. unsigned char red = r * ([brightnessSlider floatValue] / 100.0);
  400. unsigned char green = g * ([brightnessSlider floatValue] / 100.0);
  401. unsigned char blue = b * ([brightnessSlider floatValue] / 100.0);
  402. [serial sendString:[NSString stringWithFormat:@"RGB %d %d %d\n", red, green, blue]];
  403. } else {
  404. #ifdef DEBUG
  405. NSLog(@"Trying to send RGB without opened port!\n");
  406. #endif
  407. }
  408. }
  409. - (IBAction)relistSerialPorts:(id)sender {
  410. // Refill audio device list
  411. NSArray *inputDevices = [EZAudioDevice inputDevices];
  412. [menuAudio removeAllItems];
  413. for (int i = 0; i < [inputDevices count]; i++) {
  414. EZAudioDevice *dev = [inputDevices objectAtIndex:i];
  415. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[dev name] action:@selector(selectedVisualization:) keyEquivalent:@""];
  416. [item setTag:MENU_ITEM_TAG_AUDIO];
  417. NSString *lastModeString = [NSString stringWithFormat:TEXT_TEMPLATE_AUDIO, [dev name]];
  418. if ([lastModeString isEqualToString:[[NSUserDefaults standardUserDefaults] stringForKey:PREF_LED_MODE]]) {
  419. [self selectedVisualization:item];
  420. }
  421. [menuAudio addItem:item];
  422. }
  423. if ([inputDevices count] > 0) {
  424. [menuItemAudio setHidden:NO];
  425. } else {
  426. [menuItemAudio setHidden:YES];
  427. }
  428. // Refill port list
  429. NSArray *ports = [Serial listSerialPorts];
  430. [menuPorts removeAllItems];
  431. for (int i = 0; i < [ports count]; i++) {
  432. // Add Menu Item for this port
  433. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  434. [item setTag:MENU_ITEM_TAG_NOTHING];
  435. [menuPorts addItem:item];
  436. // Mark it if it is currently open
  437. if ([serial isOpen]) {
  438. if ([[ports objectAtIndex:i] isEqualToString:[serial portName]]) {
  439. [[menuPorts itemAtIndex:i] setState:NSOnState];
  440. }
  441. }
  442. }
  443. }
  444. - (void)setColorSelected:(NSMenuItem *)sender {
  445. NSColorPanel *cp = [NSColorPanel sharedColorPanel];
  446. [cp setTarget:self];
  447. [cp setAction:@selector(colorSelected:)];
  448. [cp setShowsAlpha:NO];
  449. [cp setContinuous:NO];
  450. [cp setMode:NSRGBModeColorPanel];
  451. // Try to restore last manually selected color
  452. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  453. NSData *lastColorData = [store dataForKey:PREF_COLOR];
  454. NSColor *lastColor = nil;
  455. if (lastColorData != nil) {
  456. lastColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:lastColorData];
  457. [cp setColor:lastColor];
  458. }
  459. [NSApp activateIgnoringOtherApps:YES];
  460. [application orderFrontColorPanel:cp];
  461. }
  462. - (void)colorSelected:(NSColorPanel *)sender {
  463. [self setLightsColor:[sender color]];
  464. }
  465. - (IBAction)sensitivityMoved:(NSSlider *)sender {
  466. [sensitivityLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", [sender floatValue]]];
  467. [AudioVisualizer setSensitivity:[sender floatValue]];
  468. // Store changed value in preferences
  469. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  470. [store setObject:[NSNumber numberWithFloat:[sender floatValue]] forKey:PREF_SENSITIVITY];
  471. [store synchronize];
  472. }
  473. - (IBAction)brightnessMoved:(NSSlider *)sender {
  474. [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", [sender floatValue]]];
  475. // Restore the current configuration for items where it won't happen automatically
  476. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  477. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  478. [self selectedVisualization:[menuColors itemAtIndex:i]];
  479. }
  480. }
  481. // Store changed value in preferences
  482. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  483. [store setObject:[NSNumber numberWithFloat:[sender floatValue]] forKey:PREF_BRIGHTNESS];
  484. [store synchronize];
  485. }
  486. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  487. if ([sender state] == NSOffState) {
  488. lastLEDMode = nil;
  489. // Stop previous timer setting
  490. if (animation != nil) {
  491. [animation invalidate];
  492. animation = nil;
  493. }
  494. // Stop previous audio data retrieval
  495. if (microphone != nil) {
  496. [microphone stopFetchingAudio];
  497. }
  498. // Turn off all other LED menu items
  499. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  500. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  501. lastLEDMode = [menuColors itemAtIndex:i];
  502. }
  503. [[menuColors itemAtIndex:i] setState:NSOffState];
  504. }
  505. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  506. if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
  507. lastLEDMode = [menuAnimations itemAtIndex:i];
  508. }
  509. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  510. }
  511. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  512. if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
  513. lastLEDMode = [menuVisualizations itemAtIndex:i];
  514. }
  515. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  516. }
  517. for (int i = 0; i < [menuAudio numberOfItems]; i++) {
  518. if ([[menuAudio itemAtIndex:i] state] == NSOnState) {
  519. lastLEDMode = [menuAudio itemAtIndex:i];
  520. }
  521. [[menuAudio itemAtIndex:i] setState:NSOffState];
  522. }
  523. for (int i = 0; i < [menuDisplays numberOfItems]; i++) {
  524. if ([[menuDisplays itemAtIndex:i] state] == NSOnState) {
  525. lastLEDMode = [menuDisplays itemAtIndex:i];
  526. }
  527. [[menuDisplays itemAtIndex:i] setState:NSOffState];
  528. }
  529. // Turn on "off" menu item
  530. [sender setState:NSOnState];
  531. // Store changed value in preferences
  532. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  533. [store setObject:@"" forKey:PREF_LED_MODE];
  534. [store synchronize];
  535. #ifdef DEBUG
  536. NSLog(@"Stored new mode: \"off\"!\n");
  537. #endif
  538. // Send command to turn off LEDs
  539. [self setLightsR:0 G:0 B:0];
  540. } else {
  541. // Try to restore last LED setting
  542. if (lastLEDMode != nil) {
  543. [self selectedVisualization:lastLEDMode];
  544. }
  545. }
  546. }
  547. - (IBAction)toggleLights:(NSMenuItem *)sender {
  548. if ([sender state] == NSOffState) {
  549. // Turn on lights
  550. if ([serial isOpen]) {
  551. [serial sendString:@"UV 1\n"];
  552. }
  553. [sender setState:NSOnState];
  554. } else {
  555. // Turn off lights
  556. if ([serial isOpen]) {
  557. [serial sendString:@"UV 0\n"];
  558. }
  559. [sender setState:NSOffState];
  560. }
  561. // Store changed value in preferences
  562. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  563. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  564. [store synchronize];
  565. }
  566. - (BOOL)timedVisualization:(NSString *)mode {
  567. // Stop previous timer setting
  568. if (animation != nil) {
  569. [animation invalidate];
  570. animation = nil;
  571. }
  572. // Stop previous audio data retrieval
  573. if (microphone != nil) {
  574. [microphone stopFetchingAudio];
  575. }
  576. // Schedule next invocation for this animation...
  577. if ([mode isEqualToString:TEXT_GPU_USAGE]) {
  578. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeGPUUsage:) userInfo:mode repeats:YES];
  579. } else if ([mode isEqualToString:TEXT_VRAM_USAGE]) {
  580. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeVRAMUsage:) userInfo:mode repeats:YES];
  581. } else if ([mode isEqualToString:TEXT_CPU_USAGE]) {
  582. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeCPUUsage:) userInfo:mode repeats:YES];
  583. } else if ([mode isEqualToString:TEXT_RAM_USAGE]) {
  584. animation = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(visualizeRAMUsage:) userInfo:mode repeats:YES];
  585. } else if ([mode isEqualToString:TEXT_CPU_TEMPERATURE]) {
  586. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeCPUTemperature:) userInfo:mode repeats:YES];
  587. } else if ([mode isEqualToString:TEXT_GPU_TEMPERATURE]) {
  588. animation = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(visualizeGPUTemperature:) userInfo:mode repeats:YES];
  589. } else if ([mode isEqualToString:TEXT_RGB_FADE]) {
  590. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeRGBFade:) userInfo:mode repeats:YES];
  591. } else if ([mode isEqualToString:TEXT_HSV_FADE]) {
  592. animation = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(visualizeHSVFade:) userInfo:mode repeats:YES];
  593. } else if ([mode isEqualToString:TEXT_RANDOM]) {
  594. animation = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(visualizeRandom:) userInfo:mode repeats:YES];
  595. } else {
  596. return NO;
  597. }
  598. #ifdef DEBUG
  599. NSLog(@"Scheduled animation for \"%@\"!\n", mode);
  600. #endif
  601. // ...and also execute it right now
  602. [animation fire];
  603. return YES;
  604. }
  605. - (void)displayVisualization:(NSMenuItem *)sender {
  606. // Stop previous timer setting
  607. if (animation != nil) {
  608. [animation invalidate];
  609. animation = nil;
  610. }
  611. // Stop previous audio data retrieval
  612. if (microphone != nil) {
  613. [microphone stopFetchingAudio];
  614. }
  615. // Schedule next invocation for this animation...
  616. animation = [NSTimer scheduledTimerWithTimeInterval:DISPLAY_DELAY target:self selector:@selector(visualizeDisplay:) userInfo:[NSNumber numberWithInteger:[sender tag]] repeats:YES];
  617. // ...and also execute it right now
  618. [animation fire];
  619. }
  620. - (void)selectedVisualization:(NSMenuItem *)sender {
  621. // Turn off all other LED menu items
  622. if (menuColors != nil) {
  623. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  624. [[menuColors itemAtIndex:i] setState:NSOffState];
  625. }
  626. }
  627. if (menuAnimations != nil) {
  628. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  629. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  630. }
  631. }
  632. if (menuVisualizations != nil) {
  633. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  634. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  635. }
  636. }
  637. if (menuAudio != nil) {
  638. for (int i = 0; i < [menuAudio numberOfItems]; i++) {
  639. [[menuAudio itemAtIndex:i] setState:NSOffState];
  640. }
  641. }
  642. if (menuDisplays != nil) {
  643. for (int i = 0; i < [menuDisplays numberOfItems]; i++) {
  644. [[menuDisplays itemAtIndex:i] setState:NSOffState];
  645. }
  646. }
  647. [buttonOff setState:NSOffState];
  648. [sender setState:NSOnState];
  649. // Check if it is a display
  650. BOOL found = NO;
  651. if ([sender tag] > MENU_ITEM_TAG_NOTHING) {
  652. found = YES;
  653. [self displayVisualization:sender];
  654. }
  655. // Check if it is an audio input device
  656. if ((found == NO) && ([sender tag] == MENU_ITEM_TAG_AUDIO)) {
  657. found = YES;
  658. BOOL foundDev = NO;
  659. NSArray *audioDevices = [EZAudioDevice inputDevices];
  660. for (int i = 0; i < [audioDevices count]; i++) {
  661. EZAudioDevice *dev = [audioDevices objectAtIndex:i];
  662. if ([[dev name] isEqualToString:[sender title]]) {
  663. // Send command to turn off LEDs
  664. [self setLightsR:0 G:0 B:0];
  665. // Found device
  666. foundDev = YES;
  667. if (microphone != nil) {
  668. [microphone stopFetchingAudio];
  669. } else {
  670. microphone = [EZMicrophone microphoneWithDelegate:self];
  671. }
  672. [microphone setDevice:dev];
  673. [microphone startFetchingAudio];
  674. break;
  675. }
  676. }
  677. if (foundDev == NO) {
  678. NSLog(@"Couldn't find device \"%@\"\n", [sender title]);
  679. [sender setState:NSOffState];
  680. return; // Don't store new mode
  681. }
  682. }
  683. // Check if it is the manual color select item
  684. if ((found == NO) && ([sender.title isEqualToString:TEXT_MANUAL])) {
  685. found = YES;
  686. [self colorSelected:[NSColorPanel sharedColorPanel]];
  687. }
  688. // Check if a static color was selected
  689. if ((found == NO) && (staticColors != nil)) {
  690. for (NSString *key in [staticColors allKeys]) {
  691. if ([sender.title isEqualToString:key]) {
  692. found = YES;
  693. // Stop previous timer setting
  694. if (animation != nil) {
  695. [animation invalidate];
  696. animation = nil;
  697. }
  698. // Stop previous audio data retrieval
  699. if (microphone != nil) {
  700. [microphone stopFetchingAudio];
  701. }
  702. NSColor *color = [staticColors valueForKey:key];
  703. unsigned char red = [color redComponent] * 255;
  704. unsigned char green = [color greenComponent] * 255;
  705. unsigned char blue = [color blueComponent] * 255;
  706. [self setLightsR:red G:green B:blue];
  707. break;
  708. }
  709. }
  710. }
  711. if (found == NO) {
  712. // Check if an animated visualization was selected
  713. if ([self timedVisualization:[sender title]] == NO) {
  714. NSLog(@"Unknown LED Visualization selected!\n");
  715. return;
  716. }
  717. }
  718. // Store changed value in preferences
  719. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  720. if ([sender tag] == MENU_ITEM_TAG_AUDIO) {
  721. // Prepend text for audio device names
  722. NSString *tmp = [NSString stringWithFormat:TEXT_TEMPLATE_AUDIO, [sender title]];
  723. [store setObject:tmp forKey:PREF_LED_MODE];
  724. } else {
  725. [store setObject:[sender title] forKey:PREF_LED_MODE];
  726. }
  727. [store synchronize];
  728. #ifdef DEBUG
  729. NSLog(@"Stored new mode: \"%@\"!\n", [sender title]);
  730. #endif
  731. }
  732. - (void)selectedSerialPort:(NSMenuItem *)source {
  733. // Store selection for next start-up
  734. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  735. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  736. [store synchronize];
  737. // De-select all other ports
  738. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  739. [[menuPorts itemAtIndex:i] setState:NSOffState];
  740. }
  741. // Select only the current port
  742. [source setState:NSOnState];
  743. // Close previously opened port, if any
  744. if ([serial isOpen]) {
  745. [serial closePort];
  746. }
  747. // Try to open selected port
  748. [serial setPortName:[source title]];
  749. if ([serial openPort] != 0) {
  750. [source setState:NSOffState];
  751. } else {
  752. // Restore the current configuration
  753. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  754. if ([[menuColors itemAtIndex:i] state] == NSOnState) {
  755. [self selectedVisualization:[menuColors itemAtIndex:i]];
  756. }
  757. }
  758. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  759. if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
  760. [self selectedVisualization:[menuAnimations itemAtIndex:i]];
  761. }
  762. }
  763. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  764. if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
  765. [self selectedVisualization:[menuVisualizations itemAtIndex:i]];
  766. }
  767. }
  768. for (int i = 0; i < [menuAudio numberOfItems]; i++) {
  769. if ([[menuAudio itemAtIndex:i] state] == NSOnState) {
  770. [self selectedVisualization:[menuAudio itemAtIndex:i]];
  771. }
  772. }
  773. for (int i = 0; i < [menuDisplays numberOfItems]; i++) {
  774. if ([[menuDisplays itemAtIndex:i] state] == NSOnState) {
  775. [self selectedVisualization:[menuDisplays itemAtIndex:i]];
  776. }
  777. }
  778. if ([buttonOff state] == NSOnState) {
  779. [buttonOff setState:NSOffState];
  780. [self turnLEDsOff:buttonOff];
  781. }
  782. if ([buttonLights state] == NSOnState) {
  783. [serial sendString:@"UV 1\n"];
  784. } else {
  785. [serial sendString:@"UV 0\n"];
  786. }
  787. }
  788. }
  789. - (IBAction)showAbout:(id)sender {
  790. [NSApp activateIgnoringOtherApps:YES];
  791. [application orderFrontStandardAboutPanel:self];
  792. }
  793. // ------------------------------------------------------
  794. // ----------------- Microphone Delegate ----------------
  795. // ------------------------------------------------------
  796. - (void)microphone:(EZMicrophone *)microphone hasAudioReceived:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels {
  797. __weak typeof (self) weakSelf = self;
  798. if (weakSelf.microphone == nil) {
  799. return;
  800. }
  801. // Getting audio data as an array of float buffer arrays that can be fed into the
  802. // EZAudioPlot, EZAudioPlotGL, or whatever visualization you would like to do with
  803. // the microphone data.
  804. dispatch_async(dispatch_get_main_queue(),^{
  805. if (weakSelf.microphone == nil) {
  806. return;
  807. }
  808. // buffer[0] = left channel, buffer[1] = right channel
  809. [AudioVisualizer updateBuffer:buffer[0] withBufferSize:bufferSize];
  810. });
  811. }
  812. - (void)microphone:(EZMicrophone *)microphone changedDevice:(EZAudioDevice *)device {
  813. // This is not always guaranteed to occur on the main thread so make sure you
  814. // wrap it in a GCD block
  815. dispatch_async(dispatch_get_main_queue(), ^{
  816. NSLog(@"Changed audio input device: %@", [device name]);
  817. });
  818. }
  819. // ------------------------------------------------------
  820. // ------------------- Visualizations -------------------
  821. // ------------------------------------------------------
  822. - (void)visualizeDisplay:(NSTimer *)timer {
  823. NSBitmapImageRep *screen = [Screenshot screenshot:[timer userInfo]];
  824. NSInteger spp = [screen samplesPerPixel];
  825. if (((spp != 3) && (spp != 4)) || ([screen isPlanar] == YES) || ([screen numberOfPlanes] != 1)) {
  826. NSLog(@"Unknown image format (%ld, %c, %ld)!\n", (long)spp, ([screen isPlanar] == YES) ? 'p' : 'n', (long)[screen numberOfPlanes]);
  827. return;
  828. }
  829. int redC = 0, greenC = 1, blueC = 2;
  830. if ([screen bitmapFormat] & NSAlphaFirstBitmapFormat) {
  831. redC = 1; greenC = 2; blueC = 3;
  832. }
  833. unsigned char *data = [screen bitmapData];
  834. unsigned long width = [screen pixelsWide];
  835. unsigned long height = [screen pixelsHigh];
  836. unsigned long max = width * height;
  837. unsigned long red = 0, green = 0, blue = 0;
  838. for (unsigned long i = 0; i < max; i += AVERAGE_COLOR_PERFORMANCE_INC) {
  839. unsigned long off = spp * i;
  840. red += data[off + redC];
  841. green += data[off + greenC];
  842. blue += data[off + blueC];
  843. }
  844. max /= AVERAGE_COLOR_PERFORMANCE_INC;
  845. [self setLightsR:(red / max) G:(green / max) B:(blue / max)];
  846. }
  847. - (void)visualizeGPUUsage:(NSTimer *)timer {
  848. NSNumber *usage;
  849. NSNumber *freeVRAM;
  850. NSNumber *usedVRAM;
  851. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  852. NSLog(@"Error reading GPU information\n");
  853. } else {
  854. double h = [AppDelegate map:[usage doubleValue] FromMin:0.0 FromMax:100.0 ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
  855. #ifdef DEBUG
  856. NSLog(@"GPU Usage: %.3f%%\n", [usage doubleValue]);
  857. #endif
  858. unsigned char r, g, b;
  859. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  860. [self setLightsR:r G:g B:b];
  861. }
  862. }
  863. - (void)visualizeVRAMUsage:(NSTimer *)timer {
  864. NSNumber *usage;
  865. NSNumber *freeVRAM;
  866. NSNumber *usedVRAM;
  867. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  868. NSLog(@"Error reading GPU information\n");
  869. } else {
  870. double h = [AppDelegate map:[freeVRAM doubleValue] FromMin:0.0 FromMax:([freeVRAM doubleValue] + [usedVRAM doubleValue]) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
  871. #ifdef DEBUG
  872. 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);
  873. #endif
  874. unsigned char r, g, b;
  875. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  876. [self setLightsR:r G:g B:b];
  877. }
  878. }
  879. - (void)visualizeCPUUsage:(NSTimer *)timer {
  880. JSKMCPUUsageInfo cpuUsageInfo = [JSKSystemMonitor systemMonitor].cpuUsageInfo;
  881. double h = [AppDelegate map:cpuUsageInfo.usage FromMin:0.0 FromMax:100.0 ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
  882. #ifdef DEBUG
  883. NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
  884. #endif
  885. unsigned char r, g, b;
  886. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  887. [self setLightsR:r G:g B:b];
  888. }
  889. - (void)visualizeRAMUsage:(NSTimer *)timer {
  890. JSKMMemoryUsageInfo memoryUsageInfo = [JSKSystemMonitor systemMonitor].memoryUsageInfo;
  891. double h = [AppDelegate map:memoryUsageInfo.freeMemory FromMin:0.0 FromMax:(memoryUsageInfo.usedMemory + memoryUsageInfo.freeMemory) ToMin:RAM_COLOR_MIN ToMax:RAM_COLOR_MAX];
  892. #ifdef DEBUG
  893. 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);
  894. #endif
  895. unsigned char r, g, b;
  896. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  897. [self setLightsR:r G:g B:b];
  898. }
  899. - (void)visualizeGPUTemperature:(NSTimer *)timer {
  900. JSKSMC *smc = [JSKSMC smc];
  901. double temp = [smc temperatureInCelsiusForKey:KEY_GPU_TEMPERATURE];
  902. if (temp > 1000.0) {
  903. temp /= 1000.0;
  904. }
  905. if (temp > GPU_TEMP_MAX) {
  906. temp = GPU_TEMP_MAX;
  907. }
  908. if (temp < GPU_TEMP_MIN) {
  909. temp = GPU_TEMP_MIN;
  910. }
  911. double h = [AppDelegate map:temp FromMin:GPU_TEMP_MIN FromMax:GPU_TEMP_MAX ToMin:GPU_COLOR_MIN ToMax:GPU_COLOR_MAX];
  912. #ifdef DEBUG
  913. NSLog(@"GPU Temp %.2f mapped to color %.2f!\n", temp, h);
  914. #endif
  915. unsigned char r, g, b;
  916. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  917. [self setLightsR:r G:g B:b];
  918. }
  919. - (void)visualizeCPUTemperature:(NSTimer *)timer {
  920. JSKSMC *smc = [JSKSMC smc];
  921. double temp = [smc temperatureInCelsiusForKey:KEY_CPU_TEMPERATURE];
  922. if (temp > 1000.0) {
  923. temp /= 1000.0;
  924. }
  925. if (temp > CPU_TEMP_MAX) {
  926. temp = CPU_TEMP_MAX;
  927. }
  928. if (temp < CPU_TEMP_MIN) {
  929. temp = CPU_TEMP_MIN;
  930. }
  931. double h = [AppDelegate map:temp FromMin:CPU_TEMP_MIN FromMax:CPU_TEMP_MAX ToMin:CPU_COLOR_MIN ToMax:CPU_COLOR_MAX];
  932. #ifdef DEBUG
  933. NSLog(@"CPU Temp %.2f mapped to color %.2f!\n", temp, h);
  934. #endif
  935. unsigned char r, g, b;
  936. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  937. [self setLightsR:r G:g B:b];
  938. }
  939. - (void)visualizeRGBFade:(NSTimer *)timer {
  940. static unsigned char color[3] = { 255, 0, 0 };
  941. static int dec = 0;
  942. static int val = 0;
  943. // Adapted from:
  944. // https://gist.github.com/jamesotron/766994
  945. if (dec < 3) {
  946. int inc = (dec == 2) ? 0 : (dec + 1);
  947. if (val < 255) {
  948. color[dec] -= 1;
  949. color[inc] += 1;
  950. val++;
  951. } else {
  952. val = 0;
  953. dec++;
  954. }
  955. } else {
  956. dec = 0;
  957. }
  958. [self setLightsR:color[0] G:color[1] B:color[2]];
  959. }
  960. - (void)visualizeHSVFade:(NSTimer *)timer {
  961. static float h = 0.0;
  962. if (h < 359.0) {
  963. h += 0.5;
  964. } else {
  965. h = 0.0;
  966. }
  967. unsigned char r, g, b;
  968. [AppDelegate convertH:h S:1.0 V:1.0 toR:&r G:&g B:&b];
  969. [self setLightsR:r G:g B:b];
  970. }
  971. - (void)visualizeRandom:(NSTimer *)timer {
  972. [self setLightsR:rand() % 256 G:rand() % 256 B:rand() % 256];
  973. }
  974. // -----------------------------------------------------
  975. // --------------------- Utilities ---------------------
  976. // -----------------------------------------------------
  977. + (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
  978. double norm = (val - fmin) / (fmax - fmin);
  979. return (norm * (tmax - tmin)) + tmin;
  980. }
  981. + (void)convertH:(double)h S:(double)s V:(double)v toR:(unsigned char *)r G:(unsigned char *)g B:(unsigned char *)b {
  982. // Adapted from:
  983. // https://gist.github.com/hdznrrd/656996
  984. if (s == 0.0) {
  985. // Achromatic
  986. *r = *g = *b = (unsigned char)(v * 255);
  987. return;
  988. }
  989. h /= 60; // sector 0 to 5
  990. int i = floor(h);
  991. double f = h - i; // factorial part of h
  992. double p = v * (1 - s);
  993. double q = v * (1 - s * f);
  994. double t = v * (1 - s * (1 - f));
  995. switch (i) {
  996. case 0:
  997. *r = (unsigned char)round(255 * v);
  998. *g = (unsigned char)round(255 * t);
  999. *b = (unsigned char)round(255 * p);
  1000. break;
  1001. case 1:
  1002. *r = (unsigned char)round(255 * q);
  1003. *g = (unsigned char)round(255 * v);
  1004. *b = (unsigned char)round(255 * p);
  1005. break;
  1006. case 2:
  1007. *r = (unsigned char)round(255 * p);
  1008. *g = (unsigned char)round(255 * v);
  1009. *b = (unsigned char)round(255 * t);
  1010. break;
  1011. case 3:
  1012. *r = (unsigned char)round(255 * p);
  1013. *g = (unsigned char)round(255 * q);
  1014. *b = (unsigned char)round(255 * v);
  1015. break;
  1016. case 4:
  1017. *r = (unsigned char)round(255 * t);
  1018. *g = (unsigned char)round(255 * p);
  1019. *b = (unsigned char)round(255 * v);
  1020. break;
  1021. default: case 5:
  1022. *r = (unsigned char)round(255 * v);
  1023. *g = (unsigned char)round(255 * p);
  1024. *b = (unsigned char)round(255 * q);
  1025. break;
  1026. }
  1027. }
  1028. @end