<s> import logging <EOL> from datetime import datetime <EOL> from Adafruit_I2C import Adafruit_I2C <EOL> class GPS_NavigatronSensor : <EOL> """<STR_LIT>""" <EOL> I2C_GPS_STATUS = <NUM_LIT:0> <EOL> I2C_GPS_NEW_DATA = <NUM_LIT> <EOL> I2C_GPS_2D_FIX = <NUM_LIT> <EOL> I2C_GPS_3D_FIX = <NUM_LIT> <EOL> I2C_GPS_NUM_SATS = <NUM_LIT> <EOL> I2C_GPS_COMMAND = <NUM_LIT:1> <EOL> I2C_GPS_WP_REG = <NUM_LIT:2> <EOL> I2C_GPS_REG_VERSION = <NUM_LIT:3> <EOL> I2C_GPS_LOCATION = <NUM_LIT:7> <EOL> I2C_GPS_NAV_LAT = <NUM_LIT:15> <EOL> I2C_GPS_NAV_LON = <NUM_LIT> <EOL> I2C_GPS_GROUND_SPEED = <NUM_LIT> <EOL> I2C_GPS_ALTITUDE = <NUM_LIT> <EOL> I2C_GPS_GROUND_COURSE = <NUM_LIT> <EOL> I2C_GPS_TIME = <NUM_LIT> <EOL> def __init__ ( self , address = <NUM_LIT> , interface = "<STR_LIT>" , hw_interface = "<STR_LIT>" , debug = False ) : <EOL> if hw_interface == "<STR_LIT>" : <EOL> self . i2c = Adafruit_I2C ( address , debug = debug ) <EOL> else : <EOL> self . i2c = Adafruit_I2C ( address , busnum = int ( hw_interface ) , debug = debug ) <EOL> self . address = address <EOL> self . debug = debug <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> self . version = self . i2c . readU8 ( self . I2C_GPS_REG_VERSION ) <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" , self . version ) <EOL> self . _read_errors = <NUM_LIT:100> <EOL> self . _last_lat = <NUM_LIT:0.0> <EOL> self . _last_lon = <NUM_LIT:0.0> <EOL> self . _last_heading = <NUM_LIT:0.0> <EOL> self . _last_speed = <NUM_LIT:0.0> <EOL> self . _last_altitude = <NUM_LIT:0.0> <EOL> def read_sensor ( self ) : <EOL> """<STR_LIT>""" <EOL> read_errors = <NUM_LIT:0> <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> status = self . i2c . readU8 ( self . I2C_GPS_STATUS ) <EOL> if status == - <NUM_LIT:1> : <EOL> fix = <NUM_LIT:0> <EOL> num_sat = <NUM_LIT:0> <EOL> read_errors += <NUM_LIT:10> <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> else : <EOL> new_data = status & <NUM_LIT> <EOL> fix_2d = ( status & <NUM_LIT> ) >> <NUM_LIT:1> <EOL> fix_3d = ( status & <NUM_LIT> ) >> <NUM_LIT:2> <EOL> fix = fix_2d or fix_3d <EOL> num_sat = status >> <NUM_LIT:4> <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" , hex ( status ) , fix , num_sat ) <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> loc_buffer = self . i2c . readList ( self . I2C_GPS_LOCATION , <NUM_LIT:8> ) <EOL> if loc_buffer == - <NUM_LIT:1> : <EOL> ( lat , lon ) = ( self . _last_lat , self . _last_lon ) <EOL> read_errors += <NUM_LIT:10> <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> else : <EOL> ( lat , lon ) = self . convert_buffer ( loc_buffer ) <EOL> ( self . _last_lat , self . _last_lon ) = ( lat , lon ) <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" , lat , lon ) <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> speed = self . i2c . readU16 ( self . I2C_GPS_GROUND_SPEED ) / <NUM_LIT> <EOL> altitude = self . i2c . readU16 ( self . I2C_GPS_ALTITUDE ) <EOL> heading = self . i2c . readU16 ( self . I2C_GPS_GROUND_COURSE ) <EOL> if self . debug : <EOL> logging . debug ( "<STR_LIT>" , speed , altitude , heading ) <EOL> dt = datetime . today ( ) <EOL> timestamp = dt . time ( ) <EOL> datestamp = dt . date ( ) <EOL> if read_errors == <NUM_LIT:0> : <EOL> self . _read_errors = <NUM_LIT:0> <EOL> else : <EOL> self . _read_errors += read_errors <EOL> return fix , lat , lon , heading , speed , altitude , num_sat , timestamp , datestamp <EOL> def convert_buffer ( self , loc_buffer ) : <EOL> ulat = int ( ( loc_buffer [ <NUM_LIT:3> ] << <NUM_LIT> ) | ( loc_buffer [ <NUM_LIT:2> ] << <NUM_LIT:16> ) | ( loc_buffer [ <NUM_LIT:1> ] << <NUM_LIT:8> ) | ( loc_buffer [ <NUM_LIT:0> ] ) ) <EOL> ulon = int ( ( loc_buffer [ <NUM_LIT:7> ] << <NUM_LIT> ) | ( loc_buffer [ <NUM_LIT:6> ] << <NUM_LIT:16> ) | ( loc_buffer [ <NUM_LIT:5> ] << <NUM_LIT:8> ) | ( loc_buffer [ <NUM_LIT:4> ] ) ) <EOL> lat = self . convert_u_l ( ulat ) / <NUM_LIT> <EOL> lon = self . convert_u_l ( ulon ) / <NUM_LIT> <EOL> return lat , lon <EOL> def convert_u_l ( self , value_in ) : <EOL> if value_in >> <NUM_LIT> : <EOL> return float ( int ( '<STR_LIT>' + '<STR_LIT>' . join ( '<STR_LIT:1>' if c == '<STR_LIT:0>' else '<STR_LIT:0>' for c in bin ( value_in - <NUM_LIT:1> ) . lstrip ( '<STR_LIT>' ) ) , <NUM_LIT:2> ) ) * - <NUM_LIT:1.0> <EOL> else : <EOL> return float ( value_in ) <EOL> def read_sensor_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . read_sensor ( ) </s>
