From 1e7ba5b6132883dd91545d345eb5f93985d35986 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 00:40:04 +0100 Subject: [PATCH 01/18] first Swiftly changes --- .../Versions/A/Headers/WWConstants.h | 12 +- .../Versions/A/Headers/WWRobotAPI.h | 2 + iOS/playground/Bridging-Helper.m | 16 ++ .../RobotControlPanelViewController.h | 1 + iOS/playground/RobotController.swift | 157 ++++++++++++++++++ iOS/playground/RobotViewController.swift | 51 ++++++ iOS/playground/playground-Bridging-Header.h | 14 ++ 7 files changed, 247 insertions(+), 6 deletions(-) create mode 100644 iOS/playground/Bridging-Helper.m create mode 100644 iOS/playground/RobotController.swift create mode 100644 iOS/playground/RobotViewController.swift create mode 100644 iOS/playground/playground-Bridging-Header.h diff --git a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h index 1f27547..7d7980b 100644 --- a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h +++ b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h @@ -77,11 +77,11 @@ typedef unsigned int WWComponentId; #define WW_SENSOR_ENCODER_RIGHT_WHEEL 3004 #define WW_SENSOR_MICROPHONE 3005 - -typedef unsigned int WWRobotType; -#define WW_ROBOT_UNKNOWN 1000 -#define WW_ROBOT_DASH 1001 -#define WW_ROBOT_DOT 1002 +typedef NS_ENUM(NSInteger, WWRobotType) { + WW_ROBOT_UNKNOWN = 1000, + WW_ROBOT_DASH = 1001, + WW_ROBOT_DOT = 1002, +}; typedef unsigned int WWPersonalityColorIndex; #define WW_PERSONALITY_COLOR_NONE 0 // aka white @@ -179,4 +179,4 @@ typedef enum { #undef wwstr -#endif \ No newline at end of file +#endif diff --git a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWRobotAPI.h b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWRobotAPI.h index 2d83730..7deb340 100644 --- a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWRobotAPI.h +++ b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWRobotAPI.h @@ -6,6 +6,8 @@ // Copyright (c) 2014 Wonder Workshop inc. (https://www.makewonder.com/) All rights reserved. // +#import + // common #import #import diff --git a/iOS/playground/Bridging-Helper.m b/iOS/playground/Bridging-Helper.m new file mode 100644 index 0000000..f6462c4 --- /dev/null +++ b/iOS/playground/Bridging-Helper.m @@ -0,0 +1,16 @@ +// +// Bridging-Helper.m +// playground +// +// Created by Jelle Alten on 09-11-16. +// Copyright © 2016 Wonder Workshop. All rights reserved. +// + +#import + + +@implementation WWRobotManager (shared) ++ (WWRobotManager*) sharedManager { + return [WWRobotManager manager]; +}; +@end diff --git a/iOS/playground/RobotControlPanelViewController.h b/iOS/playground/RobotControlPanelViewController.h index 31fd48e..daa0cd3 100644 --- a/iOS/playground/RobotControlPanelViewController.h +++ b/iOS/playground/RobotControlPanelViewController.h @@ -6,6 +6,7 @@ // Copyright (c) 2014 Wonder Workshop. All rights reserved. // +@import UIKit; @class RobotControlViewController; @interface RobotControlPanelViewController : UIViewController diff --git a/iOS/playground/RobotController.swift b/iOS/playground/RobotController.swift new file mode 100644 index 0000000..a488664 --- /dev/null +++ b/iOS/playground/RobotController.swift @@ -0,0 +1,157 @@ +// +// RobotController.swift +// playground +// +// Created by Jelle Alten on 09-11-16. +// Copyright © 2016 Wonder Workshop. All rights reserved. +// + +import UIKit + + +enum RobotType : Int { + case unknown = 1000 + case dash = 1001 + case dot = 1002 +} + + +enum ScanPeriod { + case immediate + case pause(Float) +} + + +class RobotManager { + private var originalManager : WWRobotManager { + return WWRobotManager.shared() + } + + static let sharedInstance = RobotManager() + var allConnectedRobots : [Robot] { + return robotList(originalManager.allConnectedRobots) + } + + private func robotList(_ array : [Any]) -> [Robot] { + guard let robotArray = array as? [WWRobot] else { + return [Robot]() + } + return robotArray.map { (object) -> Robot in + return Robot(robotInterface: object) + } + } + + private init() {} + + func startScanningForRobots(scanPeriod : ScanPeriod) { + switch scanPeriod { + case .immediate: + originalManager.startScanning(forRobots: 0) + case .pause(let duration): + originalManager.startScanning(forRobots: duration) + } + } + + +} +class RobotEvent{ + init(event:WWEvent) { + // TODO: event conversion + } +} +class SensorSet { +} +class CommandSetSequence { +} +class CommandSet { + private let originalCommandSet : WWCommandSet + var wwCommandSet : WWCommandSet { + return self.originalCommandSet + } + init() { + originalCommandSet = WWCommandSet() + } +} + +protocol RobotObserver { + + func robot(_ robot: Robot, eventsTriggered events: [RobotEvent]) + func robot(_ robot: Robot, didReceiveRobotState state:SensorSet) + func robot(_ robot: Robot, didStopExecutingCommandSequence sequence:CommandSetSequence, + withResults results:[String:String]) + func robot(_ robot: Robot, didFinishCommandSequence sequence:CommandSetSequence) + +} + +class Robot : NSObject { + private let robotInterface : WWRobot + var observers = [RobotObserver]() + var robotType: RobotType { + let type = robotInterface.robotType.rawValue as Int + guard let robotType = RobotType(rawValue: type) else { + return RobotType.unknown + } + return robotType + } + var name : String { + return robotInterface.name as String + } + var uuid : NSUUID { + guard let uuid = NSUUID(uuidString: robotInterface.uuId) else { + return NSUUID() + } + return uuid + } + private override init() { + robotInterface = WWRobot() + } + + init(robotInterface : WWRobot) { + self.robotInterface = robotInterface + } + + func reset() { + robotInterface.resetState() + } + + func add(observer:RobotObserver) { + robotInterface.add(self) + observers.append(observer) + } + + func send(command cmd:CommandSet) { + robotInterface.send(cmd.wwCommandSet) + } +} + +extension Robot: WWRobotObserver { + func robot(_ robot: WWRobot!, eventsTriggered events: [Any]!) { + // TODO +// let robotEvents = eventsList(events: events) +// for observer in observers { +// observer.robot(robot:self, eventsTriggered:events) +// } + } + + func robot(_ robot: WWRobot!, didReceiveRobotState state: WWSensorSet!) { + // TODO + } + func robot(_ robot: WWRobot!, didFinishCommand sequence: WWCommandSetSequence!) { + // TODO + } + func robot(_ robot: WWRobot!, didStopExecutingCommand sequence: WWCommandSetSequence!, withResults results: [AnyHashable : Any]!) { + // TODO + } + + private func eventsList(events: [Any]) -> [RobotEvent] { + guard let eventArray = events as? [WWEvent] else { + return [RobotEvent]() + } + return eventArray.map({ (event) -> RobotEvent in + return RobotEvent(event: event) + }) + + } +} + + diff --git a/iOS/playground/RobotViewController.swift b/iOS/playground/RobotViewController.swift new file mode 100644 index 0000000..7b47fb7 --- /dev/null +++ b/iOS/playground/RobotViewController.swift @@ -0,0 +1,51 @@ +// +// RobotViewController.swift +// playground +// +// Created by Jelle Alten on 09-11-16. +// Copyright © 2016 Wonder Workshop. All rights reserved. +// + +import UIKit + +class RobotViewController: UIViewController { + var connectedRobots = [Robot]() + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + refreshConnectedRobots() + } + + func refreshConnectedRobots() { + connectedRobots = RobotManager.sharedInstance.allConnectedRobots + for robot in connectedRobots { + robot.add(observer:self) + } + } + + func sendCommandSetToRobots(cmd: CommandSet) { + for robot in connectedRobots { + robot.send(command: cmd) + } + } + + +} + +extension RobotViewController : RobotObserver { + func robot(_ robot: Robot, eventsTriggered events:[RobotEvent]) { + + } + func robot(_ robot: Robot, didReceiveRobotState state:SensorSet) { + + } + func robot(_ robot: Robot, didStopExecutingCommandSequence sequence:CommandSetSequence, + withResults results:[String:String]) { + + } + func robot(_ robot: Robot, didFinishCommandSequence sequence:CommandSetSequence) { + + } +} + + diff --git a/iOS/playground/playground-Bridging-Header.h b/iOS/playground/playground-Bridging-Header.h new file mode 100644 index 0000000..d78335a --- /dev/null +++ b/iOS/playground/playground-Bridging-Header.h @@ -0,0 +1,14 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + +#import +#import + +#import "RobotControlPanelViewController.h" +#import "RobotControlViewController.h" +#import "RobotListTableViewCell.h" + +@interface WWRobotManager (shared) ++ (WWRobotManager*) sharedManager; +@end From ff61484de67e2601961b13d52935b16bd006b9a4 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 00:40:21 +0100 Subject: [PATCH 02/18] start replacing objc list VC with swift --- iOS/playground.xcodeproj/project.pbxproj | 38 ++- iOS/playground/RobotListTableViewCell.m | 4 - iOS/playground/RobotListViewController.h | 19 -- iOS/playground/RobotListViewController.m | 182 -------------- iOS/playground/RobotListViewController.swift | 246 +++++++++++++++++++ 5 files changed, 277 insertions(+), 212 deletions(-) delete mode 100644 iOS/playground/RobotListViewController.h delete mode 100644 iOS/playground/RobotListViewController.m create mode 100644 iOS/playground/RobotListViewController.swift diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 5efb491..6b82be0 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -9,7 +9,7 @@ /* Begin PBXBuildFile section */ 0A304C021A1A6FEF001A67B7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C011A1A6FEF001A67B7 /* main.m */; }; 0A304C051A1A6FEF001A67B7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C041A1A6FEF001A67B7 /* AppDelegate.m */; }; - 0A304C081A1A6FEF001A67B7 /* RobotListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C071A1A6FEF001A67B7 /* RobotListViewController.m */; }; + 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */; }; 0A304C0B1A1A6FEF001A67B7 /* RobotControlPanelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C0A1A1A6FEF001A67B7 /* RobotControlPanelViewController.m */; }; 0A304C0E1A1A6FEF001A67B7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C0C1A1A6FEF001A67B7 /* Main.storyboard */; }; 0A304C101A1A6FEF001A67B7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C0F1A1A6FEF001A67B7 /* Images.xcassets */; }; @@ -30,6 +30,9 @@ 0A304C541A1BEBC9001A67B7 /* ControlSensorsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C531A1BEBC9001A67B7 /* ControlSensorsViewController.m */; }; 0A304C581A1C0DA4001A67B7 /* ControlSoundViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */; }; 0A412C581A6468110045A7B0 /* wiggle.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A412C571A6468110045A7B0 /* wiggle.json */; }; + 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; + 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */; }; + 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */; }; 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D964F1A1B844000B109CC /* CircleJoystickView.m */; }; 780D96531A1B8F1800B109CC /* ControlHeadViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D96521A1B8F1800B109CC /* ControlHeadViewController.m */; }; /* End PBXBuildFile section */ @@ -50,8 +53,7 @@ 0A304C011A1A6FEF001A67B7 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 0A304C031A1A6FEF001A67B7 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 0A304C041A1A6FEF001A67B7 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; - 0A304C061A1A6FEF001A67B7 /* RobotListViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RobotListViewController.h; sourceTree = ""; }; - 0A304C071A1A6FEF001A67B7 /* RobotListViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RobotListViewController.m; sourceTree = ""; }; + 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RobotListViewController.swift; sourceTree = ""; }; 0A304C091A1A6FEF001A67B7 /* RobotControlPanelViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RobotControlPanelViewController.h; sourceTree = ""; }; 0A304C0A1A1A6FEF001A67B7 /* RobotControlPanelViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RobotControlPanelViewController.m; sourceTree = ""; }; 0A304C0D1A1A6FEF001A67B7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -84,6 +86,10 @@ 0A304C561A1C0DA4001A67B7 /* ControlSoundViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlSoundViewController.h; sourceTree = ""; }; 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ControlSoundViewController.m; sourceTree = ""; }; 0A412C571A6468110045A7B0 /* wiggle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = wiggle.json; sourceTree = ""; }; + 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "playground-Bridging-Header.h"; sourceTree = ""; }; + 6007A9021DD3BF24005DF517 /* RobotController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotController.swift; sourceTree = ""; }; + 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bridging-Helper.m"; sourceTree = ""; }; + 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotViewController.swift; sourceTree = ""; }; 780D964E1A1B844000B109CC /* CircleJoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleJoystickView.h; sourceTree = ""; }; 780D964F1A1B844000B109CC /* CircleJoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CircleJoystickView.m; sourceTree = ""; }; 780D96511A1B8F1800B109CC /* ControlHeadViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlHeadViewController.h; sourceTree = ""; }; @@ -144,6 +150,10 @@ 0A304C0F1A1A6FEF001A67B7 /* Images.xcassets */, 0A304C111A1A6FEF001A67B7 /* LaunchScreen.xib */, 0A304BFF1A1A6FEF001A67B7 /* Supporting Files */, + 6007A9021DD3BF24005DF517 /* RobotController.swift */, + 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */, + 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */, + 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */, ); path = playground; sourceTree = ""; @@ -196,8 +206,7 @@ 0A304C3F1A1AB963001A67B7 /* ControlLightsViewController.m */, 0A304C411A1ABD6D001A67B7 /* RobotControlViewController.h */, 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */, - 0A304C061A1A6FEF001A67B7 /* RobotListViewController.h */, - 0A304C071A1A6FEF001A67B7 /* RobotListViewController.m */, + 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */, 0A304C461A1AD28A001A67B7 /* ControlEyeRingViewController.h */, 0A304C471A1AD28A001A67B7 /* ControlEyeRingViewController.m */, 0A304C091A1A6FEF001A67B7 /* RobotControlPanelViewController.h */, @@ -283,7 +292,8 @@ TargetAttributes = { 0A304BFB1A1A6FEF001A67B7 = { CreatedOnToolsVersion = 6.1; - DevelopmentTeam = 6AS2VXQ4XQ; + DevelopmentTeam = XP2D8KRSZE; + LastSwiftMigration = 0810; }; 0A304C171A1A6FF0001A67B7 = { CreatedOnToolsVersion = 6.1; @@ -338,13 +348,15 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */, 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */, 0A304C3C1A1A8619001A67B7 /* RobotListTableViewCell.m in Sources */, + 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */, 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */, 0A304C051A1A6FEF001A67B7 /* AppDelegate.m in Sources */, 0A304C581A1C0DA4001A67B7 /* ControlSoundViewController.m in Sources */, 780D96531A1B8F1800B109CC /* ControlHeadViewController.m in Sources */, - 0A304C081A1A6FEF001A67B7 /* RobotListViewController.m in Sources */, + 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */, 0A304C481A1AD28A001A67B7 /* ControlEyeRingViewController.m in Sources */, 0A304C541A1BEBC9001A67B7 /* ControlSensorsViewController.m in Sources */, 0A304C021A1A6FEF001A67B7 /* main.m in Sources */, @@ -352,6 +364,7 @@ 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */, 0A304C0B1A1A6FEF001A67B7 /* RobotControlPanelViewController.m in Sources */, 0A304C401A1AB963001A67B7 /* ControlLightsViewController.m in Sources */, + 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -475,8 +488,10 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + DEVELOPMENT_TEAM = XP2D8KRSZE; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -488,6 +503,9 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + SWIFT_OBJC_BRIDGING_HEADER = "playground/playground-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -496,8 +514,10 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + DEVELOPMENT_TEAM = XP2D8KRSZE; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -509,6 +529,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + SWIFT_OBJC_BRIDGING_HEADER = "playground/playground-Bridging-Header.h"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; @@ -516,6 +538,7 @@ 0A304C261A1A6FF0001A67B7 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; FRAMEWORK_SEARCH_PATHS = ( "$(SDKROOT)/Developer/Library/Frameworks", @@ -535,6 +558,7 @@ 0A304C271A1A6FF0001A67B7 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; FRAMEWORK_SEARCH_PATHS = ( "$(SDKROOT)/Developer/Library/Frameworks", diff --git a/iOS/playground/RobotListTableViewCell.m b/iOS/playground/RobotListTableViewCell.m index 5e0e2ed..f6b0a95 100644 --- a/iOS/playground/RobotListTableViewCell.m +++ b/iOS/playground/RobotListTableViewCell.m @@ -10,10 +10,6 @@ @implementation RobotListTableViewCell -- (void)awakeFromNib { - // Initialization code -} - - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; diff --git a/iOS/playground/RobotListViewController.h b/iOS/playground/RobotListViewController.h deleted file mode 100644 index 924c99f..0000000 --- a/iOS/playground/RobotListViewController.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// RobotListViewController.h -// playground -// -// Created by Kevin Liang on 11/17/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import - -@class RobotControlPanelViewController; - -@interface RobotListViewController : UITableViewController - -@property (strong, nonatomic) RobotControlPanelViewController *controlPanelViewController; -@property (nonatomic, strong) WWRobotManager *manager; - -@end - diff --git a/iOS/playground/RobotListViewController.m b/iOS/playground/RobotListViewController.m deleted file mode 100644 index b62465a..0000000 --- a/iOS/playground/RobotListViewController.m +++ /dev/null @@ -1,182 +0,0 @@ -// -// RobotListViewController.m -// playground -// -// Created by Kevin Liang on 11/17/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "RobotListViewController.h" -#import "RobotControlPanelViewController.h" -#import "RobotControlViewController.h" -#import "RobotListTableViewCell.h" - -@interface RobotListViewController () - -@property NSMutableArray *robots; - -@end - - -@implementation RobotListViewController - -- (void)awakeFromNib { - [super awakeFromNib]; - if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { - self.clearsSelectionOnViewWillAppear = NO; - self.preferredContentSize = CGSizeMake(320.0, 600.0); - } -} - -- (void)viewDidLoad { - [super viewDidLoad]; - - self.controlPanelViewController = (RobotControlPanelViewController *)[[self.splitViewController.viewControllers lastObject] topViewController]; - self.robots = [NSMutableArray new]; - - // load custom nib - [self.tableView registerNib:[UINib nibWithNibName:@"RobotListTableViewCell" bundle:nil] forCellReuseIdentifier:@"RobotListTableViewCell"]; - - // setup robot manager - self.manager = [WWRobotManager manager]; - NSAssert(self.manager, @"unable to instantiate robot manager"); - [self.manager addManagerObserver:self]; - [self.manager startScanningForRobots:2.0f]; - - self.tableView.rowHeight = 130; - UIColor *start = [UIColor colorWithRed:58/255.0 green:108/255.0 blue:183/255.0 alpha:0.15]; - UIColor *stop = [UIColor colorWithRed:58/255.0 green:108/255.0 blue:183/255.0 alpha:0.45]; - - CAGradientLayer *gradient = [CAGradientLayer layer]; - gradient.frame = [self.view bounds]; - gradient.colors = [NSArray arrayWithObjects:(id)start.CGColor, (id)stop.CGColor, nil]; - [self.tableView.layer insertSublayer:gradient atIndex:0]; -} - -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -- (void)insertNewObject:(id)sender { -// if (!self.objects) { -// self.objects = [[NSMutableArray alloc] init]; -// } -// [self.objects insertObject:[NSDate date] atIndex:0]; -// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; -// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; -} - -#pragma mark - Segues - -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - if ([[segue identifier] isEqualToString:@"showDetail"]) { - NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; - RobotControlPanelViewController *controller = (RobotControlPanelViewController *)[[segue destinationViewController] topViewController]; - controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; - controller.navigationItem.leftItemsSupplementBackButton = YES; - } -} - -#pragma mark - Table View - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - return 1; -} - -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return self.robots.count; -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - RobotListTableViewCell *cell = (RobotListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RobotListTableViewCell" forIndexPath:indexPath]; - - // status - WWRobot *robot = (WWRobot *)self.robots[indexPath.row]; - if (robot.isConnected) { - cell.contentView.backgroundColor = [UIColor colorWithRed:50/255.0 green:200/255.0 blue:50/255.0 alpha:0.6]; - } - else { - cell.contentView.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:0.6]; - } - - // robot info - NSMutableString *detail = [NSMutableString stringWithCapacity:200]; - [detail appendFormat:@"uuId: %@\n", robot.uuId]; - [detail appendFormat:@"Firmware %@\n", robot.firmwareVersion]; - [detail appendFormat:@"Serial: %@\n", robot.serialNumber]; - [detail appendFormat:@"RSSI %d dB\n", robot.signalStrength.intValue]; - [detail appendFormat:@"Personality color: %d\n", robot.personalityColorIndex]; - cell.infoLabel.text = detail; - - // robot name - cell.nameLabel.text = robot.name; - - // image - switch (robot.robotType) { - case WW_ROBOT_DOT: - cell.robotImageView.image = [UIImage imageNamed:@"dot.png"]; - break; - case WW_ROBOT_DASH: - cell.robotImageView.image = [UIImage imageNamed:@"dash.png"]; - - default: - break; - } - - return cell; -} - -- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - WWRobot *robot = self.robots[indexPath.row]; - if (robot.isConnected) { - [self.manager disconnectFromRobot:robot]; - } - else { - [self.manager connectToRobot:robot]; - } -} - -- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { - // Return NO if you do not want the specified item to be editable. - return NO; -} - -#pragma mark - RobotManagerDelegate - -- (void) manager:(WWRobotManager *)manager didDiscoverRobot:(WWRobot *)robot { - if (![self.robots containsObject:robot]) { - // found new robots, refresh list - [self.robots addObject:robot]; - [self.tableView reloadData]; - } -} - -- (void) manager:(WWRobotManager *)manager didUpdateDiscoveredRobots:(WWRobot *)robot { - // existing robots have new data, refresh - [self.tableView reloadData]; -} - -- (void) manager:(WWRobotManager *)manager didLoseRobot:(WWRobot *)robot { - // lost connectivity with existing robot, refresh list - [self.robots removeObject:robot]; - [self.tableView reloadData]; -} - -- (void) manager:(WWRobotManager *)manager didConnectRobot:(WWRobot *)robot { - // connected with robot, refresh - [self.tableView reloadData]; - [self.controlPanelViewController.activeControlVC refreshConnectedRobots]; -} - -- (void) manager:(WWRobotManager *)manager didFailToConnectRobot:(WWRobot *)robot error:(WWError *)error { - NSLog(@"failed to connect to robot: %@, with error: %@", robot.name, error); - [NSNumber numberWithUnsignedInteger:WW_SENSOR_BUTTON_MAIN]; -} - -- (void) manager:(WWRobotManager *)manager didDisconnectRobot:(WWRobot *)robot { - // disconnected with robot, refresh - [self.tableView reloadData]; -} - -@end diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift new file mode 100644 index 0000000..805c6ac --- /dev/null +++ b/iOS/playground/RobotListViewController.swift @@ -0,0 +1,246 @@ +// +// RobotListViewController.m +// playground +// +// Created by Kevin Liang on 11/17/14. +// Copyright (c) 2014 Wonder Workshop. All rights reserved. +// + +//#import "RobotListViewController.h" +//#import "RobotControlPanelViewController.h" +//#import "RobotControlViewController.h" +//#import "RobotListTableViewCell.h" +// +//@interface RobotListViewController () +// +//@property NSMutableArray *robots; +// +//@end +// +import UIKit + +class RobotListViewController : UITableViewController { + + var controlPanelViewController : RobotControlPanelViewController? + var manager : WWRobotManager? + var robots = [WWRobot]() + + override func awakeFromNib() { + super.awakeFromNib() + if UIDevice.current.userInterfaceIdiom == .pad { + clearsSelectionOnViewWillAppear = false + preferredContentSize = CGSize(width:320.0, height:600.0) + } + } + override func viewDidLoad() { + super.viewDidLoad() + if let panelViewController = splitViewController?.viewControllers.last as? RobotControlPanelViewController { + controlPanelViewController = panelViewController + } + + let nib = UINib(nibName: "RobotListTableViewCell", bundle: nil) + tableView.register(nib, forCellReuseIdentifier: "RobotListTableViewCell") + + // setup robot manager + let manager : WWRobotManager = WWRobotManager.shared(); +// NSAssert(self.manager, @"unable to instantiate robot manager"); + manager.add(self) + + self.manager = manager + manager.startScanning(forRobots:2.0) + + + self.tableView.rowHeight = 130 + let start = UIColor(red:58/255.0, green:108/255.0, blue:183/255.0, alpha:0.15) + let stop = UIColor(red: 58/255.0, green:108/255.0, blue:183/255.0, alpha:0.45) + + let gradient = CAGradientLayer() + gradient.frame = self.view.bounds + gradient.colors = [start.cgColor,stop.cgColor] + tableView.layer.insertSublayer(gradient, at:0) + } + +// func insertNewObject() { +// +// } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if segue.identifier == "showDetail" { +// let indexPath = tableView.indexPathForSelectedRow + if let controller = segue.destination as? RobotControlPanelViewController, + let splitViewController = splitViewController { + if #available(iOS 8.0, *) { + controller.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem + } else { + // Fallback on earlier versions + } + controller.navigationItem.leftItemsSupplementBackButton = true + } + } + } + + + + + +// #pragma mark - Table View + + override func numberOfSections(in tableView: UITableView) -> Int { + return 1 + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return robots.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + + guard let cell = tableView.dequeueReusableCell(withIdentifier: "RobotListTableViewCell", for: indexPath) as? RobotListTableViewCell else { + return UITableViewCell() + } + + let robot = robots[indexPath.row] + if robot.isConnected() { + cell.contentView.backgroundColor = UIColor(red:50/255.0, green:200/255.0, blue:50/255.0, alpha:0.6) + } + else { + cell.contentView.backgroundColor = UIColor(red:250/255.0, green:250/255.0, blue:250/255.0, alpha:0.6) + } + + // robot info + var detail = String() + detail = detail.appendingFormat("uuId: %@\n", robot.uuId ) + detail = detail.appendingFormat("Firmware %@\n", robot.firmwareVersion) + detail = detail.appendingFormat("Serial: %@\n", robot.serialNumber) + detail = detail.appendingFormat("RSSI %d dB\n", robot.signalStrength.intValue) + detail = detail.appendingFormat("Personality color: %d\n", robot.personalityColorIndex) + cell.infoLabel.text = detail; + + // robot name + cell.nameLabel.text = robot.name; + + // image + switch (robot.robotType) { + case .ROBOT_DOT: + cell.robotImageView.image = UIImage(named:"dot.png") + break; + case .ROBOT_DASH: + cell.robotImageView.image = UIImage(named:"dash.png") + + default: + break; + } + + return cell; + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let robot = robots[indexPath.row] + if robot.isConnected() { + manager?.disconnect(from: robot) + } + else { + manager?.connect(to: robot) + } + } + + override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { + return false + } + +} + + +extension RobotListViewController : WWRobotManagerObserver { + func manager(_ manager: WWRobotManager!, didDiscover robot: WWRobot!) { + if !robots.contains(robot) { + robots.append(robot) + tableView.reloadData() + } + } + + + //- (void) manager:(WWRobotManager *)manager didDiscoverRobot:(WWRobot *)robot { + // if (![self.robots containsObject:robot]) { + // // found new robots, refresh list + // [self.robots addObject:robot]; + // [self.tableView reloadData]; + // } + //} + // + + + func manager(_ manager: WWRobotManager!, didUpdateDiscoveredRobots robot: WWRobot!) { + tableView.reloadData() + } + //- (void) manager:(WWRobotManager *)manager didUpdateDiscoveredRobots:(WWRobot *)robot { + // // existing robots have new data, refresh + // [self.tableView reloadData]; + //} + // + + func manager(_ manager: WWRobotManager!, didLose robot: WWRobot!) { + if let index = robots.index(of: robot) { + robots.remove(at: index) + } + tableView.reloadData() + } + //- (void) manager:(WWRobotManager *)manager didLoseRobot:(WWRobot *)robot { + // // lost connectivity with existing robot, refresh list + // [self.robots removeObject:robot]; + // [self.tableView reloadData]; + //} + // + + func manager(_ manager: WWRobotManager!, didConnect robot: WWRobot!) { + tableView.reloadData() + controlPanelViewController?.activeControlVC.refreshConnectedRobots() + } + //- (void) manager:(WWRobotManager *)manager didConnectRobot:(WWRobot *)robot { + // // connected with robot, refresh + // [self.tableView reloadData]; + // [self.controlPanelViewController.activeControlVC refreshConnectedRobots]; + //} + // + func manager(_ manager: WWRobotManager!, didFailToConnect robot: WWRobot!, error: WWError!) { + + } + //- (void) manager:(WWRobotManager *)manager didFailToConnectRobot:(WWRobot *)robot error:(WWError *)error { + // NSLog(@"failed to connect to robot: %@, with error: %@", robot.name, error); + // [NSNumber numberWithUnsignedInteger:WW_SENSOR_BUTTON_MAIN]; + //} + // + + func manager(_ manager: WWRobotManager!, didDisconnectRobot robot: WWRobot!) { + tableView.reloadData() + } + //- (void) manager:(WWRobotManager *)manager didDisconnectRobot:(WWRobot *)robot { + // // disconnected with robot, refresh + // [self.tableView reloadData]; + //} + // + +} + +// +//- (void)insertNewObject:(id)sender { +//// if (!self.objects) { +//// self.objects = [[NSMutableArray alloc] init]; +//// } +//// [self.objects insertObject:[NSDate date] atIndex:0]; +//// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; +//// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; +//} +// +//#pragma mark - Segues +// +//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { +// if ([[segue identifier] isEqualToString:@"showDetail"]) { +// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; +// RobotControlPanelViewController *controller = (RobotControlPanelViewController *)[[segue destinationViewController] topViewController]; +// controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; +// controller.navigationItem.leftItemsSupplementBackButton = YES; +// } +//} +////#pragma mark - RobotManagerDelegate +////@end From f7b7d8472ddc3d019719caa303c8824c4867b451 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 09:46:45 +0100 Subject: [PATCH 03/18] cleaning --- iOS/playground.xcodeproj/project.pbxproj | 32 +++++ iOS/playground/Base.lproj/Main.storyboard | 135 ++++++++++--------- iOS/playground/RobotListViewController.swift | 87 ++---------- 3 files changed, 111 insertions(+), 143 deletions(-) diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 6b82be0..5964e72 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -33,6 +33,8 @@ 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */; }; 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */; }; + 60E314E51DD4628700EAF47A /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 60E314E01DD4628700EAF47A /* LaunchScreen.xib */; }; + 60E314E61DD4628700EAF47A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 60E314E21DD4628700EAF47A /* Main.storyboard */; }; 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D964F1A1B844000B109CC /* CircleJoystickView.m */; }; 780D96531A1B8F1800B109CC /* ControlHeadViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D96521A1B8F1800B109CC /* ControlHeadViewController.m */; }; /* End PBXBuildFile section */ @@ -90,6 +92,8 @@ 6007A9021DD3BF24005DF517 /* RobotController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotController.swift; sourceTree = ""; }; 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bridging-Helper.m"; sourceTree = ""; }; 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotViewController.swift; sourceTree = ""; }; + 60E314E11DD4628700EAF47A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = LaunchScreen.xib; sourceTree = ""; }; + 60E314E31DD4628700EAF47A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Main.storyboard; sourceTree = ""; }; 780D964E1A1B844000B109CC /* CircleJoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleJoystickView.h; sourceTree = ""; }; 780D964F1A1B844000B109CC /* CircleJoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CircleJoystickView.m; sourceTree = ""; }; 780D96511A1B8F1800B109CC /* ControlHeadViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlHeadViewController.h; sourceTree = ""; }; @@ -202,6 +206,7 @@ 0A304C361A1A85D2001A67B7 /* controllers */ = { isa = PBXGroup; children = ( + 60E314DF1DD4628700EAF47A /* Base.lproj */, 0A304C3E1A1AB963001A67B7 /* ControlLightsViewController.h */, 0A304C3F1A1AB963001A67B7 /* ControlLightsViewController.m */, 0A304C411A1ABD6D001A67B7 /* RobotControlViewController.h */, @@ -243,6 +248,15 @@ name = animations; sourceTree = ""; }; + 60E314DF1DD4628700EAF47A /* Base.lproj */ = { + isa = PBXGroup; + children = ( + 60E314E01DD4628700EAF47A /* LaunchScreen.xib */, + 60E314E21DD4628700EAF47A /* Main.storyboard */, + ); + path = Base.lproj; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -326,10 +340,12 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 60E314E61DD4628700EAF47A /* Main.storyboard in Resources */, 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */, 0A412C581A6468110045A7B0 /* wiggle.json in Resources */, 0A304C0E1A1A6FEF001A67B7 /* Main.storyboard in Resources */, 0A304C131A1A6FEF001A67B7 /* LaunchScreen.xib in Resources */, + 60E314E51DD4628700EAF47A /* LaunchScreen.xib in Resources */, 0A304C101A1A6FEF001A67B7 /* Images.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -403,6 +419,22 @@ name = LaunchScreen.xib; sourceTree = ""; }; + 60E314E01DD4628700EAF47A /* LaunchScreen.xib */ = { + isa = PBXVariantGroup; + children = ( + 60E314E11DD4628700EAF47A /* Base */, + ); + name = LaunchScreen.xib; + sourceTree = ""; + }; + 60E314E21DD4628700EAF47A /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 60E314E31DD4628700EAF47A /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ diff --git a/iOS/playground/Base.lproj/Main.storyboard b/iOS/playground/Base.lproj/Main.storyboard index 59d0a72..b036501 100644 --- a/iOS/playground/Base.lproj/Main.storyboard +++ b/iOS/playground/Base.lproj/Main.storyboard @@ -1,8 +1,12 @@ - + + + + - + + @@ -10,6 +14,7 @@ + @@ -25,11 +30,11 @@ - + - + @@ -44,11 +49,11 @@ - + - + @@ -59,7 +64,7 @@ - + @@ -78,29 +83,29 @@ - + - + - + - + - + - + @@ -123,6 +128,7 @@ + @@ -138,7 +144,7 @@ - + @@ -215,42 +221,42 @@ - + - + - @@ -367,7 +371,7 @@ - + @@ -465,7 +469,7 @@ - + @@ -475,16 +479,15 @@ - + - + - @@ -511,14 +514,14 @@ - + - + - + + + + + - + diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift index 805c6ac..f30864c 100644 --- a/iOS/playground/RobotListViewController.swift +++ b/iOS/playground/RobotListViewController.swift @@ -6,17 +6,7 @@ // Copyright (c) 2014 Wonder Workshop. All rights reserved. // -//#import "RobotListViewController.h" -//#import "RobotControlPanelViewController.h" -//#import "RobotControlViewController.h" -//#import "RobotListTableViewCell.h" -// -//@interface RobotListViewController () -// -//@property NSMutableArray *robots; -// -//@end -// + import UIKit class RobotListViewController : UITableViewController { @@ -78,13 +68,11 @@ class RobotListViewController : UITableViewController { } } } - - - - - -// #pragma mark - Table View - +} + +// MARK: Table View methods +extension RobotListViewController { + override func numberOfSections(in tableView: UITableView) -> Int { return 1 } @@ -159,25 +147,10 @@ extension RobotListViewController : WWRobotManagerObserver { } } - - //- (void) manager:(WWRobotManager *)manager didDiscoverRobot:(WWRobot *)robot { - // if (![self.robots containsObject:robot]) { - // // found new robots, refresh list - // [self.robots addObject:robot]; - // [self.tableView reloadData]; - // } - //} - // - func manager(_ manager: WWRobotManager!, didUpdateDiscoveredRobots robot: WWRobot!) { tableView.reloadData() } - //- (void) manager:(WWRobotManager *)manager didUpdateDiscoveredRobots:(WWRobot *)robot { - // // existing robots have new data, refresh - // [self.tableView reloadData]; - //} - // func manager(_ manager: WWRobotManager!, didLose robot: WWRobot!) { if let index = robots.index(of: robot) { @@ -185,62 +158,20 @@ extension RobotListViewController : WWRobotManagerObserver { } tableView.reloadData() } - //- (void) manager:(WWRobotManager *)manager didLoseRobot:(WWRobot *)robot { - // // lost connectivity with existing robot, refresh list - // [self.robots removeObject:robot]; - // [self.tableView reloadData]; - //} - // func manager(_ manager: WWRobotManager!, didConnect robot: WWRobot!) { tableView.reloadData() controlPanelViewController?.activeControlVC.refreshConnectedRobots() } - //- (void) manager:(WWRobotManager *)manager didConnectRobot:(WWRobot *)robot { - // // connected with robot, refresh - // [self.tableView reloadData]; - // [self.controlPanelViewController.activeControlVC refreshConnectedRobots]; - //} - // + func manager(_ manager: WWRobotManager!, didFailToConnect robot: WWRobot!, error: WWError!) { + print("failed to connect to robot: \(robot.name), with error: \(error)"); + print() } - //- (void) manager:(WWRobotManager *)manager didFailToConnectRobot:(WWRobot *)robot error:(WWError *)error { - // NSLog(@"failed to connect to robot: %@, with error: %@", robot.name, error); - // [NSNumber numberWithUnsignedInteger:WW_SENSOR_BUTTON_MAIN]; - //} - // func manager(_ manager: WWRobotManager!, didDisconnectRobot robot: WWRobot!) { tableView.reloadData() } - //- (void) manager:(WWRobotManager *)manager didDisconnectRobot:(WWRobot *)robot { - // // disconnected with robot, refresh - // [self.tableView reloadData]; - //} - // } - -// -//- (void)insertNewObject:(id)sender { -//// if (!self.objects) { -//// self.objects = [[NSMutableArray alloc] init]; -//// } -//// [self.objects insertObject:[NSDate date] atIndex:0]; -//// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; -//// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; -//} -// -//#pragma mark - Segues -// -//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { -// if ([[segue identifier] isEqualToString:@"showDetail"]) { -// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; -// RobotControlPanelViewController *controller = (RobotControlPanelViewController *)[[segue destinationViewController] topViewController]; -// controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; -// controller.navigationItem.leftItemsSupplementBackButton = YES; -// } -//} -////#pragma mark - RobotManagerDelegate -////@end From 18eb451a2c022e184f1b5f41ee4683408ce5b590 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 11:20:42 +0100 Subject: [PATCH 04/18] converted lights vc to swift --- iOS/playground.xcodeproj/project.pbxproj | 46 ++----- iOS/playground/Base.lproj/Main.storyboard | 50 ++++---- iOS/playground/Bridging-Helper.m | 5 +- iOS/playground/ClassExtensions.swift | 16 +++ iOS/playground/ControlLightsViewController.h | 34 ------ iOS/playground/ControlLightsViewController.m | 100 ---------------- .../ControlLightsViewController.swift | 113 ++++++++++++++++++ .../RobotControlPanelViewController.m | 14 +-- iOS/playground/RobotListViewController.swift | 4 +- 9 files changed, 173 insertions(+), 209 deletions(-) create mode 100644 iOS/playground/ClassExtensions.swift delete mode 100644 iOS/playground/ControlLightsViewController.h delete mode 100644 iOS/playground/ControlLightsViewController.m create mode 100644 iOS/playground/ControlLightsViewController.swift diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 5964e72..250cc24 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -22,7 +22,6 @@ 0A304C341A1A833B001A67B7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0A304C331A1A833B001A67B7 /* Foundation.framework */; }; 0A304C3C1A1A8619001A67B7 /* RobotListTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C3A1A1A8619001A67B7 /* RobotListTableViewCell.m */; }; 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */; }; - 0A304C401A1AB963001A67B7 /* ControlLightsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C3F1A1AB963001A67B7 /* ControlLightsViewController.m */; }; 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */; }; 0A304C481A1AD28A001A67B7 /* ControlEyeRingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C471A1AD28A001A67B7 /* ControlEyeRingViewController.m */; }; 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */; }; @@ -33,8 +32,8 @@ 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */; }; 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */; }; - 60E314E51DD4628700EAF47A /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 60E314E01DD4628700EAF47A /* LaunchScreen.xib */; }; - 60E314E61DD4628700EAF47A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 60E314E21DD4628700EAF47A /* Main.storyboard */; }; + 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 605653781DD47ED900354A88 /* ClassExtensions.swift */; }; + 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */; }; 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D964F1A1B844000B109CC /* CircleJoystickView.m */; }; 780D96531A1B8F1800B109CC /* ControlHeadViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D96521A1B8F1800B109CC /* ControlHeadViewController.m */; }; /* End PBXBuildFile section */ @@ -73,8 +72,6 @@ 0A304C391A1A8619001A67B7 /* RobotListTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RobotListTableViewCell.h; sourceTree = ""; }; 0A304C3A1A1A8619001A67B7 /* RobotListTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RobotListTableViewCell.m; sourceTree = ""; }; 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RobotListTableViewCell.xib; sourceTree = ""; }; - 0A304C3E1A1AB963001A67B7 /* ControlLightsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlLightsViewController.h; sourceTree = ""; }; - 0A304C3F1A1AB963001A67B7 /* ControlLightsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ControlLightsViewController.m; sourceTree = ""; }; 0A304C411A1ABD6D001A67B7 /* RobotControlViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RobotControlViewController.h; sourceTree = ""; }; 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RobotControlViewController.m; sourceTree = ""; }; 0A304C461A1AD28A001A67B7 /* ControlEyeRingViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlEyeRingViewController.h; sourceTree = ""; }; @@ -92,8 +89,8 @@ 6007A9021DD3BF24005DF517 /* RobotController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotController.swift; sourceTree = ""; }; 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bridging-Helper.m"; sourceTree = ""; }; 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotViewController.swift; sourceTree = ""; }; - 60E314E11DD4628700EAF47A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = LaunchScreen.xib; sourceTree = ""; }; - 60E314E31DD4628700EAF47A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Main.storyboard; sourceTree = ""; }; + 605653781DD47ED900354A88 /* ClassExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClassExtensions.swift; sourceTree = ""; }; + 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlLightsViewController.swift; sourceTree = ""; }; 780D964E1A1B844000B109CC /* CircleJoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleJoystickView.h; sourceTree = ""; }; 780D964F1A1B844000B109CC /* CircleJoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CircleJoystickView.m; sourceTree = ""; }; 780D96511A1B8F1800B109CC /* ControlHeadViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlHeadViewController.h; sourceTree = ""; }; @@ -158,6 +155,7 @@ 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */, 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */, 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */, + 605653781DD47ED900354A88 /* ClassExtensions.swift */, ); path = playground; sourceTree = ""; @@ -206,9 +204,7 @@ 0A304C361A1A85D2001A67B7 /* controllers */ = { isa = PBXGroup; children = ( - 60E314DF1DD4628700EAF47A /* Base.lproj */, - 0A304C3E1A1AB963001A67B7 /* ControlLightsViewController.h */, - 0A304C3F1A1AB963001A67B7 /* ControlLightsViewController.m */, + 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */, 0A304C411A1ABD6D001A67B7 /* RobotControlViewController.h */, 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */, 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */, @@ -248,15 +244,6 @@ name = animations; sourceTree = ""; }; - 60E314DF1DD4628700EAF47A /* Base.lproj */ = { - isa = PBXGroup; - children = ( - 60E314E01DD4628700EAF47A /* LaunchScreen.xib */, - 60E314E21DD4628700EAF47A /* Main.storyboard */, - ); - path = Base.lproj; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -340,12 +327,10 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 60E314E61DD4628700EAF47A /* Main.storyboard in Resources */, 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */, 0A412C581A6468110045A7B0 /* wiggle.json in Resources */, 0A304C0E1A1A6FEF001A67B7 /* Main.storyboard in Resources */, 0A304C131A1A6FEF001A67B7 /* LaunchScreen.xib in Resources */, - 60E314E51DD4628700EAF47A /* LaunchScreen.xib in Resources */, 0A304C101A1A6FEF001A67B7 /* Images.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -368,6 +353,7 @@ 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */, 0A304C3C1A1A8619001A67B7 /* RobotListTableViewCell.m in Sources */, 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */, + 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */, 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */, 0A304C051A1A6FEF001A67B7 /* AppDelegate.m in Sources */, 0A304C581A1C0DA4001A67B7 /* ControlSoundViewController.m in Sources */, @@ -378,8 +364,8 @@ 0A304C021A1A6FEF001A67B7 /* main.m in Sources */, 0A304C4E1A1AE8FC001A67B7 /* ControlDriveViewController.m in Sources */, 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */, + 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */, 0A304C0B1A1A6FEF001A67B7 /* RobotControlPanelViewController.m in Sources */, - 0A304C401A1AB963001A67B7 /* ControlLightsViewController.m in Sources */, 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -419,22 +405,6 @@ name = LaunchScreen.xib; sourceTree = ""; }; - 60E314E01DD4628700EAF47A /* LaunchScreen.xib */ = { - isa = PBXVariantGroup; - children = ( - 60E314E11DD4628700EAF47A /* Base */, - ); - name = LaunchScreen.xib; - sourceTree = ""; - }; - 60E314E21DD4628700EAF47A /* Main.storyboard */ = { - isa = PBXVariantGroup; - children = ( - 60E314E31DD4628700EAF47A /* Base */, - ); - name = Main.storyboard; - sourceTree = ""; - }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ diff --git a/iOS/playground/Base.lproj/Main.storyboard b/iOS/playground/Base.lproj/Main.storyboard index b036501..284f961 100644 --- a/iOS/playground/Base.lproj/Main.storyboard +++ b/iOS/playground/Base.lproj/Main.storyboard @@ -93,11 +93,11 @@ - + - - - - + + + + - - + + - - - - + + + + - + @@ -580,11 +580,11 @@ - + diff --git a/iOS/playground/ControlHeadViewController.h b/iOS/playground/ControlHeadViewController.h deleted file mode 100644 index 95ff14d..0000000 --- a/iOS/playground/ControlHeadViewController.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// ControlHeadViewController.h -// playground -// -// Created by igor on 11/18/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "RobotControlViewController.h" - -@interface ControlHeadViewController : RobotControlViewController - -- (IBAction) onMiddleBtnTouch:(id)sender; -- (IBAction) onTopBtnTouch:(id)sender; -- (IBAction) onBottomBtnTouch:(id)sender; -- (IBAction) onLeftBtnTouch:(id)sender; -- (IBAction) onRightBtnTouch:(id)sender; - -@end diff --git a/iOS/playground/ControlHeadViewController.m b/iOS/playground/ControlHeadViewController.m deleted file mode 100644 index 8d4c73d..0000000 --- a/iOS/playground/ControlHeadViewController.m +++ /dev/null @@ -1,53 +0,0 @@ -// -// ControlHeadViewController.m -// playground -// -// Created by igor on 11/18/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "ControlHeadViewController.h" - - -@interface ControlHeadViewController () - -@end - -@implementation ControlHeadViewController - -- (void)viewDidLoad { - [super viewDidLoad]; -} - -- (IBAction) onMiddleBtnTouch:(id)sender { - [self updateRobotHeadWithPan:0 andTilt:0]; -} - -- (IBAction) onTopBtnTouch:(id)sender { - [self updateRobotHeadWithPan:0 andTilt:-15]; -} - -- (IBAction) onBottomBtnTouch:(id)sender { - [self updateRobotHeadWithPan:0 andTilt:10]; -} - -- (IBAction) onLeftBtnTouch:(id)sender { - [self updateRobotHeadWithPan:-90 andTilt:0]; -} - -- (IBAction) onRightBtnTouch:(id)sender { - [self updateRobotHeadWithPan:90 andTilt:0]; -} - -- (void) updateRobotHeadWithPan:(float)panDegree andTilt:(float)tiltDegree -{ - WWCommandSet *command = [WWCommandSet new]; - WWCommandHeadPosition *tilt = [[WWCommandHeadPosition alloc] initWithDegree:tiltDegree]; - WWCommandHeadPosition *pan = [[WWCommandHeadPosition alloc] initWithDegree:panDegree]; - [command setHeadPositionTilt:tilt pan:pan]; - - [self sendCommandSetToRobots:command]; -} - - -@end diff --git a/iOS/playground/ControlHeadViewController.swift b/iOS/playground/ControlHeadViewController.swift new file mode 100644 index 0000000..b413539 --- /dev/null +++ b/iOS/playground/ControlHeadViewController.swift @@ -0,0 +1,38 @@ +// +// ControlHeadViewController.swift +// playground +// +// Created by Jelle Alten on 10-11-16. +// Copyright © 2016 Wonder Workshop. All rights reserved. +// + +import UIKit + +class ControlHeadViewController: RobotControlViewController { + + @IBAction func onMiddleBtnTouch(_ sender: Any) { + updateRobotHead(pan: 0, tilt: 0) + } + @IBAction func onTopBtnTouch(_ sender: Any) { + updateRobotHead(pan: 0, tilt: -15) + } + @IBAction func onButtomBtnTouch(_ sender: Any) { + updateRobotHead(pan: 0, tilt: 10) + } + @IBAction func onLeftBtnTouch(_ sender: Any) { + updateRobotHead(pan: 90, tilt: 0) + } + @IBAction func onRightBtnTouch(_ sender: Any) { + updateRobotHead(pan: -90, tilt: 0) + } + + private func updateRobotHead(pan panDegree:Float, tilt tiltDegree: Float) { + let command = WWCommandSet() + let tilt = WWCommandHeadPosition(degree: Double(tiltDegree)) + let pan = WWCommandHeadPosition(degree: Double(panDegree)) + command.setHeadPositionTilt(tilt, pan: pan) + sendCommandSet(toRobots: command) + } + + +} diff --git a/iOS/playground/RobotControlPanelViewController.m b/iOS/playground/RobotControlPanelViewController.m index 62b0370..b5d9bac 100644 --- a/iOS/playground/RobotControlPanelViewController.m +++ b/iOS/playground/RobotControlPanelViewController.m @@ -8,7 +8,6 @@ #import "RobotControlPanelViewController.h" #import "ControlDriveViewController.h" -#import "ControlHeadViewController.h" #import "ControlSoundViewController.h" #import "ControlSensorsViewController.h" #import "Playground-Swift.h" From a77dd3792578f93f8a90a38ee168bb64a55b338b Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 14:24:47 +0100 Subject: [PATCH 07/18] minor self-cleaning --- iOS/playground/ControlDriveViewController.swift | 6 +++--- iOS/playground/ControlEyeRingViewController.swift | 4 ++-- iOS/playground/ControlLightsViewController.swift | 4 ++-- iOS/playground/RobotController.swift | 2 +- iOS/playground/RobotListViewController.swift | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/iOS/playground/ControlDriveViewController.swift b/iOS/playground/ControlDriveViewController.swift index b645522..d1d77e8 100644 --- a/iOS/playground/ControlDriveViewController.swift +++ b/iOS/playground/ControlDriveViewController.swift @@ -65,7 +65,7 @@ class ControlDriveViewController : RobotControlViewController { @IBAction func executeNod(_ sender: Any) { if !isNodding && !isWiggling { - for robot in self.connectedRobots as! [WWRobot] { + for robot in connectedRobots as! [WWRobot] { robot.executeCommand(nodAnimation, withOptions:nil) } } @@ -89,7 +89,7 @@ extension ControlDriveViewController : JoystickDelegate { let cmdToSend = WWCommandSet() // use linear angular! - let joystickPower = self.joystick.power; + let joystickPower = joystick.power; let lin = joystick.getY() * ROBOT_SPEED var ang = joystick.getX() * joystickPower * 3.14159 * 2.0 @@ -134,7 +134,7 @@ extension ControlDriveViewController { // : WWRobotObserver { extension ControlDriveViewController { @IBAction func didChangePoseDistanceValue(_ sender: UISlider) { - self.poseLabel.text = "\(Int(sender.value))"; + poseLabel.text = "\(Int(sender.value))"; } @IBAction func MoveDashByFixedDistance(_ sender: Any) { diff --git a/iOS/playground/ControlEyeRingViewController.swift b/iOS/playground/ControlEyeRingViewController.swift index 9e6ae98..e2032fd 100644 --- a/iOS/playground/ControlEyeRingViewController.swift +++ b/iOS/playground/ControlEyeRingViewController.swift @@ -64,7 +64,7 @@ class ControlEyeRingViewController : RobotControlViewController { } else { eyeShowInProgress = true - eyeShowTimer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.eyeShowTimerValues(timer:)), userInfo: nil, repeats: true) + eyeShowTimer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(eyeShowTimerValues(timer:)), userInfo: nil, repeats: true) eyeShowTimer.fire() eyeShowButton.setTitle("Stop eye show", for:.normal) } @@ -73,7 +73,7 @@ class ControlEyeRingViewController : RobotControlViewController { @objc func eyeShowTimerValues(timer: Timer) { - eyeShowValueState = (self.eyeShowValueState+1) % 12; // rotate + eyeShowValueState = (eyeShowValueState+1) % 12; // rotate eye0Switch.isOn = ((0 + eyeShowValueState) % 2) == 0 eye1Switch.isOn = ((1 + eyeShowValueState) % 2) == 0 diff --git a/iOS/playground/ControlLightsViewController.swift b/iOS/playground/ControlLightsViewController.swift index 03cdb54..12afac3 100644 --- a/iOS/playground/ControlLightsViewController.swift +++ b/iOS/playground/ControlLightsViewController.swift @@ -73,8 +73,8 @@ class ControlLightsViewController: RobotControlViewController { var r, g, b, mono : Float - self.lightShowValueState = (self.lightShowValueState+1) % 4; // rotate - switch (self.lightShowValueState) { + lightShowValueState = (lightShowValueState+1) % 4; // rotate + switch (lightShowValueState) { case 0: r = 1.0; g = 0; b = 0; mono = 1.0; break; diff --git a/iOS/playground/RobotController.swift b/iOS/playground/RobotController.swift index a488664..a7bea94 100644 --- a/iOS/playground/RobotController.swift +++ b/iOS/playground/RobotController.swift @@ -66,7 +66,7 @@ class CommandSetSequence { class CommandSet { private let originalCommandSet : WWCommandSet var wwCommandSet : WWCommandSet { - return self.originalCommandSet + return originalCommandSet } init() { originalCommandSet = WWCommandSet() diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift index ec626a7..918c8fe 100644 --- a/iOS/playground/RobotListViewController.swift +++ b/iOS/playground/RobotListViewController.swift @@ -39,12 +39,12 @@ class RobotListViewController : UITableViewController { manager.startScanning(forRobots:2.0) - self.tableView.rowHeight = 130 + tableView.rowHeight = 130 let start = UIColor(red:58/255.0, green:108/255.0, blue:183/255.0, alpha:0.15) let stop = UIColor(red: 58/255.0, green:108/255.0, blue:183/255.0, alpha:0.45) let gradient = CAGradientLayer() - gradient.frame = self.view.bounds + gradient.frame = view.bounds gradient.colors = [start.cgColor,stop.cgColor] tableView.layer.insertSublayer(gradient, at:0) } From 58dc15c54eb7749caa67207a019f23dc79454526 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 15:28:31 +0100 Subject: [PATCH 08/18] swiftly sensors pane --- .../Versions/A/Headers/WWConstants.h | 71 ++++----- iOS/playground.xcodeproj/project.pbxproj | 10 +- iOS/playground/ControlSensorsViewController.h | 15 -- iOS/playground/ControlSensorsViewController.m | 136 ---------------- .../ControlSensorsViewController.swift | 146 ++++++++++++++++++ .../RobotControlViewController.swift | 8 + 6 files changed, 194 insertions(+), 192 deletions(-) delete mode 100644 iOS/playground/ControlSensorsViewController.h delete mode 100644 iOS/playground/ControlSensorsViewController.m create mode 100644 iOS/playground/ControlSensorsViewController.swift diff --git a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h index 7d7980b..4d01d76 100644 --- a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h +++ b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h @@ -41,41 +41,42 @@ typedef unsigned int WWLauncherReloadDirection; // defines for componentIds (used for CommandSet and SensorSet) -typedef unsigned int WWComponentId; -#define WW_COMMAND_POWER 1 -#define WW_COMMAND_EYE_RING 100 -#define WW_COMMAND_LIGHT_RGB_EYE 101 -#define WW_COMMAND_LIGHT_RGB_LEFT_EAR 102 -#define WW_COMMAND_LIGHT_RGB_RIGHT_EAR 103 -#define WW_COMMAND_LIGHT_RGB_CHEST 104 -#define WW_COMMAND_LIGHT_MONO_TAIL 105 -#define WW_COMMAND_LIGHT_MONO_BUTTON_MAIN 106 -#define WW_COMMAND_HEAD_POSITION_TILT 202 -#define WW_COMMAND_HEAD_POSITION_PAN 203 -#define WW_COMMAND_BODY_LINEAR_ANGULAR 204 -#define WW_COMMAND_BODY_POSE 205 -#define WW_COMMAND_MOTOR_HEAD_BANG 210 -#define WW_COMMAND_BODY_WHEELS 211 -#define WW_COMMAND_BODY_COAST 212 -#define WW_COMMAND_SPEAKER 300 -#define WW_COMMAND_ON_ROBOT_ANIM 301 -#define WW_COMMAND_LAUNCHER_FLING 400 -#define WW_COMMAND_LAUNCHER_RELOAD 401 -#define WW_SENSOR_BUTTON_MAIN 1000 -#define WW_SENSOR_BUTTON_1 1001 -#define WW_SENSOR_BUTTON_2 1002 -#define WW_SENSOR_BUTTON_3 1003 -#define WW_SENSOR_HEAD_POSITION_PAN 2000 -#define WW_SENSOR_HEAD_POSITION_TILT 2001 -#define WW_SENSOR_BODY_POSE 2002 -#define WW_SENSOR_ACCELEROMETER 2003 -#define WW_SENSOR_GYROSCOPE 2004 -#define WW_SENSOR_DISTANCE_FRONT_LEFT_FACING 3000 -#define WW_SENSOR_DISTANCE_FRONT_RIGHT_FACING 3001 -#define WW_SENSOR_DISTANCE_BACK 3002 -#define WW_SENSOR_ENCODER_LEFT_WHEEL 3003 -#define WW_SENSOR_ENCODER_RIGHT_WHEEL 3004 -#define WW_SENSOR_MICROPHONE 3005 +typedef NS_ENUM(unsigned int, WWComponentId) { + WW_COMMAND_POWER = 1, + WW_COMMAND_EYE_RING = 100, + WW_COMMAND_LIGHT_RGB_EYE = 101, + WW_COMMAND_LIGHT_RGB_LEFT_EAR = 102, + WW_COMMAND_LIGHT_RGB_RIGHT_EAR = 103, + WW_COMMAND_LIGHT_RGB_CHEST = 104, + WW_COMMAND_LIGHT_MONO_TAIL = 105, + WW_COMMAND_LIGHT_MONO_BUTTON_MAIN = 106, + WW_COMMAND_HEAD_POSITION_TILT = 202, + WW_COMMAND_HEAD_POSITION_PAN = 203, + WW_COMMAND_BODY_LINEAR_ANGULAR = 204, + WW_COMMAND_BODY_POSE = 205, + WW_COMMAND_MOTOR_HEAD_BANG = 210, + WW_COMMAND_BODY_WHEELS = 211, + WW_COMMAND_BODY_COAST = 212, + WW_COMMAND_SPEAKER = 300, + WW_COMMAND_ON_ROBOT_ANIM = 301, + WW_COMMAND_LAUNCHER_FLING = 400, + WW_COMMAND_LAUNCHER_RELOAD = 401, + WW_SENSOR_BUTTON_MAIN = 1000, + WW_SENSOR_BUTTON_1 = 1001, + WW_SENSOR_BUTTON_2 = 1002, + WW_SENSOR_BUTTON_3 = 1003, + WW_SENSOR_HEAD_POSITION_PAN = 2000, + WW_SENSOR_HEAD_POSITION_TILT = 2001, + WW_SENSOR_BODY_POSE = 2002, + WW_SENSOR_ACCELEROMETER = 2003, + WW_SENSOR_GYROSCOPE = 2004, + WW_SENSOR_DISTANCE_FRONT_LEFT_FACING = 3000, + WW_SENSOR_DISTANCE_FRONT_RIGHT_FACING = 3001, + WW_SENSOR_DISTANCE_BACK = 3002, + WW_SENSOR_ENCODER_LEFT_WHEEL = 3003, + WW_SENSOR_ENCODER_RIGHT_WHEEL = 3004, + WW_SENSOR_MICROPHONE = 3005, +}; typedef NS_ENUM(NSInteger, WWRobotType) { WW_ROBOT_UNKNOWN = 1000, diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 941055b..5b3e5c4 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -24,7 +24,6 @@ 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */; }; 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */; }; 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */; }; - 0A304C541A1BEBC9001A67B7 /* ControlSensorsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C531A1BEBC9001A67B7 /* ControlSensorsViewController.m */; }; 0A304C581A1C0DA4001A67B7 /* ControlSoundViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */; }; 0A412C581A6468110045A7B0 /* wiggle.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A412C571A6468110045A7B0 /* wiggle.json */; }; 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; @@ -34,6 +33,7 @@ 604F66F91DD4885A00729682 /* ControlEyeRingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */; }; 604F66FB1DD494DA00729682 /* ControlDriveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */; }; 604F66FD1DD4A80600729682 /* ControlHeadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */; }; + 604F66FF1DD4ACB700729682 /* ControlSensorsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */; }; 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 605653781DD47ED900354A88 /* ClassExtensions.swift */; }; 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */; }; 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D964F1A1B844000B109CC /* CircleJoystickView.m */; }; @@ -77,8 +77,6 @@ 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RobotControlViewController.m; sourceTree = ""; }; 0A304C491A1AE50B001A67B7 /* JoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JoystickView.h; sourceTree = ""; }; 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JoystickView.m; sourceTree = ""; }; - 0A304C521A1BEBC9001A67B7 /* ControlSensorsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlSensorsViewController.h; sourceTree = ""; }; - 0A304C531A1BEBC9001A67B7 /* ControlSensorsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ControlSensorsViewController.m; sourceTree = ""; }; 0A304C561A1C0DA4001A67B7 /* ControlSoundViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlSoundViewController.h; sourceTree = ""; }; 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ControlSoundViewController.m; sourceTree = ""; }; 0A412C571A6468110045A7B0 /* wiggle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = wiggle.json; sourceTree = ""; }; @@ -90,6 +88,7 @@ 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlEyeRingViewController.swift; sourceTree = ""; }; 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlDriveViewController.swift; sourceTree = ""; }; 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlHeadViewController.swift; sourceTree = ""; }; + 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlSensorsViewController.swift; sourceTree = ""; }; 605653781DD47ED900354A88 /* ClassExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClassExtensions.swift; sourceTree = ""; }; 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlLightsViewController.swift; sourceTree = ""; }; 780D964E1A1B844000B109CC /* CircleJoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleJoystickView.h; sourceTree = ""; }; @@ -213,8 +212,7 @@ 0A304C0A1A1A6FEF001A67B7 /* RobotControlPanelViewController.m */, 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */, 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */, - 0A304C521A1BEBC9001A67B7 /* ControlSensorsViewController.h */, - 0A304C531A1BEBC9001A67B7 /* ControlSensorsViewController.m */, + 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */, 0A304C561A1C0DA4001A67B7 /* ControlSoundViewController.h */, 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */, ); @@ -357,9 +355,9 @@ 604F66F71DD4838800729682 /* RobotControlViewController.swift in Sources */, 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */, 604F66F91DD4885A00729682 /* ControlEyeRingViewController.swift in Sources */, - 0A304C541A1BEBC9001A67B7 /* ControlSensorsViewController.m in Sources */, 0A304C021A1A6FEF001A67B7 /* main.m in Sources */, 604F66FB1DD494DA00729682 /* ControlDriveViewController.swift in Sources */, + 604F66FF1DD4ACB700729682 /* ControlSensorsViewController.swift in Sources */, 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */, 604F66FD1DD4A80600729682 /* ControlHeadViewController.swift in Sources */, 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */, diff --git a/iOS/playground/ControlSensorsViewController.h b/iOS/playground/ControlSensorsViewController.h deleted file mode 100644 index 6dd8d72..0000000 --- a/iOS/playground/ControlSensorsViewController.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// ControlSensorsViewController.h -// playground -// -// Created by Kevin Liang on 11/18/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "RobotControlViewController.h" - -@interface ControlSensorsViewController : RobotControlViewController - -@property (weak, nonatomic) IBOutlet UITableView *dataTableView; - -@end diff --git a/iOS/playground/ControlSensorsViewController.m b/iOS/playground/ControlSensorsViewController.m deleted file mode 100644 index f0f1bc7..0000000 --- a/iOS/playground/ControlSensorsViewController.m +++ /dev/null @@ -1,136 +0,0 @@ -// -// ControlSensorsViewController.m -// playground -// -// Created by Kevin Liang on 11/18/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "ControlSensorsViewController.h" - -@interface ControlSensorsViewController () - -@property (nonatomic, strong) NSTimer *refreshDataTimer; - -- (void) refreshSensorData:(NSTimer *)timer; - -@end - -#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI)) - -@implementation ControlSensorsViewController - -- (void)viewDidLoad { - [super viewDidLoad]; - // Do any additional setup after loading the view. - - self.dataTableView.rowHeight = 200; -} - -- (void) viewWillAppear:(BOOL)animated { - [super viewWillAppear:animated]; - - self.refreshDataTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshSensorData:) userInfo:nil repeats:YES]; -} - -- (void) viewWillDisappear:(BOOL)animated { - [super viewWillDisappear:animated]; - - [self.refreshDataTimer invalidate]; -} - -- (void) refreshSensorData:(NSTimer *)timer { - [self.dataTableView reloadData]; -} - -# pragma mark - table view -- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { - return 1; -} - -- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return self.connectedRobots.count; -} - -- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell" forIndexPath:indexPath]; - - WWRobot *robot = (WWRobot *)self.connectedRobots[indexPath.row]; - WWSensorSet *sensorData = robot.history.currentState; - - NSMutableString *detail = [NSMutableString stringWithCapacity:2000]; - [detail appendFormat:@"Name: %@\n", robot.name]; - - //buttons - WWSensorButton *SButtonMain = (WWSensorButton *)[sensorData sensorForIndex:WW_SENSOR_BUTTON_MAIN]; - WWSensorButton *SButtonOne = (WWSensorButton *)[sensorData sensorForIndex:WW_SENSOR_BUTTON_1]; - WWSensorButton *SButtonTwo = (WWSensorButton *)[sensorData sensorForIndex:WW_SENSOR_BUTTON_2]; - WWSensorButton *SButtonThree = (WWSensorButton *)[sensorData sensorForIndex:WW_SENSOR_BUTTON_3]; - [detail appendFormat:@"Btn Main: %@, ", SButtonMain.isPressed? @"true": @"false"]; - [detail appendFormat:@"Btn 1: %@, ", SButtonOne.isPressed? @"true": @"false"]; - [detail appendFormat:@"Btn 2: %@, ", SButtonTwo.isPressed? @"true": @"false"]; - [detail appendFormat:@"Btn 3: %@", SButtonThree.isPressed? @"true": @"false"]; - [detail appendFormat:@"\n"]; - - //accelerometer - WWSensorAccelerometer *SAcc = (WWSensorAccelerometer *)[sensorData sensorForIndex:WW_SENSOR_ACCELEROMETER]; - [detail appendString:@"Accel: "]; - [detail appendFormat:@"X: %3.2f, ", SAcc ? SAcc.x : NAN]; - [detail appendFormat:@"Y: %3.2f, ", SAcc ? SAcc.y : NAN]; - [detail appendFormat:@"Z: %3.2f", SAcc ? SAcc.z : NAN]; - [detail appendFormat:@"\n"]; - - WWSensorMicrophone *SMic = (WWSensorMicrophone *)[sensorData sensorForIndex:WW_SENSOR_MICROPHONE]; - [detail appendFormat:@"Microphone: "]; - [detail appendFormat:@"Amplitude: %03f, ", SMic ? SMic.amplitude : NAN]; - [detail appendFormat:@"Angle: %3.2f degrees", RADIANS_TO_DEGREES(SMic ? SMic.triangulationAngle : NAN)]; - [detail appendFormat:@"\n"]; - - if (robot.robotType == WW_ROBOT_DASH) { - //distance sensors - WWSensorDistance *SDistFLF = (WWSensorDistance *)[sensorData sensorForIndex:WW_SENSOR_DISTANCE_FRONT_RIGHT_FACING]; - WWSensorDistance *SDistFRF = (WWSensorDistance *)[sensorData sensorForIndex:WW_SENSOR_DISTANCE_FRONT_LEFT_FACING]; - WWSensorDistance *SDistRRF = (WWSensorDistance *)[sensorData sensorForIndex:WW_SENSOR_DISTANCE_BACK]; - [detail appendFormat:@"Distance: "]; - [detail appendFormat:@"Left-Facing: %2.2f, ", SDistFLF ? SDistFLF.reflectance : NAN]; - [detail appendFormat:@"Dist Right-Facing: %2.2f, ", SDistFRF ? SDistFRF.reflectance : NAN]; - [detail appendFormat:@"Dist Tail: %2.2f", SDistRRF ? SDistRRF.reflectance : NAN]; - [detail appendFormat:@"\n"]; - - //gyro - WWSensorGyroscope *SGyro = (WWSensorGyroscope *)[sensorData sensorForIndex:WW_SENSOR_GYROSCOPE]; - [detail appendString:@"Gyro: "]; - [detail appendFormat:@"yaw: %3.2f, ", SGyro ? SGyro.yaw : NAN]; - [detail appendFormat:@"pitch: %3.2f, ", SGyro ? SGyro.pitch : NAN]; - [detail appendFormat:@"roll: %3.2f", SGyro ? SGyro.roll : NAN]; - [detail appendFormat:@"\n"]; - - //head - WWSensorHeadPosition *SMotorServoHeadPan = (WWSensorHeadPosition *)[sensorData sensorForIndex:WW_SENSOR_HEAD_POSITION_PAN]; - WWSensorHeadPosition *SMotorServoHeadTilt = (WWSensorHeadPosition *)[sensorData sensorForIndex:WW_SENSOR_HEAD_POSITION_TILT]; - [detail appendString:@"Head: "]; - [detail appendFormat:@"Pan: %3.2f degrees, ", RADIANS_TO_DEGREES(SMotorServoHeadPan ? SMotorServoHeadPan.radians : NAN)]; - [detail appendFormat:@"Tilt: %3.2f degrees", RADIANS_TO_DEGREES(SMotorServoHeadTilt ? SMotorServoHeadTilt.radians : NAN)]; - [detail appendFormat:@"\n"]; - - //encoder - WWSensorEncoder *SEncoderLW = (WWSensorEncoder *)[sensorData sensorForIndex:WW_SENSOR_ENCODER_LEFT_WHEEL]; - WWSensorEncoder *SEncoderRW = (WWSensorEncoder *)[sensorData sensorForIndex:WW_SENSOR_ENCODER_RIGHT_WHEEL]; - [detail appendString:@"Encoder: "]; - [detail appendFormat:@"Left: %4.2f cm, ", SEncoderLW? SEncoderLW.distance: NAN]; - [detail appendFormat:@"Right: %4.2f cm", SEncoderLW? SEncoderRW.distance: NAN]; - [detail appendFormat:@"\n"]; - } - - cell.textLabel.text = detail; - //[cell.textLabel sizeToFit]; - return cell; -} - - -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -@end diff --git a/iOS/playground/ControlSensorsViewController.swift b/iOS/playground/ControlSensorsViewController.swift new file mode 100644 index 0000000..5cef372 --- /dev/null +++ b/iOS/playground/ControlSensorsViewController.swift @@ -0,0 +1,146 @@ +// +// ControlSensorsViewController.swift +// playground +// +// Created by Jelle Alten on 10-11-16. +// Copyright © 2016 Wonder Workshop. All rights reserved. +// + +import UIKit + +// Declare radiansToDegrees function +func radiansToDegrees (radians: Double)->Double { + return radians * 180 / M_PI +} + +class ControlSensorsViewController: RobotControlViewController { + + private var refreshDataTimer = Timer() + + @IBOutlet weak var dataTableView: UITableView! + + override func viewDidLoad() { + super.viewDidLoad() + dataTableView.rowHeight = 200; + + // Do any additional setup after loading the view. + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + refreshDataTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.refreshSensorData(timer:)), userInfo: nil, repeats: true) + + } + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + refreshDataTimer.invalidate() + } + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + func refreshSensorData(timer : Timer) { + dataTableView.reloadData() + } + +} + +extension ControlSensorsViewController : UITableViewDataSource { + + func numberOfSections(in tableView: UITableView) -> Int { + return 1 + } + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return connectedRobots.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) + let robot = self.robots[indexPath.row] + let sensorData = robot.history.currentState() as WWSensorSet + + var detail = String() + detail.append("Name: \(robot.name)\n") + + if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_MAIN) as? WWSensorButton { + detail.append("Btn Main: \(button.isPressed ? "true" : "false"), ") + } + if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_1) as? WWSensorButton { + detail.append("Btn 1: \(button.isPressed ? "true" : "false"), ") + } + if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_2) as? WWSensorButton { + detail.append("Btn 2: \(button.isPressed ? "true" : "false"), ") + } + if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_3) as? WWSensorButton { + detail.append("Btn 3: \(button.isPressed ? "true" : "false")") + } + detail.append("\n") + + let sAcc = sensorData.sensor(forIndex: .SENSOR_ACCELEROMETER) as? WWSensorAccelerometer + //accelerometer + detail.append("Accel: ") + if let sAcc = sAcc { + detail = detail.appendingFormat("X: %3.2f, ", sAcc.x) + detail = detail.appendingFormat("Y: %3.2f, ", sAcc.y) + detail = detail.appendingFormat("Z: %3.2f", sAcc.z) + } + detail.append("\n") + + let sMic = sensorData.sensor(forIndex: .SENSOR_MICROPHONE) as? WWSensorMicrophone + detail.append("Microphone: ") + if let sMic = sMic { + detail = detail.appendingFormat("Amplitude: %03f, ", sMic.amplitude) + detail = detail.appendingFormat("Angle: %3.2f degrees", radiansToDegrees(radians: sMic.triangulationAngle)) + } + detail.append("\n") + + if (robot.robotType == .ROBOT_DASH) { + //distance sensors + let sDistFLF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_FRONT_RIGHT_FACING) as? WWSensorDistance + let sDistFRF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_FRONT_LEFT_FACING) as? WWSensorDistance + let sDistRRF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_BACK) as? WWSensorDistance + detail.append("Distance: ") + if let sDistFLF = sDistFLF, let sDistFRF = sDistFRF, let sDistRRF = sDistRRF { + detail = detail.appendingFormat("Left-Facing: %2.2f, ", sDistFLF.reflectance) + detail = detail.appendingFormat("Dist Right-Facing: %2.2f, ", sDistFRF.reflectance) + detail = detail.appendingFormat("Dist Tail: %2.2f", sDistRRF.reflectance) + } + detail.append("\n") + + //gyro + detail.append("Gyro: ") + if let sGyro = sensorData.sensor(forIndex: .SENSOR_GYROSCOPE) as? WWSensorGyroscope { + detail = detail.appendingFormat("yaw: %3.2f, ", sGyro.yaw) + detail = detail.appendingFormat("pitch: %3.2f, ", sGyro.pitch) + detail = detail.appendingFormat("roll: %3.2f", sGyro.roll) + } + detail.append("\n") + + //head + detail.append("Head: ") + if let sMotorServoHeadPan = sensorData.sensor(forIndex: .SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition, + let sMotorServoHeadTilt = sensorData.sensor(forIndex: .SENSOR_HEAD_POSITION_TILT) as? WWSensorHeadPosition { + detail = detail.appendingFormat("Pan: %3.2f degrees, ", radiansToDegrees(radians:sMotorServoHeadPan.radians)) + detail = detail.appendingFormat("Tilt: %3.2f degrees", radiansToDegrees(radians:sMotorServoHeadTilt.radians)) + } + detail.append("\n") + + //encoder + detail.append("Encoder: ") + if let sEncoderLW = sensorData.sensor(forIndex: .SENSOR_ENCODER_LEFT_WHEEL) as? WWSensorEncoder, + let sEncoderRW = sensorData.sensor(forIndex: .SENSOR_ENCODER_RIGHT_WHEEL) as? WWSensorEncoder { + + detail = detail.appendingFormat("Left: %4.2f cm, ", sEncoderLW.distance) + detail = detail.appendingFormat("Right: %4.2f cm", sEncoderRW.distance) + } + detail.append("\n") + } + + cell.textLabel?.text = detail; + //[cell.textLabel sizeToFit) + return cell; + } + +} diff --git a/iOS/playground/RobotControlViewController.swift b/iOS/playground/RobotControlViewController.swift index 02e20d9..5e37b0a 100644 --- a/iOS/playground/RobotControlViewController.swift +++ b/iOS/playground/RobotControlViewController.swift @@ -36,3 +36,11 @@ extension RobotControlViewController_swift : WWRobotObserver { } +extension RobotControlViewController { + var robots : [WWRobot] { + guard let robots = connectedRobots as? [WWRobot] else { + return [WWRobot]() + } + return robots; + } +} From 6efe02c4f443e494e5586abef7fb5738a6ccef55 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 17:22:38 +0100 Subject: [PATCH 09/18] swiftify sound pane --- iOS/playground.xcodeproj/project.pbxproj | 10 +-- iOS/playground/Base.lproj/Main.storyboard | 14 +-- .../ControlDriveViewController.swift | 12 +-- iOS/playground/ControlSoundViewController.h | 17 ---- iOS/playground/ControlSoundViewController.m | 88 ------------------- .../ControlSoundViewController.swift | 88 +++++++++++++++++++ .../RobotControlPanelViewController.m | 2 - iOS/playground/RobotControlViewController.h | 2 +- 8 files changed, 106 insertions(+), 127 deletions(-) delete mode 100644 iOS/playground/ControlSoundViewController.h delete mode 100644 iOS/playground/ControlSoundViewController.m create mode 100644 iOS/playground/ControlSoundViewController.swift diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 5b3e5c4..96bf592 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -24,7 +24,6 @@ 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */; }; 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */; }; 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */; }; - 0A304C581A1C0DA4001A67B7 /* ControlSoundViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */; }; 0A412C581A6468110045A7B0 /* wiggle.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A412C571A6468110045A7B0 /* wiggle.json */; }; 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */; }; @@ -34,6 +33,7 @@ 604F66FB1DD494DA00729682 /* ControlDriveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */; }; 604F66FD1DD4A80600729682 /* ControlHeadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */; }; 604F66FF1DD4ACB700729682 /* ControlSensorsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */; }; + 604F67011DD4BAA900729682 /* ControlSoundViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F67001DD4BAA900729682 /* ControlSoundViewController.swift */; }; 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 605653781DD47ED900354A88 /* ClassExtensions.swift */; }; 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */; }; 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D964F1A1B844000B109CC /* CircleJoystickView.m */; }; @@ -77,8 +77,6 @@ 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RobotControlViewController.m; sourceTree = ""; }; 0A304C491A1AE50B001A67B7 /* JoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JoystickView.h; sourceTree = ""; }; 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JoystickView.m; sourceTree = ""; }; - 0A304C561A1C0DA4001A67B7 /* ControlSoundViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ControlSoundViewController.h; sourceTree = ""; }; - 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ControlSoundViewController.m; sourceTree = ""; }; 0A412C571A6468110045A7B0 /* wiggle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = wiggle.json; sourceTree = ""; }; 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "playground-Bridging-Header.h"; sourceTree = ""; }; 6007A9021DD3BF24005DF517 /* RobotController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotController.swift; sourceTree = ""; }; @@ -89,6 +87,7 @@ 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlDriveViewController.swift; sourceTree = ""; }; 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlHeadViewController.swift; sourceTree = ""; }; 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlSensorsViewController.swift; sourceTree = ""; }; + 604F67001DD4BAA900729682 /* ControlSoundViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlSoundViewController.swift; sourceTree = ""; }; 605653781DD47ED900354A88 /* ClassExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClassExtensions.swift; sourceTree = ""; }; 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlLightsViewController.swift; sourceTree = ""; }; 780D964E1A1B844000B109CC /* CircleJoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleJoystickView.h; sourceTree = ""; }; @@ -213,8 +212,7 @@ 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */, 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */, 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */, - 0A304C561A1C0DA4001A67B7 /* ControlSoundViewController.h */, - 0A304C571A1C0DA4001A67B7 /* ControlSoundViewController.m */, + 604F67001DD4BAA900729682 /* ControlSoundViewController.swift */, ); name = controllers; sourceTree = ""; @@ -351,7 +349,6 @@ 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */, 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */, 0A304C051A1A6FEF001A67B7 /* AppDelegate.m in Sources */, - 0A304C581A1C0DA4001A67B7 /* ControlSoundViewController.m in Sources */, 604F66F71DD4838800729682 /* RobotControlViewController.swift in Sources */, 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */, 604F66F91DD4885A00729682 /* ControlEyeRingViewController.swift in Sources */, @@ -360,6 +357,7 @@ 604F66FF1DD4ACB700729682 /* ControlSensorsViewController.swift in Sources */, 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */, 604F66FD1DD4A80600729682 /* ControlHeadViewController.swift in Sources */, + 604F67011DD4BAA900729682 /* ControlSoundViewController.swift in Sources */, 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */, 0A304C0B1A1A6FEF001A67B7 /* RobotControlPanelViewController.m in Sources */, 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */, diff --git a/iOS/playground/Base.lproj/Main.storyboard b/iOS/playground/Base.lproj/Main.storyboard index 0b91fdd..523c7d6 100644 --- a/iOS/playground/Base.lproj/Main.storyboard +++ b/iOS/playground/Base.lproj/Main.storyboard @@ -512,7 +512,7 @@ - + @@ -524,7 +524,7 @@ - + @@ -556,7 +556,7 @@ - + @@ -566,7 +566,7 @@ - + @@ -580,11 +580,11 @@ - + - + diff --git a/iOS/playground/ControlDriveViewController.swift b/iOS/playground/ControlDriveViewController.swift index d1d77e8..029127c 100644 --- a/iOS/playground/ControlDriveViewController.swift +++ b/iOS/playground/ControlDriveViewController.swift @@ -49,7 +49,7 @@ class ControlDriveViewController : RobotControlViewController { @IBAction func toggleWiggle(_ sender: Any) { let btnText = isWiggling ? "Start wiggle" : "Stop wiggle" - for robot in connectedRobots as! [WWRobot] { + for robot in robots { if isWiggling { robot.stopCommand(wiggleAnimation) } @@ -65,7 +65,7 @@ class ControlDriveViewController : RobotControlViewController { @IBAction func executeNod(_ sender: Any) { if !isNodding && !isWiggling { - for robot in connectedRobots as! [WWRobot] { + for robot in robots { robot.executeCommand(nodAnimation, withOptions:nil) } } @@ -109,11 +109,11 @@ extension ControlDriveViewController : JoystickDelegate { } extension ControlDriveViewController { // : WWRobotObserver { - override func robot(_ robot: WWRobot!, didFinishCommand sequence: WWCommandSetSequence!) { + func robot(_ robot: WWRobot!, didFinishCommand sequence: WWCommandSetSequence!) { if sequence == wiggleAnimation { if isWiggling { // continue to wiggle until user stops - for robot in connectedRobots as! [WWRobot] { + for robot in robots { robot.executeCommand(wiggleAnimation, withOptions: nil) } } else { @@ -123,9 +123,9 @@ extension ControlDriveViewController { // : WWRobotObserver { } } - override func robot(_ robot: WWRobot!, didStopExecutingCommand sequence: WWCommandSetSequence!, withResults results: [AnyHashable : Any]!) { + func robot(_ robot: WWRobot!, didStopExecutingCommand sequence: WWCommandSetSequence!, withResults results: [AnyHashable : Any]!) { print("wiggle sequence terminated by user."); - for robot in connectedRobots as! [WWRobot] { + for robot in robots { robot.resetState() } } diff --git a/iOS/playground/ControlSoundViewController.h b/iOS/playground/ControlSoundViewController.h deleted file mode 100644 index fddeb5e..0000000 --- a/iOS/playground/ControlSoundViewController.h +++ /dev/null @@ -1,17 +0,0 @@ -// -// ControlSoundViewController.h -// playground -// -// Created by Kevin Liang on 11/18/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "RobotControlViewController.h" - -@interface ControlSoundViewController : RobotControlViewController - -@property (weak, nonatomic) IBOutlet UISlider *volumeSlider; - -- (IBAction)playSoundPressed:(id)sender; - -@end diff --git a/iOS/playground/ControlSoundViewController.m b/iOS/playground/ControlSoundViewController.m deleted file mode 100644 index 09c2f3c..0000000 --- a/iOS/playground/ControlSoundViewController.m +++ /dev/null @@ -1,88 +0,0 @@ -// -// ControlSoundViewController.m -// playground -// -// Created by Kevin Liang on 11/18/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. -// - -#import "ControlSoundViewController.h" - -@interface ControlSoundViewController () - -@property (nonatomic) float volume; -@property (nonatomic, strong) WWEvent *mainButtonTouchUp; -@property (nonatomic, strong) WWEvent *headPanPositionChanged; - -@end - -@implementation ControlSoundViewController - -- (void)viewDidLoad { - [super viewDidLoad]; - // Do any additional setup after loading the view. - - self.volume = (WW_VOLUME_MAX - WW_VOLUME_MUTE) / 2.0; - self.mainButtonTouchUp = [WWEventToolbelt buttonOnUp:[NSNumber numberWithUnsignedInteger:WW_SENSOR_BUTTON_MAIN]]; - - self.headPanPositionChanged = [[WWEvent alloc] initWithShouldAlertBlock:^BOOL(WWEvent *event, WWSensorHistory *history) { - WWSensorHeadPosition *currHeadPan = (WWSensorHeadPosition *)[history.currentState sensorForIndex:WW_SENSOR_HEAD_POSITION_PAN]; - WWSensorHeadPosition *prevHeadPan300ms = (WWSensorHeadPosition *)[[history pastStateAtTimeAgo:0.3] sensorForIndex:WW_SENSOR_HEAD_POSITION_PAN]; - - return fabsf(currHeadPan.radians - prevHeadPan300ms.radians) > 0.05; // allow for some errors - } identifier:@"head"]; -} - -- (void) setVolume:(float)volume { - _volume = volume; - self.volumeSlider.value = _volume; -} - -- (void)viewWillAppear:(BOOL)animated { - [super viewWillAppear:animated]; - - self.volumeSlider.value = self.volume; - - for (WWRobot *robot in self.connectedRobots) { - [robot addEvent:self.mainButtonTouchUp]; - [robot addEvent:self.headPanPositionChanged]; - } -} - -- (void)viewWillDisappear:(BOOL)animated { - [super viewWillDisappear:animated]; - - for (WWRobot *robot in self.connectedRobots) { - [robot removeAllEvents]; - } -} - -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -- (IBAction)playSoundPressed:(id)sender { - WWCommandSet *speakerCommand = [WWCommandSet new]; - WWCommandSpeaker *speaker = [[WWCommandSpeaker alloc] initWithDefaultSound:WW_SOUNDFILE_HI]; - speaker.volume = self.volume; - [speakerCommand setSound:speaker]; - - [self sendCommandSetToRobots:speakerCommand]; -} - -#pragma mark - WWRobotDelegate -- (void) robot:(WWRobot *)robot eventsTriggered:(NSArray *)events { - - for (WWEvent *event in events) { - if ([self.mainButtonTouchUp isEqual:event]) { - self.volume = (self.volume == WW_VOLUME_MUTE) ? WW_VOLUME_MAX : WW_VOLUME_MUTE; - } - else if ([event hasIdentifier:@"head"]) { - WWSensorHeadPosition *headPan = (WWSensorHeadPosition *)[robot.history.currentState sensorForIndex:WW_SENSOR_HEAD_POSITION_PAN]; - self.volume = headPan.radians / -2.0f; - } - } -} - -@end diff --git a/iOS/playground/ControlSoundViewController.swift b/iOS/playground/ControlSoundViewController.swift new file mode 100644 index 0000000..301f4e8 --- /dev/null +++ b/iOS/playground/ControlSoundViewController.swift @@ -0,0 +1,88 @@ +// +// ControlSoundViewController.swift +// playground +// +// Created by Jelle Alten on 10-11-16. +// Copyright © 2016 Wonder Workshop. All rights reserved. +// + +import UIKit + +class ControlSoundViewController: RobotControlViewController { + + private var _volume : Double = (WW_VOLUME_MAX - WW_VOLUME_MUTE) / 2.0 + var volume : Double { + set { + _volume = newValue + volumeSlider.value = Float(newValue) + } + get { + return _volume + } + } + var mainButtonTouchUp : WWEvent + var headPanPositionChanged : WWEvent + + @IBOutlet weak var volumeSlider : UISlider! + + required init?(coder aDecoder: NSCoder) { + + let buttonNumber = NSNumber(value:1000) + mainButtonTouchUp = WWEventToolbelt.button(onUp: buttonNumber) + headPanPositionChanged = WWEvent(shouldAlert: { (event, history) -> Bool in + guard let history = history, let state = history.currentState(), + let currHeadPan = state.sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition, + let prevHeadPan300ms = history.pastState(atTimeAgo:0.3).sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition else { + return false + } + let diff = currHeadPan.radians - prevHeadPan300ms.radians + return fabs(diff) > 0.05 // allow for some errors + + }, identifier: "head") + + super.init(coder: aDecoder) + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + volumeSlider.value = Float(volume) + + for robot in robots { + robot.add(mainButtonTouchUp) + robot.add(headPanPositionChanged) + } + } + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + for robot in robots { + robot.removeAllEvents() + } + } + + @IBAction func playSoundPressed(_ sender: Any) { + let speakerCommand = WWCommandSet() + if let speaker = WWCommandSpeaker(defaultSound:WW_SOUNDFILE_HI) { + speaker.volume = Float(volume) + speakerCommand.setSound(speaker) + sendCommandSet(toRobots: speakerCommand) + } + } +} + + + +extension ControlSoundViewController : WWRobotObserver { + func robot(_ robot: WWRobot!, eventsTriggered events: [Any]!) { + for event in events as! [WWEvent] { + if mainButtonTouchUp == event { + self.volume = (volume == WW_VOLUME_MUTE) ? WW_VOLUME_MAX : WW_VOLUME_MUTE + } + else if event.hasIdentifier("head") { + if let headPan = robot.history.currentState().sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition { + volume = headPan.radians / -2.0 + } + } + } + } +} diff --git a/iOS/playground/RobotControlPanelViewController.m b/iOS/playground/RobotControlPanelViewController.m index b5d9bac..6e0c686 100644 --- a/iOS/playground/RobotControlPanelViewController.m +++ b/iOS/playground/RobotControlPanelViewController.m @@ -8,8 +8,6 @@ #import "RobotControlPanelViewController.h" #import "ControlDriveViewController.h" -#import "ControlSoundViewController.h" -#import "ControlSensorsViewController.h" #import "Playground-Swift.h" @interface RobotControlPanelViewController () diff --git a/iOS/playground/RobotControlViewController.h b/iOS/playground/RobotControlViewController.h index f0ec008..52d976e 100644 --- a/iOS/playground/RobotControlViewController.h +++ b/iOS/playground/RobotControlViewController.h @@ -6,7 +6,7 @@ // Copyright (c) 2014 Wonder Workshop. All rights reserved. // -@interface RobotControlViewController : UIViewController +@interface RobotControlViewController : UIViewController @property (nonatomic, strong) NSArray *connectedRobots; From c9185258a06c370d9beeb8c1b5609bd2e05ab66e Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Thu, 10 Nov 2016 21:23:10 +0100 Subject: [PATCH 10/18] swiftly ControlDriveViewController and RobotControlPanelVC and RobotControlVC --- iOS/playground.xcodeproj/project.pbxproj | 16 +- iOS/playground/AppDelegate.m | 1 - iOS/playground/Base.lproj/Main.storyboard | 10 +- iOS/playground/CircleJoystickView.h | 1 - iOS/playground/CircleJoystickView.m | 20 --- iOS/playground/ControlDriveViewController.h | 20 --- iOS/playground/ControlDriveViewController.m | 155 ------------------ .../ControlDriveViewController.swift | 13 +- .../ControlSensorsViewController.swift | 2 +- .../ControlSoundViewController.swift | 8 +- .../AppIcon.appiconset/Contents.json | 25 +++ .../RobotControlPanelViewController.h | 21 --- .../RobotControlPanelViewController.m | 74 --------- .../RobotControlPanelViewController.swift | 58 +++++++ iOS/playground/RobotControlViewController.h | 16 -- iOS/playground/RobotControlViewController.m | 48 ------ .../RobotControlViewController.swift | 13 +- iOS/playground/RobotListViewController.swift | 2 +- iOS/playground/playground-Bridging-Header.h | 2 - 19 files changed, 107 insertions(+), 398 deletions(-) delete mode 100644 iOS/playground/ControlDriveViewController.h delete mode 100644 iOS/playground/ControlDriveViewController.m delete mode 100644 iOS/playground/RobotControlPanelViewController.h delete mode 100644 iOS/playground/RobotControlPanelViewController.m create mode 100644 iOS/playground/RobotControlPanelViewController.swift delete mode 100644 iOS/playground/RobotControlViewController.h delete mode 100644 iOS/playground/RobotControlViewController.m diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 96bf592..f277de3 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -10,7 +10,6 @@ 0A304C021A1A6FEF001A67B7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C011A1A6FEF001A67B7 /* main.m */; }; 0A304C051A1A6FEF001A67B7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C041A1A6FEF001A67B7 /* AppDelegate.m */; }; 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */; }; - 0A304C0B1A1A6FEF001A67B7 /* RobotControlPanelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C0A1A1A6FEF001A67B7 /* RobotControlPanelViewController.m */; }; 0A304C0E1A1A6FEF001A67B7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C0C1A1A6FEF001A67B7 /* Main.storyboard */; }; 0A304C101A1A6FEF001A67B7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C0F1A1A6FEF001A67B7 /* Images.xcassets */; }; 0A304C131A1A6FEF001A67B7 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C111A1A6FEF001A67B7 /* LaunchScreen.xib */; }; @@ -22,7 +21,6 @@ 0A304C341A1A833B001A67B7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0A304C331A1A833B001A67B7 /* Foundation.framework */; }; 0A304C3C1A1A8619001A67B7 /* RobotListTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C3A1A1A8619001A67B7 /* RobotListTableViewCell.m */; }; 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */; }; - 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */; }; 0A304C4B1A1AE50B001A67B7 /* JoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */; }; 0A412C581A6468110045A7B0 /* wiggle.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A412C571A6468110045A7B0 /* wiggle.json */; }; 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; @@ -36,6 +34,7 @@ 604F67011DD4BAA900729682 /* ControlSoundViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F67001DD4BAA900729682 /* ControlSoundViewController.swift */; }; 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 605653781DD47ED900354A88 /* ClassExtensions.swift */; }; 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */; }; + 60EB766F1DD4F95B00071F47 /* RobotControlPanelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60EB766E1DD4F95B00071F47 /* RobotControlPanelViewController.swift */; }; 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780D964F1A1B844000B109CC /* CircleJoystickView.m */; }; /* End PBXBuildFile section */ @@ -56,8 +55,6 @@ 0A304C031A1A6FEF001A67B7 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 0A304C041A1A6FEF001A67B7 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RobotListViewController.swift; sourceTree = ""; }; - 0A304C091A1A6FEF001A67B7 /* RobotControlPanelViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RobotControlPanelViewController.h; sourceTree = ""; }; - 0A304C0A1A1A6FEF001A67B7 /* RobotControlPanelViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RobotControlPanelViewController.m; sourceTree = ""; }; 0A304C0D1A1A6FEF001A67B7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 0A304C0F1A1A6FEF001A67B7 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 0A304C121A1A6FEF001A67B7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; @@ -73,8 +70,6 @@ 0A304C391A1A8619001A67B7 /* RobotListTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RobotListTableViewCell.h; sourceTree = ""; }; 0A304C3A1A1A8619001A67B7 /* RobotListTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RobotListTableViewCell.m; sourceTree = ""; }; 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RobotListTableViewCell.xib; sourceTree = ""; }; - 0A304C411A1ABD6D001A67B7 /* RobotControlViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RobotControlViewController.h; sourceTree = ""; }; - 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RobotControlViewController.m; sourceTree = ""; }; 0A304C491A1AE50B001A67B7 /* JoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JoystickView.h; sourceTree = ""; }; 0A304C4A1A1AE50B001A67B7 /* JoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JoystickView.m; sourceTree = ""; }; 0A412C571A6468110045A7B0 /* wiggle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = wiggle.json; sourceTree = ""; }; @@ -90,6 +85,7 @@ 604F67001DD4BAA900729682 /* ControlSoundViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlSoundViewController.swift; sourceTree = ""; }; 605653781DD47ED900354A88 /* ClassExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClassExtensions.swift; sourceTree = ""; }; 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlLightsViewController.swift; sourceTree = ""; }; + 60EB766E1DD4F95B00071F47 /* RobotControlPanelViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotControlPanelViewController.swift; sourceTree = ""; }; 780D964E1A1B844000B109CC /* CircleJoystickView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleJoystickView.h; sourceTree = ""; }; 780D964F1A1B844000B109CC /* CircleJoystickView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CircleJoystickView.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -202,13 +198,10 @@ isa = PBXGroup; children = ( 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */, - 0A304C411A1ABD6D001A67B7 /* RobotControlViewController.h */, - 0A304C421A1ABD6D001A67B7 /* RobotControlViewController.m */, 604F66F61DD4838800729682 /* RobotControlViewController.swift */, 0A304C071A1A6FEF001A67B7 /* RobotListViewController.swift */, 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */, - 0A304C091A1A6FEF001A67B7 /* RobotControlPanelViewController.h */, - 0A304C0A1A1A6FEF001A67B7 /* RobotControlPanelViewController.m */, + 60EB766E1DD4F95B00071F47 /* RobotControlPanelViewController.swift */, 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */, 604F66FC1DD4A80600729682 /* ControlHeadViewController.swift */, 604F66FE1DD4ACB700729682 /* ControlSensorsViewController.swift */, @@ -343,7 +336,6 @@ buildActionMask = 2147483647; files = ( 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */, - 0A304C431A1ABD6D001A67B7 /* RobotControlViewController.m in Sources */, 0A304C3C1A1A8619001A67B7 /* RobotListTableViewCell.m in Sources */, 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */, 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */, @@ -353,13 +345,13 @@ 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */, 604F66F91DD4885A00729682 /* ControlEyeRingViewController.swift in Sources */, 0A304C021A1A6FEF001A67B7 /* main.m in Sources */, + 60EB766F1DD4F95B00071F47 /* RobotControlPanelViewController.swift in Sources */, 604F66FB1DD494DA00729682 /* ControlDriveViewController.swift in Sources */, 604F66FF1DD4ACB700729682 /* ControlSensorsViewController.swift in Sources */, 780D96501A1B844000B109CC /* CircleJoystickView.m in Sources */, 604F66FD1DD4A80600729682 /* ControlHeadViewController.swift in Sources */, 604F67011DD4BAA900729682 /* ControlSoundViewController.swift in Sources */, 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */, - 0A304C0B1A1A6FEF001A67B7 /* RobotControlPanelViewController.m in Sources */, 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/iOS/playground/AppDelegate.m b/iOS/playground/AppDelegate.m index 6a4c5e5..274ce9b 100644 --- a/iOS/playground/AppDelegate.m +++ b/iOS/playground/AppDelegate.m @@ -7,7 +7,6 @@ // #import "AppDelegate.h" -#import "RobotControlPanelViewController.h" @interface AppDelegate () diff --git a/iOS/playground/Base.lproj/Main.storyboard b/iOS/playground/Base.lproj/Main.storyboard index 523c7d6..256c786 100644 --- a/iOS/playground/Base.lproj/Main.storyboard +++ b/iOS/playground/Base.lproj/Main.storyboard @@ -28,7 +28,7 @@ - + @@ -93,11 +93,11 @@ - + diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift index a06c409..ad9b53e 100644 --- a/iOS/playground/RobotListViewController.swift +++ b/iOS/playground/RobotListViewController.swift @@ -2,8 +2,6 @@ // RobotListViewController.m // playground // -// Created by Kevin Liang on 11/17/14. -// Copyright (c) 2014 Wonder Workshop. All rights reserved. // diff --git a/iOS/playground/playground-Bridging-Header.h b/iOS/playground/playground-Bridging-Header.h index 5e88115..e46dade 100644 --- a/iOS/playground/playground-Bridging-Header.h +++ b/iOS/playground/playground-Bridging-Header.h @@ -5,9 +5,6 @@ #import #import -#import "RobotListTableViewCell.h" -#import "JoystickView.h" - @interface WWRobotManager (shared) + (WWRobotManager*) sharedManager; @end From 8f20d5f52c1924ab4b1bb040c9268e4e6e466809 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Fri, 11 Nov 2016 23:50:01 +0100 Subject: [PATCH 15/18] remove static --- iOS/playground.xcodeproj/project.pbxproj | 8 -- iOS/playground/ClassExtensions.swift | 2 +- iOS/playground/RobotController.swift | 157 ----------------------- iOS/playground/RobotViewController.swift | 51 -------- 4 files changed, 1 insertion(+), 217 deletions(-) delete mode 100644 iOS/playground/RobotController.swift delete mode 100644 iOS/playground/RobotViewController.swift diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index bf071fc..6f3ec13 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -19,9 +19,7 @@ 0A304C341A1A833B001A67B7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0A304C331A1A833B001A67B7 /* Foundation.framework */; }; 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */; }; 0A412C581A6468110045A7B0 /* wiggle.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A412C571A6468110045A7B0 /* wiggle.json */; }; - 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9021DD3BF24005DF517 /* RobotController.swift */; }; 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */; }; - 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */; }; 604F66F71DD4838800729682 /* RobotControlViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66F61DD4838800729682 /* RobotControlViewController.swift */; }; 604F66F91DD4885A00729682 /* ControlEyeRingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */; }; 604F66FB1DD494DA00729682 /* ControlDriveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */; }; @@ -67,9 +65,7 @@ 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RobotListTableViewCell.xib; sourceTree = ""; }; 0A412C571A6468110045A7B0 /* wiggle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = wiggle.json; sourceTree = ""; }; 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "playground-Bridging-Header.h"; sourceTree = ""; }; - 6007A9021DD3BF24005DF517 /* RobotController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotController.swift; sourceTree = ""; }; 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bridging-Helper.m"; sourceTree = ""; }; - 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotViewController.swift; sourceTree = ""; }; 604F66F61DD4838800729682 /* RobotControlViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotControlViewController.swift; sourceTree = ""; }; 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlEyeRingViewController.swift; sourceTree = ""; }; 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlDriveViewController.swift; sourceTree = ""; }; @@ -140,8 +136,6 @@ 0A304C0F1A1A6FEF001A67B7 /* Images.xcassets */, 0A304C111A1A6FEF001A67B7 /* LaunchScreen.xib */, 0A304BFF1A1A6FEF001A67B7 /* Supporting Files */, - 6007A9021DD3BF24005DF517 /* RobotController.swift */, - 6007A9061DD3CE3B005DF517 /* RobotViewController.swift */, 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */, 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */, 605653781DD47ED900354A88 /* ClassExtensions.swift */, @@ -327,8 +321,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 6007A9071DD3CE3B005DF517 /* RobotViewController.swift in Sources */, - 6007A9031DD3BF24005DF517 /* RobotController.swift in Sources */, 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */, 604F66F71DD4838800729682 /* RobotControlViewController.swift in Sources */, 608033B41DD660220092A787 /* helper.swift in Sources */, diff --git a/iOS/playground/ClassExtensions.swift b/iOS/playground/ClassExtensions.swift index e4a77d6..f83ff50 100644 --- a/iOS/playground/ClassExtensions.swift +++ b/iOS/playground/ClassExtensions.swift @@ -10,7 +10,7 @@ import Foundation public extension NSObject{ - public class var nameOfClass: String{ + public class var nameOfClass: String { return String(describing: self) } } diff --git a/iOS/playground/RobotController.swift b/iOS/playground/RobotController.swift deleted file mode 100644 index a7bea94..0000000 --- a/iOS/playground/RobotController.swift +++ /dev/null @@ -1,157 +0,0 @@ -// -// RobotController.swift -// playground -// -// Created by Jelle Alten on 09-11-16. -// Copyright © 2016 Wonder Workshop. All rights reserved. -// - -import UIKit - - -enum RobotType : Int { - case unknown = 1000 - case dash = 1001 - case dot = 1002 -} - - -enum ScanPeriod { - case immediate - case pause(Float) -} - - -class RobotManager { - private var originalManager : WWRobotManager { - return WWRobotManager.shared() - } - - static let sharedInstance = RobotManager() - var allConnectedRobots : [Robot] { - return robotList(originalManager.allConnectedRobots) - } - - private func robotList(_ array : [Any]) -> [Robot] { - guard let robotArray = array as? [WWRobot] else { - return [Robot]() - } - return robotArray.map { (object) -> Robot in - return Robot(robotInterface: object) - } - } - - private init() {} - - func startScanningForRobots(scanPeriod : ScanPeriod) { - switch scanPeriod { - case .immediate: - originalManager.startScanning(forRobots: 0) - case .pause(let duration): - originalManager.startScanning(forRobots: duration) - } - } - - -} -class RobotEvent{ - init(event:WWEvent) { - // TODO: event conversion - } -} -class SensorSet { -} -class CommandSetSequence { -} -class CommandSet { - private let originalCommandSet : WWCommandSet - var wwCommandSet : WWCommandSet { - return originalCommandSet - } - init() { - originalCommandSet = WWCommandSet() - } -} - -protocol RobotObserver { - - func robot(_ robot: Robot, eventsTriggered events: [RobotEvent]) - func robot(_ robot: Robot, didReceiveRobotState state:SensorSet) - func robot(_ robot: Robot, didStopExecutingCommandSequence sequence:CommandSetSequence, - withResults results:[String:String]) - func robot(_ robot: Robot, didFinishCommandSequence sequence:CommandSetSequence) - -} - -class Robot : NSObject { - private let robotInterface : WWRobot - var observers = [RobotObserver]() - var robotType: RobotType { - let type = robotInterface.robotType.rawValue as Int - guard let robotType = RobotType(rawValue: type) else { - return RobotType.unknown - } - return robotType - } - var name : String { - return robotInterface.name as String - } - var uuid : NSUUID { - guard let uuid = NSUUID(uuidString: robotInterface.uuId) else { - return NSUUID() - } - return uuid - } - private override init() { - robotInterface = WWRobot() - } - - init(robotInterface : WWRobot) { - self.robotInterface = robotInterface - } - - func reset() { - robotInterface.resetState() - } - - func add(observer:RobotObserver) { - robotInterface.add(self) - observers.append(observer) - } - - func send(command cmd:CommandSet) { - robotInterface.send(cmd.wwCommandSet) - } -} - -extension Robot: WWRobotObserver { - func robot(_ robot: WWRobot!, eventsTriggered events: [Any]!) { - // TODO -// let robotEvents = eventsList(events: events) -// for observer in observers { -// observer.robot(robot:self, eventsTriggered:events) -// } - } - - func robot(_ robot: WWRobot!, didReceiveRobotState state: WWSensorSet!) { - // TODO - } - func robot(_ robot: WWRobot!, didFinishCommand sequence: WWCommandSetSequence!) { - // TODO - } - func robot(_ robot: WWRobot!, didStopExecutingCommand sequence: WWCommandSetSequence!, withResults results: [AnyHashable : Any]!) { - // TODO - } - - private func eventsList(events: [Any]) -> [RobotEvent] { - guard let eventArray = events as? [WWEvent] else { - return [RobotEvent]() - } - return eventArray.map({ (event) -> RobotEvent in - return RobotEvent(event: event) - }) - - } -} - - diff --git a/iOS/playground/RobotViewController.swift b/iOS/playground/RobotViewController.swift deleted file mode 100644 index 7b47fb7..0000000 --- a/iOS/playground/RobotViewController.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// RobotViewController.swift -// playground -// -// Created by Jelle Alten on 09-11-16. -// Copyright © 2016 Wonder Workshop. All rights reserved. -// - -import UIKit - -class RobotViewController: UIViewController { - var connectedRobots = [Robot]() - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - refreshConnectedRobots() - } - - func refreshConnectedRobots() { - connectedRobots = RobotManager.sharedInstance.allConnectedRobots - for robot in connectedRobots { - robot.add(observer:self) - } - } - - func sendCommandSetToRobots(cmd: CommandSet) { - for robot in connectedRobots { - robot.send(command: cmd) - } - } - - -} - -extension RobotViewController : RobotObserver { - func robot(_ robot: Robot, eventsTriggered events:[RobotEvent]) { - - } - func robot(_ robot: Robot, didReceiveRobotState state:SensorSet) { - - } - func robot(_ robot: Robot, didStopExecutingCommandSequence sequence:CommandSetSequence, - withResults results:[String:String]) { - - } - func robot(_ robot: Robot, didFinishCommandSequence sequence:CommandSetSequence) { - - } -} - - From b71c4116d9dab5c07470bb736a7935558bdd2818 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Sun, 13 Nov 2016 21:45:43 +0100 Subject: [PATCH 16/18] minor cleaning, better enums, higher supported iOS version --- .../Versions/A/Headers/WWConstants.h | 89 ++++++++++--------- iOS/playground.xcodeproj/project.pbxproj | 4 +- iOS/playground/RobotListViewController.swift | 10 +-- 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h index 4d01d76..3c56e40 100644 --- a/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h +++ b/iOS/libraries/WWRobotAPI.framework/Versions/A/Headers/WWConstants.h @@ -28,9 +28,11 @@ #define WW_LAUNCHER_POWER_MIN 0.0 #define WW_LAUNCHER_POWER_MAX 1.0 -typedef unsigned int WWLauncherReloadDirection; -#define WW_LAUNCHER_RELOAD_LEFT 1 -#define WW_LAUNCHER_RELOAD_RIGHT 2 + +typedef NS_ENUM(NSUInteger, WWLauncherReloadDirection) { + WW_LAUNCHER_RELOAD_LEFT = 1, + WW_LAUNCHER_RELOAD_RIGHT = 2 +}; // on-robot file syntax definitions #define WW_ON_ROBOT_DIR_LENGTH 4 @@ -84,39 +86,42 @@ typedef NS_ENUM(NSInteger, WWRobotType) { WW_ROBOT_DOT = 1002, }; -typedef unsigned int WWPersonalityColorIndex; -#define WW_PERSONALITY_COLOR_NONE 0 // aka white -#define WW_PERSONALITY_COLOR_YELLOW 1 // do not renumber these! -#define WW_PERSONALITY_COLOR_GREEN 2 -#define WW_PERSONALITY_COLOR_ORANGE 3 -#define WW_PERSONALITY_COLOR_BLUE 4 -#define WW_PERSONALITY_COLOR_RED 5 -#define WW_PERSONALITY_COLOR_PURPLE 6 -#define WW_PERSONALITY_COLOR_INVALID 255 - -typedef unsigned int WWRobotColorIndex; // same as WWPersonalityColorIndex with additional values. -#define WW_ROBOT_COLOR_WHITE WW_PERSONALITY_COLOR_NONE -#define WW_ROBOT_COLOR_YELLOW WW_PERSONALITY_COLOR_YELLOW -#define WW_ROBOT_COLOR_GREEN WW_PERSONALITY_COLOR_GREEN -#define WW_ROBOT_COLOR_ORANGE WW_PERSONALITY_COLOR_ORANGE -#define WW_ROBOT_COLOR_BLUE WW_PERSONALITY_COLOR_BLUE -#define WW_ROBOT_COLOR_RED WW_PERSONALITY_COLOR_RED -#define WW_ROBOT_COLOR_PURPLE WW_PERSONALITY_COLOR_PURPLE -#define WW_ROBOT_COLOR_BLUE2 (WW_PERSONALITY_COLOR_PURPLE + 1) -#define WW_ROBOT_COLOR_OFF (WW_PERSONALITY_COLOR_PURPLE + 2) -#define WW_ROBOT_COLOR_INVALID WW_PERSONALITY_COLOR_INVALID - - -typedef unsigned int WWPersonalityAnimationIndex; -#define WW_PERSONALITY_ANIMATION_NONE 0 -#define WW_PERSONALITY_ANIMATION_1 1 -#define WW_PERSONALITY_ANIMATION_2 2 -#define WW_PERSONALITY_ANIMATION_3 3 -#define WW_PERSONALITY_ANIMATION_INVALID 255 - -typedef unsigned int WWBeaconDataType; -#define WW_BEACON_DATA_TYPE_COLOR 0 -#define WW_BEACON_DATA_TYPE_USER 1 +typedef NS_ENUM(NSUInteger, WWPersonalityColorIndex) { + WW_PERSONALITY_COLOR_NONE = 0, // aka white + WW_PERSONALITY_COLOR_YELLOW = 1, // do not renumber these! + WW_PERSONALITY_COLOR_GREEN = 2, + WW_PERSONALITY_COLOR_ORANGE = 3, + WW_PERSONALITY_COLOR_BLUE = 4, + WW_PERSONALITY_COLOR_RED = 5, + WW_PERSONALITY_COLOR_PURPLE = 6, + WW_PERSONALITY_COLOR_INVALID = 255 +}; + +typedef NS_ENUM(NSUInteger, WWRobotColorIndex) { + WW_ROBOT_COLOR_WHITE = WW_PERSONALITY_COLOR_NONE, + WW_ROBOT_COLOR_YELLOW = WW_PERSONALITY_COLOR_YELLOW, + WW_ROBOT_COLOR_GREEN = WW_PERSONALITY_COLOR_GREEN, + WW_ROBOT_COLOR_ORANGE = WW_PERSONALITY_COLOR_ORANGE, + WW_ROBOT_COLOR_BLUE = WW_PERSONALITY_COLOR_BLUE, + WW_ROBOT_COLOR_RED = WW_PERSONALITY_COLOR_RED, + WW_ROBOT_COLOR_PURPLE = WW_PERSONALITY_COLOR_PURPLE, + WW_ROBOT_COLOR_BLUE2 = (WW_PERSONALITY_COLOR_PURPLE + 1), + WW_ROBOT_COLOR_OFF = (WW_PERSONALITY_COLOR_PURPLE + 2), + WW_ROBOT_COLOR_INVALID = WW_PERSONALITY_COLOR_INVALID +}; + +typedef NS_ENUM(NSUInteger, WWPersonalityAnimationIndex) { + WW_PERSONALITY_ANIMATION_NONE = 0, + WW_PERSONALITY_ANIMATION_1 = 1, + WW_PERSONALITY_ANIMATION_2 = 2, + WW_PERSONALITY_ANIMATION_3 = 3, + WW_PERSONALITY_ANIMATION_INVALID = 255, +}; + +typedef NS_ENUM(NSUInteger, WWBeaconDataType) { + WW_BEACON_DATA_TYPE_COLOR = 0, + WW_BEACON_DATA_TYPE_USER = 1 +}; #define WW_BEACON_RECEIVER_LEFT (1 << 0) #define WW_BEACON_RECEIVER_RIGHT (1 << 1) @@ -158,25 +163,25 @@ typedef unsigned int WWBeaconDataType; #define WW_COMMAND_VALUE_ORDER_INDEX __wwstr__"index" -typedef enum { +typedef NS_ENUM(NSUInteger, WWPoseMode) { WW_POSE_MODE_GLOBAL = 0, WW_POSE_MODE_RELATIVE_COMMAND, WW_POSE_MODE_RELATIVE_MEASURED, WW_POSE_MODE_SET_GLOBAL, WW_POSE_MODE_SET_TEMP_GLOBAL, WW_POSE_MODE_TEMP_GLOBAL -} WWPoseMode; +} ; -typedef enum { +typedef NS_ENUM(NSUInteger, WWPoseDirection) { WW_POSE_DIRECTION_FORWARD = 0, WW_POSE_DIRECTION_BACKWARD, WW_POSE_DIRECTION_INFERRED, -} WWPoseDirection; +} ; -typedef enum { +typedef NS_ENUM(NSUInteger, WWPoseWrap) { WW_POSE_WRAP_OFF = 0, WW_POSE_WRAP_ON, -} WWPoseWrap; +} ; #undef wwstr diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index 6f3ec13..d8a1429 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -471,7 +471,7 @@ ); GCC_PREFIX_HEADER = "playground/playground-prefix.pch"; INFOPLIST_FILE = playground/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 7.1; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; @@ -497,7 +497,7 @@ ); GCC_PREFIX_HEADER = "playground/playground-prefix.pch"; INFOPLIST_FILE = playground/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 7.1; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift index ad9b53e..3bc38a7 100644 --- a/iOS/playground/RobotListViewController.swift +++ b/iOS/playground/RobotListViewController.swift @@ -95,11 +95,11 @@ extension RobotListViewController { // robot info var detail = String() - detail = detail.appendingFormat("uuId: %@\n", robot.uuId ) - detail = detail.appendingFormat("Firmware %@\n", robot.firmwareVersion) - detail = detail.appendingFormat("Serial: %@\n", robot.serialNumber) - detail = detail.appendingFormat("RSSI %d dB\n", robot.signalStrength.intValue) - detail = detail.appendingFormat("Personality color: %d\n", robot.personalityColorIndex) + detail.append("uuId: \(robot.uuId)\n") + detail.append("Firmware \(robot.firmwareVersion)\n") + detail.append("Serial: \(robot.serialNumber)\n") + detail.append("RSSI \(robot.signalStrength.intValue) dB\n") + detail.append("Personality color: \(robot.personalityColorIndex)\n") cell.infoLabel.text = detail; // robot name From 2ebeb851bc28a499e506b8a1c185915d0ba05cf1 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Fri, 18 Nov 2016 22:46:05 +0100 Subject: [PATCH 17/18] Minor cleaning and adding of original joystick handling --- iOS/playground.xcodeproj/project.pbxproj | 10 +++---- iOS/playground/CircleJoystickView.swift | 8 +----- .../ControlHeadViewController.swift | 2 -- .../ControlSensorsViewController.swift | 2 -- .../CoordinatesHelper.swift} | 0 iOS/playground/JoystickView.swift | 27 ------------------- .../RobotControlPanelViewController.swift | 2 -- .../RobotControlViewController.swift | 5 ---- iOS/playground/RobotListTableViewCell.swift | 1 - iOS/playground/RobotListViewController.swift | 13 +-------- 10 files changed, 7 insertions(+), 63 deletions(-) rename iOS/{helper.swift => playground/CoordinatesHelper.swift} (100%) diff --git a/iOS/playground.xcodeproj/project.pbxproj b/iOS/playground.xcodeproj/project.pbxproj index d8a1429..28c7c14 100644 --- a/iOS/playground.xcodeproj/project.pbxproj +++ b/iOS/playground.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ 0A304C3D1A1A8619001A67B7 /* RobotListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0A304C3B1A1A8619001A67B7 /* RobotListTableViewCell.xib */; }; 0A412C581A6468110045A7B0 /* wiggle.json in Resources */ = {isa = PBXBuildFile; fileRef = 0A412C571A6468110045A7B0 /* wiggle.json */; }; 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */; }; + 604467961DDFAC5B00B4D2E8 /* CoordinatesHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604467951DDFAC5B00B4D2E8 /* CoordinatesHelper.swift */; }; 604F66F71DD4838800729682 /* RobotControlViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66F61DD4838800729682 /* RobotControlViewController.swift */; }; 604F66F91DD4885A00729682 /* ControlEyeRingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */; }; 604F66FB1DD494DA00729682 /* ControlDriveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */; }; @@ -30,7 +31,6 @@ 608033721DD5B28B0092A787 /* CircleJoystickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 608033711DD5B28B0092A787 /* CircleJoystickView.swift */; }; 608033B01DD5C04E0092A787 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 608033AF1DD5C04E0092A787 /* AppDelegate.swift */; }; 608033B21DD5E6880092A787 /* JoystickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 608033B11DD5E6880092A787 /* JoystickView.swift */; }; - 608033B41DD660220092A787 /* helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 608033B31DD660220092A787 /* helper.swift */; }; 608033B61DD67A730092A787 /* RobotListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 608033B51DD67A730092A787 /* RobotListTableViewCell.swift */; }; 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */; }; 60EB766F1DD4F95B00071F47 /* RobotControlPanelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60EB766E1DD4F95B00071F47 /* RobotControlPanelViewController.swift */; }; @@ -66,6 +66,7 @@ 0A412C571A6468110045A7B0 /* wiggle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = wiggle.json; sourceTree = ""; }; 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "playground-Bridging-Header.h"; sourceTree = ""; }; 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bridging-Helper.m"; sourceTree = ""; }; + 604467951DDFAC5B00B4D2E8 /* CoordinatesHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoordinatesHelper.swift; sourceTree = ""; }; 604F66F61DD4838800729682 /* RobotControlViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotControlViewController.swift; sourceTree = ""; }; 604F66F81DD4885A00729682 /* ControlEyeRingViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlEyeRingViewController.swift; sourceTree = ""; }; 604F66FA1DD494DA00729682 /* ControlDriveViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlDriveViewController.swift; sourceTree = ""; }; @@ -76,7 +77,6 @@ 608033711DD5B28B0092A787 /* CircleJoystickView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CircleJoystickView.swift; sourceTree = ""; }; 608033AF1DD5C04E0092A787 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 608033B11DD5E6880092A787 /* JoystickView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoystickView.swift; sourceTree = ""; }; - 608033B31DD660220092A787 /* helper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = helper.swift; sourceTree = ""; }; 608033B51DD67A730092A787 /* RobotListTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotListTableViewCell.swift; sourceTree = ""; }; 60E314E71DD46C1100EAF47A /* ControlLightsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlLightsViewController.swift; sourceTree = ""; }; 60EB766E1DD4F95B00071F47 /* RobotControlPanelViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RobotControlPanelViewController.swift; sourceTree = ""; }; @@ -108,7 +108,6 @@ 0A304BF31A1A6FEF001A67B7 = { isa = PBXGroup; children = ( - 608033B31DD660220092A787 /* helper.swift */, 0A304BFE1A1A6FEF001A67B7 /* playground */, 0A304C1B1A1A6FF0001A67B7 /* playgroundTests */, 0A304C5A1A1D12C4001A67B7 /* Frameworks */, @@ -128,7 +127,6 @@ 0A304BFE1A1A6FEF001A67B7 /* playground */ = { isa = PBXGroup; children = ( - 608033AF1DD5C04E0092A787 /* AppDelegate.swift */, 0A412C561A6468060045A7B0 /* animations */, 0A304C361A1A85D2001A67B7 /* controllers */, 0A304C351A1A85CB001A67B7 /* views */, @@ -136,6 +134,8 @@ 0A304C0F1A1A6FEF001A67B7 /* Images.xcassets */, 0A304C111A1A6FEF001A67B7 /* LaunchScreen.xib */, 0A304BFF1A1A6FEF001A67B7 /* Supporting Files */, + 608033AF1DD5C04E0092A787 /* AppDelegate.swift */, + 604467951DDFAC5B00B4D2E8 /* CoordinatesHelper.swift */, 6007A9011DD3BF24005DF517 /* playground-Bridging-Header.h */, 6007A9041DD3C9EF005DF517 /* Bridging-Helper.m */, 605653781DD47ED900354A88 /* ClassExtensions.swift */, @@ -323,7 +323,6 @@ files = ( 60E314E81DD46C1100EAF47A /* ControlLightsViewController.swift in Sources */, 604F66F71DD4838800729682 /* RobotControlViewController.swift in Sources */, - 608033B41DD660220092A787 /* helper.swift in Sources */, 608033721DD5B28B0092A787 /* CircleJoystickView.swift in Sources */, 0A304C081A1A6FEF001A67B7 /* RobotListViewController.swift in Sources */, 608033B21DD5E6880092A787 /* JoystickView.swift in Sources */, @@ -335,6 +334,7 @@ 604F66FD1DD4A80600729682 /* ControlHeadViewController.swift in Sources */, 604F67011DD4BAA900729682 /* ControlSoundViewController.swift in Sources */, 605653791DD47ED900354A88 /* ClassExtensions.swift in Sources */, + 604467961DDFAC5B00B4D2E8 /* CoordinatesHelper.swift in Sources */, 608033B01DD5C04E0092A787 /* AppDelegate.swift in Sources */, 6007A9051DD3C9EF005DF517 /* Bridging-Helper.m in Sources */, ); diff --git a/iOS/playground/CircleJoystickView.swift b/iOS/playground/CircleJoystickView.swift index 07489a3..16a7e8a 100644 --- a/iOS/playground/CircleJoystickView.swift +++ b/iOS/playground/CircleJoystickView.swift @@ -23,8 +23,7 @@ class CircleJoystickView: JoystickView { var power : Float { get { - return Float(abs(dotPos.y - _center.y)) / circleRadius -// return getDistance(dotPos, self._center) / circleRadius + return getDistance(p1: dotPos, p2: self._center) / circleRadius } } @@ -34,25 +33,20 @@ class CircleJoystickView: JoystickView { override func enforceJoystickBound(_ point: CGPoint) -> CGPoint? { -// super.enforceJoystickBound(point) let oldPoint = point let v1 = oldPoint.x - _center.x let v2 = oldPoint.y - _center.y let length = sqrt( v1 * v1 + v2 * v2) let maxLength = CGFloat(circleRadius) -// print("length \(length) maxlength \(maxLength) ") if length > maxLength { let phi = atan2(v2, v1) -// print("phi \(phi) ") let x = _center.x + maxLength * cos(phi) let y = _center.y + maxLength * sin(phi) return CGPoint(x: x, y: y) } else { return nil } - } - } extension CircleJoystickView { diff --git a/iOS/playground/ControlHeadViewController.swift b/iOS/playground/ControlHeadViewController.swift index b413539..e089320 100644 --- a/iOS/playground/ControlHeadViewController.swift +++ b/iOS/playground/ControlHeadViewController.swift @@ -33,6 +33,4 @@ class ControlHeadViewController: RobotControlViewController { command.setHeadPositionTilt(tilt, pan: pan) sendCommandSet(toRobots: command) } - - } diff --git a/iOS/playground/ControlSensorsViewController.swift b/iOS/playground/ControlSensorsViewController.swift index adf3de2..a6f7d6f 100644 --- a/iOS/playground/ControlSensorsViewController.swift +++ b/iOS/playground/ControlSensorsViewController.swift @@ -22,8 +22,6 @@ class ControlSensorsViewController: RobotControlViewController { override func viewDidLoad() { super.viewDidLoad() dataTableView.rowHeight = 200; - - // Do any additional setup after loading the view. } override func viewWillAppear(_ animated: Bool) { diff --git a/iOS/helper.swift b/iOS/playground/CoordinatesHelper.swift similarity index 100% rename from iOS/helper.swift rename to iOS/playground/CoordinatesHelper.swift diff --git a/iOS/playground/JoystickView.swift b/iOS/playground/JoystickView.swift index e8f612c..0de6b46 100644 --- a/iOS/playground/JoystickView.swift +++ b/iOS/playground/JoystickView.swift @@ -17,14 +17,6 @@ import UIKit @objc optional func joystickBounds() -> CGRect } -//extension JoystickDelegate { -// func onJoystickMoved() {} -// @objc func onJoystickSoftReleased() {} -// func onJoystickReleased() {} -// @objc func joystickSoftReturnDuration() -> Float { return 0.0 } -//} - - class JoystickView: UIView { enum JoystickType : UInt { @@ -82,11 +74,6 @@ class JoystickView: UIView { } -//} -// -// -//// MARK: Joystick Layout -//extension JoystickView { func updateDraggerImage() { let imgName : String switch type { @@ -112,18 +99,6 @@ class JoystickView: UIView { return CGPoint(x: clamp(point.x, bounds.origin.x, bounds.width), y: clamp(point.y, bounds.origin.y, bounds.height) ) } - -// // forbid the joystick dot going out of circle -// override func enforceJoystickBound(_ point: UnsafeMutablePointer!) { -// guard let bounds = joystickDelegate.joystickBounds?() else { -// return -// } -// let originalPoint = point.pointee -// point.pointee = CGPoint(x: clamp(originalPoint.x, bounds.origin.x, bounds.width), -// y: clamp(originalPoint.y, bounds.origin.y, bounds.height) ) -// -// } - } @@ -206,7 +181,6 @@ extension JoystickView { updateDraggerPosition() } - } @@ -214,7 +188,6 @@ extension JoystickView { extension JoystickView { - func updateDraggerPosition() { draggerImgView.center = dotPos; } diff --git a/iOS/playground/RobotControlPanelViewController.swift b/iOS/playground/RobotControlPanelViewController.swift index d38824c..f3d5f35 100644 --- a/iOS/playground/RobotControlPanelViewController.swift +++ b/iOS/playground/RobotControlPanelViewController.swift @@ -27,10 +27,8 @@ class RobotControlPanelViewController: UIViewController { let head = sb.instantiateViewController(withIdentifier: ControlHeadViewController.nameOfClass) as? RobotControlViewController, let sound = sb.instantiateViewController(withIdentifier: ControlSoundViewController.nameOfClass) as? RobotControlViewController, let sensor = sb.instantiateViewController(withIdentifier: ControlSensorsViewController.nameOfClass) as? RobotControlViewController { - controlsVC = [lights, eyeRing, drive, head, sound, sensor] - presentRobotControlsVC(vc: controlsVC.first!) } } diff --git a/iOS/playground/RobotControlViewController.swift b/iOS/playground/RobotControlViewController.swift index 530cfec..ff9bf1a 100644 --- a/iOS/playground/RobotControlViewController.swift +++ b/iOS/playground/RobotControlViewController.swift @@ -5,11 +5,6 @@ // import UIKit -protocol RobotControlViewController_P { - func refreshConnectedRobots() - func sendCommandSet(toRobots:WWCommandSet) -} - class RobotControlViewController : UIViewController { var connectedRobots = [WWRobot]() override func viewWillAppear(_ animated:Bool) { diff --git a/iOS/playground/RobotListTableViewCell.swift b/iOS/playground/RobotListTableViewCell.swift index 2c803cf..b34ad32 100644 --- a/iOS/playground/RobotListTableViewCell.swift +++ b/iOS/playground/RobotListTableViewCell.swift @@ -13,5 +13,4 @@ class RobotListTableViewCell: UITableViewCell { @IBOutlet var robotImageView : UIImageView! @IBOutlet var nameLabel : UILabel! @IBOutlet var infoLabel : UILabel! - } diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift index 3bc38a7..fe4b8ec 100644 --- a/iOS/playground/RobotListViewController.swift +++ b/iOS/playground/RobotListViewController.swift @@ -47,20 +47,11 @@ class RobotListViewController : UITableViewController { tableView.layer.insertSublayer(gradient, at:0) } -// func insertNewObject() { -// -// } - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { -// let indexPath = tableView.indexPathForSelectedRow if let controller = segue.destination as? RobotControlPanelViewController, let splitViewController = splitViewController { - if #available(iOS 8.0, *) { - controller.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem - } else { - // Fallback on earlier versions - } + controller.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem controller.navigationItem.leftItemsSupplementBackButton = true } } @@ -144,7 +135,6 @@ extension RobotListViewController : WWRobotManagerObserver { tableView.reloadData() } } - func manager(_ manager: WWRobotManager!, didUpdateDiscoveredRobots robot: WWRobot!) { tableView.reloadData() @@ -171,5 +161,4 @@ extension RobotListViewController : WWRobotManagerObserver { func manager(_ manager: WWRobotManager!, didDisconnectRobot robot: WWRobot!) { tableView.reloadData() } - } From d39f435b675e535d3b42ab55f530c0f205f07d86 Mon Sep 17 00:00:00 2001 From: Jelle Alten Date: Sat, 19 Nov 2016 17:27:26 +0100 Subject: [PATCH 18/18] Cleaning code based on swiftlint --- iOS/.swiftlint.yml | 3 + iOS/playground/AppDelegate.swift | 9 +- iOS/playground/CircleJoystickView.swift | 46 +++--- iOS/playground/ClassExtensions.swift | 3 +- .../ControlDriveViewController.swift | 77 +++++----- .../ControlEyeRingViewController.swift | 34 ++--- .../ControlHeadViewController.swift | 6 +- .../ControlLightsViewController.swift | 116 ++++++++------- .../ControlSensorsViewController.swift | 138 ++++++++++-------- .../ControlSoundViewController.swift | 47 +++--- iOS/playground/CoordinatesHelper.swift | 32 ++-- iOS/playground/JoystickView.swift | 117 +++++++-------- .../RobotControlPanelViewController.swift | 33 +++-- .../RobotControlViewController.swift | 13 +- iOS/playground/RobotListTableViewCell.swift | 8 +- iOS/playground/RobotListViewController.swift | 87 ++++++----- 16 files changed, 389 insertions(+), 380 deletions(-) create mode 100644 iOS/.swiftlint.yml diff --git a/iOS/.swiftlint.yml b/iOS/.swiftlint.yml new file mode 100644 index 0000000..48d625a --- /dev/null +++ b/iOS/.swiftlint.yml @@ -0,0 +1,3 @@ +line_length: 120 + + diff --git a/iOS/playground/AppDelegate.swift b/iOS/playground/AppDelegate.swift index 4b2e574..0e1f112 100644 --- a/iOS/playground/AppDelegate.swift +++ b/iOS/playground/AppDelegate.swift @@ -13,15 +13,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele var window: UIWindow? - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { - + func application(_ application: UIApplication, didFinishLaunchingWithOptions + launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + if let splitViewController = window?.rootViewController as? UISplitViewController { - splitViewController.delegate = self; + splitViewController.delegate = self } return true } - } - diff --git a/iOS/playground/CircleJoystickView.swift b/iOS/playground/CircleJoystickView.swift index 16a7e8a..c91708b 100644 --- a/iOS/playground/CircleJoystickView.swift +++ b/iOS/playground/CircleJoystickView.swift @@ -9,39 +9,38 @@ import UIKit class CircleJoystickView: JoystickView { - - private var _circleRadius : Float = 0.0 - let CIRCLE_RECT_PADDING : CGFloat = 10 - + private var _circleRadius: Float = 0.0 + let circleRectPadding: CGFloat = 10 + override func layoutSubviews() { super.layoutSubviews() - + // get radius for circle drawing - _circleRadius = Float( bounds.insetBy(dx: CIRCLE_RECT_PADDING, dy:CIRCLE_RECT_PADDING).size.width / CGFloat(2.0)) + let boundsInset = bounds.insetBy(dx: circleRectPadding, dy:circleRectPadding) + _circleRadius = Float( boundsInset.size.width / CGFloat(2.0)) } - - var power : Float { + + var power: Float { get { - return getDistance(p1: dotPos, p2: self._center) / circleRadius + return getDistance(point1: dotPos, point2: self.dragCenter) / circleRadius } } - + var circleRadius: Float { - return _circleRadius - Float(DOTSIZE) / 2.0 + return _circleRadius - Float(dotSize) / 2.0 } - override func enforceJoystickBound(_ point: CGPoint) -> CGPoint? { let oldPoint = point - let v1 = oldPoint.x - _center.x - let v2 = oldPoint.y - _center.y + let v1 = oldPoint.x - dragCenter.x + let v2 = oldPoint.y - dragCenter.y let length = sqrt( v1 * v1 + v2 * v2) let maxLength = CGFloat(circleRadius) if length > maxLength { let phi = atan2(v2, v1) - let x = _center.x + maxLength * cos(phi) - let y = _center.y + maxLength * sin(phi) + let x = dragCenter.x + maxLength * cos(phi) + let y = dragCenter.y + maxLength * sin(phi) return CGPoint(x: x, y: y) } else { return nil @@ -52,18 +51,17 @@ class CircleJoystickView: JoystickView { extension CircleJoystickView { override func getX() -> Float { - let src_min = Float(_center.x) - circleRadius - let src_max = Float(_center.x) + circleRadius - + let src_min = Float(dragCenter.x) - circleRadius + let src_max = Float(dragCenter.x) + circleRadius + return normalizeValueToNewRange(Float(dotPos.x), src_min, src_max, 1, -1) } - + override func getY() -> Float { - let src_min = Float(_center.y) - circleRadius - let src_max = Float(_center.y) + circleRadius - + let src_min = Float(dragCenter.y) - circleRadius + let src_max = Float(dragCenter.y) + circleRadius + return normalizeValueToNewRange(Float(dotPos.y), src_min, src_max, 1, -1) } - } diff --git a/iOS/playground/ClassExtensions.swift b/iOS/playground/ClassExtensions.swift index f83ff50..87f7027 100644 --- a/iOS/playground/ClassExtensions.swift +++ b/iOS/playground/ClassExtensions.swift @@ -8,8 +8,7 @@ import Foundation - -public extension NSObject{ +public extension NSObject { public class var nameOfClass: String { return String(describing: self) } diff --git a/iOS/playground/ControlDriveViewController.swift b/iOS/playground/ControlDriveViewController.swift index b8cf2a3..46a19d6 100644 --- a/iOS/playground/ControlDriveViewController.swift +++ b/iOS/playground/ControlDriveViewController.swift @@ -9,21 +9,20 @@ import Foundation import UIKit -class ControlDriveViewController : RobotControlViewController { +class ControlDriveViewController: RobotControlViewController { - let ROBOT_SPEED : Float = 50 + let robotSpeed: Float = 50 @IBOutlet weak var joystick: CircleJoystickView! @IBOutlet weak var toggleWiggleBtn: UIButton! - var isWiggling = false var isNodding = false - var wiggleAnimation : WWCommandSetSequence - var nodAnimation : WWCommandSetSequence - + var wiggleAnimation: WWCommandSetSequence + var nodAnimation: WWCommandSetSequence + @IBOutlet weak var poseLabel: UILabel! - + required init?(coder aDecoder: NSCoder) { wiggleAnimation = WWCommandSetSequence(fromFileInBundle: "wiggle", fileType: "json") nodAnimation = WWCommandSetSequence() @@ -35,34 +34,33 @@ class ControlDriveViewController : RobotControlViewController { nodAnimation.add(lookdown, withDuration: 0.4) nodAnimation.add(lookup, withDuration: 0.4) nodAnimation.add(lookdown, withDuration: 0.4) - + super.init(coder: aDecoder) } override func viewDidLoad() { super.viewDidLoad() - + joystick.type = .DRIVE_JOYSTICK joystick.updateDraggerImage() - + } @IBAction func toggleWiggle(_ sender: Any) { - let btnText = isWiggling ? "Start wiggle" : "Stop wiggle" + let btnText = isWiggling ? "Start wiggle": "Stop wiggle" for robot in connectedRobots { if isWiggling { robot.stopCommand(wiggleAnimation) - } - else { + } else { // always start from the beginning of animation robot.executeCommand(wiggleAnimation, withOptions: nil) } } - + toggleWiggleBtn.setTitle(btnText, for: .normal) isWiggling = !isWiggling } - + @IBAction func executeNod(_ sender: Any) { if !isNodding && !isWiggling { for robot in connectedRobots { @@ -72,44 +70,42 @@ class ControlDriveViewController : RobotControlViewController { } } - -extension ControlDriveViewController : JoystickDelegate { +extension ControlDriveViewController: JoystickDelegate { func onJoystickMoved() { let cmd = commandFromJoystick() sendCommandSet(toRobots: cmd) } - + func onJoystickReleased() { if let stopCmd = WWCommandToolbelt.moveStop() { sendCommandSet(toRobots: stopCmd) } } - - + private func commandFromJoystick() -> WWCommandSet { let cmdToSend = WWCommandSet() - + // use linear angular! - let joystickPower = joystick.power; - let lin = joystick.getY() * ROBOT_SPEED + let joystickPower = joystick.power + let lin = joystick.getY() * robotSpeed var ang = joystick.getX() * joystickPower * 3.14159 * 2.0 - - //NSLog(@"sending linear: %5.2f angular: %5.2f", lin, ang); - + + //NSLog(@"sending linear: %5.2f angular: %5.2f", lin, ang) + // fudge factor to be tweaked - ang *= 0.5; + ang *= 0.5 if lin < 0 { - ang *= -1.0; + ang *= -1.0 } - + let linAng = WWCommandBodyLinearAngular(linear: Double(lin), angular: Double(ang)) cmdToSend.setBodyLinearAngular(linAng) - return cmdToSend; + return cmdToSend } } -extension ControlDriveViewController { // : WWRobotObserver { +extension ControlDriveViewController { //: WWRobotObserver { func robot(_ robot: WWRobot!, didFinishCommand sequence: WWCommandSetSequence!) { if sequence == wiggleAnimation { if isWiggling { @@ -123,9 +119,10 @@ extension ControlDriveViewController { // : WWRobotObserver { } } } - - func robot(_ robot: WWRobot!, didStopExecutingCommand sequence: WWCommandSetSequence!, withResults results: [AnyHashable : Any]!) { - print("wiggle sequence terminated by user."); + + func robot(_ robot: WWRobot!, didStopExecutingCommand sequence: WWCommandSetSequence!, + withResults results: [AnyHashable: Any]!) { + print("wiggle sequence terminated by user.") for robot in connectedRobots { robot.resetState() } @@ -134,18 +131,18 @@ extension ControlDriveViewController { // : WWRobotObserver { extension ControlDriveViewController { @IBAction func didChangePoseDistanceValue(_ sender: UISlider) { - - poseLabel.text = "\(Int(sender.value))"; + + poseLabel.text = "\(Int(sender.value))" } - + @IBAction func MoveDashByFixedDistance(_ sender: Any) { - + let cmdToSend = WWCommandSet() let distance = Double(poseLabel.text ?? "0") ?? 0.0 - + let bodyPose = WWCommandBodyPose(relativeMeasuredX:distance, y:0.0, radians:0.0, time:2.0) cmdToSend.setBodyPose(bodyPose) sendCommandSet(toRobots: cmdToSend) } - + } diff --git a/iOS/playground/ControlEyeRingViewController.swift b/iOS/playground/ControlEyeRingViewController.swift index c19d81d..d478cb3 100644 --- a/iOS/playground/ControlEyeRingViewController.swift +++ b/iOS/playground/ControlEyeRingViewController.swift @@ -9,8 +9,8 @@ import Foundation import UIKit -class ControlEyeRingViewController : RobotControlViewController { - +class ControlEyeRingViewController: RobotControlViewController { + @IBOutlet weak var eye0Switch: UISwitch! @IBOutlet weak var eye1Switch: UISwitch! @IBOutlet weak var eye2Switch: UISwitch! @@ -23,20 +23,20 @@ class ControlEyeRingViewController : RobotControlViewController { @IBOutlet weak var eye9Switch: UISwitch! @IBOutlet weak var eye10Switch: UISwitch! @IBOutlet weak var eye11Switch: UISwitch! - + @IBOutlet weak var eyeBrightnessSlider: UISlider! @IBOutlet weak var eyeShowButton: UIButton! - + private var eyeShowInProgress = false private var eyeShowTimer = Timer() - private var eyeShowValueState : UInt = 0 + private var eyeShowValueState: UInt = 0 @IBAction func setEyeRing(_ sender: Any) { setEyeRing() } - + @IBAction func setEyeRing() { - + let eyeBitmap = [eye0Switch.isOn, eye1Switch.isOn, eye2Switch.isOn, @@ -56,26 +56,26 @@ class ControlEyeRingViewController : RobotControlViewController { sendCommandSet(toRobots: cmd) } - @IBAction func toggleEyeShow(_ sender: UIButton) { if eyeShowInProgress { eyeShowInProgress = false eyeShowTimer.invalidate() eyeShowButton.setTitle("Start eye show", for:.normal) - } - else { + } else { eyeShowInProgress = true - eyeShowTimer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(eyeShowTimerValues(timer:)), userInfo: nil, repeats: true) + eyeShowTimer = Timer.scheduledTimer(timeInterval: 0.4, target: self, + selector: #selector(eyeShowTimerValues(timer:)), + userInfo: nil, repeats: true) eyeShowTimer.fire() eyeShowButton.setTitle("Stop eye show", for:.normal) } - + } - + @objc func eyeShowTimerValues(timer: Timer) { - + eyeShowValueState = (eyeShowValueState+1) % 12; // rotate - + eye0Switch.isOn = ((0 + eyeShowValueState) % 2) == 0 eye1Switch.isOn = ((1 + eyeShowValueState) % 2) == 0 eye2Switch.isOn = ((2 + eyeShowValueState) % 2) == 0 @@ -88,13 +88,12 @@ class ControlEyeRingViewController : RobotControlViewController { eye9Switch.isOn = ((9 + eyeShowValueState) % 2) == 0 eye10Switch.isOn = ((10 + eyeShowValueState) % 2) == 0 eye11Switch.isOn = ((11 + eyeShowValueState) % 2) == 0 - + let newValue = Double(eyeShowValueState) / 11.0 eyeBrightnessSlider.value = Float(newValue) setEyeRing() } - @IBAction func runEyeAnimation(_ sender: Any) { let cmd = WWCommandSet() let eye = WWCommandEyeRing(animation: WW_EYEANIM_FULL_BLINK) @@ -103,5 +102,4 @@ class ControlEyeRingViewController : RobotControlViewController { } - } diff --git a/iOS/playground/ControlHeadViewController.swift b/iOS/playground/ControlHeadViewController.swift index e089320..59d52e5 100644 --- a/iOS/playground/ControlHeadViewController.swift +++ b/iOS/playground/ControlHeadViewController.swift @@ -9,7 +9,7 @@ import UIKit class ControlHeadViewController: RobotControlViewController { - + @IBAction func onMiddleBtnTouch(_ sender: Any) { updateRobotHead(pan: 0, tilt: 0) } @@ -25,8 +25,8 @@ class ControlHeadViewController: RobotControlViewController { @IBAction func onRightBtnTouch(_ sender: Any) { updateRobotHead(pan: -90, tilt: 0) } - - private func updateRobotHead(pan panDegree:Float, tilt tiltDegree: Float) { + + private func updateRobotHead(pan panDegree: Float, tilt tiltDegree: Float) { let command = WWCommandSet() let tilt = WWCommandHeadPosition(degree: Double(tiltDegree)) let pan = WWCommandHeadPosition(degree: Double(panDegree)) diff --git a/iOS/playground/ControlLightsViewController.swift b/iOS/playground/ControlLightsViewController.swift index 12afac3..5f322cc 100644 --- a/iOS/playground/ControlLightsViewController.swift +++ b/iOS/playground/ControlLightsViewController.swift @@ -9,46 +9,55 @@ import UIKit class ControlLightsViewController: RobotControlViewController { - + @IBOutlet weak var leftEarRSlider: UISlider! @IBOutlet weak var leftEarGSlider: UISlider! @IBOutlet weak var leftEarBSlider: UISlider! - + @IBOutlet weak var chestRSlider: UISlider! @IBOutlet weak var chestGSlider: UISlider! @IBOutlet weak var chestBSlider: UISlider! - + @IBOutlet weak var rightEarRSlider: UISlider! @IBOutlet weak var rightEarGSlider: UISlider! @IBOutlet weak var rightEarBSlider: UISlider! - + @IBOutlet weak var mainButtonMonoSlider: UISlider! - + @IBOutlet weak var backMonoSlider: UISlider! - + @IBOutlet weak var lightShowButton: UIButton! - + private var lightShowInProgress = false private var lightShowTimer = Timer() - private var lightShowValueState : UInt = 0 - - + private var lightShowValueState: UInt = 0 + @IBAction func setRGBLights(_ sender: UISlider) { setRGBLights() } - + @IBAction func setRGBLights() { - + let cmd = WWCommandSet() - cmd.setLeftEarLight(WWCommandLightRGB(red: Double(leftEarRSlider.value), green: Double(leftEarGSlider.value), blue: Double(leftEarBSlider.value))) - cmd.setRightEarLight(WWCommandLightRGB(red: Double(rightEarRSlider.value), green: Double(rightEarGSlider.value), blue: Double(rightEarBSlider.value))) - - cmd.setChestLight(WWCommandLightRGB(red: Double(chestRSlider.value), green: Double(chestGSlider.value), blue: Double(chestBSlider.value))) - - cmd.setChestLight(WWCommandLightRGB(red: Double(chestRSlider.value), green: Double(chestGSlider.value), blue: Double(chestBSlider.value))) - - cmd.setEyeLight(WWCommandLightRGB(red: Double(chestRSlider.value), green: Double(chestGSlider.value), blue: Double(chestBSlider.value))) - + cmd.setLeftEarLight(WWCommandLightRGB(red: Double(leftEarRSlider.value), + green: Double(leftEarGSlider.value), + blue: Double(leftEarBSlider.value))) + cmd.setRightEarLight(WWCommandLightRGB(red: Double(rightEarRSlider.value), + green: Double(rightEarGSlider.value), + blue: Double(rightEarBSlider.value))) + + cmd.setChestLight(WWCommandLightRGB(red: Double(chestRSlider.value), + green: Double(chestGSlider.value), + blue: Double(chestBSlider.value))) + + cmd.setChestLight(WWCommandLightRGB(red: Double(chestRSlider.value), + green: Double(chestGSlider.value), + blue: Double(chestBSlider.value))) + + cmd.setEyeLight(WWCommandLightRGB(red: Double(chestRSlider.value), + green: Double(chestGSlider.value), + blue: Double(chestBSlider.value))) + cmd.setMainButtonLight(WWCommandLightMono(brightness: Double(mainButtonMonoSlider.value))) cmd.setTailLight(WWCommandLightMono(brightness: Double(backMonoSlider.value))) @@ -59,51 +68,52 @@ class ControlLightsViewController: RobotControlViewController { lightShowInProgress = false lightShowTimer.invalidate() lightShowButton.setTitle("Start light show", for:.normal) - } - else { + } else { lightShowInProgress = true - lightShowTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.lightShowTimerValues(timer:)), userInfo: nil, repeats: true) + lightShowTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, + selector: #selector(self.lightShowTimerValues(timer:)), + userInfo: nil, repeats: true) lightShowTimer.fire() lightShowButton.setTitle("Stop light show", for:.normal) } - + } - + @objc func lightShowTimerValues(timer: Timer) { - - var r, g, b, mono : Float - + + var r, g, b, mono: Float + lightShowValueState = (lightShowValueState+1) % 4; // rotate - switch (lightShowValueState) { + switch lightShowValueState { case 0: - r = 1.0; g = 0; b = 0; mono = 1.0; - break; + r = 1.0; g = 0; b = 0; mono = 1.0 + break case 1: - r = 0; g = 1.0; b = 0; mono = 0.5; - break; + r = 0; g = 1.0; b = 0; mono = 0.5 + break case 2: - r = 0; g = 0; b = 1.0; mono = 0; - break; + r = 0; g = 0; b = 1.0; mono = 0 + break case 3: - r = 0.5; g = 0.5; b = 0.5; mono = 0.75; - break; + r = 0.5; g = 0.5; b = 0.5; mono = 0.75 + break default: - r = 0; g = 0; b = 0; mono = 0; - break; + r = 0; g = 0; b = 0; mono = 0 + break } - - leftEarRSlider.value = r; - leftEarGSlider.value = g; - leftEarBSlider.value = b; - rightEarRSlider.value = r; - rightEarGSlider.value = g; - rightEarBSlider.value = b; - chestRSlider.value = r; - chestGSlider.value = g; - chestBSlider.value = b; - mainButtonMonoSlider.value = mono; - backMonoSlider.value = mono; - + + leftEarRSlider.value = r + leftEarGSlider.value = g + leftEarBSlider.value = b + rightEarRSlider.value = r + rightEarGSlider.value = g + rightEarBSlider.value = b + chestRSlider.value = r + chestGSlider.value = g + chestBSlider.value = b + mainButtonMonoSlider.value = mono + backMonoSlider.value = mono + setRGBLights() } diff --git a/iOS/playground/ControlSensorsViewController.swift b/iOS/playground/ControlSensorsViewController.swift index a6f7d6f..5a71242 100644 --- a/iOS/playground/ControlSensorsViewController.swift +++ b/iOS/playground/ControlSensorsViewController.swift @@ -9,24 +9,26 @@ import UIKit // Declare radiansToDegrees function -func radiansToDegrees (radians: Double)->Double { +func radiansToDegrees (radians: Double) -> Double { return radians * 180 / M_PI } class ControlSensorsViewController: RobotControlViewController { - + private var refreshDataTimer = Timer() @IBOutlet weak var dataTableView: UITableView! - + override func viewDidLoad() { super.viewDidLoad() - dataTableView.rowHeight = 200; + dataTableView.rowHeight = 200 } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - refreshDataTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.refreshSensorData(timer:)), userInfo: nil, repeats: true) + refreshDataTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, + selector: #selector(self.refreshSensorData(timer:)), + userInfo: nil, repeats: true) } override func viewWillDisappear(_ animated: Bool) { @@ -37,45 +39,45 @@ class ControlSensorsViewController: RobotControlViewController { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - - func refreshSensorData(timer : Timer) { + + func refreshSensorData(timer: Timer) { dataTableView.reloadData() } } -extension ControlSensorsViewController : UITableViewDataSource { - +extension ControlSensorsViewController: UITableViewDataSource { + func numberOfSections(in tableView: UITableView) -> Int { return 1 } - + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return connectedRobots.count } - + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) let robot = connectedRobots[indexPath.row] let sensorData = robot.history.currentState() as WWSensorSet - + var detail = String() detail.append("Name: \(robot.name)\n") - + if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_MAIN) as? WWSensorButton { - detail.append("Btn Main: \(button.isPressed ? "true" : "false"), ") + detail.append("Btn Main: \(button.isPressed ? "true": "false"), ") } if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_1) as? WWSensorButton { - detail.append("Btn 1: \(button.isPressed ? "true" : "false"), ") + detail.append("Btn 1: \(button.isPressed ? "true": "false"), ") } if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_2) as? WWSensorButton { - detail.append("Btn 2: \(button.isPressed ? "true" : "false"), ") + detail.append("Btn 2: \(button.isPressed ? "true": "false"), ") } if let button = sensorData.sensor(forIndex: .SENSOR_BUTTON_3) as? WWSensorButton { - detail.append("Btn 3: \(button.isPressed ? "true" : "false")") + detail.append("Btn 3: \(button.isPressed ? "true": "false")") } detail.append("\n") - + let sAcc = sensorData.sensor(forIndex: .SENSOR_ACCELEROMETER) as? WWSensorAccelerometer //accelerometer detail.append("Accel: ") @@ -85,60 +87,68 @@ extension ControlSensorsViewController : UITableViewDataSource { detail = detail.appendingFormat("Z: %3.2f", sAcc.z) } detail.append("\n") - + let sMic = sensorData.sensor(forIndex: .SENSOR_MICROPHONE) as? WWSensorMicrophone detail.append("Microphone: ") if let sMic = sMic { detail = detail.appendingFormat("Amplitude: %03f, ", sMic.amplitude) - detail = detail.appendingFormat("Angle: %3.2f degrees", radiansToDegrees(radians: sMic.triangulationAngle)) + let degrees = radiansToDegrees(radians: sMic.triangulationAngle) + detail = detail.appendingFormat("Angle: %3.2f degrees", degrees) } detail.append("\n") - - if (robot.robotType == .ROBOT_DASH) { - //distance sensors - let sDistFLF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_FRONT_RIGHT_FACING) as? WWSensorDistance - let sDistFRF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_FRONT_LEFT_FACING) as? WWSensorDistance - let sDistRRF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_BACK) as? WWSensorDistance - detail.append("Distance: ") - if let sDistFLF = sDistFLF, let sDistFRF = sDistFRF, let sDistRRF = sDistRRF { - detail = detail.appendingFormat("Left-Facing: %2.2f, ", sDistFLF.reflectance) - detail = detail.appendingFormat("Dist Right-Facing: %2.2f, ", sDistFRF.reflectance) - detail = detail.appendingFormat("Dist Tail: %2.2f", sDistRRF.reflectance) - } - detail.append("\n") - - //gyro - detail.append("Gyro: ") - if let sGyro = sensorData.sensor(forIndex: .SENSOR_GYROSCOPE) as? WWSensorGyroscope { - detail = detail.appendingFormat("yaw: %3.2f, ", sGyro.yaw) - detail = detail.appendingFormat("pitch: %3.2f, ", sGyro.pitch) - detail = detail.appendingFormat("roll: %3.2f", sGyro.roll) - } - detail.append("\n") - - //head - detail.append("Head: ") - if let sMotorServoHeadPan = sensorData.sensor(forIndex: .SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition, - let sMotorServoHeadTilt = sensorData.sensor(forIndex: .SENSOR_HEAD_POSITION_TILT) as? WWSensorHeadPosition { - detail = detail.appendingFormat("Pan: %3.2f degrees, ", radiansToDegrees(radians:sMotorServoHeadPan.radians)) - detail = detail.appendingFormat("Tilt: %3.2f degrees", radiansToDegrees(radians:sMotorServoHeadTilt.radians)) - } - detail.append("\n") - - //encoder - detail.append("Encoder: ") - if let sEncoderLW = sensorData.sensor(forIndex: .SENSOR_ENCODER_LEFT_WHEEL) as? WWSensorEncoder, - let sEncoderRW = sensorData.sensor(forIndex: .SENSOR_ENCODER_RIGHT_WHEEL) as? WWSensorEncoder { - - detail = detail.appendingFormat("Left: %4.2f cm, ", sEncoderLW.distance) - detail = detail.appendingFormat("Right: %4.2f cm", sEncoderRW.distance) - } - detail.append("\n") + + if robot.robotType == .ROBOT_DASH { + detail.append(getDashDetail(sensorData:sensorData)) } - - cell.textLabel?.text = detail; + + cell.textLabel?.text = detail //[cell.textLabel sizeToFit) - return cell; + return cell + } + + func getDashDetail(sensorData: WWSensorSet) -> String { + var detail = "" + + //distance sensors + let sDistFLF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_FRONT_RIGHT_FACING) as? WWSensorDistance + let sDistFRF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_FRONT_LEFT_FACING) as? WWSensorDistance + let sDistRRF = sensorData.sensor(forIndex: .SENSOR_DISTANCE_BACK) as? WWSensorDistance + detail.append("Distance: ") + if let sDistFLF = sDistFLF, let sDistFRF = sDistFRF, let sDistRRF = sDistRRF { + detail = detail.appendingFormat("Left-Facing: %2.2f, ", sDistFLF.reflectance) + detail = detail.appendingFormat("Dist Right-Facing: %2.2f, ", sDistFRF.reflectance) + detail = detail.appendingFormat("Dist Tail: %2.2f", sDistRRF.reflectance) + } + detail.append("\n") + + //gyro + detail.append("Gyro: ") + if let sGyro = sensorData.sensor(forIndex: .SENSOR_GYROSCOPE) as? WWSensorGyroscope { + detail = detail.appendingFormat("yaw: %3.2f, ", sGyro.yaw) + detail = detail.appendingFormat("pitch: %3.2f, ", sGyro.pitch) + detail = detail.appendingFormat("roll: %3.2f", sGyro.roll) + } + detail.append("\n") + + //head + detail.append("Head: ") + if let sMotorServoHeadPan = sensorData.sensor(forIndex: .SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition, + let sMotorServoHeadTilt = sensorData.sensor(forIndex: .SENSOR_HEAD_POSITION_TILT) as? WWSensorHeadPosition { + detail = detail.appendingFormat("Pan: %3.2f degrees, ", sMotorServoHeadPan.degrees) + detail = detail.appendingFormat("Tilt: %3.2f degrees", sMotorServoHeadTilt.degrees) + } + detail.append("\n") + + //encoder + detail.append("Encoder: ") + if let sEncoderLW = sensorData.sensor(forIndex: .SENSOR_ENCODER_LEFT_WHEEL) as? WWSensorEncoder, + let sEncoderRW = sensorData.sensor(forIndex: .SENSOR_ENCODER_RIGHT_WHEEL) as? WWSensorEncoder { + + detail = detail.appendingFormat("Left: %4.2f cm, ", sEncoderLW.distance) + detail = detail.appendingFormat("Right: %4.2f cm", sEncoderRW.distance) + } + detail.append("\n") + return detail } } diff --git a/iOS/playground/ControlSoundViewController.swift b/iOS/playground/ControlSoundViewController.swift index c626d00..604b85d 100644 --- a/iOS/playground/ControlSoundViewController.swift +++ b/iOS/playground/ControlSoundViewController.swift @@ -10,8 +10,8 @@ import UIKit class ControlSoundViewController: RobotControlViewController { - private var _volume : Double = (WW_VOLUME_MAX - WW_VOLUME_MUTE) / 2.0 - var volume : Double { + private var _volume: Double = (WW_VOLUME_MAX - WW_VOLUME_MUTE) / 2.0 + var volume: Double { set { _volume = newValue volumeSlider.value = Float(newValue) @@ -20,34 +20,35 @@ class ControlSoundViewController: RobotControlViewController { return _volume } } - var mainButtonTouchUp : WWEvent - var headPanPositionChanged : WWEvent + var mainButtonTouchUp: WWEvent + var headPanPositionChanged: WWEvent + + @IBOutlet weak var volumeSlider: UISlider! - @IBOutlet weak var volumeSlider : UISlider! - required init?(coder aDecoder: NSCoder) { - + let buttonNumber = NSNumber(value:1000) mainButtonTouchUp = WWEventToolbelt.button(onUp: buttonNumber) headPanPositionChanged = WWEvent(shouldAlert: { (event, history) -> Bool in guard let history = history, let state = history.currentState(), - let currHeadPan = state.sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition, - let prevHeadPan300ms = history.pastState(atTimeAgo:0.3).sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition else { + let currHeadPan = state.sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition, + let pastHeadPanSensor = history.pastState(atTimeAgo:0.3).sensor(forIndex:.SENSOR_HEAD_POSITION_PAN), + let prevHeadPan300ms = pastHeadPanSensor as? WWSensorHeadPosition else { return false } let diff = currHeadPan.radians - prevHeadPan300ms.radians return fabs(diff) > 0.05 // allow for some errors - + }, identifier: "head") - + super.init(coder: aDecoder) } - + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - + volumeSlider.value = Float(volume) - + for robot in connectedRobots { robot.add(mainButtonTouchUp) robot.add(headPanPositionChanged) @@ -59,7 +60,7 @@ class ControlSoundViewController: RobotControlViewController { robot.removeAllEvents() } } - + @IBAction func playSoundPressed(_ sender: Any) { let speakerCommand = WWCommandSet() if let speaker = WWCommandSpeaker(defaultSound:WW_SOUNDFILE_HI) { @@ -70,16 +71,18 @@ class ControlSoundViewController: RobotControlViewController { } } - // MARK: WWRobotObserver -extension ControlSoundViewController { +extension ControlSoundViewController { func robot(_ robot: WWRobot!, eventsTriggered events: [Any]!) { - for event in events as! [WWEvent] { + guard let wwEvents = events as? [WWEvent] else { + return + } + for event in wwEvents as [WWEvent] { if mainButtonTouchUp == event { - self.volume = (volume == WW_VOLUME_MUTE) ? WW_VOLUME_MAX : WW_VOLUME_MUTE - } - else if event.hasIdentifier("head") { - if let headPan = robot.history.currentState().sensor(forIndex:.SENSOR_HEAD_POSITION_PAN) as? WWSensorHeadPosition { + self.volume = (volume == WW_VOLUME_MUTE) ? WW_VOLUME_MAX: WW_VOLUME_MUTE + } else if event.hasIdentifier("head") { + if let sensor = robot.history.currentState().sensor(forIndex:.SENSOR_HEAD_POSITION_PAN), + let headPan = sensor as? WWSensorHeadPosition { volume = headPan.radians / -2.0 } } diff --git a/iOS/playground/CoordinatesHelper.swift b/iOS/playground/CoordinatesHelper.swift index 8c4e35a..3c6835c 100644 --- a/iOS/playground/CoordinatesHelper.swift +++ b/iOS/playground/CoordinatesHelper.swift @@ -9,30 +9,28 @@ import Foundation import UIKit - -func clamp(_ d: CGFloat , _ min : CGFloat ,_ max : CGFloat) -> CGFloat { - let t = d < min ? min : d; - return t > max ? max : t; +func clamp(_ input: CGFloat, _ min: CGFloat, _ max: CGFloat) -> CGFloat { + let t = input < min ? min: input + return t > max ? max: t } -func lerp(_ min: CGFloat , _ max: CGFloat, _ T: CGFloat) -> CGFloat -{ - return min + (max - min) * T; +func lerp(_ min: CGFloat, _ max: CGFloat, _ dist: CGFloat) -> CGFloat { + return min + (max - min) * dist } -func getDistance(p1: CGPoint , p2: CGPoint) -> Float { - return hypotf(Float(p1.x - p2.x), Float(p1.y - p2.y)) +func getDistance(point1: CGPoint, point2: CGPoint) -> Float { + return hypotf(Float(point1.x - point2.x), Float(point1.y - point2.y)) } // translate from old coordinate system into new one -func normalizeValueToNewRange(_ value: Float, _ src_min: Float, _ src_max: Float, _ dst_min: Float, _ dst_max: Float) -> Float -{ - return ( value - src_min ) / ( src_max - src_min ) * ( dst_max - dst_min ) + dst_min +func normalizeValueToNewRange(_ value: Float, + _ srcMin: Float, _ srcMax: Float, + _ dstMin: Float, _ dstMax: Float) -> Float { + return ( value - srcMin ) / ( srcMax - srcMin ) * ( dstMax - dstMin ) + dstMin } -func lerpPoints(min: CGPoint , max: CGPoint , T: Double) -> CGPoint -{ - let result = CGPoint(x: lerp(min.x, max.x, CGFloat(T)), - y: lerp(min.y, max.y, CGFloat(T)) ) - return result; +func lerpPoints(min: CGPoint, max: CGPoint, dist: Double) -> CGPoint { + let result = CGPoint(x: lerp(min.x, max.x, CGFloat(dist)), + y: lerp(min.y, max.y, CGFloat(dist)) ) + return result } diff --git a/iOS/playground/JoystickView.swift b/iOS/playground/JoystickView.swift index 0de6b46..16f728a 100644 --- a/iOS/playground/JoystickView.swift +++ b/iOS/playground/JoystickView.swift @@ -8,39 +8,38 @@ import UIKit - @objc protocol JoystickDelegate { @objc optional func onJoystickMoved() @objc optional func onJoystickSoftReleased() @objc optional func onJoystickReleased() - @objc optional func joystickSoftReturnDuration() -> Float // max time for "return center" method which is called after user releases joystick + @objc optional func joystickSoftReturnDuration() -> Float // max time for return center after user releases joystick @objc optional func joystickBounds() -> CGRect } class JoystickView: UIView { - - enum JoystickType : UInt { + + enum JoystickType: UInt { case DRIVE_JOYSTICK case HEAD_JOYSTICK } - let DRIVE_DRAGGER_IMAGE = "RC_drive_joystick" - let HEAD_DRAGGER_IMAGE = "RC_head_joystick" - let DOTSIZE : CGFloat = 90 - let TIMER_RETURN_UPDATE_RATE : Float = 30.0 // delegates calls update rate ( affects robot update state ) - - var type : JoystickType = .DRIVE_JOYSTICK - var draggerImgView : UIImageView = UIImageView() - - @IBOutlet weak var joystickDelegate : JoystickDelegate! + let driveDraggerImage = "RC_drive_joystick" + let headDraggerImage = "RC_head_joystick" + let dotSize: CGFloat = 90 + let timerReturnUpdateRate: Float = 30.0 // delegates calls update rate ( affects robot update state ) + + var type: JoystickType = .DRIVE_JOYSTICK + var draggerImgView: UIImageView = UIImageView() + + @IBOutlet weak var joystickDelegate: JoystickDelegate! - var returnTimer : Timer? - var currentReturnRatio : Float = 0.0 - var returnTime : Float = 0.0 + var returnTimer: Timer? + var currentReturnRatio: Float = 0.0 + var returnTime: Float = 0.0 - var _center : CGPoint = CGPoint() + var dragCenter: CGPoint = CGPoint() private var _dotPos = CGPoint() - var dotPos : CGPoint { + var dotPos: CGPoint { set { self._dotPos = newValue joystickDelegate.onJoystickMoved?() @@ -49,60 +48,57 @@ class JoystickView: UIView { return self._dotPos } } - + required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } - + override init(frame: CGRect) { super.init(frame: frame) commonInit() } - + func commonInit() { contentMode = .scaleAspectFit autoresizingMask = UIViewAutoresizing(rawValue: 0) self.isUserInteractionEnabled = true } - + override func layoutSubviews() { super.layoutSubviews() - _center = CGPoint(x:bounds.width / 2.0, y: bounds.height / 2.0) - dotPos = _center - self.updateDraggerPosition() + dragCenter = CGPoint(x:bounds.width / 2.0, y: bounds.height / 2.0) + dotPos = dragCenter + self.updateDraggerPosition() } - func updateDraggerImage() { - let imgName : String + let imgName: String switch type { case .DRIVE_JOYSTICK: - imgName = DRIVE_DRAGGER_IMAGE + imgName = driveDraggerImage case .HEAD_JOYSTICK: - imgName = HEAD_DRAGGER_IMAGE + imgName = headDraggerImage } let img = UIImage(named: imgName) draggerImgView = UIImageView(image: img) - draggerImgView.frame = CGRect(x: 0.0, y: 0.0, width: DOTSIZE, height: DOTSIZE) + draggerImgView.frame = CGRect(x: 0.0, y: 0.0, width: dotSize, height: dotSize) draggerImgView.center = dotPos print("adding the dragger") addSubview(draggerImgView) } - - + func enforceJoystickBound(_ point: CGPoint) -> CGPoint? { guard let bounds = joystickDelegate.joystickBounds?() else { return nil } - + return CGPoint(x: clamp(point.x, bounds.origin.x, bounds.width), y: clamp(point.y, bounds.origin.y, bounds.height) ) } } - // MARK: - // MARK: handle touches extension JoystickView { @@ -111,9 +107,9 @@ extension JoystickView { currentReturnRatio = 0 draggerImgView.layer.removeAllAnimations() } - + override func touchesEnded(_ touches: Set, with event: UIEvent?) { - + if (joystickDelegate.joystickSoftReturnDuration?()) != nil { joystickDelegate.onJoystickSoftReleased?() returnJoystickToCenter() @@ -122,54 +118,56 @@ extension JoystickView { joystickDelegate?.onJoystickReleased?() } } - + func returnNow() { if let timer = returnTimer { timer.invalidate() returnTimer = nil } - dotPos = _center + dotPos = dragCenter updateDraggerPosition() } - + func returnJoystickToCenter() { - let dist = getDistance(p1: _center, p2: dotPos) + let dist = getDistance(point1: dragCenter, point2: dotPos) if dist < 0.1 { returnNow() } else if let returnDuration = joystickDelegate.joystickSoftReturnDuration?() { returnTime = returnDuration * Float(dist) / Float(bounds.width) // timer will update robot with new joystick values while is being animated - returnTimer = Timer.scheduledTimer(timeInterval: TimeInterval(returnTime / TIMER_RETURN_UPDATE_RATE), target:self, selector:#selector(self.updateReturnJoystick), userInfo:nil, repeats:true) - + returnTimer = Timer.scheduledTimer(timeInterval: TimeInterval(returnTime / timerReturnUpdateRate), + target:self, + selector:#selector(self.updateReturnJoystick), + userInfo:nil, repeats:true) + // animate soft return UIView.animate(withDuration: TimeInterval(returnTime), animations: { - self.draggerImgView.center = self._center + self.draggerImgView.center = self.dragCenter }) - + } } - + func updateReturnJoystick() { if currentReturnRatio > 1 { - dotPos = _center + dotPos = dragCenter // means we have returned it to the center joystickDelegate.onJoystickReleased?() - + // clean returnTimer?.invalidate() returnTimer = nil - currentReturnRatio = 0; + currentReturnRatio = 0 } else { - currentReturnRatio += (returnTime / TIMER_RETURN_UPDATE_RATE) + currentReturnRatio += (returnTime / timerReturnUpdateRate) joystickDelegate.onJoystickMoved?() - - dotPos = lerpPoints(min: dotPos, max: _center, T: Double(currentReturnRatio * 0.4)) + + dotPos = lerpPoints(min: dotPos, max: dragCenter, dist: Double(currentReturnRatio * 0.4)) } } - - + override func touchesMoved(_ touches: Set, with event: UIEvent?) { - + if let originalPos = touches.first?.location(in: self) { if let newPos = enforceJoystickBound(originalPos) { dotPos = newPos @@ -177,21 +175,18 @@ extension JoystickView { dotPos = originalPos } } - updateDraggerPosition() } - -} - +} extension JoystickView { func updateDraggerPosition() { - draggerImgView.center = dotPos; + draggerImgView.center = dotPos } - + func getX() -> Float { return 0 } @@ -200,5 +195,3 @@ extension JoystickView { } } - - diff --git a/iOS/playground/RobotControlPanelViewController.swift b/iOS/playground/RobotControlPanelViewController.swift index f3d5f35..0bca4ad 100644 --- a/iOS/playground/RobotControlPanelViewController.swift +++ b/iOS/playground/RobotControlPanelViewController.swift @@ -13,31 +13,36 @@ class RobotControlPanelViewController: UIViewController { @IBOutlet weak var controlsSelector: UISegmentedControl! @IBOutlet weak var controlsView: UIView! var activeControlVC: RobotControlViewController? - + var controlsVC = [RobotControlViewController]() override func viewDidLoad() { super.viewDidLoad() let sb = UIStoryboard(name: "Main", bundle: nil) - if - let lights = sb.instantiateViewController(withIdentifier: ControlLightsViewController.nameOfClass) as? RobotControlViewController, - let eyeRing = sb.instantiateViewController(withIdentifier: ControlEyeRingViewController.nameOfClass) as? RobotControlViewController, - let drive = sb.instantiateViewController(withIdentifier: ControlDriveViewController.nameOfClass) as? RobotControlViewController, - let head = sb.instantiateViewController(withIdentifier: ControlHeadViewController.nameOfClass) as? RobotControlViewController, - let sound = sb.instantiateViewController(withIdentifier: ControlSoundViewController.nameOfClass) as? RobotControlViewController, - let sensor = sb.instantiateViewController(withIdentifier: ControlSensorsViewController.nameOfClass) as? RobotControlViewController { - + let lights = sb.instantiateViewController(withIdentifier: ControlLightsViewController.nameOfClass) + let eyeRing = sb.instantiateViewController(withIdentifier: ControlEyeRingViewController.nameOfClass) + let drive = sb.instantiateViewController(withIdentifier: ControlDriveViewController.nameOfClass) + let head = sb.instantiateViewController(withIdentifier: ControlHeadViewController.nameOfClass) + let sound = sb.instantiateViewController(withIdentifier: ControlSoundViewController.nameOfClass) + let sensor = sb.instantiateViewController(withIdentifier: ControlSensorsViewController.nameOfClass) + + if let lights = lights as? RobotControlViewController, + let eyeRing = eyeRing as? RobotControlViewController, + let drive = drive as? RobotControlViewController, + let head = head as? RobotControlViewController, + let sound = sound as? RobotControlViewController, + let sensor = sensor as? RobotControlViewController { + controlsVC = [lights, eyeRing, drive, head, sound, sensor] presentRobotControlsVC(vc: controlsVC.first!) } } - + @IBAction func switchControls(_ control: UISegmentedControl) { let newVC = controlsVC[control.selectedSegmentIndex] presentRobotControlsVC(vc: newVC) } - private func presentRobotControlsVC(vc newVC: RobotControlViewController) { if activeControlVC != newVC { @@ -45,12 +50,12 @@ class RobotControlPanelViewController: UIViewController { previousVC.view.removeFromSuperview() previousVC.removeFromParentViewController() } - - activeControlVC = newVC; + + activeControlVC = newVC addChildViewController(newVC) controlsView.addSubview(newVC.view) newVC.didMove(toParentViewController: self) } } - + } diff --git a/iOS/playground/RobotControlViewController.swift b/iOS/playground/RobotControlViewController.swift index ff9bf1a..256feb5 100644 --- a/iOS/playground/RobotControlViewController.swift +++ b/iOS/playground/RobotControlViewController.swift @@ -5,13 +5,13 @@ // import UIKit -class RobotControlViewController : UIViewController { +class RobotControlViewController: UIViewController { var connectedRobots = [WWRobot]() - override func viewWillAppear(_ animated:Bool) { + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) refreshConnectedRobots() } - + func refreshConnectedRobots() { if let robots = WWRobotManager.shared().allConnectedRobots as? [WWRobot] { connectedRobots = robots @@ -20,14 +20,13 @@ class RobotControlViewController : UIViewController { } } } - - func sendCommandSet(toRobots cmd:WWCommandSet) { + + func sendCommandSet(toRobots cmd: WWCommandSet) { for robot in connectedRobots { robot.send(cmd) } } } -extension RobotControlViewController : WWRobotObserver { +extension RobotControlViewController: WWRobotObserver { } - diff --git a/iOS/playground/RobotListTableViewCell.swift b/iOS/playground/RobotListTableViewCell.swift index b34ad32..cd1d0d7 100644 --- a/iOS/playground/RobotListTableViewCell.swift +++ b/iOS/playground/RobotListTableViewCell.swift @@ -9,8 +9,8 @@ import UIKit class RobotListTableViewCell: UITableViewCell { - @IBOutlet var statusColorView : UIView! - @IBOutlet var robotImageView : UIImageView! - @IBOutlet var nameLabel : UILabel! - @IBOutlet var infoLabel : UILabel! + @IBOutlet var statusColorView: UIView! + @IBOutlet var robotImageView: UIImageView! + @IBOutlet var nameLabel: UILabel! + @IBOutlet var infoLabel: UILabel! } diff --git a/iOS/playground/RobotListViewController.swift b/iOS/playground/RobotListViewController.swift index fe4b8ec..83e0eb7 100644 --- a/iOS/playground/RobotListViewController.swift +++ b/iOS/playground/RobotListViewController.swift @@ -4,15 +4,14 @@ // // - import UIKit -class RobotListViewController : UITableViewController { - - var controlPanelViewController : RobotControlPanelViewController? - var manager : WWRobotManager? +class RobotListViewController: UITableViewController { + + var controlPanelViewController: RobotControlPanelViewController? + var manager: WWRobotManager? var robots = [WWRobot]() - + override func awakeFromNib() { super.awakeFromNib() if UIDevice.current.userInterfaceIdiom == .pad { @@ -25,28 +24,27 @@ class RobotListViewController : UITableViewController { if let panelViewController = splitViewController?.viewControllers.last as? RobotControlPanelViewController { controlPanelViewController = panelViewController } - + let nib = UINib(nibName: "RobotListTableViewCell", bundle: nil) tableView.register(nib, forCellReuseIdentifier: "RobotListTableViewCell") - + // setup robot manager - let manager : WWRobotManager = WWRobotManager.shared(); + let manager: WWRobotManager = WWRobotManager.shared() manager.add(self) - + self.manager = manager manager.startScanning(forRobots:2.0) - - + tableView.rowHeight = 130 let start = UIColor(red:58/255.0, green:108/255.0, blue:183/255.0, alpha:0.15) let stop = UIColor(red: 58/255.0, green:108/255.0, blue:183/255.0, alpha:0.45) - + let gradient = CAGradientLayer() gradient.frame = view.bounds - gradient.colors = [start.cgColor,stop.cgColor] + gradient.colors = [start.cgColor, stop.cgColor] tableView.layer.insertSublayer(gradient, at:0) } - + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { if let controller = segue.destination as? RobotControlPanelViewController, @@ -58,32 +56,32 @@ class RobotListViewController : UITableViewController { } } -// MARK: - -// MARK: Table View methods +// mark: - +// mark: Table View methods extension RobotListViewController { override func numberOfSections(in tableView: UITableView) -> Int { return 1 } - + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return robots.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - guard let cell = tableView.dequeueReusableCell(withIdentifier: "RobotListTableViewCell", for: indexPath) as? RobotListTableViewCell else { + let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "RobotListTableViewCell", for: indexPath) + guard let cell = tableViewCell as? RobotListTableViewCell else { return UITableViewCell() } let robot = robots[indexPath.row] if robot.isConnected() { cell.contentView.backgroundColor = UIColor(red:50/255.0, green:200/255.0, blue:50/255.0, alpha:0.6) - } - else { + } else { cell.contentView.backgroundColor = UIColor(red:250/255.0, green:250/255.0, blue:250/255.0, alpha:0.6) } - + // robot info var detail = String() detail.append("uuId: \(robot.uuId)\n") @@ -91,73 +89,72 @@ extension RobotListViewController { detail.append("Serial: \(robot.serialNumber)\n") detail.append("RSSI \(robot.signalStrength.intValue) dB\n") detail.append("Personality color: \(robot.personalityColorIndex)\n") - cell.infoLabel.text = detail; - + cell.infoLabel.text = detail + // robot name - cell.nameLabel.text = robot.name; - + cell.nameLabel.text = robot.name + // image - switch (robot.robotType) { + switch robot.robotType { case .ROBOT_DOT: cell.robotImageView.image = UIImage(named:"dot.png") - break; + break case .ROBOT_DASH: cell.robotImageView.image = UIImage(named:"dash.png") - + default: - break; + break } - - return cell; + + return cell } - + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let robot = robots[indexPath.row] if robot.isConnected() { manager?.disconnect(from: robot) - } - else { + } else { manager?.connect(to: robot) } } - + override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return false } - + } -// MARK: - -extension RobotListViewController : WWRobotManagerObserver { +// mark: - +extension RobotListViewController: WWRobotManagerObserver { func manager(_ manager: WWRobotManager!, didDiscover robot: WWRobot!) { if !robots.contains(robot) { robots.append(robot) tableView.reloadData() } } - + func manager(_ manager: WWRobotManager!, didUpdateDiscoveredRobots robot: WWRobot!) { tableView.reloadData() } - + func manager(_ manager: WWRobotManager!, didLose robot: WWRobot!) { if let index = robots.index(of: robot) { robots.remove(at: index) } tableView.reloadData() } - + func manager(_ manager: WWRobotManager!, didConnect robot: WWRobot!) { tableView.reloadData() controlPanelViewController?.activeControlVC?.refreshConnectedRobots() } func manager(_ manager: WWRobotManager!, didFailToConnect robot: WWRobot!, error: WWError!) { - print("failed to connect to robot: \(robot.name), with error: \(error)"); + print("failed to connect to robot: \(robot.name), with error: \(error)") print() - + } - + func manager(_ manager: WWRobotManager!, didDisconnectRobot robot: WWRobot!) { tableView.reloadData() }