0.1.0 AD5245

This commit is contained in:
rob tillaart 2022-08-03 11:00:56 +02:00
parent 37954b5b62
commit 1e12430eea
16 changed files with 643 additions and 0 deletions

View File

@ -0,0 +1,11 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- 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@v2
- 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@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

113
libraries/AD5245/AD5245.cpp Normal file
View File

@ -0,0 +1,113 @@
//
// FILE: AD5245.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Arduino library for I2C digital potentiometer AD5245.
// DATE: 2022-07-31
// URL: https://github.com/RobTillaart/AD5245
//
// HISTORY
// 2022-07-31 0.1.1 initial version (stripped from AD524X)
#include "AD5245.h"
#define AD5245_WRITE 0x00
#define AD5245_RESET 0x40
#define AD5245_SHUTDOWN 0x20
AD5245::AD5245(const uint8_t address, TwoWire *wire)
{
// address: 0x010110x = 0x2C - 0x2D
_address = address;
_wire = wire;
_lastValue = 128; // power on reset => mid position
}
#if defined (ESP8266) || defined(ESP32)
bool AD5245::begin(uint8_t dataPin, uint8_t clockPin)
{
_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
if (! isConnected()) return false;
reset();
return true;
}
#endif
bool AD5245::begin()
{
_wire->begin();
if (! isConnected()) return false;
reset();
return true;
}
bool AD5245::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t AD5245::reset()
{
uint8_t cmd = AD5245_RESET;
_lastValue = 128;
return send(cmd, _lastValue);
}
uint8_t AD5245::write(const uint8_t value)
{
uint8_t cmd = AD5245_WRITE;
_lastValue = value;
return send(cmd, value);
}
uint8_t AD5245::read()
{
return _lastValue;
}
uint8_t AD5245::readDevice()
{
Wire.requestFrom(_address, (uint8_t)1);
return Wire.read();
}
// read datasheet
uint8_t AD5245::shutDown()
{
uint8_t cmd = AD5245_SHUTDOWN;
return send(cmd, 0);
}
//////////////////////////////////////////////////////////
//
// PRIVATE
//
uint8_t AD5245::send(const uint8_t cmd, const uint8_t value)
{
Wire.beginTransmission(_address);
Wire.write(cmd);
Wire.write(value);
return Wire.endTransmission();
}
// -- END OF FILE --

57
libraries/AD5245/AD5245.h Normal file
View File

@ -0,0 +1,57 @@
#pragma once
//
// FILE: AD5245.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Arduino library for I2C digital potentiometer AD5245.
// DATE: 2022-07-31
// URL: https://github.com/RobTillaart/AD5245
// experimental - to be tested - use at own risk
#include "Arduino.h"
#include "Wire.h"
#define AD5245_LIB_VERSION (F("0.1.0"))
#define AD5245_OK 0
#define AD5245_ERROR 100
class AD5245
{
public:
explicit AD5245(const uint8_t address, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl);
#endif
bool begin();
bool isConnected();
uint8_t reset();
uint8_t read(); // read from cache (fast!)
uint8_t readDevice(); // read from device
uint8_t write(const uint8_t value);
// experimental - to be tested - use at own risk
uint8_t shutDown();
private:
uint8_t send(const uint8_t cmd, const uint8_t value);
uint8_t _address;
uint8_t _lastValue;
TwoWire* _wire;
};
// -- END OF FILE --

21
libraries/AD5245/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2022 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,66 @@
[![Arduino CI](https://github.com/RobTillaart/AD5245/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/AD5245/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/AD5245/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/AD5245/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/AD5245/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AD5245/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AD5245.svg?maxAge=3600)](https://github.com/RobTillaart/AD5245/releases)
# AD5245
Arduino library for I2C digital potentiometer AD5245.
The library is experimental and not tested yet.
## Description
The AD5245 is a digital potentiometer which comes in 10K, 100K and 1M
and can be set in 256 steps.
An important property of the device is that it defaults
to the mid position at startup.
#### Related libraries
This library is based upon the AD524X library, and triggered by this issue:
- https://github.com/RobTillaart/AD524X/issues/11
## I2C address
The AD5245 has one address line to configure the I2C address.
| Addr(dec)| Addr(Hex) | AD0 |
|:--------:|:---------:|:----:|
| 44 | 0x2C | GND |
| 45 | 0x2D | +5V |
## Interface
The library has a number of functions which are all quite straightforward.
One can get / set the value of the potentiometer.
- **AD5245(uint8_t address, TwoWire \*wire = &Wire)** constructor
- **bool begin(uint8_t sda, uint8_t scl)** ESP32 a.o initializing of Wire.
- **bool begin()** for UNO.
- **bool isConnected()** See if address set in constructor is on the bus.
- **uint8_t reset()** sets potentiometer to midpoint = 128. (startup default)
- **uint8_t write(uint8_t value)** set to value 0 .. 255.
- **uint8_t read(uint8_t rdac)** read value from cache.
- **uint8_t readDevice(uint8_t rdac)** read value from device.
- **uint8_t shutDown()** check datasheet, not tested yet, use at own risk.
## Operation
The examples show the basic working of the functions.
## Future
- sync with AD520X library
- sync with AD524X library

View File

@ -0,0 +1,48 @@
//
// FILE: AD5245_followA0.ino
// AUTHOR: Rob Tillaart
// PURPOSE: AD5245 demo program
// URL: https://github.com/RobTillaart/AD5245
//
#include "AD5245.h"
AD5245 AD(0x2C); // AD0 == GND
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.println();
Serial.println(AD5245_LIB_VERSION);
Wire.begin();
Wire.setClock(400000);
bool b = AD.begin();
Serial.println(b ? "true" : "false");
Serial.println(AD.isConnected());
}
void loop()
{
int x = analogRead(A0);
Serial.print(x);
Serial.print('\t');
Serial.print(x/4);
int rv = AD.write(x/4);
Serial.print('\t');
Serial.println(rv);
delay(100);
}
// -- END OF FILE --

View File

@ -0,0 +1,41 @@
//
// FILE: AD5245_isConnected.ino
// AUTHOR: Rob Tillaart
// PURPOSE: AD5245 demo program
// URL: https://github.com/RobTillaart/AD5245
//
#include "AD5245.h"
AD5245 AD(0x2C); // AD0 == GND
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.println();
Serial.println(AD5245_LIB_VERSION);
Wire.begin();
Wire.setClock(400000);
bool b = AD.begin();
Serial.println(b ? "true" : "false");
Serial.println(AD.isConnected());
Serial.println("\nDone...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,44 @@
//
// FILE: AD5245_sawtooth.ino
// AUTHOR: Rob Tillaart
// PURPOSE: AD5245 demo program
// URL: https://github.com/RobTillaart/AD5245
//
#include "AD5245.h"
AD5245 AD(0x2C); // AD0 == GND
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.println();
Serial.println(AD5245_LIB_VERSION);
Wire.begin();
Wire.setClock(400000);
bool b = AD.begin();
Serial.println(b ? "true" : "false");
Serial.println(AD.isConnected());
}
void loop()
{
for (int val = 0; val < 255; val++)
{
AD.write(val);
Serial.println(val);
delay(20);
}
}
// -- END OF FILE --

View File

@ -0,0 +1,53 @@
//
// FILE: AD5245_write_read.ino
// AUTHOR: Rob Tillaart
// PURPOSE: AD5245 demo program
// URL: https://github.com/RobTillaart/AD5245
//
#include "AD5245.h"
AD5245 AD(0x2C); // AD0 == GND
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.println();
Serial.println(AD5245_LIB_VERSION);
Wire.begin();
Wire.setClock(400000);
bool b = AD.begin();
Serial.println(b ? "true" : "false");
Serial.println(AD.isConnected());
}
void loop()
{
Serial.println("\nwrite\tread\treadBack");
for (int val = 0; val < 255; val++)
{
Serial.print(val);
AD.write(val);
delay(100);
Serial.print('\t');
Serial.print(AD.read());
delay(100);
Serial.print('\t');
Serial.println(AD.readDevice());
delay(100);
}
delay(1000);
}
// -- END OF FILE --

View File

@ -0,0 +1,23 @@
# Syntax Colouring Map For AD5245
# Data types (KEYWORD1)
AD5245 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isCOnnected KEYWORD2
reset KEYWORD2
read KEYWORD2
readDevice KEYWORD2
write KEYWORD2
shutDown KEYWORD2
# Constants (LITERAL1)
AD5245_LIB_VERSION LITERAL1
AS5245_OK LITERAL1
AS5245_ERROR LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "AD5245",
"keywords": "I2C,digital,PotentioMeter, AD5245",
"description": "Library to control digital potentiometer AD5245",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/AD5245"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
"headers": "AD5245.h"
}

View File

@ -0,0 +1,11 @@
name=AD5245
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library for AD5245 digital potentiometer.
paragraph=Library to control digital potentiometer AD5245.
category=Signal Input/Output
url=https://github.com/RobTillaart/AD5245
architectures=*
includes=AD5245.h
depends=

View File

@ -0,0 +1,84 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// DATE: 2020-12-03
// PURPOSE: unit tests for I2C digital PotentioMeter AD5241 AD5242
// https://github.com/RobTillaart/AD524X
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// 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 "AD5245.h"
unittest_setup()
{
fprintf(stderr, "\nAD5245_LIB_VERSION %s\n", (char*) AD5245_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constants)
{
assertEqual(000, AD5245_OK);
assertEqual(100, AD5245_ERROR);
}
unittest(test_constructors)
{
Wire.begin();
AD5245 AD(0x2C);
AD.begin();
assertEqual(128, AD.read());
}
unittest(test_reset)
{
Wire.begin();
AD5245 AD(0x2C);
assertEqual(128, AD.read());
AD.write(0);
assertEqual(0, AD.read());
AD.reset();
assertEqual(128, AD.read());
}
unittest(test_write_read)
{
Wire.begin();
AD5245 AD(0x2C);
assertEqual(128, AD.read());
AD.write(42);
assertEqual(42, AD.read());
}
unittest_main()
// --------