Reading Wii Nunchuck data in Python: Difference between revisions

From XPUB & Lens-Based wiki
mNo edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
__NOINDEX__
[[Category: Cookbook]]
'''Brief description:'''
Use the Wii Nunchuck to draw in the terminal and turn the drawing into a SVG, or in a broader sense: read values sensed by the Arduino into Python
Since I installed Linux I use the terminal a lot. I really like how you can use something text based and really basic to perform all kinds of complex tasks.
Since I installed Linux I use the terminal a lot. I really like how you can use something text based and really basic to perform all kinds of complex tasks.


After messing with SVG for a bit I thought it might be interesting to use the text based terminal as the interface to a SVG generating script. Also, I wanted to see how I could use the terminal together with an Arduino, so I used one to connect my Python script too a Wii controller (Nunchuck), which is used as input for the script.
After messing with SVG for a bit I thought it might be interesting to use the text based terminal as the interface to a SVG generating script. Also, I wanted to see how I could use the terminal together with an Arduino, so I used one to connect my Python script too a Wii controller (Nunchuck), which is used as input for the script.


The Nunchuck's joystick controls the cursor, the Z (large) button lets jou draw lines (press to select begin point, press again to select end point) and the c button creates an SVG and shows it.
The Nunchuck's joystick controls the cursor, the Z button lets jou draw lines (hold down the button and then move around) and the c button creates and shows the SVG.
Using the WiiChuck class the arduino reads the data which is then send to Python though a serial connection. To connect the Nunchuck to the Arduino without cutting the cable I used the Wiichuck adapter. Both the class and the adapter are available [http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/ here].
Using the WiiChuck class the arduino reads the data which is then send to Python though a serial connection. To connect the Nunchuck to the Arduino without cutting the cable I used the Wiichuck adapter. Both the class and the adapter are available [http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/ here].


Line 10: Line 17:
<references />
<references />


[[Image:pythonArduinoNunchuck.jpg]]
[[Image:pythonArduinoNunchuck.png]]
==Python code==
[[Image:wiiNunchuck.jpg]]
<source lang="python">
#=========================================================
# Import libs and set basic vars
#=========================================================
import serial, os, math
width, height = 50, 20
xpos, ypos = 0, 0
sx, sy = -1, -1
zbut, prevZbut, cbut = 0, 0, 0
cursorChar, prevCursorChar = "o", ""
svgScale = 10.0
svgHeightCorrection = 2


theScreen = [] #will hold all of the 'pixels'
==Arduino code==
fullRow = "" # the line of #s for use at the top and bottom
Using the WiiChuck adapter the Nunchuck is connected to analog pins 2, 3, 4 and 5. Pins 4 and 5 are the I2C communication pins and Pins 2 and 3 are used to supply power. You could also connect the Nunchuck by cutting the cables, but then you have to figure out which cable does what.
for x in range(0, width+2):
    fullRow += "#"


#initiate the screen arrays
This script gets several values from the Nunchuck: the x and y acceleration (e.g. if the controller is physically rotated), the state of the Z and C buttons (wether they are up or down) and the x and y position of the joystick, where ±128 should be the center position, but these values tend to be slightly different per device.
for y in range(0, height):
    xlist = []
    for x in range(0, width):
        xlist.append(" ")
    theScreen.append(xlist)


svgOut = ""
    #include <Wire.h>
#=========================================================
    #include "nunchuck_funcs.h"
# function to draw the arrays to the screen
#=========================================================
def display():
    global theScreen
    global cursorChar
    global xpos
    global ypos


     prevCursorChar = theScreen[ypos][xpos]
     int loop_cnt=0;
    theScreen[ypos][xpos] = cursorChar
    os.system("clear")
    print fullRow
    for y in theScreen:
        printLine = "#"
        for x in y:
            printLine += str(x)
        print printLine+"#"
    print fullRow
    theScreen[ypos][xpos] = prevCursorChar


#=========================================================
     byte accx,accy,zbut,cbut,joyx,joyy;
# function to draw a line
    int ledPin = 13;
#=========================================================
def drawLine(x1, y1, x2, y2):
     global theScreen
    global svgOut
    global svgScale
    global svgHeightCorrection
    bx1, by1, bx2, by2 = int(x1), int(y1), int(x2), int(y2)


     x1 = max(0, min(x1, width))
     void setup() {
    x2 = max(0, min(x2, width))
        Serial.begin(19200); //baudrate of the serial connection, be sure to set the same number in the Python script
     y1 = max(0, min(y1, height))
        nunchuck_setpowerpins(); // initiates pin 2 and 3 so they can power the nunchuck
     y2 = max(0, min(y2, height))
        nunchuck_init(); // send the initilization handshake
          
     }
    d = math.atan2(y2-y1, x2 - x1)*180/math.pi #d = direction
 
    d2 = d
     void loop() {
    if d2 < 0:
         if(loop_cnt > 100 ) { // every 100 msecs get new data
        d2 = (360-abs(d2))       
            loop_cnt = 0;
    dist = math.floor(math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)))
 
    lineChar = "*"
            nunchuck_get_data(); // receive the data from the device
    if (d2 >= 360-22.53 or d2 < 22.5) or (d2>=157.5 and d2<202.5):
            //nunchuck_print_data(); //if you want to show the data in the Serial monitor
      lineChar = "~"
 
    elif (d2 >= 22.5 and d2 < 67.5) or (d2>=202.5 and d2<247.5):
            accx  = nunchuck_accelx(); // ranges from approx 70 - 182
      lineChar = "\\"
            accy  = nunchuck_accely(); // ranges from approx 65 - 173
    elif (d2>=67.5 and d2<112.5) or (d2>=247.5 and d2<292.5):
            zbut = nunchuck_zbutton(); // either 0 or 1
      lineChar = "|"
            cbut = nunchuck_cbutton();  // dito
    elif (d2>=112.5 and d2<157.5) or (d2>=292.5 and d2<337.5):
            joyx = nunchuck_joyx();
      lineChar = "/"
            joyy = nunchuck_joyy();
      
      
    step = 0
            //print each of the values as a decimal value, followed by a comma
    while(step < dist):
            Serial.print((byte)accx,DEC);
        x1 += math.cos(d*math.pi/180)
            Serial.print(",");
        y1 += math.sin(d*math.pi/180)
            Serial.print((byte)accy,DEC);
        if x1>width:
            Serial.print(",");
             x1 = width-1
            Serial.print((byte)zbut,DEC);
             x2 = width-1
            Serial.print(",");
        elif x1<0:
             Serial.print((byte)cbut,DEC);
             x1 = 0
             Serial.print(",");
             x2 = 0
            Serial.print((byte)joyx,DEC);
        if y1>height:
             Serial.print(",");
             y1 = height-1
             Serial.print((byte)joyy,DEC);
             y2 = height-1
 
         elif y2<0:
             // when all values have been printed, print a newline to end the transmission
            y1 = 0
             Serial.println();
            y2 = 0
         }
         drawY = int(math.floor(y1))
        loop_cnt++;
        drawX = int(math.floor(x1))
         delay(1);
        if drawY < len(theScreen):
    }
            if drawX < len(theScreen[drawY]):
 
                theScreen[drawY][drawX] = lineChar
==Python code==  #=========================================================
        step = step + 1
  # Import libs and set basic vars
    theScreen[by1][bx1], theScreen[by2][bx2] = "*", "*"
  #=========================================================
  import serial, os, math
 
  width, height = 50, 20 # drawing area size
  xpos, ypos = 0, 0 # cursor location
  sx, sy = -1, -1 # stores the location to draw a line from
  zbut, prevZbut, cbut = 0, 0, 0 # wether the buttons are pressed
  cursorChar, prevCursorChar = "o", "" # the character used for the cursor
  svgScale = 10.0 # the svg is svgScale-times bigger than the terminal
  svgHeightCorrection = 2 # compensate for the line-height of the text


    svgOut = svgOut + """<line x1="{0}" y1="{1}" x2="{2}" y2="{3}"
  #theScren will hold all of the 'pixels'
    style="stroke:rgb(0,0,0);stroke-width:{4}"/>""".format(bx1*svgScale, by1*(svgScale*svgHeightCorrection), bx2*svgScale, by2*(svgScale*svgHeightCorrection), svgScale)
  theScreen = []
  # fullRow is the line of #s for use at the top and bottom
  fullRow = ""  
  for x in range(0, width+2):
      fullRow += "#"


#=========================================================
  #initiate the screen arrays, fill them with spaces
# Save image, open and quit
  for y in range(0, height):
#=========================================================
      xlist = []
def saveAndOpen():
      for x in range(0, width):
    global svgOut
          xlist.append(" ")
    global width
      theScreen.append(xlist)
    global height
    global svgScale
    svgOut = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="{0}" height="{1}">""".format(svgScale*width, svgScale*(height*svgHeightCorrection)) + svgOut
    svgOut = svgOut + "</svg>"


    fo = open("image.svg", "wb")
  #variable to hold the svg file
    fo.write( svgOut);
  svgOut = ""
    fo.close()
    os.system("google-chrome image.svg &")


#=========================================================
  #=========================================================
# Main loop
  # function to draw the arrays to the screen
#=========================================================
  #=========================================================
loop = 0
  def display():
ser = serial.Serial('/dev/ttyACM0', 19200)
      global theScreen
while 1:
      global cursorChar
    loop = loop + 1
      global xpos
    serialData = ser.readline()
      global ypos
    serialData = serialData.replace("\r\n", "")
    dataList = serialData.split(',')


    if len(dataList)==6:
      prevCursorChar = theScreen[ypos][xpos]  
        prevZbut = zbut
      theScreen[ypos][xpos] = cursorChar
        accx = float(dataList[0])
      os.system("clear")
        accy = float(dataList[1])
      print fullRow
        zbut = float(dataList[2])
      for y in theScreen:
        cbut = float(dataList[3])
          printLine = "#"
        joyx = float(dataList[4])
          for x in y:
        joyy = float(dataList[5])
              printLine += str(x)
       
          print printLine+"#"
        #move cursor around
      print fullRow
        if joyx>150:
      theScreen[ypos][xpos] = prevCursorChar
            xpos = xpos + 1
        elif joyx<100:
            xpos = xpos - 1


        if joyy>150:
  #=========================================================
            ypos = ypos - 1
  # function to draw a line given a start and end xy position
        elif joyy<100:
  # useInSvg determines of the line should be saved in the svg
            ypos = ypos + 1
  # the line that is drawn while still holding down the button
  # will not be saved (to prevent filling the whole screen when
  # moving around )
  #=========================================================
  def drawLine(x1, y1, x2, y2, useInSvg):
      global theScreen
      global svgOut
      global svgScale
      global svgHeightCorrection


        xpos = max(0, min(xpos, width-1))
      bx1, by1, bx2, by2 = int(x1), int(y1), int(x2), int(y2) #remember the original values
        ypos = max(0, min(ypos, height-1))


        #remember previous char on cursor location
      x1 = max(0, min(x1, width))
         prevCursorChar = theScreen[ypos][xpos]
      x2 = max(0, min(x2, width))
          
      y1 = max(0, min(y1, height))
         if cbut==1:
      y2 = max(0, min(y2, height))
            saveAndQuit()
         
      #d = direction
      d = math.atan2(y2-y1, x2 - x1)*180/math.pi
      d2 = d
     
      # calculate a different character for each direction the line can go
      if d2 < 0:
          d2 = (360-abs(d2))       
      dist = math.floor(math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)))
      lineChar = "*"
      if (d2 >= 360-22.53 or d2 < 22.5) or (d2>=157.5 and d2<202.5):
         lineChar = "~"
      elif (d2 >= 22.5 and d2 < 67.5) or (d2>=202.5 and d2<247.5):
         lineChar = "\\"
      elif (d2>=67.5 and d2<112.5) or (d2>=247.5 and d2<292.5):
         lineChar = "|"
      elif (d2>=112.5 and d2<157.5) or (d2>=292.5 and d2<337.5):
        lineChar = "/"
     
      step = 0
      while(step < dist):
          x1 += math.cos(d*math.pi/180)
          y1 += math.sin(d*math.pi/180)
          if x1>width:
              x1 = width-1
              x2 = width-1
          elif x1<0:
              x1 = 0
              x2 = 0
          if y1>height:
              y1 = height-1
              y2 = height-1
          elif y2<0:
              y1 = 0
              y2 = 0
          drawY = int(math.floor(y1))
          drawX = int(math.floor(x1))
          if drawY < len(theScreen):
              if drawX < len(theScreen[drawY]):
                  theScreen[drawY][drawX] = lineChar
          step = step + 1
      theScreen[by1][bx1], theScreen[by2][bx2] = "*", "*"


        cursorChar = "o"
      if useInSvg==1:
        if zbut==1:#button down
          svgOut = svgOut + """<line x1="{0}" y1="{1}" x2="{2}" y2="{3}"
            cursorChar = "x"
      style="stroke:rgb(0,0,0);stroke-width:{4}"/>""".format(bx1*svgScale, by1*(svgScale*svgHeightCorrection), bx2*svgScale, by2*(svgScale*svgHeightCorrection), svgScale)
        elif zbut==0 and prevZbut==1: #button released
 
            if sx==-1:#first time releasing button
 
                sx, sy = xpos, ypos
  #=========================================================
                theScreen[sy][sx] = "*"
  # Save svg image and show it in google chrome
            else:#button released, draw line
  #=========================================================
                drawLine(sx, sy, xpos, ypos)
  def saveOpenAndQuit():
                sx, sx = -1, -1
      global svgOut
   
      global width
    display()
      global height
</source>
      global svgScale
      svgOut = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="{0}" height="{1}">""".format(svgScale*width, svgScale*(height*svgHeightCorrection)) + svgOut
      svgOut = svgOut + "</svg>"
 
      fo = open("image.svg", "wb")
      fo.write( svgOut);
      fo.close()
      os.system("google-chrome image.svg &")
 
  #=========================================================
  # Main loop
  # Read data from Arduino, update cursor position and such, display screen
  #=========================================================
  # open a serial connection to the Arduino.
  # Make sure the last number, the baudrate, is the same as in your Arduino code
  ser = serial.Serial('/dev/ttyACM0', 19200)
 
  # keep looping forever
  while 1:
      # read the serial data coming in and split it on the comma so you'll have a list you can work with.
      serialData = ser.readline()
      serialData = serialData.replace("\r\n", "")
      dataList = serialData.split(',')


==Arduino code==
      # only go on if there are 6 elements
<source lang="java">
      # this prevents errors when somehow the script didn't receive the full message
#include <Wire.h>
      if len(dataList)==6:
#include "nunchuck_funcs.h"
          prevZbut = zbut
          accx = float(dataList[0])
          accy = float(dataList[1])
          zbut = float(dataList[2])
          cbut = float(dataList[3])
          joyx = float(dataList[4])
          joyy = float(dataList[5])
         
          #move cursor around
          # using 150/100 leaves some room for weird offsets that the controller might have
          if joyx>150:
              xpos = xpos + 1
          elif joyx<100:
              xpos = xpos - 1


int loop_cnt=0;
          if joyy>150:
              ypos = ypos - 1
          elif joyy<100:
              ypos = ypos + 1


byte accx,accy,zbut,cbut,joyx,joyy;
          # limit the cursor position to positions in the drawing area
int ledPin = 13;
          xpos = max(0, min(xpos, width-1))
          ypos = max(0, min(ypos, height-1))


          #remember previous char on cursor location
          prevCursorChar = theScreen[ypos][xpos]
         
          if cbut==1:
              saveOpenAndQuit()
          cursorChar = "o"


void setup()
          if zbut==1 and prevZbut==0:#click, starting to draw
{
              sx, sy = xpos, ypos
  Serial.begin(19200);
              theScreen[ypos][xpos] = "*"
  nunchuck_setpowerpins();
              cursorChar = "x"
  nunchuck_init(); // send the initilization handshake
              display()
}
          elif zbut==1 and prevZbut==1:#dragging around
              cursorChar = "*"
              #somehow I need to iterate over the tempScreen and theScreen arrays
              #instead of just copying them with .extend
              tempScreen = []
              for y in range(0, height):
                  xlist = []
                  for x in range(0, width):
                      xlist.append(theScreen[y][x])
                  tempScreen.append(xlist)


void loop()
              drawLine(sx, sy, xpos, ypos, 0)
{
              display()
  if( loop_cnt > 100 ) { // every 100 msecs get new data
              theScreen = []
    loop_cnt = 0;
              for y in range(0, height):
                  xlist = []
                  for x in range(0, width):
                      xlist.append(tempScreen[y][x])
                  theScreen.append(xlist)


    nunchuck_get_data();
          elif zbut==0 and prevZbut==1:#released
    //nunchuck_print_data();
              theScreen[ypos][xpos] = "*"
              cursorChar = "x"
              if sx!=xpos or sy!=ypos:
                  drawLine(sx, sy, xpos, ypos, 1)
              sx, sy = -1, -1
              display()


    accx  = nunchuck_accelx(); // ranges from approx 70 - 182
          elif zbut==0 and prevZbut==0:#doing nuthin'
    accy  = nunchuck_accely(); // ranges from approx 65 - 173
              cursorChar = "o"
    zbut = nunchuck_zbutton();
              display()
    cbut = nunchuck_cbutton();
    joyx = nunchuck_joyx();
    joyy = nunchuck_joyy();
   
    Serial.print((byte)accx,DEC);
    Serial.print(",");
    Serial.print((byte)accy,DEC);
    Serial.print(",");
    Serial.print((byte)zbut,DEC);
    Serial.print(",");
    Serial.print((byte)cbut,DEC);
    Serial.print(",");
    Serial.print((byte)joyx,DEC);
    Serial.print(",");
    Serial.print((byte)joyy,DEC);
    Serial.println();
  }
  loop_cnt++;
  delay(1);
}
</source>

Latest revision as of 21:42, 4 September 2022


Brief description: Use the Wii Nunchuck to draw in the terminal and turn the drawing into a SVG, or in a broader sense: read values sensed by the Arduino into Python


Since I installed Linux I use the terminal a lot. I really like how you can use something text based and really basic to perform all kinds of complex tasks.

After messing with SVG for a bit I thought it might be interesting to use the text based terminal as the interface to a SVG generating script. Also, I wanted to see how I could use the terminal together with an Arduino, so I used one to connect my Python script too a Wii controller (Nunchuck), which is used as input for the script.

The Nunchuck's joystick controls the cursor, the Z button lets jou draw lines (hold down the button and then move around) and the c button creates and shows the SVG. Using the WiiChuck class the arduino reads the data which is then send to Python though a serial connection. To connect the Nunchuck to the Arduino without cutting the cable I used the Wiichuck adapter. Both the class and the adapter are available here.

[Disclaimer] Since I wrote the code from scratch and added 'features' as I went along I bet it's far from optimized. But it seems to do the job.


PythonArduinoNunchuck.png WiiNunchuck.jpg

Arduino code

Using the WiiChuck adapter the Nunchuck is connected to analog pins 2, 3, 4 and 5. Pins 4 and 5 are the I2C communication pins and Pins 2 and 3 are used to supply power. You could also connect the Nunchuck by cutting the cables, but then you have to figure out which cable does what.

This script gets several values from the Nunchuck: the x and y acceleration (e.g. if the controller is physically rotated), the state of the Z and C buttons (wether they are up or down) and the x and y position of the joystick, where ±128 should be the center position, but these values tend to be slightly different per device.

   #include <Wire.h>
   #include "nunchuck_funcs.h" 
   int loop_cnt=0;
   byte accx,accy,zbut,cbut,joyx,joyy;
   int ledPin = 13;
   void setup() {
       Serial.begin(19200); //baudrate of the serial connection, be sure to set the same number in the Python script
       nunchuck_setpowerpins(); // initiates pin 2 and 3 so they can power the nunchuck
       nunchuck_init(); // send the initilization handshake
   }
   void loop() {
       if(loop_cnt > 100 ) { // every 100 msecs get new data
           loop_cnt = 0;
           nunchuck_get_data(); // receive the data from the device
           //nunchuck_print_data(); //if you want to show the data in the Serial monitor
           accx  = nunchuck_accelx(); // ranges from approx 70 - 182
           accy  = nunchuck_accely(); // ranges from approx 65 - 173
           zbut = nunchuck_zbutton(); // either 0 or 1
           cbut = nunchuck_cbutton();  // dito
           joyx = nunchuck_joyx();
           joyy = nunchuck_joyy(); 
   
           //print each of the values as a decimal value, followed by a comma
           Serial.print((byte)accx,DEC);
           Serial.print(",");
           Serial.print((byte)accy,DEC);
           Serial.print(",");
           Serial.print((byte)zbut,DEC);
           Serial.print(",");
           Serial.print((byte)cbut,DEC);
           Serial.print(",");
           Serial.print((byte)joyx,DEC);
           Serial.print(",");
           Serial.print((byte)joyy,DEC);
           // when all values have been printed, print a newline to end the transmission
           Serial.println();
       }
       loop_cnt++;
       delay(1);
   }

Python code== #=======================================================

 # Import libs and set basic vars
 #=========================================================
 import serial, os, math
 width, height = 50, 20 # drawing area size
 xpos, ypos = 0, 0 # cursor location
 sx, sy = -1, -1 # stores the location to draw a line from
 zbut, prevZbut, cbut = 0, 0, 0 # wether the buttons are pressed
 cursorChar, prevCursorChar = "o", "" # the character used for the cursor
 svgScale = 10.0 # the svg is svgScale-times bigger than the terminal
 svgHeightCorrection = 2 # compensate for the line-height of the text
 #theScren will hold all of the 'pixels'
 theScreen = [] 
 # fullRow is the line of #s for use at the top and bottom
 fullRow = "" 
 for x in range(0, width+2):
     fullRow += "#"
 #initiate the screen arrays, fill them with spaces
 for y in range(0, height):
     xlist = []
     for x in range(0, width):
         xlist.append(" ")
     theScreen.append(xlist)
 #variable to hold the svg file
 svgOut = ""
 #=========================================================
 # function to draw the arrays to the screen
 #=========================================================
 def display():
     global theScreen
     global cursorChar
     global xpos
     global ypos
     prevCursorChar = theScreen[ypos][xpos] 
     theScreen[ypos][xpos] = cursorChar
     os.system("clear")
     print fullRow
     for y in theScreen:
         printLine = "#"
         for x in y:
             printLine += str(x)
         print printLine+"#"
     print fullRow
     theScreen[ypos][xpos] = prevCursorChar
 #=========================================================
 # function to draw a line given a start and end xy position
 # useInSvg determines of the line should be saved in the svg
 # the line that is drawn while still holding down the button
 # will not be saved (to prevent filling the whole screen when
 # moving around )
 #=========================================================
 def drawLine(x1, y1, x2, y2, useInSvg):
     global theScreen
     global svgOut
     global svgScale
     global svgHeightCorrection
     bx1, by1, bx2, by2 = int(x1), int(y1), int(x2), int(y2) #remember the original values
     x1 = max(0, min(x1, width))
     x2 = max(0, min(x2, width))
     y1 = max(0, min(y1, height))
     y2 = max(0, min(y2, height))
         
     #d = direction
     d = math.atan2(y2-y1, x2 - x1)*180/math.pi 
     d2 = d
     
     # calculate a different character for each direction the line can go
     if d2 < 0:
         d2 = (360-abs(d2))        
     dist = math.floor(math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)))
     lineChar = "*"
     if (d2 >= 360-22.53 or d2 < 22.5) or (d2>=157.5 and d2<202.5):
       lineChar = "~"
     elif (d2 >= 22.5 and d2 < 67.5) or (d2>=202.5 and d2<247.5):
       lineChar = "\\"
     elif (d2>=67.5 and d2<112.5) or (d2>=247.5 and d2<292.5):
       lineChar = "|"
     elif (d2>=112.5 and d2<157.5) or (d2>=292.5 and d2<337.5):
       lineChar = "/"
     
     step = 0
     while(step < dist):
         x1 += math.cos(d*math.pi/180)
         y1 += math.sin(d*math.pi/180)
         if x1>width:
             x1 = width-1
             x2 = width-1
         elif x1<0:
             x1 = 0
             x2 = 0
         if y1>height:
             y1 = height-1
             y2 = height-1
         elif y2<0:
             y1 = 0
             y2 = 0
         drawY = int(math.floor(y1))
         drawX = int(math.floor(x1))
         if drawY < len(theScreen):
             if drawX < len(theScreen[drawY]):
                 theScreen[drawY][drawX] = lineChar
         step = step + 1
     theScreen[by1][bx1], theScreen[by2][bx2] = "*", "*"
     if useInSvg==1:
         svgOut = svgOut + """<line x1="{0}" y1="{1}" x2="{2}" y2="{3}"
     style="stroke:rgb(0,0,0);stroke-width:{4}"/>""".format(bx1*svgScale, by1*(svgScale*svgHeightCorrection), bx2*svgScale, by2*(svgScale*svgHeightCorrection), svgScale)


 #=========================================================
 # Save svg image and show it in google chrome 
 #=========================================================
 def saveOpenAndQuit():
     global svgOut
     global width
     global height
     global svgScale
     svgOut = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="{0}" height="{1}">""".format(svgScale*width, svgScale*(height*svgHeightCorrection)) + svgOut
     svgOut = svgOut + "</svg>"
     fo = open("image.svg", "wb")
     fo.write( svgOut);
     fo.close()
     os.system("google-chrome image.svg &")
 #=========================================================
 # Main loop
 # Read data from Arduino, update cursor position and such, display screen
 #=========================================================
 # open a serial connection to the Arduino. 
 # Make sure the last number, the baudrate, is the same as in your Arduino code
 ser = serial.Serial('/dev/ttyACM0', 19200)
 # keep looping forever
 while 1:
     # read the serial data coming in and split it on the comma so you'll have a list you can work with.
     serialData = ser.readline()
     serialData = serialData.replace("\r\n", "")
     dataList = serialData.split(',')
     # only go on if there are 6 elements
     # this prevents errors when somehow the script didn't receive the full message
     if len(dataList)==6: 
         prevZbut = zbut
         accx = float(dataList[0])
         accy = float(dataList[1])
         zbut = float(dataList[2])
         cbut = float(dataList[3])
         joyx = float(dataList[4])
         joyy = float(dataList[5])
         
         #move cursor around
         # using 150/100 leaves some room for weird offsets that the controller might have
         if joyx>150: 
             xpos = xpos + 1
         elif joyx<100:
             xpos = xpos - 1
         if joyy>150:
             ypos = ypos - 1
         elif joyy<100:
             ypos = ypos + 1
         # limit the cursor position to positions in the drawing area
         xpos = max(0, min(xpos, width-1)) 
         ypos = max(0, min(ypos, height-1))
         #remember previous char on cursor location
         prevCursorChar = theScreen[ypos][xpos]
         
         if cbut==1:
             saveOpenAndQuit()
         cursorChar = "o"
         if zbut==1 and prevZbut==0:#click, starting to draw
             sx, sy = xpos, ypos
             theScreen[ypos][xpos] = "*"
             cursorChar = "x"
             display()
         elif zbut==1 and prevZbut==1:#dragging around
             cursorChar = "*"
             #somehow I need to iterate over the tempScreen and theScreen arrays
             #instead of just copying them with .extend
             tempScreen = []
             for y in range(0, height):
                 xlist = []
                 for x in range(0, width):
                     xlist.append(theScreen[y][x])
                 tempScreen.append(xlist)
             drawLine(sx, sy, xpos, ypos, 0)
             display()
             theScreen = []
             for y in range(0, height):
                 xlist = []
                 for x in range(0, width):
                     xlist.append(tempScreen[y][x])
                 theScreen.append(xlist)
         elif zbut==0 and prevZbut==1:#released
             theScreen[ypos][xpos] = "*"
             cursorChar = "x"
             if sx!=xpos or sy!=ypos:
                 drawLine(sx, sy, xpos, ypos, 1)
             sx, sy = -1, -1
             display()
         elif zbut==0 and prevZbut==0:#doing nuthin'
             cursorChar = "o"
             display()