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