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
28#include <supla/io.h>
29#include <supla/mutex.h>
30#include <supla/element.h>
31#include <supla/log_wrapper.h>
32
33namespace Supla {
34namespace Io {
35
36class ADS1115 : public Supla::Io::Base {
37 public:
38 explicit ADS1115(uint8_t address = 0x48,
39 Supla::Mutex *mutex = nullptr,
40 TwoWire *wire = &Wire,
41 uint8_t dataRrate = 7)
42 : Supla::Io::Base(false), ads_(address, wire), mutex_(mutex) {
43 if (!ads_.begin()) {
44 SUPLA_LOG_ERROR("Unable to find ADS1115 at address 0x%x", address);
45 } else {
46 ads_.setDataRate(dataRrate);
47 ads_.setMode(0);
48 ads_.readADC(0);
49 SUPLA_LOG_DEBUG("ADS1115 is connected at address: 0x%x, Gain: %d, "
50 "DataRate: %d", address, ads_.getGain(), ads_.getDataRate());
51 }
52 }
53
54 void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {}
55
56 void customDigitalWrite(int channelNumber, uint8_t pin,
57 uint8_t val) override {}
58
59 int customDigitalRead(int channelNumber, uint8_t pin) override {
60 return 0;
61 }
62
63 unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
64 uint64_t timeoutMicro) override {
65 return 0;
66 }
67
68 void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}
69
70 int customAnalogRead(int channelNumber, uint8_t pin) override {
71 if (pin > 3) {
72 SUPLA_LOG_WARNING("[ADS1115] invalid pin %d", pin);
73 return -1;
74 }
75 if (mutex_) mutex_->lock();
76 if (ads_.isConnected()) {
77 ads_.setGain(gain_);
78 readValue_[pin] = ads_.readADC(pin);
79 }
80 if (mutex_) mutex_->unlock();
81 return readValue_[pin];
82 }
83
84 void setGain(uint8_t value) {
85 gain_ = value;
86 }
87
88 protected:
89 ::ADS1115 ads_;
90 uint8_t gain_ = 0;
91 int16_t readValue_[4] = {-1, -1, -1, -1};
92 Supla::Mutex *mutex_ = nullptr;
93};
94
95}; // namespace Io
96}; // namespace Supla
Definition io.h:35
Definition mutex.h:22