def readAlarmTime(month, year): #--------- # minute #-------- minute, minute2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x09), i2c.reading(rtc_address1,2))[0] timeminute1 = ((minute >> 4) & 0x07) timeminute2 = (minute & 0x0F) #------ # hour #------ hour, hour2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x0A), i2c.reading(rtc_address1,2))[0] timehour1 = ((hour >> 4) & 0x03) timehour2 = (hour & 0x0F) #------ # Day #----- day, day2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x0B), i2c.reading(rtc_address1,2))[0] timeday1 = ((day >> 4) & 0x05) timeday2 = (day & 0x0F) alminute = int(str(timeminute1)+str(timeminute2)) alhour = int(str(timehour1)+str(timehour2)) alday = int(str(timeday1)+str(timeday2)) weekday,dayname = weekDay(year,month,alday) return alminute, alhour, alday, weekday, dayname
def initSensor(self): """ initalizes the first channel if the MAX1164 device in single endded mode with vdd as ref """ with i2c.I2CMaster(1) as bus: bus.transaction(i2c.writing_bytes(self.address, 0x8a )) bus.transaction(i2c.writing_bytes(self.address, 0x01 ))
def write4(self, value, mask=0): v = self.flip[value] | self.cached_led_pin | mask self.chip.registers.master.transaction( i2c.writing_bytes(self.chip.registers.address, GPIOB, v | HD44780_EN)) self.chip.registers.master.transaction( i2c.writing_bytes(self.chip.registers.address, GPIOB, v))
def getDate(): # get date and return value #get day day, day2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x05), i2c.reading(rtc_address1,2))[0] timeday1 = ((day >> 4) & 0x03) timeday2 = (day & 0x0F) #get month month, month2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x07), i2c.reading(rtc_address1,2))[0] timemonth1 = ((month >> 4) & 0x01) timemonth2 = (month & 0x0F) #get year year, year2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x08), i2c.reading(rtc_address1,2))[0] timeyear1 = ((year >> 4)) timeyear2 = (year & 0x0F) return str(timeday1) + str(timeday2) + "/" + str(timemonth1) + str(timemonth2) + "/" + str(timeyear1) + str(timeyear2)
def readAlarmTime(month, year): #--------- # minute #-------- minute, minute2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x09), i2c.reading(rtc_address1,2))[0] timeminute1 = ((minute >> 4) & 0x07) timeminute2 = (minute & 0x0F) #------ # hour #------ hour, hour2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x0A), i2c.reading(rtc_address1,2))[0] timehour1 = ((hour >> 4) & 0x03) timehour2 = (hour & 0x0F) #------ # Day #----- day, day2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x0B), i2c.reading(rtc_address1,2))[0] timeday1 = ((day >> 4) & 0x03) timeday2 = (day & 0x0F) alminute = int(str(timeminute1)+str(timeminute2)) alhour = int(str(timehour1)+str(timehour2)) alday = int(str(timeday1)+str(timeday2)) weekday,dayname = weekDay(year,month,alday) return alminute, alhour, alday, weekday, dayname
def digitalWrite(self, pin, value): if (self.modes[pin] != self.MODE_PWM and self.modes[pin] != self.MODE_ANALOG_INPUT and self.modes[pin] != self.MODE_DIGITAL_OUTPUT and self.modes[pin] != self.MODE_DIGITAL_INPUT): self.warn( "pin " + str(pin) + " not configured for digital output, pwm output or analog input" ) self.warn("Please configure the pin accordingly") return -1 if (value != 0 and value != 1): self.warn("Digital write only accepts input value 0 or 1") return -1 if (self.modes[pin] == self.MODE_PWM): self.bus.transaction( i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_SET, pin, 255 * value)) else: self.bus.transaction( i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_SET, pin, value)) time.sleep(self.delay) return 0
def getFullLuminosity(self): self.enable() self.wait() with i2c.I2CMaster(self.i2cbus) as bus: read_results = bus.transaction( i2c.writing_bytes( address, self.COMMAND_BIT | self.WORD_BIT | self.REGISTER_CHAN1_LOW), i2c.reading(address, 2), i2c.writing_bytes( address, self.COMMAND_BIT | self.WORD_BIT | self.REGISTER_CHAN0_LOW), i2c.reading(address, 2)) self.disable() full = read_results[0][1] # print("---- full: %#08x" % full) full = full << 8 full += read_results[0][0] # print("---- full: %#08x" % full) full = full << 8 full += read_results[1][1] # print("---- full: %#08x" % full) full = full << 8 full += read_results[1][0] # print("---- full: %#08x" % full) return full
def __read_adc(self, cmd): with I2CMaster() as master: master.transaction( writing_bytes( self._address, (MS5607._CMD_ADC_CONV | cmd))) # Send conversion command # Map of times to delay for conversions delay_time = {} delay_time[MS5607._CMD_ADC_256] = 0.001 delay_time[MS5607._CMD_ADC_512] = 0.003 delay_time[MS5607._CMD_ADC_1024] = 0.004 delay_time[MS5607._CMD_ADC_2048] = 0.006 delay_time[MS5607._CMD_ADC_4096] = 0.01 time.sleep(delay_time[cmd & 0x0f]) # Wait necessary conversion time with I2CMaster() as master: read_bytes = master.transaction( writing_bytes(self._address, MS5607._CMD_ADC_READ), reading(self._address, 3)) tmp = 65536 * read_bytes[0][0] # Read MSB tmp = tmp + 256 * read_bytes[0][1] # Read byte tmp = tmp + read_bytes[0][2] # Read LSB return tmp
def getFullLuminosity(self): self.enable() self.wait() with i2c.I2CMaster(self.i2cbus) as bus: read_results = bus.transaction( i2c.writing_bytes(address, self.COMMAND_BIT | self.WORD_BIT | self.REGISTER_CHAN1_LOW ), i2c.reading(address, 2), i2c.writing_bytes(address, self.COMMAND_BIT | self.WORD_BIT | self.REGISTER_CHAN0_LOW ), i2c.reading(address, 2) ) self.disable() full = read_results[0][1] # print("---- full: %#08x" % full) full = full << 8 full += read_results[0][0] # print("---- full: %#08x" % full) full = full << 8 full += read_results[1][1] # print("---- full: %#08x" % full) full = full << 8 full += read_results[1][0] # print("---- full: %#08x" % full) return full
def write_data(port_expand_addr, port, value): with q2w_i2c.I2CMaster() as bus: if port == 'A': bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, GPIOA, value)) elif port == 'B': bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, GPIOB, value))
def set_IO_PULL_UP(port_expand_addr, port, value): with q2w_i2c.I2CMaster() as bus: if port == 'A': bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, GPPUA, value)) elif port == 'B': bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, GPPUB, value))
def set_IO_DIR(port_expand_addr, port, value): with q2w_i2c.I2CMaster() as bus: if port == 'A': bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, IODIRA, value)) elif port == 'B': bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, IODIRB, value))
def readSensor() : bus.transaction(i2c.writing_bytes(ADDR, 0x22, 0x20)) time.sleep(0.05) readout = bus.transaction(i2c.writing_bytes(ADDR, 0xE0, 0x00), i2c.reading(ADDR, 6)) time.sleep(1.0) return readout
def readSensor(): bus.transaction(i2c.writing_bytes(ADDR, 0x22, 0x20)) time.sleep(0.05) readout = bus.transaction(i2c.writing_bytes(ADDR, 0xE0, 0x00), i2c.reading(ADDR, 6)) time.sleep(1.0) return readout
def read_data(port_expand_addr, port): with q2w_i2c.I2CMaster() as bus: if port == 'A': return bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, GPIOA), q2w_i2c.reading(port_expand_addr, 1))[0][0] elif port == 'B': return bus.transaction( q2w_i2c.writing_bytes(port_expand_addr, GPIOB), q2w_i2c.reading(port_expand_addr, 1))[0][0]
def calibrateSensor(self): sleep(self.TD_DEFAULT) print("Entering Calibration Mode") with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.ENTER_CAL)) sleep(self.TD_ENTER_CAL) input("Press Enter to exit calibration mode") bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.EXIT_CAL)) sleep(self.TD_EXIT_CAL) print("Exited Calibration Mode")
def write(self, register, value): log.debug( "Writing to address {0:#4X} register 0x{1:#4X} value {2:#10b}". format(self.ADDRESS, register, value)) if self.TOGGLE_MODE: a = value & 0b11111111 b = (value >> 8) & 0b11111111 BUS.transaction(i2c.writing_bytes(self.ADDRESS, register, a, b), ) else: BUS.transaction(i2c.writing_bytes(self.ADDRESS, register, value), )
def _injectConfig(self, num, location, length, setting): mask = ((1 << length) - 1) << location setting = (setting << location) & mask res = self.bus.transaction( i2c.writing_bytes(self.address, CONFIG_REG), i2c.reading(self.address, 2)) data = list(*res) data[num] &= ~mask data[num] |= setting self.bus.transaction( i2c.writing_bytes(self.address, CONFIG_REG, data[0], data[1]))
def bank_mode(self, bank): self.BANK = bank log.info("Bank set to {0:d}".format(bank)) #EVERYTHING else goes to zero - some magic to write bit on both settings if self.BANK == 1: #assume has been bank=0 before BUS.transaction( i2c.writing_bytes(self.ADDRESS, 0x15, IOCON['BANK']), i2c.writing_bytes(self.ADDRESS, 0x0A, IOCON['BANK'])) elif self.BANK == 0: BUS.transaction(i2c.writing_bytes(self.ADDRESS, 0x15, 0), i2c.writing_bytes(self.ADDRESS, 0x0A, 0))
def write_cgchar(self, pos, data): cgaddr = pos & 0x07 cgaddr = (cgaddr << 3) | 0x40 time.sleep(0.1) self.bus.transaction(i2c.writing_bytes(self.address, 0x00, 0x38)) # Function Set, 8bit, 2lines, NoDoubleHeight, InstructionTable=0 time.sleep(0.1) self.bus.transaction(i2c.writing_bytes(self.address, 0x00, cgaddr)) # select CGRAM address time.sleep(0.1) for datum in data: self.bus.transaction(i2c.writing_bytes(self.address, 0x40, datum)) # Write data, byte by byte (normally 8) self.bus.transaction(i2c.writing_bytes(self.address, 0x00, 0x80)) # return to DDRAM
def write(self, register, value): log.debug("Writing to address {0:#4X} register 0x{1:#4X} value {2:#10b}".format(self.ADDRESS, register, value)) if self.TOGGLE_MODE: a = value & 0b11111111 b = (value >> 8) & 0b11111111 BUS.transaction( i2c.writing_bytes(self.ADDRESS, register, a, b), ) else: BUS.transaction( i2c.writing_bytes(self.ADDRESS, register ,value), )
def readFilterValue(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.READ_EEPROM, self.FILTER_LSB)) sleep(self.TD_READ_EEPROM) filterL = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_EEPROM_REG)) sleep(self.TD_DEFAULT) bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.READ_EEPROM, self.FILTER_MSB)) sleep(self.TD_READ_EEPROM) filterM = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_EEPROM_REG)) filterValue = (256*filterM[0][0] + filterL[0][0]) print("Filter Value = %d" %filterValue)
def bank_mode(self, bank): self.BANK = bank log.info("Bank set to {0:d}".format(bank)) #EVERYTHING else goes to zero - some magic to write bit on both settings if self.BANK == 1: #assume has been bank=0 before BUS.transaction( i2c.writing_bytes(self.ADDRESS,0x15, IOCON['BANK']), i2c.writing_bytes(self.ADDRESS,0x0A, IOCON['BANK'])) elif self.BANK == 0: BUS.transaction( i2c.writing_bytes(self.ADDRESS,0x15, 0 ), i2c.writing_bytes(self.ADDRESS,0x0A, 0 ))
def callback(self, channel): log.debug( "Interrupt detected on address 0x{0:x} with prefix 0x{1:x}; channel {2}" .format(self.parent.ADDRESS, self.PREFIX, channel)) self.lock.acquire() log.debug("Lock aquired!") log.debug("Before State is 0b{0:b}".format(self.state)) erg = BUS.transaction( #READ INTF TO FIND OUT INITIATING PIN i2c.writing_bytes( self.parent.ADDRESS, self._resolve_register(self.parent.REGISTER['INTF'])), i2c.reading(self.parent.ADDRESS, 1), #READ INTCAP TO GET CURRENTLY ACTIVATED PINS | RESETS THE INTERRUPT i2c.writing_bytes( self.parent.ADDRESS, self._resolve_register(self.parent.REGISTER['INTCAP'])), i2c.reading(self.parent.ADDRESS, 1), #READ GPIO TO GET CURRENTLY ACTIVATED PINS | RESETS THE INTERRUPT i2c.writing_bytes( self.parent.ADDRESS, self._resolve_register(self.parent.REGISTER['GPIO'])), i2c.reading(self.parent.ADDRESS, 1), ) intf = erg[0][0] log.debug("INTF was 0b{0:b}".format(intf)) intcap = erg[1][0] log.debug("INTCAP was 0b{0:b}".format(intcap)) gpio = erg[2][0] log.debug("GPIO was 0b{0:b}".format(gpio)) current = intf | gpio #calculate only changes changes = (self.state ^ 0b11111111) & current #set new state self.state = gpio log.debug("After State is 0b{0:b}".format(self.state)) self.lock.release() log.debug("Lock released!") #call callback after lock release log.debug("Sending changes 0b{0:b} to callback method".format(changes)) self.external_callback(changes, self.PREFIX, self.parent.ADDRESS) if self.accuracy_callback: self.accuracy += 1 if (self.state == 0): self.accuracy_callback(self.accuracy) self.accuracy = 0
def __readADC(self): command = 0x00 #Read D1 self.bus.transaction(i2c.writing_bytes(self.address, 0x48)) time.sleep(0.01) values = self.bus.transaction(i2c.writing_bytes(self.address, command), i2c.reading(self.address, 3)) self.D[1] = (values[0][0] << 16) | (values[0][1] << 8) | (values[0][2]) self.logger.debug("CMD(" + "{0:#04x}".format(command) + ") -> D1 = " + str(self.D[1])) # Read D2 self.bus.transaction(i2c.writing_bytes(self.address, 0x58)) time.sleep(0.01) values = self.bus.transaction(i2c.writing_bytes(self.address, command), i2c.reading(self.address, 3)) self.D[2] = (values[0][0] << 16) | (values[0][1] << 8) | (values[0][2]) self.logger.debug("CMD(" + "{0:#04x}".format(command) + ") -> D2 = " + str(self.D[2]))
def writeEEPROM(self, reg, value): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.WRITE_EEPROM, reg, value)) sleep(self.TD_WRITE_EEPROM) bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.READ_EEPROM, reg)) sleep(self.TD_READ_EEPROM) readValue = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_EEPROM_REG)) if(readValue[0][0] == value): print("Reg 0x%02x written with 0x%02x successfully" %(reg, readValue[0][0])) else: print("Write not successful")
def resetAlarm(): #----------------------- # Clear status register #----------------------- bus.transaction(i2c.writing_bytes(rtc_address1, 0x01, 0x00)) #----------------------- # Clear alarm registers #----------------------- bus.transaction(i2c.writing_bytes(rtc_address1, 0x09, 0x00)) bus.transaction(i2c.writing_bytes(rtc_address1, 0x0A, 0x00)) bus.transaction(i2c.writing_bytes(rtc_address1, 0x0B, 0x00)) bus.transaction(i2c.writing_bytes(rtc_address1, 0x0C, 0x00))
def triggerAfterSleep(self, pin=8, holdtime_1st=8, holdtime_2nd=2, wait_1st=10, wait_2nd=100, inverse=True): if (holdtime_1st > 255): holdtime_1st = 255 if (holdtime_2nd > 255): holdtime_2nd = 255 if (wait_1st > 65535): wait_1st = 65535 if (wait_2nd > 65535): wait_2nd = 65535 inverse_bit = 0 if (inverse): inverse_bit = 1 self.bus.transaction( i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_TRIGGER_AFTER_SLEEP, pin, holdtime_1st, holdtime_2nd, inverse_bit, int(wait_1st), int(wait_2nd / 256), int(wait_2nd % 256))) time.sleep(self.delay) return 0
def __set_channel(self, channel, resolution, gain): try: self.bus.transaction(i2c.writing_bytes(self.address, MCP342x.C_READY | MCP342x.CHANNEL[channel] | MCP342x.C_OC_MODE | MCP342x.RESOLUTION[resolution] | MCP342x.GAIN[gain])) except KeyError: print("Error : this channel (%s) does not exist" % channel)
def select_mifare(self): with I2CMaster() as master: # select mifare card # <len> <cmd> master.transaction(writing_bytes(ADDRESS, 1, CMD_SELECT_MIFARE)) time.sleep(WR_RD_DELAY) # read the response responses = master.transaction(reading(ADDRESS, 15)) response = responses[0] # <len> <cmd> <status> <UUID> <type> len = response[0] cmd = response[1] status = response[2] if (status != 0x00): self.uid = None self.type = None return False # uid length varies on type, and type is after uuid uid = response[3:len] type = response[len] self.type = type self.uid = uid return True
def selectMode(self, mode): sleep(self.TD_DEFAULT) successFlag = 1 if(mode == self.ENTER_SLEEP): enterSleep() return else: with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, mode)) sleep(self.TD_ENTER_MODE) OPMode1 = self.readOPMode1() if(mode == self.ENTER_RUN): if(OPMode1 & 0x10 == 0): successFlag = 0 elif(mode == self.ENTER_STANDBY): if(OPMode1 & 0x08 == 0): successFlag = 0 else: print("Mode value not valid") if(successFlag == 0): print("Failed to set mode") else: print("Mode set successfully")
def getadcreading(address, adcConfig, res): bus.transaction(i2c.writing_bytes(address, adcConfig)) if (res == 0): h, m, l, s = bus.transaction(i2c.reading(address, 4))[0] while (s & 128): h, m, l, s = bus.transaction(i2c.reading(address, 4))[0] # shift bits to product result t = ((h & 0b00000001) << 16) | (m << 8) | l # check if positive or negative number and invert if needed if (h > 128): t = ~(0x020000 - t) return (t / 64000) else: m, l, s = bus.transaction(i2c.reading(address, 3))[0] while (s & 128): m, l, s = bus.transaction(i2c.reading(address, 3))[0] # shift bits to product result t = (m << 8) | l # check if positive or negative number and invert if needed if (m > 128): t = ~(0x02000 - t) if (res == 4): return (t / 16000) if (res == 8): return (t / 4000) if (res == 12): return (t / 1000)
def readAccel(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_ACCEL)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_POST_DATA)) accelX = (256*readValues[0][0] + readValues[0][1]) if(accelX & 0x01<<15 != 0x00): accelX = (-(self.MAX_16_BIT+1) + accelX) accelX = accelX/1024.0 accelY = (256*readValues[0][2] + readValues[0][3]) if(accelY & 0x01<<15 != 0x00): accelY = (-(self.MAX_16_BIT+1) + accelY) accelY = accelY/1024.0 accelZ = (256*readValues[0][4] + readValues[0][5]) if(accelZ & 0x01<<15 != 0x00): accelZ = (-(self.MAX_16_BIT+1) + accelZ) accelZ = accelZ/1024.0 print("AccelX = %f" %accelX) print("AccelY = %f" %accelY) print("AccelZ = %f" %accelZ)
def readMag(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_MAG)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_POST_DATA)) magX = (256*readValues[0][0] + readValues[0][1]) if(magX & 0x01<<15 != 0x00): magX = (-(self.MAX_16_BIT+1) + magX) magX = magX/10.0 magY = (256*readValues[0][2] + readValues[0][3]) if(magY & 0x01<<15 != 0x00): magY = (-(self.MAX_16_BIT+1) + magY) magY = magY/10.0 magZ = (256*readValues[0][4] + readValues[0][5]) if(magZ & 0x01<<15 != 0x00): magZ = (-(self.MAX_16_BIT+1) + magZ) magZ = magZ/10.0 print("MagX = %f" %magX) print("MagY = %f" %magY) print("MagZ = %f" %magZ)
def readTilt(self): sleep(self.TD_DEFAULT) with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.POST_TILT)) sleep(self.TD_POST_DATA) readValues = bus.transaction(i2c.reading(self.I2C_ADDR, self.BLEN_POST_DATA)) pitch = (256*readValues[0][0] + readValues[0][1]) if(pitch & 0x01<<15 != 0x00): pitch = (-(self.MAX_16_BIT+1) + pitch) pitch = pitch/10.0 roll = (256*readValues[0][2] + readValues[0][3]) if(roll & 0x01<<15 != 0x00): roll = (-(self.MAX_16_BIT+1) + roll) roll = roll/10.0 temp = (256*readValues[0][4] + readValues[0][5]) if(temp & 0x01<<15 != 0x00): temp = (-(self.MAX_16_BIT+1) + temp) temp = temp/10.0 print("Pitch = %f" %pitch) print("Roll = %f" %roll) print("Temperature = %f" %temp)
def _extractConfig(self, num, location=0, length=8): res = self.bus.transaction( i2c.writing_bytes(self.address, CONFIG_REG), i2c.reading(self.address, 2)) data = res[0] mask = (1 << length) - 1 return (data[num] >> location) & mask
def resetProc(self): sleep(self.TD_DEFAULT) print("Resetting the HMC6343 processor") with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, self.RESET)) sleep(self.TD_RESET) print("HMC 6343 Processor Reset")
def setAlarm(minuite, hour, day, weekday): # set alarm data to clock enableAlarm() bus.transaction( i2c.writing_bytes(rtc_address1, 0x09, decToBcd(minuite), decToBcd(hour), decToBcd(day), decToBcd(weekday)) )
def get_resolutions(self): user_reg = self.bus.transaction( i2c.writing_bytes(self.ADDR, self.CMD_READ_USER_REG), i2c.reading(self.ADDR, 1), ) user_reg_int = int.from_bytes(user_reg[0], byteorder="big") return self.RESOLUTIONS[user_reg_int >> 6, user_reg_int & 0x1]
def read_card(self): logging.info("Fetching card id from %0X" % self.i2c_address) # Fetch the card ID by sending 1/1 to the SL030 card reader with i2c.I2CMaster() as bus: bus.transaction( i2c.writing_bytes(0x50, 0x1, 0x1)) time.sleep(0.1) # SL030 requires this time to respond reliably read_results = bus.transaction( i2c.reading(0x50, 10)) returned_len = read_results[0][0] status = read_results[0][2] if returned_len == 0: logging.info("Error fetching from card reader") return(None) if status == 0x1: # No Tag logging.info("No tag detected") return(None) else: # Format the read card ID as a hex string card_as_hex = [] for x in range(3, returned_len): card_as_hex.append('{1:02x}'.format(x, read_results[0][x]).upper()) logging.debug("Card presented: " % card_as_hex) return(''.join(card_as_hex))
def setTiming(self, timing): self.timing = timing with i2c.I2CMaster(self.i2cbus) as bus: bus.transaction( i2c.writing_bytes(self.address, self.COMMAND_BIT | self.REGISTER_TIMING, self.gain | self.timing))
def setup_pwm_freq(self, pin, freq): # timer 0 #[2]: 31250, 3906, 488, 122, 30, // 1,8,64,256,1024 // untested pwm channel p2 #[3]: 31250, 3906, 488, 122, 30, // 1,8,64,256,1024 // untested pwm channel p3 # timer 1 #[1]: 15625, 1953, 244, 61, 15 // 1,2,3,4,5 // untested pwm channel p1 #[0]: 15625, 1953, 244, 61, 15 // 1,2,3,4,5 // untested pwm channel p0 # timer 2 #[7]: 15625, 1953, 488, 244, 122, 61, 15 // 1,8,32,64,128,256,1024 // 1,2,3,4,5,6,7 // motor left tested #[9]: 15625, 1953, 488, 244, 122, 61, 15 // 1,8,32,64,128,256,1024 // 1,2,3,4,5,6,7 // motor right tested a_freq = [] a_freq.append([15625,1953,244,61,15]) a_freq.append([15625,1953,244,61,15]) a_freq.append([31250,3906,488,122,30]) a_freq.append([31250,3906,488,122,30]) a_freq.append([]) a_freq.append([]) a_freq.append([]) a_freq.append([15625,1953,488,244,122,61,15]) a_freq.append([]) a_freq.append([15625,1953,488,244,122,61,15]) if(pin > len(a_freq) or pin <0): self.warn("pin not available for pwm manipulation") return -1 elif(freq in a_freq[pin]): divisor = (a_freq[pin].index(freq))+1 else: self.warn("Frequency not available. For this pin the following freq are possible: "+str(a_freq[pin])) return -1 self.bus.transaction(i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_PWM_FREQ, pin, divisor))
def reset_config(self): # reset avr and give some time for reboot self.bus.transaction(i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_RESET)) time.sleep(1) self.modes = [None] * 15 self.ws2812count = [0] * 15 return 0
def values(self): i=0 errcode=0 hum = 999 temp = 999 while i <= MAXTRYS: with i2c.I2CMaster() as bus: try: bus.transaction(i2c.writing_bytes(AM2315_I2CADDR, FUNCTION_CODE_READ,*readBytes )) time.sleep(AM2315_WAITTIME) read_results = bus.transaction(i2c.reading(AM2315_I2CADDR, 8)) # print(read_results) break except: i = i+1 if i > MAXTRYS: errcode=1 else: s=bytearray(read_results[0]) crc = 256*s[7]+s[6] t = bytearray([s[0],s[1],s[2],s[3],s[4],s[5]]) c = self.crc16(t) if crc != c: errcode=2 else: hum = (256*s[2]+s[3])/10 temp = (256*s[4]+s[5])/10 return hum,temp,errcode
def setOrientation(self, orientation): sleep(self.TD_DEFAULT) successFlag = 1 with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(self.I2C_ADDR, orientation)) sleep(self.TD_SET_ORIENTATION) OPMode1 = self.readOPMode1() if(orientation == self.ORIENT_LEVEL): if(OPMode1 & 0x01 == 0): successFlag = 0 elif(orientation == self.ORIENT_SIDEWAYS): if(OPMode1 & 0x02 == 0): successFlag = 0 elif(orientation == self.ORIENT_FLATFRONT): if(OPMode1 & 0x04 == 0): successFlag = 0 else: print("Orientation value not valid") if(successFlag == 0): print("Failed to set orientation") else: print("Orientation set successfully")
def getadcreading(address, channel, gain, res): channel = channel - 1 adcConfig = MCP342X_START | MCP342X_CHANNEL_1 | MCP342X_CONTINUOUS adcConfig |= channel << 5 | res << 2 | gain #print("adcConfig") #print(adcConfig) varDivisior = 1 << (gain + 2*res) bus.transaction(i2c.writing_bytes(address, adcConfig)) time.sleep(0.05) if (res ==3): h, m, l ,s = bus.transaction(i2c.reading(address,4))[0] time.sleep(0.05) h, m, l, s = bus.transaction(i2c.reading(address,4))[0] t = ((h & 0b00000001) << 16) | (m << 8) | l else: h, m, l = bus.transaction(i2c.reading(address,3))[0] time.sleep(0.05) h, m, l = bus.transaction(i2c.reading(address,3))[0] t = (h << 8) | m if (h > 128): t = ~(0x020000 - t) # remove / 1000 to return value in milivolts return ((t/varDivisior) * 2.4705882) / 1000
def getadcreading(address, adcConfig, res): bus.transaction(i2c.writing_bytes(address, adcConfig)) if res == 0: h, m, l, s = bus.transaction(i2c.reading(address, 4))[0] while s & 128: h, m, l, s = bus.transaction(i2c.reading(address, 4))[0] # shift bits to product result t = ((h & 0b00000001) << 16) | (m << 8) | l # check if positive or negative number and invert if needed if h > 128: t = ~(0x020000 - t) return t / 64000 else: m, l, s = bus.transaction(i2c.reading(address, 3))[0] while s & 128: m, l, s = bus.transaction(i2c.reading(address, 3))[0] # shift bits to product result t = (m << 8) | l # check if positive or negative number and invert if needed if m > 128: t = ~(0x02000 - t) if res == 4: return t / 16000 if res == 8: return t / 4000 if res == 12: return t / 1000
def digitalWrite(self,pin,value): if(self.modes[pin]!=self.MODE_PWM and self.modes[pin]!=self.MODE_ANALOG_INPUT and self.modes[pin]!=self.MODE_DIGITAL_OUTPUT and self.modes[pin]!=self.MODE_DIGITAL_INPUT): self.warn("pin "+str(pin)+" not configured for digital output, pwm output or analog input") self.warn("Please configure the pin accordingly") return -1 if(value!=0 and value!=1): self.warn("Digital write only accepts input value 0 or 1") return -1 if(self.modes[pin]==self.MODE_PWM): self.bus.transaction(i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_SET, pin, 255*value)) else: self.bus.transaction(i2c.writing_bytes(self.address, self.START_BYTE, self.CMD_SET, pin, value)) time.sleep(self.delay) return 0
def read_card(self): logging.info("Fetching card id from %0X" % self.i2c_address) # Fetch the card ID by sending 1/1 to the SL030 card reader with i2c.I2CMaster() as bus: bus.transaction(i2c.writing_bytes(0x50, 0x1, 0x1)) time.sleep(0.1) # SL030 requires this time to respond reliably read_results = bus.transaction(i2c.reading(0x50, 10)) returned_len = read_results[0][0] status = read_results[0][2] if returned_len == 0: logging.info("Error fetching from card reader") return (None) if status == 0x1: # No Tag logging.info("No tag detected") return (None) else: # Format the read card ID as a hex string card_as_hex = [] for x in range(3, returned_len): card_as_hex.append('{1:02x}'.format( x, read_results[0][x]).upper()) logging.debug("Card presented: " % card_as_hex) return (''.join(card_as_hex))
def __readPROM(self, address): #PROM read code command = 0xA0 + (address << 1) values = self.bus.transaction(i2c.writing_bytes(self.address, command), i2c.reading(self.address, 2)) C = (values[0][0] << 8) | values[0][1] self.logger.debug("CMD(" + "{0:#04x}".format(command) + ") -> " + "C = " + str(C)) return C
def initClock(): # reset all clock values to factory default bus.transaction( i2c.writing_bytes( rtc_address1, 0x0, 0x0, 0x0, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x80, 0x80, 0x80, 0x0, 0x0 ) )
def _write_control_flags(self): if self._last_channel_read is None: self._last_channel_read = 0 self.master.transaction( writing_bytes(self.address, self._control_flags | self._last_channel_read))
def callback(self, channel): log.debug("Interrupt detected on address 0x{0:x} with prefix 0x{1:x}; channel {2}".format(self.parent.ADDRESS, self.PREFIX, channel)) self.lock.acquire() log.debug("Lock aquired!") log.debug("Before State is 0b{0:b}".format(self.state)) erg = BUS.transaction( #READ INTF TO FIND OUT INITIATING PIN i2c.writing_bytes(self.parent.ADDRESS,self._resolve_register(self.parent.REGISTER['INTF'])), i2c.reading(self.parent.ADDRESS,1), #READ INTCAP TO GET CURRENTLY ACTIVATED PINS | RESETS THE INTERRUPT i2c.writing_bytes(self.parent.ADDRESS,self._resolve_register(self.parent.REGISTER['INTCAP'])), i2c.reading(self.parent.ADDRESS,1), #READ GPIO TO GET CURRENTLY ACTIVATED PINS | RESETS THE INTERRUPT i2c.writing_bytes(self.parent.ADDRESS,self._resolve_register(self.parent.REGISTER['GPIO'])), i2c.reading(self.parent.ADDRESS,1), ) intf = erg[0][0] log.debug("INTF was 0b{0:b}".format(intf)) intcap = erg[1][0] log.debug("INTCAP was 0b{0:b}".format(intcap)) gpio = erg[2][0] log.debug("GPIO was 0b{0:b}".format(gpio)) current = intf | gpio #calculate only changes changes = (self.state ^ 0b11111111) & current #set new state self.state = gpio log.debug("After State is 0b{0:b}".format(self.state)) self.lock.release() log.debug("Lock released!") #call callback after lock release log.debug("Sending changes 0b{0:b} to callback method".format(changes)) self.external_callback(changes, self.PREFIX, self.parent.ADDRESS) if self.accuracy_callback: self.accuracy += 1 if (self.state == 0): self.accuracy_callback(self.accuracy) self.accuracy = 0
def send_i2c(): with I2CMaster() as master: while(True): c = input(':') if c.startswith('q'): break master.transaction( writing_bytes(address, ord(c[0])))
def write_xy(self, col, line, character): if line == 1: command = (col & 0x3F) | 0xC0 else: command = (col & 0x3F) | 0x80 self.bus.transaction(i2c.writing_bytes(self.address, 0x00, command)) self.write_char(character) return