supla-device
Loading...
Searching...
No Matches
MCP23017.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6/*
7 Dependency: https://github.com/RobTillaart/MCP23017_RT
8 Use library manager to install it
9*/
10
11#include <MCP23017.h>
12
13#include <supla/io.h>
14#include <supla/mutex.h>
15#include <supla/element.h>
16#include <supla/log_wrapper.h>
17
18namespace Supla {
19namespace Io {
20
22 public:
23 explicit MCP23017(uint8_t address = 0x20,
24 Supla::Mutex *mutex = nullptr,
25 TwoWire *wire = &Wire,
26 bool pullUp = false)
27 : Supla::Io::Base(), mcp_(address, wire), mutex_(mutex) {
28 if (!mcp_.begin(pullUp)) {
29 SUPLA_LOG_ERROR("Unable to find MCP23017 at address: 0x%x", address);
30 } else {
31 SUPLA_LOG_DEBUG("MCP23017 is connected at address: 0x%x", address);
32 }
33 }
34
35 void onInit() {
36 read16FromMCP();
37 outState_ = inState_;
38 lastOutState_ = inState_;
39 }
40
41 void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {
42 if (mutex_) mutex_->lock();
43 if (mcp_.isConnected()) {
44 mcp_.pinMode1(pin, mode);
45 }
46 if (mutex_) mutex_->unlock();
47 }
48
49 void customDigitalWrite(int channelNumber, uint8_t pin,
50 uint8_t val) override {
51 if (pin >= 16) {
52 SUPLA_LOG_WARNING("[MCP23017] can't write, pin %d out of range", pin);
53 return;
54 }
55 if (val) {
56 outState_ |= (1 << pin);
57 } else {
58 outState_ &= ~(1 << pin);
59 }
60 }
61
62 int customDigitalRead(int channelNumber, uint8_t pin) override {
63 if (pin >= 16) {
64 SUPLA_LOG_WARNING("[MCP23017] can't read, pin %d out of range", pin);
65 return 0;
66 }
67 return (inState_ >> pin) & 0x01;
68 }
69
70 unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
71 uint64_t timeoutMicro) override {
72 return 0;
73 }
74
75 void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}
76
77 int customAnalogRead(int channelNumber, uint8_t pin) override {
78 return 0;
79 }
80
81 void onTimer() override {
82 read16FromMCP();
83 write16ToMCP();
84 }
85
86 void read16FromMCP() {
87 if (mutex_) mutex_->lock();
88 if (mcp_.isConnected()) {
89 uint16_t data = mcp_.read16();
90 inState_ = (data >> 8) | (data << 8);
91 }
92 if (mutex_) mutex_->unlock();
93 }
94
95 void write16ToMCP() {
96 if (mutex_) mutex_->lock();
97 if (mcp_.isConnected() && outState_ != lastOutState_) {
98 uint16_t data = (outState_ >> 8) | (outState_ << 8);
99 mcp_.write16(data);
100 lastOutState_ = outState_;
101 }
102 if (mutex_) mutex_->unlock();
103 }
104
105 protected:
106 ::MCP23017 mcp_;
107 uint16_t outState_ = 0;
108 uint16_t inState_ = 0;
109 uint16_t lastOutState_ = 0;
110 Supla::Mutex *mutex_ = nullptr;
111};
112
113}; // namespace Io
114}; // namespace Supla
Base class for all elements of SuplaDevice.
Definition element.h:27
Definition io.h:23
Definition MCP23017.h:21
void onInit()
Third method called on element in SuplaDevice.begin()
Definition MCP23017.h:35
void onTimer() override
Method called on timer interupt.
Definition MCP23017.h:81
Definition mutex.h:9