<commit_before>#!/usr/bin/python
#  bme280.py
#  Read data from a digital pressure sensor.
#
#  Official datasheet available from :
#  https://www.bosch-sensortec.com/bst/products/all_products/bme280
#
# Based on bme280.py from <NAME>
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
import smbus
import time
import json
from ctypes import c_short
from ctypes import c_byte
from ctypes import c_ubyte

class bme280:
  DEVICE = 0x77 # Default device I2C address
  bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1
  # Rev 1 Pi uses bus 0

  def __init__(self, addr=DEVICE):
    DEVICE = addr
  
  def getShort(self, data, index):
    # return two bytes from data as a signed 16-bit value
    return c_short((data[index+1] << 8) + data[index]).value

  def getUShort(self, data, index):
    # return two bytes from data as an unsigned 16-bit value
    return (data[index+1] << 8) + data[index]

  def getChar(self, data,index):
    # return one byte from data as a signed char
    result = data[index]
    if result > 127:
      result -= 256
    return result

  def getUChar(self, data,index):
    # return one byte from data as an unsigned char
    result =  data[index] & 0xFF
    return result

  def readBME280ID(self, addr=DEVICE):
    # Chip ID Register Address
    REG_ID     = 0xD0
    (chip_id, chip_version) = bme280.bus.read_i2c_block_data(addr, REG_ID, 2)
    return chip_id, chip_version

  def readBME280All(self, addr=DEVICE):
    
    # Register Addresses
    REG_DATA = 0xF7
    REG_CONTROL = 0xF4
    REG_CONFIG  = 0xF5

    REG_HUM_MSB = 0xFD
    REG_HUM_LSB = 0xFE

    # Oversample setting - page 27
    OVERSAMPLE_TEMP = 2
    OVERSAMPLE_PRES = 2
    MODE = 1

    control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE
    bme280.bus.write_byte_data(addr, 0xF2, 1)
    
    bme280.bus.write_byte_data(addr, REG_CONTROL, control)
    
    # Read blocks of calibration data from EEPROM
    # See Page 22 data sheet
    cal1 = bme280.bus.read_i2c_block_data(addr, 0x88, 24)
    cal2 = bme280.bus.read_i2c_block_data(addr, 0xA1, 1)
    cal3 = bme280.bus.read_i2c_block_data(addr, 0xE1, 7)

    # Convert byte data to word values
    dig_T1 = self.getUShort(cal1, 0)
    dig_T2 = self.getShort(cal1, 2)
    dig_T3 = self.getShort(cal1, 4)
    
    dig_P1 = self.getUShort(cal1, 6)
    dig_P2 = self.getShort(cal1, 8)
    dig_P3 = self.getShort(cal1, 10)
    dig_P4 = self.getShort(cal1, 12)
    dig_P5 = self.getShort(cal1, 14)
    dig_P6 = self.getShort(cal1, 16)
    dig_P7 = self.getShort(cal1, 18)
    dig_P8 = self.getShort(cal1, 20)
    dig_P9 = self.getShort(cal1, 22)
    
    dig_H1 = self.getUChar(cal2, 0)
    dig_H2 = self.getShort(cal3, 0)
    dig_H3 = self.getUChar(cal3, 2)
    
    dig_H4 = self.getChar(cal3, 3)
    dig_H4 = (dig_H4 << 24) >> 20
    dig_H4 = dig_H4 | (self.getChar(cal3, 4) & 0x0F)
    
    dig_H5 = self.getChar(cal3, 5)
    dig_H5 = (dig_H5 << 24) >> 20
    dig_H5 = dig_H5 | (self.getUChar(cal3, 4) >> 4 & 0x0F)
    
    dig_H6 = self.getChar(cal3, 6)
    
    # Read temperature/pressure/humidity
    data = bme280.bus.read_i2c_block_data(addr, REG_DATA, 8)
    pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
    temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
    hum_raw = (data[6] << 8) | data[7]
    
    #Refine temperature
    var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11
    var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14
    t_fine = var1+var2
    temperature = float(((t_fine * 5) + 128) >> 8);
    
    # Refine pressure and adjust for temperature
    var1 = t_fine / 2.0 - 64000.0
    var2 = var1 * var1 * dig_P6 / 32768.0
    var2 = var2 + var1 * dig_P5 * 2.0
    var2 = var2 / 4.0 + dig_P4 * 65536.0
    var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0
    var1 = (1.0 + var1 / 32768.0) * dig_P1
    if var1 == 0:
      pressure=0
    else:
      pressure = 1048576.0 - pres_raw
      pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1
      var1 = dig_P9 * pressure * pressure / 2147483648.0
      var2 = pressure * dig_P8 / 32768.0
      pressure = pressure + (var1 + var2 + dig_P7) / 16.0

    # Refine humidity
    humidity = t_fine - 76800.0
    humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.8 * humidity)) * \
               (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * \
                                  (1.0 + dig_H3 / 67108864.0 * humidity)))
    humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0)
    if humidity > 100:
      humidity = 100
    elif humidity < 0:
      humidity = 0

    return temperature/100.0,pressure/100.0,humidity

  def getMeasurement(self):
    chipId, chipVersion = self.readBME280ID()
    temperature,pressure,humidity = self.readBME280All()

    resp = "{ 'chip_id': '%s', 'chip_version': '%s', 'tempf': '%s', 'barmin': '%s' }" % (chipId, chipVersion, temperature, pressure)
    
    return json.dumps(resp)
    
def main():
  sensor = bme280()
  
  chip_id, chip_version = sensor.readBME280ID()
  print "Chip ID     :", chip_id
  print "Version     :", chip_version

  temperature,pressure,humidity = sensor.readBME280All()

  print temperature,  pressure, humidity
  print "Temperature : ", temperature, "C, ", (temperature * 1.8) + 32, "F"
  print "Pressure : ", pressure, "hPa"
  print "Humidity : ", humidity, "%"
  print sensor.getMeasurement()

if __name__=="__main__":
  main()
<commit_msg>Fixed some references to self in bme<commit_after>#!/usr/bin/python
#  bme280.py
#  Read data from a digital pressure sensor.
#
#  Official datasheet available from :
#  https://www.bosch-sensortec.com/bst/products/all_products/bme280
#
# Based on bme280.py from <NAME>
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
import smbus
import time
import json
from ctypes import c_short
from ctypes import c_byte
from ctypes import c_ubyte

class sensor:
  DEVICE = 0x76 # Default device I2C address
  bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1
  # Rev 1 Pi uses bus 0

  def __init__(self, addr=DEVICE):
    DEVICE = addr
  
  def getShort(self, data, index):
    # return two bytes from data as a signed 16-bit value
    return c_short((data[index+1] << 8) + data[index]).value

  def getUShort(self, data, index):
    # return two bytes from data as an unsigned 16-bit value
    return (data[index+1] << 8) + data[index]

  def getChar(self, data,index):
    # return one byte from data as a signed char
    result = data[index]
    if result > 127:
      result -= 256
    return result

  def getUChar(self, data,index):
    # return one byte from data as an unsigned char
    result =  data[index] & 0xFF
    return result

  def readBME280ID(self, addr=DEVICE):
    # Chip ID Register Address
    REG_ID     = 0xD0
    (chip_id, chip_version) = self.bus.read_i2c_block_data(addr, REG_ID, 2)
    return chip_id, chip_version

  def readBME280All(self, addr=DEVICE):
    
    # Register Addresses
    REG_DATA = 0xF7
    REG_CONTROL = 0xF4
    REG_CONFIG  = 0xF5

    REG_HUM_MSB = 0xFD
    REG_HUM_LSB = 0xFE

    # Oversample setting - page 27
    OVERSAMPLE_TEMP = 2
    OVERSAMPLE_PRES = 2
    MODE = 1

    control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE
    self.bus.write_byte_data(addr, 0xF2, 1)
    
    self.bus.write_byte_data(addr, REG_CONTROL, control)
    
    # Read blocks of calibration data from EEPROM
    # See Page 22 data sheet
    cal1 = self.bus.read_i2c_block_data(addr, 0x88, 24)
    cal2 = self.bus.read_i2c_block_data(addr, 0xA1, 1)
    cal3 = self.bus.read_i2c_block_data(addr, 0xE1, 7)

    # Convert byte data to word values
    dig_T1 = self.getUShort(cal1, 0)
    dig_T2 = self.getShort(cal1, 2)
    dig_T3 = self.getShort(cal1, 4)
    
    dig_P1 = self.getUShort(cal1, 6)
    dig_P2 = self.getShort(cal1, 8)
    dig_P3 = self.getShort(cal1, 10)
    dig_P4 = self.getShort(cal1, 12)
    dig_P5 = self.getShort(cal1, 14)
    dig_P6 = self.getShort(cal1, 16)
    dig_P7 = self.getShort(cal1, 18)
    dig_P8 = self.getShort(cal1, 20)
    dig_P9 = self.getShort(cal1, 22)
    
    dig_H1 = self.getUChar(cal2, 0)
    dig_H2 = self.getShort(cal3, 0)
    dig_H3 = self.getUChar(cal3, 2)
    
    dig_H4 = self.getChar(cal3, 3)
    dig_H4 = (dig_H4 << 24) >> 20
    dig_H4 = dig_H4 | (self.getChar(cal3, 4) & 0x0F)
    
    dig_H5 = self.getChar(cal3, 5)
    dig_H5 = (dig_H5 << 24) >> 20
    dig_H5 = dig_H5 | (self.getUChar(cal3, 4) >> 4 & 0x0F)
    
    dig_H6 = self.getChar(cal3, 6)
    
    # Read temperature/pressure/humidity
    data = self.bus.read_i2c_block_data(addr, REG_DATA, 8)
    pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
    temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
    hum_raw = (data[6] << 8) | data[7]
    
    #Refine temperature
    var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11
    var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14
    t_fine = var1+var2
    temperature = float(((t_fine * 5) + 128) >> 8);
    
    # Refine pressure and adjust for temperature
    var1 = t_fine / 2.0 - 64000.0
    var2 = var1 * var1 * dig_P6 / 32768.0
    var2 = var2 + var1 * dig_P5 * 2.0
    var2 = var2 / 4.0 + dig_P4 * 65536.0
    var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0
    var1 = (1.0 + var1 / 32768.0) * dig_P1
    if var1 == 0:
      pressure=0
    else:
      pressure = 1048576.0 - pres_raw
      pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1
      var1 = dig_P9 * pressure * pressure / 2147483648.0
      var2 = pressure * dig_P8 / 32768.0
      pressure = pressure + (var1 + var2 + dig_P7) / 16.0

    # Refine humidity
    humidity = t_fine - 76800.0
    humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.8 * humidity)) * \
               (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * \
                                  (1.0 + dig_H3 / 67108864.0 * humidity)))
    humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0)
    if humidity > 100:
      humidity = 100
    elif humidity < 0:
      humidity = 0

    return temperature/100.0,pressure/100.0,humidity

  def getMeasurement(self):
    resp = {}
    temperature,pressure,humidity = self.readBME280All()
    resp = { 'tempf': (temperature * 1.8) + 32,
             'humidity': humidity,
             'baromin': pressure* 0.029529988 }
    
    return resp
    
def main():
  snsr = sensor()
  
  chip_id, chip_version = snsr.readBME280ID()
  print "Chip ID     :", chip_id
  print "Version     :", chip_version

  temperature,pressure,humidity = snsr.readBME280All()

  print temperature,  pressure, humidity
  print "Temperature : ", temperature, "C, ", (temperature * 1.8) + 32, "F"
  print "Pressure : ", pressure, "hPa"
  print "Humidity : ", humidity, "%"
  print snsr.getMeasurement()

if __name__=="__main__":
  main()

{'fraction_non_alphanumeric': 0.13544721407624633, 'fraction_numerical': 0.08009530791788856, 'mean_word_length': 2.617169373549884, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 57, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3470937', 'n_tokens_mistral': 4806, 'n_tokens_neox': 4225, 'n_words': 1023}