Skip to content

Commit

Permalink
Merge pull request #5 from ThinkerPal/cleaning-up
Browse files Browse the repository at this point in the history
Cleaned up code, added microbit code, production ready! 🚀
  • Loading branch information
ThinkerPal authored Jun 16, 2021
2 parents f76d7d4 + 1037625 commit 3fedfcf
Show file tree
Hide file tree
Showing 24 changed files with 71,201 additions and 153 deletions.
12 changes: 12 additions & 0 deletions GRACE.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
25E1A5312677662F0027C3B0 /* WaitingLiftViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25E1A5302677662F0027C3B0 /* WaitingLiftViewController.swift */; };
323AC8152679EF5B006DE39C /* Manager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 323AC8142679EF5B006DE39C /* Manager.swift */; };
3256CDF82677C97A00EF4F6C /* MSDOSDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3256CDF72677C97A00EF4F6C /* MSDOSDelegate.swift */; };
327C18412678A3B600716D55 /* FloorSelectionButtons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 327C18402678A3B600716D55 /* FloorSelectionButtons.swift */; };
327C18432678A58200716D55 /* MSDOS+BLE.swift in Sources */ = {isa = PBXBuildFile; fileRef = 327C18422678A58200716D55 /* MSDOS+BLE.swift */; };
Expand All @@ -30,6 +31,7 @@

/* Begin PBXFileReference section */
25E1A5302677662F0027C3B0 /* WaitingLiftViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaitingLiftViewController.swift; sourceTree = "<group>"; };
323AC8142679EF5B006DE39C /* Manager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Manager.swift; sourceTree = "<group>"; };
3256CDF72677C97A00EF4F6C /* MSDOSDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSDOSDelegate.swift; sourceTree = "<group>"; };
327C18402678A3B600716D55 /* FloorSelectionButtons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloorSelectionButtons.swift; sourceTree = "<group>"; };
327C18422678A58200716D55 /* MSDOS+BLE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MSDOS+BLE.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -63,6 +65,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
323AC8162679EF5F006DE39C /* Manager */ = {
isa = PBXGroup;
children = (
323AC8142679EF5B006DE39C /* Manager.swift */,
);
path = Manager;
sourceTree = "<group>";
};
32D6D16826775F4A00E53AB1 /* Multi Lobby */ = {
isa = PBXGroup;
children = (
Expand All @@ -74,6 +84,7 @@
32D6D16926775F5300E53AB1 /* Floor Selection */ = {
isa = PBXGroup;
children = (
323AC8162679EF5F006DE39C /* Manager */,
A0D1DA542677D49A00316905 /* FloorSelection.storyboard */,
A0604D5C267761A100444469 /* FloorSelectionViewController.swift */,
327C18402678A3B600716D55 /* FloorSelectionButtons.swift */,
Expand Down Expand Up @@ -243,6 +254,7 @@
A0604D492677594A00444469 /* AppDelegate.swift in Sources */,
32D6D1742677685600E53AB1 /* Microbit.swift in Sources */,
A0604D4B2677594A00444469 /* SceneDelegate.swift in Sources */,
323AC8152679EF5B006DE39C /* Manager.swift in Sources */,
32D6D172267766CC00E53AB1 /* MSDOS.swift in Sources */,
32D6D16F26775F9C00E53AB1 /* Lobby.swift in Sources */,
3256CDF82677C97A00EF4F6C /* MSDOSDelegate.swift in Sources */,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 9 additions & 45 deletions GRACE/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,91 +1,55 @@
{
"images" : [
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"filename" : "[email protected]",
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"filename" : "[email protected]",
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
Expand Down
68 changes: 56 additions & 12 deletions GRACE/Bluetooth/MSDOS+BLE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {

public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

print(peripheral.name)

guard let name = peripheral.name else { return }

print(name)
let regex = try! NSRegularExpression(pattern: "BBC micro:bit \\[[z|v|g|p|t][u|o|i|e|a][z|v|g|p|t][u|o|i|e|a][z|v|g|p|t]\\]",
options: [])
Expand All @@ -22,13 +25,13 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {
return
}

if !microbits.contains(where: { $0.peripheral.identifier == peripheral.identifier }) {
let microbit = Microbit(with: peripheral)
microbits.append(microbit)
centralManager.connect(microbit.peripheral, options: nil)
}
print("RSSI", RSSI.intValue)

let microbit = Microbit(with: peripheral)

microbits.append(microbit)

centralManager.connect(microbit.peripheral, options: nil)
}

public func centralManagerDidUpdateState(_ central: CBCentralManager) {
Expand All @@ -51,8 +54,9 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {

public func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("👋 MS-DOS: Disconnected from", peripheral.name ?? "unnamed")

delegate?.didDisconnect()
if microbit?.peripheral.identifier == peripheral.identifier {
delegate?.didDisconnect()
}
}

public func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
Expand Down Expand Up @@ -83,14 +87,15 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {
$0.peripheral.identifier == peripheral.identifier
}) {
let internalMicrobit = microbits[microbitIndex]
internalMicrobit.write("metadata please")

Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
internalMicrobit.write("metadata please")
}
}

} else if characteristic.uuid == readUUID {
peripheral.setNotifyValue(true, for: characteristic)
}

peripheral.setNotifyValue(true, for: characteristic)
}
}

Expand All @@ -104,6 +109,7 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {
switch scanType {
case .lobby:
if stringValue.first == "#" {
print("Continuing with \(peripheral.name!)")
// It is a lobby
// We can continue as a lobby
var content = stringValue
Expand All @@ -124,7 +130,13 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {
})!

microbit = microbits[microbitIndex]
} else if stringValue == "@BYE!" {
centralManager.cancelPeripheralConnection(peripheral)
self.microbit = nil
microbits = []

} else {
print("Cancelling \(peripheral.name!)")
centralManager.cancelPeripheralConnection(peripheral)

if let index = microbits.firstIndex(where: {
Expand All @@ -139,6 +151,38 @@ extension MSDOS: CBCentralManagerDelegate, CBPeripheralDelegate {
}
break
case .lift:
print("LIFT", stringValue)
if stringValue == "Hi im a lift" {

print("Magically Connected to \(peripheral.name)")

let microbitIndex = microbits.firstIndex(where: {
$0.peripheral == peripheral
})!

microbit = microbits[microbitIndex]

delegate?.didFindLift(microbit: microbit!)
} else if stringValue == "thanks! bye!" {
// GOOD NIGHT
centralManager.cancelPeripheralConnection(peripheral)
centralManager.stopScan()
delegate?.didFinishLift()
} else {

print("Killing \(peripheral.name!)")
centralManager.cancelPeripheralConnection(peripheral)

if let index = microbits.firstIndex(where: {
$0.peripheral.identifier == peripheral.identifier
}) {
microbits.remove(at: index)
}

if let microbit = microbits.first {
centralManager.connect(microbit.peripheral, options: nil)
}
}
break
case .none:
break
Expand Down
14 changes: 6 additions & 8 deletions GRACE/Bluetooth/MSDOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ public class MSDOS: NSObject {
}

var microbit: Microbit?

var centralManager: CBCentralManager!

let readUUID = CBUUID(string: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
let writeUUID = CBUUID(string: "6E400003-B5A3-F393-E0A9-E50E24DCCA9E")

var microbits: [Microbit] = []

var lobbyDevice: Microbit?

var scanType: ScanType!

var delegate: MSDOSDelegate?
Expand All @@ -43,6 +42,11 @@ public class MSDOS: NSObject {
}

func startScanning(for scanType: ScanType = .lobby) {

if centralManager != nil {
centralManager.stopScan()
}

self.scanType = scanType
centralManager = CBCentralManager(delegate: self, queue: .main)

Expand All @@ -63,13 +67,7 @@ public class MSDOS: NSObject {
}

if scanType == .lobby {
print(direction.rawValue)
microbit.write(direction.rawValue)

// Disconnect from Lobby after we are done
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { [self] _ in
centralManager.cancelPeripheralConnection(microbit.peripheral)
}
} else {
print("🛑 MS-DOS: Unable to perform `ScanType.lobby` functions on a `ScanType.lift`")
}
Expand Down
8 changes: 8 additions & 0 deletions GRACE/Bluetooth/MSDOSDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import Foundation
protocol MSDOSDelegate {
func didFindLobby(_ lobby: Lobby)
func didDisconnect()

func didFindLift(microbit: Microbit)

func didFinishLift()
}

extension MSDOSDelegate {
func didFindLobby(_ lobby: Lobby) {}
func didDisconnect() {}

func didFindLift(microbit: Microbit) {}

func didFinishLift() {}
}
12 changes: 5 additions & 7 deletions GRACE/Floor Selection/FloorSelection.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,10 @@
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="JHx-Ps-Umx">
<rect key="frame" x="0.0" y="385.5" width="374" height="126.5"/>
<subviews>
<button opaque="NO" alpha="0.30000001192092896" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="z0G-7u-fGA">
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="GZ4-xV-3D4">
<rect key="frame" x="0.0" y="0.0" width="124.5" height="126.5"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="tintColor" name="DarkBlue"/>
<state key="normal">
<preferredSymbolConfiguration key="preferredSymbolConfiguration" configurationType="pointSize" pointSize="80"/>
</state>
</button>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4gn-DE-N5P">
<rect key="frame" x="124.5" y="0.0" width="125" height="126.5"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
Expand Down Expand Up @@ -279,7 +275,9 @@
<connections>
<outlet property="blockLabel" destination="2Xa-sD-UkA" id="RwS-Xd-JaK"/>
<outlet property="floorLabel" destination="BDi-JM-jbk" id="Ceu-Cn-OWS"/>
<outlet property="headerView" destination="q2g-JK-1pN" id="UlL-kw-VJF"/>
<outlet property="lobbyLabel" destination="JBj-Qz-fdy" id="Q8d-LB-YDH"/>
<outlet property="welcomeTitleLabel" destination="Wvs-Bu-Y5V" id="NQS-5y-7Ut"/>
<outletCollection property="keypadButtons" destination="vxW-oP-Nhn" collectionClass="NSMutableArray" id="rJt-2C-sqB"/>
<outletCollection property="keypadButtons" destination="B7z-wn-Bcm" collectionClass="NSMutableArray" id="mZL-V8-a21"/>
<outletCollection property="keypadButtons" destination="WIj-1Q-fsv" collectionClass="NSMutableArray" id="hi4-Yg-Sm5"/>
Expand Down
Loading

0 comments on commit 3fedfcf

Please sign in to comment.