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