# gta02.py - A pygtk2 application to demonstrate some
#            features of the OpenMoko FreeRunner
#
# Version 0.1
#
# Authors: Thomas Wood <thomas@openedhand.com>
#
# Copyright (c) 2008 Thomas Wood
#
# gta02.py is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# gta02.py is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# TODO
#
# * Read the status of the LEDs at startup
# * Get the initial wireless information display asynchronously
# * Improve accelerometer display



from struct import unpack_from

import gobject,gtk
import os
import sys


# LED toggle button callback
def led_cb (button, filename):

  if button.get_active():
    state = "1"
  else:
    state = "0"

  f = open (filename, "w")
  f.write(state)
  f.close()


# Open the accelerometer
f = open("/dev/input/event3", "r")
x = y = z = 0
newmode = ""

# Read the accelerometer and unpack values
def accell_cb ( label):
    global x, y, z
    global newmode

    text = "x = %3d;  y = %3d;  z = %3d" % ( x, y, z )
    maxx = maxy = maxz = 0
    minx = miny = minz = 0

    block = f.read(16)
    if block[8] == "\x02":
        if block[10] == "\x00":
           x = unpack_from( "@l", block[12:] )[0]
           maxx, minx = max( x, maxx ), min( x, minx )
        if block[10] == "\x01":
           y = unpack_from( "@l", block[12:] )[0]
           maxy, miny = max( y, maxy ), min( y, miny )
        if block[10] == "\x02":
           z = unpack_from( "@l", block[12:] )[0]
           maxz, minz = max( z, maxz ), min( z, minz )
        text = "x = %3d;  y = %3d;  z = %3d" % ( x, y, z )

    if abs(x) < 600 and abs(y) > 600:
        newmode = "portrait"
    if abs(x) > 600 and abs(y) < 600:
        newmode = "landscape"

    label.set_text ("Values: %s\n%s" % (text, newmode))
    return True

# Scrape the output from iwlist to display a list of Access Points and Ad-Hoc
# cells in range

def read_wifi (label):
  output = os.popen ("iwlist eth0 scan|sed 's/          //'", "r")     
  text = output.read ()
  label.set_text (text)
  return True


# Create the user interface

win = gtk.Window()
win.connect('delete-event', gtk.main_quit)

vbox = gtk.VBox()
win.add (vbox)

l = gtk.Label()
l.set_markup ("<b>LEDs</b>")
vbox.pack_start (l)

box = gtk.HBox()
vbox.pack_start (box)


# LEDs

b = gtk.ToggleButton ("Red")
b.connect ("toggled", led_cb, "/sys/class/leds/gta02-aux:red/brightness")
box.pack_start (b)

b = gtk.ToggleButton ("Orange")
b.connect ("toggled", led_cb, "/sys/class/leds/gta02-power:orange/brightness")
box.pack_start (b)

b = gtk.ToggleButton ("Blue")
b.connect ("toggled", led_cb, "/sys/class/leds/gta02-power:blue/brightness")
box.pack_start (b)


# Wireless

l = gtk.Label()      
l.set_markup ("<b>Wireless</b>")                                           
vbox.pack_start (l) 

t = gtk.Label ("Scanning...")
t.set_alignment (0.0, 0.0)
# Read the wifi output every three minutes
gobject.timeout_add (180000, read_wifi, t)
# Get the first reading
read_wifi (t)

sw = gtk.ScrolledWindow()
sw.set_policy (gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
sw.add_with_viewport (t)
vbox.pack_start (sw)


# Accelerometer

l = gtk.Label()                                       
l.set_markup ("<b>Accelerometer</b>")                      
vbox.pack_start (l)

l = gtk.Label("")
vbox.pack_start (l)

# Read the accelerometer every 100 milliseconds
gobject.timeout_add (100, accell_cb, l)


# Show all widgets and start the main loop
win.show_all()
gtk.main()

# Close the accelerometer
f.close()
