<commit_before>#
# StSimpleApi.py
#
# functions to enable simple value fetching from the sensortag
# 

# import stuff
import os, sys, pexpect
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir) 
import sensortag
from sensor_calcs import *

# SensorTag class
# from 
#    https://github.com/msaunby/ble-sensor-pi/blob/master/sensortag/sensortag.py
class SensorTag:

	def __init__( self, bluetooth_adr ):
		self.con = pexpect.spawn('gatttool -b ' + bluetooth_adr + ' --interactive')
		self.con.expect('\[LE\]>', timeout=600)
		print >> sys.stderr, "Preparing to connect. You might need to press the side button..."
		self.con.sendline('connect')
		# test for success of connect
		self.con.expect('Connection successful.*\[LE\]>')
		# Earlier versions of gatttool returned a different message.  Use this pattern -
		#self.con.expect('\[CON\].*>')
		self.cb = {}
		return

        # legacy stuff
		#self.con.expect('\[CON\].*>')
		#self.cb = {}
		#return

	def char_write_cmd( self, handle, value ):
		# The 0%x for value is VERY naughty!  Fix this!
		cmd = 'char-write-cmd 0x%02x 0%x' % (handle, value)
		print >> sys.stderr, "Sending %s"%cmd
		self.con.sendline( cmd )
		return

	def char_read_hnd( self, handle ):
		self.con.sendline('char-read-hnd 0x%02x' % handle)
		self.con.expect('descriptor: .*? \r')
		after = self.con.after
		rval = after.split()[1:]
		print >> sys.stderr, "Received %s"%rval
		return [long(float.fromhex(n)) for n in rval]
        
    # deletes stuff related to notification

def getHum( ST ):
    ''' uses the Sensortag object to read, process and return the humidity
    '''
    # enable humidity
    ST.char_write_cmd(0x3c,0x01)
    
    # read the value
    v = ST.char_read_hnd( 0x38 )

    # copied from
    #    https://github.com/msaunby/ble-sensor-pi/blob/master/sensortag/sensortag.py
    # line 118
    rawT = (v[1]<<8)+v[0]
    rawH = (v[3]<<8)+v[2]
    (t, rh) = calcHum(rawT, rawH)
    print >> sys.stderr, "HUMD %.1f TEMP %.1f" % (rh, t)
    
    return (t, rh)
    
# and a test run main
if __name__ == "__main__":
    if len( sys.argv ) < 2:
        print "usage: %s <Sensortag MAC>"%sys.argv[0]
        exit(1)
    else:
        StMac = sys.argv[1]
        
    print "Reading data from sensortag with MAC: %s"%StMac

    ST = SensorTag( StMac )
    print getHum( ST )<commit_msg>Maliks addition<commit_after>#
# StSimpleApi.py
#
# functions to enable simple value fetching from the sensortag
# 

# import stuff
import os, sys, pexpect
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir) 
import sensortag
from sensor_calcs import *

# SensorTag class
# from 
#    https://github.com/msaunby/ble-sensor-pi/blob/master/sensortag/sensortag.py
class SensorTag:

	def __init__( self, bluetooth_adr ):
		self.con = pexpect.spawn('gatttool -b ' + bluetooth_adr + ' --interactive')
		self.con.expect('\[LE\]>', timeout=600)
		print >> sys.stderr, "Preparing to connect. You might need to press the side button..."
		self.con.sendline('connect')
		# test for success of connect
		self.con.expect('Connection successful.*\[LE\]>')
		# Earlier versions of gatttool returned a different message.  Use this pattern -
		#self.con.expect('\[CON\].*>')
		self.cb = {}
		return

        # legacy stuff
		#self.con.expect('\[CON\].*>')
		#self.cb = {}
		#return

	def char_write_cmd( self, handle, value ):
		# The 0%x for value is VERY naughty!  Fix this!
		cmd = 'char-write-cmd 0x%02x 0%x' % (handle, value)
		print >> sys.stderr, "Sending %s"%cmd
		self.con.sendline( cmd )
		return

	def char_read_hnd( self, handle ):
		self.con.sendline('char-read-hnd 0x%02x' % handle)
		self.con.expect('descriptor: .*? \r')
		after = self.con.after
		rval = after.split()[1:]
		print >> sys.stderr, "Received %s"%rval
		return [long(float.fromhex(n)) for n in rval]
        
    # deletes stuff related to notification

def getHum( ST ):
    ''' uses the Sensortag object to read, process and return the humidity
    '''
    # enable humidity
    ST.char_write_cmd(0x3c,0x01)
    
    # read the value
    v = ST.char_read_hnd( 0x38 )

    # copied from
    #    https://github.com/msaunby/ble-sensor-pi/blob/master/sensortag/sensortag.py
    # line 118
    rawT = (v[1]<<8)+v[0]
    rawH = (v[3]<<8)+v[2]
    (t, rh) = calcHum(rawT, rawH)
    print >> sys.stderr, "HUMD %.1f TEMP %.1f" % (rh, t)
    
    return (t, rh)

def getAcc( ST ):

    ST.char_write_cmd(0x31, 0x01)
    v = ST.char_read_hnd( 0x2d )
    
    (xyz,mag) = calcAccel(v[0],v[1],v[2])
    self.data['accl'] = xyz
    return xyz

def getTemp( ST ):

    ST.char_write_cmd(0x29, 0x01)
    v = ST.char_read_hnd( 0x25 )
    
    objT = (v[1]<<8)+v[0]
    ambT = (v[3]<<8)+v[2]
    targetT = calcTmpTarget(objT, ambT)
    self.data['t006'] = targetT
    return targetT

def getMag( ST ):
    
    ST.char_write_cmd(0x44, 0x01)
    v = ST.char_read_hnd( 0x40 )
    x = (v[1]<<8)+v[0]
    y = (v[3]<<8)+v[2]
    z = (v[5]<<8)+v[4]
    xyz = calcMagn(x, y, z)
    self.data['magn'] = xyz
    return xyz

def getGyro( ST ):
    
    ST.char_write_cmd(0x5b, 0x07)
    v = ST.char_read_hnd( 0x57 )
    return v

def getBaro( ST ):

    ST.char_write_cmd(0x4f, 0x01)
    v = ST.char_read_hnd( 0x4b )
    rawT = (v[1]<<8)+v[0]
    rawP = (v[3]<<8)+v[2]
    (temp, pres) =  self.data['baro'] = barometer.calc(rawT, rawP)
    return temp, pres
    #print "BARO", temp, pres
    self.data['time'] = long(time.time() * 1000);
    # The socket or output file might not be writeable
    # check with select so we don't block.
    (re,wr,ex) = select.select([],[datalog],[],0)
    if len(wr) > 0:
        datalog.write(json.dumps(self.data) + "\n")
        datalog.flush()
        pass
    
# and a test run main
if __name__ == "__main__":
    if len( sys.argv ) < 2:
        print "usage: %s <Sensortag MAC>"%sys.argv[0]
        exit(1)
    else:
        StMac = sys.argv[1]
        
    print "Reading data from sensortag with MAC: %s"%StMac

    ST = SensorTag( StMac )
    print getHum( ST )
	print getAcc( ST )
	print getBaro( ST )
	print getMag( ST )
	print getGyro( ST )
	print getTemp( ST )
{'fraction_non_alphanumeric': 0.15456902138690862, 'fraction_numerical': 0.023655217109526897, 'mean_word_length': 2.91191381495564, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '753231', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2405, 'n_words': 571}