supla-device
Loading...
Searching...
No Matches
ADS1115.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#pragma once
20
21/*
22Dependency: https://github.com/RobTillaart/ADS1X15
23Use library manager to install it
24*/
25
26#include <ADS1X15.h>
27#include <supla/io.h>
28#include <supla/log_wrapper.h>
29
30namespace Supla {
31namespace Io {
32
33class ADS1115 : public Supla::Io::Base {
34 public:
35 explicit ADS1115(uint8_t address = 0x48,
36 TwoWire *wire = &Wire,
37 uint8_t dataRrate = 7)
38 : Supla::Io::Base(false), ads_(address, wire) {
39 if (!ads_.begin()) {
40 SUPLA_LOG_ERROR("Unable to find ADS1115 at address 0x%x", address);
41 } else {
42 ads_.setDataRate(dataRrate);
43 SUPLA_LOG_DEBUG("ADS1115 is connected at address: 0x%x, Gain: %d, "
44 "DataRate: %d", address, ads_.getGain(), ads_.getDataRate());
45 }
46 }
47
48 void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {}
49
50 void customDigitalWrite(int channelNumber, uint8_t pin,
51 uint8_t val) override {}
52
53 int customDigitalRead(int channelNumber, uint8_t pin) override {
54 return 0;
55 }
56
57 unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
58 uint64_t timeoutMicro) override {
59 return 0;
60 }
61
62 void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}
63
64 int customAnalogRead(int channelNumber, uint8_t pin) override {
65 if (ads_.isConnected()) {
66 if (pin > 3) {
67 SUPLA_LOG_WARNING("[ADS1115] invalid pin %d", pin);
68 return -1;
69 }
70 ads_.setGain(gain_);
71 return ads_.readADC(pin);
72 } else {
73 return -1;
74 }
75 }
76
77 void setGain(uint8_t value) {
78 gain_ = value;
79 }
80
81 protected:
82 ::ADS1115 ads_;
83 uint8_t gain_ = 0;
84};
85
86}; // namespace Io
87}; // namespace Supla
Definition io.h:35