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.
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. Together we are working to build a
programming language to empower everyone
to turn their ideas into apps on any platform.
https://swift.org/
12. Video of coin bank
Our ‘Savings Account’ product
Real product*
*not made by us
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’
64. // Setup individual pins for 'dt' (data) and 'sck' (serial clock)
var dt = gpios[.P5]!
var sck = gpios[.P6]!
dt.direction = .IN
sck.direction = .OUT
65. 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)
}
66. £ 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
67. 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
}
}