Monday, December 28, 2015

Python Bus Pirate and I2C touchscreen


This code is from project: Interface I2C touch screen with Bus Pirate and Python.

touchscreenBusPirate.py

#!/usr/bin/python

from pyBusPirateLite.I2Chigh import *

import time

class TS:
   def __init__(self):
      print "init!"
      self.i2c = I2Chigh("/dev/ttyUSB0", 115200, 1)
      self.i2c.BBmode()
      self.i2c.enter_I2C()
      self.i2c.cfg_pins(I2CPins.POWER)
      self.i2c.set_speed(I2CSpeed._400KHZ)

   def __del__(self):
      print "cleanup!"
      self.i2c.resetBP()

   def read(self):
      self.i2c.send_start_bit()
      self.i2c.bulk_trans(2, [0x82, 0x10])
      self.i2c.send_stop_bit()

      out = []
      self.i2c.send_start_bit()
      self.i2c.bulk_trans(1, [0x83])

      data_len = 9
      while(data_len):
         out.append(ord(self.i2c.read_byte()))

         if data_len > 1:
            self.i2c.send_ack()
         data_len-=1
      self.i2c.send_nack()
      self.i2c.send_stop_bit()

      d_out = { 'fingers'  :  out[0],
                'f1_x'     :  out[1] | (out[2] << 8),
                'f1_y'     :  out[3] | (out[4] << 8),
                'f2_x'     :  out[5] | (out[6] << 8),
                'f2_y'     :  out[7] | (out[8] << 8) }

      return d_out

   def get(self):
       print self.read()


ts = TS()

try:
   while(1):
      ts.get()
      time.sleep(0.1)


except KeyboardInterrupt:
   print "exit"

No comments:

Post a Comment