#!/usr/bin/env python3 """ Quick SPI test for basic functionality verification """ import spidev import time import sys import json def quick_spi_test(): """Quick test to verify SPI communication""" print("Quick SPI Test") print("=" * 30) try: # Initialize SPI spi = spidev.SpiDev() spi.open(0, 0) # Bus 0, Device 0 spi.max_speed_hz = 1000000 # 1MHz spi.mode = 0 # SPI Mode 0 spi.bits_per_word = 8 print("SPI initialized successfully") print(f"Mode: {spi.mode}, Speed: {spi.max_speed_hz}Hz") # Test 1: Simple byte print("\nTest 1: Single byte (0xAA)") response = spi.xfer2([0xAA]) print(f"Sent: [0xAA], Received: {[hex(b) for b in response]}") # Test 2: Multiple bytes print("\nTest 2: Multiple bytes") test_data = [0x01, 0x02, 0x03, 0x04] response = spi.xfer2(test_data) print(f"Sent: {[hex(b) for b in test_data]}") print(f"Received: {[hex(b) for b in response]}") # Test 3: Hello message print("\nTest 3: Hello message") hello = [0x48, 0x65, 0x6C, 0x6C, 0x6F] # "Hello" response = spi.xfer2(hello) print(f"Sent: {[hex(b) for b in hello]} ({''.join([chr(b) for b in hello])})") print(f"Received: {[hex(b) for b in response]}") # Test 4: JSON message (led-bar format) print("\nTest 4: JSON message (led-bar format)") payload = {"settings": {"pattern": "off", "brightness": 128}, "save": False} json_bytes = list(json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8")) response = spi.xfer2(json_bytes) print(f"Sent JSON: {json.dumps(payload)}") print(f"Received: {[hex(b) for b in response]}") spi.close() print("\nTest completed successfully!") return True except Exception as e: print(f"Test failed: {e}") return False if __name__ == "__main__": success = quick_spi_test() sys.exit(0 if success else 1)