Browse Source

cleaner Version of the same code lol

Adrian 6 months ago
parent
commit
08877a2960
4 changed files with 86 additions and 9 deletions
  1. 2 2
      analysing/test.py
  2. 77 0
      libs/DigitalStethoskop/__init__.py
  3. 6 6
      libs/MAX4466/__init__.py
  4. 1 1
      libs/__init__.py

+ 2 - 2
analysing/test.py

@@ -5,7 +5,7 @@ import numpy as np
 import matplotlib.pyplot as plt
 from scipy.signal import find_peaks
 
-sample_rate = 800
+sample_rate = 1024
 
 def plot_spectrogram_with_peak_spectrum(column1_data, column2_data, fs):
     """
@@ -80,7 +80,7 @@ time = []
 data = []
 data2 = []
 # Open the CSV file for reading
-with open('analysing/data_good.csv', 'r') as file:
+with open('analysing/data.csv', 'r') as file:
     # Create a CSV reader object
     csv_reader = csv.reader(file)
     

+ 77 - 0
libs/DigitalStethoskop/__init__.py

@@ -0,0 +1,77 @@
+"""
+    Library für die Nutzung des MAX4466 Mikrofons als digitales Stethoskop
+
+    Author: Adrian Böschel
+    letzte Änderung: 18.04.2024
+
+    threads
+    https://www.electrosoftcloud.com/en/multithreaded-script-on-raspberry-pi-pico-and-micropython/
+    https://docs.python.org/3.5/library/_thread.html#module-_thread
+"""
+
+from machine import Pin, Timer, ADC
+
+class DigitalStethoskop:
+    
+    ##############################     INITIALISIERUNG    ##############################
+    def __init__(self, SAMPLE_RATE:int, SIGNAL_PIN:Pin = Pin(26), MAX_DATA:int = 2048):
+        # Variables
+        self.SR = SAMPLE_RATE
+        self.count = 0
+        
+        # Pins
+        self.__SIGNAL_PIN = self.check_Pin(SIGNAL_PIN)
+
+        # ADC
+        self.adc = ADC(self.__SIGNAL_PIN) # 0 - 65535
+        self.__deltaU = 3.3/65536
+
+        # TIMER
+        self.__timer = Timer()
+
+        # DATA FILE
+        self.f = open('data.txt') # touch the file to create it
+        self.f.close()
+
+
+    ##############################        CHECKS          ##############################
+    def check_Pin(self, Pin):
+        return Pin
+
+
+    ##############################         ISRS           ##############################
+    def __isr(self, timer):
+        self.f.write(self.adc.value() * self.__deltaU)
+        self.count += 1
+
+    ##############################        METHODEN        ##############################
+    def Start(self):
+        self.__timer.init(mode=Timer.PERIODIC, freq=self.SR, callback=self.__isr)
+
+    def Stop(self):
+        self.__timer.deinit()
+
+    def record_data(self, time_seconds):
+        self.f = open("data.txt", 'w')
+
+        max = time_seconds * self.SR
+
+        # start recording
+        self.Start()
+
+        while self.count < max:
+            pass
+
+        # end recording
+        self.Stop()
+        self.f.close()
+
+    def get_data(self):
+        pass
+
+    def save_data(self, path):
+        pass
+
+    def reset(self):
+        self.__timer.deinit()
+        self.count = 0

+ 6 - 6
libs/MAX4466/__init__.py

@@ -29,7 +29,7 @@ class Stethoskop:
     """
 
     ##############################             Initialisierung    ##############################
-    def __init__(self, SAMPLE_RATE:int, SIGNAL_PIN:Pin = Pin(26), GROVE_CONNECTOR_NR:int = 0, MAX_DATA:int = 8000):
+    def __init__(self, SAMPLE_RATE:int, SIGNAL_PIN:Pin = Pin(26), GROVE_CONNECTOR_NR:int = 0, MAX_DATA:int = 2048):
         """_summary_
 
         Args:
@@ -287,19 +287,19 @@ class Stethoskop:
         # append the data to .bin file
         self.f.write(v)
 
-    def record_data(self, time_in_s):
+    def record_data(self, time_seconds):
         # start Timer
-        max = time_in_s * self.SAMPLE_RATE # calculates how many data points have to be recorded
+        max = time_seconds * self.SAMPLE_RATE # calculates how many data points have to be recorded
         # create the file and erase previous data
         self.f = open("data.bin", mode='wb')
 
         self.__timer_isr.init(mode=Timer.PERIODIC, freq=self.SAMPLE_RATE, callback=self.__isr)
-        
+
         while self.counter < max:
             pass
 
         self.f.close()
-        
+
         self.__timer_isr.deinit()
 
     ## timer neu
@@ -436,7 +436,7 @@ class Stethoskop:
             if self.__lock.locked(): self.__lock.release()
             print(f"Lock1: {self.__lock.locked()}")
 
-    def GetData(self):
+    def get_data(self):
         """Returns all available Data depending if there is data available
         Else it will return an empty array
 

+ 1 - 1
libs/__init__.py

@@ -1,2 +1,2 @@
-from libs import foo, MAX4466
+from libs import foo, MAX4466, DigitalStethoskop