0.1.0 HC4052

This commit is contained in:
rob tillaart 2023-01-26 12:34:18 +01:00
parent 8237f027bd
commit 265fc6086b
14 changed files with 461 additions and 0 deletions

View File

@ -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

4
libraries/HC4052/.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: RobTillaart

View File

@ -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

View File

@ -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

View File

@ -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$"

View File

@ -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

94
libraries/HC4052/HC4052.h Normal file
View File

@ -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 --

21
libraries/HC4052/LICENSE Normal file
View File

@ -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.

View File

@ -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.

View File

@ -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 --

View File

@ -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

View File

@ -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"
}

View File

@ -0,0 +1,11 @@
name=HC4052
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for a HC4052 2x4 channel multiplexer
paragraph=
category=Sensors
url=https://github.com/RobTillaart/HC4052
architectures=*
includes=HC4052.h
depends=

View File

@ -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 <ArduinoUnitTests.h>
#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 --