Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional compilation for test / deploy build with raspberry pi

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.

like image 910
quantumbyte Avatar asked Oct 19 '22 07:10

quantumbyte


1 Answers

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.

like image 61
Assen Kolov Avatar answered Oct 22 '22 00:10

Assen Kolov