import os import struct import socket from binascii import crc32 import time import threading dataTypes = dict(version=bytes([0x00, 0x00, 0x10, 0x00]), ports=bytes([0x01, 0x00, 0x10, 0x00]), data=bytes([0x02, 0x00, 0x10, 0x00])) iioDevices = [] iioPath = "/sys/bus/iio/devices/iio:device" def getIIODevices(): # Get list of IIO devices (Gyroscope, Accelerometers, Light Sensors, Lid Angle) for i in range(0, 6): device = os.popen(f'cat {iioPath}{i}/name {iioPath}{i}/label | tr "\\n" " "').read() iioDevices.append(device [0:-1]) def readSensorValue(file): file.seek(0) value=file.read() if value=='': return 0 return int(value) def checkSensors(): # Read sensor values and save to global variables global baseGyroXMotion # todo: make it look better, store sensors in a dictionary global baseGyroYMotion global baseGyroZMotion global baseAccelXMotion global baseAccelYMotion global baseAccelZMotion global screenAccelXMotion global screenAccelYMotion global screenAccelZMotion global in_angl global in_illuminance_base global in_illuminance_display global batteryCap global batteryChrg in_angl=readSensorValue(angl) in_illuminance_base=readSensorValue(baseLight) in_illuminance_display=readSensorValue(screenLight) baseGyroXMotion = readSensorValue(baseGyroX) baseGyroYMotion = readSensorValue(baseGyroY) baseGyroZMotion = readSensorValue(baseGyroZ) baseAccelXMotion = readSensorValue(baseAccelX) baseAccelYMotion = readSensorValue(baseAccelY) baseAccelZMotion = readSensorValue(baseAccelZ) screenAccelXMotion = readSensorValue(screenAccelX) screenAccelYMotion = readSensorValue(screenAccelY) screenAccelZMotion = readSensorValue(screenAccelZ) batteryCap = readSensorValue(batteryCapacity) batteryStatus.seek(0) batteryChrg = (str(batteryStatus.read()) == "Charging\n") def finalizeMessage(messageType, data): # Attaches header and calculates CRC message=[ 0x44, 0x53, 0x55, 0x53, # DSUS 0xE9, 0x03, # protocol version 1001 *struct.pack('