About_josh_adams

Josh Adams

CTO / Principal / Lead Developer

Josh Adams is a developer and architect with over eleven years of professional experience building production-quality software and managing projects. Josh is isotope|eleven's lead architect, and is responsible for overseeing architectural decisions and translating customer requirements into working software. Josh graduated from the University of Alabama at Birmingham (UAB) with Bachelor of Science degrees in both Mathematics and Philosophy. He also occasionally provides Technical Review for Apress Publishing, specifically regarding Arduino microprocessors. When he's not working, Josh enjoys spending time with his family.

Blog Posts

About_josh_adams

April 12th, 2013 09:49

OCTOPUS IS WORKING

Ton of links today...

That's all I've got for now. Expecting to have a fun post later today as well...

Tags:

So I got a bit more involved after my experiments last week.

This week I did the following:

  • Got the LED Toggler Ruby script from last week exposing a toggle-able LED object over the network with drb under MRI on the Raspberry Pi.
  • Built an Android app.
  • Inside of that Android app I embedded a JRuby ScriptingContainer.
  • In that ScriptingContainer I evaluated some ruby.
  • In that ruby script I connected to the aforementioned drb object.
  • On button press in the Android app, I call the #toggle method of the drb object.

This all combines to have the effect of an android app to toggle an LED on the network. This is, of course, extremely awesome and extensible. See the video above for proof.

The code for these things is available here:

I'll also just put the relevant bits of code in the post here:

server.rb

require 'drb/drb'
require 'pi_piper'

# The URI for the server to connect to
URI="druby://192.168.1.76:8787"

class Toggler
  attr_accessor :pin, :state

  def initialize
    @pin = PiPiper::Pin.new(pin: 15, direction: :out)
    @state = false
  end

  def toggle
    state ? pin.on : pin.off
    @state = !state
    puts 'toggled'
  end
end

# The object that handles requests on the server
FRONT_OBJECT=Toggler.new

$SAFE = 1   # disable eval() and friends

DRb.start_service(URI, FRONT_OBJECT)
# Wait for the drb server thread to finish before exiting.
DRb.thread.join

MainActivity.java

package com.isotope11.ledclicker;

import java.util.ArrayList;
import java.util.List;

import org.apache.bsf.BSFException;
import org.jruby.embed.ScriptingContainer;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

  protected final String TAG = MainActivity.class.toString();
  protected ScriptingContainer mRubyContainer;
  protected String mDrbResult;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.setProperty("jruby.bytecode.version", "1.5");
    mRubyContainer = new ScriptingContainer();
    List<String> loadPaths = new ArrayList<String>();
    loadPaths.add("jruby.home/lib/ruby/shared");
    loadPaths.add("jruby.home/lib/ruby/1.8");
    mRubyContainer.setLoadPaths(loadPaths);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  public void onLEDClickerClick(View view) {
    RubyRunner rubyRunner = new RubyRunner();
    rubyRunner.execute();
  }

  public void handleDrbResult() {
    Toast.makeText(this, mDrbResult, Toast.LENGTH_LONG).show();
  }

  private class RubyRunner extends AsyncTask<Object, Void, String> {

    @Override
    protected String doInBackground(Object... arg0) {
      String rubyDrbClient = "require 'drb/drb';SERVER_URI='druby://192.168.1.76:8787';DRb.start_service;toggler = DRbObject.new_with_uri(SERVER_URI);toggler.toggle";
      String result = (String) mRubyContainer.runScriptlet(rubyDrbClient);
      return result;
    }

    @Override
    protected void onPostExecute(String result){
      mDrbResult = result;
      handleDrbResult();
    }

  }
}
Tags:
About_josh_adams

April 5th, 2013 07:55

Yup

Here's a video Seth shared re: impressive ruby productivity with tmux and vim

Tags:
About_josh_adams

March 29th, 2013 17:37

Links are so close......but first watermelon cat.

watermelon cat

And if you watch one video this week, let it be Guy Steele's Growing a Language:

Tags:
About_josh_adams

March 28th, 2013 23:00

So had to continue the last post. Here's an LED that turns on when you tweet at it :)

Tags: