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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, gamepadCreated;
  16. - (id)init {
  17. self = [super init];
  18. if (self != nil) {
  19. serialThread = nil;
  20. gamepadCreated = NO;
  21. [self setDelegate:self];
  22. }
  23. return self;
  24. }
  25. - (BOOL)windowShouldClose:(id)sender {
  26. if (gamepadCreated) {
  27. [fooHID close];
  28. gamepadCreated = NO;
  29. }
  30. if (serialThread != nil) {
  31. // Stop thread and wait for it to finish
  32. [serialThread setRunning:NO];
  33. while ([serialThread isFinished] == NO) {
  34. usleep(1000);
  35. }
  36. }
  37. return YES;
  38. }
  39. - (IBAction)connectButtonPressed:(id)sender {
  40. if (serialThread == nil) {
  41. serialThread = [[Thread alloc] initWithWindow:self];
  42. [serialThread setName:@"Serial Communication"];
  43. [serialThread setPortName:[portList titleOfSelectedItem]];
  44. if ([serialThread openPort] != 0) {
  45. serialThread = nil;
  46. } else {
  47. [serialThread start];
  48. [connectButton setTitle:@"Disconnect"];
  49. }
  50. } else {
  51. [serialThread setRunning:NO];
  52. serialThread = nil;
  53. [connectButton setTitle:@"Connect"];
  54. }
  55. }
  56. - (IBAction)createButtonPressed:(id)sender {
  57. if (gamepadCreated) {
  58. [fooHID close];
  59. gamepadCreated = NO;
  60. [createButton setTitle:@"Create"];
  61. } else {
  62. if ([fooHID init] == 0) {
  63. gamepadCreated = YES;
  64. [createButton setTitle:@"Destroy"];
  65. }
  66. }
  67. }
  68. - (void)populatePortList {
  69. NSArray *ports = [Serial listSerialPorts];
  70. if ([ports count] > 0) {
  71. [portList removeAllItems];
  72. [portList addItemsWithTitles:ports];
  73. for (int i = 0; i < [ports count]; i++) {
  74. if ([[ports objectAtIndex:i] isEqualToString:@"/dev/tty.SLAB_USBtoUART"]) {
  75. [portList selectItemAtIndex:i];
  76. }
  77. }
  78. [portList setEnabled:YES];
  79. [connectButton setEnabled:YES];
  80. [createButton setEnabled:YES];
  81. } else {
  82. NSLog(@"Couldn't find any serial ports!\n");
  83. }
  84. }
  85. - (void)setChannels:(id)data {
  86. if ([data count] < 6) {
  87. NSLog(@"Not enough channel data (%lu)?!\n", (unsigned long)[data count]);
  88. } else {
  89. [level1 setDoubleValue:[[data objectAtIndex:0] doubleValue]];
  90. [level2 setDoubleValue:[[data objectAtIndex:1] doubleValue]];
  91. [level3 setDoubleValue:[[data objectAtIndex:2] doubleValue]];
  92. [level4 setDoubleValue:[[data objectAtIndex:3] doubleValue]];
  93. [level5 setDoubleValue:[[data objectAtIndex:4] doubleValue]];
  94. [level6 setDoubleValue:[[data objectAtIndex:5] doubleValue]];
  95. if (gamepadCreated) {
  96. [fooHID send:data];
  97. }
  98. }
  99. }
  100. @end