diff --git a/.arduino-ci.yml b/.arduino-ci.yml new file mode 100644 index 0000000..77a333f --- /dev/null +++ b/.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/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..90d9ab4 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: RobTillaart + diff --git a/.github/workflows/arduino-lint.yml b/.github/workflows/arduino-lint.yml new file mode 100644 index 0000000..8a26f14 --- /dev/null +++ b/.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/.github/workflows/arduino_test_runner.yml b/.github/workflows/arduino_test_runner.yml new file mode 100644 index 0000000..fadfa90 --- /dev/null +++ b/.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/.github/workflows/jsoncheck.yml b/.github/workflows/jsoncheck.yml new file mode 100644 index 0000000..37a1129 --- /dev/null +++ b/.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/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5add16e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# Change Log HC4051 + +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/HC4051.h b/HC4051.h new file mode 100644 index 0000000..1128070 --- /dev/null +++ b/HC4051.h @@ -0,0 +1,96 @@ +#pragma once +// +// FILE: HC4051.h +// AUTHOR: Rob Tillaart +// DATE: 2023-01-25 +// VERSION: 0.1.0 +// PURPOSE: Arduino library for CD74HC4051 8 channel multiplexer and compatibles. +// URL: https://github.com/RobTillaart/HC4051 + + + +#include "Arduino.h" + +#define HC4051_LIB_VERSION (F("0.1.0")) + + +class HC4051 +{ +public: + explicit HC4051(uint8_t A, uint8_t B, uint8_t C, uint8_t enablePin = 255) + { + _pins[0] = A; + _pins[1] = B; + _pins[2] = C; + uint8_t i = 3; + 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 & 0x07) != _channel) + { + _channel = channel & 0x07; + digitalWrite(_pins[0], _channel & 0x01); + digitalWrite(_pins[1], _channel & 0x02); + digitalWrite(_pins[2], _channel & 0x04); + } + } + + + 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/LICENSE b/LICENSE index 9833ff9..16ef155 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Rob Tillaart +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 diff --git a/README.md b/README.md index c3e6aaa..d23bcf3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,98 @@ + +[![Arduino CI](https://github.com/RobTillaart/HC4051/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![Arduino-lint](https://github.com/RobTillaart/HC4051/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/HC4051/actions/workflows/arduino-lint.yml) +[![JSON check](https://github.com/RobTillaart/HC4051/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/HC4051/actions/workflows/jsoncheck.yml) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/HC4051/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/HC4051.svg?maxAge=3600)](https://github.com/RobTillaart/HC4051/releases) + + # HC4051 -Arduino library for CD74HC4051 1x8 channel multiplexer and compatibles. + +HC4051 is an Arduino library for a HC4051 1x8 channel multiplexer. + + +## Description + +HC4051 is a library to control the CD74HC4051 8 channel +multiplexer / demultiplexer and compatible devices. + +The HC4051 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 HC4051 + +-------------+ +-------------+ + | | | | + | A |------------->| S0 Y0 | + | B |------------->| S1 Y1 | + | C |------------->| S2 Y2 | + | | | Y3 | + | E |------------->| INH Y4 | + | | | Y5 | + | A0 |<-------------| Y Y6 | + | | | Y7 | + | GND |--------------| GND Y8 | + | | | VCC | + | | | | + +-------------+ +-------------+ +``` + + +## Interface + +```cpp +#include "HC4051.h" +``` + +#### Core + +- **HC4051(uint8_t A, uint8_t B, uint8_t C, uint8_t enablePin = 255)** constructor. +Set the three 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..7, this value is not checked, only the lower 3 bits will be used. +- **uint8_t getChannel()** get current channel 0..7. + + +#### 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/examples/HC4051_demo/HC4051_demo.ino b/examples/HC4051_demo/HC4051_demo.ino new file mode 100644 index 0000000..2fe05f6 --- /dev/null +++ b/examples/HC4051_demo/HC4051_demo.ino @@ -0,0 +1,36 @@ +// +// FILE: HC4051_demo.ino +// AUTHOR: Rob Tillaart +// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer + + +#include "HC4051.h" + +HC4051 mp(4, 5, 6); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("HC4051 LIBRARY VERSION: "); + Serial.println(HC4051_LIB_VERSION); + Serial.println(); + + delay(1000); +} + + +void loop() +{ + for (uint8_t channel = 0; channel < 8; channel++) + { + mp.setChannel(channel); + Serial.println(analogRead(A0)); + delay(100); + } + Serial.println(); +} + + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..875e5c7 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,17 @@ +# Syntax Colouring Map For HC4051 + +# Data types (KEYWORD1) +HC4051 KEYWORD1 + +# Methods and Functions (KEYWORD2) +setChannel KEYWORD2 +getChannel KEYWORD2 +enable KEYWORD2 +disable KEYWORD2 +isEnabled KEYWORD2 + + +# Constants (LITERAL1) +HC4051_LIB_VERSION LITERAL1 + + diff --git a/library.json b/library.json new file mode 100644 index 0000000..310dff3 --- /dev/null +++ b/library.json @@ -0,0 +1,23 @@ +{ + "name": "HC4051", + "keywords": "", + "description": "Arduino library for a HC4051 1x8 channel multiplexer.", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/HC4051.git" + }, + "version": "0.1.0", + "license": "MIT", + "frameworks": "arduino", + "platforms": "*", + "headers": "HC4051.h" +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..a42fc54 --- /dev/null +++ b/library.properties @@ -0,0 +1,11 @@ +name=HC4051 +version=0.1.0 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Arduino library for a HC4051 1x8 channel multiplexer +paragraph= +category=Sensors +url=https://github.com/RobTillaart/HC4051 +architectures=* +includes=HC4051.h +depends= diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp new file mode 100644 index 0000000..78af02e --- /dev/null +++ b/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 HC4051 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 "HC4051.h" + + +unittest_setup() +{ + fprintf(stderr, "HC4051_LIB_VERSION: %s\n", (char *) HC4051_LIB_VERSION); +} + + +unittest_teardown() +{ +} + + +unittest(test_channel) +{ + HC4051 mp(3,4,5); + + for (int ch = 0; ch < 8; ch++) + { + mp.setChannel(ch); + assertEqual(ch, mp.getChannel()); + } +} + + +unittest(test_enable) +{ + HC4051 mp1(3,4,5); + assertTrue(mp1.isEnabled()); + + HC4051 mp2(3,4,5,6); + assertFalse(mp2.isEnabled()); + + mp2.enable(); + assertTrue(mp2.isEnabled()); + + mp2.disable(); + assertFalse(mp2.isEnabled()); +} + + +unittest_main() + + +// -- END OF FILE --