Giter Site home page Giter Site logo

nunchuck.swift's Introduction

Nunchuck.swift

A Swift library for the Wii Nunchuck Controller

Linux-only Swift 4 compatible License: MIT

Nunchuck connector

Summary

This library reads accelerometer, joystick and button values from a Wii nunchuck controller connected via I2C to your RaspberryPi or similar boards.

Hardware Details

There are a few cheap clones of this controller on the market, and they differ from the original for the initialization sequence required to enable the controller. The few defaults provided should cover most of the available models.

The controller requires 3.3V to work but should be 5V tolerant, and you'll likely need an adapter for the proprietary connector, to make the various pins easier to access. I was using an ICSP adapter for Olimex boards, but as long as you can connect stuff to it (e.g. Dupont female connectors or connect it directly to the RaspberryPi headers), every adapter will do.

If you want to tinker with the controller via terminal, its I2C address is 0x52 (not decimal, like many guides report).

Usage

To initialize the Nunchuck class of this library you'll need an I2CInterface instance from SwiftyGPIO, and you'll need to specify the controller type if it's not original (try all the enums until you find one that works). Optionally, an I2C address can also be specified for non-standard controllers.

import SwiftyGPIO
import Nunchuck
import Foundation

let i2cs = SwiftyGPIO.hardwareI2Cs(for:.RaspberryPi2)!
let i2c = i2cs[1]

let mp = Nunchuck(i2c, type: .Knockoff1)

The values read from the controller (accelerometer values, analog joystick position and buttons status) can be read as single properties:

public var AccelX: Int
public var AccelY: Int
public var AccelZ: Int
public var AnalogX: Int
public var AnalogY: Int
public var Buttons: Int

Or all in one go, calling the getAll() method:

let (ax,ay,az,jx,jy,b) = mp.getAll()
print("Accelerometer - x:\(ax),y:\(ay),z:\(az)")
print("Analog Joystick - x:\(jx),y:\(jy)")
switch b {
   case 0:
       print("Buttons: Both pressed")
   case 1:
        print("Buttons: C pressed")
   case 2:
        print("Buttons: Z pressed")
   default:
        print("Buttons: None pressed")
}

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.

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/Nunchuck.swift.git", majorVersion: 2),
      ]
  ) 

The directory Examples contains sample projects that uses SPM, compile it and run the sample with ./.build/debug/TestNunchuck.

If SPM is not supported, you'll need to manually download the library and its dependencies:

wget https://raw.githubusercontent.com/uraimo/Nunchuck.swift/master/Sources/Nunchuck.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.

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.