supla-device
Loading...
Searching...
No Matches
MCP23017.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/*
22 Dependency: https://github.com/RobTillaart/MCP23017_RT
23 Use library manager to install it
24*/
25
26#include <MCP23017.h>
27#include <supla/io.h>
28#include <supla/element.h>
29#include <supla/log_wrapper.h>
30
31namespace Supla {
32namespace Io {
33
34class MCP23017 : public Supla::Io::Base, Supla::Element {
35 public:
36 explicit MCP23017(uint8_t address = 0x20,
37 TwoWire *wire = &Wire,
38 bool pullUp = false)
39 : Supla::Io::Base(false), mcp_(address, wire) {
40 if (!mcp_.begin(pullUp)) {
41 SUPLA_LOG_ERROR("Unable to find MCP23017 at address: 0x%x", address);
42 } else {
43 SUPLA_LOG_DEBUG("MCP23017 is connected at address: 0x%x", address);
44 }
45 }
46
47 void onInit() {
48 if (mcp_.isConnected()) {
49 read16FromMCP();
50 }
51 }
52
53 void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {
54 if (mcp_.isConnected()) {
55 mcp_.pinMode1(pin, mode);
56 }
57 }
58
59 void customDigitalWrite(int channelNumber, uint8_t pin,
60 uint8_t val) override {
61 if (mcp_.isConnected()) {
62 mcp_.write1(pin, val);
63 } else {
64 SUPLA_LOG_WARNING(
65 "[MCP23017] not connected, cannot write to pin %d", pin);
66 }
67 }
68
69 int customDigitalRead(int channelNumber, uint8_t pin) override {
70 if (pin >= 16) {
71 return 0;
72 }
73 return (gpioState_ >> pin) & 0x01;
74 }
75
76 unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
77 uint64_t timeoutMicro) override {
78 return 0;
79 }
80
81 void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}
82
83 int customAnalogRead(int channelNumber, uint8_t pin) override {
84 return 0;
85 }
86
87 void onTimer() override {
88 read16FromMCP();
89 }
90
91 void read16FromMCP() {
92 if (mcp_.isConnected()) {
93 uint16_t data = mcp_.read16();
94 gpioState_ = (data >> 8) | (data << 8);
95 }
96 }
97
98 protected:
99 ::MCP23017 mcp_;
100 uint16_t gpioState_ = 0;
101};
102
103}; // namespace Io
104}; // namespace Supla
Base class for all elements of SuplaDevice.
Definition element.h:37
Definition io.h:35
void onInit()
Third method called on element in SuplaDevice.begin()
Definition MCP23017.h:47
void onTimer() override
Method called on timer interupt.
Definition MCP23017.h:87