def setupCompass(self): self.comp=wiringPiI2CSetup(HMC5883) if(self.comp == -1): print "Error al conectar con HMC5883" return -1 #Turning on the ADXL345 wiringPiI2CWriteReg8(self.comp, 0x02, 0x00) return 0
def setupAcc(self): self.acc=wiringPiI2CSetup(ADXL345) if(self.acc == -1): print "Error al conectar con ADXL345" return -1 #Turning on the ADXL345 wiringPiI2CWriteReg8(self.acc, 0x31, 0x01) wiringPiI2CWriteReg8(self.acc, 0x2d, 0x08) return 0
def start_dev(address, adres_rejestru1, adres_rejestru2, bajt_danych1, bajt_danych2): fd = wiringpi.wiringPiI2CSetup(address) time.sleep(0.01) wiringpi.wiringPiI2CWriteReg8(fd, adres_rejestru1, bajt_danych1) wiringpi.wiringPiI2CWriteReg8(fd, adres_rejestru2, bajt_danych2) return fd
def get_gyroscope_xyz(): fd = wiringpi.wiringPiI2CSetup(0x6B) wiringpi.wiringPiI2CWriteReg8(fd, 0x20, 15) x = twos_complement_combine(wiringpi.wiringPiI2CReadReg8(fd, 0x29), wiringpi.wiringPiI2CReadReg8( fd, 0x29)) / 32768 # left, right y = twos_complement_combine(wiringpi.wiringPiI2CReadReg8( fd, 0x2B), wiringpi.wiringPiI2CReadReg8(fd, 0x2A)) / 32768 # up, down z = twos_complement_combine(wiringpi.wiringPiI2CReadReg8( fd, 0x2D), wiringpi.wiringPiI2CReadReg8(fd, 0x2C)) / 32768 # rotation return jsonify({"gyroscope": [str(x), str(y), str(z)]})
def customSymbol(self, location, charmap): location &= 0x7 # we only have 8 locations 0-7 self.command(LCD_SETCGRAMADDR | (location << 3)) #data = bytearray(9); data = 0x40 #for i in range(0,8): # data[i+1] = charmap[i] #self.send(data,9,charmap) for i in range(0, 8): wiringpi.wiringPiI2CWriteReg8(self.LCD, 0x40, charmap[i])
def __init__(self,devid=0x76): '''Initialze the I2C port with wiringpi and start the BMP280 sensor. Arguments: devid = the device ID of the sensor, defaults to 0x76''' wp.wiringPiSetupGpio() # Use the GPIO numbering scheme. self.fh = wp.wiringPiI2CSetup(devid) # Open com to devide dat = wp.wiringPiI2CReadReg8(self.fh,0xD0) # Check device ID if dat != 0x58: print('''The device at address 0x{:02x} returned 0x{:02x} instead of 0x58.\n Probably this is the wrong device.'''.format(devid,dat)) return(0) wp.wiringPiI2CWriteReg8(self.fh,0xF4,0b00100111) # Set for normal operation, 1x oversampling.
def init(self): global a a=wpi.wiringPiI2CSetup(0x1e) #set range wpi.wiringPiI2CWriteReg8(a,0x01, 0b001 << 5); #setMeasurementMode value = wpi.wiringPiI2CReadReg8(a,0x02); value &= 0b11111100; value |= 0b00; wpi.wiringPiI2CWriteReg8(a,0x02, value); #setDataRate value = wpi.wiringPiI2CReadReg8(a,0x00); value &= 0b11100011; value |= (0b101 << 2); wpi.wiringPiI2CWriteReg8(a,0x00, value); #setSamples value = wpi.wiringPiI2CReadReg8(a,0x00); value &= 0b10011111; value |= (0b11 << 5); wpi.wiringPiI2CWriteReg8(a,0x00, value);
def __init__(self): global a a = wpi.wiringPiI2CSetup(0x1e) #set range wpi.wiringPiI2CWriteReg8(a, 0x01, 0b001 << 5) #setMeasurementMode value = wpi.wiringPiI2CReadReg8(a, 0x02) value &= 0b11111100 value |= 0b00 wpi.wiringPiI2CWriteReg8(a, 0x02, value) #setDataRate value = wpi.wiringPiI2CReadReg8(a, 0x00) value &= 0b11100011 value |= (0b101 << 2) wpi.wiringPiI2CWriteReg8(a, 0x00, value) #setSamples value = wpi.wiringPiI2CReadReg8(a, 0x00) value &= 0b10011111 value |= (0b11 << 5) wpi.wiringPiI2CWriteReg8(a, 0x00, value)
def start_pwm_driver(pwm_addr): global PWM_ON_ADDR_LOW global PWM_ON_ADDR_HIGH pwm_id = wiringpi.wiringPiI2CSetup(pwm_addr) time.sleep(0.001) # OSCILLATOR = ON wiringpi.wiringPiI2CWriteReg8(pwm_id, mode_1_reg_address, mode_1_reg_value) # FREQ = 200Hz wiringpi.wiringPiI2CWriteReg8(pwm_id, 0xFE, 0x1E) # SET MOTORS ENABLE DELAY TIME = 100 for a in range(0, 6): PWM_MSB, PWM_LSB = divmod(100, 1 << 8) byte_low = PWM_ON_ADDR_LOW[a] byte_high = PWM_ON_ADDR_HIGH[a] wiringpi.wiringPiI2CWriteReg8(pwm_id, byte_low, PWM_LSB) wiringpi.wiringPiI2CWriteReg8(pwm_id, byte_high, PWM_MSB) return pwm_id
def set_times(pwm_id, times): global PWM_OFF_ADDR_LOW global PWM_OFF_ADDR_HIGH for a in range(0, 6): PWM_MSB, PWM_LSB = divmod(times[a], 1 << 8) byte_low = PWM_OFF_ADDR_LOW[a] byte_high = PWM_OFF_ADDR_HIGH[a] wiringpi.wiringPiI2CWriteReg8(pwm_id, byte_low, PWM_LSB) wiringpi.wiringPiI2CWriteReg8(pwm_id, byte_high, PWM_MSB)
def set_laneChange(self, direction): result = wiringpi.wiringPiI2CWriteReg8(self.file_descriptor, 0x22, direction) if result == -1: print "Error writing. errno: ", result else: print "Changing lane to: ", direction
def startLineFollow(self, speed): result = wiringpi.wiringPiI2CWriteReg8(self.file_descriptor, 0x20, speed) if result == -1: print "Error writing. errno: ", result else: print "Start linefollow at speed ", speed
def set_tone(self, tone): result = wiringpi.wiringPiI2CWriteReg8(self.file_descriptor, 0x40, tone) if result == -1: print "Error writing. errno: ", result else: print "Setting tone to: ", tone
def _triggerPulse(self, channel, on, off): wiringpi.wiringPiI2CWriteReg8( self.device, self.registers["LED0_ON_L"] + 4 * channel, on & 0xFF) wiringpi.wiringPiI2CWriteReg8( self.device, self.registers["LED0_ON_H"] + 4 * channel, on >> 8) wiringpi.wiringPiI2CWriteReg8( self.device, self.registers["LED0_OFF_L"] + 4 * channel, off & 0xFF) wiringpi.wiringPiI2CWriteReg8( self.device, self.registers["LED0_OFF_H"] + 4 * channel, off >> 8)
def setupGyro(self): self.gy=wiringPiI2CSetup(ITG3200) if(self.gy == -1): print "Error al conectar con ITG3200" return -1 #Turning on the ITG3200 wiringPiI2CWriteReg8(self.gy, 0x3e, 0x00) wiringPiI2CWriteReg8(self.gy, 0x15, 0x07) wiringPiI2CWriteReg8(self.gy, 0x16, 0x1e) wiringPiI2CWriteReg8(self.gy, 0x17, 0x00) delay(10) self.gyroCalibrate()
def _write_reg(self, reg, data): """ Writes one byte of *data* to register *reg* reg: int register value to write byte to data: 8 bit 8 bit of data to write Returns ------- Integer indicating successful write """ return wp.wiringPiI2CWriteReg8(self.device_id, reg, data)
def SetUp(self): wp.wiringPiSetup() self.fd = wp.wiringPiI2CSetup(self.device) wp.wiringPiI2CWriteReg8(self.fd, self.register_A, 0x70) wp.wiringPiI2CWriteReg8(self.fd, self.register_B, 0x20) wp.wiringPiI2CWriteReg8(self.fd, self.register_Mode, 0x00) # self.CalibrateMag(self.fd) return
def set_pwm_freq(self, freq_hz): """Set the PWM frequency to the provided value in hertz.""" prescaleval = 25000000.0 # 25MHz prescaleval /= 4096.0 # 12-bit prescaleval /= float(freq_hz) prescaleval -= 0.5 logger.debug('Setting PWM frequency to {0} Hz'.format(freq_hz)) logger.debug('Estimated pre-scale: {0}'.format(prescaleval)) prescale = int(math.floor(prescaleval + 0.5)) logger.debug('Final pre-scale: {0}'.format(prescale)) oldmode = wp.wiringPiI2CReadReg8(self.fd, MODE1); newmode = (oldmode & 0x7F) | 0x10; wp.wiringPiI2CWriteReg8(self.fd, MODE1, newmode) # go to sleep wp.wiringPiI2CWriteReg8(self.fd, PRESCALE, prescale) wp.wiringPiI2CWriteReg8(self.fd, MODE1, oldmode) time.sleep(0.005) wp.wiringPiI2CWriteReg8(self.fd, MODE1, oldmode | 0x80)
def _setPWMFreq(self, freq): self.freq = (1000 if freq > 1000 else freq if freq < 400 else 400) prescale = int(25000000 / (4096 * freq) - 0.5) settings = wiringpi.wiringPiI2CReadReg8(self.device, self.registers["MODE1"]) & 0x7F sleep = settings | 0x10 wake = settings & 0xEF restart = wake | 0x80 wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], sleep) wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["PRESCALE"], prescale) wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], wake) time.sleep(0.001) wiringpi.wiringPiI2CWriteReg8(self.device, self.registers["MODE1"], restart)
def init_start(self): # Setup I2C interface for the device. self.set_all_pwm(0, 0) wp.wiringPiI2CWriteReg8(self.fd, MODE1, OUTDRV); wp.wiringPiI2CWriteReg8(self.fd, MODE1, ALLCALL); time.sleep(0.005) # wait for oscillator mode1 = wp.wiringPiI2CReadReg8(self.fd, MODE1); mode1 = mode1 & ~SLEEP; # wake up (reset sleep) wp.wiringPiI2CWriteReg8(self.fd, MODE1, mode1); time.sleep(0.005) # wait for oscillator self.set_pwm_freq(1000)
def configure(self): #enable the module wp.wiringPiI2CWriteReg8(self.m_i2cHandle, RegMap.REG_PWR1, Config.PWR_CLK_PLL_GYRO_X) wp.wiringPiI2CWriteReg8(self.m_i2cHandle, RegMap.REG_ACC_CONFIG, Config.ACC_RANGE_2G) self.m_accFactor = 2. wp.wiringPiI2CWriteReg8(self.m_i2cHandle, RegMap.REG_GYRO_CONFIG, Config.GYRO_RANGE_500) self.m_gyroFactor = 500.
def initInterface(self, sampling_freqeuncy_hz): wpi = self.wpi file_descriptor = wpi.wiringPiI2CSetup(self.I2C_ADDRESS) #configure device to use remote register for sampling rate control reg_value = 0x08 | (1 << 5) #0x08 default value wpi.wiringPiI2CWriteReg8(file_descriptor, self.I2C_REGISTER_ACQ_CONFIG_REG, reg_value) #configure device to capture continuously wpi.wiringPiI2CWriteReg8(file_descriptor, self.I2C_REGISTER_TRIGGER_COUNT, 0xFF) #configure triggering rate wpi.wiringPiI2CWriteReg8(file_descriptor, self.I2C_REGISTER_SAMPLING_DELAY, int(2000 / sampling_freqeuncy_hz)) return file_descriptor
def set_all_pwm(self, on, off): """Sets all PWM channels.""" wp.wiringPiI2CWriteReg8(self.fd, ALL_LED_ON_L, on & 0xFF) wp.wiringPiI2CWriteReg8(self.fd, ALL_LED_ON_H, on >> 8) wp.wiringPiI2CWriteReg8(self.fd, ALL_LED_OFF_L, off & 0xFF) wp.wiringPiI2CWriteReg8(self.fd, ALL_LED_OFF_H, off >> 8)
def set_pwm(self, channel, on, off): """Sets a single PWM channel.""" wp.wiringPiI2CWriteReg8(self.fd, LED0_ON_L + 4 * channel, on & 0xFF) wp.wiringPiI2CWriteReg8(self.fd, LED0_ON_H + 4 * channel, on >> 8) wp.wiringPiI2CWriteReg8(self.fd, LED0_OFF_L + 4 * channel, off & 0xFF) wp.wiringPiI2CWriteReg8(self.fd, LED0_OFF_H + 4 * channel, off >> 8)
def configureIMU(devHandle): wp.wiringPiI2CWriteReg8(devHandle, 0x6b, 0x01) wp.wiringPiI2CWriteReg8(devHandle, 0x1b, 0x00) wp.wiringPiI2CWriteReg8(devHandle, 0x1c, 0x18)
def i2c_write(reg_adress, reg_data): wpi.wiringPiI2CWriteReg8(fb, reg_adress, reg_data)
def update(fd, data): """HT16K33 LEDマトリクス表示更新""" for index, r in enumerate(ROW_ADDRESS): wiringpi.wiringPiI2CWriteReg8(fd, r, data[index])
def cmd(self, code): if wpi.wiringPiI2CWriteReg8(self.fd, 0x02, code): raise IOError("I2C write ERROR {0:x}@{1:x}".format(self.addr, code))
import wiringpi as wpi import math ACC = wpi.wiringPiI2CSetup(0x18) wpi.wiringPiI2CWriteReg8(ACC, 0x20, 0x27) while True: ACC_x = wpi.wiringPiI2CReadReg8(ACC, 0x28) << 8 | wpi.wiringPiI2CReadReg8( ACC, 0x29) # ACC_y = wpi.wiringPiI2CReadReg8(ACC, 0x2a) << 8 | wpi.wiringPiI2CReadReg8( ACC, 0x2b) # ACC_z = wpi.wiringPiI2CReadReg8(ACC, 0x2c) << 8 | wpi.wiringPiI2CReadReg8( ACC, 0x2d) # # # # # if ACC_x > 32768: ACC_x = -(65535 - ACC_x + 1) # # if ACC_y > 32768: ACC_y = -(65535 - ACC_y + 1) # # if ACC_z > 32768: ACC_z = -(65535 - ACC_z + 1) #
import wiringpi import sys fd = wiringpi.wiringPiI2CSetup(0x04) if fd == -1: print "errno: ", errno quit() var = int(sys.argv[1]) print "Trying to set scoop to: ", var result = wiringpi.wiringPiI2CWriteReg8(fd, 1, var) if result == -1: print "Error writing. errno: ", result else: print "Scoop set to: ", var
def sys2rtcSet(fd): wiringpi.wiringPiI2CWriteReg8(fd, 0x02, getHours()) wiringpi.wiringPiI2CWriteReg8(fd, 0x01, getMins()) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, getSecs() + 0b10000000) wiringpi.wiringPiI2CWriteReg8(fd, 0x03, getWeeks()) wiringpi.wiringPiI2CWriteReg8(fd, 0x04, getDays()) wiringpi.wiringPiI2CWriteReg8(fd, 0x05, getMons()) wiringpi.wiringPiI2CWriteReg8(fd, 0x06, getYear())
def clear_register(fd): wiringpi.wiringPiI2CWriteReg8(fd, 0x02, 0b0) # hours wiringpi.wiringPiI2CWriteReg8(fd, 0x01, 0b0) # mins wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0b10000000) # secs wiringpi.wiringPiI2CWriteReg8(fd, 0x03, 0b0) # weeks wiringpi.wiringPiI2CWriteReg8(fd, 0x04, 0b0) # days wiringpi.wiringPiI2CWriteReg8(fd, 0x05, 0b0) # mons
def update(fd, data): """HT16K33 7seg表示更新""" for index, r in enumerate(DIGIT_ADDRESS): wiringpi.wiringPiI2CWriteReg8(fd, r, data[index])
def main(): #init wiringpi.wiringPiSetup() fd = wiringpi.wiringPiI2CSetup(I2C_ADDR) if not fd: return False wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xa1) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xc8) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0x8d) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0x14) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xa6) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0x21) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0x00) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0x7f) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xaf) #clear oled for a in range(8): wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xb0 + a) for b in range(128): wiringpi.wiringPiI2CWriteReg8(fd, 0x40, 0x00) while True: try: #get time yi[0] = full_string("++++OrangePi++++") #yi[1] = full_string(time.strftime("%m/%d %w")) yi[1] = full_string(time.strftime("%m/%d %a")) yi[2] = full_string(time.strftime("%H:%M")) yi[3] = full_string("SSD1306 I2C OLED") #write for zt3 in range(4): wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xb0 + (zt3 * 2)) for zt4 in range(16): for zt in range(8): wiringpi.wiringPiI2CWriteReg8( fd, 0x40, zi[ord(yi[zt3][zt4]) * 16 + zt]) wiringpi.wiringPiI2CWriteReg8(fd, 0x00, 0xb0 + (zt3 * 2) + 1) for zt4 in range(16): for zt in range(8): wiringpi.wiringPiI2CWriteReg8( fd, 0x40, zi[ord(yi[zt3][zt4]) * 16 + zt + 8]) time.sleep(1) except KeyboardInterrupt: print("\nexit") sys.exit(0)