A Swift library for the DS1307(also DS1302 and DS3231 w/o enhanced functions) I2C Real-Time Clock
Summary
This library can configure a DS1307 RTC clock and once it’s running can retrieve the current date and time.
This component is powered with 5V and requires a 32.768kHz Quartz Crystal oscillator to operate. It exposes an I2C interface that this library uses to interact with it.
If you are not using a backup battery with this RTC, remember to connect Vbatt to GND.
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:
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:
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:
DS1307.swift
A Swift library for the DS1307(also DS1302 and DS3231 w/o enhanced functions) I2C Real-Time Clock
Summary
This library can configure a DS1307 RTC clock and once it’s running can retrieve the current date and time.
This component is powered with 5V and requires a 32.768kHz Quartz Crystal oscillator to operate. It exposes an I2C interface that this library uses to interact with it.
If you are not using a backup battery with this RTC, remember to connect Vbatt to GND.
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 theDS1307
object: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:
The functions
start()
andstop()
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
: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:
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:
The compiler will create a main executable.