From 42a82b6f5d9632207a0ae1ba945da693c802254d Mon Sep 17 00:00:00 2001 From: superbeelavaman <70459143+superbeelavaman@users.noreply.github.com> Date: Mon, 20 May 2024 14:59:37 -0700 Subject: [PATCH] Upload main.py --- main.py | 332 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 332 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..508cc6e --- /dev/null +++ b/main.py @@ -0,0 +1,332 @@ +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('