add serial readout. finish graph layout

This commit is contained in:
2025-04-22 22:14:43 +02:00
parent c81b2e3fbb
commit 58ebddcd48
7 changed files with 90 additions and 39 deletions

View File

@@ -2,8 +2,8 @@ from bleak import BleakClient, BleakScanner
import pandas as pd
from tkinter.ttk import Entry
from tkinter.messagebox import showerror, showinfo
import asyncio
from time import time
from serial import Serial
from ..config import MILLIS_UUID, WEIGHT_UUID
@@ -27,7 +27,12 @@ class Device:
self.weights = []
async def connect(self):
self.device = await BleakScanner.find_device_by_name(self.device_name.get())
device_name = self.device_name.get()
if device_name.startswith('/dev'):
self.device = device_name
else:
self.device = await BleakScanner.find_device_by_name(self.device_name.get())
return self.device is not None
@@ -35,25 +40,50 @@ class Device:
self.device = None
async def read_values(self, duration):
duration = int(duration)
if not await self.connect():
showerror("Record Data", f"Device {self.device_name.get()} not found!")
return
self.clear_data()
async with BleakClient(self.device.address) as client:
showinfo("Recording Data", f"Recording data for {duration} seconds.")
if isinstance(self.device, str):
self._read_values_serial(duration)
else:
await self._read_values_ble(duration)
def _read_values_serial(self, duration):
with Serial(self.device, baudrate=115200) as ser:
showinfo("Record Data", f"Recording data for {duration} seconds.")
time_start = time()
time_passed = 0
while time_passed < duration:
millis = await client.read_gatt_char(MILLIS_UUID)
millis = int.from_bytes(millis, byteorder='little') # Adjust based on your data format
weight = await client.read_gatt_char(WEIGHT_UUID)
weight = int.from_bytes(weight, byteorder='little') # Adjust based on your data format
line = ser.readline()
_timestamp, _raw_weight = line.decode('utf-8').split(',')
self.timestamps.append(millis)
self.weights.append(weight)
self.timestamps.append(int(_timestamp))
self.weights.append(int(_raw_weight))
time_passed = time() - time_start
async def _read_values_ble(self, duration):
self.clear_data()
try:
async with BleakClient(self.device.address) as client:
showinfo("Record Data", f"Recording data for {duration} seconds.")
time_start = time()
time_passed = 0
while time_passed < duration:
millis = await client.read_gatt_char(MILLIS_UUID)
millis = int.from_bytes(millis, byteorder='little') # Adjust based on your data format
weight = await client.read_gatt_char(WEIGHT_UUID)
weight = int.from_bytes(weight, byteorder='little') # Adjust based on your data format
self.timestamps.append(millis)
self.weights.append(weight)
time_passed = time() - time_start
except:
showerror("Record Data", f"Client could not be started for {self.device_name.get()}: {self.device.address}.")
def clear_data(self):

View File

@@ -3,17 +3,24 @@ from tkinter import ttk
from ..slider import Slider
from serial.tools import list_ports
class RecordForm(tk.Frame):
def __init__(self, master, record_command, **kwargs):
super().__init__(master, **kwargs)
self.device_label = ttk.Label(self, text="Device Name:")
# get serial ports
serials = [d.device for d in list_ports.grep('usbmodem')]
devices = serials + ["Smaage"]
default_record_len = 10 if len(serials) > 0 else 30
self.device_label = ttk.Label(self, text="Device:")
self.device_label.pack(pady=10)
self.device_name = ttk.Entry(self)
self.device_name.insert(0, "Smaage") # Set default value
self.device_name = ttk.Combobox(self, values=devices)
self.device_name.set(devices[0])
self.device_name.pack()
self.record_time = Slider(self, "Record Time:", 10, 30, 10, lambda: None)
self.record_time = Slider(self, "Record Time:", 10, 60, default_record_len, lambda: None)
self.record_time.pack(pady=10)
self.record_button = ttk.Button(self, text="Record Data", command=record_command)
self.record_button.pack(pady=10)