def begin(self, mode=None): if mode is None: mode = BNO055.OPERATION_MODE_NDOF # Open I2C bus self._i2c = mraa.I2c(0) self._i2c.address(self._address) # First send a thow-away command and ignore any response or I2C errors # just to make sure the BNO is in a good state and ready to accept # commands (this seems to be necessary after a hard power down). try: self.writeBytes(BNO055.BNO055_PAGE_ID_ADDR, 0x0) except IOError: # Swallow an IOError that might be raised by an I2C issue. Only do # this for this very first command to help get the BNO and board's # I2C into a clear state ready to accept the next commands. pass # Make sure we have the right device if self.readBytes(BNO055.BNO055_CHIP_ID_ADDR)[0] != BNO055.BNO055_ID: time.sleep(1) # Wait for the device to boot up if self.readBytes( BNO055.BNO055_CHIP_ID_ADDR)[0] != BNO055.BNO055_ID: return False # Switch to config mode self.setMode(BNO055.OPERATION_MODE_CONFIG) # Trigger a reset and wait for the device to boot up again self.writeBytes(BNO055.BNO055_SYS_TRIGGER_ADDR, 0x20) time.sleep(1) while self.readBytes( BNO055.BNO055_CHIP_ID_ADDR)[0] != BNO055.BNO055_ID: time.sleep(0.01) time.sleep(0.05) # Set to normal power mode self.writeBytes(BNO055.BNO055_PWR_MODE_ADDR, BNO055.POWER_MODE_NORMAL) time.sleep(0.01) self.writeBytes(BNO055.BNO055_PAGE_ID_ADDR, 0) #####new from ros imu driver##### #self.writeBytes(BNO055.BNO055_UNIT_SEL_ADDR, 0x82) #Android fusion data, C, RadianPerS, m/s2 (not mg) #self.writeBytes(BNO055.BNO055_AXIS_MAP_CONFIG_ADDR, 0x21) #switch x and y axis #self.writeBytes(BNO055.BNO055_AXIS_MAP_SIGN_ADDR, 0x0) #####end new#### self.writeBytes(BNO055.BNO055_SYS_TRIGGER_ADDR, 0) time.sleep(0.01) # Set the requested mode self.setMode(mode) time.sleep(0.02) return True
def resetPinmuxOfUserConfig(self, pinmux): for i in range(0, 20): pinmuxes = self.pinmuxArray(i) if pinmux in ' '.join(pinmuxes): defaultPinmux = pinmuxes[0] self._setPinmuxOfUserConfig(defaultPinmux, i) self._setPullModeOfUserConfig(i, 'Hiz') if 'GPIO' in defaultPinmux: self.setPinmuxToGpio(defaultPinmux, i) elif 'IIC_SDA' in defaultPinmux: mraa.I2c(0)
def __init__(self, address=0x5e, delaytime=0.01, debug=False): if (debug): print("Initialising the sensor") self.magSensor = mraa.I2c(2, True) self.magSensor.address(address) self.rbuffer = bytearray([0] * 10) self.magSensor.writeReg(0x00, 0x05) self.latestMeasurements = (0, 0, 0, 0, False) #self.shouldContinueContinuousMeasurements = True #self.latestMeasurementsTime = 0 if (debug): print("Sensor set to low power mode")
def __init__(self, bus, debug=False, freq=I2C_STD): # self.x = m.I2c(bus, raw=True) # raw=True forces manual bus selection, vs. board default self.x = m.I2c( bus ) # note i had to remove the raw keyword as it did not work. maybe needs updating? self.debug = debug # if self.debug: print self.x self.x.frequency( freq ) #default to I2C_STD (up to 100kHz). Other options: I2C_FAST (up to 400kHz), I2C_HIGH (up to 3.4MHz) self.x.address(0x05) # Address of the HTU21D sensor
def i2c_write(self, pin_number, address, register, data): """ Requires a pin number, device address, a register to write to, and the data to write to the register. """ if not self.pins["i2c"]: bus = mraa.I2c(pin_number) self.pins["i2c"] = bus else: bus = self.pins["i2c"] bus.address(address) bus.writeReg(register, data)
def configureArduinoI2c(self): btnchoicewind = ButtonChoiceWindow(screen=self.topmenu.gscreen, title='Enable I2C on IO18 & IO19', text='', buttons=['Enable', 'Disable', ('Cancel', 'ESC')], width=40) if btnchoicewind == 'cancel': return elif btnchoicewind == 'enable': i2c = mraa.I2c(0) self.setPinmuxOfUserConfig('I2C') elif (btnchoicewind == 'disable') and self.checkPinmuxConfig('I2C'): self.resetPinmuxOfUserConfig('I2C') self.saveConfig(self.config)
def begin(self): self.i2c = mraa.I2c(1) self.i2c.address(self.address) #calibrate to 32V 2A self.calValue = 4096 self.currentDivider_mA = 10 self.powerDivider_mW = 2 config = (Registers.INA219_CONFIG_BVOLTAGERANGE_32V | Registers.INA219_CONFIG_GAIN_8_320MV | Registers.INA219_CONFIG_BADCRES_12BIT | Registers.INA219_CONFIG_SADCRES_12BIT_1S_532US | Registers.INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS) self.writeRegister(Registers.INA219_REG_CONFIG, config)
def __init__(self, bus=1, address=TRONE_BASEADDR, debug=False, freq=I2C_STD): self.x = m.I2c( bus) # raw=True forces manual bus selection, vs. board default self.address = address self.x.frequency( freq ) # default to I2C_STD (up to 100kHz). Other options: I2C_FAST (up to 400kHz), I2C_HIGH (up to 3.4Mhz) self.x.address(self.address) # address of the TeraRanger sensor self.debug = debug if self.debug: print m.printError(self.x.address(address))
def __init__(self): # set up I2C self.i2c = mraa.I2c(1) self.i2c.frequency(100000) self.i2c.address(0x04) self.leftWheelSpeed = 0 self.rightWheelSpeed = 0 self.leftWheelDirection = 0 self.rightWheelDirection = 0 self.stopped = False # Motor corrections in range (0, 1) self.L_CORRECTION = 1 self.R_CORRECTION = .888
def __init__(self): self.lcd_device = mraa.I2c(0) self.lcd_device.address(LCD_ADDRESS) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x02) self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) time.sleep(0.2)
def __init__(self): self._i2c = mraa.I2c(Mpu.I2C_PORT) self._i2c.address(Mpu.I2C_ADDRESS) # Read WHO_AM_I register (0x75) of device at address. self._i2c.writeByte(0x75) self._i2c.readByte() # Set power management register (0x6B) to use gyro clock (0x01). self._i2c.writeReg(0x6B, 0x01) # Set configuration register (0x1A) filter at 90Hz (0x02). self._i2c.writeReg(0x1A, 0x02) # Set gyro configuration register (0x1B) to all zeros (0x00). self._i2c.writeReg(0x1B, 0x00) # Set accelerometer configuration register (0x1C) to all zeros (0x00). self._i2c.writeReg(0x1C, 0x00)
def __init__(self, address=ADXL345_ADDRESS, i2c=None, **kwargs): """ Initialize the ADXL345 accelerometer using its I2C interface. i2c and kwargs do nothing; they are retained for compatibility with Adafruit_ADXL345. """ self.device = mraa.I2c(0) # Check that accelerometer is connected, then enable it self.device.address(ADXL345_ADDRESS) device_id = self.device.readReg(ADXL345_REG_DEVID) if device_id == ADXL345_DEVID: # Put device into ready-to-measure mode self.device.writeReg(ADXL345_REG_POWER_CTL, ADXL345_POWERCTL_MEASURE) else: raise RuntimeError('Expected deviceid %02x but got %02x' % (ADXL345_DEVID, device_id))
def __init__(self): """Setup the display, turn on backlight and text display + ...?""" self.device = m.I2c(BUS, False) self.device.frequency(m.I2C_STD) self.device.address(ADDRESS) self.write(0x03) self.write(0x03) self.write(0x03) self.write(0x02) self.write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) self.write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.write(LCD_CLEARDISPLAY) self.write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep(0.2)
def temp(): x = mraa.Gpio(26) x.dir(mraa.DIR_OUT) ads1115 = mraa.I2c(0) ads1115.address(0x48) #for i in range(0, 10): ads1115.writeWordReg(1, 0x83C1) raw = ads1115.readWordReg(0) analogValue = ((raw&0xff00)>>8)+((raw&0x00ff)<<8) dat=(85 * analogValue) /4096 celsius = dat fahrenheit = celsius * 9.0/5.0 + 32.0; #print "%d degrees Celsius, or %d degrees Fahrenheit" \ #% (celsius, fahrenheit) #time.sleep(1) return fahrenheit
def rtc_get_time(): rtc = mraa.I2c(0) rtc.address(DS3231_I2C_ADDR) tsec = bcd2dec(rtc.readReg(0x00)) tmin = bcd2dec(rtc.readReg(0x01)) thour = bcd2dec(rtc.readReg(0x02)) tweek = rtc.readReg(0x03) tday = bcd2dec(rtc.readReg(0x04)) tmon = bcd2dec(rtc.readReg(0x05)) tyear_s = bcd2dec(rtc.readReg(0x06)) tyear = 2000 + tyear_s t = str(tyear) + "-" + str(tmon) + "-" + str(tday) + " " + str( thour) + ":" + str(tmin) + ":" + str(tsec) t = datetime.strptime(t, "%Y-%m-%d %H:%M:%S") return t
def set_key(key): mac = open('/sys/class/net/eth0/address').readline().upper().strip() DEVICE_ID = mac.replace(':', '') dig = hmac.new(key, msg=DEVICE_ID, digestmod=hashlib.sha256).digest() key = base64.b64encode(dig).decode() # py3k-mode #print "key =", key[:7] # use the first 7 bytes as the secure key rtc = mraa.I2c(0) rtc.address(DS3231_I2C_ADDR) rtc.writeReg(0x07, ord(key[0]) & 0xff) rtc.writeReg(0x08, ord(key[1]) & 0xff) rtc.writeReg(0x09, ord(key[2]) & 0xff) rtc.writeReg(0x0A, ord(key[3]) & 0xff) rtc.writeReg(0x0B, ord(key[4]) & 0xff) rtc.writeReg(0x0C, ord(key[5]) & 0xff) rtc.writeReg(0x0D, ord(key[6]) & 0xff)
def __init__(self, i2cBus, debugging=False, i2cAddress=0x40): self.i2c = mraa.I2c(i2cBus) self.i2cAddress = i2cAddress self.debugging = debugging self.frequency = 50 #Default Frequency: 50 Hz self.reset() self.i2c.writeReg(self.__MODE2, self.__OUTDRV) self.i2c.writeReg(self.__MODE1, self.__ALLCALL) time.sleep(0.005) # wait for oscillator self.i2c.address(self.i2cAddress) mode1 = self.i2c.readReg(self.__MODE1) mode1 = mode1 & ~self.__SLEEP # wake up (reset sleep) self.i2c.address(self.i2cAddress) self.i2c.writeReg(self.__MODE1, mode1) time.sleep(0.005) # wait for oscillator self.setFrequency(self.frequency)
def __init__(self, address=PN532_I2C_SLAVE_ADDRESS, i2c_channel=RPI_DEFAULT_I2C_NEW):#TODO: poner el canal por defecto """Constructor for the Pn532_i2c class. Arguments: @param[in] address I2C slave address for the PN532 (default = PN532_FRAME_TYPE_DATA) @param[in] i2c_channel I2C channel to use. (default = RPI_DEFAULT_I2C_NEW) """ self.logger = logging.getLogger() self.logger.propagate = LOGGING_ENABLED if self.logger.propagate: self.logger.setLevel("DEBUG") self.address = address self.i2c_channel = i2c_channel self.PN532 = mraa.I2c(self.i2c_channel) #Cambiado a mraa self.PN532.address(self.address)
def __init__(self, address=0x3C): super(SH1107G_SSD1327, self).__init__() # self._bus = Bus() self._bus = mraa.I2c(0) self._addr = address self._bus.address(self._addr) if self._bus.writeByte(0): print("Check if the OLED SH1107G/SSD1307 inserted, then try again") sys.exit(1) # id = self._bus.read_byte_data(self._addr, SH1107G_SSD1327._REG_CMD) id = self._bus.readReg(SH1107G_SSD1327._REG_CMD) # print(" id = 0x{:2x}".format(id)) self._sh1107 = (id & 0x3F) == 0x07 if not self._sh1107: self._ssd1327 = SSD1327(0) return blk = [0xAE] # Display OFF blk.append(0xD5) # Set Dclk blk.append(0x50) # 100Hz blk.append(0x20) # Set row address blk.append(0x81) # Set contrast control blk.append(0x80) blk.append(0xA0) # Segment remap blk.append(0xA4) # Set Entire Display ON blk.append(0xA6) # Normal display blk.append(0xAD) # Set external VCC blk.append(0x80) blk.append(0xC0) # Set Common scan direction blk.append(0xD9) # Set phase leghth blk.append(0x1F) blk.append(0xDB) # Set Vcomh voltage blk.append(0x27) blk.append(0xAF) # Display ON blk.append(0xB0) blk.append(0x00) blk.append(0x10) self._cmds(blk) self.clear()
def rtc_set_time(t): rtc = mraa.I2c(0) rtc.address(DS3231_I2C_ADDR) tyear = t.year tweek = dec2bcd(t.weekday()) tweek = t.weekday() tmon = dec2bcd(t.month) tday = dec2bcd(t.day) thour = dec2bcd(t.hour) tmin = dec2bcd(t.minute) tsec = dec2bcd(t.second) tyear_s = dec2bcd(t.year - 2000) rtc.writeReg(0x00, tsec & 0xff) rtc.writeReg(0x01, tmin & 0xff) rtc.writeReg(0x02, thour & 0xff) rtc.writeReg(0x03, tweek & 0xff) rtc.writeReg(0x04, tday & 0xff) rtc.writeReg(0x05, tmon & 0xff) rtc.writeReg(0x06, tyear_s & 0xff)
def __init__(self): self.GYRO_ADDRESS = 0x68 # ITG3200 Register Defines self.ITG3200_WHO = 0x00 self.ITG3200_SMPL = 0x15 self.ITG3200_DLPF = 0x16 self.ITG3200_INT_C = 0x17 self.ITG3200_INT_S = 0x1A self.ITG3200_TMP_H = 0x1B self.ITG3200_TMP_L = 0x1C self.ITG3200_GX_H = 0x1D self.ITG3200_GX_L = 0x1E self.ITG3200_GY_H = 0x1F self.ITG3200_GY_L = 0x20 self.ITG3200_GZ_H = 0x21 self.ITG3200_GZ_L = 0x22 self.ITG3200_PWR_M = 0x3E self.x_offset = 0 self.y_offset = 0 self.z_offset = 0 self.bus = mraa.I2c(1) self.bus.address(self.GYRO_ADDRESS)
f = os.popen('ifconfig eth0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') DEVICE_IP = f.read() if (DEVICE_IP == ''): f = os.popen( 'ifconfig apcli0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') DEVICE_IP = f.read() if Use_RTC_DS3231 == 1: import mraa import hmac import hashlib import base64 import urllib DS3231_I2C_ADDR = 0x68 rtc = mraa.I2c(0) rtc.address(DS3231_I2C_ADDR) SecureKey = chr(rtc.readReg(0x07)) + chr(rtc.readReg(0x08)) + chr( rtc.readReg(0x09)) + chr(rtc.readReg(0x0A)) + chr( rtc.readReg(0x0B)) + chr(rtc.readReg(0x0C)) + chr( rtc.readReg(0x0D)) SecureKey = urllib.quote_plus(SecureKey) print "SecureKey = ", SecureKey pm_q = Queue(maxsize=5) tmp_q = Queue(maxsize=5) light_q = Queue(maxsize=5) gas_q = Queue(maxsize=5) tvoc_q = Queue(maxsize=5) fields = {
# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE # # Example Usage: Shows the 'advanced' i2c functionality from python i2c # read/write import mraa as m # initialise I2C x = m.I2c(0) x.address(0x77) # initialise device if x.readReg(0xd0) != 0x55: print("error") # we want to read temperature so write 0x2e into control reg x.writeReg(0xf4, 0x2e) # read a 16bit reg, obviously it's uncalibrated so mostly a useless value :) print(str(x.readWordReg(0xf6))) # and we can do the same thing with the read()/write() calls if we wished # thought I'd really not recommend it!
import mraa mraa.init() print(mraa.getPinCount()) print(mraa.getVersion()) print("Raw adc bits: " + str(mraa.adcRawBits())) print("Support adc bits: " + str(mraa.adcSupportedBits())) count = 0 while (count < 100): try: #There is no adcs on this dragonboard. wtf!! even from 0 to 100 print("Reading I2c" + str(count)) port = mraa.I2c(count) print("Trying to read I2c" + str(count)) port.dir(mraa.DIR_IN) a = port.read(count) if (a / 255 * 100 > 90): pass #BOTTLE IF FULL elif (a / 255 * 100 < 50): pass #BOTTLE IS HALF FULL elif (a / 255 * 100 < 25): pass #YOU MIGHT WANNA CONSIDER REFILL THE MEDICINE elif (a / 255 * 100 < 10): pass #TIME TO GO GET MEDICATION print("Got a I2c" + str(count) + " of " + str(a)) except: print("Are you sure you have an I2c" + str(count)) count = count + 1
def enableI2c(self): x = mraa.I2c(0) self.showIoConfiguration()
import mraa import time i2c = mraa.I2c(0) i2c.address(0x48) while True: d = i2c.read(2) temp = (d[0] * 256.0 + d[1]) / 128.0 print(temp) time.sleep(1)
# "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE import mraa # This example will change the LCD backlight on the Grove-LCD RGB backlight # to a nice shade of purple x = mraa.I2c(0) x.address(0x62) x.writeReg(0,0) x.writeReg(1,0) # Be careful that your i2c device can actually handle a 'batch' handling of # such data, this is not typical in arduino type devices s = "\x08\xAA\x04\xFF\x02\xFF" x.write(s)
def __init__( self, bus = 1, address = 0x48): self.command = 0b1000001101000100 self.adc = mraa.I2c(1) self.adc.address(address)
def setUp(self): self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
import sys import mraa import time import json import socket from datetime import datetime if __name__ == '__main__': #connect with mqtt protocol bus = mraa.I2c(0) bus.address(0x1D) # ADXL355 address, 0x53(83) bus.writeReg(0x2C, 0x01) #0x2C register: #bit 7:I2C speed #bit 6:interrupt polarity #bits [1:0]: range bus.writeReg(0x24, 0x07) #0x24 register: #bit 2: active Z accl #bit 1: active Y accl #bit 0: active X accl bus.writeReg(0x2D, 0x02) #0x2D register: #bit 2: DRDY on or off #bit 1: disable temperature measure #bit 0: STANDBY mode if 1 time.sleep(0.5) last_t = time.time()