Right way to use GenServer included in another Module?


i am currently exploring Elixir and also Nerves. Since i needed something to work on, i got a raspberry and some sensors. I want to controll them over I2C.

Now i want to write a module for a specific sensor. I already laid everything out, and got it working. Of course i use elixir_ale access the I2C Pins. So all my module does, is providing functions which will controll/read the sensor on top of elixir_ale.

My problem is: I don’t know how to “bundle” this. Ale starts a GenServer {:ok, pid} = I2C.start_link(name, i2c_address). How do i handle this in my own module? Should i also use GenServer and call ale’s start_link in my init function? This way the end user would only start my module’s GenServer, and could work with it?

I am somehow unsure if this is the optimal way or will create unneeded complexity. Can you provide me some help? :slight_smile:

1 Like

Sounds like you could use a supervisor module that starts the I2C server and your own GenServer with a :rest_for_one strategy.

Clients then just add your supervisor module to their supervision tree.