From 83c7c1a07018843d3a626d1d79c3cd68b4c311d5 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Mon, 31 Jul 2023 19:23:13 +0200 Subject: [PATCH] 0.1.0 PT2314 --- libraries/PT2314/.arduino-ci.yml | 27 ++ libraries/PT2314/.github/FUNDING.yml | 4 + .../PT2314/.github/workflows/arduino-lint.yml | 13 + .../.github/workflows/arduino_test_runner.yml | 17 ++ .../PT2314/.github/workflows/jsoncheck.yml | 18 ++ libraries/PT2314/CHANGELOG.md | 11 + libraries/PT2314/LICENSE | 21 ++ libraries/PT2314/PT2314.cpp | 249 ++++++++++++++++++ libraries/PT2314/PT2314.h | 86 ++++++ libraries/PT2314/README.md | 104 ++++++++ .../examples/PT2314_demo/PT2314_demo.ino | 32 +++ libraries/PT2314/keywords.txt | 42 +++ libraries/PT2314/library.json | 23 ++ libraries/PT2314/library.properties | 12 + libraries/PT2314/test/unit_test_001.cpp | 75 ++++++ 15 files changed, 734 insertions(+) create mode 100644 libraries/PT2314/.arduino-ci.yml create mode 100644 libraries/PT2314/.github/FUNDING.yml create mode 100644 libraries/PT2314/.github/workflows/arduino-lint.yml create mode 100644 libraries/PT2314/.github/workflows/arduino_test_runner.yml create mode 100644 libraries/PT2314/.github/workflows/jsoncheck.yml create mode 100644 libraries/PT2314/CHANGELOG.md create mode 100644 libraries/PT2314/LICENSE create mode 100644 libraries/PT2314/PT2314.cpp create mode 100644 libraries/PT2314/PT2314.h create mode 100644 libraries/PT2314/README.md create mode 100644 libraries/PT2314/examples/PT2314_demo/PT2314_demo.ino create mode 100644 libraries/PT2314/keywords.txt create mode 100644 libraries/PT2314/library.json create mode 100644 libraries/PT2314/library.properties create mode 100644 libraries/PT2314/test/unit_test_001.cpp diff --git a/libraries/PT2314/.arduino-ci.yml b/libraries/PT2314/.arduino-ci.yml new file mode 100644 index 00000000..25980972 --- /dev/null +++ b/libraries/PT2314/.arduino-ci.yml @@ -0,0 +1,27 @@ +platforms: + rpipico: + board: rp2040:rp2040:rpipico + package: rp2040:rp2040 + gcc: + features: + defines: + - ARDUINO_ARCH_RP2040 + warnings: + flags: + +packages: + rp2040:rp2040: + url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json + +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + # - due + # - zero + # - leonardo + - m4 + - esp32 + # - esp8266 + # - mega2560 + - rpipico diff --git a/libraries/PT2314/.github/FUNDING.yml b/libraries/PT2314/.github/FUNDING.yml new file mode 100644 index 00000000..90d9ab4c --- /dev/null +++ b/libraries/PT2314/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: RobTillaart + diff --git a/libraries/PT2314/.github/workflows/arduino-lint.yml b/libraries/PT2314/.github/workflows/arduino-lint.yml new file mode 100644 index 00000000..8a26f14a --- /dev/null +++ b/libraries/PT2314/.github/workflows/arduino-lint.yml @@ -0,0 +1,13 @@ + +name: Arduino-lint + +on: [push, pull_request] +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: arduino/arduino-lint-action@v1 + with: + library-manager: update + compliance: strict diff --git a/libraries/PT2314/.github/workflows/arduino_test_runner.yml b/libraries/PT2314/.github/workflows/arduino_test_runner.yml new file mode 100644 index 00000000..fadfa904 --- /dev/null +++ b/libraries/PT2314/.github/workflows/arduino_test_runner.yml @@ -0,0 +1,17 @@ +--- +name: Arduino CI + +on: [push, pull_request] + +jobs: + runTest: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.6 + - run: | + gem install arduino_ci + arduino_ci.rb diff --git a/libraries/PT2314/.github/workflows/jsoncheck.yml b/libraries/PT2314/.github/workflows/jsoncheck.yml new file mode 100644 index 00000000..37a11298 --- /dev/null +++ b/libraries/PT2314/.github/workflows/jsoncheck.yml @@ -0,0 +1,18 @@ +name: JSON check + +on: + push: + paths: + - '**.json' + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: json-syntax-check + uses: limitusus/json-syntax-check@v1 + with: + pattern: "\\.json$" + diff --git a/libraries/PT2314/CHANGELOG.md b/libraries/PT2314/CHANGELOG.md new file mode 100644 index 00000000..99d5a35c --- /dev/null +++ b/libraries/PT2314/CHANGELOG.md @@ -0,0 +1,11 @@ +# Change Log PT2314 + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + + +## [0.1.0] - 2023-07-30 +- initial version. + diff --git a/libraries/PT2314/LICENSE b/libraries/PT2314/LICENSE new file mode 100644 index 00000000..16ef1551 --- /dev/null +++ b/libraries/PT2314/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023-2023 Rob Tillaart + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libraries/PT2314/PT2314.cpp b/libraries/PT2314/PT2314.cpp new file mode 100644 index 00000000..9e28e197 --- /dev/null +++ b/libraries/PT2314/PT2314.cpp @@ -0,0 +1,249 @@ +// +// FILE: PT2314.cpp +// AUTHOR: Rob Tillaart +// DATE: 2023-07-30 +// VERSION: 0.1.0 +// PURPOSE: Arduino library for PT2314 i2C 4 channel audio processor. +// URL: https://github.com/RobTillaart/PT2314 + + +#include "PT2314.h" + +PT2314::PT2314(TwoWire *wire) +{ + _wire = wire; +} + + +#if defined (ESP8266) || defined(ESP32) +bool PT2314::begin(int dataPin, int clockPin) +{ + _wire = &Wire; + if ((dataPin < 255) && (clockPin < 255)) + { + _wire->begin(dataPin, clockPin); + } else { + _wire->begin(); + } + if (! isConnected()) return false; + setChannel(); + setMute(); + setLoudness(); + setVolume(); + setBass(); + setTreble(); + setGain(); + // setAttn(31,31); // already muted + return true; +} +#endif + + +bool PT2314::begin() +{ + _wire->begin(); + if (! isConnected()) return false; + setChannel(); + setMute(); + setLoudness(); + setVolume(); + setBass(); + setTreble(); + setGain(); + // setAttn(31,31); // already muted + return true; +} + + +bool PT2314::isConnected() +{ + _wire->beginTransmission(_address); + return ( _wire->endTransmission() == 0); +} + + +void PT2314::setChannel(uint8_t channel) +{ + if (channel > 3) channel = 3; + _channel = channel; + updateAudioRegister(); +} + +uint8_t PT2314::getChannel() +{ + return _channel; +} + + +/////////////////////////////////////////////////// +// +// AUDIO +// +void PT2314::setMute(bool on) +{ + _mute = on; + if (_mute) + { + write(0xDF); // left + write(0xFF); // right + } + else + { + write(0xC0 | _attnLeft); + write(0xE0 | _attnRight); + } +} + + +bool PT2314::getMute() +{ + return _mute; +} + + +void PT2314::setLoudness(bool on) +{ + _loudness = on; + updateAudioRegister(); +} + + +bool PT2314::getLoudness() +{ + return _loudness; +} + + +void PT2314::setVolume(uint8_t volume) +{ + if (volume > 63) volume = 63; + _volume = volume; + write(_volume); +} + + +uint8_t PT2314::getVolume() +{ + return _volume; +} + + +void PT2314::setBass(int8_t bass) +{ + if (bass < -14) bass = -14; + if (bass > 14) bass = 14; + _bass = bass; + uint8_t value; + if (_bass <= 0) value = 7 + (_bass / 2); + else value = 15 - (_bass / 2); + write(0x60 | value); +} + + +int8_t PT2314::getBass() +{ + return _bass; +} + + +void PT2314::setTreble(int8_t treble) +{ + if (treble < -14) treble = -14; + if (treble > 14) treble = 14; + _treble = treble; + uint8_t value; + if (_treble <= 0) value = 7 + (_treble / 2); + else value = 15 - (_treble / 2); + write(0x70 | value); +} + + +int8_t PT2314::getTreble() +{ + return _treble; +} + + +void PT2314::setGain(uint8_t gain) +{ + if (gain > 3) gain = 3; + _gain = gain << 3; + updateAudioRegister(); +} + + +uint8_t PT2314::getGain() +{ + return _gain >> 3; +} + + +void PT2314::setAttnLeft(uint8_t attnLeft) +{ + if (attnLeft > 31) attnLeft = 31; + _attnLeft = attnLeft; + write(0xC0 | _attnLeft); +} + + +uint8_t PT2314::getAttnLeft() +{ + return _attnLeft; +} + + +void PT2314::setAttnRight(uint8_t attnRight) +{ + if (attnRight > 31) attnRight = 31; + _attnRight = attnRight; + write(0xE0 | _attnRight); +} + + +uint8_t PT2314::getAttnRight() +{ + return _attnRight; +} + + +void PT2314::setAttn(uint8_t attnLeft, uint8_t attnRight) +{ + setAttnLeft(attnLeft); + setAttnRight(attnRight); +} + + + +/////////////////////////////////////////////////// +// +// PROTECTED +// +void PT2314::write(const uint8_t value) +{ + _wire->beginTransmission(_address); + _wire->write(value); + _error = _wire->endTransmission(); +} + + +void PT2314::updateAudioRegister() +{ + uint8_t value = 0x40; + value |= _channel; + if (_loudness == false) value |= 0x04; + value |= _gain; + write(value); +} + + + +/////////////////////////////////////////////////// +// +// DERIVED CLASS +// +PT7314::PT7314(TwoWire *wire) : PT2314(wire) +{ +}; + +// -- END OF FILE -- + diff --git a/libraries/PT2314/PT2314.h b/libraries/PT2314/PT2314.h new file mode 100644 index 00000000..3299f1ed --- /dev/null +++ b/libraries/PT2314/PT2314.h @@ -0,0 +1,86 @@ +#pragma once +// +// FILE: PT2314.h +// AUTHOR: Rob Tillaart +// DATE: 2023-07-30 +// VERSION: 0.1.0 +// PURPOSE: Arduino library for PT2314 i2C 4 channel audio processor. +// URL: https://github.com/RobTillaart/PT2314 + + +#include "Arduino.h" +#include "Wire.h" + + +#define PT2314_LIB_VERSION (F("0.1.0")) + + +class PT2314 +{ +public: + PT2314(TwoWire *wire = &Wire); + +#if defined (ESP8266) || defined(ESP32) + bool begin(int sda, int scl); +#endif + bool begin(); + bool isConnected(); + + // AUDIO + void setChannel(uint8_t channel = 0); // 0..3 + uint8_t getChannel(); + + void setMute(bool on = true); + bool getMute(); + void setLoudness(bool on = true); + bool getLoudness(); + + void setVolume(uint8_t volume = 0); // 0..63 + uint8_t getVolume(); + void setBass(int8_t bass = 0); // -14..14 + int8_t getBass(); + void setTreble(int8_t treble = 0); // -14..14 + int8_t getTreble(); + + void setGain(uint8_t gain = 0); // 0..3 + uint8_t getGain(); + + void setAttnLeft(uint8_t value = 31); // 0..31 + uint8_t getAttnLeft(); + void setAttnRight(uint8_t value = 31); // 0..31 + uint8_t getAttnRight(); + void setAttn(uint8_t attnLeft, uint8_t attnRight); + + +protected: + void write(const uint8_t value); + void updateAudioRegister(); + + TwoWire *_wire; + uint8_t _address = 0x44; + + uint8_t _channel = 0; + bool _mute = false; + bool _loudness = false; + uint8_t _volume = 0; + int8_t _bass = 0; + int8_t _treble = 0; + uint8_t _gain = 0; + uint8_t _attnLeft = 0; + uint8_t _attnRight = 0; + // to be elaborated. + int _error = 0; +}; + +/////////////////////////////////////////////////////////////// +// +// DERIVED +// +class PT7314 : public PT2314 +{ +public: + PT7314(TwoWire *wire = &Wire); +}; + +// -- END OF FILE -- + diff --git a/libraries/PT2314/README.md b/libraries/PT2314/README.md new file mode 100644 index 00000000..bf55933e --- /dev/null +++ b/libraries/PT2314/README.md @@ -0,0 +1,104 @@ + +[![Arduino CI](https://github.com/RobTillaart/PT2314/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![JSON check](https://github.com/RobTillaart/PT2314/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PT2314/actions/workflows/jsoncheck.yml) +[![Arduino-lint](https://github.com/RobTillaart/PT2314/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PT2314/actions/workflows/arduino-lint.yml) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PT2314/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/PT2314.svg?maxAge=3600)](https://github.com/RobTillaart/PT2314/releases) + + +# PT2314 + +Arduino library for PT2314 i2C 4 channel audio processor. + +## Description + +The PT2314 library is experimental as it is not tested with hardware. +The library should also work with the PT7314, a separate constructor +exist for it. + +Feedback is welcome. + +The library is based upon Datasheet: PT2314 V1.4 July, 2005 + + +#### I2C + +According to the datasheet: +- PT2314 only work on 100 KHz. +- PT7314 can support 400 KHz. +- check if higher speeds work? + +Datasheet PT7314 states that the device needs 50 ms to wake up +before it can process I2C commands. +The datasheet of the PT2314 does not mention such timeout. + + +## Interface + +```cpp +#include "PT2314.h" +``` + +#### Constructors + +- **PT2314(TwoWire \*wire = &Wire)** constructor. +- **PT7314(TwoWire \*wire = &Wire)** constructor. +- **bool begin(int sda, int scl)** for ESP32 et al. +- **bool begin()** other +- **bool isConnected()** device address can be seen on I2C bus. + +#### Channel + +- **void setChannel(uint8_t channel = 0)** 0..3 +- **uint8_t getChannel()** + +#### Volume + +- **void setMute(bool on = true)** +- **bool getMute()** +- **void setLoudness(bool on = true)** +- **bool getLoudness()** +- **void setVolume(uint8_t volume = 0)** 0..63 +- **uint8_t getVolume()** + +#### Bass Treble + +- **void setBass(int8_t bass = 0)** -14..14 dB +- **int8_t getBass()** +- **void setTreble(int8_t treble = 0)** -14..14 dB +- **int8_t getTreble()** + +#### Gain + +- **void setGain(uint8_t gain = 0)** 0..3 +- **uint8_t getGain()** + +#### Attenuation + +- **void setAttnLeft(uint8_t value = 31)** 0..31 +- **uint8_t getAttnLeft()** +- **void setAttnRight(uint8_t value = 31)** 0..31 +- **uint8_t getAttnRight()** +- **void setAttn(uint8_t attnLeft, uint8_t attnRight)** + +## Future + +#### Must + +- improve documentation +- test with hardware + +#### Should + +- error handling +- add more examples. +- check parameters range (0..63 => -63..0? + - some are in dB others not. + +#### Could + +- replace magic numbers with defines. +- extend unit test + +#### Wont (unless on request) + diff --git a/libraries/PT2314/examples/PT2314_demo/PT2314_demo.ino b/libraries/PT2314/examples/PT2314_demo/PT2314_demo.ino new file mode 100644 index 00000000..399a663f --- /dev/null +++ b/libraries/PT2314/examples/PT2314_demo/PT2314_demo.ino @@ -0,0 +1,32 @@ +// +// FILE: PT2314_demo.ino +// AUTHOR: Rob Tillaart +// PURPOSE: simple demo +// URL: https://github.com/RobTillaart/PT2314 + + +#include "PT2314.h" + +PT7314 pt; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + delay(100); + + pt.begin(); + + // do your settings + pt.setVolume(20); + pt.setAttn(15, 24); // shift the balance + pt.setLoudness(true); +} + + +void loop() +{ +} + +// -- END OF FILE -- diff --git a/libraries/PT2314/keywords.txt b/libraries/PT2314/keywords.txt new file mode 100644 index 00000000..f5907ef0 --- /dev/null +++ b/libraries/PT2314/keywords.txt @@ -0,0 +1,42 @@ +# Syntax Colouring Map For PT2314 + +# Data types (KEYWORD1) +PT2314 KEYWORD1 +PT7314 KEYWORD1 + + +# Methods and Functions (KEYWORD2) +begin KEYWORD2 +isConnected KEYWORD2 + +setChannel KEYWORD2 +getChannel KEYWORD2 + +setMute KEYWORD2 +getMute KEYWORD2 +setLoudness KEYWORD2 +getLoudness KEYWORD2 + +setVolume KEYWORD2 +getVolume KEYWORD2 +setBass KEYWORD2 +getBass KEYWORD2 +setTreble KEYWORD2 +getTreble KEYWORD2 + +setGain KEYWORD2 +getGain KEYWORD2 + +setAttnLeft KEYWORD2 +getAttnLeft KEYWORD2 +setAttnRight KEYWORD2 +getAttnRight KEYWORD2 +setAttn KEYWORD2 + + +# Constants (LITERAL1) +PT2314_LIB_VERSION LITERAL1 + + + + diff --git a/libraries/PT2314/library.json b/libraries/PT2314/library.json new file mode 100644 index 00000000..6d77875f --- /dev/null +++ b/libraries/PT2314/library.json @@ -0,0 +1,23 @@ +{ + "name": "PT2314", + "keywords": "PT2314, PT7314, I2C", + "description": "Arduino library for PT2314 i2C 4 channel audio processor.", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/PT2314.git" + }, + "version": "0.1.0", + "license": "MIT", + "frameworks": "*", + "platforms": "*", + "headers": "PT2314.h" +} diff --git a/libraries/PT2314/library.properties b/libraries/PT2314/library.properties new file mode 100644 index 00000000..3762335a --- /dev/null +++ b/libraries/PT2314/library.properties @@ -0,0 +1,12 @@ +name=PT2314 +version=0.1.0 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Arduino library for PT2314 i2C 4 channel audio processor, +paragraph=PT7314 +category=Sensors +url=https://github.com/RobTillaart/PT2314 +architectures=* +includes=PT2314.h +depends= + diff --git a/libraries/PT2314/test/unit_test_001.cpp b/libraries/PT2314/test/unit_test_001.cpp new file mode 100644 index 00000000..e5a8f71e --- /dev/null +++ b/libraries/PT2314/test/unit_test_001.cpp @@ -0,0 +1,75 @@ +// +// FILE: unit_test_001.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// DATE: 2023-07-30 +// PURPOSE: unit tests for the PT2314 +// https://github.com/RobTillaart/PT2314 +// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md +// + +// supported assertions +// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42 +// ---------------------------- +// assertEqual(expected, actual); // a == b +// assertNotEqual(unwanted, actual); // a != b +// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b)) +// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b)) +// assertLess(upperBound, actual); // a < b +// assertMore(lowerBound, actual); // a > b +// assertLessOrEqual(upperBound, actual); // a <= b +// assertMoreOrEqual(lowerBound, actual); // a >= b +// assertTrue(actual); +// assertFalse(actual); +// assertNull(actual); + +// // special cases for floats +// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon +// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon +// assertInfinity(actual); // isinf(a) +// assertNotInfinity(actual); // !isinf(a) +// assertNAN(arg); // isnan(a) +// assertNotNAN(arg); // !isnan(a) + + + +//////////////////////////////////// +// +// MANY TESTS WILL BLOCK AS BUILD CI HAS NO GOOD TIMEOUT +// ALL FAILING TESTS ARE COMMENTED +// +// USE GODMODE SERIAL TO IMPROVE THESE TESTS +// + +#include + +#include "Arduino.h" +#include "PT2314.h" + + +unittest_setup() +{ + fprintf(stderr, "PT2314_LIB_VERSION:\t%s\n", (char *) PT2314_LIB_VERSION); +} + + +unittest_teardown() +{ +} + + +unittest(test_constructor) +{ + PT2314 pt2; + PT7314 pt7; + + assertEqual(1,1); +} + + +unittest_main() + + +// -- END OF FILE -- + +