Initial commit
This commit is contained in:
0
tcd1304_viewer/__init__.py
Normal file
0
tcd1304_viewer/__init__.py
Normal file
47
tcd1304_viewer/__main__.py
Normal file
47
tcd1304_viewer/__main__.py
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import numpy as np
|
||||
import serial
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from tcd1304_viewer.viewer import Viewer
|
||||
|
||||
viewer = Viewer()
|
||||
cfg = [1680, 8400000]
|
||||
|
||||
if len(sys.argv) > 2:
|
||||
cfg = [int(i) for i in sys.argv[1:]]
|
||||
|
||||
msg = struct.pack("<HBII", 0x1304, 0, *cfg)
|
||||
|
||||
viewer.show()
|
||||
|
||||
# Open the serial port
|
||||
ser = serial.Serial('/dev/serial0', baudrate=921600)
|
||||
|
||||
ser.write(msg)
|
||||
|
||||
# Now read and print data from the serial port forever
|
||||
while True:
|
||||
m = ser.read(1)[0] << 8
|
||||
|
||||
while m != 0x1304:
|
||||
m >>= 8
|
||||
m |= ser.read(1)[0] << 8
|
||||
|
||||
t, l = struct.unpack("<BI", ser.read(5))
|
||||
c = ser.read(l)
|
||||
print(f"msg t={t:#02x}, l={l}")
|
||||
|
||||
if t == 0x00:
|
||||
print(f"content {c}")
|
||||
if t == 0x01:
|
||||
data = np.array()
|
||||
|
||||
while len(c):
|
||||
d, c = c[:2], c[2:]
|
||||
(r, ) = struct.unpack("<H", d)
|
||||
data.append(r * 3.3 / 4096)
|
||||
|
||||
viewer.update_data(data)
|
||||
12
tcd1304_viewer/serial.py
Normal file
12
tcd1304_viewer/serial.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import serial
|
||||
|
||||
|
||||
class SerialHandler:
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.ser = serial.Serial()
|
||||
self.ser.baudrate = 921600
|
||||
self.ser.port = "/dev/serial0"
|
||||
|
||||
def start(self) -> None:
|
||||
self.ser.open()
|
||||
19
tcd1304_viewer/viewer.py
Normal file
19
tcd1304_viewer/viewer.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
class Viewer:
|
||||
|
||||
def update_data(self, data: np.array) -> None:
|
||||
plt.clf()
|
||||
|
||||
plt.xlabel("Pixel")
|
||||
plt.ylabel("Tension (V)")
|
||||
plt.legend()
|
||||
|
||||
x = np.arange(1, len(data) + 1)
|
||||
|
||||
plt.plot(x, data, label="Intensity")
|
||||
|
||||
def show(self) -> None:
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user