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