Archive for the ‘ CG ’ Category

Android Adventures

Go to part 2…

I finally got a smartphone:  Samsung Captivate.  Why?  Mainly since the bar for making apps on them seems pretty low, which is something I’ve wanted to try for some time.  Plus after seeing these two posts…

And their reference to Processing For Android

…and considering my love for Processing
…it just all seemed to make sense.

The first thing I wanted to do though had nothing to do with Processing:  I wanted to get the ‘Scripting Layer For Android‘ (SL4A) installed on it so I could run Python (which I love more than Processing), in a shell, on my phone.  I documented how to do this on the Android Emulator in a previous post (and waxed lyrical on how I’d like to program on an Android phone in a post before that). However, to get all this working on a real phone, I had to jump through some hoops.

Disclaimer:    I’m a total smartphone\Android noob, and this is the first time I’ve ever written words like  ‘sideload’ or ‘root’.  I did this a day after getting the phone, never having owned a smartphone before.  So for most of  it I was just fumbling in the dark.  There may be easier ways, but I had a terrible time finding any kind of documentation on anything.  Any documentation I did use is linked to below.

A.  Time To Root:

AT&T in its infinite wisdom has disallowed ‘sideloading’ of apps onto the phone:  You can’t copy something onto the internal SD card and ‘install’.  If you try, you get a nice “You can only install apps from the Marketplace” window pop up.  Meaning, I can’t install SL4A  (or if you can, I just went through a lot more steps than needed…).   Talking with my buddies who’s smartphone kung-fu is much more powerful than my own, they told me to look into ‘rooting’ the phone:  Give me ‘root access’ on the phone I just bought (shouldn’t everyone have that?  I must have missed the politics class on that one).  This will allow me to be able to ‘sideload’ apps.

After a lot of websearching, I came across this form post, which describes how to install the “Unleash the Beast” root tool.  It seemed to do everything I needed it to in one step:  Clean off all the bloatware that came with the phone, disable the annoying on\off sound fx, plus a bunch of other stuff.  Most importantly though it gives you root access and enable sideloading.  I followed the directions on the site, and it worked flawlessly.  Not to say it didn’t stress me out during the process :)

Excellent, root complete!

B.  What happened to my USB?

Loooong story short:  When I first got the phone and before the root, my XP laptop wouldn’t detect it.  Went online to the Samsung web site to get drivers:  Site claimed it had drivers, but when I went to the download page:  “This product has no downloads”.  I contacted customer support explaining the issue, and their reply was to do the exact thing I had just done.  Robot.  More websearching:  Based on this post, I got a link to the drivers, and installed:  the  machine could now see my phone.  Win.

After the root, when I connected phone to computer, the computer again failed to detect it.  Tried plugging it into a different machine:  Same result.   The phone came with no directions, and the web page for downloads claiming it had the manual didn’t work (like mentioned above).  More websearching:

  • Settings -> Applications -> USB Settings -> Set to “Ask on connection” (was ‘Kies’).

Now when I plug the phone into the computer, the phone asks me ‘how it should be connected’:  Set to “Mass storage” (not Kies).  Then access the notifications menu (that one that scrolls down from the top), click on the “USB connected” item, and then “Mount”.  Suddenly the computer detects it.  What a pain…

C.  Get Astro File Manager

Part of  executing “Unleash The Beast” was the removal of the built-in “My Files” app.  To install the new tools I’d be getting I needed a new file manager:  Recommended in several places was “Astro File Manager“.  Successfully installed from the marketplace.

D.  Install SL4A with Python:

From the Android Scripting download page, I downloaded both sl4a_r2.apk and python_for_android_r1.apk.  After connecting the phone to computer and jumping through all the USB hoops, I copied them both to the \Android dir (I have no idea if that’s the right place).  Unmounting the the usb drive from the phone’s menu, I then accessed Astro:  Browsed to the /sdcard/Android dir, selected ‘sl4a_r2’ -> Open App Manager -> Install.  I then repeated this process on ‘python_for_android_r1’.  While python was installing, it also downloaded and installed all the other Python .zip files found in the SL4A download page as well.

When it was complete, I had the nice ‘SL4A’ app  ready for usage

Note: In hind-site I found the official doc for “Installing Interpreters” which saves you from having to install the Python shell by hand.  Main problem is the menu for his app on the phone isn’t that intuitive, and I totally missed how to do it the first time around.

Update: A buddy of mine did this, his install was even easier:  On his phone he simply browsed to the SL4A page, and clicked on the bar-code:  It installed automatically.  Magic.

E.  Do something with it:

Launch it ->  Settings Menu -> View -> Interpreters -> Python 2.6.2 ->  the shell opens:

>>> import this

Continue to Part 2

Basic Pygame Program

Whenever I begin a new Pygame application, I always start with a ‘basic program’ to begin things. Saves me from having to re-type all the simple stuff. Obviously a Pygame program can get far more complex than this, but everything has to start somewhere. Thought I’d post it up for reference.

"""
default.py
www.akeric.com - 2010-09-07
Default, base, initial setup for a pygame program.
In this case, black background, white circle follows mouse position, and
framerate is shown in the title-bar.
"""

#-------------------------------------------------------------------------------
# Imports & Inits
import sys
import pygame
from pygame.locals import *
pygame.init()

#-------------------------------------------------------------------------------
# Constants
VERSION = '1.0'
WIDTH = 512
HEIGHT = 512
FRAMERATE = 60

#-------------------------------------------------------------------------------
# Screen Setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
bgCol = Color('black')
clock = pygame.time.Clock()
moduleName = __file__.split('\\')[-1]

#-------------------------------------------------------------------------------
# Define helper functions, classes, etc...
def spam():
    pos = pygame.mouse.get_pos()
    pygame.draw.circle(screen, Color('white'), pos, 32)

#-------------------------------------------------------------------------------
# Main Program
def main():
    print "Running Python version: %s"%sys.version
    print "Running PyGame version: %s"%pygame.ver
    print "Running %s version: %s"%(moduleName, VERSION)
    looping = True

    # Main Loop-----------------------
    while looping:
        # Maintain our framerate, set caption, clear background:
        clock.tick(FRAMERATE)
        pygame.display.set_caption("%s - FPS: %.2f" %(moduleName,clock.get_fps()) )
        screen.fill(bgCol)

        # Detect for events-----------
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                looping = False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    looping = False

        # Do stuff!-------------------
        spam()

        # Update our display:---------
        pygame.display.flip()

#-------------------------------------------------------------------------------
# Execution from shell\icon:
if __name__ == "__main__":
    # Make running from IDE work better:
    sys.exit(main())

pylolgraph + drawing = pyloldraw

Based on my PyLolGraph app , PyLolDraw extends it by creating a simple ‘paint’ application for the Arduino/ lolshield.

See its page for source code and examples.

(any flickering you see is due to the video capture, and not the application)

http://www.akeric.com/blog/?page_id=1180

Python lolshield graphics

Or pylolgraph for short.  See latest info for this subject over on it’s official page.

My son and I soldered together a lolshield for the Arduino Duemilanove we picked up at the 2010 Maker Faire:

(Video Note: The flickering you see is based on the camera capture, it doesn’t actually do that…)

After making it… what to do with it?  I thought it would be fun to make a Python \ Arduino library that would allow you to send Pygame graphics to the lolshield, and that’s what I implemented.

There are two main parts to the library:

And, I’ve made an example Pygame application illustrating usage:

You can also download all three in a zip here pylolgraph.zip

How to use:

  • My system is WinXP.  Not tested on anything else.
  • Download pylolgraph.pde, and upload it to your Arduino.  Make sure the BAUD variable matches that in your Python module (below).
  • Download pylolgraph.py, put it in your Python path.
  • Download example01.py:  Set it’s BAUD variable to match that in pylolgraph.pde, and set its PORT variable to be the port your Arduino is on (will vary depending on your OS).
  • Start pylolgraph.pde on the Arduino.  Run example01.py from the command-line or double-click the icon: You should see a small Pygame window pop up:  When you click the LMB, a white donut will appear in the Pygame window.  The same donut should then appear on the lolshield.

Inspect the documentation in pylolgraph.py for more details on implementation.  Here is example pseudo-code for writing your own Pygame application with the code:

import pygame
import pylolgraph

# Setup some constants:
# Create a resolution that corresponds to the lolshield.
# See docs on the pylolgraph.LOLShield.setSurface() method
# for acceptable resolutions.
WIDTH, HEIGHT = (448, 288)
# Baud rate must be the same as setup in pylolgraph.pde
BAUD = 28800
# Port the Arduino is on, on my WinXP box.
PORT = 'COM4'

# Start Pygame application:
# Make surface that will have its data sent to lolshield:
screenSurf = pygame.display.set_mode((WIDTH, HEIGHT))

# Setup lolshield object:
lolshield = pylolgraph.LolShield(port=PORT, baudrate=BAUD)
# Set the surface that will have its pixel data sent to the Arduino\lolshield:
lolshield.setSurface(screenSurf)

# inside main loop:
    # Draw stuff to screenSurf.  Then:
    lolshield.sendData()

Python talks to the Arduino…

Sort of a followup to my previous post ‘Arduino talks to Processing, Python…‘, I thought I’d try going the other way:  Send commands to the Arduino from Python over the serial port.

My son and I recently soldered together a lolshield (‘lots of LEDs’) for the Arduino.  The ultimate goal for this is to have Python, via Pygame (most likely) run graphics on the lolshield as if it was a mini-flatscreen.  But first I need to get something sent from Python to Arduino, and this was surprisingly hard.

First, the internets came to my aid and I got some sample code allowing the Arduino to read a single character from the serial port, and blink an LED that number of times.  The source was by Tod E. Kurt, and I got it off his  page here.  My modified version is below (modified mainly with docs so I could understand what’s going on…)

// Arduino Code:
// Below, 'character' types are defined:  They hold 1 byte of data, 256 values.
// A char can be interpreted as a small number (0-255) or as a member of the
// ASCII set (which is what we deal with below).  Characters expressed as
// ASCII are surrounded in single-quotes, like '5'.
// Thus each char has a corresponding numeric value can thus be tested against.

int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the data from the serial port

void setup() {
 pinMode(ledPin,OUTPUT);    // declare the LED's pin as output
 Serial.begin(9600);        // connect to the serial port
}

void loop () {
  if (Serial.available()) {
    // For the below examples, let's pretend that the passed-in serial 
    // value is character '5'.
    // Since the declared variable val is an int, it converts the char 
    // value passed in into an int.
    // If char val = '5', the numeric representation is 53.
    val = Serial.read();      // read the serial port

    // If the stored value is a single-digit number, blink the LED 
    // that number of times.
    // Here we compare the int value of val against the int values 
    // of the string.
    // Characters '0' and '9' are equivalent to integer 48 and 57.
    if (val > '0' && val <= '9' ) {
      Serial.println(val);
      // Convert from char to int:
      // From above, int conversion of val, which is char '5', is 53.
      // int conversion of char '0' is 48.
      // 53-48 = 5  : blink that # of times
      val = val - '0';  
      for(int i=0; i<val; i++) {
        Serial.println("blink!");
        digitalWrite(ledPin,HIGH);
        delay(150);
        digitalWrite(ledPin, LOW);
        delay(150);
      }
    }
  }
}

Now for the Python stuff.  On the ‘Arduino: Playground‘ site, I found this example Python code using the pySerial libary:

>>> import serial # if you have not already done so
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)
>>> ser.write('5')

And it works, presuming you’re in the interactive Python shell.  If however you try to make a module out of the same code and execute it, it will fail:  No multi-blinking LED.  After researching several posts of other people that had similar issue (here, here, here, and here) I tracked down the main culprit:  When you make a connection to the serial port via Python, it sends a reset command to the Arduino. If you immediately send your data to the port immediately after, the Arduino won’t be initialized and ready to receive it.  To solve this, we simply pause the module for a bit while the Arduino does its thing:

# simpleSerialSend.py
import sys
import serial
import time
PORT = 'COM4' # The port my Arduino is on, on my WinXP box.

def main(val=5):
    # Open a connection to the serial port.  This will reset the Arduino, and
    # make the LED flash once:
    ser = serial.Serial(PORT)

    # Must given Arduino time to rest.
    # Any time less than this does not seem to work...
    time.sleep(1.5)

    # Now we can start sending data to it:
    written = ser.write(val)
    ser.close()
    print "Bytes Written to port:", written
    print "Value written to port: '%s'"%val

if __name__ == '__main__':
    args = sys.argv
    try:
        main(args[1])
    except IndexError:
        main()

This module can now be ran from the command prompt, and you can optionally pass in (from 0->9) the number of times you want the LED to blink:

c:\pyModules\simpleSerialSend.py 5

When this runs, you’ll see the LED blink once when the reset happens, a 1.5 second pause, and then the LED will blink 5 times.

What’s interesting is I tried this same thing with Processing, and failed, and I presume for the same reason:  The Processing sketch was executing too fast to allow the Arduino time to warm up.  And, there doesn’t seem to be a ‘pauses \ sleep \ wait’ function in Processing.  I tried using java.lang.Thread, but it didn’t seem to pause the sketch until it was done running.  So I need to do some more research there.  Had I got that working, then the title of this blog post would probably be different 😉