Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swift hardware hacking @ try! Swift

1,103 views

Published on

A talk looking at building a smart savings account - some hardware hacking, Raspberry Pi development, Bluetooth LE, electronics, bit banging - all using Swift.

Published in: Technology
  • Be the first to comment

Swift hardware hacking @ try! Swift

  1. 1. Swift Hardware Hacking Sally Shepard @mostgood
  2. 2. First Local Cat Savings Bank! Congratulations on joining @mostgood
  3. 3. Video of coin bank Savings account v1.0 @mostgood
  4. 4. Savings account v2.0 @mostgood Ready for deposit £4.85
  5. 5. How to: Build the smart savings account @mostgood
  6. 6. Step 0: Setup development environment @mostgood
  7. 7. " @mostgood
  8. 8. @mostgood
  9. 9. @mostgood
  10. 10. Install Swift on Raspberry Pi curl -s https://packagecloud.io/install/ repositories/swift-arm/release/script.deb.sh | sudo bash sudo apt install swift4 //if on 3B+ sudo apt install swift4rpi01 //if using a Zero @mostgood Create a project mkdir ~/Documents/<project-name> cd ~/Documents/<project-name> swift package init --type executable
  11. 11. Setting up Geany for build/run Reference: https://www.geany.org/manual/current/index.html @mostgood
  12. 12. Step 1: Controlling power to the bank @mostgood
  13. 13. v1 #$ v2 % & @mostgood
  14. 14. Circuits @mostgood
  15. 15. Circuits @mostgood
  16. 16. @mostgood + — + —
  17. 17. GPIOHeader @mostgood
  18. 18. Dependencies -> dependencies: [ // Dependencies declare other packages that this package depends on. .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0")] .target( name: "GeePeeEyeOh", dependencies: ["SwiftyGPIO"]) Reference: https://github.com/uraimo/SwiftyGPIO @mostgood
  19. 19. @mostgood
  20. 20. @mostgood
  21. 21. Relay.swift import SwiftyGPIO struct Relay { var state: State { get { if let pin = self.gpio { switch pin.value { case 1: return .On default: return .Off } } return .Off } } private var gpio: RaspberryGPIO? init() { setup() } init(connectTo pin: RaspberryGPIO) { self.gpio = pin setup() } private mutating func setup() { if self.gpio == nil { let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPiPlusZero) self.gpio = gpios[.P4] as? RaspberryGPIO } if let gpio = self.gpio { gpio.direction = .OUT } } public func switchTo(_ state: State) { if let gpio = self.gpio { gpio.value = state.rawValue } } } @mostgood
  22. 22. Digital Signal 0 1 1 = 3.3v 1 = 3.3v 0 = 0v 0 = 0v @mostgood
  23. 23. Step 2: tell when the lid is open @mostgood
  24. 24. @mostgood
  25. 25. @mostgood
  26. 26. Analogue Signal 0 128 256 384 512 640 768 896 1024 768 140 @mostgood
  27. 27. @mostgood
  28. 28. @mostgood
  29. 29. Dependencies -> MCP3008.swift dependencies: [ // Dependencies declare other packages that this package depends on. .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: “1.0.0”), .package(url: "https://github.com/uraimo/MCP3008.swift.git", from: “2.0.0")] .target( name: "GeePeeEyeOh", dependencies: [“SwiftyGPIO”, “MCP3008”]) Reference: https://github.com/uraimo/MCP3008 @mostgood
  30. 30. import SwiftyGPIO struct Photoresistor { private var maxLightThreshold: UInt32 = 0 private var minLightThreshhold: UInt32 = 1024 private var tolerance: UInt32 = 10 var lightReading: UInt32 = 1024 var light: Bool { get { let lightRange = 0...(lightReading + tolerance) return lightRange.contains(lightReading) } } init(light minLightThreshhold: UInt32, dark maxLightThreshold: UInt32, tolerance tolerance: UInt32) { self.minLightThreshhold = minLightThreshhold self.maxLightThreshold = maxLightThreshold self.tolerance = tolerance self.init() } } Photoresistor. swift @mostgood
  31. 31. Step 3: figuring out what coin was deposited @mostgood
  32. 32. coin weight = currentDeposit - previousDeposit @mostgood
  33. 33. @mostgood
  34. 34. @mostgood Red E+ Black E- White A- Green A+ Ground DF SCK Vcc
  35. 35. Datasheets! @mostgood
  36. 36. @mostgood
  37. 37. @mostgood
  38. 38. @mostgood
  39. 39. Digital Signal 0 1 1 = 3.3v 1 = 3.3v 0 = 0v 0 = 0v @mostgood
  40. 40. Analogue Signal 0 128 256 384 512 640 768 896 1024 768 140 @mostgood
  41. 41. Analogue to Digital? 140 768 @mostgood
  42. 42. Analogue to Digital? 140 768 0000 1000 1100 0011 0000 0000 @mostgood
  43. 43. Analogue to Digital? 0000 1000 1100 0011 0000 0000 @mostgood
  44. 44. Analogue to Digital? 0 0 0 0 0 0 1 0 1 0 0 1 word length = 12 bits @mostgood
  45. 45. @mostgood
  46. 46. @mostgood
  47. 47. @mostgood
  48. 48. @mostgood two’s complement
  49. 49. import SwiftGPIO struct HX711 { var dt: RaspberryGPIO? var sck: RaspberryGPIO? init() { let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPiPlusZero) dt = gpios[.P5] as? RaspberryGPIO sck = gpios[.P6] as? RaspberryGPIO self.setup() } init(data dt: RaspberryGPIO, clock ck: RaspberryGPIO) { self.dt = dt self.sck = ck self.setup() } private func setup() { if let dt = self.dt, let sck = self.sck { dt.direction = .IN sck.direction = .OUT } } // read from the Data pin public func readData() -> Int { if let dt = self.dt { if dt.value == 0 { for _ in 0..<24 { self. sck.value = 1 if let dt = self.dt { //MAKE BITS DO THINGS HERE } self. sck.value = 0 } } else { print("Not Ready") } } } } HX711.swift @mostgood
  50. 50. @mostgood @UIKitten
  51. 51. Step 5: Bluetooth connection to iOS @mostgood
  52. 52. Bluetooth Low Energy “Central” client “Peripheral” server GATT Profile 0x180A “Device Information” • Manufacturer Name • Model Number • Serial Number • Hardware/Firmware/Software Revision • System ID • Certification List • Device ID GATT Profile 0x180F “Battery Service” • Battery Level • Level Changed Notify “Battery Level Changed” • Battery Percentage (optional) Write “User Data - First Name” Read “Device Information - Serial Number” @mostgood
  53. 53. Bluetooth Low Energy Core Bluetooth API PureSwift/Bluetooth PureSwift/BluetoothLinux PureSwift/GATT @mostgood
  54. 54. Bluetooth Low Energy dependencies: [ // Dependencies declare other packages that this package depends on. .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: “1.0.0”), .package(url: "https://github.com/uraimo/MCP3008.swift.git", from: “2.0.0”), .package(url: "https://github.com/PureSwift/BluetoothLinux.git", from: “3.0.0"), .package(url: "https://github.com/PureSwift/GATT.git", from : "1.0.0")] .target( name: "GeePeeEyeOh", dependencies: [“SwiftyGPIO”, “MCP3008”, “BluetoothLinux”, “GATT”]) Reference: https://github.com/PureSwift/BluetoothLinux https://github.com/PureSwift/GATT @mostgood
  55. 55. Core Bluetooth // Core Bluetooth Manager var centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main) centralManager?.scanForPeripherals(withServices: nil, options: nil) // Delegate Methods for Bluetooth Central Manager func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) // Delegate methods for connected peripherals func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) // Peripheral methods peripheral.writeValue(data, for: descriptor) peripheral.readValue(for: descriptor) peripheral.setNotifyValue(true, for: characteristic) @mostgood
  56. 56. Bluetooth Linux // Instantiate controller & peripheral let controller = HostController.default let peripheral = PeripheralManager(controller: controller) // configure service(s) let service = BluetoothUUID(rawValue: “180A”), // Hex value of GATT Service let controllerType = serviceControllers.first(where: { $0.service == service }) let serviceController = try controllerType.init(peripheral: peripheral) // start the peripheral try peripheral.start() Reference: https://github.com/MillerTechnologyPeru/gattserver @mostgood
  57. 57. Final product @mostgood
  58. 58. @mostgood
  59. 59. Thank you for listening! Sally Shepard @mostgood

×