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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #define PREF_SERIAL_PORT @"SerialPort"
  12. #define PREF_LIGHTS_STATE @"LightState"
  13. @interface AppDelegate ()
  14. @end
  15. @implementation AppDelegate
  16. @synthesize statusMenu, application;
  17. @synthesize menuColors, menuAnimations, menuVisualizations, menuPorts;
  18. @synthesize buttonOff, buttonLights;
  19. @synthesize statusItem, statusImage;
  20. @synthesize staticColors;
  21. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  22. // Prepare status bar menu
  23. statusImage = [NSImage imageNamed:@"MenuIcon"];
  24. [statusImage setTemplate:YES];
  25. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  26. [statusItem setImage:statusImage];
  27. [statusItem setMenu:statusMenu];
  28. // Set default configuration values, load existing ones
  29. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  30. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
  31. [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
  32. [store registerDefaults:appDefaults];
  33. [store synchronize];
  34. NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
  35. BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
  36. // Prepare static colors menu
  37. staticColors = [NSDictionary dictionaryWithObjectsAndKeys:
  38. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:0.0f alpha:0.0f], @"Red",
  39. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:0.0f alpha:0.0f], @"Green",
  40. [NSColor colorWithCalibratedRed:0.0f green:0.0f blue:1.0f alpha:0.0f], @"Blue",
  41. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:1.0f alpha:0.0f], @"Cyan",
  42. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:1.0f alpha:0.0f], @"Magenta",
  43. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:0.0f alpha:0.0f], @"Yellow",
  44. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:0.0f], @"White",
  45. nil];
  46. for (NSString *key in [staticColors allKeys]) {
  47. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedStaticColor:) keyEquivalent:@""];
  48. [menuColors addItem:item];
  49. // TODO Enable item if it was last used
  50. }
  51. // TODO Prepare animations menu
  52. // Check if GPU Stats are available, add menu items if so
  53. NSNumber *usage;
  54. NSNumber *freeVRAM;
  55. NSNumber *usedVRAM;
  56. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  57. NSLog(@"Error reading GPU information\n");
  58. } else {
  59. NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:@"GPU Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  60. [menuVisualizations addItem:itemUsage];
  61. NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:@"VRAM Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  62. [menuVisualizations addItem:itemVRAM];
  63. // TODO Enable item if it was last used
  64. }
  65. // Prepare serial port menu
  66. NSArray *ports = [Serial listSerialPorts];
  67. if ([ports count] > 0) {
  68. [menuPorts removeAllItems];
  69. for (int i = 0; i < [ports count]; i++) {
  70. // Add Menu Item for this port
  71. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  72. [menuPorts addItem:item];
  73. // Set Enabled if it was used the last time
  74. if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
  75. [[menuPorts itemAtIndex:i] setState:NSOnState];
  76. }
  77. }
  78. }
  79. // TODO Open serial port, if it was already specified and found
  80. // Restore previously used lights configuration
  81. if (turnOnLights) {
  82. // TODO Turn on lights
  83. [buttonLights setState:NSOnState];
  84. } else {
  85. // TODO Turn off lights
  86. }
  87. // TODO Restore previously used LED configuration
  88. }
  89. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  90. // TODO Close serial port, if it was specified and opened
  91. }
  92. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  93. if ([sender state] == NSOffState) {
  94. // Turn off all other LED menu items
  95. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  96. [[menuColors itemAtIndex:i] setState:NSOffState];
  97. }
  98. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  99. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  100. }
  101. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  102. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  103. }
  104. // Turn on "off" menu item
  105. [sender setState:NSOnState];
  106. // TODO Send command to turn off LEDs
  107. } else {
  108. // TODO Try to restore last LED setting
  109. }
  110. }
  111. - (IBAction)toggleLights:(NSMenuItem *)sender {
  112. if ([sender state] == NSOffState) {
  113. // TODO Turn on lights
  114. [sender setState:NSOnState];
  115. } else {
  116. // TODO Turn off lights
  117. [sender setState:NSOffState];
  118. }
  119. // Store changed value in preferences
  120. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  121. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  122. [store synchronize];
  123. }
  124. - (void)selectedStaticColor:(NSMenuItem *)source {
  125. }
  126. - (void)selectedGPUVisualization:(NSMenuItem *)sender {
  127. }
  128. - (void)selectedSerialPort:(NSMenuItem *)source {
  129. // Store selection for next start-up
  130. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  131. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  132. [store synchronize];
  133. // De-select all other ports
  134. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  135. [[menuPorts itemAtIndex:i] setState:NSOffState];
  136. }
  137. // Select only the current port
  138. [source setState:NSOnState];
  139. // TODO Close previously opened port, if any
  140. // TODO Try to open selected port
  141. }
  142. - (IBAction)showAbout:(id)sender {
  143. [NSApp activateIgnoringOtherApps:YES];
  144. [application orderFrontStandardAboutPanel:self];
  145. }
  146. @end