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

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