Created
March 3, 2024 20:12
-
-
Save bkram/6d12c643ddcdca63d99680ac548496ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This script sets the frequency and pre-emphasis values for a device using a serial interface. | |
It takes a frequency argument in MHz and an optional pre-emphasis argument (default: 1). | |
The script sends commands to the device via the specified serial port to set the frequency | |
and pre-emphasis values. | |
Command line arguments: | |
frequency (float): Frequency in MHz. | |
--emphasis (int): Pre-emphasis value (default: 1). | |
""" | |
import argparse | |
import serial | |
# Define the command codes | |
COMMAND_SET_FREQUENCY = 0x01 | |
COMMAND_SET_PRE_EMPHASIS = 0x04 | |
# Define the serial port settings | |
BAUD_RATE = 19200 | |
SERIAL_PORT = '/dev/ttyAMA0' # Use the primary UART on GPIO pins | |
def send_command(ser_port, command, param1, param2): | |
""" | |
Sends a command to the serial port. | |
Args: | |
ser_port (serial.Serial): The serial port instance. | |
command (int): The command code. | |
param1 (int): The first parameter. | |
param2 (int): The second parameter. | |
""" | |
command_string = bytes([command, param1, param2]) | |
print(f"Sending: {command_string.hex()}") | |
ser_port.write(command_string) | |
response = ser_port.read(1) # Read the response byte | |
if response == b'\x80': | |
parameters = ser_port.read(2) | |
print("Command succeeded. Parameters:", parameters.hex()) | |
else: | |
print("Command failed.") | |
def calculate_bytes_for_frequency(frequency, ser_port): | |
""" | |
Calculates the byte values for the given frequency and sends the set frequency command. | |
Args: | |
frequency (float): The frequency in MHz. | |
ser_port (serial.Serial): The serial port instance. | |
""" | |
value = int(frequency * 10 ** 6 / 10 ** 4) | |
d0 = value // 256 | |
d1 = value % 256 | |
send_command(ser_port, COMMAND_SET_FREQUENCY, d0, d1) | |
def set_pre_emphasis(ser_port, d0): | |
""" | |
Sets the pre-emphasis value. | |
Args: | |
ser_port (serial.Serial): The serial port instance. | |
d0 (int): The pre-emphasis value. | |
""" | |
send_command(ser_port, COMMAND_SET_PRE_EMPHASIS, d0, 0x00) | |
if __name__ == "__main__": | |
# Parse command line arguments | |
parser = argparse.ArgumentParser(description='Set frequency and pre emphasis') | |
parser.add_argument('frequency', type=float, help='Frequency in MHz') | |
parser.add_argument('--emphasis', type=int, default=1, help='Pre emphasis value (default: 1)') | |
args = parser.parse_args() | |
# Open the serial port | |
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser: | |
# Calculate bytes for frequency and set frequency | |
calculate_bytes_for_frequency(args.frequency, ser) | |
# Set pre emphasis | |
set_pre_emphasis(ser, args.emphasis) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment