First steps towards a robot: GPIO on the NGW100

After the arduous task of getting Python to cross-compile for the AVR32, the next step was to relax a bit and take a stab at getting some preliminary communication with the I/O ports going (which is the first step towards getting peripherals going, which means robot time). My co-conspirator on this project, Tim, was looking into CherryPy to allow web-based communication. This is an interesting idea, so I decided to take a stab at mixing it all together to make a web-based LED control program. Resulting code is after the break.

#!/home/user/install/bin/python
 
import cherrypy
 
class gpio_pin:
    def open(self, port_name, pin_mask, mode):
        self.port_name = port_name
        self.pin_mask = pin_mask
        self.mode = mode
        self.file = open(self.port_name, self.mode)
 
    def close(self):
        self.file.close()
 
    def toggle_on(self):
        if self.mode == 'w':
            self.file.write('\x00\x00\x00\x00')
            self.file.flush() 
 
    def toggle_off(self):
        if self.mode == 'w':
            self.file.write(self.pin_mask)
            self.file.flush()
 
class HelloWorld:
    def index(self, led=None, command=None):
        data = """<form action="index" method="post">"""
 
        if( led and command ):
           if( led == 'A' ):
               gpio.open("/dev/gpio1",'\x00\x08\x00\x00','w')
           if( led == 'B' ):
               gpio.open("/dev/gpio2",'\x00\x08\x00\x00','w')
           if( command == "On" ):
               gpio.toggle_on()
           if( command == "Off" ):
               gpio.toggle_off()
           gpio.close()
 
        data += """
<p>Led:
<select name="led">
<option>A</option>
<option>B</option>
</select></p>
<p>Command:
<select name="command">
<option>On</option>
<option>Off</option>
</select></p>
<input type="submit" value="Send">
</form>"""
        return data
    index.exposed = True
 
gpio = gpio_pin()
cherrypy.quickstart(HelloWorld())
This entry was posted in tech. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>