I am building an application for the raspberry pi and use pi4j as a dependency for Software PWM on GPIO. I'd like to test my code on my local machine though, so I would like to compile my code without the pi4j dependency and skip method calls to the library.
Example code:
(ns led-server.model
(:require [clojure.tools.logging :as log])
(:import [com.pi4j.wiringpi SoftPwm Gpio])) ;; pi4j dependency, only compiles on rPi
(defn- soft-pwm-write [pin value]
(let [ival (Math/round (double (* value RANGE)))]
(SoftPwm/softPwmWrite pin ival) ;; call to pi4j. This is what I want to skip
(log/info "pin" pin "set to" ival))
)
pi4j requires the wiringPi C library, which is only available on the raspberry pi (which makes sense). For testing on my dev machine it would be sufficient to see the log printout. I don't want to comment out the :import
and method calls for testing, I would like a more elegant solution.
Apart from the question if conditional compilation is the best approach here, it is not difficult to compile files conditionally with leiningen: put the files you want to compile conditionally in a folder different than src/clj, and define it as a source folder in a profile:
:profiles {
:native {:source-paths ["src/native/clj/"]}
:mock {:source-paths ["src/mock/clj/"]}
Then, run leininig with profile:
lein with-profiles +mock repl
See more at https://github.com/technomancy/leiningen/blob/master/doc/PROFILES.md
In your situation, you could define a protocol, provide 2 implementations and make sure you load only the classes relevant to your environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With