-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
165 lines (131 loc) · 4.72 KB
/
stack.py
File metadata and controls
165 lines (131 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from threading import Thread, Lock
import uuid
import time
import numpy as np
from scipy.io import wavfile
import matplotlib.pyplot as plt
import sounddevice as sd
from scipy.signal import lfilter
from phy import PhyLayer
from lnk import LnkLayer
from net import NetLayer
from frame import Frame
from packet import Packet
from util import bytes_to_str
class Stack:
def __init__(self):
# Layer setup
self.phy = PhyLayer()
self.lnk = LnkLayer()
self.net = NetLayer()
# Data packet buffers, for communication with application
self.rx_packets_mutex = Lock()
self.tx_packets_mutex = Lock()
self.rx_packets = []
self.tx_packets = []
# Audio sample buffers, for communication with audio device
self.rx_samples_mutex = Lock()
self.tx_samples_mutex = Lock()
self.rx_samples = np.array([])
self.tx_samples = np.array([])
# Worker threads, one per buffer
self.rx_thread = Thread(target=self.__rx)
self.tx_thread = Thread(target=self.__tx)
self.play_thread = Thread(target=self.__play)
self.rec_thread = Thread(target=self.__rec)
self.history = np.array([])
def __rec(self):
def callback(indata, frames, time, status):
if any(indata):
self.rx_samples_mutex.acquire()
self.rx_samples = np.append(self.rx_samples, indata)
self.rx_samples_mutex.release()
fs = self.phy.freq_sample
with sd.InputStream(channels=1, callback=callback, blocksize=int(fs/10), samplerate=fs, dtype='float32'):
while True:
pass
def __play(self):
while True:
self.tx_samples_mutex.acquire()
samples = np.asarray(np.asarray(self.tx_samples, dtype='float32'))
self.tx_samples = np.array(())
self.tx_samples_mutex.release()
if len(samples) > 0:
sd.play(samples, self.phy.freq_sample, blocking=True)
else:
time.sleep(0.1)
def __rx(self):
while True:
window_size = int(self.phy.freq_sample / 10)
self.rx_samples_mutex.acquire()
if window_size > len(self.rx_samples):
window_size = len(self.rx_samples)
samples = self.rx_samples[:window_size]
self.rx_samples = self.rx_samples[window_size:]
self.rx_samples_mutex.release()
if any(samples):
self.history = np.append(self.history, samples)
bits = self.phy.rx(samples)
frames = self.lnk.rx(bits)
packets = self.net.rx(frames)
for pkt in packets:
string = bytes_to_str(pkt.data)
print('* received: ' + string)
self.rx_packets_mutex.acquire()
self.rx_packets += packets
self.rx_packets_mutex.release()
time.sleep(0.01)
def __tx(self):
while True:
pkt = None
self.tx_packets_mutex.acquire()
if len(self.tx_packets) > 0:
pkt = self.tx_packets[0]
self.tx_packets = self.tx_packets[1:]
self.tx_packets_mutex.release()
frames = self.net.tx(pkt)
bits = self.lnk.tx(frames)
samples = self.phy.tx(bits)
if len(samples) > 0:
self.tx_samples_mutex.acquire()
self.tx_samples = np.append(self.tx_samples, samples)
self.tx_samples_mutex.release()
time.sleep(0.01)
def start(self):
self.rx_thread.start()
self.tx_thread.start()
self.rec_thread.start()
self.play_thread.start()
def send(self, string, receiver):
data = [ord(char) for char in string]
pkt = Packet(data=data, receiver=receiver)
self.tx_packets_mutex.acquire()
self.tx_packets.append(pkt)
self.tx_packets_mutex.release()
def recv(self):
while True:
packet = None
self.rx_packets_mutex.acquire()
if len(self.rx_packets) > 0:
packet = self.rx_packets[0]
self.rx_packets = self.rx_packets[1:]
self.rx_packets_mutex.release()
if packet is not None:
return packet
time.sleep(0.1)
if __name__ == '__main__':
me = uuid.getnode()
stack = Stack()
stack.start()
stack.send('Hello, world!', me)
time.sleep(10)
f0 = stack.phy.filter_freq_0
y0 = lfilter(f0[0], f0[1], stack.history)
f1 = stack.phy.filter_freq_1
y1 = lfilter(f1[0], f1[1], stack.history)
plt.figure()
plt.plot(stack.history)
plt.plot(y0)
plt.plot(y1)
plt.draw()
plt.show()