supla-device
Loading...
Searching...
No Matches
PCF8574.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/PCF8574
23Use library manager to install it
24*/
25
26#include <PCF8574.h>
27
28#include <supla/io.h>
29#include <supla/mutex.h>
30#include <supla/log_wrapper.h>
31
32namespace Supla {
33namespace Io {
34
35class PCF8574 : public Supla::Io::Base {
36 public:
37 explicit PCF8574(uint8_t address = 0x20,
38 Supla::Mutex *mutex = nullptr,
39 uint8_t initialPinState = 0xFF,
40 TwoWire *wire = &Wire)
41 : Supla::Io::Base(false), pcf_(address, wire), mutex_(mutex) {
42 if (!pcf_.begin(initialPinState)) {
43 SUPLA_LOG_ERROR("Unable to find PCF8574 at address 0x%x", address);
44 } else {
45 SUPLA_LOG_DEBUG("PCF8574 is connected at address: 0x%x", address);
46 }
47 }
48
49 void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {
50 if (mutex_) mutex_->lock();
51 if (mode == INPUT_PULLUP && pcf_.isConnected()) {
52 pcf_.write(pin, HIGH);
53 }
54 if (mutex_) mutex_->unlock();
55 }
56
57 void customDigitalWrite(int channelNumber, uint8_t pin,
58 uint8_t val) override {
59 if (mutex_) mutex_->lock();
60 if (pcf_.isConnected()) {
61 pcf_.write(pin, val);
62 }
63 if (mutex_) mutex_->unlock();
64 }
65
66 int customDigitalRead(int channelNumber, uint8_t pin) override {
67 uint8_t val;
68 if (mutex_) mutex_->lock();
69 val = pcf_.isConnected() ? pcf_.read(pin) : 0;
70 if (mutex_) mutex_->unlock();
71 return val;
72 }
73
74 unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
75 uint64_t timeoutMicro) override {
76 return 0;
77 }
78
79 void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}
80
81 int customAnalogRead(int channelNumber, uint8_t pin) override {
82 return 0;
83 }
84
85 protected:
86 ::PCF8574 pcf_;
87 Supla::Mutex *mutex_ = nullptr;
88};
89
90}; // namespace Io
91}; // namespace Supla
Definition io.h:35
Definition mutex.h:22