From b8cd6673790972f69f56384d27bb6096e59e9e75 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Thu, 6 Jan 2022 20:21:11 +0100 Subject: [PATCH] add channelCount (#13) * add channelCount, documentation, update examples --- PCA9685.cpp | 14 ++++++++------ PCA9685.h | 13 ++++++++++--- README.md | 1 + .../PCA9685_allOFF_test/PCA9685_allOFF_test.ino | 2 +- .../PCA9685_digitalWrite_test.ino | 2 ++ .../PCA9685_maxPWM_test/PCA9685_maxPWM_test.ino | 2 ++ .../PCA9685_setFrequency_offset.ino | 2 ++ .../PCA9685_setFrequency_test.ino | 8 +++++--- examples/PCA9685_test01/PCA9685_test01.ino | 8 +++++--- examples/PCA9685_test02/PCA9685_test02.ino | 7 +++++-- keywords.txt | 3 +++ library.json | 2 +- library.properties | 2 +- test/unit_test_001.cpp | 2 ++ 14 files changed, 48 insertions(+), 20 deletions(-) diff --git a/PCA9685.cpp b/PCA9685.cpp index b3a73e4..f4277af 100644 --- a/PCA9685.cpp +++ b/PCA9685.cpp @@ -2,7 +2,7 @@ // FILE: PCA9685.cpp // AUTHOR: Rob Tillaart // DATE: 24-apr-2016 -// VERSION: 0.3.3 +// VERSION: 0.3.4 // PURPOSE: Arduino library for I2C PCA9685 16 channel PWM // URL: https://github.com/RobTillaart/PCA9685_RT // @@ -17,6 +17,7 @@ // 0.3.1 2021-01-05 Arduino-CI + unit test // 0.3.2 2021-01-14 WireN support // 0.3.3 2021-12-23 update library.json, license, readme, minor edits +// 0.3.4 2022-01-03 add channelCount() #include "PCA9685.h" @@ -145,7 +146,7 @@ uint8_t PCA9685::readMode(uint8_t reg) void PCA9685::setPWM(uint8_t channel, uint16_t onTime, uint16_t offTime) { _error = PCA9685_OK; - if (channel > 15) + if (channel >= _channelCount) { _error = PCA9685_ERR_CHANNEL; return; @@ -167,7 +168,7 @@ void PCA9685::setPWM(uint8_t channel, uint16_t offTime) void PCA9685::getPWM(uint8_t channel, uint16_t* onTime, uint16_t* offTime) { _error = PCA9685_OK; - if (channel > 15) + if (channel >= _channelCount) { _error = PCA9685_ERR_CHANNEL; return; @@ -193,8 +194,9 @@ void PCA9685::setFrequency(uint16_t freq, int offset) { _error = PCA9685_OK; _freq = freq; - if (_freq < 24) _freq = 24; // page 25 datasheet - if (_freq > 1526) _freq = 1526; + // limits frequency see page 25 datasheet + if (_freq < PCA9685_MIN_FREQ) _freq = PCA9685_MIN_FREQ; + if (_freq > PCA9685_MAX_FREQ) _freq = PCA9685_MAX_FREQ; // removed float operation for speed // faster but equal accurate // uint8_t scaler = round(25e6 / (_freq * 4096)) - 1; @@ -226,7 +228,7 @@ int PCA9685::getFrequency(bool cache) void PCA9685::digitalWrite(uint8_t channel, uint8_t mode) { _error = PCA9685_OK; - if (channel > 15) + if (channel >= _channelCount) { _error = PCA9685_ERR_CHANNEL; return; diff --git a/PCA9685.h b/PCA9685.h index 317e880..89735bd 100644 --- a/PCA9685.h +++ b/PCA9685.h @@ -1,9 +1,9 @@ #pragma once // -// FILE: PCA9685.H +// FILE: PCA9685.h // AUTHOR: Rob Tillaart // DATE: 24-apr-2016 -// VERSION: 0.3.3 +// VERSION: 0.3.4 // PURPOSE: Arduino library for I2C PCA9685 16 channel PWM // URL: https://github.com/RobTillaart/PCA9685_RT @@ -12,7 +12,7 @@ #include "Wire.h" -#define PCA9685_LIB_VERSION (F("0.3.3")) +#define PCA9685_LIB_VERSION (F("0.3.4")) // ERROR CODES #define PCA9685_OK 0x00 @@ -21,6 +21,10 @@ #define PCA9685_ERR_MODE 0xFD #define PCA9685_ERR_I2C 0xFC +// get/setFrequency() +#define PCA9685_MIN_FREQ 24 +#define PCA9685_MAX_FREQ 1526 + class PCA9685 { @@ -34,6 +38,8 @@ class PCA9685 void reset(); bool isConnected(); + uint8_t channelCount() { return _channelCount; }; + // reg = 1, 2 check datasheet for values void writeMode(uint8_t reg, uint8_t value); uint8_t readMode(uint8_t reg); @@ -78,6 +84,7 @@ class PCA9685 uint8_t _address; int _error; int _freq = 200; // default PWM frequency - P25 datasheet + uint8_t _channelCount = 16; TwoWire* _wire; }; diff --git a/README.md b/README.md index d25d683..e1251dc 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Lower frequencies do better than higher frequencies. Library does not support multiple Wire instances (yet). - **void reset()** resets the library to start up conditions. - **bool isConnected()** checks if address is available on I2C bus. +- **uint8_t channelCount()** returns the number of channels = 16. ### Mode diff --git a/examples/PCA9685_allOFF_test/PCA9685_allOFF_test.ino b/examples/PCA9685_allOFF_test/PCA9685_allOFF_test.ino index 781f110..96998f7 100644 --- a/examples/PCA9685_allOFF_test/PCA9685_allOFF_test.ino +++ b/examples/PCA9685_allOFF_test/PCA9685_allOFF_test.ino @@ -27,7 +27,7 @@ void setup() Serial.println(); pinMode(PIN, INPUT_PULLUP); - for (int channel = 0; channel < 16; channel++) + for (int channel = 0; channel < PCA.channelCount(); channel++) { PCA.setPWM(channel, 0, 1000); } diff --git a/examples/PCA9685_digitalWrite_test/PCA9685_digitalWrite_test.ino b/examples/PCA9685_digitalWrite_test/PCA9685_digitalWrite_test.ino index 5fcdf77..a01195c 100644 --- a/examples/PCA9685_digitalWrite_test/PCA9685_digitalWrite_test.ino +++ b/examples/PCA9685_digitalWrite_test/PCA9685_digitalWrite_test.ino @@ -9,6 +9,8 @@ // to see the frequency of the PWM +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h" diff --git a/examples/PCA9685_maxPWM_test/PCA9685_maxPWM_test.ino b/examples/PCA9685_maxPWM_test/PCA9685_maxPWM_test.ino index a39d4f2..35d9eb5 100644 --- a/examples/PCA9685_maxPWM_test/PCA9685_maxPWM_test.ino +++ b/examples/PCA9685_maxPWM_test/PCA9685_maxPWM_test.ino @@ -9,6 +9,8 @@ // to see the frequency of the PWM +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h" diff --git a/examples/PCA9685_setFrequency_offset/PCA9685_setFrequency_offset.ino b/examples/PCA9685_setFrequency_offset/PCA9685_setFrequency_offset.ino index 3151d10..220dcc4 100644 --- a/examples/PCA9685_setFrequency_offset/PCA9685_setFrequency_offset.ino +++ b/examples/PCA9685_setFrequency_offset/PCA9685_setFrequency_offset.ino @@ -14,6 +14,8 @@ // Note: the higher the frequency, the more inaccurate the real frequency, +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h" diff --git a/examples/PCA9685_setFrequency_test/PCA9685_setFrequency_test.ino b/examples/PCA9685_setFrequency_test/PCA9685_setFrequency_test.ino index fa5517d..662bb07 100644 --- a/examples/PCA9685_setFrequency_test/PCA9685_setFrequency_test.ino +++ b/examples/PCA9685_setFrequency_test/PCA9685_setFrequency_test.ino @@ -9,6 +9,8 @@ // to see the frequency of the PWM +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h" @@ -35,8 +37,8 @@ void setup() pinMode(IRQ_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(IRQ_PIN), irq, RISING); // CHANGE - PCA.setFrequency(24); - PCA.setPWM(15, 0, 4095); // gives 2 changes per interval + PCA.setFrequency(PCA9685_MIN_FREQ); // == 24 + PCA.setPWM(15, 0, 4095); // gives 2 changes per interval Serial.println("\nSET\tGET\tGET%\tIRQ\tIRQ%"); } @@ -73,7 +75,7 @@ void loop() Serial.print("\n"); freq += 4; - if (freq >= 1526) freq = 24; + if (freq >= 1526) freq = 24; // page 25 datasheet; PCA9685_MAX_FREQ, PCA9685_MIN_FREQ PCA.setFrequency(freq); PCA.setPWM(15, 0, 4095); diff --git a/examples/PCA9685_test01/PCA9685_test01.ino b/examples/PCA9685_test01/PCA9685_test01.ino index 1666182..a2ee746 100644 --- a/examples/PCA9685_test01/PCA9685_test01.ino +++ b/examples/PCA9685_test01/PCA9685_test01.ino @@ -5,6 +5,8 @@ // PUPROSE: test PCA9685 library +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h" @@ -36,7 +38,7 @@ void testSetON() Serial.print(millis()); Serial.print("\t"); Serial.println("Test - setHIGH"); - for (uint8_t channel = 0; channel < 16; channel++) + for (uint8_t channel = 0; channel < ledArray.channelCount(); channel++) { ledArray.digitalWrite(channel, HIGH); } @@ -48,7 +50,7 @@ void testSetOFF() Serial.print(millis()); Serial.print("\t"); Serial.println("Test - setLOW"); - for (uint8_t channel = 0; channel < 16; channel++) + for (uint8_t channel = 0; channel < ledArray.channelCount(); channel++) { ledArray.digitalWrite(channel, LOW); } @@ -60,7 +62,7 @@ void testPWMMode() Serial.print(millis()); Serial.print("\t"); Serial.println("Test - setPwm getPWM"); - for (uint16_t channel = 0; channel < 16; channel++) + for (uint16_t channel = 0; channel < ledArray.channelCount(); channel++) { // every next line ~twice as much time ledArray.setPWM(channel, channel * 127, channel * 255); diff --git a/examples/PCA9685_test02/PCA9685_test02.ino b/examples/PCA9685_test02/PCA9685_test02.ino index 574102f..f7691ea 100644 --- a/examples/PCA9685_test02/PCA9685_test02.ino +++ b/examples/PCA9685_test02/PCA9685_test02.ino @@ -5,8 +5,11 @@ // PUPROSE: test PCA9685 library +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h" + PCA9685 ledArray(0x40); @@ -39,7 +42,7 @@ void testDigitalWrite(uint8_t mode) Serial.print(millis()); Serial.print("\t"); Serial.println(__FUNCTION__); - for (int channel = 0; channel < 16; channel++) + for (int channel = 0; channel < ledArray.channelCount(); channel++) { ledArray.digitalWrite(channel, mode); delay(100); @@ -73,7 +76,7 @@ void testPWMMode() Serial.print(millis()); Serial.print("\t"); Serial.println(__FUNCTION__); - for (uint16_t channel = 1; channel < 16; channel++) + for (uint16_t channel = 1; channel < ledArray.channelCount(); channel++) { // every next line ~twice as much time ledArray.setPWM(channel, channel * 127, channel * 255); diff --git a/keywords.txt b/keywords.txt index a474737..e858598 100644 --- a/keywords.txt +++ b/keywords.txt @@ -27,10 +27,13 @@ lastError KEYWORD2 # Constants (LITERAL1) PCA9685_LIB_VERSION LITERAL1 + PCA9685_OK LITERAL1 PCA9685_ERROR LITERAL1 PCA9685_ERR_CHANNEL LITERAL1 PCA9685_ERR_MODE LITERAL1 PCA9685_ERR_I2C LITERAL1 +PCA9685_MIN_FREQ LITERAL1 +PCA9685_MAX_FREQ LITERAL1 diff --git a/library.json b/library.json index 8474975..d9dc21c 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/PCA9685_RT.git" }, - "version": "0.3.3", + "version": "0.3.4", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index fb7edc0..1d658b6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=PCA9685_RT -version=0.3.3 +version=0.3.4 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for I2C PCA9685 16 channel PWM diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 25b0146..2e3ed91 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -31,6 +31,8 @@ #include +#include "Arduino.h" +#include "Wire.h" #include "PCA9685.h"