Beispiel #1
0
 def toggle_enable(self, bits):
     # Toggle enable
     time.sleep(self.E_DELAY)
     Adafruit_I2C.writeRaw8(self, bits | self.ENABLE)
     time.sleep(self.E_PULSE)
     Adafruit_I2C.writeRaw8(self, bits & ~self.ENABLE)
     time.sleep(self.E_DELAY)
Beispiel #2
0
	def read_sensor(self):

		hival = Adafruit_I2C.write8(self.sensor_address, self.MEASURE_REG, self.MEASURE_VAL)
		sleep(.02)

		loval = Adafruit_I2C.readU8(self.sensor_address, self.DISTANCE_REG_LO)

		hival = Adafruit_I2C.readS8(self.sensor_address, self.DISTANCE_REG_HI)
		return ((hival << 8) + loval)
Beispiel #3
0
 def write_byte(self, reg, value):
     """SC16IS7X0 expects a R/W first, followd by a
     4 bit register address and combine with a value
     """
     Write_bit = 0b00000000
     # left shift by 3 bit
     reg = reg << 3
     # bitwise or with a write bit
     actual_reg = reg | Write_bit
     Adafruit_I2C.write8(self, actual_reg, value)
Beispiel #4
0
    def send_byte(self, bits, mode):
        # Send byte to data pins
        # bits = the data
        # mode = 1 for data
        #        0 for command

        bits_high = mode | (bits & 0xF0) | self.LCD_BACKLIGHT
        bits_low = mode | ((bits<<4) & 0xF0) | self.LCD_BACKLIGHT

        # High bits
        Adafruit_I2C.writeRaw8(self, bits_high)
        self.toggle_enable(bits_high)

        # Low bits
        Adafruit_I2C.writeRaw8(self, bits_low)
        self.toggle_enable(bits_low)
Beispiel #5
0
 def read_byte(self, reg):
     """SC16IS7X0 expects a R/W first, followd by a
     4 bit register address and combine with a value
     """
     Read_bit = 0b10000000
     # left shift by 3 bit
     reg = reg << 3
     # bitwise or with a write bit
     actual_reg = reg | Read_bit
     return Adafruit_I2C.readU8(self, actual_reg)
def updFuelGauge():
	global chkFuel, batteryLast, batteryLevel, Volt, Fuel, text_file, t_i2c 

	Volt = ((Adafruit_I2C.readU16(AdaI2C, 0x02, little_endian=False)) >> 4) * 1.25 /1000
	Fuel = Adafruit_I2C.readU8(AdaI2C, 0x04)  # Load Fuel Level

#	text_file = open("Output.txt", "a")
#	text_file.write("Fuel: %003d, Volts: %.4f \n" % (Fuel, Volt))
#	text_file.close()

	if Volt >= 3.68:
	  batteryLevel = 0
	elif Volt >= 3.61:
	  batteryLevel = 1
	elif Volt >= 3.54:
	  batteryLevel = 2
	elif Volt >= 3.47:
	  batteryLevel = 3
	elif Volt >= 3.40:
	  batteryLevel = 4
	elif Volt == 0.00:
	  batteryLevel = 4
	else:
	  batteryLevel = 4
	  chkFuel = False

	if (GPIO.input(17)==0):
	  batteryLevel += 5

	if batteryLevel != batteryLast:
	  if batteryLevel in [4, 9]:
	    GPIO.output(18, GPIO.LOW)
	  else:
	    GPIO.output(18, GPIO.HIGH)
#	  print "Fuel: 0x%02X - Level: 0x%02X - Last: 0x%02X " % (Fuel, batteryLevel, batteryLast)
	  batteryLast = batteryLevel
	  setBatteryLevel(batteryLevel)
	  pygame.display.update()
Beispiel #7
0
	def read_raw(self, reg, allowZero):
		i = 0
		value = ""
		sleep(.001)
		while True:
			value = Adafruit_I2C.readU8(self.sensor_address, reg)
			if value == self.ERROR_READ or (value == 0 and not allowZero):
				sleep(.02)
				if i > 50:
					print "Timeout"
					return self.ERROR_READ
				i += 1
			else:
				return value
  def __init__(self, address=0x70, bus=Adafruit_I2C.getPiI2CBusNumber(), debug=False):
    self.i2c = Adafruit_I2C(address, bus)
    self.address = address
    self.debug = debug

    # Turn the oscillator on
    self.i2c.write8(self.__HT16K33_REGISTER_SYSTEM_SETUP | 0x01, 0x00)

    # Turn blink off
    self.setBlinkRate(self.__HT16K33_BLINKRATE_OFF)

    # Set maximum brightness
    self.setBrightness(15)

    # Clear the screen
    self.clear()
Beispiel #9
0
def srf08 (pin, cycles, outData, outTime, name, location):
	print name, 'started'
	GPIO.setup(pin[1], GPIO.OUT)    	 
	i2c = bus(0x70)
	bus.write8(i2c, 2, 255)
	bus.write8(i2c, 1, 0)
	window = []
	data = []
	time_ = []
	rowTime = []	
	Location = []
	i = 0
	
	while i < cycles:
		bus.write8(i2c, 0, 84)
		time.sleep(0.07)
		ranging_result = []
		n = 4
	
		while n < 36 :
			ranging_result.append(bus.readU8(i2c, n)) 
			n += 1
	
	#********
		for index, element in enumerate(ranging_result):
			if element == 255: 
				GPIO.output(pin[1], GPIO.HIGH)
				flag = 1
				Location.append(locations[index])
				break		
			else:
				GPIO.output(pin[1], GPIO.LOW)
				flag = 0
				Location.append(locations[index])
		data.append(flag)
		rowTime.append(time.time())
		time_.append(rowTime[i] - rowTime[0])
		i += 1

	#**********
	print name, 'finished'
	outData.put(data)
	outTime.put(time_)
	location.put(Location)
Beispiel #10
0
	def read_status(self):
		value = "okay"
		status = Adafruit_I2C.readU8(self.sensor_address, self.STATUS_REG)
		if (status & self.STAT_BUSY):
			value = "busy"
		if (status & self.STAT_REF_OVER):
			value = "reference overflow"
		if (status & self.STAT_SIG_OVER):
			value = "signal overflow"
		if (status & self.STAT_PIN):
			value = "mode select pin"
		if (status & self.STAT_SECOND_PEAK):
			value = "second peak"
		if (status & self.STAT_TIME):
			value = "active between pairs"
		if (status & self.STAT_INVALID):
			value = "no signal"
		if (status & self.STAT_EYE):
			value = " eye safety"
		return value
#!/usr/bin/env python

from Adafruit_I2C import Adafruit_I2C
import time
import math

Oled = Adafruit_I2C(0x3c)
Command_Mode = 0x80
Data_mode = 0x40

grayH = 0xF0
grayL = 0x0F
Normal_Display_Cmd = 0xA4

BasicFont = [[0 for x in xrange(8)] for x in xrange(10)]
BasicFont = [[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00],
             [0x00, 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00],
             [0x00, 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00, 0x00],
             [0x00, 0x23, 0x13, 0x08, 0x64, 0x62, 0x00, 0x00],
             [0x00, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00],
             [0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x41, 0x22, 0x1C, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x08, 0x2A, 0x1C, 0x2A, 0x08, 0x00, 0x00],
             [0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x00],
             [0x00, 0xA0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00],
             [0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],
             [0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00],
Beispiel #12
0
    def __init__(self):
        # Get the ~private namespace parameters from command line or launch file
        rate = float(rospy.get_param('~rate', '10.0'))
        address = rospy.get_param('~address', '0x0e')
        no_sensors = rospy.get_param('~no_sensors', 1)

        # Create a publisher for magnet messages
        pub = rospy.Publisher("magnet_topic", AxesValues, queue_size=0)

        # Initialize and I2C nodes
        i2c = []
        msg = AxesValues()
        for i in range(0, no_sensors):
            i2c.append(Adafruit_I2C(address[i]))
            msg.id.append(i)
            msg.x_axis.append(0)
            msg.y_axis.append(0)
            msg.z_axis.append(0)

            #initialize bus
            i2c[i].write8(17, 128)
            i2c[i].write8(16, 1)
            time.sleep(0.0002)

        # Main while loop
        while not rospy.is_shutdown():
            data = [0, 0, 0, 0, 0, 0]

            for node in range(0, no_sensors):
                # read x
                i2c[node].write8(1, 0)
                time.sleep(0.0002)
                data[0] = i2c[node].readU8(1)
                time.sleep(0.0002)
                i2c[node].write8(2, 0)
                time.sleep(0.0002)
                data[1] = i2c[node].readU8(2)
                msg.x_axis[node] = data[1] + (data[0] << 8)

                # read y
                i2c[node].write8(3, 0)
                time.sleep(0.0002)
                data[2] = i2c[node].readU8(3)
                time.sleep(0.0002)
                i2c[node].write8(4, 0)
                time.sleep(0.0002)
                data[3] = i2c[node].readU8(4)
                msg.y_axis[node] = data[3] + (data[2] << 8)

                # read z
                i2c[node].write8(5, 0)
                time.sleep(0.0002)
                data[4] = i2c[node].readU8(5)
                time.sleep(0.0002)
                i2c[node].write8(6, 0)
                time.sleep(0.0002)
                data[5] = i2c[node].readU8(6)
                msg.z_axis[node] = data[5] + (data[4] << 8)

                msg.id[node] = node

            # publish data out
            pub.publish(msg)

            # Sleep for a while before publishing new messages
            if rate:
                rospy.sleep(1 / rate)
            else:
                rospy.sleep(1.0)
Beispiel #13
0
    def __init__(self, address=0x40, debug=False):
        self.i2c = Adafruit_I2C(address, debug=False)
        self.address = address
        self.debug = debug

        self.ina219SetCalibration_32V_2A()
Beispiel #14
0
 def __init__(self, addr, bus):
     self.i2c = Adafruit_I2C(0x20, bus=1)
     #starts sensor watching proximity
     self.i2c.write8(0x3, 0xFE)
    def __init__(self, busnum=-1, addr=0x20, debug=False):

        self.i2c = Adafruit_I2C(addr, busnum, debug)

        # I2C is relatively slow.  MCP output port states are cached
        # so we don't need to constantly poll-and-change bit states.
        self.porta, self.portb, self.ddrb = 0, 0, 0b00010000

        # Set MCP23017 IOCON register to Bank 0 with sequential operation.
        # If chip is already set for Bank 0, this will just write to OLATB,
        # which won't seriously bother anything on the plate right now
        # (blue backlight LED will come on, but that's done in the next
        # step anyway).
        self.i2c.bus.write_byte_data(self.i2c.address,
                                     self.MCP23017_IOCON_BANK1, 0)

        # Brute force reload ALL registers to known state.  This also
        # sets up all the input pins, pull-ups, etc. for the Pi Plate.
        self.i2c.bus.write_i2c_block_data(
            self.i2c.address,
            0,
            [
                0b00111111,  # IODIRA    R+G LEDs=outputs, buttons=inputs
                self.ddrb,  # IODIRB    LCD D7=input, Blue LED=output
                0b00111111,  # IPOLA     Invert polarity on button inputs
                0b00000000,  # IPOLB
                0b00000000,  # GPINTENA  Disable interrupt-on-change
                0b00000000,  # GPINTENB
                0b00000000,  # DEFVALA
                0b00000000,  # DEFVALB
                0b00000000,  # INTCONA
                0b00000000,  # INTCONB
                0b00000000,  # IOCON
                0b00000000,  # IOCON
                0b00111111,  # GPPUA     Enable pull-ups on buttons
                0b00000000,  # GPPUB
                0b00000000,  # INTFA
                0b00000000,  # INTFB
                0b00000000,  # INTCAPA
                0b00000000,  # INTCAPB
                self.porta,  # GPIOA
                self.portb,  # GPIOB
                self.porta,  # OLATA     0 on all outputs; side effect of
                self.portb
            ])  # OLATB     turning on R+G+B backlight LEDs.

        # Switch to Bank 1 and disable sequential operation.
        # From this point forward, the register addresses do NOT match
        # the list immediately above.  Instead, use the constants defined
        # at the start of the class.  Also, the address register will no
        # longer increment automatically after this -- multi-byte
        # operations must be broken down into single-byte calls.
        self.i2c.bus.write_byte_data(self.i2c.address,
                                     self.MCP23017_IOCON_BANK0, 0b10100000)

        self.displayshift = (self.LCD_CURSORMOVE | self.LCD_MOVERIGHT)
        self.displaymode = (self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT)
        self.displaycontrol = (self.LCD_DISPLAYON | self.LCD_CURSOROFF
                               | self.LCD_BLINKOFF)

        self.write(0x33)  # Init
        self.write(0x32)  # Init
        self.write(0x28)  # 2 line 5x8 matrix
        self.write(self.LCD_CLEARDISPLAY)
        self.write(self.LCD_CURSORSHIFT | self.displayshift)
        self.write(self.LCD_ENTRYMODESET | self.displaymode)
        self.write(self.LCD_DISPLAYCONTROL | self.displaycontrol)
        self.write(self.LCD_RETURNHOME)
Beispiel #16
0
def magnetometer():

	#I2C code for reading the magnetometer
   	counter = 0
	global Zmag_raw
	global Ymag_raw
	global Xmag_raw 
	global Zmag
	global Ymag
	global Xmag
	global mag_vector
	for counter in xrange(0,10):            #send the commands several times to ensure Magnetometer receives the command 
		Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement
		Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain
		Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement
		counter += 1
	z_l = Adafruit_I2C.readU8(i2c, Z_lsb)   #Data is stored in 2 registers on the chip, the Least significant and the
	z_m = Adafruit_I2C.readS8(i2c, Z_msb)   #most significant. Both must be read and the two combined into the actual number
	Zmag_raw = z_m << 8 | z_l
	Zmag = Zmag_raw/2048.0 * 1.3                      #Calculate Gauss value from the raw 12 bit number

	y_l = Adafruit_I2C.readU8(i2c, Y_lsb)   #rinse and repeat for each axis
	y_m = Adafruit_I2C.readS8(i2c, Y_msb)
	Ymag_raw = y_m << 8 | y_l
	Ymag = Ymag_raw/2048.0 * 1.3

	x_l = Adafruit_I2C.readU8(i2c, X_lsb)
	x_m = Adafruit_I2C.readS8(i2c, X_msb)
	Xmag_raw = x_m << 8 | x_l
	Xmag = Xmag_raw/2048.0 * 1.3

	mag_vector = np.array([Xmag,Ymag,Zmag])
	return
#!/usr/bin/python

import time
from Adafruit_I2C import Adafruit_I2C

#Create variable called i2c which refers to the address of the TPA2016 amplifier.
i2c = Adafruit_I2C(0x58)

TPA2016_SETUP			= 0x1
TPA2016_SETUP_R_EN		= 0x80
TPA2016_SETUP_L_EN		= 0x40
TPA2016_SETUP_R_FAULT	= 0x10
TPA2016_SETUP_L_FAULT	= 0x08

TPA2016_GAIN            = 0x5
TPA2016_AGC_OFF         = 0x00

# def setup():
# Set up the speakers
setup_speakers = i2c.readS8(TPA2016_SETUP)
print 'Speaker setup value =', setup_speakers

setup_rt_spkr = i2c.readS8(TPA2016_SETUP_R_EN)
# write_rt_spkr = i2c.write8(TPA2016_SETUP_R_FAULT, 1)
i2c.write8(TPA2016_SETUP_R_FAULT, 0)
print 'Right Speaker value =', setup_rt_spkr

# setup_rt_fault = i2c.readS8(TPA2016_SETUP_R_FAULT)
# # write_rt_fault = i2c.write8(TPA2016_SETUP_R_FAULT, 1)
# i2c.write8(TPA2016_SETUP_R_FAULT, 0)
# print 'Right Speaker Fault value =', setup_rt_fault
Beispiel #18
0
    pub = rospy.Publisher('mpu6050', Imu)
    rospy.init_node('MPU6050-Driver')

    #calibrate(imu, accel+gyro, samples = 0)

    global seq
    seq = 0

    while not rospy.is_shutdown():
        sample = Imu()
        sample.header = make_header()
        sample.orientation_covariance[0] = -1
        sample.angular_velocity_covariance = [0] * 9
        sample.angular_velocity = read_gyros()
        sample.linear_acceleration_covariance = [0] * 9
        sample.linear_acceleration = read_accels()

        rospy.loginfo(str(sample))
        pub.publish(sample)
        time.sleep(0.1)


if __name__ == "__main__":
    try:
        global imu
        imu = Adafruit_I2C(0x68)  # initializing the i2c device
        imu.write8(0x6b, 0)  # waking the imu
        talker()  # ROS Stuff
    except rospy.ROSInterruptException:
        pass
Beispiel #19
0
#!/usr/bin/python
from Adafruit_I2C import Adafruit_I2C
import time



tester = Adafruit_I2C(0x05, 3, True)

print "----------------------------------------"

tester.write8(1,0x00)
tester.write8(2,0x00)

#print tester.readU8(5)

print "----------------------------------------"
Beispiel #20
0
import Adafruit_BBIO.GPIO as GPIO
import time
from Adafruit_I2C import Adafruit_I2C
#http://datasheets.maximintegrated.com/en/ds/DS3231M.pdf

DS3231M_address = 0x68 
RTC_address = 0x68 
RTC_i2c = Adafruit_I2C(RTC_address)

#RTC address information

secondsAddress 			= 0x00
minutesAddress 			= 0x01
hoursAddress			= 0x02
dayOfWeekAddress		= 0x03
dayOfMonthAddress		= 0x04
monthCenturAddressy		= 0x05
yeaAddressr				= 0x06
controlAddress			= 0x0E
statusAddress			= 0x0F
agingOffsetAddress		= 0x10
temperatureMSBAddress	= 0x11
temperatureLSBAddress	= 0x12



seconds 		= 0x00
minutes 		= 0x00
hours			= 0x00
dayOfWeek		= 00000XXX
dayOfMonth		= 00NNXXXX
from Adafruit_I2C import Adafruit_I2C
import time

ADDR_ADC121 = 0x50

REG_ADDR_RESULT = 0x00
REG_ADDR_ALERT = 0x01
REG_ADDR_CONFIG = 0x02
REG_ADDR_LIMITL = 0x03
REG_ADDR_LIMITH = 0x04
REG_ADDR_HYST = 0x05
REG_ADDR_CONVL = 0x06
REG_ADDR_CONVH = 0x07

#i2c = Adafruit_I2C(ADDR_ADC121, debug=1)
i2c = Adafruit_I2C(ADDR_ADC121)


class I2cAdc:
    def __init__(self):
        i2c.write8(REG_ADDR_CONFIG, 0x20)

    def read_adc(self):
        data_list = i2c.readList(REG_ADDR_RESULT, 2)
        #print 'data list', data_list
        data = ((data_list[0] & 0x0f) << 8 | data_list[1]) & 0xfff
        return data


if __name__ == '__main__':
    adc = I2cAdc()
Beispiel #22
0
sock_out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#Code on Teensy:
#uint8_t adc_pins[] = {A0,A1,A2,A3, A6,A7,A8,A9,A10,A11,A13,A14, A18,A19,A20};
adc_linpot_i = [11, 12, 0, 1, 2]
adc_strain_i = [7, 6, 5, 4, 3]
adc_jointpot_i = [13, 14, 10, 9, 8]
index = 0

adc_linpot = [0, 0, 0, 0, 0]
adc_strain = [0, 0, 0, 0, 0]
adc_jointpot = [0, 0, 0, 0, 0]

adc_values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

i2c = Adafruit_I2C(9)  #communication with Teensy at address 9

#Motor DIR
GPIO.setup("P8_26", GPIO.OUT)
GPIO.setup("P8_14", GPIO.OUT)
GPIO.setup("P8_15", GPIO.OUT)
GPIO.setup("P8_16", GPIO.OUT)
GPIO.setup("P8_17", GPIO.OUT)
#Motor PWM
PWM.start("P9_31", 0.0)
PWM.set_frequency("P9_31", 1000.0)
PWM.start("P9_14", 0.0)
PWM.set_frequency("P9_14", 1000.0)
PWM.start("P8_19", 0.0)
PWM.set_frequency("P8_19", 1000.0)
PWM.start("P9_42", 0.0)
Beispiel #23
0
#f2 = open('rawData.csv','a');
f1.write("Datestamp,Timestamp,Latitude,Longitude,Speed,Course,Accel Z,Accel Y,Accel X,Accel Norm,Mag Z,Mag Y,Mag X,Mag Norm,Int Temp,Ext Temp,Pressure,Sound,\n")
#f2.write("Datestamp,Timestamp,Latitude,Longitude,Speed,Course,Accel Z,Accel Y,Accel X,Mag Z,May Y,Mag X,Int Temp,Ext Temp,Pressure,Sound\n")

while 1 :

#   get gps data
    gps = ser.readline()
    while(not gps.startswith('$GPRMC')):        
       gps = ser.readline()    
    gprmc.parse(gps)

#   I2C code for reading the magnetometer
    counter = 0 
    for counter in xrange(0,10):            #send the commands several times to ensure Magnetometer receives the command 
       Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement
       Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain
       Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement
       counter += 1
    z_l = Adafruit_I2C.readU8(i2c, Z_lsb)   #Data is stored in 2 registers on the chip, the Least significant and the
    z_m = Adafruit_I2C.readS8(i2c, Z_msb)   #most significant. Both must be read and the two combined into the actual number
    Zmag_raw = z_m << 8 | z_l
    Zmag = Zmag_raw/2048.0 * 1.3                      #Calculate Gauss value from the raw 12 bit number

    y_l = Adafruit_I2C.readU8(i2c, Y_lsb)   #rinse and repeat for each axis
    y_m = Adafruit_I2C.readS8(i2c, Y_msb)
    Ymag_raw = y_m << 8 | y_l
    Ymag = Ymag_raw/2048.0 * 1.3

    x_l = Adafruit_I2C.readU8(i2c, X_lsb)
    x_m = Adafruit_I2C.readS8(i2c, X_msb)
#!/usr/bin/python

import time
import datetime
from Adafruit_7Segment import SevenSegment
from Adafruit_I2C import Adafruit_I2C

# ===========================================================================
# Clock Example
# ===========================================================================
i2CBus = Adafruit_I2C.getPiI2CBusNumber()

# if using this example with a BeagleBone, uncomment the line below and specify
# which I2C Bus the backpack is connected on. If connected to I2C1 (Header P9, 
# pin 17 & 18) specify 1, if connected to I2C2 (Header P9, pin 19 & 20) specify 3.
# WARNING: if using with a BeagleBone use a logic-level convertor such as this one
# https://www.adafruit.com/products/757
# i2CBus = 1 | 3

segment = SevenSegment(address=0x70, bus=i2CBus)

print "Press CTRL+Z to exit"

# Continually update the time on a 4 char, 7-segment display
while(True):
  now = datetime.datetime.now()
  hour = now.hour
  minute = now.minute
  second = now.second
  # Set hours
  segment.writeDigit(0, int(hour / 10))     # Tens
Beispiel #25
0
class PWM:
    # Registers/etc.
    __MODE1 = 0x00
    __MODE2 = 0x01
    __SUBADR1 = 0x02
    __SUBADR2 = 0x03
    __SUBADR3 = 0x04
    __PRESCALE = 0xFE
    __LED0_ON_L = 0x06
    __LED0_ON_H = 0x07
    __LED0_OFF_L = 0x08
    __LED0_OFF_H = 0x09
    __ALL_LED_ON_L = 0xFA
    __ALL_LED_ON_H = 0xFB
    __ALL_LED_OFF_L = 0xFC
    __ALL_LED_OFF_H = 0xFD

    # Bits
    __RESTART = 0x80
    __SLEEP = 0x10
    __ALLCALL = 0x01
    __INVRT = 0x10
    __OUTDRV = 0x04

    general_call_i2c = Adafruit_I2C(0x00)

    @classmethod
    def softwareReset(cls):
        "Sends a software reset (SWRST) command to all the servo drivers on the bus"
        cls.general_call_i2c.writeRaw8(0x06)  # SWRST

    def __init__(self, address=0x40, debug=False):
        self.i2c = Adafruit_I2C(address)
        self.i2c.debug = debug
        self.address = address
        self.debug = debug
        if (self.debug):
            print "Reseting PCA9685 MODE1 (without SLEEP) and MODE2"
        self.setAllPWM(0, 0)
        self.i2c.write8(self.__MODE2, self.__OUTDRV)
        self.i2c.write8(self.__MODE1, self.__ALLCALL)
        time.sleep(0.005)  # wait for oscillator

        mode1 = self.i2c.readU8(self.__MODE1)
        mode1 = mode1 & ~self.__SLEEP  # wake up (reset sleep)
        self.i2c.write8(self.__MODE1, mode1)
        time.sleep(0.005)  # wait for oscillator

    def setPWMFreq(self, freq):
        "Sets the PWM frequency"
        prescaleval = 25000000.0  # 25MHz
        prescaleval /= 4096.0  # 12-bit
        prescaleval /= float(freq)
        prescaleval -= 1.0
        if (self.debug):
            print "Setting PWM frequency to %d Hz" % freq
            print "Estimated pre-scale: %d" % prescaleval
        prescale = math.floor(prescaleval + 0.5)
        if (self.debug):
            print "Final pre-scale: %d" % prescale

        oldmode = self.i2c.readU8(self.__MODE1)
        newmode = (oldmode & 0x7F) | 0x10  # sleep
        self.i2c.write8(self.__MODE1, newmode)  # go to sleep
        self.i2c.write8(self.__PRESCALE, int(math.floor(prescale)))
        self.i2c.write8(self.__MODE1, oldmode)
        time.sleep(0.005)
        self.i2c.write8(self.__MODE1, oldmode | 0x80)

    def setPWM(self, channel, on, off):
        "Sets a single PWM channel"
        self.i2c.write8(self.__LED0_ON_L + 4 * channel, on & 0xFF)
        self.i2c.write8(self.__LED0_ON_H + 4 * channel, on >> 8)
        self.i2c.write8(self.__LED0_OFF_L + 4 * channel, off & 0xFF)
        self.i2c.write8(self.__LED0_OFF_H + 4 * channel, off >> 8)

    def setAllPWM(self, on, off):
        "Sets a all PWM channels"
        self.i2c.write8(self.__ALL_LED_ON_L, on & 0xFF)
        self.i2c.write8(self.__ALL_LED_ON_H, on >> 8)
        self.i2c.write8(self.__ALL_LED_OFF_L, off & 0xFF)
        self.i2c.write8(self.__ALL_LED_OFF_H, off >> 8)
Beispiel #26
0
from Adafruit_I2C import Adafruit_I2C

i2c = Adafruit_I2C(0x77)

# some useful APIs are as follows:
# class Adafruit_I2C
# |  Methods defined here:
# |
# |  __init__(self, address, busnum=-1, debug=False)
# |
# |  errMsg(self)
# |
# |  readList(self, reg, length)
# |      Read a list of bytes from the I2C device
# |
# |  readS16(self, reg)
# |      Reads a signed 16-bit value from the I2C device
# |
# |  readS16Rev(self, reg)
# |      Reads a signed 16-bit value from the I2C device with rev byte order
# |
# |  readS8(self, reg)
# |      Reads a signed byte from the I2C device
# |
# |  readU16(self, reg)
# |      Reads an unsigned 16-bit value from the I2C device
# |
# |  readU16Rev(self, reg)
# |      Reads an unsigned 16-bit value from the I2C device with rev byte order
# |
# |  readU8(self, reg)
Beispiel #27
0
    def __init__(self, address):
	Adafruit_I2C.__init__(self, address)
Beispiel #28
0
CUPHOLDER_2_PWM = 13
UNDER_SEAT_PWM_R = 0
UNDER_SEAT_PWM_G = 1
UNDER_SEAT_PWM_B = 2
UPPER_SHELL_RED = 8
UPPER_SHELL_GREEN = 9
UPPER_SHELL_BLUE = 10

############################################################
# i2c
############################################################

# prox detection address
GP2Y0E02B = 0x40

proxSensor1 = Adafruit_I2C(GP2Y0E02B)
#proxSensor2 = Adafruit_I2C(VCNL4010_I2CADDR_DEFAULT)

#PWM driver connected on i2c
ledDriver = PWM(0x42)

########################################################################
# websocket
########################################################################

amcServerIP = '192.168.42.1'


def on_show_video_1(message):
    if (message['id'] == ID):
        global player
Beispiel #29
0
#!/usr/bin/python
from Adafruit_I2C import Adafruit_I2C
from TSL2591 import TSL2591

if __name__ == "__main__":
    print "Revision", Adafruit_I2C.getPiRevision()
    chip = TSL2591(debug=True)
    chip.getFullLuminosity()
Beispiel #30
0
    def __init__(self, busnum=-1, debug=False, hires=False):

        #define the pilight address
        self.pilight = Adafruit_I2C(self.PILIGHT_ADDRESS, busnum, debug)
Beispiel #31
0
B6C = 0x0101  # 0.0157 * 2^LUX_SCALE
M6C = 0x0127  # 0.0180 * 2^LUX_SCALE
K7C = 0x029a  # 1.3 * 2^RATIO_SCALE
B7C = 0x0037  # 0.00338 * 2^LUX_SCALE
M7C = 0x002b  # 0.00260 * 2^LUX_SCALE
K8C = 0x029a  # 1.3 * 2^RATIO_SCALE
B8C = 0x0000  # 0.000 * 2^LUX_SCALE
M8C = 0x0000  # 0.000 * 2^LUX_SCALE

# bus parameters
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
    bus = smbus.SMBus(1)
else:
    bus = smbus.SMBus(0)
i2c = Adafruit_I2C(TSL2561_Address)

debug = False
packageType = 0  # 0=T package, 1=CS package
gain = 0  # current gain: 0=1x, 1=16x [dynamically selected]
gain_m = 1  # current gain, as multiplier
timing = 2  # current integration time: 0=13.7ms, 1=101ms, 2=402ms [dynamically selected]
timing_ms = 0  # current integration time, in ms
channel0 = 0  # raw current value of visible+ir sensor
channel1 = 0  # raw current value of ir sensor
schannel0 = 0  # normalized current value of visible+ir sensor
schannel1 = 0  # normalized current value of ir sensor


def readRegister(address):
    try:
Beispiel #32
0
import datetime

POCKETNC_DIRECTORY = "/home/pocketnc/pocketnc"
INI_FILE = os.path.join(POCKETNC_DIRECTORY, "Settings/PocketNC.ini")

sys.path.insert(0, os.path.join(POCKETNC_DIRECTORY, "Rockhopper"))
from ini import read_ini_data, get_parameter

import Adafruit_BBIO.GPIO as GPIO

try:
    from Adafruit_GPIO.I2C import Device
    i2c = Device(0x60, 2)
except:
    from Adafruit_I2C import Adafruit_I2C
    i2c = Adafruit_I2C(0x60)

import hal
import time

iniData = read_ini_data(INI_FILE)
spindleClockPinParam = get_parameter(iniData, "POCKETNC_PINS",
                                     "SPINDLE_CLOCK_PIN")

resetTime = time.time()
pulses = 0


def countPulses(x):
    global pulses
    pulses += 1
Beispiel #33
0
 def __init__(self, address, busnum=-1, debug=False, height=4, width=20):
     Adafruit_I2C.__init__(self, address, busnum, debug)
     self.height = height
     self.width = width
#!/usr/bin/python

from sys import exit
from Adafruit_I2C import Adafruit_I2C
from time import sleep
import datetime
import math
import Adafruit_BMP.BMP085 as BMP085

CTRL_REG1_XM = 0x20
CTRL_REG2_XM = 0x21
WHO_AM_I_XM = 0x0F

LSB_VAL = 0.732

accel = Adafruit_I2C(0x1D)
sensor = BMP085.BMP085()

INIT_ALT = 72
Po = sensor.read_sealevel_pressure(
    INIT_ALT)  #must calibrate using a known itial state


#our altimeter function
def osu_aiaa_alt(s):
    alt = s.read_altitude(Po)
    alt -= INIT_ALT
    return alt


accel_in = accel.readU8(WHO_AM_I_XM)
import time
from Adafruit_I2C import Adafruit_I2C
# this device has two I2C addresses
DisplayRGB = Adafruit_I2C(0x62)
DisplayText = Adafruit_I2C(0x3e)
dot = [0b00111,\
       0b00101,\
       0b00111,\
       0b00000,\
       0b00000,\
       0b00000,\
       0b00000,\
       0b00000]
       
class LCD():
    def functionset(self):
        DisplayText.write8(0x20,0b00111100)
        
    def displaycontrol(self):
        DisplayText.write8(0x08, 0b11110000)
        
    def displayclear(self):
        DisplayText.write8(0x01, 100000000)
        
    def entrymodeset(self):
        DisplayText.write8(0x04, 111000000)
        
    def setText(self,texte):
        DisplayText.write8(0x80,0x01)
        time.sleep(.05)
        DisplayText.write8(0x80,0x08 | 0x04)
Beispiel #36
0
'''
A fan is for blowing stuff away. This one is for Replicape. 

Author: Elias Bakken
email: elias(dot)bakken(at)gmail(dot)com
Website: http://www.thing-printer.com
License: CC BY-SA: http://creativecommons.org/licenses/by-sa/2.0/
'''

from Adafruit_I2C import Adafruit_I2C
import time

PCA9685_MODE1 = 0x0
PCA9685_PRESCALE = 0xFE

pwm = Adafruit_I2C(0x70, 1, False)  # Open device

pwm.write8(PCA9685_MODE1, 0x01)  # Reset
time.sleep(0.05)  # Wait for reset


class Fan:
    @staticmethod
    def set_PWM_frequency(freq):
        """ Set the PWM frequency for all fans connected on this PWM-chip """
        prescaleval = 25000000
        prescaleval /= 4096
        prescaleval /= float(freq)
        prescaleval -= 1
        prescale = int(prescaleval + 0.5)
Beispiel #37
0
 def __init__(self, address=0x70):
     self.i2c = Adafruit_I2C(address)
     self.address = address
     self.i2c.write8(self.__LED_CONTROL, self.__FULLOFF)
Beispiel #38
0
# -*- coding: utf-8 -*-

# Get heading from a HMC5883L magnetometer connected via I2C
#
# Requires Adafruit_I2C from:
#  https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2C
#
# The Adafruit_I2C module included with the BBB doesn't have the ability to change
# the endianness of the read 16-bit values
#

from Adafruit_I2C import Adafruit_I2C
import math
from time import sleep

mag = Adafruit_I2C(0x1e)

mag.write8(0x02, 0x00)

while 1:  #                    X- Register               Z-Register                Y-Register
    (x, z, y) = (mag.readS16(0x04, False), mag.readS16(0x06, False),
                 mag.readS16(0x08, False))

    # Scaling valuesbased on defaults from datasheet
    x = x / 1100 * 100
    y = y / 1100 * 100
    z = z / 980 * 100

    # Convert X & Y angles to a heading
    headingRad = math.atan2(y, x) + (11 + 35 / 60) * (math.pi / 180)
    # Correct for reversed heading
Beispiel #39
0
magxPin = h.newpin("mag-x", hal.HAL_FLOAT, hal.HAL_OUT)
magyPin = h.newpin("mag-y", hal.HAL_FLOAT, hal.HAL_OUT)
magzPin = h.newpin("mag-z", hal.HAL_FLOAT, hal.HAL_OUT)

accelxPin = h.newpin("accel-x", hal.HAL_FLOAT, hal.HAL_OUT)
accelyPin = h.newpin("accel-y", hal.HAL_FLOAT, hal.HAL_OUT)
accelzPin = h.newpin("accel-z", hal.HAL_FLOAT, hal.HAL_OUT)

gyroxPin = h.newpin("gyro-x", hal.HAL_FLOAT, hal.HAL_OUT)
gyroyPin = h.newpin("gyro-y", hal.HAL_FLOAT, hal.HAL_OUT)
gyrozPin = h.newpin("gyro-z", hal.HAL_FLOAT, hal.HAL_OUT)

h.ready()

i2c_bus = int(args.bus_id)  #  /dev/i2c-2
mpu6050 = Adafruit_I2C(0x68, i2c_bus)  # ADC 16bit
hmc5883 = Adafruit_I2C(0x1e, i2c_bus)  # ADC 12bit
ms5611 = Adafruit_I2C(0x77, i2c_bus)  # ADC 24bit

# mpu6050 i2c master enable bit
mpu6050.write8(0x6A, 0)
# mpu6050 i2c bypass enable bit
mpu6050.write8(0x37, 2)
# mpu6050 turn off sleep mode
mpu6050.write8(0x6B, 0)

# hmc5883 continuous mode
hmc5883.write8(0x02, 0x00)

# ms5611 reset
ms5611.writeList(0x1e, [])
Beispiel #40
0
import time
from Adafruit_I2C import Adafruit_I2C as bus
i2c = bus(0x70)

bus.write16(i2c, 0x02, 0xff)

while True :

	bus.write16(i2c, 0x00, 0x51)
	time.sleep(0.1)
	print bus.readS16(i2c, 0x03), '   ' , bus.readS16(i2c, 0x05)	
Beispiel #41
0
#! /usr/bin/python2.7

from Adafruit_I2C import Adafruit_I2C

reserved = [0,1,2,44,45,46,47,56,57,58,59,60,61,62]

blocks = []

for address in range(0,128):
    if address in reserved:
        continue

    block = {}

    i2caddr = Adafruit_I2C(address)
    block['addr'] = address
    block['func'] = i2caddr.readU8(0)
    if block['func'] < 0:
        continue
    block['x'] = i2caddr.readU8(5)
    block['y'] = i2caddr.readU8(4)

    # print(block)
    blocks.append(block)

func_to_str = {
    36: 'A',
    12: ':',
    117: '3',
    19: 'p',
    0:' '
Beispiel #42
0
      acc_y = [ACC_Y_MSB, ACC_Y_LSB]
      acc_z = [ACC_Z_MSB, ACC_Z_LSB]
      pitch = [PITCH_MSB, PITCH_LSB]
      roll = [ROLL_MSB, ROLL_LSB]
      yaw = [YAW_MSB, YAW_LSB]
      mag_x = [MAG_X_MSB, MAG_X_LSB]
      mag_y = [MAG_Y_MSB, MAG_Y_LSB]
      mag_z = [MAG_Z_MSB, MAG_Z_LSB]

      #dump registers into parent lists
      self.accel = [acc_x, acc_y, acc_z]
      self.gyro = [pitch, roll, yaw]
      self.mag = [mag_x, mag_y, mag_z]

      #set accelerometer and gyroscope data rate
      I2C.write8(self.accel_gyro_addr, CTRL_REG1_G, 0xc0) #0xc0 is 952 Hz

   #Method to read accelerometer data
#   def read_accel(self)
      #
#initialize IMU

#set accelerometer and gyroscope data rate
#I2C.write8(accgy, 0x10, 0xc0) #0xc0 is 952 Hz
#I2C.write8(accgy, 0x20, 0xc0) 

#set high resolution mode for accelerometer
#I2C.write8(accgy, 0x21, 0x80)

#set continuous FIFO mode
#I2C.write8(accgy, 0x2E, 0xc0)
Beispiel #43
0
X_lsb = 0x04
Z_msb = 0x05
Z_lsb = 0x06
Y_msb = 0x07
Y_lsb = 0x08


i2c = Adafruit_I2C(mag_addr)
#i2c_read = Adafruit_I2C(mag_read_addr)
#i2c_write = Adafruit_I2C(mag_write_addr)

while 1:
    x = 0
    time.sleep(1)
    for x in xrange(0,10):
       Adafruit_I2C.write8(i2c, 0x00, 0x18) # 1 Average, 15 Hz, normal measurement
       Adafruit_I2C.write8(i2c, 0x01, 0x20) # Set Gain
       Adafruit_I2C.write8(i2c, 0x02, 0x00) # Continuous measurement
       x += 1
    z_l = Adafruit_I2C.readU8(i2c, Z_lsb)
    z_m = Adafruit_I2C.readS8(i2c, Z_msb)
    z = z_m << 8 | z_l
    z = z/2048.0 * 1.3
    
    y_l = Adafruit_I2C.readU8(i2c, Y_lsb)
    y_m = Adafruit_I2C.readS8(i2c, Y_msb)
    y = y_m << 8 | y_l
    y = y/2048.0 * 1.3

    x_l = Adafruit_I2C.readU8(i2c, X_lsb)
    x_m = Adafruit_I2C.readS8(i2c, X_msb)
Beispiel #44
0
## Follow this guide on the adafruit_i2c module if this test code
## is not running.
## https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/i2c
## Probably, you need to apt-get python-smbus
##
from Adafruit_I2C import Adafruit_I2C
import time
address = 0x6b  #I2C address. can also be 0x6a if SD0 is grounded
i2c = Adafruit_I2C(address)

print "id: %d" % i2c.readU8(0x0F)  #print device id.

i2c.write8(0x20, 0x0F)  #switch to normal mode
i2c.write8(0x23, 0x00)  #set resolution.

time.sleep(1)

n = 0
while n < 20000:
    time.sleep(.05)

    xlo = i2c.readU8(0x28)  #read the xlo bit. it is signed
    xhi = i2c.readS8(0x29)  #read the xhi bit. it is signed
    print((xlo | (xhi << 8)) * 0.00875)
    # bit shift into place and multiply

    n = n + 1
Beispiel #45
0
 def __init__(self, address, busnum=-1, debug=False, height = 4, width = 20):
     Adafruit_I2C.__init__(self, address, busnum, debug)
     self.height = height
     self.width = width
Beispiel #46
0
from Adafruit_I2C import Adafruit_I2C

i2c = Adafruit_I2C(0x6a,2)

reg = 0

print i2c.readS16(0x4819c000)

print reg


Beispiel #47
0
 def __init__(self, address=0x6b, debug=False):
     self.i2c = Adafruit_I2C(address=address, debug=debug)
     self.address = address
     self.debug = debug
     self.i2c.write8(self.__L3G4200D_CTRL_REG1,
                     self.__L3G4200D_CTRL_DEFAULT_POWER_ON)
Beispiel #48
0
	def read_sensor(self):
		hival = Adafruit_I2C.write8(self.sensor_address, self.MEASURE_REG, self.MEASURE_VAL)
		sleep(.02)
		loval = self.read_raw(self.DISTANCE_REG_LO, False)
		hival = self.read_raw(self.DISTANCE_REG_HI, True)
		return ((hival << 8) + loval)
atexit.register(camera.close)
camera.resolution = sizeData[sizeMode][1]
#camera.crop       = sizeData[sizeMode][2]
camera.crop       = (0.0, 0.0, 1.0, 1.0)
# Leave raw format at default YUV, don't touch, don't set to RGB!

# Load all icons at startup.
for file in os.listdir(iconPath):
  if fnmatch.fnmatch(file, '*.png'):
    icons.append(Icon(file.split('.')[0]))

# Initialize I2C
try:
  # print "Initialize I2C"
  AdaI2C = Adafruit_I2C(0x36)
  Adafruit_I2C.__init__(AdaI2C, 0x36)
  AdaI2C.address = 0x36                    # SparkFun "Fuel Gauge"
  AdaI2C.bus = smbus.SMBus(Adafruit_I2C.getPiI2CBusNumber())
  Adafruit_I2C.write16(AdaI2C, 0xFE, 0x0054)  # POR register
  Adafruit_I2C.write16(AdaI2C, 0x0C, 0x00FE)  # RCOMP register
  time.sleep(10)
  # Initialize GPIO
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(17, GPIO.IN)  # Monitor Charging Status LED on LiPo Charger
  GPIO.setup(18, GPIO.OUT) # Controls an Alarm LED during last 20% Battey Life
  GPIO.output(18, GPIO.HIGH)
  updFuelGauge() 
  getFuelLevel()
  # print "Default I2C bus is accessible"
except:
  # print "Error accessing default I2C bus"
Beispiel #50
0
 def __init__(self, address=0x1e, debug=False):
     self.i2c = Adafruit_I2C(address=address, debug=debug)
     self.address = address
     self.debug = debug
     self.powerOn()
Beispiel #51
0
# BeagleBone Black Health Sensors
# Autores: Mario Baldini, Joao Baggio, Raimes Moraes

POWER_CTL_addr = 0x2D
DATA_FORMAT_addr = 0x31

import numpy
import os
import time
import Adafruit_BBIO.ADC as ADC
from time import sleep
from Adafruit_I2C import Adafruit_I2C

ADC.setup()
adx1 = Adafruit_I2C(0x1d, 1)  #(device addr, i2c bus addr)
adx2 = Adafruit_I2C(0x53, 1)
adx3 = Adafruit_I2C(0x1d, 2)
adx4 = Adafruit_I2C(0x53, 2)

adx1.write8(POWER_CTL_addr, 0x08)
adx2.write8(POWER_CTL_addr, 0x08)
adx3.write8(POWER_CTL_addr, 0x08)
adx4.write8(POWER_CTL_addr, 0x08)

#D7		D6		D5		D4		D3		D2		D1
#SELF_TEST	SPI		INT_INVERT	FULL_RES	Justify		Range 1		Range 2
#0		1		1		0		0		0		1		#arduino  0x31
#0		0		0		0		0		0		0		#nosso: 2g

adx1.write8(DATA_FORMAT_addr, 0x00)
 def __init__(self, address=0x70, bus=Adafruit_I2C.getPiI2CBusNumber(), debug=False):
   if (debug):
     print "Initializing a new instance of LEDBackpack at 0x%02X" % address
   self.disp = LEDBackpack(address=address, bus=bus, debug=debug)