uraimo/ds1307.swift
*A Swift library for the DS1307(also DS1302 and DS3231 w/o enhanced functions) I2C Real-Time Clock*
Supported Boards
Every board supported by SwiftyGPIO: RaspberryPis, BeagleBones, C.H.I.P., etc...
To use this library, you'll need a Linux ARM board with Swift 3.x/4.x.
The example below will use a RaspberryPi 2 board but you can easily modify the example to use one the other supported boards, a full working demo projects for the RaspberryPi2 is available in the Examples directory.
Usage
The first thing we need to do is to obtain an instance of I2CInterface from SwiftyGPIO and use it to initialize the DS1307 object:
import SwiftyGPIO
import DS1307
let i2cs = SwiftyGPIO.hardwareI2Cs(for:.RaspberryPi2)!
let i2c = i2cs[0]
let ds = DS1307(i2c)Once you have an instance of the DS1307 object, you can set and get the time as individual components (day, month, year[0-99], hours, minutes, seconds) or as a Foundation's Date or all the components in one go:
//Properties of DS1307 (all gettable/settable individually):
//year,month,date,hours,minutes,seconds
ds.setTime(hours: 16, minutes: 25, seconds: 00, date: 10, month: 5, year: 17)
ds.start()
var (hours, minutes, seconds, date, month, year) = ds.getTime()
print("\(year)/\(month)/\(date) \(hours):\(minutes):\(seconds)")
sleep(10)
(hours, minutes, seconds, date, month, year) = ds.getTime()
print("\(year)/\(month)/\(date) \(hours):\(minutes):\(seconds)")
//Also available:
//
//ds.setDate(Date())
//let now = ds.getDate()The functions start() and stop() resume or pause the clock.
A complete example is available under Examples/
Installation
Please refer to the SwiftyGPIO readme for Swift installation instructions.
Once your board runs Swift, if your version support the Swift Package Manager, you can simply add this library as a dependency of your project and compile with swift build:
let package = Package(
name: "MyProject",
dependencies: [
.Package(url: "https://github.com/uraimo/DS1307.swift.git", majorVersion: 1),
]
) The directory Examples contains sample projects that uses SPM, compile it and run the sample with ./.build/debug/TestRTC.
If SPM is not supported, you'll need to manually download the library and its dependencies:
wget https://raw.githubusercontent.com/uraimo/DS1307.swift/master/Sources/DS1307.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/Presets.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/I2C.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SunXi.swift
And once all the files have been downloaded, create an additional file that will contain the code of your application (e.g. main.swift). When your code is ready, compile it with:
swiftc *.swift
The compiler will create a main executable.
Package Metadata
Repository: uraimo/ds1307.swift
Default branch: master
README: README.md