Mac OS X gamepad emulator for serial RC transmitters
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.

MainWindow.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // MainWindow.m
  3. // SerialGamepad
  4. //
  5. // Created by Thomas Buck on 14.12.15.
  6. // Copyright © 2015 xythobuz. All rights reserved.
  7. //
  8. #import "MainWindow.h"
  9. #import "fooHID.h"
  10. #import "Serial.h"
  11. #import "Thread.h"
  12. @implementation MainWindow
  13. @synthesize portList, connectButton, createButton;
  14. @synthesize level1, level2, level3, level4, level5, level6;
  15. @synthesize serialThread;
  16. - (id)init {
  17. self = [super init];
  18. if (self != nil) {
  19. serialThread = nil;
  20. [self setDelegate:self];
  21. }
  22. return self;
  23. }
  24. - (BOOL)windowShouldClose:(id)sender {
  25. if (serialThread != nil) {
  26. [fooHID close];
  27. // Stop thread and wait for it to finish
  28. [serialThread setRunning:NO];
  29. while ([serialThread isFinished] == NO) {
  30. usleep(1000);
  31. }
  32. }
  33. return YES;
  34. }
  35. - (IBAction)connectButtonPressed:(id)sender {
  36. if (serialThread == nil) {
  37. serialThread = [[Thread alloc] initWithWindow:self];
  38. [serialThread setName:@"Serial Communication"];
  39. [serialThread setPortName:[portList titleOfSelectedItem]];
  40. if ([serialThread openPort] != 0) {
  41. serialThread = nil;
  42. } else {
  43. if ([fooHID init] == 0) {
  44. [serialThread start];
  45. [connectButton setTitle:@"Disconnect"];
  46. } else {
  47. serialThread = nil;
  48. }
  49. }
  50. } else {
  51. [serialThread setRunning:NO];
  52. serialThread = nil;
  53. [fooHID close];
  54. [connectButton setTitle:@"Connect"];
  55. }
  56. }
  57. - (void)populatePortList {
  58. NSArray *ports = [Serial listSerialPorts];
  59. if ([ports count] > 0) {
  60. [portList removeAllItems];
  61. [portList addItemsWithTitles:ports];
  62. for (int i = 0; i < [ports count]; i++) {
  63. if ([[ports objectAtIndex:i] isEqualToString:@"/dev/tty.SLAB_USBtoUART"]) {
  64. [portList selectItemAtIndex:i];
  65. }
  66. }
  67. [portList setEnabled:YES];
  68. [connectButton setEnabled:YES];
  69. [createButton setEnabled:YES];
  70. } else {
  71. NSLog(@"Couldn't find any serial ports!\n");
  72. }
  73. }
  74. - (void)setChannels:(id)data {
  75. if ([data count] < 6) {
  76. NSLog(@"Not enough channel data (%lu)?!\n", (unsigned long)[data count]);
  77. } else {
  78. [level1 setDoubleValue:[[data objectAtIndex:0] doubleValue]];
  79. [level2 setDoubleValue:[[data objectAtIndex:1] doubleValue]];
  80. [level3 setDoubleValue:[[data objectAtIndex:2] doubleValue]];
  81. [level4 setDoubleValue:[[data objectAtIndex:3] doubleValue]];
  82. [level5 setDoubleValue:[[data objectAtIndex:4] doubleValue]];
  83. [level6 setDoubleValue:[[data objectAtIndex:5] doubleValue]];
  84. if (serialThread != nil) {
  85. [fooHID send:data];
  86. }
  87. }
  88. }
  89. @end