Best Python code snippet using Airtest
bmeall.py
Source:bmeall.py
...25from ctypes import c_ubyte26DEVICE = 0x76 # Default device I2C address27bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 128 # Rev 1 Pi uses bus 029def getShort(data, index):30 # return two bytes from data as a signed 16-bit value31 return c_short((data[index+1] << 8) + data[index]).value32def getUShort(data, index):33 # return two bytes from data as an unsigned 16-bit value34 return (data[index+1] << 8) + data[index]35def getChar(data,index):36 # return one byte from data as a signed char37 result = data[index]38 if result > 127:39 result -= 25640 return result41def getUChar(data,index):42 # return one byte from data as an unsigned char43 result = data[index] & 0xFF44 return result45def readBME280ID(addr=DEVICE):46 # Chip ID Register Address47 REG_ID = 0xD048 (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)49 return (chip_id, chip_version)50def readhumi(addr=DEVICE):51 # Register Addresses52 REG_DATA = 0xF753 REG_CONTROL = 0xF454 REG_CONFIG = 0xF555 REG_CONTROL_HUM = 0xF256 REG_HUM_MSB = 0xFD57 REG_HUM_LSB = 0xFE58 # Oversample setting - page 2759 OVERSAMPLE_TEMP = 260 OVERSAMPLE_PRES = 261 MODE = 162 # Oversample setting for humidity register - page 2663 OVERSAMPLE_HUM = 264 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM)65 control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE66 bus.write_byte_data(addr, REG_CONTROL, control)67 # Read blocks of calibration data from EEPROM68 # See Page 22 data sheet69 cal1 = bus.read_i2c_block_data(addr, 0x88, 24)70 cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)71 cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)72 # Convert byte data to word values73 dig_T1 = getUShort(cal1, 0)74 dig_T2 = getShort(cal1, 2)75 dig_T3 = getShort(cal1, 4)76 dig_P1 = getUShort(cal1, 6)77 dig_P2 = getShort(cal1, 8)78 dig_P3 = getShort(cal1, 10)79 dig_P4 = getShort(cal1, 12)80 dig_P5 = getShort(cal1, 14)81 dig_P6 = getShort(cal1, 16)82 dig_P7 = getShort(cal1, 18)83 dig_P8 = getShort(cal1, 20)84 dig_P9 = getShort(cal1, 22)85 dig_H1 = getUChar(cal2, 0)86 dig_H2 = getShort(cal3, 0)87 dig_H3 = getUChar(cal3, 2)88 dig_H4 = getChar(cal3, 3)89 dig_H4 = (dig_H4 << 24) >> 2090 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)91 dig_H5 = getChar(cal3, 5)92 dig_H5 = (dig_H5 << 24) >> 2093 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)94 dig_H6 = getChar(cal3, 6)95 # Wait in ms (Datasheet Appendix B: Measurement time and current calculation)96 wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575)97 time.sleep(wait_time/1000) # Wait the required time98 # Read temperature/pressure/humidity99 data = bus.read_i2c_block_data(addr, REG_DATA, 8)100 pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)101 temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)102 hum_raw = (data[6] << 8) | data[7]103 #Refine temperature104 var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11105 var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14106 t_fine = var1+var2107 temperature = float(((t_fine * 5) + 128) >> 8);108 # Refine pressure and adjust for temperature109 var1 = t_fine / 2.0 - 64000.0110 var2 = var1 * var1 * dig_P6 / 32768.0111 var2 = var2 + var1 * dig_P5 * 2.0112 var2 = var2 / 4.0 + dig_P4 * 65536.0113 var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0114 var1 = (1.0 + var1 / 32768.0) * dig_P1115 if var1 == 0:116 pressure=0117 else:118 pressure = 1048576.0 - pres_raw119 pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1120 var1 = dig_P9 * pressure * pressure / 2147483648.0121 var2 = pressure * dig_P8 / 32768.0122 pressure = pressure + (var1 + var2 + dig_P7) / 16.0123 # Refine humidity124 humidity = t_fine - 76800.0125 humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.0 * humidity)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * (1.0 + dig_H3 / 67108864.0 * humidity)))126 humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0)127 if humidity > 100:128 humidity = 100129 elif humidity < 0:130 humidity = 0131 #return temperature/100.0132 return humidity133 134def readpres(addr=DEVICE):135 # Register Addresses136 REG_DATA = 0xF7137 REG_CONTROL = 0xF4138 REG_CONFIG = 0xF5139 REG_CONTROL_HUM = 0xF2140 REG_HUM_MSB = 0xFD141 REG_HUM_LSB = 0xFE142 # Oversample setting - page 27143 OVERSAMPLE_TEMP = 2144 OVERSAMPLE_PRES = 2145 MODE = 1146 # Oversample setting for humidity register - page 26147 OVERSAMPLE_HUM = 2148 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM)149 control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE150 bus.write_byte_data(addr, REG_CONTROL, control)151 # Read blocks of calibration data from EEPROM152 # See Page 22 data sheet153 cal1 = bus.read_i2c_block_data(addr, 0x88, 24)154 cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)155 cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)156 # Convert byte data to word values157 dig_T1 = getUShort(cal1, 0)158 dig_T2 = getShort(cal1, 2)159 dig_T3 = getShort(cal1, 4)160 dig_P1 = getUShort(cal1, 6)161 dig_P2 = getShort(cal1, 8)162 dig_P3 = getShort(cal1, 10)163 dig_P4 = getShort(cal1, 12)164 dig_P5 = getShort(cal1, 14)165 dig_P6 = getShort(cal1, 16)166 dig_P7 = getShort(cal1, 18)167 dig_P8 = getShort(cal1, 20)168 dig_P9 = getShort(cal1, 22)169 dig_H1 = getUChar(cal2, 0)170 dig_H2 = getShort(cal3, 0)171 dig_H3 = getUChar(cal3, 2)172 dig_H4 = getChar(cal3, 3)173 dig_H4 = (dig_H4 << 24) >> 20174 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)175 dig_H5 = getChar(cal3, 5)176 dig_H5 = (dig_H5 << 24) >> 20177 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)178 dig_H6 = getChar(cal3, 6)179 # Wait in ms (Datasheet Appendix B: Measurement time and current calculation)180 wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575)181 time.sleep(wait_time/1000) # Wait the required time182 # Read temperature/pressure/humidity183 data = bus.read_i2c_block_data(addr, REG_DATA, 8)184 pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)185 temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)186 hum_raw = (data[6] << 8) | data[7]187 #Refine temperature188 var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11189 var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14190 t_fine = var1+var2191 temperature = float(((t_fine * 5) + 128) >> 8);192 # Refine pressure and adjust for temperature193 var1 = t_fine / 2.0 - 64000.0194 var2 = var1 * var1 * dig_P6 / 32768.0195 var2 = var2 + var1 * dig_P5 * 2.0196 var2 = var2 / 4.0 + dig_P4 * 65536.0197 var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0198 var1 = (1.0 + var1 / 32768.0) * dig_P1199 if var1 == 0:200 pressure=0201 else:202 pressure = 1048576.0 - pres_raw203 pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1204 var1 = dig_P9 * pressure * pressure / 2147483648.0205 var2 = pressure * dig_P8 / 32768.0206 pressure = pressure + (var1 + var2 + dig_P7) / 16.0207 #return temperature/100.0208 press1 = pressure/100.0209 return press1210 211def readtemp(addr=DEVICE):212 # Register Addresses213 REG_DATA = 0xF7214 REG_CONTROL = 0xF4215 REG_CONFIG = 0xF5216 REG_CONTROL_HUM = 0xF2217 REG_HUM_MSB = 0xFD218 REG_HUM_LSB = 0xFE219 # Oversample setting - page 27220 OVERSAMPLE_TEMP = 2221 OVERSAMPLE_PRES = 2222 MODE = 1223 # Oversample setting for humidity register - page 26224 OVERSAMPLE_HUM = 2225 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM)226 control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE227 bus.write_byte_data(addr, REG_CONTROL, control)228 # Read blocks of calibration data from EEPROM229 # See Page 22 data sheet230 cal1 = bus.read_i2c_block_data(addr, 0x88, 24)231 cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)232 cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)233 # Convert byte data to word values234 dig_T1 = getUShort(cal1, 0)235 dig_T2 = getShort(cal1, 2)236 dig_T3 = getShort(cal1, 4)237 dig_P1 = getUShort(cal1, 6)238 dig_P2 = getShort(cal1, 8)239 dig_P3 = getShort(cal1, 10)240 dig_P4 = getShort(cal1, 12)241 dig_P5 = getShort(cal1, 14)242 dig_P6 = getShort(cal1, 16)243 dig_P7 = getShort(cal1, 18)244 dig_P8 = getShort(cal1, 20)245 dig_P9 = getShort(cal1, 22)246 dig_H1 = getUChar(cal2, 0)247 dig_H2 = getShort(cal3, 0)248 dig_H3 = getUChar(cal3, 2)249 dig_H4 = getChar(cal3, 3)250 dig_H4 = (dig_H4 << 24) >> 20251 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)252 dig_H5 = getChar(cal3, 5)253 dig_H5 = (dig_H5 << 24) >> 20254 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)255 dig_H6 = getChar(cal3, 6)256 # Wait in ms (Datasheet Appendix B: Measurement time and current calculation)257 wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575)258 time.sleep(wait_time/1000) # Wait the required time259 # Read temperature/pressure/humidity260 data = bus.read_i2c_block_data(addr, REG_DATA, 8)261 pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)262 temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)263 hum_raw = (data[6] << 8) | data[7]264 #Refine temperature265 var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11266 var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14267 t_fine = var1+var2268 temperature = float(((t_fine * 5) + 128) >> 8);269 #return temperature/100.0270 temp1 = temperature/100.0271 return temp1272 273def readall(addr=DEVICE):274 # Register Addresses275 REG_DATA = 0xF7276 REG_CONTROL = 0xF4277 REG_CONFIG = 0xF5278 REG_CONTROL_HUM = 0xF2279 REG_HUM_MSB = 0xFD280 REG_HUM_LSB = 0xFE281 # Oversample setting - page 27282 OVERSAMPLE_TEMP = 2283 OVERSAMPLE_PRES = 2284 MODE = 1285 # Oversample setting for humidity register - page 26286 OVERSAMPLE_HUM = 2287 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM)288 control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE289 bus.write_byte_data(addr, REG_CONTROL, control)290 # Read blocks of calibration data from EEPROM291 # See Page 22 data sheet292 cal1 = bus.read_i2c_block_data(addr, 0x88, 24)293 cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)294 cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)295 # Convert byte data to word values296 dig_T1 = getUShort(cal1, 0)297 dig_T2 = getShort(cal1, 2)298 dig_T3 = getShort(cal1, 4)299 dig_P1 = getUShort(cal1, 6)300 dig_P2 = getShort(cal1, 8)301 dig_P3 = getShort(cal1, 10)302 dig_P4 = getShort(cal1, 12)303 dig_P5 = getShort(cal1, 14)304 dig_P6 = getShort(cal1, 16)305 dig_P7 = getShort(cal1, 18)306 dig_P8 = getShort(cal1, 20)307 dig_P9 = getShort(cal1, 22)308 dig_H1 = getUChar(cal2, 0)309 dig_H2 = getShort(cal3, 0)310 dig_H3 = getUChar(cal3, 2)311 dig_H4 = getChar(cal3, 3)312 dig_H4 = (dig_H4 << 24) >> 20313 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)314 dig_H5 = getChar(cal3, 5)315 dig_H5 = (dig_H5 << 24) >> 20316 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)317 dig_H6 = getChar(cal3, 6)318 # Wait in ms (Datasheet Appendix B: Measurement time and current calculation)319 wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575)320 time.sleep(wait_time/1000) # Wait the required time321 # Read temperature/pressure/humidity322 data = bus.read_i2c_block_data(addr, REG_DATA, 8)323 pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)...
bme280.py
Source:bme280.py
...25from ctypes import c_ubyte26DEVICE = 0x76 # Default device I2C address27bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 128 # Rev 1 Pi uses bus 029def getShort(data, index):30 # return two bytes from data as a signed 16-bit value31 return c_short((data[index+1] << 8) + data[index]).value32def getUShort(data, index):33 # return two bytes from data as an unsigned 16-bit value34 return (data[index+1] << 8) + data[index]35def getChar(data,index):36 # return one byte from data as a signed char37 result = data[index]38 if result > 127:39 result -= 25640 return result41def getUChar(data,index):42 # return one byte from data as an unsigned char43 result = data[index] & 0xFF44 return result45def readBME280ID(addr=DEVICE):46 # Chip ID Register Address47 REG_ID = 0xD048 (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)49 return (chip_id, chip_version)50def readBME280All(addr=DEVICE):51 # Register Addresses52 REG_DATA = 0xF753 REG_CONTROL = 0xF454 REG_CONFIG = 0xF555 REG_CONTROL_HUM = 0xF256 REG_HUM_MSB = 0xFD57 REG_HUM_LSB = 0xFE58 # Oversample setting - page 2759 OVERSAMPLE_TEMP = 260 OVERSAMPLE_PRES = 261 MODE = 162 # Oversample setting for humidity register - page 2663 OVERSAMPLE_HUM = 264 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM)65 control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE66 bus.write_byte_data(addr, REG_CONTROL, control)67 # Read blocks of calibration data from EEPROM68 # See Page 22 data sheet69 cal1 = bus.read_i2c_block_data(addr, 0x88, 24)70 cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)71 cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)72 # Convert byte data to word values73 dig_T1 = getUShort(cal1, 0)74 dig_T2 = getShort(cal1, 2)75 dig_T3 = getShort(cal1, 4)76 dig_P1 = getUShort(cal1, 6)77 dig_P2 = getShort(cal1, 8)78 dig_P3 = getShort(cal1, 10)79 dig_P4 = getShort(cal1, 12)80 dig_P5 = getShort(cal1, 14)81 dig_P6 = getShort(cal1, 16)82 dig_P7 = getShort(cal1, 18)83 dig_P8 = getShort(cal1, 20)84 dig_P9 = getShort(cal1, 22)85 dig_H1 = getUChar(cal2, 0)86 dig_H2 = getShort(cal3, 0)87 dig_H3 = getUChar(cal3, 2)88 dig_H4 = getChar(cal3, 3)89 dig_H4 = (dig_H4 << 24) >> 2090 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)91 dig_H5 = getChar(cal3, 5)92 dig_H5 = (dig_H5 << 24) >> 2093 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)94 dig_H6 = getChar(cal3, 6)95 # Wait in ms (Datasheet Appendix B: Measurement time and current calculation)96 wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575)97 time.sleep(wait_time/1000) # Wait the required time 98 # Read temperature/pressure/humidity99 data = bus.read_i2c_block_data(addr, REG_DATA, 8)100 pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)...
bmpsensor.py
Source:bmpsensor.py
...9 10def convertToString(data):11 # Simple function to convert binary data into12 return str((data[1] + (256 * data[0])) / 1.2)13def getShort(data, index):14 # return two bytes from data as a signed 16-bit value15 return c_short((data[index] << 8) + data[index + 1]).value16def getUshort(data, index):17 # return two bytes from data as an unsigned 16-bit value18 return (data[index] << 8) + data[index + 1]19def readBmp180Id(addr=DEVICE):20 # Chip ID Register Address21 REG_ID = 0xD022 (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)23 return (chip_id, chip_version)24 25def readBmp180(addr=0x77):26 # Register Addresses27 REG_CALIB = 0xAA28 REG_MEAS = 0xF429 REG_MSB = 0xF630 REG_LSB = 0xF731 # Control Register Address32 CRV_TEMP = 0x2E33 CRV_PRES = 0x34 34 # Oversample setting35 OVERSAMPLE = 3 # 0 - 336 37 # Read calibration data from EEPROM38 cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)39 # Convert byte data to word values40 AC1 = getShort(cal, 0)41 AC2 = getShort(cal, 2)42 AC3 = getShort(cal, 4)43 AC4 = getUshort(cal, 6)44 AC5 = getUshort(cal, 8)45 AC6 = getUshort(cal, 10)46 B1 = getShort(cal, 12)47 B2 = getShort(cal, 14)48 MB = getShort(cal, 16)49 MC = getShort(cal, 18)50 MD = getShort(cal, 20)51 # Read temperature52 bus.write_byte_data(addr, REG_MEAS, CRV_TEMP)53 time.sleep(0.005)54 (msb, lsb) = bus.read_i2c_block_data(addr, REG_MSB, 2)55 UT = (msb << 8) + lsb56 # Read pressure57 bus.write_byte_data(addr, REG_MEAS, CRV_PRES + (OVERSAMPLE << 6))58 time.sleep(0.04)59 (msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)60 UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE)61 # Refine temperature62 X1 = ((UT - AC6) * AC5) >> 1563 X2 = (MC << 11) / (X1 + MD)64 B5 = X1 + X2...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!