Commit f87d68ff authored by Pawel Jasinski's avatar Pawel Jasinski Committed by Arnim Läuger
Browse files

added support for ads1015 (#2231)

* ads1015 is supported, up to 4 devices can be connected at the same time

* removed debug, updated documentation

* changed to oop API

* added __gc to handle active timer cleanup

* reworked argument validation and error reporting

* stack is no longer messed up after __del
parent ed56d949
This diff is collapsed.
......@@ -3,17 +3,96 @@
| :----- | :-------------------- | :---------- | :------ |
| 2017-04-24 | [fetchbot](https://github.com/fetchbot) | [fetchbot](https://github.com/fetchbot) | [ads1115.c](../../../app/modules/ads1115.c)|
This module provides access to the ADS1115 16-Bit analog-to-digital converter.
This module provides access to the ADS1115 (16-Bit) and ADS1015 (12-Bit) analog-to-digital converters.
Other chips from the same family (ADS1113, ADS1114, ADS1013 and ADS1014) are likely to work. Missing hardware features will be silently ignored.
This module supports multiple devices connected to I²C bus. The devices of different types can be mixed.
The addressing of ADS family allows for maximum of 4 devices connected to the same I²C bus.
!!! caution
The **ABSOLUTE MAXIMUM RATINGS** for all analog inputs are `–0.3V to VDD+0.3V` referred to GND.
## ads1115.read()
## ads1115.ads1115()
Registers ADS1115 (ADS1113, ADS1114) device.
#### Syntax
`ads1115.ADS1115(I2C_ID, I2C_ADDR)`
#### Parameters
- `I2C_ID` - always 0
- `ADDRESS` - I²C address of a device
* `ads1115.ADDR_GND`
* `ads1115.ADDR_VDD`
* `ads1115.ADDR_SDA`
* `ads1115.ADDR_SCL`
#### Returns
Registered `device` object
#### Example
```lua
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1115(id, ads1115.ADDR_GND)
```
## ads1115.ads1015()
Registers ADS1015 (ADS1013, ADS1014) device.
#### Syntax
`ads1115.ads1015(I2C_ID, I2C_ADDR)`
#### Parameters
- `I2C_ID` - always 0
- `ADDRESS` - I²C address of a device
* `ads1115.ADDR_GND`
* `ads1115.ADDR_VDD`
* `ads1115.ADDR_SDA`
* `ads1115.ADDR_SCL`
#### Returns
Registered `device` object
#### Example
```lua
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1015(id, ads1115.ADDR_VDD)
adc2 = ads1115.ads1115(id, ads1115.ADDR_SDA)
```
## ads1115.reset()
Reset all devices connected to I²C interface.
### Syntax
ads1115.reset()
#### Parameters
none
#### Returns
`nil`
#### Example
```lua
local id, alert_pin, sda, scl = 0, 7, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
```
# ADS Device
## ads1115.device:read()
Gets the result stored in the register of a previously issued conversion, e.g. in continuous mode or with a conversion ready interrupt.
#### Syntax
`volt, volt_dec, adc = ads1115.read()`
`volt, volt_dec, adc = device:read()`
#### Parameters
none
......@@ -25,39 +104,43 @@ none
!!! note
If using float firmware then `volt` is a floating point number. On an integer firmware, the final value has to be concatenated from `volt` and `volt_dec`.
If using float firmware then `volt` is a floating point number. On an integer firmware, the final value has to be concatenated from `volt` and `volt_dec`. Both values `volt` and `volt_dec` contains sign.
#### Example
```lua
local id, alert_pin, sda, scl = 0, 7, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.setup(ads1115.ADDR_GND)
ads1115.reset()
adc1 = ads1115.ads1115(id, ads1115.ADDR_GND)
-- continuous mode
ads1115.setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS)
-- read adc result with read()
volt, volt_dec, adc = ads1115.read()
volt, volt_dec, adc = ads1:read()
print(volt, volt_dec, adc)
-- comparator
ads1115.setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS, ads1115.COMP_1CONV, 1000, 2000)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS, ads1115.COMP_1CONV, 1000, 2000)
local function comparator(level, when)
-- read adc result with read() when threshold reached
volt, volt_dec, adc = ads1115.read()
gpio.trig(alert_pin)
volt, volt_dec, adc = ads1:read()
print(volt, volt_dec, adc)
end
gpio.mode(alert_pin, gpio.INT)
gpio.trig(alert_pin, "both", comparator)
-- read adc result with read()
volt, volt_dec, adc = ads1115.read()
volt, volt_dec, adc = ads1115:read()
print(volt, volt_dec, adc)
```
## ads1115.setting()
## ads1115.device:setting()
Configuration settings for the ADC.
#### Syntax
`ads1115.setting(GAIN, SAMPLES, CHANNEL, MODE[, CONVERSION_RDY][, COMPARATOR, THRESHOLD_LOW, THRESHOLD_HI])`
`device:setting(GAIN, SAMPLES, CHANNEL, MODE[, CONVERSION_RDY][, COMPARATOR, THRESHOLD_LOW, THRESHOLD_HI[,COMP_MODE]])`
#### Parameters
- `GAIN` Programmable gain amplifier
......@@ -68,14 +151,19 @@ Configuration settings for the ADC.
* `ads1115.GAIN_0_512V` 8x Gain
* `ads1115.GAIN_0_256V` 16x Gain
- `SAMPLES` Data rate in samples per second
* `ads1115.DR_8SPS`
* `ads1115.DR_16SPS`
* `ads1115.DR_32SPS`
* `ads1115.DR_64SPS`
* `ads1115.DR_8SPS` ADS1115 only
* `ads1115.DR_16SPS` ADS1115 only
* `ads1115.DR_32SPS` ADS1115 only
* `ads1115.DR_64SPS` ADS1115 only
* `ads1115.DR_128SPS`
* `ads1115.DR_250SPS`
* `ads1115.DR_475SPS`
* `ads1115.DR_860SPS`
* `ads1115.DR_475SPS` ADS1115 only
* `ads1115.DR_490SPS` ADS1015 only
* `ads1115.DR_860SPS` ADS1115 only
* `ads1115.DR_920SPS` ADS1015 only
* `ads1115.DR_1600SPS` ADS1015 only
* `ads1115.DR_2400SPS` ADS1015 only
* `ads1115.DR_3300SPS` ADS1015 only
- `CHANNEL` Input multiplexer for single-ended or differential measurement
* `ads1115.SINGLE_0` channel 0 to GND
* `ads1115.SINGLE_1` channel 1 to GND
......@@ -102,31 +190,11 @@ Configuration settings for the ADC.
- `THRESHOLD_HI`
* `0` - `+ GAIN_MAX` in mV for single-ended inputs
* `- GAIN_MAX` - `+ GAIN_MAX` in mV for differential inputs
- `COMP_MODE` Comparator mode
* `ads1115.CMODE_TRAD` traditional comparator mode (with hysteresis)
* `ads1115.CMODE_WINDOW` window comparator mode
#### Returns
`nil`
#### Example
```lua
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.setup(ads1115.ADDR_GND)
ads1115.setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT)
```
## ads1115.setup()
Initializes the device on the defined I²C device address.
#### Syntax
`ads1115.setup(ADDRESS)`
#### Parameters
- `ADDRESS`
* `ads1115.ADDR_GND`
* `ads1115.ADDR_VDD`
* `ads1115.ADDR_SDA`
* `ads1115.ADDR_SCL`
note: Comparator and conversion ready are always configured to non-latching, active low.
#### Returns
`nil`
......@@ -135,15 +203,18 @@ Initializes the device on the defined I²C device address.
```lua
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1015(id, ads1115.ADDR_GND)
ads1115.setup(ads1115.ADDR_GND)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_3300SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT)
```
## ads1115.startread()
## ads1115.device:startread()
Starts the ADC reading for single-shot mode and after the conversion is done it will invoke an optional callback function in which the ADC conversion result can be obtained.
#### Syntax
`ads1115.startread([CALLBACK])`
`device:startread([CALLBACK])`
#### Parameters
- `CALLBACK` callback function which will be invoked after the adc conversion is done
......@@ -156,21 +227,23 @@ Starts the ADC reading for single-shot mode and after the conversion is done it
```lua
local id, alert_pin, sda, scl = 0, 7, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.setup(ads1115.ADDR_GND)
ads1115.reset()
adc1 = ads1115.ads1115(id, ads1115.ADDR_VDD)
-- single shot
ads1115.setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT)
-- start adc conversion and get result in callback after conversion is ready
ads1115.startread(function(volt, volt_dec, adc) print(volt, volt_dec, adc) end)
adc1:startread(function(volt, volt_dec, adc) print(volt, volt_dec, adc) end)
-- conversion ready
ads1115.setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT, ads1115.CONV_RDY_1)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT, ads1115.CONV_RDY_1)
local function conversion_ready(level, when)
volt, volt_dec, adc = ads1115.read()
gpio.trig(alert_pin)
volt, volt_dec, adc = adc1:read()
print(volt, volt_dec, adc)
end
gpio.mode(alert_pin, gpio.INT)
gpio.trig(alert_pin, "down", conversion_ready)
-- start conversion and get result with read() after conversion ready pin asserts
ads1115.startread()
adc1:startread()
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment