Getting Started
Access complete code examples and documentation from our github repo.
1.Clone or download the repository from GitHub
2.Follow installation instructions in the README.md
3.Run example files to test your NSP32m connection
4.Integrate the library into your own project
Python SDK
Cross-platform Python SDK for desktop and embedded systems
Python
Installation & Setup
# Install NSP32m Python library
pip install nsp32m
# Import and use
import nsp32m
import serial
# Connect to NSP32m via serial
spec = nsp32m.NSP32mSerial('/dev/ttyUSB0', 115200)
spectrum = spec.measure_spectrum(integration_time=128)
print(f"Captured {len(spectrum)} spectral points")Complete Example
#!/usr/bin/env python3
# Python serial communication with NSP32m
import nsp32m
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
class SpectrumAnalyzer:
def __init__(self, port='/dev/ttyUSB0'):
self.spec = nsp32m.NSP32mSerial(port, 115200)
def capture_and_analyze(self, integration_time=128):
"""Capture spectrum and perform basic analysis"""
spectrum = self.spec.measure_spectrum(integration_time)
if spectrum is None:
print("Failed to capture spectrum")
return None
wavelengths = self.spec.get_wavelengths()
# Find peak wavelength
peak_idx = np.argmax(spectrum)
peak_wavelength = wavelengths[peak_idx]
peak_intensity = spectrum[peak_idx]
print(f"Peak: {peak_wavelength:.1f}nm at intensity {peak_intensity}")
# Save data
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"spectrum_{timestamp}.csv"
self.spec.save_spectrum(spectrum, filename)
return {
'wavelengths': wavelengths,
'spectrum': spectrum,
'peak_wavelength': peak_wavelength,
'peak_intensity': peak_intensity
}
def plot_spectrum(self, data):
"""Plot spectrum data"""
plt.figure(figsize=(12, 8))
plt.plot(data['wavelengths'], data['spectrum'], 'b-', linewidth=1)
plt.axvline(data['peak_wavelength'], color='r', linestyle='--',
label=f"Peak: {data['peak_wavelength']:.1f}nm")
plt.xlabel('Wavelength (nm)')
plt.ylabel('Intensity')
plt.title('NSP32m Spectrum Analysis')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# Usage example
if __name__ == "__main__":
analyzer = SpectrumAnalyzer('/dev/ttyUSB0')
while True:
try:
data = analyzer.capture_and_analyze(128)
if data:
analyzer.plot_spectrum(data)
input("Press Enter for next measurement (Ctrl+C to exit)...")
except KeyboardInterrupt:
print("\nExiting...")
break