LoginSignup
2

More than 5 years have passed since last update.

posted at

Rubymotion + Konashi でフィジカルコンピューティング

みなさま、クリスマスイブをいかがお過ごしでしょうか?
今年はアベノミクスでケーキが良いみたいですね。クリスマスだけに。
ということで、RubyMotion Advent Calendar 2013、24日目スタート!

Konashi とは

konashi に搭載されている Bluetooth Low Energy モジュールで iPhone と通信し、ハードウェアをいろんころんできます。詳しくはこちら↓↓↓↓
http://konashi.ux-xu.com/

何か作ってみよう

オンボード入力端子のボタンでカメラのシャッターを押すアプリを作ってみたいと思います。
実際に作ったアプリを動作させたもの↓↓↓↓
http://youtu.be/2AikiDdx1yE
連写しているように見えますが、連写できていません。そこまで手回らんかった。。。

1.cocoapodsを使ってKonashiのライブラリを導入します。

Gemfile
gem "motion-cocoapods"

2.Rakefileにkonashi-ios-sdkを追加

Rakefile
app.pods do
    pod 'konashi-ios-sdk'
end

3.コントローラーに接続のための下準備

root_controller.rb
def viewDidLoad
  Konashi.initialize
  Konashi.addObserver(self, selector:"update_pio_input", name:KONASHI_EVENT_UPDATE_PIO_INPUT)
end

接続のためのボタンも追加

root_controller.rb
@btn_find = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |b|
      b.frame = CGRectMake(10, 100, 80, 50)
      b.setTitle("find", forState:UIControlStateNormal)
      b.addTarget(self, action:"find:", forControlEvents:UIControlEventTouchUpInside)
end
self.view.addSubview(@btn_find )

メソッドを追加

root_controller.rb
def find(sender)
    Konashi.find
end

def update_pio_input
    input = Konashi.digitalReadAll
    if input[0] == HIGH # オンボード入力端子
      @imagePickerController.takePicture
    end
end

findボタンを押すと、接続可能なKonashiの一覧が表示されます。
KONASHI_EVENT_UPDATE_PIO_INPUT はPIOの入力状態が変化したときのイベントとなります。
ライブラリがしっかりしているので、難しい事を考えなくてもiPhoneとKonashiの相互操作が可能になります。

そして次のステップへ!
http://youtu.be/loZad9r8FP8

ソース

Gemfile
source 'https://rubygems.org'

gem 'rake'
gem "motion-cocoapods"
Rakefile
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'

begin
  require 'bundler'
  Bundler.require
rescue LoadError
end

Motion::Project::App.setup do |app|
  # Use `rake config' to see complete project settings.
  app.name = ''
  app.development do
      app.provisioning_profile = ''
      app.codesign_certificate = ''
  end
  app.pods do
    pod 'konashi-ios-sdk'
  end
end
app_delegate.rb
class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(RootController.alloc.init)
    @window.rootViewController.wantsFullScreenLayout = true
    @window.makeKeyAndVisible
    true
  end
end
root_controller.rb
class RootController < UIViewController
  def viewDidLoad
    self.navigationController.setNavigationBarHidden(true, animated:false)
    self.view.backgroundColor = UIColor.whiteColor

    Konashi.initialize
    Konashi.addObserver(self, selector:"update_pio_input", name:KONASHI_EVENT_UPDATE_PIO_INPUT)

    @btn_find = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |b|
      b.frame = CGRectMake(10, 100, 80, 50)
      b.setTitle("find", forState:UIControlStateNormal)
      b.addTarget(self, action:"find:", forControlEvents:UIControlEventTouchUpInside)
    end

    @btn_show_camera = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |b|
      b.frame = CGRectMake(100, 100, 80, 50)
      b.setTitle("カメラ起動", forState:UIControlStateNormal)
      b.addTarget(self, action:"show_camera", forControlEvents:UIControlEventTouchUpInside)
    end

    [@btn_find, @btn_show_camera].each do |b|
      self.view.addSubview(b)
    end
  end

  def imagePickerController(picker, didFinishPickingImage:image, editingInfo:info)
    self.dismissViewControllerAnimated(true, completion:nil)
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

  end

  def imagePickerControllerDidCancel(picker)
    self.dismissViewControllerAnimated(true, completion:nil)
  end

  private

  def show_camera
    @imagePickerController = UIImagePickerController.alloc.init
    @imagePickerController.delegate = self
    @imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera
    self.presentViewController(@imagePickerController, animated:true, completion:nil)
  end

  def find(sender)
    Konashi.find
  end

  def update_pio_input
    input = Konashi.digitalReadAll
    if input[0] == HIGH # オンボード入力端子
      @imagePickerController.takePicture
    end
  end
end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2