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.5KB

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