Successfully reported this slideshow.
Your SlideShare is downloading. ×

Swift on Raspberry Pi

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 72 Ad

Swift on Raspberry Pi

Download to read offline

From Southend Raspberry Jam - 8th of March 2020.
A talk showing how to make a smart coin bank using Swift on a Raspberry Pi, including using an LCD component, working with Load Cells, and Bit Banging.

From Southend Raspberry Jam - 8th of March 2020.
A talk showing how to make a smart coin bank using Swift on a Raspberry Pi, including using an LCD component, working with Load Cells, and Bit Banging.

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Similar to Swift on Raspberry Pi (20)

Advertisement

More from Sally Shepard (18)

Recently uploaded (20)

Advertisement

Swift on Raspberry Pi

  1. 1. @mostgood Swift on Raspberry Pi
  2. 2. @mostgood 👋 Sally Student: MSc Internet of Things Also: iOS developer and Technical Writer
  3. 3. @mostgood
  4. 4. https://www.ben-evans.com/presentations
  5. 5. https://www.ben-evans.com/presentations
  6. 6. https://www.ben-evans.com/presentations
  7. 7. What is Swift?
  8. 8. 🍎 📱⌚🖥 Open source programming langage
  9. 9. Safe. Code should behave in a safe manner. Undefined behaviour is the enemy of safety, and developer mistakes should be caught before software is in production. Fast. Swift is intended as a replacement for C-based languages (C, C++, and Objective-C). As such, Swift must be comparable to those languages in performance for most tasks. Expressive. Swift benefits from decades of advancement in computer science to offer syntax that is a joy to use, with modern features developers expect.
  10. 10. Together we are working to build a programming language to empower everyone to turn their ideas into apps on any platform. https://swift.org/
  11. 11. First Local Cat Savings Bank! @mostgood Introducing…
  12. 12. Video of coin bank Our ‘Savings Account’ product Real product* *not made by us
  13. 13. I want to know how much I just deposited I want to know how much is in my savings account I want to access information about my savings account from my phone Customer feedback for ‘Savings Account’
  14. 14. Introducing our new product: Smart Deposit Platform
  15. 15. Savings Account READY... Smart Deposit Platform
  16. 16. Savings Account 💰 READY... Smart Deposit Platform
  17. 17. Savings Account 💰 Smart Deposit Platform DEPOSIT IN PROGRESS DEPOSITED: 20p
  18. 18. Savings Account Smart Deposit Platform BALANCE: £5.85
  19. 19. Savings Account BALANCE: £5.85 Balance: £5.85 Deposit 22/5/19 £0.20 Deposit 22/5/19 £0.50 Deposit 20/5/19 £1.00
  20. 20. What’s under the hood? @mostgood
  21. 21. Cloud Smart Deposit Platform Hardware iOS app
  22. 22. Smart Deposit Platform • Counts money as it is deposited • Sends deposit information to the server Hardware
  23. 23. Cloud Maintains customers account • Deposit information • Account overview
  24. 24. Smart Deposit Platform Hardware Vapor iOS app
  25. 25. Building the SmartDepositPlatform @mostgood
  26. 26. Setup development environment @mostgood
  27. 27. Raspberry Pi 85mm 56m m 17mm 3.25” 2.2” 0.67”
  28. 28. @mostgood GPIO Pins
  29. 29. GPIOHeader @mostgood
  30. 30. curl -s https://packagecloud.io/install/repositories/ swift-arm/release/script.deb.sh | sudo bash sudo apt install ________ (version of Swift check: https://packagecloud.io/swift-arm/ release) For more info: https://swift-arm.com Install Swift on the Raspberry Pi
  31. 31. @mostgood mkdir ~/Documents/<project-name> cd ~/Documents/<project-name> swift package init --type executable swift package init --type executable Creating executable package: FirstLocalCatSavingsBank Creating Package.swift Creating README.md Creating .gitignore Creating Sources/ Creating Sources/FirstLocalCatSavingsBank/main.swift Creating Tests/ Creating Tests/LinuxMain.swift Creating Tests/FirstLocalCatSavingsBankTests/ Creating Tests/FirstLocalCatSavingsBankTests/FirstLocalCatSavingsBankTests.swift Creating Tests/FirstLocalCatSavingsBankTests/XCTestManifests.swift Create a new project
  32. 32. 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: "FirstLocalCatSavingsBank", dependencies: ["SwiftyGPIO"]) Reference: https://github.com/uraimo/SwiftyGPIO @mostgood
  33. 33. Reference: https://github.com/uraimo/SwiftyGPIO @mostgood Basically, ARMv7/8+Ubuntu/Debian/Raspbian or an ARMv6+Raspbian/Debian should work • Raspberry Pi 3, 3B+, 2, Zero, Zero W, Classic A,B,A+,B+ Rev1/Rev2 • BeagleBones • OrangePi, OrangePi Zero • Asus Tinkerboard • C.H.I.P. • BananaPi M1/2/3
  34. 34. Showing the deposit status
  35. 35. HD44780 16x2 LCD https://github.com/uraimo/HD44780CharacterLCD.swift
  36. 36. import SwiftyGPIO import HD44780LCD let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPiPlusZero) var rs = gpios[.P2]! var e = gpios[.P3]! var d4 = gpios[.P4]! var d5 = gpios[.P17]! var d6 = gpios[.P27]! var d7 = gpios[.P22]! let lcd = HD44780LCD(rs:rs, e:e, d7:d7, d6:d6, d5:d5, d4:d4, width:16, height:2) lcd.clearScreen() lcd.cursorHome() lcd.printString(x:0, y:0, what:"Deposited: (depositValue)”, usCharSet:true)
  37. 37. What coin is the customer depositing? @mostgood
  38. 38. @mostgood Smart Deposit Platform Savings Account 💰 How do we find the value of this? Each coin has a unique weight
  39. 39. @mostgood 💰 current ‘Savings Account’ weight previous ‘Savings Account’ weight = - Savings Account 💰 Savings Account
  40. 40. @mostgood 💰 current ‘Savings Account’ weight previous ‘Savings Account’ weight = - 💰 1003.5g 994g= -
  41. 41. @mostgood 💰 current ‘Savings Account’ weight previous ‘Savings Account’ weight = - 💰 1003.5g 994g= - 💰 9.5g= = £1
  42. 42. How do we weigh something?
  43. 43. @mostgood
  44. 44. Load Cell
  45. 45. @mostgood HX711
  46. 46. Datasheets! @mostgood
  47. 47. @mostgood
  48. 48. @mostgood
  49. 49. @mostgood
  50. 50. Digital Signal 0 1 1 = 3.3v 1 = 3.3v 0 = 0v 0 = 0v @mostgood
  51. 51. Analogue Signal 0 128 256 384 512 640 768 896 1024 768 140 @mostgood
  52. 52. Analogue to Digital? 140 768 @mostgood
  53. 53. Analogue to Digital? 140 768 0000 1000 1100 0011 0000 0000 @mostgood
  54. 54. Analogue to Digital? 0000 1000 1100 0011 0000 0000 @mostgood
  55. 55. Analogue to Digital? 0 0 0 0 0 0 1 0 1 0 0 1 word length = 12 bits @mostgood
  56. 56. @mostgood
  57. 57. @mostgood
  58. 58. @mostgood
  59. 59. @mostgood
  60. 60. @mostgood two’s complement
  61. 61. // Setup individual pins for 'dt' (data) and 'sck' (serial clock) var dt = gpios[.P5]! var sck = gpios[.P6]! dt.direction = .IN sck.direction = .OUT
  62. 62. func read() -> Int32 { sck.value = 0 // read first 24 bits of data var dataIn: UInt32 = 0 for _ in 0..<24 { sck.value = 1 // Shift the bits as they come to dataIn variable. // Left shift by one bit then bitwise OR with the new bit. dataIn = (dataIn << 1) | UInt32(dt.value) sck.value = 0 } // check if data is valid // 0x7fffff is max value, 0x800000 is min value if (dataIn == 0x7fffff || dataIn == 0x800000) { print("Invalid data detected: (dataIn)") return 0 } // calculate int from 2's complement var signedData: Int32 = 0 if (Int32(dataIn & 0x800000) == 1) { // convert from 2's complement to int signedData = Int32(((dataIn ^ 0xffffff) + 1)) } else { // else do not do anything the value is positive number signedData = Int32(dataIn) } print("Converted 2's complement value: (signedData)") return Int32(signedData) }
  63. 63. £ g 2.00 12 1.00 9.5 0.50 8 0.20 5 0.10 6.5 0.05 3.25 0.02 7.12 0.01 3.56 switch coinWeight { case 3.15...3.35: // 5p coin depositValue = 0.05 break case 3.4...3.6: // 1p coin depositValue = 0.01 break case 4.9...5.1: // 20p coin depositValue = 0.2 break case 6.4...6.6: // 10p coin depositValue = 0.1 break case 7.02...7.22: // 2p coin depositValue = 0.2 break case 7.9...8.1: // 50p coin depositValue = 0.5 break case 9.4...9.6: // £1 coin depositValue = 1.0 break case 11.9...12.1: // £2 coin depositValue = 2.0 break default: print("Value not within a range of a predefined coin value") } Coin Value and weight
  64. 64. struct DepositEntry: Codable { var id: String? var value: Float var accountWeight: Float var date: Date init?(id: String?, value: Float, accountWeight: Float, date: Date) { if value.isEmpty { return nil } self.id = id self.value = value self.accountWeight = accountWeight self.date = date } }
  65. 65. Try Swift out :)
  66. 66. https://www.hackingwithswift.com https://swift.org https://swift-arm.com https://github.com/apple/HomeKitADK
  67. 67. Sally Shepard @mostgood Thanks!

×