From 265fc6086bb55984b2230c8354fc495b05fa5906 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Thu, 26 Jan 2023 12:34:18 +0100 Subject: [PATCH] 0.1.0 HC4052 --- libraries/HC4052/.arduino-ci.yml | 28 ++++++ libraries/HC4052/.github/FUNDING.yml | 4 + .../HC4052/.github/workflows/arduino-lint.yml | 13 +++ .../.github/workflows/arduino_test_runner.yml | 17 ++++ .../HC4052/.github/workflows/jsoncheck.yml | 18 ++++ libraries/HC4052/CHANGELOG.md | 11 +++ libraries/HC4052/HC4052.h | 94 ++++++++++++++++++ libraries/HC4052/LICENSE | 21 ++++ libraries/HC4052/README.md | 98 +++++++++++++++++++ .../examples/HC4052_demo/HC4052_demo.ino | 36 +++++++ libraries/HC4052/keywords.txt | 17 ++++ libraries/HC4052/library.json | 23 +++++ libraries/HC4052/library.properties | 11 +++ libraries/HC4052/test/unit_test_001.cpp | 70 +++++++++++++ 14 files changed, 461 insertions(+) create mode 100644 libraries/HC4052/.arduino-ci.yml create mode 100644 libraries/HC4052/.github/FUNDING.yml create mode 100644 libraries/HC4052/.github/workflows/arduino-lint.yml create mode 100644 libraries/HC4052/.github/workflows/arduino_test_runner.yml create mode 100644 libraries/HC4052/.github/workflows/jsoncheck.yml create mode 100644 libraries/HC4052/CHANGELOG.md create mode 100644 libraries/HC4052/HC4052.h create mode 100644 libraries/HC4052/LICENSE create mode 100644 libraries/HC4052/README.md create mode 100644 libraries/HC4052/examples/HC4052_demo/HC4052_demo.ino create mode 100644 libraries/HC4052/keywords.txt create mode 100644 libraries/HC4052/library.json create mode 100644 libraries/HC4052/library.properties create mode 100644 libraries/HC4052/test/unit_test_001.cpp diff --git a/libraries/HC4052/.arduino-ci.yml b/libraries/HC4052/.arduino-ci.yml new file mode 100644 index 00000000..77a333f9 --- /dev/null +++ b/libraries/HC4052/.arduino-ci.yml @@ -0,0 +1,28 @@ +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/HC4052/.github/FUNDING.yml b/libraries/HC4052/.github/FUNDING.yml new file mode 100644 index 00000000..90d9ab4c --- /dev/null +++ b/libraries/HC4052/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: RobTillaart + diff --git a/libraries/HC4052/.github/workflows/arduino-lint.yml b/libraries/HC4052/.github/workflows/arduino-lint.yml new file mode 100644 index 00000000..8a26f14a --- /dev/null +++ b/libraries/HC4052/.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/HC4052/.github/workflows/arduino_test_runner.yml b/libraries/HC4052/.github/workflows/arduino_test_runner.yml new file mode 100644 index 00000000..fadfa904 --- /dev/null +++ b/libraries/HC4052/.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/HC4052/.github/workflows/jsoncheck.yml b/libraries/HC4052/.github/workflows/jsoncheck.yml new file mode 100644 index 00000000..37a11298 --- /dev/null +++ b/libraries/HC4052/.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/HC4052/CHANGELOG.md b/libraries/HC4052/CHANGELOG.md new file mode 100644 index 00000000..5c9fed31 --- /dev/null +++ b/libraries/HC4052/CHANGELOG.md @@ -0,0 +1,11 @@ +# Change Log HC4052 + +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-01-25 +- initial version + diff --git a/libraries/HC4052/HC4052.h b/libraries/HC4052/HC4052.h new file mode 100644 index 00000000..7e0cea14 --- /dev/null +++ b/libraries/HC4052/HC4052.h @@ -0,0 +1,94 @@ +#pragma once +// +// FILE: HC4052.h +// AUTHOR: Rob Tillaart +// DATE: 2023-01-25 +// VERSION: 0.1.0 +// PURPOSE: Arduino library for HC4052 2x4 channel multiplexer and compatibles. +// URL: https://github.com/RobTillaart/HC4052 + + + +#include "Arduino.h" + +#define HC4052_LIB_VERSION (F("0.1.0")) + + +class HC4052 +{ +public: + explicit HC4052(uint8_t A, uint8_t B, uint8_t enablePin = 255) + { + _pins[0] = A; + _pins[1] = B; + uint8_t i = 2; + while (i--) + { + pinMode(_pins[i], OUTPUT); + digitalWrite(_pins[i], LOW); + } + _channel = 0; + + if (enablePin != 255) + { + _enablePin = enablePin; + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, HIGH); + } + } + + + void setChannel(uint8_t channel) + { + if ((channel & 0x03) != _channel) + { + _channel = channel & 0x03; + digitalWrite(_pins[0], _channel & 0x01); + digitalWrite(_pins[1], _channel & 0x02); + } + } + + + uint8_t getChannel() + { + return _channel; + } + + + void enable() + { + if (_enablePin != 255) + { + digitalWrite(_enablePin, LOW); + } + } + + + void disable() + { + if (_enablePin != 255) + { + digitalWrite(_enablePin, HIGH); + } + } + + + bool isEnabled() + { + if (_enablePin != 255) + { + return (digitalRead(_enablePin) == LOW); + } + return true; + } + + +private: + uint8_t _pins[3]; + uint8_t _enablePin = 255; + uint8_t _channel = 0; +}; + + +// -- END OF FILE -- + diff --git a/libraries/HC4052/LICENSE b/libraries/HC4052/LICENSE new file mode 100644 index 00000000..16ef1551 --- /dev/null +++ b/libraries/HC4052/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/HC4052/README.md b/libraries/HC4052/README.md new file mode 100644 index 00000000..2a67afe2 --- /dev/null +++ b/libraries/HC4052/README.md @@ -0,0 +1,98 @@ + +[![Arduino CI](https://github.com/RobTillaart/HC4052/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![Arduino-lint](https://github.com/RobTillaart/HC4052/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/HC4052/actions/workflows/arduino-lint.yml) +[![JSON check](https://github.com/RobTillaart/HC4052/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/HC4052/actions/workflows/jsoncheck.yml) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/HC4052/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/HC4052.svg?maxAge=3600)](https://github.com/RobTillaart/HC4052/releases) + + +# HC4052 + +HC4052 is an Arduino library for a HC4052 2 x 4 channel multiplexer. + + +## Description + +HC4052 is a library to control the CD74HC4052 8 channel +multiplexer / demultiplexer and compatible devices. + +The HC4052 allows e.g one analog port read up to 8 different analog channels, +or one digital port to read the state of 8 buttons. + + +The channel selection is done with four select lines **A, B, C** + +The device can be enabled/disabled by the enable line **INH** + + +#### Compatibles + +to elaborate. + + +#### Related to + +- https://github.com/RobTillaart/HC4051 (1x8 mux) +- https://github.com/RobTillaart/HC4052 (2x8 mux) +- https://github.com/RobTillaart/HC4053 (3x2 mux) +- https://github.com/RobTillaart/HC4067 (1x16 mux) + + +## Hardware connection + +Typical connection is to connect the four **select pins** to four IO Pins of your board. + +The optional **enablePin (INH)** must be connected to GND if not used. +This way the device is continuous enabled. + +Example multiplexing analog in. + +``` + processor HC4052 + +-------------+ +-------------+ + | | | | + | A |------------->| A X0 | + | B |------------->| B X1 | + | enable |------------->| INH X2 | + | | | X3 | + | analog in |<-------------| X | + | | | | + | analog in |<-------------| Y Y0 | + | | | Y1 | + | GND |--------------| GND Y2 | + | | | VCC Y3 | + | | | | + +-------------+ +-------------+ +``` + + +## Interface + +```cpp +#include "HC4052.h" +``` + +#### Core + +- **HC4052(uint8_t A, uint8_t B, uint8_t enablePin = 255)** constructor. +Set the two select pins and optional the enable pin. +If the enablePin == 255 it is considered not used. +- **void setChannel(uint8_t channel)** set the current channel. +Valid values 0..3, this value is not checked, only the lower 2 bits will be used. +- **uint8_t getChannel()** get current channel 0..3. + + +#### Enable + +These functions work only if enablePin is set in the constructor. + +- **void enable()** idem. +- **void disable()** idem. +- **bool isEnabled()** idem. +Also returns true if enablePin is not set. + + +## Future + +- keep in sync with HC4067 et.al. + diff --git a/libraries/HC4052/examples/HC4052_demo/HC4052_demo.ino b/libraries/HC4052/examples/HC4052_demo/HC4052_demo.ino new file mode 100644 index 00000000..25857065 --- /dev/null +++ b/libraries/HC4052/examples/HC4052_demo/HC4052_demo.ino @@ -0,0 +1,36 @@ +// +// FILE: HC4052_demo.ino +// AUTHOR: Rob Tillaart +// PURPOSE: Demo for HC4052 2x4 channel (simple) multiplexer + + +#include "HC4052.h" + +HC4052 mp(4, 5, 6); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("HC4052 LIBRARY VERSION: "); + Serial.println(HC4052_LIB_VERSION); + Serial.println(); + + delay(1000); +} + + +void loop() +{ + for (uint8_t channel = 0; channel < 4; channel++) + { + mp.setChannel(channel); + Serial.println(analogRead(A0)); + delay(100); + } + Serial.println(); +} + + +// -- END OF FILE -- diff --git a/libraries/HC4052/keywords.txt b/libraries/HC4052/keywords.txt new file mode 100644 index 00000000..c289eb54 --- /dev/null +++ b/libraries/HC4052/keywords.txt @@ -0,0 +1,17 @@ +# Syntax Colouring Map For HC4052 + +# Data types (KEYWORD1) +HC4052 KEYWORD1 + +# Methods and Functions (KEYWORD2) +setChannel KEYWORD2 +getChannel KEYWORD2 +enable KEYWORD2 +disable KEYWORD2 +isEnabled KEYWORD2 + + +# Constants (LITERAL1) +HC4052_LIB_VERSION LITERAL1 + + diff --git a/libraries/HC4052/library.json b/libraries/HC4052/library.json new file mode 100644 index 00000000..e70f5822 --- /dev/null +++ b/libraries/HC4052/library.json @@ -0,0 +1,23 @@ +{ + "name": "HC4052", + "keywords": "", + "description": "Arduino library for a HC4052 2x4 channel multiplexer.", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/HC4052.git" + }, + "version": "0.1.0", + "license": "MIT", + "frameworks": "arduino", + "platforms": "*", + "headers": "HC4052.h" +} diff --git a/libraries/HC4052/library.properties b/libraries/HC4052/library.properties new file mode 100644 index 00000000..0d21f0ce --- /dev/null +++ b/libraries/HC4052/library.properties @@ -0,0 +1,11 @@ +name=HC4052 +version=0.1.0 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Arduino library for a HC4052 2x4 channel multiplexer +paragraph= +category=Sensors +url=https://github.com/RobTillaart/HC4052 +architectures=* +includes=HC4052.h +depends= diff --git a/libraries/HC4052/test/unit_test_001.cpp b/libraries/HC4052/test/unit_test_001.cpp new file mode 100644 index 00000000..b17d9766 --- /dev/null +++ b/libraries/HC4052/test/unit_test_001.cpp @@ -0,0 +1,70 @@ +// +// FILE: unit_test_001.cpp +// AUTHOR: Rob Tillaart +// DATE: 2023-01-25 +// PURPOSE: unit tests for the HC4052 sensor +// https://github.com/RobTillaart/HC4067 +// + +// supported assertions +// ---------------------------- +// assertEqual(expected, actual) +// assertNotEqual(expected, actual) +// assertLess(expected, actual) +// assertMore(expected, actual) +// assertLessOrEqual(expected, actual) +// assertMoreOrEqual(expected, actual) +// assertTrue(actual) +// assertFalse(actual) +// assertNull(actual) + + +#include + +#include "Arduino.h" +#include "HC4052.h" + + +unittest_setup() +{ + fprintf(stderr, "HC4052_LIB_VERSION: %s\n", (char *) HC4052_LIB_VERSION); +} + + +unittest_teardown() +{ +} + + +unittest(test_channel) +{ + HC4052 mp(3, 4); + + for (int ch = 0; ch < 4; ch++) + { + mp.setChannel(ch); + assertEqual(ch, mp.getChannel()); + } +} + + +unittest(test_enable) +{ + HC4052 mp1(3, 4); + assertTrue(mp1.isEnabled()); + + HC4052 mp2(3, 4, 5); + assertFalse(mp2.isEnabled()); + + mp2.enable(); + assertTrue(mp2.isEnabled()); + + mp2.disable(); + assertFalse(mp2.isEnabled()); +} + + +unittest_main() + + +// -- END OF FILE --