Skip to content

Commit

Permalink
Fix #14, rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Dec 19, 2023
1 parent 2fbe329 commit 98e5240
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 47 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.0] - 2023-12-19
- Fix #14, support for Arduino ESP32 S3 - breaking change
- update readme.md
- update examples
- minor edits

----

## [0.3.0] - 2023-12-10
- update API, begin
- add **uint8_t getADdress()**
- add **uint8_t getAddress()**
- update readme.md
- update examples
- minor edits
Expand Down
18 changes: 9 additions & 9 deletions PCF8591.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: PCF8591.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-03-12
// VERSION: 0.3.0
// VERSION: 0.4.0
// PURPOSE: Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
// URL: https://github.com/RobTillaart/PCF8591

Expand Down Expand Up @@ -37,7 +37,7 @@ bool PCF8591::begin(uint8_t val)
return false;
}
if (!isConnected()) return false;
analogWrite(val);
write(val);
return true;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ bool PCF8591::isINCREnabled()
}


uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
uint8_t PCF8591::read(uint8_t channel, uint8_t mode)
{
if (mode > 3)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
}


uint8_t PCF8591::analogRead4()
uint8_t PCF8591::read4()
{
// clear all except flags
// MODE == PCF8591_FOUR_SINGLE_CHANNEL.
Expand Down Expand Up @@ -172,28 +172,28 @@ uint8_t PCF8591::lastRead(uint8_t channel)
// comparator calls need testing.
int PCF8591::readComparator_01()
{
int8_t v = analogRead(0, 3);
int8_t v = read(0, 3);
return v;
}


int PCF8591::readComparator_23()
{
int8_t v = analogRead(1, 3);
int8_t v = read(1, 3);
return v;
}


int PCF8591::readComparator_03()
{
int8_t v = analogRead(0, 1);
int8_t v = read(0, 1);
return v;
}


int PCF8591::readComparator_13()
{
int8_t v = analogRead(1, 1);
int8_t v = read(1, 1);
return v;
}

Expand Down Expand Up @@ -221,7 +221,7 @@ bool PCF8591::isDACEnabled()
};


bool PCF8591::analogWrite(uint8_t value)
bool PCF8591::write(uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(_control);
Expand Down
14 changes: 7 additions & 7 deletions PCF8591.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: PCF8591.h
// AUTHOR: Rob Tillaart
// DATE: 2020-03-12
// VERSION: 0.3.0
// VERSION: 0.4.0
// PURPOSE: Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
// URL: https://github.com/RobTillaart/PCF8591

Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define PCF8591_LIB_VERSION (F("0.3.0"))
#define PCF8591_LIB_VERSION (F("0.4.0"))

#define PCF8591_OK 0x00
#define PCF8591_PIN_ERROR 0x81
Expand Down Expand Up @@ -45,12 +45,12 @@ class PCF8591
void disableINCR();
bool isINCREnabled();

// analogRead() returns the value.
// read() returns the value.
// mode 0 = PCF8591_FOUR_SINGLE_CHANNEL
uint8_t analogRead(uint8_t channel, uint8_t mode = 0);
// analogRead4() returns PCF8591_OK or an error code.
uint8_t analogRead4();
// access the 4 channels read with analogRead4()
uint8_t read(uint8_t channel, uint8_t mode = 0);
// read4() returns PCF8591_OK or an error code.
uint8_t read4();
// access the 4 channels read with read4()
uint8_t lastRead(uint8_t channel);

// datasheet par 8.2 figure 4
Expand Down
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,38 @@ After some time it was getting pretty hot and it broke down.
So overclocking is fun but **not recommended**.

PCF8591 has one 8 bit ADC on board for 4 channels. The ADC is 8 bit and quite fast.
At 100 KHz one gets \> 2000 reads per second for **analogRead()** and
\> 2000 writes per second for **analogWrite()**.
At 100 KHz one gets \> 2000 reads per second for **read()** and
\> 2000 writes per second for **write()**.
Note that most time is probably spend on I2C communication.

The auto-increment functionality is used in the **analogRead4()** function.
The auto-increment functionality is used in the **read4()** function.
The library only supports it for the mode 0 (plain ADC, no differential).
The **lastRead()** function is needed to get access to the values.
First tests shows it is 2.6 x faster than 4 individual reads.


#### 0.4.0 Breaking change

The version 0.4.0 has breaking changes in the interface.
The rationale is that the programming environment of the **Arduino ESP32 S3**
board uses a remapping by means of the include file **io_pin_remap.h**.
This file remaps the pins of several core Arduino functions.
The remapping is implemented by #define macros and these implement "hard" text
replacements without considering context.
The effect is that methods from this class (and several others) which have the same
name as those Arduino core functions will be remapped into something not working.

The following library functions have been renamed:

| old name | new name | notes |
|:-----------------|:-------------|:--------|
| analogRead() | read() |
| analogWrite() | write() |
| pinMode() | pinMode1() |
| digitalRead() | read1() |
| digitalWrite() | write1() |


#### 0.3.0 Breaking change

Version 0.3.0 introduced a breaking change.
Expand Down Expand Up @@ -69,24 +91,24 @@ Returns **true** if successful.

The PCF8591 has four 8 bit ADC channels. Values = 0..255.

- **void enableINCR()** used in analogRead4().
- **void enableINCR()** used in read4().
Could become private in the future.
- **void disableINCR()** idem.
- **bool isINCREnabled()** idem.
- **uint8_t analogRead(uint8_t channel, uint8_t mode = 0)** returns value of the analogue channel.
- **uint8_t read(uint8_t channel, uint8_t mode = 0)** returns value of the analogue channel.
Return 0 in case of an error, so check **lastError()** to be verify validity.
Default mode is PCF8591_FOUR_SINGLE_CHANNEL, see table below.
For details comparator modes see datasheet figure 4 page 6.
- **uint8_t analogRead4()** read all 4 channels in one call.
- **uint8_t read4()** read all 4 channels in one call.
Works in PCF8591_FOUR_SINGLE_CHANNEL mode only.
Uses **enableINCR()** to do that efficiently.
It is about 2.6 times faster than 4 individual **analogRead()** calls.
It is about 2.6 times faster than 4 individual **read()** calls.
The latter allows for optimized timing per channel and the order
in which the channels are read.
Use **lastRead()** to access the 4 values.
Returns **PCF8591_OK** or an error code if appropriate.
- **uint8_t lastRead(uint8_t channel)** get last read value from cache.
This cache is filled both by **analogRead()** and **analogRead4()**.
This cache is filled both by **read()** and **read4()**.
See example sketch.


Expand All @@ -101,13 +123,13 @@ See example sketch.
#### Comparator

Since 0.2.0 four direct comparator calls are added to make the code more explicit.
These four calls are in fact wrappers around the **analogRead()**.
These four calls are in fact wrappers around the **read()**.
These still need to be tested as I had no hardware available.

- **int readComparator_01()** == analogRead(channel = 0, mode = 3);
- **int readComparator_23()** == analogRead(channel = 1, mode = 3);
- **int readComparator_03()** == analogRead(channel = 0, mode = 1);
- **int readComparator_13()** == analogRead(channel = 1, mode = 1);
- **int readComparator_01()** == read(channel = 0, mode = 3);
- **int readComparator_23()** == read(channel = 1, mode = 3);
- **int readComparator_03()** == read(channel = 0, mode = 1);
- **int readComparator_13()** == read(channel = 1, mode = 1);

The values of the comparator calls are cached and can be accessed with **lastRead()**.
Be sure to select the right channel for **lastRead()**.
Expand All @@ -122,7 +144,7 @@ The PCF8591 has one 8 bit DAC. output value 0..255 == 0..Vref Volts (datasheet).
- **void enableDAC()** switch on the analogue output.
- **void disableDAC()** switch off the analogue output (high impedance). Sort of energy saving mode.
- **bool isDACEnabled()** check the modus operandi.
- **bool analogWrite(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage.
- **bool write(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage.
Note, this is a real voltage not a PWM signal like **analogWrite()** on an UNO.
- **uint8_t lastWrite()** get last value written (from cache).

Expand Down
7 changes: 3 additions & 4 deletions examples/PCF8591_demo/PCF8591_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void test_DAC()
{
uint8_t val = 127 + 127 * sin(i * 0.01);
Serial.println(val);
dev.analogWrite(val);
dev.write(val);
delay(1); // just to slow the effect
}
dev.disableDAC();
Expand All @@ -61,15 +61,15 @@ void test_DAC()

void test_ADC_mode(uint8_t mode)
{
uint8_t channels[] = {4, 3, 3, 2 }; // channels per mode
uint8_t channels[] = {4, 3, 3, 2 }; // channels per mode
Serial.println(__FUNCTION__);
Serial.println("--------------");
Serial.println("CH0\tCH1\tCH2\tCH3");
for (int m = 0; m < 20; m++)
{
for (uint8_t i = 0; i < channels[mode]; i++)
{
Serial.print(dev.analogRead(i, mode));
Serial.print(dev.read(i, mode));
Serial.print('\t');
}
Serial.println();
Expand All @@ -79,4 +79,3 @@ void test_ADC_mode(uint8_t mode)


// -- END OF FILE --

Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ void setup()
delay(100);

Serial.print("POR 1st read:\t");
Serial.println(dev.analogRead(3));
Serial.println(dev.read(3));
Serial.println();

//////////////////////////////////////////////////////

Serial.println("Read 4 one by one");
for (uint8_t i = 0; i < 4; i++)
{
Serial.print(dev.analogRead(i));
Serial.print(dev.read(i));
Serial.print('\t');
}
delay(10);

start = micros();
for (uint8_t i = 0; i < 4; i++)
{
dev.analogRead(i);
dev.read(i);
}
stop = micros();
dura2 = stop - start;
Expand All @@ -59,7 +59,7 @@ void setup()

Serial.println("Read 4 with auto increment");
start = micros();
dev.analogRead4();
dev.read4();
stop = micros();
for (uint8_t i = 0; i < 4; i++)
{
Expand Down
12 changes: 6 additions & 6 deletions examples/PCF8591_performance/PCF8591_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void loop()

void test1()
{
Serial.println(F("| - Wire clock KHz - | - analogWrite() us - | - analogRead() us - |"));
Serial.println(F("| - Wire clock KHz - | - write() us - | - read() us - |"));
Serial.println(F("|:----:|:----:|:----:|"));
for (uint8_t i = 1; i < 14; i++)
{
Expand All @@ -69,7 +69,7 @@ void test1()

void test2()
{
Serial.println(F("| - Wire clock KHz - | - analogWrite() OK% - | - analogRead() OK% - |"));
Serial.println(F("| - Wire clock KHz - | - write() OK% - | - read() OK% - |"));
Serial.println(F("|:----:|:----:|:----:|"));
for (uint8_t i = 1; i < 14; i++)
{
Expand All @@ -94,7 +94,7 @@ void test_DAC()
for (int i = 0; i < 1000; i++)
{
uint8_t val = i % 127;
dev.analogWrite(val);
dev.write(val);
}
stop = micros();
dev.disableDAC();
Expand All @@ -109,7 +109,7 @@ void test_ADC()
start = micros();
for (int i = 0; i < 1000; i++)
{
x = dev.analogRead(2);
x = dev.read(2);
}
stop = micros();
Serial.print(" | ");
Expand All @@ -125,7 +125,7 @@ void test_DAC_error()
for (int i = 0; i < 1000; i++)
{
uint8_t val = i % 127;
dev.analogWrite(val);
dev.write(val);
if (dev.lastError() == PCF8591_OK) perc += 0.1;
}
dev.disableDAC();
Expand All @@ -140,7 +140,7 @@ void test_ADC_error()
volatile uint8_t x = 0;
for (int i = 0; i < 1000; i++)
{
x = dev.analogRead(2);
x = dev.read(2);
if (dev.lastError() == PCF8591_OK) perc += 0.1;
}
Serial.print(" | ");
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/PCF8591.git"
},
"version": "0.3.0",
"version": "0.4.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PCF8591
version=0.3.0
version=0.4.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=PCF8591 library for Arduino. Supports multiple I2C WireN bus.
Expand Down

0 comments on commit 98e5240

Please sign in to comment.