Giter VIP home page Giter VIP logo
 photo

swiftylinkerkit Goto Github PK

repos: 3.0 gists: 0.0

Type: Organization

Bio: Swift Modules to build LinkerKit Projects

Twitter: helje5

Location: Magdeburg, Germany

SwiftyLinkerKit

Swift4 tuxOS

A Swift module to control LinkerKit components in a convenient way. This is currently focused on LinkerKit things attached to a Raspberry Pi LK-Base-RB 2 shield (since this is what I have 🤓).

SwiftyLinkerKit is based on the excellent SwiftyGPIO module, and of course on the great work of the Swift-ARM Community.

Supported Components

Right now SwiftyLinkerKit supports only a few components listed below. It is very easy to add new ones, we very much welcome contributions!

Also: We need help with understanding LK-Accel and LK-Temp (how to connect them, how to hook them up w/ SPI, contact us if you know more about that weird hardware stuff 🤖).

LK-Button 2

Two buttons :-)

import SwiftyLinkerKit

let shield  = LKRBShield.default
let buttons = LKButton2()

shield.connect(buttons, to: .digital2122)

buttons.onPress1 {
    print("Button 1 was pressed!")
}
buttons.onChange2 { isPressed in
    print("Button 2 changed, it is now: \(isPressed ? "pressed" : "off" )")
}

LK-Digi

A neat 7-segment display.

import SwiftyLinkerKit

let shield  = LKRBShield.default
let display = LKDigi()

shield.connect(display, to: .digital45)

display.show("SWIFT")
sleep(2)

display.show(1337)
sleep(2)

display.showTime()
sleep(2)

for i in (0...10).reversed {
    display.show(i)
    sleep(1)
}

LK-PIR

An IR movement detector.

import SwiftyLinkerKit

let shield   = LKRBShield.default
let watchdog = LKPIR()

shield.connect(watchdog, to: .digital1213)

watchdog.onChange { didMove in
    if didMove { print("careful, don't move!") }
    else       { print("nothing is moving.")   }
}

LK-Temp

Temperature sensor, uses a thermistor to detect the environmental temperature. LK-Temp is connected to one of the analog ports (and hosted via the board ADC running on SPI).

import SwiftyLinkerKit

let shield      = LKRBShield.default
let thermometer = LKTemp(interval: 5.0, valueType: .celsius)

thermometer.onChange { temperature in
    print("Temperatur is", temperature, "")
}

How to setup and run

Note: This is for 32-bit, 64-bit doesn't seem to work yet.

Raspi Docker Setup

You don't have to, but I recommend running things in a HypriotOS docker container.

Setup is trivial. Grab the flash tool, then insert your empty SD card into your Mac and do:

$ flash --hostname zpi3 \
  https://github.com/hypriot/image-builder-rpi/releases/download/v1.8.0/hypriotos-rpi-v1.8.0.img.zip

Boot your Raspi and you should be able to reach it via zpi3.local.

I also recommend to use docker-machine (e.g. see here), but that is not necessary either.

Running an ARM Swift container

Boot the container like so:

$ docker run --rm \
  --cap-add SYS_RAWIO \
  --privileged \
  --device /dev/mem \
  -it --name swiftfun \
  helje5/rpi-swift-dev:4.1.0 /bin/bash

You end up in a Swift 4.1 environment with some dev tools like emacs pre-installed. Sudo password for user swift is swift.

Importing the Swift Package

Sample Package.swift file:

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "testit",
    dependencies: [
        .package(url: "https://github.com/SwiftyLinkerKit/SwiftyLinkerKit.git",
                 from: "0.1.0"),
    ],
    targets: [
        .target(
            name: "testit",
            dependencies: [ "SwiftyLinkerKit" ]),
    ]
)

Example: Clock

A simple digital clock.

swift@f296eaf9ee96:~$ mkdir clock && cd clock && swift package init --type executable
Creating executable package: clock
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/clock/main.swift
Creating Tests/

Then edit the Package.swift file to look like this:

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "clock",
    dependencies: [
        .package(url: "https://github.com/SwiftyLinkerKit/SwiftyLinkerKit.git",
                 from: "0.1.0"),
    ],
    targets: [
        .target(
            name: "clock",
            dependencies: [ "SwiftyLinkerKit" ]),
    ]
)

Edit the Sources/clock/main.swift with the following Swift code. In the example the LK-Digi is connected to the Digital-4/5 slot of the LK-RB-Shield, adjust accordingly.

import SwiftyLinkerKit
import Dispatch

let shield  = LKRBShield.default
let display = LKDigi()

shield.connect(display, to: .digital45)

let timer = DispatchSource.makeTimerSource()

timer.setEventHandler {
    display.showTime()
}

timer.schedule(deadline  : .now(),
               repeating : .seconds(1),
               leeway    : .milliseconds(1))
timer.resume()

dispatchMain()

Build everything:

swift@f296eaf9ee96:~/testit$ swift build
Fetching https://github.com/SwiftyLinkerKit/SwiftyLinkerKit.git
Fetching https://github.com/uraimo/SwiftyGPIO.git
Fetching https://github.com/AlwaysRightInstitute/SwiftyTM1637.git
Cloning https://github.com/SwiftyLinkerKit/SwiftyLinkerKit.git
Resolving https://github.com/SwiftyLinkerKit/SwiftyLinkerKit.git at 0.1.0
Cloning https://github.com/uraimo/SwiftyGPIO.git
Resolving https://github.com/uraimo/SwiftyGPIO.git at 1.0.5
Cloning https://github.com/AlwaysRightInstitute/SwiftyTM1637.git
Resolving https://github.com/AlwaysRightInstitute/SwiftyTM1637.git at 0.1.2
Compile Swift Module 'SwiftyGPIO' (10 sources)
Compile Swift Module 'SwiftyTM1637' (5 sources)
Compile Swift Module 'SwiftyLinkerKit' (5 sources)
Compile Swift Module 'clock' (1 sources)
Linking /home/swift/clock/.build/armv7-unknown-linux-gnueabihf/debug/clock

You need to run it using sudo (password in the Docker is swift):

swift@f296eaf9ee96:~/testit$ sudo .build/armv7-unknown-linux-gnueabihf/debug/clock

Want to see it in action? SwiftyLinkerKit driven input/output using LinkerKit components

Who

SwiftyLinkerKit is brought to you by AlwaysRightInstitute. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.

There is a channel on the Swift-ARM Slack.

swiftylinkerkit's Projects

swiftylinkerkit icon swiftylinkerkit

A Swift module to control LinkerKit things in a convenient and easy way.

swiftytm1637 icon swiftytm1637

A Swift class to drive the TM1637 chipset (i.e. 7-segment LK-Digi Display)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.