Reading Wii Nunchuck data in Python: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(9 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 11: Line 18:


[[Image:pythonArduinoNunchuck.png]]
[[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
==Arduino code==
xpos, ypos = 0, 0
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.
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'
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.
fullRow = "" # the line of #s for use at the top and bottom
for x in range(0, width+2):
    fullRow += "#"


#initiate the screen arrays
    #include <Wire.h>
for y in range(0, height):
     #include "nunchuck_funcs.h"  
     xlist = []
    for x in range(0, width):
        xlist.append(" ")
    theScreen.append(xlist)


svgOut = ""
    int loop_cnt=0;
#=========================================================
# function to draw the arrays to the screen
#=========================================================
def display():
    global theScreen
    global cursorChar
    global xpos
    global ypos


     prevCursorChar = theScreen[ypos][xpos]
     byte accx,accy,zbut,cbut,joyx,joyy;
    theScreen[ypos][xpos] = cursorChar
     int ledPin = 13;
    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


#=========================================================
    void setup() {
# function to draw a line
        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
def drawLine(x1, y1, x2, y2, useInSvg):
        nunchuck_init(); // send the initilization handshake
    global theScreen
     }
    global svgOut
    global svgScale
     global svgHeightCorrection


     bx1, by1, bx2, by2 = int(x1), int(y1), int(x2), int(y2)
     void loop() {
        if(loop_cnt > 100 ) { // every 100 msecs get new data
            loop_cnt = 0;


    x1 = max(0, min(x1, width))
            nunchuck_get_data(); // receive the data from the device
    x2 = max(0, min(x2, width))
            //nunchuck_print_data(); //if you want to show the data in the Serial monitor
    y1 = max(0, min(y1, height))
 
    y2 = max(0, min(y2, height))
            accx  = nunchuck_accelx(); // ranges from approx 70 - 182
       
            accy  = nunchuck_accely(); // ranges from approx 65 - 173
    d = math.atan2(y2-y1, x2 - x1)*180/math.pi #d = direction
            zbut = nunchuck_zbutton(); // either 0 or 1
    d2 = d
            cbut = nunchuck_cbutton();  // dito
    if d2 < 0:
            joyx = nunchuck_joyx();
        d2 = (360-abs(d2))       
            joyy = nunchuck_joyy();
    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
            //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:
             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:
            // when all values have been printed, print a newline to end the transmission
         svgOut = svgOut + """<line x1="{0}" y1="{1}" x2="{2}" y2="{3}"
            Serial.println();
    style="stroke:rgb(0,0,0);stroke-width:{4}"/>""".format(bx1*svgScale, by1*(svgScale*svgHeightCorrection), bx2*svgScale, by2*(svgScale*svgHeightCorrection), svgScale)
        }
         loop_cnt++;
        delay(1);
    }


def drawEllipse(x, y):
==Python code==  #=========================================================
    global svgOut
  # Import libs and set basic vars
    print "at: "+str(x)+", "+str(y)
  #=========================================================
    svgOut = svgOut + """<ellipse cx="{0}" cy="{1}" rx="{2}" ry="{2}"
   import serial, os, math
   style="fill:black;"/>""".format(float(x)*2*svgScale, float(y)*svgScale, svgScale/2)


#=========================================================
  width, height = 50, 20 # drawing area size
# Save image, open and quit
  xpos, ypos = 0, 0 # cursor location
#=========================================================
  sx, sy = -1, -1 # stores the location to draw a line from
def saveOpenAndQuit():
  zbut, prevZbut, cbut = 0, 0, 0 # wether the buttons are pressed
    global svgOut
  cursorChar, prevCursorChar = "o", "" # the character used for the cursor
    global width
  svgScale = 10.0 # the svg is svgScale-times bigger than the terminal
    global height
  svgHeightCorrection = 2 # compensate for the line-height of the text
    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")
  #theScren will hold all of the 'pixels'
    fo.write( svgOut);
  theScreen = []
    fo.close()
  # fullRow is the line of #s for use at the top and bottom
    os.system("google-chrome image.svg &")
  fullRow = ""  
  for x in range(0, width+2):
      fullRow += "#"


#=========================================================
  #initiate the screen arrays, fill them with spaces
# Main loop
  for y in range(0, height):
#=========================================================
      xlist = []
loop = 0
      for x in range(0, width):
ser = serial.Serial('/dev/ttyACM0', 19200)
          xlist.append(" ")
while 1:
      theScreen.append(xlist)
    loop = loop + 1
    serialData = ser.readline()
    serialData = serialData.replace("\r\n", "")
    dataList = serialData.split(',')


    if len(dataList)==6:
  #variable to hold the svg file
        prevZbut = zbut
  svgOut = ""
        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
        if joyx>150:
            xpos = xpos + 1
        elif joyx<100:
            xpos = xpos - 1


        if joyy>150:
  #=========================================================
            ypos = ypos - 1
  # function to draw the arrays to the screen
        elif joyy<100:
  #=========================================================
            ypos = ypos + 1
  def display():
      global theScreen
      global cursorChar
      global xpos
      global ypos


        xpos = max(0, min(xpos, width-1))
      prevCursorChar = theScreen[ypos][xpos]
        ypos = max(0, min(ypos, height-1))
      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


        #remember previous char on cursor location
  #=========================================================
        prevCursorChar = theScreen[ypos][xpos]
  # function to draw a line given a start and end xy position
       
  # useInSvg determines of the line should be saved in the svg
        if cbut==1:
  # the line that is drawn while still holding down the button
            saveOpenAndQuit()
  # will not be saved (to prevent filling the whole screen when
        cursorChar = "o"
  # moving around )
  #=========================================================
  def drawLine(x1, y1, x2, y2, useInSvg):
      global theScreen
      global svgOut
      global svgScale
      global svgHeightCorrection


        if zbut==1 and prevZbut==0:#click, starting to draw
      bx1, by1, bx2, by2 = int(x1), int(y1), int(x2), int(y2) #remember the original values
            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)
      x1 = max(0, min(x1, width))
            display()
      x2 = max(0, min(x2, width))
            theScreen = []
      y1 = max(0, min(y1, height))
            for y in range(0, height):
      y2 = max(0, min(y2, height))
                xlist = []
         
                for x in range(0, width):
      #d = direction
                    xlist.append(tempScreen[y][x])
      d = math.atan2(y2-y1, x2 - x1)*180/math.pi
                theScreen.append(xlist)
      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] = "*", "*"


        elif zbut==0 and prevZbut==1:#released
      if useInSvg==1:
            theScreen[ypos][xpos] = "*"
          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)
            if sx==xpos and sy==ypos:
                drawEllipse(xpos, ypos)
            else:
                drawLine(sx, sy, xpos, ypos, 1)
            sx, sy = -1, -1
            display()


        elif zbut==0 and prevZbut==0:#doing nuthin'
            cursorChar = "o"
            display()


  #=========================================================
  # 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>"


</source>
      fo = open("image.svg", "wb")
      fo.write( svgOut);
      fo.close()
      os.system("google-chrome image.svg &")


==Arduino code==
  #=========================================================
<source lang="java">
  # Main loop
#include <Wire.h>
  # Read data from Arduino, update cursor position and such, display screen
#include "nunchuck_funcs.h"
  #=========================================================
  # 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


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()