Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added linux support. #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Adafruit_ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
v1.0 - First release
v1.1 - Added ADS1115 support - W. Earl
v2.0 - Refactor - C. Nelson
v3.0 - Added Linux support - HonestQiao

@section license License

Expand Down Expand Up @@ -62,10 +63,28 @@ Adafruit_ADS1115::Adafruit_ADS1115() {
@return true if successful, otherwise false
*/
/**************************************************************************/
#if defined(__linux__)
bool Adafruit_ADS1X15::begin(uint8_t i2c_addr, int id) {
char i2c_device[15] = {0};
sprintf(i2c_device, "/dev/i2c-%d", id);
fd = open(i2c_device, O_RDWR);
if (fd < 0) {
return -1;
} else {
if (ioctl(fd, ADS1X15_I2C_SLAVE, i2c_addr) < 0) {
close(fd);
return -2;
} else {
return true;
}
}
}
#else
bool Adafruit_ADS1X15::begin(uint8_t i2c_addr, TwoWire *wire) {
m_i2c_dev = new Adafruit_I2CDevice(i2c_addr, wire);
return m_i2c_dev->begin();
}
#endif

/**************************************************************************/
/*!
Expand Down Expand Up @@ -383,7 +402,11 @@ void Adafruit_ADS1X15::writeRegister(uint8_t reg, uint16_t value) {
buffer[0] = reg;
buffer[1] = value >> 8;
buffer[2] = value & 0xFF;
#if defined(__linux__)
write(fd, buffer, 3);
#else
m_i2c_dev->write(buffer, 3);
#endif
}

/**************************************************************************/
Expand All @@ -397,7 +420,12 @@ void Adafruit_ADS1X15::writeRegister(uint8_t reg, uint16_t value) {
/**************************************************************************/
uint16_t Adafruit_ADS1X15::readRegister(uint8_t reg) {
buffer[0] = reg;
#if defined(__linux__)
write(fd, buffer, 1);
read(fd, buffer, 2);
#else
m_i2c_dev->write(buffer, 1);
m_i2c_dev->read(buffer, 2);
#endif
return ((buffer[0] << 8) | buffer[1]);
}
34 changes: 31 additions & 3 deletions Adafruit_ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,34 @@
#ifndef __ADS1X15_H__
#define __ADS1X15_H__

#if defined(__linux__)
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define delay(x) usleep(1000 * x)
#else
#include <Adafruit_I2CDevice.h>
#include <Arduino.h>
#include <Wire.h>
#endif

/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define ADS1X15_ADDRESS (0x48) ///< 1001 000 (ADDR = GND)
#if defined(__linux__)
#define ADS1X15_I2C_SLAVE (0x0703) // IIC SLAVE
#endif
/*=========================================================================*/

/*=========================================================================
Expand Down Expand Up @@ -148,13 +168,21 @@ typedef enum {
class Adafruit_ADS1X15 {
protected:
// Instance-specific properties
#if defined(__linux__)
int fd;
#else
Adafruit_I2CDevice *m_i2c_dev; ///< I2C bus device
uint8_t m_bitShift; ///< bit shift amount
adsGain_t m_gain; ///< ADC gain
uint16_t m_dataRate; ///< Data rate
#endif
uint8_t m_bitShift; ///< bit shift amount
adsGain_t m_gain; ///< ADC gain
uint16_t m_dataRate; ///< Data rate

public:
#if defined(__linux__)
bool begin(uint8_t i2c_addr = ADS1X15_ADDRESS, int id = 0);
#else
bool begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire);
#endif
int16_t readADC_SingleEnded(uint8_t channel);
int16_t readADC_Differential_0_1();
int16_t readADC_Differential_0_3();
Expand Down
76 changes: 76 additions & 0 deletions examples/linux/singleended.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "../../Adafruit_ADS1X15.h"

Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
// Adafruit_ADS1015 ads; /* Use this for the 12-bit version */

void setup(void) {
// Serial.begin(9600);
// Serial.println("Hello!");

// Serial.println("Getting single-ended readings from AIN0..3");
// Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015,
// 0.1875mV/ADS1115)");
printf("Getting single-ended readings from AIN0..3");
printf("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");

// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015
// ADS1115
// -------
// -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV
// (default) ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit =
// 2mV 0.125mV ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1
// bit = 1mV 0.0625mV ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V
// 1 bit = 0.5mV 0.03125mV ads.setGain(GAIN_EIGHT); // 8x gain +/-
// 0.512V 1 bit = 0.25mV 0.015625mV ads.setGain(GAIN_SIXTEEN); // 16x
// gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV

if (!ads.begin()) {
// Serial.println("Failed to initialize ADS.");
printf("Failed to initialize ADS.");
while (1)
;
}
}

void loop(void) {
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;

adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
adc3 = ads.readADC_SingleEnded(3);

volts0 = ads.computeVolts(adc0);
volts1 = ads.computeVolts(adc1);
volts2 = ads.computeVolts(adc2);
volts3 = ads.computeVolts(adc3);

// Serial.println("-----------------------------------------------------------");
// Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" ");
// Serial.print(volts0); Serial.println("V"); Serial.print("AIN1: ");
// Serial.print(adc1); Serial.print(" "); Serial.print(volts1);
// Serial.println("V"); Serial.print("AIN2: "); Serial.print(adc2);
// Serial.print(" "); Serial.print(volts2); Serial.println("V");
// Serial.print("AIN3: "); Serial.print(adc3); Serial.print(" ");
// Serial.print(volts3); Serial.println("V");
printf("-----------------------------------------------------------\n");
printf("AIN0: %d %f V\n", adc0, volts0);
printf("AIN0: %d %f V\n", adc1, volts1);
printf("AIN0: %d %f V\n", adc2, volts2);
printf("AIN0: %d %f V\n", adc3, volts3);

delay(1000);
}

int main(int argc, char *argv[]) {
setup();
while (true) {
loop();
}
}