Skip to content

Instantly share code, notes, and snippets.

@zenom
Created December 18, 2023 00:54
Show Gist options
  • Save zenom/b0d6c5fb9e5d4235b67bdb30e24f7be8 to your computer and use it in GitHub Desktop.
Save zenom/b0d6c5fb9e5d4235b67bdb30e24f7be8 to your computer and use it in GitHub Desktop.
My uart test.
odule RadioListener do
# use Circuits.UART
# when you use circuits.uart.enumerate there is an id
# which we could map to which module we want to use.
@serial_port "/dev/cu.usbserial-2210" # e.g., "/dev/ttyUSB0"
@baud_rate 115200
@ic7300_address "94" # Hexadecimal address of IC-7300
@controller_address "E0" # Hexadecimal address of the controller (PC)
def start_link do
{:ok, pid} = Circuits.UART.start_link()
case Circuits.UART.open(pid, @serial_port, speed: @baud_rate, active: false) do
:ok ->
# Port opened successfully, start listening
IO.puts("PORT OPENEND...LISTENING")
listen_for_frequency(pid)
{:error, reason} ->
# Handle error
IO.puts("Failed to open port: #{reason}")
end
end
defp listen_for_frequency(pid) do
case Circuits.UART.read(pid, 1000) do
{:ok, ""} -> listen_for_frequency(pid)
{:ok, data} ->
IO.inspect(data)
extract_frequency(data)
listen_for_frequency(pid)
{:error, reason} ->
IO.puts("Read error: #{reason}")
end
listen_for_frequency(pid)
end
defp extract_frequency(data) do
# could make this a tuple that I could then create
# parsing methods "FE FE 0 94 0 70 22 20 14 0 FD" is
# an example
# switching mode: "FE FE 0 94 1 3 2 FD"
# this means 1: mode switch, 3: to CW, 2: filter 2
testing = :erlang.binary_to_list(data)
|> Enum.map(fn x -> Integer.to_string(x, 16) end)
determine_mode(testing)
# IO.inspect(testing)
# Implement CI-V data parsing to extract frequency
end
def determine_mode(["FE", "FE", "0", "94", "1", operating_mode, filter, "FD"] = data) do
modes = %{
0 => "LSB",
1 => "USB",
2 => "AM",
3 => "CW",
4 => "RTTY",
5 => "FM",
7 => "CW-R",
8 => "RTTY_R"
}
IO.puts("#{modes[operating_mode]} Filter: #{filter}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment