Skip to content
Snippets Groups Projects
Commit 0a215e11 authored by fxk8y's avatar fxk8y :spider:
Browse files

Implementing low-level protocol

parent 7983c5d7
No related branches found
No related tags found
No related merge requests found
class QthingDevice:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
print('Setting name to ' + value)
return AttributeError
class FxCyanFDevice(QthingDevice):
pass
class SiliconTorch:
def __init__(self):
self._devices = {}
#!/usr/bin/env nix-shell
#!nix-shell -i python -p python3
import sys
import time
import struct
from socket import *
......@@ -19,6 +20,50 @@ def f2b(f):
return bytearray(struct.pack("f", f))
def send(x):
data = f2b(x)
for _ in range(5):
data += f2b(0.0)
data = bytearray(b'fxCyanF') + data
for host in hosts:
try:
s.sendto(data, (host, port))
except:
pass
t = 2 * 60**2 # seconds
countDown = True
bits = 10
step = 2**bits / t / 10 / 2**bits
print('step: ' + str(step))
x = 1.0
i = 0
while x > 0.0:
if countDown:
send(x)
else:
send(1.0 - x)
i += 1
x -= step
time.sleep(0.1)
print('i: ' + str(i))
send(1.0 - x) #REMOVE
sys.exit(0)
###########
x = 0.0
goUp = True
......@@ -29,11 +74,11 @@ while True:
else:
x -= step
if x > 1:
if x > 1.0:
x = 1.0
goUp = not goUp
if x < 0:
if x < 0.0:
x = 0.0
goUp = not goUp
......
import struct
from threading import Thread
from socket import socket, AF_INET, SOCK_DGRAM
class DEFAULT:
port = 4213
header = b'fxCyanF'
listenAddr = '0.0.0.0'
class Sender:
_socket = socket(AF_INET, SOCK_DGRAM)
def __init__(self, host: str, port: int = DEFAULT.port):
self._host = host
self._port = port
@staticmethod
def _convertData(data):
channels = []
for item in data:
f = float(item)
if f > 1.0: f = 1.0
if f < 0.0: f = 0.0
channels += [f]
return struct.pack('f' * len(channels), *channels)
@classmethod
def _sendUDP(cls, data, host, port):
try:
cls._socket.sendto(data, (host, port))
except:
# TODO: logging…?
pass
@classmethod
def sendTo(cls, channels, host, port):
data = DEFAULT.header
data += Sender._convertData(channels)
cls._sendUDP(data, host, port)
def send(self, channels):
Sender.sendTo(channels, self._host, self._port)
class Receiver:
def __init__(self, host: str = DEFAULT.listenAddr, port: int = DEFAULT.port):
self._socket = socket(AF_INET, SOCK_DGRAM)
self._socket.bind((host, port))
self._callbacks = Set()
t = Thread(target=self._recvLoop)
t.daemon = True
t.start()
def _recvLoop(self):
header = DEFAULT.header
headerLen = len(header)
while True:
data = self._socket.recv(2048)
if data.startswith(header):
data = data[ headerLen : ]
else:
continue
if len(data) % 4 != 0:
# TODO: logging…?
continue
_chs = struct.unpack('f' * len(data), data)
channels = []
for ch in _chs:
if ch > 1.0: ch = 1.0
if ch < 0.0: ch = 0.0
channels += [ch]
for callback in self._callbacks:
try:
callback(channels)
except:
# TODO: logging…?
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment