diff --git a/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Pods-ROAR.xcscheme b/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Pods-ROAR.xcscheme new file mode 100644 index 0000000..3b13247 --- /dev/null +++ b/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Pods-ROAR.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Pods/Pods.xcodeproj/xcuserdata/adamcurtis.xcuserdatad/xcschemes/xcschememanagement.plist b/Pods/Pods.xcodeproj/xcuserdata/adamcurtis.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..7eeba79 --- /dev/null +++ b/Pods/Pods.xcodeproj/xcuserdata/adamcurtis.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + Pods-ROAR.xcscheme_^#shared#^_ + + orderHint + 0 + + + SuppressBuildableAutocreation + + 116FD2C091C5A305EDD68EF70CE006DA + + primary + + + + + diff --git a/ROAR.xcodeproj/project.pbxproj b/ROAR.xcodeproj/project.pbxproj index 552febd..a97fc33 100644 --- a/ROAR.xcodeproj/project.pbxproj +++ b/ROAR.xcodeproj/project.pbxproj @@ -27,7 +27,7 @@ E83E6CE426ED6981006B1E39 /* Loaf in Frameworks */ = {isa = PBXBuildFile; productRef = E83E6CE326ED6981006B1E39 /* Loaf */; }; E83E6CE626ED6A9A006B1E39 /* SelectBluetoothViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E83E6CE526ED6A9A006B1E39 /* SelectBluetoothViewController.swift */; }; E83E6CE926ED7147006B1E39 /* JGProgressHUD in Frameworks */ = {isa = PBXBuildFile; productRef = E83E6CE826ED7147006B1E39 /* JGProgressHUD */; }; - E87D8FFB2737A9F60078F31F /* ChooseBLEViewController+BLE.swift in Sources */ = {isa = PBXBuildFile; fileRef = E87D8FFA2737A9F60078F31F /* ChooseBLEViewController+BLE.swift */; }; + E87D8FFB2737A9F60078F31F /* ChooseBLEViewController+BLE_original.swift in Sources */ = {isa = PBXBuildFile; fileRef = E87D8FFA2737A9F60078F31F /* ChooseBLEViewController+BLE_original.swift */; }; E88C3796270557F800289DC1 /* models.scnassets in Resources */ = {isa = PBXBuildFile; fileRef = E88C3795270557F800289DC1 /* models.scnassets */; }; E88C37A22705594800289DC1 /* ARPhoto2ViewController+Record.swift in Sources */ = {isa = PBXBuildFile; fileRef = E88C379C2705594800289DC1 /* ARPhoto2ViewController+Record.swift */; }; E88C37A32705594800289DC1 /* ARPhoto2ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E88C379D2705594800289DC1 /* ARPhoto2ViewController.swift */; }; @@ -71,7 +71,7 @@ A498B2D702D7CB6768B7DD47 /* Pods_ROAR.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ROAR.framework; sourceTree = BUILT_PRODUCTS_DIR; }; AA70681EA7FA776D886D4DC2 /* Pods-ROAR.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ROAR.release.xcconfig"; path = "Target Support Files/Pods-ROAR/Pods-ROAR.release.xcconfig"; sourceTree = ""; }; E821AECA27274AF1009F49D2 /* ScanQRCodeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScanQRCodeViewController.swift; sourceTree = ""; }; - E83E6C9D26ED57BF006B1E39 /* ROAR.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ROAR.app; sourceTree = BUILT_PRODUCTS_DIR; }; + E83E6C9D26ED57BF006B1E39 /* HardwarePID-ROAR.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "HardwarePID-ROAR.app"; sourceTree = BUILT_PRODUCTS_DIR; }; E83E6CA026ED57BF006B1E39 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; E83E6CA226ED57BF006B1E39 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; E83E6CA426ED57BF006B1E39 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -91,7 +91,7 @@ E83E6CDC26ED5D4F006B1E39 /* ViewController+AR.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ViewController+AR.swift"; sourceTree = ""; }; E83E6CDE26ED623E006B1E39 /* ControlCenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControlCenter.swift; sourceTree = ""; }; E83E6CE526ED6A9A006B1E39 /* SelectBluetoothViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectBluetoothViewController.swift; sourceTree = ""; }; - E87D8FFA2737A9F60078F31F /* ChooseBLEViewController+BLE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChooseBLEViewController+BLE.swift"; sourceTree = ""; }; + E87D8FFA2737A9F60078F31F /* ChooseBLEViewController+BLE_original.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChooseBLEViewController+BLE_original.swift"; sourceTree = ""; }; E88C3795270557F800289DC1 /* models.scnassets */ = {isa = PBXFileReference; lastKnownFileType = wrapper.scnassets; path = models.scnassets; sourceTree = ""; }; E88C379C2705594800289DC1 /* ARPhoto2ViewController+Record.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ARPhoto2ViewController+Record.swift"; sourceTree = ""; }; E88C379D2705594800289DC1 /* ARPhoto2ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ARPhoto2ViewController.swift; sourceTree = ""; }; @@ -176,7 +176,7 @@ E83E6C9E26ED57BF006B1E39 /* Products */ = { isa = PBXGroup; children = ( - E83E6C9D26ED57BF006B1E39 /* ROAR.app */, + E83E6C9D26ED57BF006B1E39 /* HardwarePID-ROAR.app */, E83E6CB326ED57C4006B1E39 /* ROARTests.xctest */, E83E6CBE26ED57C4006B1E39 /* ROARUITests.xctest */, ); @@ -246,7 +246,7 @@ children = ( E8C8EC912737A5F300CB2683 /* ChooseBLE.storyboard */, E8C8EC932737A60700CB2683 /* ChooseBLEViewController.swift */, - E87D8FFA2737A9F60078F31F /* ChooseBLEViewController+BLE.swift */, + E87D8FFA2737A9F60078F31F /* ChooseBLEViewController+BLE_original.swift */, ); path = ChooseBLE; sourceTree = ""; @@ -263,9 +263,9 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ - E83E6C9C26ED57BF006B1E39 /* ROAR */ = { + E83E6C9C26ED57BF006B1E39 /* HardwarePID-ROAR */ = { isa = PBXNativeTarget; - buildConfigurationList = E83E6CC726ED57C4006B1E39 /* Build configuration list for PBXNativeTarget "ROAR" */; + buildConfigurationList = E83E6CC726ED57C4006B1E39 /* Build configuration list for PBXNativeTarget "HardwarePID-ROAR" */; buildPhases = ( E1EA542063A4D250171F60C5 /* [CP] Check Pods Manifest.lock */, E83E6C9926ED57BF006B1E39 /* Sources */, @@ -276,7 +276,7 @@ ); dependencies = ( ); - name = ROAR; + name = "HardwarePID-ROAR"; packageProductDependencies = ( E83E6CD726ED5B9E006B1E39 /* Vapor */, E83E6CDA26ED5C20006B1E39 /* SwiftyBeaver */, @@ -289,7 +289,7 @@ E8EABA302737C2650009DCC1 /* CocoaAsyncSocket */, ); productName = ROAR; - productReference = E83E6C9D26ED57BF006B1E39 /* ROAR.app */; + productReference = E83E6C9D26ED57BF006B1E39 /* HardwarePID-ROAR.app */; productType = "com.apple.product-type.application"; }; E83E6CB226ED57C4006B1E39 /* ROARTests */ = { @@ -371,7 +371,7 @@ projectDirPath = ""; projectRoot = ""; targets = ( - E83E6C9C26ED57BF006B1E39 /* ROAR */, + E83E6C9C26ED57BF006B1E39 /* HardwarePID-ROAR */, E83E6CB226ED57C4006B1E39 /* ROARTests */, E83E6CBD26ED57C4006B1E39 /* ROARUITests */, ); @@ -456,7 +456,7 @@ E83E6CD526ED5AEF006B1E39 /* AppStruct.swift in Sources */, E8C8EC8F273466C600CB2683 /* ViewController+UDP.swift in Sources */, E88C37A42705594800289DC1 /* SelectContentDelegate.swift in Sources */, - E87D8FFB2737A9F60078F31F /* ChooseBLEViewController+BLE.swift in Sources */, + E87D8FFB2737A9F60078F31F /* ChooseBLEViewController+BLE_original.swift in Sources */, E88C37A22705594800289DC1 /* ARPhoto2ViewController+Record.swift in Sources */, E821AECB27274AF1009F49D2 /* ScanQRCodeViewController.swift in Sources */, E83E6CA326ED57BF006B1E39 /* SceneDelegate.swift in Sources */, @@ -484,12 +484,12 @@ /* Begin PBXTargetDependency section */ E83E6CB526ED57C4006B1E39 /* PBXTargetDependency */ = { isa = PBXTargetDependency; - target = E83E6C9C26ED57BF006B1E39 /* ROAR */; + target = E83E6C9C26ED57BF006B1E39 /* HardwarePID-ROAR */; targetProxy = E83E6CB426ED57C4006B1E39 /* PBXContainerItemProxy */; }; E83E6CC026ED57C4006B1E39 /* PBXTargetDependency */ = { isa = PBXTargetDependency; - target = E83E6C9C26ED57BF006B1E39 /* ROAR */; + target = E83E6C9C26ED57BF006B1E39 /* HardwarePID-ROAR */; targetProxy = E83E6CBF26ED57C4006B1E39 /* PBXContainerItemProxy */; }; /* End PBXTargetDependency section */ @@ -638,7 +638,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = D9D2HSUW4S; INFOPLIST_FILE = ROAR/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -661,7 +661,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = D9D2HSUW4S; INFOPLIST_FILE = ROAR/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -772,7 +772,7 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - E83E6CC726ED57C4006B1E39 /* Build configuration list for PBXNativeTarget "ROAR" */ = { + E83E6CC726ED57C4006B1E39 /* Build configuration list for PBXNativeTarget "HardwarePID-ROAR" */ = { isa = XCConfigurationList; buildConfigurations = ( E83E6CC826ED57C4006B1E39 /* Debug */, diff --git a/ROAR.xcodeproj/xcshareddata/xcschemes/HardwarePID-ROAR.xcscheme b/ROAR.xcodeproj/xcshareddata/xcschemes/HardwarePID-ROAR.xcscheme new file mode 100644 index 0000000..14ecf9c --- /dev/null +++ b/ROAR.xcodeproj/xcshareddata/xcschemes/HardwarePID-ROAR.xcscheme @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ROAR.xcodeproj/xcuserdata/adamcurtis.xcuserdatad/xcschemes/xcschememanagement.plist b/ROAR.xcodeproj/xcuserdata/adamcurtis.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..828942f --- /dev/null +++ b/ROAR.xcodeproj/xcuserdata/adamcurtis.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,37 @@ + + + + + SchemeUserState + + HardwarePID-ROAR.xcscheme_^#shared#^_ + + orderHint + 1 + + ROAR.xcscheme_^#shared#^_ + + orderHint + 1 + + + SuppressBuildableAutocreation + + E83E6C9C26ED57BF006B1E39 + + primary + + + E83E6CB226ED57C4006B1E39 + + primary + + + E83E6CBD26ED57C4006B1E39 + + primary + + + + + diff --git a/ROAR.xcworkspace/xcuserdata/adamcurtis.xcuserdatad/UserInterfaceState.xcuserstate b/ROAR.xcworkspace/xcuserdata/adamcurtis.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..858de53 Binary files /dev/null and b/ROAR.xcworkspace/xcuserdata/adamcurtis.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ROAR.xcworkspace/xcuserdata/adamcurtis.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ROAR.xcworkspace/xcuserdata/adamcurtis.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..f8aa3f2 --- /dev/null +++ b/ROAR.xcworkspace/xcuserdata/adamcurtis.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,6 @@ + + + diff --git a/ROAR/AppStruct.swift b/ROAR/AppStruct.swift index 9c17870..72f3812 100644 --- a/ROAR/AppStruct.swift +++ b/ROAR/AppStruct.swift @@ -6,7 +6,12 @@ // import Foundation +import CoreBluetooth + struct AppInfo : Codable { + + + static var sessionData: SessionData = SessionData() static var curr_world_name: String = "berkeley" static var bluetootConfigurations: BluetoothConfigurations? = nil @@ -19,6 +24,15 @@ struct AppInfo : Codable { return "\(name)_ar_experience_data" } + static func forget() { + UserDefaults.standard.setValue([ + "bluetooth_name": "none", + "bluetooth_uuid": "none"], + forKey: "bluetooth_data") + + UserDefaults.standard.setValue(AppInfo.pc_address, forKey: "pc_address") + } + static func save() { UserDefaults.standard.setValue([ "bluetooth_name": AppInfo.bluetootConfigurations?.name ?? "", diff --git a/ROAR/Base.lproj/Main.storyboard b/ROAR/Base.lproj/Main.storyboard index 5b17a6e..40d70a2 100644 --- a/ROAR/Base.lproj/Main.storyboard +++ b/ROAR/Base.lproj/Main.storyboard @@ -12,7 +12,7 @@ - + @@ -127,7 +127,7 @@ - + @@ -142,7 +142,7 @@ - + @@ -182,87 +182,85 @@ - + - - + + + + + - - + + - - + + - @@ -329,8 +381,11 @@ + + + @@ -342,7 +397,7 @@ - + @@ -353,42 +408,42 @@ - - + - + - + - + - - + + @@ -399,7 +454,7 @@ - + @@ -464,7 +519,7 @@ - + diff --git a/ROAR/Caliberation/CaliberationViewController+BLE.swift b/ROAR/Caliberation/CaliberationViewController+BLE.swift index deae470..bf4588d 100644 --- a/ROAR/Caliberation/CaliberationViewController+BLE.swift +++ b/ROAR/Caliberation/CaliberationViewController+BLE.swift @@ -70,6 +70,12 @@ extension CaliberationViewController:CBCentralManagerDelegate, CBPeripheralDeleg self.bluetoothPeripheral.readValue(for: self.velocityCharacteristic) } } + @objc + func readThrottle() { + if throtReturnCharacteristic != nil { + self.bluetoothPeripheral.readValue(for: self.throtReturnCharacteristic) + } + } func disconnectBluetooth() { self.bleTimer.invalidate() if self.bluetoothPeripheral != nil { @@ -79,8 +85,8 @@ extension CaliberationViewController:CBCentralManagerDelegate, CBPeripheralDeleg func writeToBluetoothDevice(throttle: CGFloat, steering: CGFloat){ // turn CGFloat into Int, and then into a string in format of (THROTTLE, STEERING) to send it. - let currThrottleRPM = throttle.map(from: self.iOSControllerRange, to: self.throttle_range) - var currSteeringRPM = steering.map(from: self.iOSControllerRange, to: self.steer_range) + let currThrottleRPM = throttle.map(from: self.ThrottleControllerRange, to: self.throttle_range) + var currSteeringRPM = steering.map(from: self.SteeringControllerRange, to: self.steer_range) currSteeringRPM = currSteeringRPM.clamped(to: 1000...2000) @@ -108,9 +114,15 @@ extension CaliberationViewController:CBCentralManagerDelegate, CBPeripheralDeleg if char.uuid.uuidString == "19B10011-E8F2-537E-4F6C-D104768A1215" { velocityCharacteristic = char } + if char.uuid.uuidString == "19B10012-E8F2-537E-4F6C-D104768A1214" { + newNameCharacteristic = char + } if char.uuid.uuidString == "19B10011-E8F2-537E-4F6C-D104768A1216" { configCharacteristic = char } + if char.uuid.uuidString == "19B10011-E8F2-537E-4F6C-D104768A1217" { + throtReturnCharacteristic = char + } } } } @@ -128,6 +140,21 @@ extension CaliberationViewController:CBCentralManagerDelegate, CBPeripheralDeleg self.velocity_label.text = "Current Velocity: \(self.velocity)" } } + if characteristic == throtReturnCharacteristic { + // catch a throttle change and update the throttle label + guard let throt = characteristic.value else { return } + self.throtReturn = throt.withUnsafeBytes { $0.load(as: Float.self) } + DispatchQueue.main.async { + self.throt_return_label.text = "Current throttle: \(self.throtReturn)" + } + } + } + + func sendBLENewName(peripheral: CBPeripheral, message: String){ + if newNameCharacteristic != nil { + peripheral.writeValue(message.data(using: .utf8)!, for: newNameCharacteristic, type: .withoutResponse) + //AppInfo.forget() + } } } diff --git a/ROAR/Caliberation/CaliberationViewController.swift b/ROAR/Caliberation/CaliberationViewController.swift index 3e85802..3c550f1 100644 --- a/ROAR/Caliberation/CaliberationViewController.swift +++ b/ROAR/Caliberation/CaliberationViewController.swift @@ -16,28 +16,37 @@ class CaliberationViewController: UIViewController { @IBOutlet weak var bleButton: UIButton! @IBOutlet weak var sendControlBtn: UIButton! @IBOutlet weak var sendKValuesBtn: UIButton! + @IBOutlet weak var requestBLENameChangeButton: UIButton! + @IBOutlet weak var newBLENameTextField: UITextField! @IBOutlet weak var throttleTextField: UITextField! @IBOutlet weak var SteeringTextField: UITextField! @IBOutlet weak var KpTextField: UITextField! @IBOutlet weak var KiTextField: UITextField! @IBOutlet weak var KdTextField: UITextField! @IBOutlet weak var velocity_label: UILabel! + @IBOutlet weak var throt_return_label: UILabel! var bluetoothPeripheral: CBPeripheral! var centralManager: CBCentralManager! var logger: SwiftyBeaver.Type {return (UIApplication.shared.delegate as! AppDelegate).logger} - var iOSControllerRange: ClosedRange = CGFloat(-1.0)...CGFloat(1.0); +// add velocity control + var ThrottleControllerRange: ClosedRange = CGFloat(-5.0)...CGFloat(5.0); + var SteeringControllerRange: ClosedRange = CGFloat(-1.0)...CGFloat(1.0); let throttle_range = CGFloat(1000)...CGFloat(2000) let steer_range = CGFloat(1000)...CGFloat(2000) var bleTimer: Timer! var bluetoothDispatchWorkitem:DispatchWorkItem! var bleControlCharacteristic: CBCharacteristic! var velocityCharacteristic: CBCharacteristic! + var throtReturnCharacteristic: CBCharacteristic! var configCharacteristic: CBCharacteristic! + var newNameCharacteristic: CBCharacteristic! var velocity: Float = 0 + var throtReturn: Float = 0 var readVelocityTimer: Timer! + var readThrottleTimer: Timer! override func viewDidLoad() { super.viewDidLoad() @@ -62,6 +71,8 @@ class CaliberationViewController: UIViewController { self.bleButton.setTitle("BLE: \(AppInfo.bluetootConfigurations?.name ?? "No Name")", for: .normal) AppInfo.save() self.readVelocityTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.readVelocity), userInfo: nil, repeats: true) + self.readThrottleTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.readThrottle), userInfo: nil, repeats: true) + } func onBLEDisconnected() { @@ -70,6 +81,9 @@ class CaliberationViewController: UIViewController { if self.readVelocityTimer != nil { self.readVelocityTimer.invalidate() } + if self.readThrottleTimer != nil { + self.readThrottleTimer.invalidate() + } } @IBAction func onSendControlBtnTapped(_ sender: UIButton) { @@ -85,6 +99,12 @@ class CaliberationViewController: UIViewController { } } + + @IBAction func onBLENameChangeBtn(_ sender: UIButton) { + let blename_str = self.newBLENameTextField.text ?? "0" + self.sendBLENewName(peripheral: self.bluetoothPeripheral, message: blename_str) + } + @IBAction func onSendKValuesTapped(_ sender: UIButton) { // First extract k values from text field and cast it into float var kp = Float(self.KpTextField.text ?? "1") ?? 1 diff --git a/ROAR/ChooseBLE/ChooseBLE.storyboard b/ROAR/ChooseBLE/ChooseBLE.storyboard index eed72e3..dc3f745 100644 --- a/ROAR/ChooseBLE/ChooseBLE.storyboard +++ b/ROAR/ChooseBLE/ChooseBLE.storyboard @@ -1,9 +1,9 @@ - + - + @@ -12,7 +12,7 @@ - + @@ -22,15 +22,15 @@ - + - - + + @@ -38,24 +38,24 @@ - - + + - - - - - - + + + + + + @@ -186,7 +192,7 @@ - + diff --git a/ROAR/ChooseBLE/ChooseBLEViewController+BLE.swift b/ROAR/ChooseBLE/ChooseBLEViewController+BLE_original.swift similarity index 62% rename from ROAR/ChooseBLE/ChooseBLEViewController+BLE.swift rename to ROAR/ChooseBLE/ChooseBLEViewController+BLE_original.swift index 5ea27d1..ed9691b 100644 --- a/ROAR/ChooseBLE/ChooseBLEViewController+BLE.swift +++ b/ROAR/ChooseBLE/ChooseBLEViewController+BLE_original.swift @@ -1,10 +1,8 @@ -// // ChooseBLEViewController+BLE.swift // ROAR // // Created by Michael Wu on 11/6/21. // - import Foundation import CoreBluetooth import UIKit @@ -85,12 +83,23 @@ extension ChooseBLEViewController: CBCentralManagerDelegate, CBPeripheralDelegat } func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { + if let characteristics = service.characteristics { for char in characteristics { print("discovered characteristic with UUID: \(char.uuid)") - if char.uuid.uuidString == "19B10011-E8F2-537E-4F6C-D104768A1214" { - bleControlCharacteristic = char + switch char.uuid.uuidString { + case "19B10011-E8F2-537E-4F6C-D104768A1214": + bleSpeedCharacteristic = char; + case "19B10015-E8F2-537E-4F6C-D104768A1214": + bleSteeringCharacteristic = char; + case "19B10012-E8F2-537E-4F6C-D104768A1214": + newNameCharacteristic = char + case "19B10013-E8F2-537E-4F6C-D104768A1214": + overrideCharacteristic = char + default: + self.logger.info("Unknown UUID discovered") } + } } } @@ -103,24 +112,57 @@ extension ChooseBLEViewController: CBCentralManagerDelegate, CBPeripheralDelegat } } - func writeToBluetoothDevice(throttle: CGFloat, steering: CGFloat){ - let currThrottleRPM = throttle.map(from: CGFloat(-1.0)...CGFloat(1.0), to: CGFloat(1000)...CGFloat(2000)) - var currSteeringRPM = steering.map(from: CGFloat(-1.0)...CGFloat(1.0), to: CGFloat(1000)...CGFloat(2000)) - - currSteeringRPM = currSteeringRPM.clamped(to: 1000...2000) - - - let message: String = "(" + String(Int(currThrottleRPM)) + "," + String(Int(currSteeringRPM)) + ")" +// ======Original Function==== +// func writeToBluetoothDevice(throttle: CGFloat, steering: CGFloat){ +// let currThrottleRPM = throttle.map(from: CGFloat(-1.0)...CGFloat(1.0), to: CGFloat(1000)...CGFloat(2000)) +// var currSteeringRPM = steering.map(from: CGFloat(-1.0)...CGFloat(1.0), to: CGFloat(1000)...CGFloat(2000)) +// +// currSteeringRPM = currSteeringRPM.clamped(to: 1000...2000) +// +// +// let message: String = "(" + String(Int(currThrottleRPM)) + "," + String(Int(currSteeringRPM)) + ")" +// if self.bluetoothPeripheral != nil { +// sendMessage(peripheral: self.bluetoothPeripheral, message: message) +// } +// } + + func writeSpeedToBluetoothDevice(throttle: CGFloat){ + let currThrottle = throttle.map(from: CGFloat(-1.0)...CGFloat(1.0), to: CGFloat(-3.0)...CGFloat(5.0)) if self.bluetoothPeripheral != nil { - sendMessage(peripheral: self.bluetoothPeripheral, message: message) + sendThrottle(peripheral: self.bluetoothPeripheral, message: Double(currThrottle)) } } - func sendMessage(peripheral: CBPeripheral, message: String) { - if bleControlCharacteristic != nil { - peripheral.writeValue(message.data(using: .utf8)!, for: bleControlCharacteristic, type: .withoutResponse) + + func writeSteeringToBluetoothDevice(steering: CGFloat){ + let currSteering = steering.map(from: CGFloat(-1.0)...CGFloat(1.0), to: CGFloat(1000)...CGFloat(2000)) + if self.bluetoothPeripheral != nil { + sendSteering(peripheral: self.bluetoothPeripheral, message: Double(currSteering)) + } + } + + + func sendThrottle(peripheral: CBPeripheral, message: Double) { + if bleSpeedCharacteristic != nil { + let double_data: Data = Data(withUnsafeBytes(of: message, Array.init)) + peripheral.writeValue(double_data, for: bleSpeedCharacteristic, type: .withoutResponse) } - } + func sendSteering(peripheral: CBPeripheral, message: Double) { + if bleSteeringCharacteristic != nil { + let double_data: Data = Data(withUnsafeBytes(of: message, Array.init)) + peripheral.writeValue(double_data, for: bleSteeringCharacteristic, type: .withoutResponse) + } + } + + func sendBLENewName(peripheral: CBPeripheral, message: String){ + if newNameCharacteristic != nil { + peripheral.writeValue(message.data(using: .utf8)!, for: newNameCharacteristic, type: .withoutResponse) + AppInfo.forget() + } + } + func override(peripheral: CBPeripheral, message: String){ + peripheral.writeValue(message.data(using: .utf8)!, for: overrideCharacteristic, type: .withoutResponse) + } } diff --git a/ROAR/ChooseBLE/ChooseBLEViewController.swift b/ROAR/ChooseBLE/ChooseBLEViewController.swift index 23df34b..cf24195 100644 --- a/ROAR/ChooseBLE/ChooseBLEViewController.swift +++ b/ROAR/ChooseBLE/ChooseBLEViewController.swift @@ -20,9 +20,14 @@ class ChooseBLEViewController: UIViewController { var logger: SwiftyBeaver.Type {return (UIApplication.shared.delegate as! AppDelegate).logger} var bluetoothPeripheral: CBPeripheral! var centralManager: CBCentralManager! - var bleControlCharacteristic: CBCharacteristic! + var bleSpeedCharacteristic: CBCharacteristic! + var bleSteeringCharacteristic: CBCharacteristic! + var newNameCharacteristic: CBCharacteristic! + var overrideCharacteristic: CBCharacteristic! var wifiMenu: UIMenu! + + @IBOutlet weak var chooseBLEButton: UIButton! @IBOutlet weak var throttleTextField: UITextField! @IBOutlet weak var steeringTextField: UITextField! @@ -81,21 +86,43 @@ class ChooseBLEViewController: UIViewController { self.view.transform = newTransform }) } + +// @IBAction func onSendControlBtnClicked(_ sender: UIButton) { +// let throttle_str = self.throttleTextField.text ?? "0" +// let steering_str = self.steeringTextField.text ?? "0" +// if self.checkControl(val: throttle_str) && self.checkControl(val: steering_str) { +// let throttle = Float(throttle_str)! +// let steering = Float(steering_str)! +// self.writeToBluetoothDevice(throttle: CGFloat(throttle), steering: CGFloat(steering)) +// Loaf.init("(\(throttle), \(steering)) sent", state: .info, location: .bottom, presentingDirection: .vertical, dismissingDirection: .vertical, sender: self).show(.short) +// } else { +// Loaf.init("Please make sure controls are in (-1, 1)", state: .error, location: .bottom, presentingDirection: .vertical, dismissingDirection: .vertical, sender: self).show(.long, completionHandler: nil) +// } +// } + @IBAction func onSendControlBtnClicked(_ sender: UIButton) { let throttle_str = self.throttleTextField.text ?? "0" let steering_str = self.steeringTextField.text ?? "0" if self.checkControl(val: throttle_str) && self.checkControl(val: steering_str) { - let throttle = Float(throttle_str)! - let steering = Float(steering_str)! - self.writeToBluetoothDevice(throttle: CGFloat(throttle), steering: CGFloat(steering)) + let throttle = Double(throttle_str)! + let steering = Double(steering_str)! + self.writeSpeedToBluetoothDevice(throttle: CGFloat(throttle)) + self.writeSteeringToBluetoothDevice(steering: CGFloat(steering)) Loaf.init("(\(throttle), \(steering)) sent", state: .info, location: .bottom, presentingDirection: .vertical, dismissingDirection: .vertical, sender: self).show(.short) } else { Loaf.init("Please make sure controls are in (-1, 1)", state: .error, location: .bottom, presentingDirection: .vertical, dismissingDirection: .vertical, sender: self).show(.long, completionHandler: nil) } } + @IBAction func onBLENameChangeBtn(_ sender: UIButton) { - + let blename_str = self.newBLENameTextField.text ?? "0" + self.sendBLENewName(peripheral: self.bluetoothPeripheral, message: blename_str) + } + + @IBAction func override(_ sender: UIButton) { + self.override(peripheral: self.bluetoothPeripheral, message: "override") } + @IBAction func onSSIDClicked(_ sender: UIButton) { } diff --git a/ROAR/Info.plist b/ROAR/Info.plist index 9f232c7..98b28a1 100644 --- a/ROAR/Info.plist +++ b/ROAR/Info.plist @@ -4,6 +4,8 @@ CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) + CFBundleDisplayName + HardwarePID-ROAR CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier