0.3.1 AD520X

This commit is contained in:
rob tillaart 2023-01-10 20:37:57 +01:00
parent aa20622e3c
commit fffe2ad15b
12 changed files with 197 additions and 78 deletions

View File

@ -6,7 +6,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update

View File

@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

View File

@ -10,7 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:

View File

@ -2,12 +2,11 @@
// FILE: AD520X.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-24
// VERSION: 0.3.0
// VERSION: 0.3.1
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers
// (+ AD8400, AD8402, AD8403)
// URL: https://github.com/RobTillaart/AD520X
//
// HISTORY: see CHANGELOG.md
#include "AD520X.h"
@ -70,21 +69,10 @@ void AD520X::begin(uint8_t value)
}
#if defined(ESP32)
void AD520X::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
_dataOut = mosi;
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
mySPI->end(); // disable SPI and restart
mySPI->begin(clk, miso, mosi, select);
}
#endif
/////////////////////////////////////////////////////////////////////////////
//
// SET VALUE
//
bool AD520X::setValue(uint8_t pm, uint8_t value)
{
if (pm >= _pmCount) return false;
@ -93,6 +81,14 @@ bool AD520X::setValue(uint8_t pm, uint8_t value)
return true;
}
uint8_t AD520X::getValue(uint8_t pm)
{
if (pm >= _pmCount) return 0;
return _value[pm];
}
// STEREO same value
bool AD520X::setValue(uint8_t pmA, uint8_t pmB, uint8_t value)
{
@ -115,13 +111,25 @@ void AD520X::setAll(uint8_t value)
}
uint8_t AD520X::getValue(uint8_t pm)
void AD520X::setGroupValue(uint8_t mask, uint8_t value)
{
if (pm >= _pmCount) return 0;
return _value[pm];
uint8_t m = 0x01;
for (uint8_t pm = 0; pm < _pmCount; pm++ )
{
if (mask & m)
{
_value[pm] = value;
updateDevice(pm, value);
}
m <<= 1;
}
}
/////////////////////////////////////////////////////////////////////////////
//
// SET PERCENTAGE
//
bool AD520X::setPercentage(uint8_t pm, float percentage)
{
if ((percentage < 0) || (percentage > 100.0)) return false;
@ -129,14 +137,6 @@ bool AD520X::setPercentage(uint8_t pm, float percentage)
}
// STEREO same percentage
bool AD520X::setPercentage(uint8_t pmA, uint8_t pmB, float percentage)
{
if ((percentage < 0) || (percentage > 100.0)) return false;
return setValue(pmA, pmB, round(percentage * (255.0 / 100.0)));
}
float AD520X::getPercentage(uint8_t pm)
{
if (pm >= _pmCount) return 0;
@ -146,6 +146,32 @@ float AD520X::getPercentage(uint8_t pm)
}
// STEREO same percentage
bool AD520X::setPercentage(uint8_t pmA, uint8_t pmB, float percentage)
{
if ((percentage < 0) || (percentage > 100.0)) return false;
return setValue(pmA, pmB, round(percentage * (255.0 / 100.0)));
}
void AD520X::setGroupPercentage(uint8_t mask, float percentage)
{
uint8_t m = 0x01;
for (uint8_t pm = 0; pm < _pmCount; pm++ )
{
if (mask & m)
{
setValue(pm, round(percentage * (255.0 / 100.0)));
}
m <<= 1;
}
}
/////////////////////////////////////////////////////////////////////////////
//
// OTHER
//
void AD520X::reset(uint8_t value)
{
digitalWrite(_reset, LOW);
@ -154,6 +180,34 @@ void AD520X::reset(uint8_t value)
}
uint8_t AD520X::pmCount()
{
return _pmCount;
}
void AD520X::powerOn()
{
digitalWrite(_shutdown, HIGH);
}
void AD520X::powerOff()
{
digitalWrite(_shutdown, LOW);
}
bool AD520X::isPowerOn()
{
return digitalRead(_shutdown) == HIGH;
}
/////////////////////////////////////////////////////////////////////////////
//
// SPI
//
void AD520X::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
@ -161,6 +215,59 @@ void AD520X::setSPIspeed(uint32_t speed)
};
uint32_t AD520X::getSPIspeed()
{
return _SPIspeed;
}
bool AD520X::usesHWSPI()
{
return _hwSPI;
};
#if defined(ESP32)
void AD520X::selectHSPI()
{
_useHSPI = true;
}
void AD520X::selectVSPI()
{
_useHSPI = false;
}
bool AD520X::usesHSPI()
{
return _useHSPI;
}
bool AD520X::usesVSPI()
{
return !_useHSPI;
}
void AD520X::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
_dataOut = mosi;
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
mySPI->end(); // disable SPI and restart
mySPI->begin(clk, miso, mosi, select);
}
#endif
/////////////////////////////////////////////////////////////////////////////
//
// PROTECTED

View File

@ -3,7 +3,7 @@
// FILE: AD520X.h
// AUTHOR: Rob Tillaart
// DATE: 2020-07-24
// VERSION: 0.3.0
// VERSION: 0.3.1
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers
// (+ AD8400, AD8402, AD8403)
// URL: https://github.com/RobTillaart/AD520X
@ -15,7 +15,7 @@
#include "SPI.h"
#define AD520X_LIB_VERSION (F("0.3.0"))
#define AD520X_LIB_VERSION (F("0.3.1"))
#ifndef AD520X_MIDDLE_VALUE
@ -30,51 +30,60 @@ public:
void begin(uint8_t value = AD520X_MIDDLE_VALUE);
// MONO / SINGLE
bool setValue(uint8_t pm = 0, uint8_t value = AD520X_MIDDLE_VALUE);
uint8_t getValue(uint8_t pm = 0);
// STEREO / DOUBLE
bool setValue(uint8_t pmA, uint8_t pmB, uint8_t value);
// GROUP
void setAll(uint8_t value = AD520X_MIDDLE_VALUE);
uint8_t getValue(uint8_t pm = 0);
void setGroupValue(uint8_t mask, uint8_t value = AD520X_MIDDLE_VALUE);
// MONO / SINGLE
bool setPercentage(uint8_t pm = 0, float percentage = 50);
float getPercentage(uint8_t pm = 0);
// STEREO / DOUBLE
bool setPercentage(uint8_t pmA, uint8_t pmB, float percentage);
float getPercentage(uint8_t pm = 0);
// GROUP
void setGroupPercentage(uint8_t mask, float percentage);
void reset(uint8_t value = AD520X_MIDDLE_VALUE);
uint8_t pmCount() { return _pmCount; };
uint8_t pmCount();
void powerOn();
void powerOff();;
bool isPowerOn();
void powerOn() { digitalWrite(_shutdown, HIGH); };
void powerOff() { digitalWrite(_shutdown, LOW); };
bool isPowerOn() { return digitalRead(_shutdown) == HIGH; };
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
uint32_t getSPIspeed();
// debugging
bool usesHWSPI() { return _hwSPI; };
bool usesHWSPI();
// ESP32 specific
#if defined(ESP32)
void selectHSPI() { _useHSPI = true; };
void selectVSPI() { _useHSPI = false; };
bool usesHSPI() { return _useHSPI; };
bool usesVSPI() { return !_useHSPI; };
void selectHSPI();
void selectVSPI();
bool usesHSPI();
bool usesVSPI();
// to overrule ESP32 default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
protected:
uint8_t _dataOut;
uint8_t _clock;
uint8_t _select;
uint8_t _reset;
uint8_t _shutdown;
bool _hwSPI = 3;
bool _hwSPI;
uint32_t _SPIspeed = 16000000;
uint8_t _value[6];

View File

@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.1] - 2023-01-10
- update GitHub actions
- update license
- add setGroupValue(mask, value)
- add setGroupPercentage(mask, percentage)
- move all code to .cpp file
## [0.3.0] - 2022-10-12
- Fix 13 shutdown / reset. (kudos to Ale11Re)
- Fix 14 wrong SPI mode. (kudos to Ale11Re)

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020-2022 Rob Tillaart
Copyright (c) 2020-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

View File

@ -72,9 +72,10 @@ Returns true if successful, false if not.
- **void setAll(uint8_t value = 128)** set all potentiometers to the same value e.g. 0 or max or mid value.
Can typically be used for **mute**.
- **uint8_t getValue(uint8_t pm = 0)** returns the last set value of a specific potentiometer.
- **setGroupValue(mask, value)** bit mask to set 0..8 channels in one call.
#### percentage
#### Percentage
- **bool setPercentage(uint8_t pm = 0, float percentage = 50)** similar to setValue, percentage from 0..100%
Returns true when successful, false if not.
@ -82,7 +83,7 @@ Returns true when successful, false if not.
Note, no default value.
Returns true when successful, false if not.
- **float getPercentage(uint8_t pm = 0)** return the value of potentiometer pm as percentage.
- **setGroupPercentage(mask, value)** bit mask to set 0..8 channels in one call.
The library has defined **#define AD520X_MIDDLE_VALUE 128**
@ -130,27 +131,12 @@ See examples.
## Future
#### 0.3.1
- **setGroupValue(mask, value)** bit mask to set 0..8 channels in one call
- loop over mask ?
- **setGroupPercentage(mask, value)** bit mask to set 0..8 channels in one call
- wrapper
- move all code to .cpp file
#### Must
#### Should
- **void setInvert(uint8_t pm)** invert flag per potentiometer.
- 0..255 -> 255..0
- 1 uint8_t can hold 8 flags
- will slow performance.
- how does this work with **stereo** functions.
- at what level should invert work.
- **bool getInvert(uint8_t pm)**
- extend unit tests
#### Could (only if requested.)
@ -159,6 +145,13 @@ See examples.
- **setSWSPIdelay()** to tune software SPI?
- bit delay / not byte delay
- unit microseconds
- **void setInvert(uint8_t pm)** invert flag per potentiometer.
- 0..255 -> 255..0
- 1 uint8_t can hold 8 flags
- **will slow performance**
- how does this work with **stereo** functions.
- at what level should invert work.
- **bool getInvert(uint8_t pm)**
#### Wont

View File

@ -14,9 +14,11 @@ begin KEYWORD2
setValue KEYWORD2
setAll KEYWORD2
getValue KEYWORD2
setGroupValue KEYWORD2
setPercentage KEYWORD2
getPercentage KEYWORD2
setGroupPercentage KEYWORD2
reset KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/AD520X.git"
},
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=AD520X
version=0.3.0
version=0.3.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for SPI AD5204 and AD5206 digital potentiometers

View File

@ -156,4 +156,4 @@ unittest(test_pm_count)
unittest_main()
// --------
// -- END OF FILE --