@@ -9,15 +9,15 @@ defmodule CircuitsSim.GPIO.GPIOServer do
99
1010 require Logger
1111
12- defstruct pin_spec : nil ,
12+ defstruct gpio_spec : nil ,
1313 device: nil ,
1414 direction: :input ,
1515 pull_mode: :none ,
1616 cached_value: 0 ,
1717 interrupt_receiver: nil ,
1818 interrupt_trigger: :none
1919
20- @ type init_args :: [ pin_spec : GPIO . pin_spec ( ) ]
20+ @ type init_args :: [ gpio_spec : GPIO . gpio_spec ( ) ]
2121
2222 @ doc """
2323 Helper for creating child_specs for simple I2C implementations
@@ -27,9 +27,9 @@ defmodule CircuitsSim.GPIO.GPIOServer do
2727 :start => { __MODULE__ , :start_link , [ [ any ( ) ] , ... ] }
2828 }
2929 def child_spec_helper ( device , args ) do
30- pin_spec = Keyword . fetch! ( args , :pin_spec )
30+ gpio_spec = Keyword . fetch! ( args , :gpio_spec )
3131
32- combined_args = Keyword . merge ( [ device: device , name: via_name ( pin_spec ) ] , args )
32+ combined_args = Keyword . merge ( [ device: device , name: via_name ( gpio_spec ) ] , args )
3333
3434 % {
3535 id: __MODULE__ ,
@@ -39,64 +39,59 @@ defmodule CircuitsSim.GPIO.GPIOServer do
3939
4040 @ spec start_link ( keyword ( ) ) :: GenServer . on_start ( )
4141 def start_link ( init_args ) do
42- pin_spec = Keyword . fetch! ( init_args , :pin_spec )
42+ gpio_spec = Keyword . fetch! ( init_args , :gpio_spec )
4343
44- GenServer . start_link ( __MODULE__ , init_args , name: via_name ( pin_spec ) )
44+ GenServer . start_link ( __MODULE__ , init_args , name: via_name ( gpio_spec ) )
4545 end
4646
4747 # Helper for constructing the via_name for GPIODevice servers
48- defp via_name ( pin_spec ) do
49- DeviceRegistry . via_name ( :gpio , pin_spec , 0 )
48+ defp via_name ( gpio_spec ) do
49+ DeviceRegistry . via_name ( :gpio , gpio_spec , 0 )
5050 end
5151
52- @ spec write ( GPIO . pin_spec ( ) , GPIO . value ( ) ) :: :ok
53- def write ( pin_spec , value ) do
54- GenServer . call ( via_name ( pin_spec ) , { :write , value } )
52+ @ spec write ( GPIO . gpio_spec ( ) , GPIO . value ( ) ) :: :ok
53+ def write ( gpio_spec , value ) do
54+ GenServer . call ( via_name ( gpio_spec ) , { :write , value } )
5555 end
5656
57- @ spec read ( GPIO . pin_spec ( ) ) :: GPIO . value ( )
58- def read ( pin_spec ) do
59- GenServer . call ( via_name ( pin_spec ) , :read )
57+ @ spec read ( GPIO . gpio_spec ( ) ) :: GPIO . value ( )
58+ def read ( gpio_spec ) do
59+ GenServer . call ( via_name ( gpio_spec ) , :read )
6060 end
6161
62- @ spec set_direction ( GPIO . pin_spec ( ) , GPIO . pin_direction ( ) ) :: :ok | { :error , atom ( ) }
63- def set_direction ( pin_spec , direction ) do
64- GenServer . call ( via_name ( pin_spec ) , { :set_direction , direction } )
62+ @ spec set_direction ( GPIO . gpio_spec ( ) , GPIO . pin_direction ( ) ) :: :ok | { :error , atom ( ) }
63+ def set_direction ( gpio_spec , direction ) do
64+ GenServer . call ( via_name ( gpio_spec ) , { :set_direction , direction } )
6565 end
6666
67- @ spec set_interrupts ( GPIO . pin_spec ( ) , GPIO . trigger ( ) , GPIO . interrupt_options ( ) ) ::
67+ @ spec set_interrupts ( GPIO . gpio_spec ( ) , GPIO . trigger ( ) , GPIO . interrupt_options ( ) ) ::
6868 :ok | { :error , atom ( ) }
69- def set_interrupts ( pin_spec , trigger , options ) do
69+ def set_interrupts ( gpio_spec , trigger , options ) do
7070 receiver = options [ :receiver ] || self ( )
71- GenServer . call ( via_name ( pin_spec ) , { :set_interrupts , trigger , receiver } )
71+ GenServer . call ( via_name ( gpio_spec ) , { :set_interrupts , trigger , receiver } )
7272 end
7373
74- @ spec set_pull_mode ( GPIO . pin_spec ( ) , GPIO . pull_mode ( ) ) :: :ok | { :error , atom ( ) }
75- def set_pull_mode ( pin_spec , pull_mode ) do
76- GenServer . call ( via_name ( pin_spec ) , { :set_pull_mode , pull_mode } )
74+ @ spec set_pull_mode ( GPIO . gpio_spec ( ) , GPIO . pull_mode ( ) ) :: :ok | { :error , atom ( ) }
75+ def set_pull_mode ( gpio_spec , pull_mode ) do
76+ GenServer . call ( via_name ( gpio_spec ) , { :set_pull_mode , pull_mode } )
7777 end
7878
79- @ spec info ( GPIO . pin_spec ( ) ) :: map ( )
80- def info ( pin_spec ) do
81- GenServer . call ( via_name ( pin_spec ) , :info )
79+ @ spec render ( GPIO . gpio_spec ( ) ) :: IO.ANSI . ansidata ( )
80+ def render ( gpio_spec ) do
81+ GenServer . call ( via_name ( gpio_spec ) , :render )
8282 end
8383
84- @ spec render ( GPIO . pin_spec ( ) ) :: IO.ANSI . ansidata ( )
85- def render ( pin_spec ) do
86- GenServer . call ( via_name ( pin_spec ) , :render )
87- end
88-
89- @ spec send_message ( GPIO . pin_spec ( ) , any ( ) ) :: any ( )
90- def send_message ( pin_spec , message ) do
91- GenServer . call ( via_name ( pin_spec ) , { :send_message , message } )
84+ @ spec send_message ( GPIO . gpio_spec ( ) , any ( ) ) :: any ( )
85+ def send_message ( gpio_spec , message ) do
86+ GenServer . call ( via_name ( gpio_spec ) , { :send_message , message } )
9287 end
9388
9489 @ impl GenServer
9590 def init ( init_args ) do
96- pin_spec = Keyword . fetch! ( init_args , :pin_spec )
91+ gpio_spec = Keyword . fetch! ( init_args , :gpio_spec )
9792 device = Keyword . fetch! ( init_args , :device )
9893
99- { :ok , % __MODULE__ { pin_spec: pin_spec , device: device } }
94+ { :ok , % __MODULE__ { gpio_spec: gpio_spec , device: device } }
10095 end
10196
10297 @ impl GenServer
@@ -109,7 +104,7 @@ defmodule CircuitsSim.GPIO.GPIOServer do
109104 { :reply , :ok , % { state | device: new_device , cached_value: value } }
110105
111106 :input ->
112- Logger . warning ( "Ignoring write to input GPIO #{ inspect ( state . pin_spec ) } " )
107+ Logger . warning ( "Ignoring write to input GPIO #{ inspect ( state . gpio_spec ) } " )
113108 { :reply , :ok , state }
114109 end
115110 end
@@ -183,7 +178,7 @@ defmodule CircuitsSim.GPIO.GPIOServer do
183178
184179 defp process_read ( state , :hi_z ) do
185180 Logger . warning (
186- "GPIO #{ inspect ( state . pin_spec ) } is in high impedance state. Set pull mode to reliably read."
181+ "GPIO #{ inspect ( state . gpio_spec ) } is in high impedance state. Set pull mode to reliably read."
187182 )
188183
189184 0
@@ -211,6 +206,6 @@ defmodule CircuitsSim.GPIO.GPIOServer do
211206 defp send_interrupt_message ( state , value ) do
212207 { uptime_ms , _ } = :erlang . statistics ( :wall_clock )
213208 uptime_ns = uptime_ms * 1_000_000
214- send ( state . interrupt_receiver , { :circuits_gpio , state . pin_spec , uptime_ns , value } )
209+ send ( state . interrupt_receiver , { :circuits_gpio , state . gpio_spec , uptime_ns , value } )
215210 end
216211end
0 commit comments