supla-device
Loading...
Searching...
No Matches
PCA9685.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/*
22Version: 25.11.24
23Dependency: https://github.com/RobTillaart/PCA9685_RT
24Use library manager to install it
25*/
26
27#include <PCA9685.h>
28
29#include <supla/io.h>
30#include <supla/mutex.h>
31#include <supla/log_wrapper.h>
32
33namespace Supla {
34namespace Io {
35
36class PCA9685 : public Supla::Io::Base {
37 public:
38 explicit PCA9685(uint8_t address = 0x40,
39 Supla::Mutex *mutex = nullptr,
40 TwoWire *wire = &Wire)
41 : Supla::Io::Base(false), pca_(address, wire), mutex_(mutex) {
42 if (!pca_.begin()) {
43 SUPLA_LOG_ERROR("Unable to find PCA9685 at address 0x%x", address);
44 } else {
45 SUPLA_LOG_DEBUG("PCA9685 is connected at address: 0x%x, "
46 "with PWM freq: %d Hz", address, pca_.getFrequency());
47 }
48 }
49
50 void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {
51 }
52 void customDigitalWrite(int channelNumber, uint8_t pin,
53 uint8_t val) override {
54 if (mutex_) mutex_->lock();
55 if (pca_.isConnected()) {
56 pca_.write1(pin, val);
57 }
58 if (mutex_) mutex_->unlock();
59 }
60 int customDigitalRead(int channelNumber, uint8_t pin) override {
61 uint8_t val = 0;
62 if (mutex_) mutex_->lock();
63 if (pca_.isConnected()) {
64 val = pca_.read1(pin);
65 }
66 if (mutex_) mutex_->unlock();
67 return (val == 1) ? 1 : 0;
68 }
69 unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
70 uint64_t timeoutMicro) override {
71 return 0;
72 }
73 void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {
74 if (mutex_) mutex_->lock();
75 if (pca_.isConnected()) {
76 val = map(val, 0, 1023, 0, 4095);
77 pca_.setPWM(pin, val);
78 }
79 if (mutex_) mutex_->unlock();
80 }
81
82 int customAnalogRead(int channelNumber, uint8_t pin) override {
83 return 0;
84 }
85
86 // Default frequency: 200 Hz
87 void setPWMFrequency(uint16_t frequency) {
88 if (mutex_) mutex_->lock();
89 if (pca_.isConnected()) {
90 pca_.setFrequency(frequency);
91 SUPLA_LOG_DEBUG("[PCA9685] set PWM frequency: %d Hz",
92 pca_.getFrequency());
93 }
94 if (mutex_) mutex_->unlock();
95 }
96
97 protected:
98 ::PCA9685 pca_;
99 Supla::Mutex *mutex_ = nullptr;
100};
101
102}; // namespace Io
103}; // namespace Supla
Definition io.h:35
Definition mutex.h:22