ch07

packingVBox.py

#!/usr/bin/env python
# packingVBox.py - Pack many widgets on top of each other.  
# (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
 
import gtk
 
window=gtk.Window()
window.connect("destroy", gtk.main_quit)
 
vbox=gtk.VBox() 
window.add(vbox) 
 
b1=gtk.Button("Button 1") 
vbox.pack_start(b1) 
 
b2=gtk.Button("Button 2") 
vbox.pack_start(b2)
 
window.show_all()
gtk.main()

remoteControl.py

#!/usr/bin/env python
# remoteControl.py - Graphical user interface for remote control. 
# (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
 
import serial 
import gtk
 
ser=None    # global variable 
 
def sendSerial(widget, ch): 
    global ser 
    print("Sending "+ch) 
    ser.write(ch) 
 
def main(): 
    global ser 
    # File name might differ between Linux, Windows, and Mac OS X 
    ser = serial.Serial('/dev/ttyUSB0', 9600)
 
    if (ser): 
        print("Serial port " + ser.portstr + " opened.")
 
    window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
    window.connect("destroy", gtk.main_quit) 
 
    vbox=gtk.VBox() 
    window.add(vbox)
    row1=gtk.HBox() 
    off1 = gtk.Button("1 off") 
    off1.connect("clicked", sendSerial, "2") 
    row1.pack_start(off1) 
    vbox.pack_start(row1)
    on1 = gtk.Button("1 on") 
    on1.connect("clicked", sendSerial, "8")
    row1.pack_start(on1)
 
    row2=gtk.HBox() 
    vbox.pack_start(row2) # vertical box starts at the bottom
    off2 = gtk.Button("2 off")
    off2.connect("clicked", sendSerial, "3")
    row2.pack_start(off2)
    on2 = gtk.Button("2 on")
    on2.connect("clicked", sendSerial, "7")
    row2.pack_start(on2)
 
    row3=gtk.HBox()
    vbox.pack_start(row3)
    off3 = gtk.Button("3 off")
    off3.connect("clicked", sendSerial, "4")
    row3.pack_start(off3)
    on3 = gtk.Button("3 on")
    on3.connect("clicked", sendSerial, "6")
    row3.pack_start(on3)
 
    window.show_all() 
    gtk.main() 
    print ("Thanks for using BotBook.com remote control.")
 
if __name__ == "__main__": 
    main()

boxInsideBox.py

#!/usr/bin/env python
# boxInsideBox.py - Combine boxes to lay out elements 
# in rows and columns. 
# (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
 
import gtk
 
window=gtk.Window()
window.connect("destroy", gtk.main_quit)
 
vbox=gtk.VBox()
window.add(vbox)
 
b1=gtk.Button("Button 1")
vbox.pack_start(b1) 
 
hbox=gtk.HBox()
vbox.pack_start(hbox) 
 
b2=gtk.Button("Button 2")
hbox.pack_start(b2) 
 
b3=gtk.Button("Button 3")
hbox.pack_start(b3) 
 
window.show_all()
gtk.main()

ledSerialControl.pde

// ledSerialControl.pde - Control led via serial
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
 
int relayPin=13;

void setup()
{
    pinMode(relayPin, OUTPUT);
    Serial.begin(9600);
    Serial.println("Control relay with serial - ready. www.BotBook.com");
}

void loop()
{
    char ch = Serial.read();
    if (ch=='2') {
        digitalWrite(relayPin, LOW);
    }
    if (ch=='8') {
        digitalWrite(relayPin, HIGH);
    }
}

sixRelays.pde

// sixRelays.pde - Control relays with a computer 
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com

ledPin=13; 
 
void relayPulse(int pin) 
{
    pinMode(pin, OUTPUT); 
    digitalWrite(ledPin, HIGH); 
    digitalWrite(pin, HIGH);
    delay(1000); 
    digitalWrite(pin, LOW); 
    digitalWrite(ledPin, LOW);
}
 
int charToInt(char ch) 
{
    return ch-'0';
}
 
void setup()
{
    pinMode(ledPin, OUTPUT); 
    Serial.begin(9600); 
    Serial.println("Relay controller ready - www.BotBook.com");
}
 
void loop()
{
    if ( Serial.available()) { 
        char ch = Serial.read(); 
        int pin = charToInt(ch); 
        relayPulse(pin); 
    }
    delay(20); 
}

ledRelay.pde

// ledRelay.pde - Control led with a relay
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
 
int relayPin = 2;
void setup()
{
  pinMode(relayPin, OUTPUT);
}

void loop()
{
  digitalWrite(relayPin, HIGH);
  delay(1000);
  digitalWrite(relayPin, LOW);
  delay(1000);
}