import spidev
import time
import sys

class MCP3208 :
	def __init__(self,spi=None):
		self.spi = spi

	def create_cmd(self,channel=None):
		if self.spi is None or channel is None :
			return []

		start_bit = 0x01
		end_bit = 0x08
		# last element must be 0 --> Why ?
		return [start_bit,(end_bit | channel)<<4,0]
	
	def process_data(self,data):
		'''
		Take in result as array of three bytes.
		Returns two lowest bits of the 2nd byte and
		all of the third byte
		'''
		if data is None :
			return None
		secd_byte = (data[1] & 0x03) # & is bitwise 'and'
		return (secd_byte<<8) | data[2] # | bitwise 'or'

	def read_adc(self,channel):
		if channel > 7 or channel < 0 :
			return None
		if self.spi is None :
			return None
		data = self.spi.xfer2(self.create_cmd(channel))
		return self.process_data(data)



		

@@ -11,30 +11,8 @@
 dev%0A
-import time%0Aimport sys
 %0A%0Acl
@@ -47,30 +47,269 @@
 self
-,spi=None):
+):%0A%09%09try:%0A%09%09%09# To open spi bus%0A%09%09%09self.spi = spidev.SpiDev()%0A%09%09%09# Raspi has only one spi bus (#0) and able to connect 2 devices (#0,#1) by default.%0A%09%09%09#self.spi.open(0,0) # connect to bus=0,device=0%0A%09%09%09self.spi.open(0,0) # connect to bus=0,device=0
 %0A%09%09
+%09#
 self.spi
  = s
@@ -304,22 +304,114 @@
 self.spi
- = spi
+.mode = 0%0A%09%09%09#self.bits_per_word = 8%0A%09%09%09#self.max_speed_hz = 50000%0A%09%09except :%0A%09%09%09self.spi = None%0A%0A
 %0A%0A%09def c
@@ -1117,8 +1117,4 @@
 )%0A%0A%0A
-%0A%09%09%0A

Update MCP3208.py