In the recent discussion about new features @ahmetus suggested it would be useful to have a draft style guide for submitting sensor interfaces for inclusion in the uLisp Sensor Library. Comments welcomed.
Scope
My belief is that the interface routines should be designed to be as simple as possible, adopting default settings to keep the functions short.
Where there are additional settings, these should be provided by a separate optional function, rather than making the main function try and cater for everything.
Naming
Each function should be named with a short version of the sensor part number, followed by -init for the initialisation function, -temp for the temperature function, and so on.
So the functions for the Si7021 Humidity Sensor would be named:
si7021-init, si7021-temp, and si7021-humidity.
This naming convention is designed so you can have multiple sensors installed at once without conflict, even if more than one provides a temperature reading, for example.
Integer or floating-point?
Where possible the routines should be designed to use integer arithmetic, so they can be run on 8/16-bit platforms. For example, this routine for the ADT7410 returns the temperature in sixteenths of a degree:
(defun adt7410-temp ()
(with-i2c (str #x48)
(write-byte 0 str)
(restart-i2c str 2)
(let* ((hi (read-byte str))
(lo (read-byte str))
(tmp (logior (ash hi 5) (ash lo -3))))
(- (logand tmp #x0FFF) (logand tmp #x1000)))))
A user who wants the temperature as a floating-point number, in Ā°C, can do:
(/ (adt7410-temp) 16)
Sample output
An example of sample output from the routine should be shown; for example:
> (adt7410-temp)
338
shows that the temperature is 21.125Ā°C.
I accept that many of the functions already in the sensor library donāt keep to these conventions, and Iāll update them in due course.
Currently thereās no way for users to upload new sensors to the library, but if you post them on the forum in this Sensor Library category Iāll add them to the library.